Skip to content

Commit

Permalink
refactor(TeslaFleetApiService): extract HandleUnsignedCommands method
Browse files Browse the repository at this point in the history
  • Loading branch information
pkuehnel committed Jan 14, 2024
1 parent 82e601d commit d9fa31e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
19 changes: 19 additions & 0 deletions TeslaSolarCharger.Tests/Services/Server/TeslaFleetApiService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Newtonsoft.Json;
using System.Threading.Tasks;
using TeslaSolarCharger.Server.Dtos.TeslaFleetApi;
using Xunit;
using Xunit.Abstractions;

namespace TeslaSolarCharger.Tests.Services.Server;

public class TeslaFleetApiService(ITestOutputHelper outputHelper) : TestBase(outputHelper)
{
[Fact]
public async Task CanHandleUnsignedCommands()
{
var commandResult = JsonConvert.DeserializeObject<DtoGenericTeslaResponse<DtoVehicleCommandResult>>("{\"response\":{\"result\":false,\"reason\":\"unsigned_cmds_hardlocked\"}}");
Assert.NotNull(commandResult?.Response);
var fleetApiService = Mock.Create<TeslaSolarCharger.Server.Services.TeslaFleetApiService>();
await fleetApiService.HandleUnsignedCommands(commandResult.Response).ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@

namespace TeslaSolarCharger.Tests.Services.Server;

public class TeslaMateApiService : TestBase
public class TeslaMateApiService(ITestOutputHelper outputHelper) : TestBase(outputHelper)
{
public TeslaMateApiService(ITestOutputHelper outputHelper)
: base(outputHelper)
{
}

[Theory]
[InlineData(18, null, null, false)]
[InlineData(18, null, 19, true)]
Expand Down
40 changes: 23 additions & 17 deletions TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,29 +327,35 @@ 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 (string.Equals(vehicleCommandResult.Reason, "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(),
});
}

}
await HandleUnsignedCommands(vehicleCommandResult).ConfigureAwait(false);
}
}
logger.LogDebug("Response: {responseString}", responseString);
return teslaCommandResultResponse;
}

internal async Task HandleUnsignedCommands(DtoVehicleCommandResult vehicleCommandResult)
{
if (string.Equals(vehicleCommandResult.Reason, "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(),
});
await teslaSolarChargerContext.SaveChangesAsync().ConfigureAwait(false);
}

}
}

public async Task<bool> IsFleetApiProxyNeededInDatabase()
{
return await teslaSolarChargerContext.TscConfigurations.AnyAsync(c => c.Key == constants.FleetApiProxyNeeded).ConfigureAwait(false);
Expand Down

0 comments on commit d9fa31e

Please sign in to comment.