Skip to content

Commit

Permalink
Merge pull request #1853 from pkuehnel/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
pkuehnel authored Feb 19, 2025
2 parents 0c6b3e5 + d00e12c commit 94a553f
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 47 deletions.
7 changes: 7 additions & 0 deletions TeslaSolarCharger/Server/Scheduling/JobManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Options;
using Quartz;
using Quartz.Spi;
using TeslaSolarCharger.Server.Scheduling.Jobs;
Expand Down Expand Up @@ -52,6 +53,7 @@ public async Task StartJobs()
var errorDetectionJob = JobBuilder.Create<ErrorDetectionJob>().WithIdentity(nameof(ErrorDetectionJob)).Build();
var bleApiVersionDetectionJob = JobBuilder.Create<BleApiVersionDetectionJob>().WithIdentity(nameof(BleApiVersionDetectionJob)).Build();
var fleetTelemetryReconnectionJob = JobBuilder.Create<FleetTelemetryReconnectionJob>().WithIdentity(nameof(FleetTelemetryReconnectionJob)).Build();
var fleetTelemetryReconfigurationJob = JobBuilder.Create<FleetTelemetryReconfigurationJob>().WithIdentity(nameof(FleetTelemetryReconfigurationJob)).Build();

var currentDate = dateTimeProvider.DateTimeOffSetNow();
var chargingTriggerStartTime = currentDate.AddSeconds(5);
Expand Down Expand Up @@ -129,6 +131,10 @@ public async Task StartJobs()
.StartAt(currentDate.AddSeconds(10))
.WithSchedule(SimpleScheduleBuilder.RepeatSecondlyForever(61)).Build();

var fleetTelemetryReconfigurationTrigger = TriggerBuilder.Create().WithIdentity("fleetTelemetryReconfigurationTrigger")
.StartAt(currentDate.AddSeconds(13))
.WithSchedule(SimpleScheduleBuilder.RepeatHourlyForever(constants.FleetTelemetryReconfigurationBufferHours)).Build();

var random = new Random();
var hour = random.Next(0, 5);
var minute = random.Next(0, 59);
Expand All @@ -153,6 +159,7 @@ public async Task StartJobs()
{errorDetectionJob, new HashSet<ITrigger> {errorDetectionTrigger}},
{bleApiVersionDetectionJob, new HashSet<ITrigger> {bleApiVersionDetectionTrigger}},
{fleetTelemetryReconnectionJob, new HashSet<ITrigger> {fleetTelemetryReconnectionTrigger}},
{fleetTelemetryReconfigurationJob, new HashSet<ITrigger> {fleetTelemetryReconfigurationTrigger}},
};

if (!configurationWrapper.ShouldUseFakeSolarValues())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace TeslaSolarCharger.Server.Scheduling.Jobs;

[DisallowConcurrentExecution]
public class ErrorDetectionJob(ILogger<ErrorMessagingJob> logger, IErrorDetectionService service) : IJob
public class ErrorDetectionJob(ILogger<ErrorDetectionJob> logger, IErrorDetectionService service) : IJob
{
public async Task Execute(IJobExecutionContext context)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Quartz;
using TeslaSolarCharger.Server.Services.Contracts;

namespace TeslaSolarCharger.Server.Scheduling.Jobs;

public class FleetTelemetryReconfigurationJob(
ILogger<FleetTelemetryReconfigurationJob> logger,
IFleetTelemetryConfigurationService service) : IJob
{
public async Task Execute(IJobExecutionContext context)
{
logger.LogTrace("{method}({context})", nameof(Execute), context);
await service.ReconfigureAllCarsIfRequired();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
namespace TeslaSolarCharger.Server.Scheduling.Jobs;

public class FleetTelemetryReconnectionJob(
ILogger<BleApiVersionDetectionJob> logger,
IFleetTelemetryWebSocketService service,
IFleetTelemetryConfigurationService fleetTelemetryConfigurationService) : IJob
ILogger<FleetTelemetryReconnectionJob> logger,
IFleetTelemetryWebSocketService service) : IJob
{
public async Task Execute(IJobExecutionContext context)
{
logger.LogTrace("{method}({context})", nameof(Execute), context);
await fleetTelemetryConfigurationService.ReconfigureAllCarsIfRequired();
await service.ReconnectWebSocketsForEnabledCars();
}
}
14 changes: 3 additions & 11 deletions TeslaSolarCharger/Server/Scheduling/Jobs/MqttReconnectionJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,11 @@
namespace TeslaSolarCharger.Server.Scheduling.Jobs;

[DisallowConcurrentExecution]
public class MqttReconnectionJob : IJob
public class MqttReconnectionJob(ILogger<MqttReconnectionJob> logger, IMqttConnectionService service) : IJob
{
private readonly ILogger<MqttReconnectionJob> _logger;
private readonly IMqttConnectionService _service;

public MqttReconnectionJob(ILogger<MqttReconnectionJob> logger, IMqttConnectionService service)
{
_logger = logger;
_service = service;
}
public async Task Execute(IJobExecutionContext context)
{
_logger.LogTrace("{method}({context})", nameof(Execute), context);
await _service.ReconnectMqttServices().ConfigureAwait(false);
logger.LogTrace("{method}({context})", nameof(Execute), context);
await service.ReconnectMqttServices().ConfigureAwait(false);
}
}
14 changes: 3 additions & 11 deletions TeslaSolarCharger/Server/Scheduling/Jobs/PvValueJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,11 @@
namespace TeslaSolarCharger.Server.Scheduling.Jobs;

[DisallowConcurrentExecution]
public class PvValueJob : IJob
public class PvValueJob(ILogger<PvValueJob> logger, IPvValueService service) : IJob
{
private readonly ILogger<PvValueJob> _logger;
private readonly IPvValueService _service;

public PvValueJob(ILogger<PvValueJob> logger, IPvValueService service)
{
_logger = logger;
_service = service;
}
public async Task Execute(IJobExecutionContext context)
{
_logger.LogTrace("{method}({context})", nameof(Execute), context);
await _service.UpdatePvValues().ConfigureAwait(false);
logger.LogTrace("{method}({context})", nameof(Execute), context);
await service.UpdatePvValues().ConfigureAwait(false);
}
}
16 changes: 3 additions & 13 deletions TeslaSolarCharger/Server/Scheduling/Jobs/SpotPriceJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,11 @@
namespace TeslaSolarCharger.Server.Scheduling.Jobs;

[DisallowConcurrentExecution]
public class SpotPriceJob : IJob
public class SpotPriceJob(ILogger<SpotPriceJob> logger, ISpotPriceService service) : IJob
{
private readonly ILogger<SpotPriceJob> _logger;
private readonly ISpotPriceService _service;

public SpotPriceJob(ILogger<SpotPriceJob> logger, ISpotPriceService service)
{
_logger = logger;
_service = service;
}


public async Task Execute(IJobExecutionContext context)
{
_logger.LogTrace("{method}({context})", nameof(Execute), context);
await _service.UpdateSpotPrices().ConfigureAwait(false);
logger.LogTrace("{method}({context})", nameof(Execute), context);
await service.UpdateSpotPrices().ConfigureAwait(false);
}
}
1 change: 1 addition & 0 deletions TeslaSolarCharger/Server/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static IServiceCollection AddMyDependencies(this IServiceCollection servi
.AddTransient<ErrorDetectionJob>()
.AddTransient<BleApiVersionDetectionJob>()
.AddTransient<FleetTelemetryReconnectionJob>()
.AddTransient<FleetTelemetryReconfigurationJob>()
.AddTransient<JobFactory>()
.AddTransient<IJobFactory, JobFactory>()
.AddTransient<ISchedulerFactory, StdSchedulerFactory>()
Expand Down
10 changes: 7 additions & 3 deletions TeslaSolarCharger/Server/Services/BackendApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ public async Task<bool> IsFleetApiLicensed(string vin, bool useCache)
/// <returns></returns>
public async Task<Dtos.Result<T>> SendRequestToBackend<T>(HttpMethod httpMethod, string? accessToken, string requestUrlPart, object? content)
{
logger.LogTrace("{method}({httpMethod}, {accessToken}, {requestUrlPart}, {content}, {@serializedContent})", nameof(SendRequestToBackend), httpMethod, accessToken, requestUrlPart, content, content);
var request = new HttpRequestMessage();
var finalUrl = configurationWrapper.BackendApiBaseUrl() + requestUrlPart;
request.RequestUri = new Uri(finalUrl);
Expand All @@ -326,10 +327,12 @@ public async Task<bool> IsFleetApiLicensed(string vin, bool useCache)
request.Method = HttpMethod.Post;
if (content != default)
{
var serializedContent = JsonConvert.SerializeObject(content);
var jsonContent = new StringContent(
JsonConvert.SerializeObject(content),
serializedContent,
System.Text.Encoding.UTF8,
"application/json");
logger.LogTrace("Sending content: {serializedContent}", serializedContent);
request.Content = jsonContent;
}
}
Expand All @@ -342,12 +345,13 @@ public async Task<bool> IsFleetApiLicensed(string vin, bool useCache)
);
}
var response = await httpClient.SendAsync(request).ConfigureAwait(false);
var responseContentString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
logger.LogTrace("Response: {responseContent}", responseContentString);
if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
if (typeof(T) != typeof(object))
{
var deserializedObject = JsonConvert.DeserializeObject<T>(responseContent);
var deserializedObject = JsonConvert.DeserializeObject<T>(responseContentString);

if (deserializedObject == null)
{
Expand Down
5 changes: 2 additions & 3 deletions TeslaSolarCharger/Server/Services/ConfigJsonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ public class ConfigJsonService(
IConfigurationWrapper configurationWrapper,
ITeslaSolarChargerContext teslaSolarChargerContext,
IConstants constants,
IDateTimeProvider dateTimeProvider,
JobManager jobManager,
ITeslaMateDbContextWrapper teslaMateDbContextWrapper,
IFleetTelemetryWebSocketService fleetTelemetryWebSocketService)
IFleetTelemetryConfigurationService fleetTelemetryConfigurationService)
: IConfigJsonService
{
private bool CarConfigurationFileExists()
Expand Down Expand Up @@ -266,6 +264,7 @@ public async Task UpdateCarBasicConfiguration(int carId, CarBasicConfiguration c
settingsCar.ShouldBeManaged = carBasicConfiguration.ShouldBeManaged;
settingsCar.UseBle = carBasicConfiguration.UseBle;
settingsCar.BleApiBaseUrl = carBasicConfiguration.BleApiBaseUrl;
await fleetTelemetryConfigurationService.SetFleetTelemetryConfiguration(settingsCar.Vin, false);
}

public async Task SaveOrUpdateCar(DtoCar car)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public async Task ReconfigureAllCarsIfRequired()
!memoryCache.TryGetValue(constants.FleetTelemetryConfigurationExpiryKey + car.Vin, out DateTimeOffset expiryTime);
if(!reconfigurationRequired)
{
reconfigurationRequired = expiryTime < currentDate;
reconfigurationRequired = expiryTime < currentDate.AddHours(constants.FleetTelemetryReconfigurationBufferHours);
}
if (!reconfigurationRequired)
{
Expand Down
2 changes: 2 additions & 0 deletions TeslaSolarCharger/Shared/Resources/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class Constants : IConstants
public string IsBaseAppLicensedKey => "IsBaseAppLicensed";
public string IsFleetApiLicensedKey => "IsFleetApiLicensed_";
public string FleetTelemetryConfigurationExpiryKey => "FleetTelemetryConfigurationExpiry_";
//Also on Cloud Server in Solar4Car.Backend.Helper.Constants
public int FleetTelemetryReconfigurationBufferHours => 3;

public string GridPoleIcon => "<svg id=\"Layer_1\" data-name=\"Layer 1\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 83.77 122.88\"><title>power-pole</title><path d=\"M27.55,17.57h0L40.89.58A1.52,1.52,0,0,1,43,.33a1.38,1.38,0,0,1,.31.33L56.29,17.58l.09.12,24.5,11.21a1.53,1.53,0,0,1-.51,3H77.8v3.48a1.87,1.87,0,1,1-3.74,0V31.88H72.22v3.48a1.87,1.87,0,0,1-3.74,0V31.88H56.6v7.06L80.88,49.83a1.53,1.53,0,0,1-.51,3H77.8v3.71a1.87,1.87,0,1,1-3.74,0V52.8H72.22v3.71a1.87,1.87,0,1,1-3.74,0V52.8H56.6V68.11l9.7,21.43a1.53,1.53,0,0,1,.39.82.07.07,0,0,0,0,0l10.52,23.24h6.55v9.23H62.94v-9.23H73.09L44.79,92.09H38.9L10.8,113.65h10v9.23H0v-9.23H6.6L27.24,68.1V52.8H14.36v3.71a1.87,1.87,0,1,1-3.74,0V52.8h-2v3.71a1.88,1.88,0,0,1-3.75,0V52.8H3.46a1.53,1.53,0,0,1-.62-2.92l24.4-11.09V31.88H14.36v3.48a1.87,1.87,0,1,1-3.74,0V31.88h-2v3.48a1.88,1.88,0,0,1-3.75,0V31.88H3.46A1.53,1.53,0,0,1,2.82,29l24.73-11.4ZM30.3,63.91l9.93-11.38L30.52,41.38H30.3V63.91ZM41.92,50.59l8-9.21H33.9l8,9.21Zm11.42-9.21L43.62,52.53l9.91,11.38V41.38ZM41.92,54.47,31.1,66.88H52.74L41.92,54.47ZM30.3,36.63l9.31-7.79L30.3,21.05V36.63Zm11.62-9.72,7.7-6.44H34.23l7.69,6.44Zm11.61-5.83-9.29,7.77,9.29,7.79V21.08Zm-11.6,9.71-9,7.54h18l-9-7.54ZM71.64,108.7,64.11,92.09H49.82L71.64,108.7ZM33.89,92.09H19.72l-7.55,16.66L33.89,92.09Zm25.6-3L41.93,78.27,24.37,89ZM39,76.48l-9.57-5.87L22.08,86.87,39,76.48Zm2.91-1.79,7.75-4.75H34.18l7.75,4.75Zm12.46-4.06-9.55,5.85L61.73,86.84,54.39,70.63Zm2.21-41.8H73.37L56.6,21.15v7.68ZM27.24,21.07,10.42,28.83H27.24V21.07ZM56.6,42.28v7.47H73.25L56.6,42.28ZM27.24,49.75V42.14L10.51,49.75ZM52.15,17.18,42.07,4,31.74,17.18Z\"/></svg>";
}
1 change: 1 addition & 0 deletions TeslaSolarCharger/Shared/Resources/Contracts/IConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ public interface IConstants
string IsBaseAppLicensedKey { get; }
string IsFleetApiLicensedKey { get; }
string FleetTelemetryConfigurationExpiryKey { get; }
int FleetTelemetryReconfigurationBufferHours { get; }
}

0 comments on commit 94a553f

Please sign in to comment.