Ejemplo n.º 1
0
        public async Task InitTests()
        {
            var builder = new ConfigurationBuilder()
                          .AddUserSecrets <TestTeslaApi>();

            // Build now because we need configuration from appsettings.json to build the configuration for key vault.
            _configuration = builder.Build();
            _teslaApi      = new TeslaApi(_configuration["HarvesterSettings:TeslaClientId"], _configuration["HarvesterSettings:TeslaClientSecret"], "Dashboard", 60);
            var loginResponse = await _teslaApi.LoginAsync(_configuration["HarvesterSettings:TeslaUsername"], _configuration["HarvesterSettings:TeslaPassword"]);

            var allVehicles = await _teslaApi.GetAllVehiclesAsync();

            _vehicle = allVehicles[0];
        }
Ejemplo n.º 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);
        }