Пример #1
0
        public async Task Vehicle_get_information_successful()
        {
            if (_vehicle.Status != "online")
            {
                int wakeUpCounter = 4;
                while (true)
                {
                    var wakeUpResponse = await _teslaApi.WakeUpVehicleAsync(_vehicle.Id, 10);

                    await Task.Delay(TimeSpan.FromSeconds(5));

                    if (wakeUpResponse.Status == "online")
                    {
                        break;
                    }
                    wakeUpCounter--;
                    if (wakeUpCounter < 0)
                    {
                        throw new Exception("Failed to wake up car");
                    }
                }
            }

            var vehicleData = await _teslaApi.GetVehicleDataAsync(_vehicle.Id);

            Assert.IsNotNull(vehicleData);
        }
Пример #2
0
        public override async Task <SourceData> GetData()
        {
            _logger.LogInformation("Requesting vehicle data");

            if (string.IsNullOrWhiteSpace(_harvesterSettings.TeslaClientId))
            {
                _logger.LogCritical("Missing Tesla Client ID from configuration!");
                return(null);
            }

            if (string.IsNullOrWhiteSpace(_harvesterSettings.TeslaClientSecret))
            {
                _logger.LogCritical("Missing Tesla Client Secret from configuration!");
                return(null);
            }

            if (string.IsNullOrWhiteSpace(_harvesterSettings.TeslaUsername))
            {
                _logger.LogCritical("Missing Tesla username from configuration!");
                return(null);
            }

            if (string.IsNullOrWhiteSpace(_harvesterSettings.TeslaPassword))
            {
                _logger.LogCritical("Missing Tesla password from configuration!");
                return(null);
            }

            var teslaApi      = new TeslaApi(_harvesterSettings.TeslaClientId, _harvesterSettings.TeslaClientSecret, "Dashboard", 60);
            var loginResponse = await teslaApi.LoginAsync(_harvesterSettings.TeslaUsername, _harvesterSettings.TeslaPassword);

            var allVehicles = await teslaApi.GetAllVehiclesAsync();

            var vehicle = allVehicles[0];

            // Only get data if car is awake. Don't wake it up because it would have negative impact on battery
            if (vehicle.Status != "online")
            {
                _logger.LogInformation($"Car is not online. Not retrieving data. Status is: '{vehicle.Status}'");
                // return new SourceData
                // {
                //  Id = Guid.NewGuid().ToString(),
                //  SourceId = "tesla",
                //  TimeStampUtc = DateTimeOffset.UtcNow,
                //  DataItems = new DataItem[] {
                //      new DataItem
                //      {
                //          Id = "vehicle_state",
                //          Type = DataItemType.Decimal,
                //          Label = "Fahrzeugzustand",
                //          Value = vehicle.Status
                //      }
                //  }
                // };

                return(null);
            }

            var vehicleData = await teslaApi.GetVehicleDataAsync(vehicle.Id);

            var sourceData = new SourceData
            {
                Id           = Guid.NewGuid().ToString(),
                SourceId     = "tesla",
                TimeStampUtc = DateTimeOffset.UtcNow,
                DataItems    = new DataItem[] {
                    new DataItem
                    {
                        Id    = "vehicle_state",
                        Type  = DataItemType.Decimal,
                        Label = "Fahrzeugzustand",
                        Value = vehicle.Status
                    },
                    new DataItem
                    {
                        Id    = "inside_temperature",
                        Type  = DataItemType.Decimal,
                        Label = "Temperatur Innenraum (Grad Celsius)",
                        Value = vehicleData.ClimateState.InsideTemperature
                    },
                    new DataItem
                    {
                        Id    = "climate_enabled",
                        Type  = DataItemType.Boolean,
                        Label = "Heizung/Klimaanlage an",
                        Value = vehicleData.ClimateState.ClimateEnabled
                    },
                    new DataItem
                    {
                        Id          = "heading",
                        Type        = DataItemType.Integer,
                        Label       = "Fahrtrichtung",
                        Value       = vehicleData.DriveState.Heading,
                        IsSensitive = true
                    },
                    new DataItem
                    {
                        Id          = "shift_state",
                        Type        = DataItemType.Text,
                        Label       = "Fahrzustand",
                        Value       = vehicleData.DriveState.ShiftState,
                        IsSensitive = true
                    },
                    new DataItem
                    {
                        Id          = "latitude",
                        Type        = DataItemType.Decimal,
                        Label       = "Breitengrad",
                        Value       = vehicleData.DriveState.Latitude,
                        IsSensitive = true
                    },
                    new DataItem
                    {
                        Id          = "longitude",
                        Type        = DataItemType.Decimal,
                        Label       = "Längengrad",
                        Value       = vehicleData.DriveState.Longitude,
                        IsSensitive = true
                    },
                    new DataItem
                    {
                        Id    = "charge_level_percent",
                        Type  = DataItemType.Integer,
                        Label = "Ladezustand (%)",
                        Value = vehicleData.ChargeState.ChargeLevelPercent
                    },
                    new DataItem
                    {
                        Id    = "charging_state",
                        Type  = DataItemType.Text,
                        Label = "Ladevorgang",
                        Value = vehicleData.ChargeState.ChargingState
                    },
                    new DataItem
                    {
                        Id    = "charge_power",
                        Type  = DataItemType.Decimal,
                        Label = "Ladeleistung (kW)",
                        Value = vehicleData.ChargeState.ChargePower
                    }
                }
            };

            return(sourceData);
        }