diff --git a/TeslaSolarCharger.SharedBackend/Contracts/IConstants.cs b/TeslaSolarCharger.SharedBackend/Contracts/IConstants.cs index 6734a9a5d..d1ae1829d 100644 --- a/TeslaSolarCharger.SharedBackend/Contracts/IConstants.cs +++ b/TeslaSolarCharger.SharedBackend/Contracts/IConstants.cs @@ -16,4 +16,5 @@ public interface IConstants string TokenRefreshUnauthorized { get; } string TokenMissingScopes { get; } string BackupZipBaseFileName { get; } + string FleetApiProxyNeeded { get; } } diff --git a/TeslaSolarCharger.SharedBackend/Values/Constants.cs b/TeslaSolarCharger.SharedBackend/Values/Constants.cs index 47c001dd3..9e7ed4890 100644 --- a/TeslaSolarCharger.SharedBackend/Values/Constants.cs +++ b/TeslaSolarCharger.SharedBackend/Values/Constants.cs @@ -15,4 +15,5 @@ public class Constants : IConstants public string FleetApiTokenRequested => "FleetApiTokenRequested"; public string TokenRefreshUnauthorized => "TokenRefreshUnauthorized"; public string TokenMissingScopes => "TokenMissingScopes"; + public string FleetApiProxyNeeded => "FleetApiProxyNeeded"; } diff --git a/TeslaSolarCharger/Server/Program.cs b/TeslaSolarCharger/Server/Program.cs index a8dc66a84..538bbef51 100644 --- a/TeslaSolarCharger/Server/Program.cs +++ b/TeslaSolarCharger/Server/Program.cs @@ -93,6 +93,13 @@ await configJsonService.UpdateAverageGridVoltage().ConfigureAwait(false); + var teslaFleetApiService = app.Services.GetRequiredService(); + var settings = app.Services.GetRequiredService(); + if (await teslaFleetApiService.IsFleetApiProxyNeededInDatabase().ConfigureAwait(false)) + { + settings.FleetApiProxyNeeded = true; + } + var jobManager = app.Services.GetRequiredService(); await jobManager.StartJobs().ConfigureAwait(false); } diff --git a/TeslaSolarCharger/Server/Services/Contracts/ITeslaFleetApiService.cs b/TeslaSolarCharger/Server/Services/Contracts/ITeslaFleetApiService.cs index 7ff3e9394..c233cbf6c 100644 --- a/TeslaSolarCharger/Server/Services/Contracts/ITeslaFleetApiService.cs +++ b/TeslaSolarCharger/Server/Services/Contracts/ITeslaFleetApiService.cs @@ -13,4 +13,5 @@ public interface ITeslaFleetApiService Task> TestFleetApiAccess(int carId); DtoValue IsFleetApiEnabled(); DtoValue IsFleetApiProxyEnabled(); + Task IsFleetApiProxyNeededInDatabase(); } diff --git a/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs b/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs index 3e72dde78..51f7c8b6d 100644 --- a/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs +++ b/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs @@ -327,12 +327,34 @@ await backendApiService.PostErrorInformation(nameof(TeslaFleetApiService), nameo await backendApiService.PostErrorInformation(nameof(TeslaFleetApiService), nameof(SendCommandToTeslaApi), $"Result of command request is false {fleetApiRequest.RequestUrl}, {contentData}. Response string: {responseString}") .ConfigureAwait(false); + if (responseString.Contains("unsigned_cmds_hardlocked")) + { + settings.FleetApiProxyNeeded = true; + //remove post after a few versions as only used for debugging + await backendApiService.PostErrorInformation(nameof(TeslaFleetApiService), nameof(SendCommandToTeslaApi), + "FleetAPI proxy needed set to true") + .ConfigureAwait(false); + if (!await IsFleetApiProxyNeededInDatabase().ConfigureAwait(false)) + { + teslaSolarChargerContext.TscConfigurations.Add(new TscConfiguration() + { + Key = constants.FleetApiProxyNeeded, + Value = true.ToString(), + }); + } + + } } } logger.LogDebug("Response: {responseString}", responseString); return teslaCommandResultResponse; } + public async Task IsFleetApiProxyNeededInDatabase() + { + return await teslaSolarChargerContext.TscConfigurations.AnyAsync(c => c.Key == constants.FleetApiProxyNeeded).ConfigureAwait(false); + } + private string GetFleetApiBaseUrl(TeslaFleetApiRegion region, bool useProxyBaseUrl) { if (useProxyBaseUrl && configurationWrapper.UseFleetApiProxy()) diff --git a/TeslaSolarCharger/Shared/Dtos/Contracts/ISettings.cs b/TeslaSolarCharger/Shared/Dtos/Contracts/ISettings.cs index ba3bedbc8..ed184c341 100644 --- a/TeslaSolarCharger/Shared/Dtos/Contracts/ISettings.cs +++ b/TeslaSolarCharger/Shared/Dtos/Contracts/ISettings.cs @@ -19,4 +19,5 @@ public interface ISettings int TeslaApiRequestCounter { get; set; } bool CrashedOnStartup { get; set; } string? StartupCrashMessage { get; set; } + bool FleetApiProxyNeeded { get; set; } } diff --git a/TeslaSolarCharger/Shared/Dtos/Settings/Settings.cs b/TeslaSolarCharger/Shared/Dtos/Settings/Settings.cs index 90a3683ea..e47f2876c 100644 --- a/TeslaSolarCharger/Shared/Dtos/Settings/Settings.cs +++ b/TeslaSolarCharger/Shared/Dtos/Settings/Settings.cs @@ -25,5 +25,7 @@ public Settings() public bool CrashedOnStartup { get; set; } public string? StartupCrashMessage { get; set; } + public bool FleetApiProxyNeeded { get; set; } + public List Cars { get; set; } } diff --git a/TeslaSolarCharger/Shared/Wrappers/ConfigurationWrapper.cs b/TeslaSolarCharger/Shared/Wrappers/ConfigurationWrapper.cs index e11bcba7c..515289e73 100644 --- a/TeslaSolarCharger/Shared/Wrappers/ConfigurationWrapper.cs +++ b/TeslaSolarCharger/Shared/Wrappers/ConfigurationWrapper.cs @@ -131,6 +131,10 @@ public bool UseFleetApi() public bool UseFleetApiProxy() { + if (settings.FleetApiProxyNeeded) + { + return true; + } var environmentVariableName = "UseFleetApiProxy"; var value = configuration.GetValue(environmentVariableName); return value;