Skip to content

Commit

Permalink
feat(TeslaFleetApiService): autoenable fleetapiproxyIfneeded
Browse files Browse the repository at this point in the history
  • Loading branch information
pkuehnel committed Jan 9, 2024
1 parent 388df04 commit 768d667
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions TeslaSolarCharger.SharedBackend/Contracts/IConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public interface IConstants
string TokenRefreshUnauthorized { get; }
string TokenMissingScopes { get; }
string BackupZipBaseFileName { get; }
string FleetApiProxyNeeded { get; }
}
1 change: 1 addition & 0 deletions TeslaSolarCharger.SharedBackend/Values/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public class Constants : IConstants
public string FleetApiTokenRequested => "FleetApiTokenRequested";
public string TokenRefreshUnauthorized => "TokenRefreshUnauthorized";
public string TokenMissingScopes => "TokenMissingScopes";
public string FleetApiProxyNeeded => "FleetApiProxyNeeded";
}
7 changes: 7 additions & 0 deletions TeslaSolarCharger/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@

await configJsonService.UpdateAverageGridVoltage().ConfigureAwait(false);

var teslaFleetApiService = app.Services.GetRequiredService<ITeslaFleetApiService>();
var settings = app.Services.GetRequiredService<ISettings>();
if (await teslaFleetApiService.IsFleetApiProxyNeededInDatabase().ConfigureAwait(false))
{
settings.FleetApiProxyNeeded = true;
}

var jobManager = app.Services.GetRequiredService<JobManager>();
await jobManager.StartJobs().ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public interface ITeslaFleetApiService
Task<DtoValue<bool>> TestFleetApiAccess(int carId);
DtoValue<bool> IsFleetApiEnabled();
DtoValue<bool> IsFleetApiProxyEnabled();
Task<bool> IsFleetApiProxyNeededInDatabase();
}
22 changes: 22 additions & 0 deletions TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> IsFleetApiProxyNeededInDatabase()
{
return await teslaSolarChargerContext.TscConfigurations.AnyAsync(c => c.Key == constants.FleetApiProxyNeeded).ConfigureAwait(false);
}

private string GetFleetApiBaseUrl(TeslaFleetApiRegion region, bool useProxyBaseUrl)
{
if (useProxyBaseUrl && configurationWrapper.UseFleetApiProxy())
Expand Down
1 change: 1 addition & 0 deletions TeslaSolarCharger/Shared/Dtos/Contracts/ISettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public interface ISettings
int TeslaApiRequestCounter { get; set; }
bool CrashedOnStartup { get; set; }
string? StartupCrashMessage { get; set; }
bool FleetApiProxyNeeded { get; set; }
}
2 changes: 2 additions & 0 deletions TeslaSolarCharger/Shared/Dtos/Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ public Settings()
public bool CrashedOnStartup { get; set; }
public string? StartupCrashMessage { get; set; }

public bool FleetApiProxyNeeded { get; set; }

public List<Car> Cars { get; set; }
}
4 changes: 4 additions & 0 deletions TeslaSolarCharger/Shared/Wrappers/ConfigurationWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ public bool UseFleetApi()

public bool UseFleetApiProxy()
{
if (settings.FleetApiProxyNeeded)
{
return true;
}
var environmentVariableName = "UseFleetApiProxy";
var value = configuration.GetValue<bool>(environmentVariableName);
return value;
Expand Down

0 comments on commit 768d667

Please sign in to comment.