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

Develop #1784

Merged
merged 2 commits into from
Jan 26, 2025
Merged

Develop #1784

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
4 changes: 1 addition & 3 deletions TeslaSolarCharger/Server/Contracts/ITeslaService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using TeslaSolarCharger.Shared.Dtos;
using TeslaSolarCharger.Shared.Enums;
using TeslaSolarCharger.Shared.Enums;

namespace TeslaSolarCharger.Server.Contracts;

public interface ITeslaService
{
Task StartCharging(int carId, int startAmp, CarStateEnum? carState);
Task WakeUpCar(int carId);
Task StopCharging(int carId);
Task SetAmp(int carId, int amps);
}
20 changes: 10 additions & 10 deletions TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ public async Task StartCharging(int carId, int startAmp, CarStateEnum? carState)
}


public async Task WakeUpCar(int carId)
public async Task WakeUpCar(int carId, bool isFleetApiTest)
{
logger.LogTrace("{method}({carId})", nameof(WakeUpCar), carId);
var car = settings.Cars.First(c => c.Id == carId);
var result = await SendCommandToTeslaApi<DtoVehicleWakeUpResult>(car.Vin, WakeUpRequest).ConfigureAwait(false);
var result = await SendCommandToTeslaApi<DtoVehicleWakeUpResult>(car.Vin, WakeUpRequest, null, true).ConfigureAwait(false);
if (car.TeslaMateCarId != default)
{
//ToDo: fix with https://github.com/pkuehnel/TeslaSolarCharger/issues/1511
Expand Down Expand Up @@ -165,10 +165,10 @@ public async Task<DtoValue<bool>> TestFleetApiAccess(int carId)
var inMemoryCar = settings.Cars.First(c => c.Id == carId);
try
{
await WakeUpCarIfNeeded(carId).ConfigureAwait(false);
await WakeUpCarIfNeeded(carId, true).ConfigureAwait(false);
var amps = 7;
var commandData = $"{{\"charging_amps\":{amps}}}";
var result = await SendCommandToTeslaApi<DtoVehicleCommandResult>(vin, SetChargingAmpsRequest, amps).ConfigureAwait(false);
var result = await SendCommandToTeslaApi<DtoVehicleCommandResult>(vin, SetChargingAmpsRequest, amps, true).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 @@ -797,7 +797,7 @@ internal DateTimeOffset RoundToNextQuarterHour(DateTimeOffset chargingStartTime)
return chargingStartTime;
}

private async Task WakeUpCarIfNeeded(int carId)
private async Task WakeUpCarIfNeeded(int carId, bool isFleetApiTest = false)
{
logger.LogTrace("{method}({carId})", nameof(WakeUpCarIfNeeded), carId);
var car = settings.Cars.First(c => c.Id == carId);
Expand All @@ -810,7 +810,7 @@ private async Task WakeUpCarIfNeeded(int carId)
{
case CarStateEnum.Offline or CarStateEnum.Asleep:
logger.LogInformation("Wakeup car.");
await WakeUpCar(carId).ConfigureAwait(false);
await WakeUpCar(carId, isFleetApiTest).ConfigureAwait(false);
break;
case CarStateEnum.Suspended:
logger.LogInformation("Resume logging as is suspended");
Expand All @@ -824,14 +824,14 @@ private async Task WakeUpCarIfNeeded(int carId)
}
}

private async Task<DtoGenericTeslaResponse<T>?> SendCommandToTeslaApi<T>(string vin, DtoFleetApiRequest fleetApiRequest, int? intParam = null) where T : class
private async Task<DtoGenericTeslaResponse<T>?> SendCommandToTeslaApi<T>(string vin, DtoFleetApiRequest fleetApiRequest, int? intParam = null, bool isFleetApiTest = false) where T : class
{
logger.LogTrace("{method}({vin}, {@fleetApiRequest}, {intParam})", nameof(SendCommandToTeslaApi), vin, fleetApiRequest, intParam);
var fleetTelemetryEnabled = await teslaSolarChargerContext.Cars
.Where(c => c.Vin == vin)
.Select(c => c.UseFleetTelemetry)
.FirstAsync();
if (fleetTelemetryEnabled)
if (!isFleetApiTest && fleetTelemetryEnabled)
{
if(!fleetTelemetryWebSocketService.IsClientConnected(vin))
{
Expand All @@ -856,7 +856,7 @@ await errorHandlingService.HandleError(nameof(TeslaFleetApiService), nameof(Send
await errorHandlingService.HandleErrorResolved(issueKeys.BaseAppNotLicensed, null);

var car = settings.Cars.First(c => c.Vin == vin);
if (fleetApiRequest.BleCompatible)
if (!isFleetApiTest && fleetApiRequest.BleCompatible)
{

var isCarBleEnabled = car.UseBle;
Expand Down Expand Up @@ -949,7 +949,7 @@ await errorHandlingService.HandleError(nameof(TeslaFleetApiService), nameof(Send
}
}

if (fleetApiRequest.RequestUrl != VehicleRequest.RequestUrl && (!await backendApiService.IsFleetApiLicensed(car.Vin, true)))
if (!isFleetApiTest && fleetApiRequest.RequestUrl != VehicleRequest.RequestUrl && (!await backendApiService.IsFleetApiLicensed(car.Vin, true)))
{
await errorHandlingService.HandleError(nameof(TeslaFleetApiService), nameof(SendCommandToTeslaApi), $"Fleet API not licensed for car {car.Vin}",
"Can not send Fleet API commands to car as Fleet API is not licensed",
Expand Down
Loading