Skip to content

Commit

Permalink
Merge pull request #1067 from pkuehnel/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
pkuehnel authored Jan 20, 2024
2 parents 82e601d + e4d6983 commit 917d38b
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Plugins.Modbus/Plugins.Modbus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="FluentModbus" Version="5.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
Expand Down
2 changes: 1 addition & 1 deletion Plugins.SmaEnergymeter/Plugins.SmaEnergymeter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Quartz" Version="3.8.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Plugins.SolarEdge/Plugins.SolarEdge.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
Expand Down
4 changes: 2 additions & 2 deletions Plugins.Solax/Plugins.Solax.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="GraphQL.Client" Version="6.0.2" />
<PackageReference Include="GraphQL.Client.Serializer.SystemTextJson" Version="6.0.2" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
Expand Down
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
2 changes: 1 addition & 1 deletion TeslaSolarCharger.Tests/TeslaSolarCharger.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PackageReference Include="Moq.EntityFrameworkCore" Version="7.0.0.2" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.XUnit" Version="3.0.5" />
<PackageReference Include="xunit" Version="2.6.5" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
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
2 changes: 1 addition & 1 deletion TeslaSolarCharger/Client/TeslaSolarCharger.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.1" PrivateAssets="all" />
<PackageReference Include="MudBlazor" Version="6.12.0" />
</ItemGroup>

Expand Down
9 changes: 6 additions & 3 deletions TeslaSolarCharger/Server/Services/ChargingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ private double GetDistance(double longitude, double latitude, double otherLongit

public int CalculateAmpByPowerAndCar(int powerToControl, Car car)
{
_logger.LogTrace("{method}({powerToControl}, {carId})", nameof(CalculateAmpByPowerAndCar), powerToControl, car.Id);
return Convert.ToInt32(Math.Floor(powerToControl / ((double)(_settings.AverageHomeGridVoltage ?? 230) * car.CarState.ActualPhases)));
}

Expand Down Expand Up @@ -407,7 +408,7 @@ private async Task<int> ChangeCarAmp(Car car, int ampToChange, DtoValue<int> max
{
_logger.LogDebug("Charging should stop");
//Falls Ausschaltbefehl erst seit Kurzem
if (car.CarState.EarliestSwitchOff > _dateTimeProvider.Now())
if ((car.CarState.EarliestSwitchOff == default) || (car.CarState.EarliestSwitchOff > _dateTimeProvider.Now()))
{
_logger.LogDebug("Can not stop charging: earliest Switch Off: {earliestSwitchOff}",
car.CarState.EarliestSwitchOff);
Expand Down Expand Up @@ -533,9 +534,11 @@ private void UpdateShouldStartStopChargingSince(Car car)
var actualCurrent = car.CarState.ChargerActualCurrent ?? 0;
_logger.LogTrace("Actual current: {actualCurrent}", actualCurrent);
//This is needed because sometimes actual current is higher than last set amp, leading to higher calculated amp to set, than actually needed
if (actualCurrent > car.CarState.LastSetAmp)
var lastSetAmp = car.CarState.ChargerRequestedCurrent ?? car.CarState.LastSetAmp;
if (actualCurrent > lastSetAmp)
{
actualCurrent = car.CarState.LastSetAmp;
_logger.LogTrace("Actual current {actualCurrent} higher than last set amp {lastSetAmp}. Setting actual current as last set amp.", actualCurrent, lastSetAmp);
actualCurrent = lastSetAmp;
}
ampToSet += actualCurrent;
}
Expand Down
61 changes: 43 additions & 18 deletions TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Globalization;
using System.Net;
using System.Net.Http.Headers;
Expand Down Expand Up @@ -107,9 +108,27 @@ public async Task StopCharging(int carId)
public async Task SetAmp(int carId, int amps)
{
logger.LogTrace("{method}({carId}, {amps})", nameof(SetAmp), carId, amps);
var car = settings.Cars.First(c => c.Id == carId);
if (car.CarState.ChargerRequestedCurrent == amps)
{
logger.LogDebug("Correct charging amp already set.");
return;
}
var vin = await GetVinByCarId(carId).ConfigureAwait(false);
var commandData = $"{{\"charging_amps\":{amps}}}";
var result = await SendCommandToTeslaApi<DtoVehicleCommandResult>(vin, SetChargingAmpsRequest, commandData).ConfigureAwait(false);
if (amps < 5 && car.CarState.LastSetAmp >= 5
|| amps >= 5 && car.CarState.LastSetAmp < 5)
{
logger.LogDebug("Double set amp to be able to jump over or below 5A");
await Task.Delay(TimeSpan.FromSeconds(3)).ConfigureAwait(false);
result = await SendCommandToTeslaApi<DtoVehicleCommandResult>(vin, SetChargingAmpsRequest, commandData).ConfigureAwait(false);
}

if (result?.Response?.Result == true)
{
car.CarState.LastSetAmp = amps;
}
}

public async Task SetScheduledCharging(int carId, DateTimeOffset? chargingStartTime)
Expand Down Expand Up @@ -327,29 +346,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
6 changes: 3 additions & 3 deletions TeslaSolarCharger/Server/TeslaSolarCharger.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@

<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -50,7 +50,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="Quartz" Version="3.8.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion TeslaSolarCharger/Shared/TeslaSolarCharger.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Runtime.Caching" Version="8.0.0" />
Expand Down

0 comments on commit 917d38b

Please sign in to comment.