Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(TeslaFleetApiService): extract HandleUnsignedCommands method #1066

Merged
merged 3 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public void Calculates_Correct_Full_Speed_Charge_Durations(int minimumSoc, int?
};

var chargeTimeCalculationService = Mock.Create<TeslaSolarCharger.Server.Services.ChargeTimeCalculationService>();
Mock.Mock<IConstants>().Setup(c => c.MinimumSocDifference).Returns(2);
var chargeDuration = chargeTimeCalculationService.CalculateTimeToReachMinSocAtFullSpeedCharge(car);

var expectedTimeSpan = TimeSpan.FromSeconds(expectedTotalSeconds);
Expand Down
26 changes: 26 additions & 0 deletions TeslaSolarCharger.Tests/Services/Server/TeslaFleetApiService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Newtonsoft.Json;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using TeslaSolarCharger.Server.Dtos.TeslaFleetApi;
using Xunit;
using Xunit.Abstractions;

namespace TeslaSolarCharger.Tests.Services.Server;

[SuppressMessage("ReSharper", "UseConfigureAwaitFalse")]
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>();
var fleetApiProxyNeeded = await fleetApiService.IsFleetApiProxyNeededInDatabase();
Assert.False(fleetApiProxyNeeded);
await fleetApiService.HandleUnsignedCommands(commandResult.Response);
fleetApiProxyNeeded = await fleetApiService.IsFleetApiProxyNeededInDatabase();
Assert.True(fleetApiProxyNeeded);

}
}
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
4 changes: 4 additions & 0 deletions TeslaSolarCharger.Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
using TeslaSolarCharger.Server.MappingExtensions;
using TeslaSolarCharger.Shared.Contracts;
using TeslaSolarCharger.Shared.TimeProviding;
using TeslaSolarCharger.SharedBackend.Contracts;
using Xunit.Abstractions;
using Constants = TeslaSolarCharger.SharedBackend.Values.Constants;

namespace TeslaSolarCharger.Tests;

Expand Down Expand Up @@ -58,6 +60,7 @@ protected TestBase(

_fake = new AutoFake();
_fake.Provide<IMapperConfigurationFactory, MapperConfigurationFactory>();
_fake.Provide<IConstants, Constants>();
_fake.Provide<IDateTimeProvider>(new FakeDateTimeProvider(currentFakeTime));
_fake.Provide<IConfiguration>(configuration);

Expand All @@ -66,6 +69,7 @@ protected TestBase(
{
b.Register((_, _) => Context);
b.Register((_, _) => _fake.Resolve<IMapperConfigurationFactory>());
b.Register((_, _) => _fake.Resolve<IConstants>());
b.Register((_, _) => _fake.Resolve<IConfiguration>());
b.RegisterType<FakeDateTimeProvider>();
//b.Register((_, _) => _fake.Resolve<IDateTimeProvider>());
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
Loading