Skip to content

Commit

Permalink
feat(FleetApiService): test fleet api access with charging current as…
Browse files Browse the repository at this point in the history
… command scope is removed
  • Loading branch information
pkuehnel committed Dec 21, 2024
1 parent ebcf0b0 commit a309dcb
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 48 deletions.
2 changes: 1 addition & 1 deletion TeslaSolarCharger/Client/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ else
{
<div>
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
<span class="sr-only">Testing Fleet API access might take about 30 seconds and the headlights might flash...</span>
<span class="sr-only">Testing Fleet API access might take about 30 seconds...</span>
</div>
}
else if (_isFleetApiWorkingForCar.Any(d => d.Key == car.CarId))
Expand Down
1 change: 0 additions & 1 deletion TeslaSolarCharger/Server/Contracts/ITeslaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ public interface ITeslaService
Task StopCharging(int carId);
Task SetAmp(int carId, int amps);
Task SetChargeLimit(int carId, int limitSoC);
Task SetSentryMode(int carId, bool active);
}
14 changes: 0 additions & 14 deletions TeslaSolarCharger/Server/Controllers/FleetApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,6 @@ public Task SetChargeLimit(int carId, int percent)
return teslaService.SetChargeLimit(carId, percent);
}

/// <summary>
/// Note: This endpoint is only available in development environment
/// </summary>
/// <exception cref="InvalidOperationException">Is thrown when not beeing in dev Mode</exception>
[HttpGet]
public Task SetSentryMode(int carId, bool active)
{
if (!configurationWrapper.IsDevelopmentEnvironment())
{
throw new InvalidOperationException("This method is only available in development environment");
}
return teslaService.SetSentryMode(carId, active);
}

[HttpGet]
public Task<DtoValue<bool>> TestFleetApiAccess(int carId) => fleetApiService.TestFleetApiAccess(carId);
[HttpGet]
Expand Down
35 changes: 3 additions & 32 deletions TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,6 @@ public class TeslaFleetApiService(
NeedsProxy = true,
TeslaApiRequestType = TeslaApiRequestType.Command,
};
private DtoFleetApiRequest SetSentryModeRequest => new()
{
RequestUrl = constants.SetSentryModeRequestUrl,
NeedsProxy = true,
TeslaApiRequestType = TeslaApiRequestType.Command,
};
private DtoFleetApiRequest FlashHeadlightsRequest => new()
{
RequestUrl = constants.FlashHeadlightsRequestUrl,
NeedsProxy = true,
//Do not make this BLE compatible as this is used to test fleet api access
BleCompatible = false,
TeslaApiRequestType = TeslaApiRequestType.Charging,
};
private DtoFleetApiRequest WakeUpRequest => new()
{
RequestUrl = constants.WakeUpRequestUrl,
Expand Down Expand Up @@ -212,19 +198,6 @@ public async Task SetChargeLimit(int carId, int limitSoC)
await SendCommandToTeslaApi<DtoVehicleCommandResult>(vin, SetChargeLimitRequest, HttpMethod.Post, JsonConvert.SerializeObject(parameters)).ConfigureAwait(false);
}

public async Task SetSentryMode(int carId, bool active)
{
logger.LogTrace("{method}({param1}, {param2})", nameof(SetSentryMode), carId, active);
var vin = GetVinByCarId(carId);
var car = settings.Cars.First(c => c.Id == carId);
await WakeUpCarIfNeeded(carId, car.State).ConfigureAwait(false);
var parameters = new Dictionary<string, int>()
{
{ "on", active ? 1 : 0 },
};
await SendCommandToTeslaApi<DtoVehicleCommandResult>(vin, SetSentryModeRequest, HttpMethod.Post, JsonConvert.SerializeObject(parameters)).ConfigureAwait(false);
}

public async Task<DtoValue<bool>> TestFleetApiAccess(int carId)
{
logger.LogTrace("{method}({carId})", nameof(TestFleetApiAccess), carId);
Expand All @@ -233,7 +206,9 @@ public async Task<DtoValue<bool>> TestFleetApiAccess(int carId)
try
{
await WakeUpCarIfNeeded(carId, inMemoryCar.State).ConfigureAwait(false);
var result = await SendCommandToTeslaApi<DtoVehicleCommandResult>(vin, FlashHeadlightsRequest, HttpMethod.Post).ConfigureAwait(false);
var amps = 7;
var commandData = $"{{\"charging_amps\":{amps}}}";
var result = await SendCommandToTeslaApi<DtoVehicleCommandResult>(vin, SetChargingAmpsRequest, HttpMethod.Post, commandData, amps).ConfigureAwait(false);
var successResult = result?.Response?.Result == true;
var car = teslaSolarChargerContext.Cars.First(c => c.Id == carId);
car.TeslaFleetApiState = successResult ? TeslaCarFleetApiState.Ok : TeslaCarFleetApiState.NotWorking;
Expand Down Expand Up @@ -1022,10 +997,6 @@ private void AddRequestToCar(string vin, DtoFleetApiRequest fleetApiRequest)
{
car.OtherCommandCalls.Add(currentDate);
}
else if (fleetApiRequest.RequestUrl == FlashHeadlightsRequest.RequestUrl)
{
car.OtherCommandCalls.Add(currentDate);
}
else if (fleetApiRequest.RequestUrl == WakeUpRequest.RequestUrl)
{
car.WakeUpCalls.Add(currentDate);
Expand Down

0 comments on commit a309dcb

Please sign in to comment.