Skip to content

Commit

Permalink
Merge pull request #1671 from pkuehnel/feat/bleStates
Browse files Browse the repository at this point in the history
feat(BleController): can get car states via ble
  • Loading branch information
pkuehnel authored Dec 7, 2024
2 parents f941fe3 + 2bfab55 commit d7716b2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions TeslaSolarCharger/Server/Controllers/BleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ public class BleController (IBleService bleService) : ApiBaseController

[HttpGet]
public Task<DtoBleCommandResult> WakeUp(string vin) => bleService.WakeUpCar(vin);

[HttpGet]
public Task<DtoBleCommandResult> GetChargeState(string vin) => bleService.GetChargeState(vin);

[HttpGet]
public Task<DtoBleCommandResult> GetDriveState(string vin) => bleService.GetDriveState(vin);
}
2 changes: 2 additions & 0 deletions TeslaSolarCharger/Server/Services/Contracts/IBleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface IBleService
Task<DtoBleCommandResult> PairKey(string vin, string role);
Task<DtoBleCommandResult> WakeUpCar(string vin);
Task CheckBleApiVersionCompatibilities();
Task<DtoBleCommandResult> GetChargeState(string vin);
Task<DtoBleCommandResult> GetDriveState(string vin);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
31 changes: 30 additions & 1 deletion TeslaSolarCharger/Server/Services/TeslaBleService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using LanguageExt;
using Newtonsoft.Json;
using System.Net;
using System.Web;
using TeslaSolarCharger.Server.Dtos.Ble;
Expand Down Expand Up @@ -41,6 +42,34 @@ public async Task<DtoBleCommandResult> WakeUpCar(string vin)
return result;
}

public async Task<DtoBleCommandResult> 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);

Check failure

Code scanning / CodeQL

Log entries created from user input High

This log entry depends on a
user-provided value
.
var request = new DtoBleRequest
{
Vin = vin,
CommandName = "state",
Parameters = ["charge"],
};
var result = await SendCommandToBle(request).ConfigureAwait(false);
return result;
}

public async Task<DtoBleCommandResult> GetDriveState(string vin)
{
//Not tested
logger.LogTrace("{method}({vin})", nameof(GetDriveState), vin);

Check failure

Code scanning / CodeQL

Log entries created from user input High

This log entry depends on a
user-provided value
.
var request = new DtoBleRequest
{
Vin = vin,
CommandName = "state",
Parameters = ["drive"],
};
var result = await SendCommandToBle(request).ConfigureAwait(false);
return result;
}

public async Task<DtoBleCommandResult> StopCharging(string vin)
{
var request = new DtoBleRequest
Expand Down

0 comments on commit d7716b2

Please sign in to comment.