Skip to content

Commit

Permalink
Merge pull request #1062 from pkuehnel/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
pkuehnel authored Jan 9, 2024
2 parents d16bb07 + 4bad948 commit 82e601d
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Plugins.Modbus/Plugins.Modbus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<PackageReference Include="FluentModbus" Version="5.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
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 @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<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" />
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 @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<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.Settings.Configuration" Version="8.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion Plugins.Solax/Plugins.Solax.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
Expand Down
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";
}
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.4" />
<PackageReference Include="xunit" Version="2.6.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
22 changes: 22 additions & 0 deletions TeslaSolarCharger/Server/Helper/IgnorePropertiesResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using System.Reflection;

namespace TeslaSolarCharger.Server.Helper;

public class IgnorePropertiesResolver(IEnumerable<string> propNamesToIgnore) : DefaultContractResolver
{
private readonly HashSet<string> _ignoreProps = new(propNamesToIgnore);

protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);

if (property.PropertyName != null && _ignoreProps.Contains(property.PropertyName))
{
property.ShouldSerialize = _ => false;
}

return property;
}
}
9 changes: 8 additions & 1 deletion TeslaSolarCharger/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@

var tscConfigurationService = app.Services.GetRequiredService<ITscConfigurationService>();
var installationId = await tscConfigurationService.GetInstallationId().ConfigureAwait(false);
logger.LogTrace("Installation Id: {installationId}", installationId);
logger.LogInformation("Installation Id: {installationId}", installationId);

var chargingCostService = app.Services.GetRequiredService<IChargingCostService>();
await chargingCostService.DeleteDuplicatedHandleCharges().ConfigureAwait(false);
Expand All @@ -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
21 changes: 19 additions & 2 deletions TeslaSolarCharger/Server/Services/ChargingService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System.Runtime.CompilerServices;
using TeslaSolarCharger.Model.Contracts;
using TeslaSolarCharger.Server.Contracts;
using TeslaSolarCharger.Server.Helper;
using TeslaSolarCharger.Server.Services.ApiServices.Contracts;
using TeslaSolarCharger.Server.Services.Contracts;
using TeslaSolarCharger.Shared.Contracts;
Expand Down Expand Up @@ -98,8 +100,23 @@ public async Task SetNewChargingValues()
.ThenBy(c => c.Id)
.ToList();

_logger.LogDebug("Relevant cars: {@relevantCars}", relevantCars);
_logger.LogDebug("Irrelevant cars: {@irrlevantCars}", irrelevantCars);
if (_configurationWrapper.LogLocationData())
{
_logger.LogDebug("Relevant cars: {@relevantCars}", relevantCars);
_logger.LogDebug("Irrelevant cars: {@irrelevantCars}", irrelevantCars);
}
else
{
var jsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new IgnorePropertiesResolver(new[] { nameof(Car.CarState.Longitude), nameof(Car.CarState.Latitude) }),
};
var relevantCarsJson = JsonConvert.SerializeObject(relevantCars, jsonSerializerSettings);
_logger.LogDebug("Relevant cars: {relevantCarsJson}", relevantCarsJson);
var irrelevantCarsJson = JsonConvert.SerializeObject(irrelevantCars, jsonSerializerSettings);
_logger.LogDebug("Irrelevant cars: {irrelevantCarsJson}", irrelevantCarsJson);
}


if (relevantCarIds.Count < 1)
{
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();
}
23 changes: 22 additions & 1 deletion TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public async Task WakeUpCar(int carId)
var vin = await GetVinByCarId(carId).ConfigureAwait(false);
var result = await SendCommandToTeslaApi<DtoVehicleWakeUpResult>(vin, WakeUpRequest).ConfigureAwait(false);
await teslamateApiService.ResumeLogging(carId).ConfigureAwait(false);
//ToDo: Next line is never executed
await Task.Delay(TimeSpan.FromSeconds(20)).ConfigureAwait(false);
}

Expand Down Expand Up @@ -328,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 (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(),
});
}

}
}
}
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
9 changes: 8 additions & 1 deletion TeslaSolarCharger/Server/Services/TeslaMateMqttService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ public async Task ConnectMqttClient()
_mqttClient.ApplicationMessageReceivedAsync += e =>
{
var value = GetValueFromMessage(e.ApplicationMessage);
_logger.LogTrace("Car Id: {carId}, Topic: {topic}, Value: {value}", value.CarId, value.Topic, value.Value);
if ((!_configurationWrapper.LogLocationData()) && (string.Equals(value.Topic, TopicLongitude) || string.Equals(value.Topic, TopicLatitude)))
{
_logger.LogTrace("Car Id: {carId}, Topic: {topic}, Value: xx.xxxxx", value.CarId, value.Topic);
}
else
{
_logger.LogTrace("Car Id: {carId}, Topic: {topic}, Value: {value}", value.CarId, value.Topic, value.Value);
}
UpdateCar(value);
return Task.CompletedTask;
};
Expand Down
2 changes: 1 addition & 1 deletion TeslaSolarCharger/Server/TeslaSolarCharger.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="MinVer" Version="4.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
1 change: 1 addition & 0 deletions TeslaSolarCharger/Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"BackendApiBaseUrl": "https://www.teslasolarcharger.de/api/",
"TeslaFleetApiBaseUrl": "https://www.teslasolarcharger.de/teslaproxy/",
"UseFleetApiProxy": false,
"LogLocationData": false,
"AwattarBaseUrl": "https://api.awattar.de/v1/marketdata",
"GridPriceProvider": {
"EnergyProvider": "FixedPrice",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@ public interface IConfigurationWrapper
string RestoreTempDirectory();
string ConfigFileDirectory();
string AutoBackupsZipDirectory();
bool LogLocationData();
}
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; }
}
11 changes: 11 additions & 0 deletions TeslaSolarCharger/Shared/Wrappers/ConfigurationWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,22 @@ public bool UseFleetApi()

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

public bool LogLocationData()
{
var environmentVariableName = "LogLocationData";
var value = configuration.GetValue<bool>(environmentVariableName);
return value;
}

public string BackendApiBaseUrl()
{
var environmentVariableName = "BackendApiBaseUrl";
Expand Down

0 comments on commit 82e601d

Please sign in to comment.