Skip to content

Commit

Permalink
feat(FleetTelemetryWebSocketService): reconnect fleet telemetry on ca…
Browse files Browse the repository at this point in the history
…r data save
  • Loading branch information
pkuehnel committed Oct 31, 2024
1 parent 4407579 commit dc459d8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
4 changes: 3 additions & 1 deletion TeslaSolarCharger/Server/Services/ConfigJsonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public class ConfigJsonService(
IDateTimeProvider dateTimeProvider,
IMapperConfigurationFactory mapperConfigurationFactory,
JobManager jobManager,
ITeslaMateDbContextWrapper teslaMateDbContextWrapper)
ITeslaMateDbContextWrapper teslaMateDbContextWrapper,
IFleetTelemetryWebSocketService fleetTelemetryWebSocketService)
: IConfigJsonService
{
private bool CarConfigurationFileExists()
Expand Down Expand Up @@ -262,6 +263,7 @@ public async Task UpdateCarBasicConfiguration(int carId, CarBasicConfiguration c
settingsCar.UseBle = carBasicConfiguration.UseBle;
settingsCar.UseBleForWakeUp = carBasicConfiguration.UseBleForWakeUp;
settingsCar.BleApiBaseUrl = carBasicConfiguration.BleApiBaseUrl;
await fleetTelemetryWebSocketService.DisconnectWebSocketsByVin(carBasicConfiguration.Vin);
}

public Task UpdateCarConfiguration(int carId, DepricatedCarConfiguration carConfiguration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public interface IFleetTelemetryWebSocketService
{
Task ReconnectWebSocketsForEnabledCars();
Task DisconnectWebSocketsByVin(string vin);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using Microsoft.EntityFrameworkCore;
using MudBlazor.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks.Sources;
using TeslaSolarCharger.Model.Entities.TeslaSolarCharger;
using TeslaSolarCharger.Model.EntityFramework;
using TeslaSolarCharger.Server.Dtos;
Expand All @@ -30,7 +27,7 @@ public async Task ReconnectWebSocketsForEnabledCars()
var scope = serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<TeslaSolarChargerContext>();
var cars = await context.Cars
.Where(c => c.UseFleetTelemetry)
.Where(c => c.UseFleetTelemetry && (c.ShouldBeManaged == true))
.Select(c => new
{
c.Vin,
Expand Down Expand Up @@ -73,6 +70,21 @@ await existingClient.WebSocketClient.SendAsync(segment, WebSocketMessageType.Tex
}
}

public async Task DisconnectWebSocketsByVin(string vin)
{
logger.LogTrace("{method}({vin})", nameof(DisconnectWebSocketsByVin), vin);
var client = Clients.FirstOrDefault(c => c.Vin == vin);
if (client != default)
{
if (client.WebSocketClient.State == WebSocketState.Open)
{
await client.WebSocketClient.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", new CancellationTokenSource(_heartbeatsendTimeout).Token).ConfigureAwait(false);
}
client.WebSocketClient.Dispose();
Clients.Remove(client);
}
}

private async Task ConnectToFleetTelemetryApi(string vin, bool useFleetTelemetryForLocationData)
{
logger.LogTrace("{method}({carId})", nameof(ConnectToFleetTelemetryApi), vin);
Expand Down
2 changes: 1 addition & 1 deletion TeslaSolarCharger/Server/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"ShouldUseFakeSolarValues": true,
"IgnoreSslErrors": true,
"BleBaseUrl": "http://raspible:7210/",
"FleetTelemetryApiUrl": "wss://localhost:7281/ws?",
//"FleetTelemetryApiUrl": "wss://localhost:7281/ws?",
"GridPriceProvider": {
"EnergyProvider": "Tibber",
"Octopus": {
Expand Down
1 change: 1 addition & 0 deletions TeslaSolarCharger/Shared/Dtos/CarBasicConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public CarBasicConfiguration(int id, string? name)
}
public int Id { get; set; }
public string? Name { get; set; }
[Disabled]
public string Vin { get; set; }
[Range(1, int.MaxValue)]
[Postfix("A")]
Expand Down

0 comments on commit dc459d8

Please sign in to comment.