diff --git a/TeslaSolarCharger/Server/Services/ConfigJsonService.cs b/TeslaSolarCharger/Server/Services/ConfigJsonService.cs index 597952615..587a90b82 100644 --- a/TeslaSolarCharger/Server/Services/ConfigJsonService.cs +++ b/TeslaSolarCharger/Server/Services/ConfigJsonService.cs @@ -31,7 +31,8 @@ public class ConfigJsonService( IDateTimeProvider dateTimeProvider, IMapperConfigurationFactory mapperConfigurationFactory, JobManager jobManager, - ITeslaMateDbContextWrapper teslaMateDbContextWrapper) + ITeslaMateDbContextWrapper teslaMateDbContextWrapper, + IFleetTelemetryWebSocketService fleetTelemetryWebSocketService) : IConfigJsonService { private bool CarConfigurationFileExists() @@ -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) diff --git a/TeslaSolarCharger/Server/Services/Contracts/IFleetTelemetryWebSocketService.cs b/TeslaSolarCharger/Server/Services/Contracts/IFleetTelemetryWebSocketService.cs index 2d0ac3e66..32ecd978a 100644 --- a/TeslaSolarCharger/Server/Services/Contracts/IFleetTelemetryWebSocketService.cs +++ b/TeslaSolarCharger/Server/Services/Contracts/IFleetTelemetryWebSocketService.cs @@ -3,4 +3,5 @@ public interface IFleetTelemetryWebSocketService { Task ReconnectWebSocketsForEnabledCars(); + Task DisconnectWebSocketsByVin(string vin); } diff --git a/TeslaSolarCharger/Server/Services/FleetTelemetryWebSocketService.cs b/TeslaSolarCharger/Server/Services/FleetTelemetryWebSocketService.cs index 3b65b1b05..a947ed28c 100644 --- a/TeslaSolarCharger/Server/Services/FleetTelemetryWebSocketService.cs +++ b/TeslaSolarCharger/Server/Services/FleetTelemetryWebSocketService.cs @@ -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; @@ -30,7 +27,7 @@ public async Task ReconnectWebSocketsForEnabledCars() var scope = serviceProvider.CreateScope(); var context = scope.ServiceProvider.GetRequiredService(); var cars = await context.Cars - .Where(c => c.UseFleetTelemetry) + .Where(c => c.UseFleetTelemetry && (c.ShouldBeManaged == true)) .Select(c => new { c.Vin, @@ -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); diff --git a/TeslaSolarCharger/Server/appsettings.Development.json b/TeslaSolarCharger/Server/appsettings.Development.json index d4bd62a8f..424d49e0f 100644 --- a/TeslaSolarCharger/Server/appsettings.Development.json +++ b/TeslaSolarCharger/Server/appsettings.Development.json @@ -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": { diff --git a/TeslaSolarCharger/Shared/Dtos/CarBasicConfiguration.cs b/TeslaSolarCharger/Shared/Dtos/CarBasicConfiguration.cs index 3872f08e1..f42468e2a 100644 --- a/TeslaSolarCharger/Shared/Dtos/CarBasicConfiguration.cs +++ b/TeslaSolarCharger/Shared/Dtos/CarBasicConfiguration.cs @@ -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")]