From 2bfab55b4dffffd094d3e94ba7ccc25aef65e854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20K=C3=BChnel?= Date: Thu, 5 Dec 2024 11:50:56 +0100 Subject: [PATCH] feat(BleController): can get car states via ble --- .../Server/Controllers/BleController.cs | 6 ++++ .../Server/Services/Contracts/IBleService.cs | 2 ++ .../FleetTelemetryWebSocketService.cs | 3 +- .../Server/Services/TeslaBleService.cs | 31 ++++++++++++++++++- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/TeslaSolarCharger/Server/Controllers/BleController.cs b/TeslaSolarCharger/Server/Controllers/BleController.cs index 5c1cb0e3a..2d0ae8d0f 100644 --- a/TeslaSolarCharger/Server/Controllers/BleController.cs +++ b/TeslaSolarCharger/Server/Controllers/BleController.cs @@ -24,4 +24,10 @@ public class BleController (IBleService bleService) : ApiBaseController [HttpGet] public Task WakeUp(string vin) => bleService.WakeUpCar(vin); + + [HttpGet] + public Task GetChargeState(string vin) => bleService.GetChargeState(vin); + + [HttpGet] + public Task GetDriveState(string vin) => bleService.GetDriveState(vin); } diff --git a/TeslaSolarCharger/Server/Services/Contracts/IBleService.cs b/TeslaSolarCharger/Server/Services/Contracts/IBleService.cs index 98312a280..0fca075b5 100644 --- a/TeslaSolarCharger/Server/Services/Contracts/IBleService.cs +++ b/TeslaSolarCharger/Server/Services/Contracts/IBleService.cs @@ -13,4 +13,6 @@ public interface IBleService Task PairKey(string vin, string role); Task WakeUpCar(string vin); Task CheckBleApiVersionCompatibilities(); + Task GetChargeState(string vin); + Task GetDriveState(string vin); } diff --git a/TeslaSolarCharger/Server/Services/FleetTelemetryWebSocketService.cs b/TeslaSolarCharger/Server/Services/FleetTelemetryWebSocketService.cs index 80ace55ec..d5c607530 100644 --- a/TeslaSolarCharger/Server/Services/FleetTelemetryWebSocketService.cs +++ b/TeslaSolarCharger/Server/Services/FleetTelemetryWebSocketService.cs @@ -79,7 +79,8 @@ await existingClient.WebSocketClient.SendAsync(segment, WebSocketMessageType.Tex continue; } - logger.LogInformation("Websocket Client State for car {vin} is {state}, last heartbeat is {lastHeartbeat} while earliest Possible Heartbeat is {earliestPossibleHeartbeat}. Disposing client", car.Vin, existingClient.WebSocketClient.State, existingClient.LastReceivedHeartbeat, earliestPossibleLastHeartbeat); + logger.LogInformation("Websocket Client State for car {vin} is {state}, last heartbeat is {lastHeartbeat} while earliest Possible Heartbeat is {earliestPossibleHeartbeat}. Disposing client", + car.Vin, existingClient.WebSocketClient.State, existingClient.LastReceivedHeartbeat, earliestPossibleLastHeartbeat); existingClient.WebSocketClient.Dispose(); Clients.Remove(existingClient); } diff --git a/TeslaSolarCharger/Server/Services/TeslaBleService.cs b/TeslaSolarCharger/Server/Services/TeslaBleService.cs index 31ccfc5f8..8aaee08a1 100644 --- a/TeslaSolarCharger/Server/Services/TeslaBleService.cs +++ b/TeslaSolarCharger/Server/Services/TeslaBleService.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using LanguageExt; +using Newtonsoft.Json; using System.Net; using System.Web; using TeslaSolarCharger.Server.Dtos.Ble; @@ -41,6 +42,34 @@ public async Task WakeUpCar(string vin) return result; } + public async Task GetChargeState(string vin) + { + //Not tested, should contain a json with the charge state. Other options would be climate, drive, closures, charge-schedule, precondition-schedule, tire-pressue, media, media-detail, software-update + logger.LogTrace("{method}({vin})", nameof(GetChargeState), vin); + var request = new DtoBleRequest + { + Vin = vin, + CommandName = "state", + Parameters = ["charge"], + }; + var result = await SendCommandToBle(request).ConfigureAwait(false); + return result; + } + + public async Task GetDriveState(string vin) + { + //Not tested + logger.LogTrace("{method}({vin})", nameof(GetDriveState), vin); + var request = new DtoBleRequest + { + Vin = vin, + CommandName = "state", + Parameters = ["drive"], + }; + var result = await SendCommandToBle(request).ConfigureAwait(false); + return result; + } + public async Task StopCharging(string vin) { var request = new DtoBleRequest