Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BleController): can get car states via ble #1671

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
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);
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
Loading