Skip to content

Commit

Permalink
Merge pull request #1009 from pkuehnel/feat/fleetApiIssues
Browse files Browse the repository at this point in the history
feat(IssueService): display proper issues on token request
  • Loading branch information
pkuehnel authored Dec 4, 2023
2 parents 64d3ac8 + a1ce5b0 commit 406607f
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 30 deletions.
5 changes: 5 additions & 0 deletions TeslaSolarCharger/Client/Pages/BaseConfiguration.razor
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ else
You already have requested a token but did not receive it yet. It can take up to five minutes to receive the token. If the token did not arrive within five minutes please try again:
</div>
break;
case FleetApiTokenState.TokenRequestExpired:
<div>
Your token request has expired. Please generate a new token:
</div>
break;
case FleetApiTokenState.Expired:
<div>
Your token has expired. This could happen if you changed your Tesla password or did not use TeslaSolarCharger for too long. Please generate a new token:
Expand Down
6 changes: 4 additions & 2 deletions TeslaSolarCharger/Client/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,11 @@ else
{
<p><em>Version: <a href="https://github.com/pkuehnel/TeslaSolarCharger/releases/tag/v@_version" target="_blank">@_version</a></em></p>
}
<div>@_installationId</div>
<div>
<b>CurrentCulture</b>: @CultureInfo.CurrentCulture
<b>Installation ID</b>: @_installationId
</div>
<div>
<b>Language settings</b>: @CultureInfo.CurrentCulture
</div>
</div>

Expand Down
40 changes: 22 additions & 18 deletions TeslaSolarCharger/Server/Resources/PossibleIssues/IssueKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

public class IssueKeys
{
public string MqttNotConnected = "MqttNotConnected";
public string CarSocLimitNotReadable = "CarSocLimitNotReadable";
public string CarSocNotReadable = "CarSocNotReadable";
public string GridPowerNotAvailable = "GridPowerNotAvailable";
public string InverterPowerNotAvailable = "InverterPowerNotAvailable";
public string HomeBatterySocNotAvailable = "HomeBatterySocNotAvailable";
public string HomeBatterySocNotPlausible = "HomeBatterySocNotPlausible";
public string HomeBatteryPowerNotAvailable = "HomeBatteryPowerNotAvailable";
public string HomeBatteryMinimumSocNotConfigured = "HomeBatteryMinimumSocNotConfigured";
public string HomeBatteryChargingPowerNotConfigured = "HomeBatteryChargingPowerNotConfigured";
public string TeslaMateApiNotAvailable = "TeslaMateApiNotAvailable";
public string DatabaseNotAvailable = "DatabaseNotAvailable";
public string GeofenceNotAvailable = "GeofenceNotAvailable";
public string CarIdNotAvailable = "CarIdNotAvailable";
public string VersionNotUpToDate = "VersionNotUpToDate";
public string CorrectionFactorZero = "CorrectionFactorZero";
public string ServerTimeZoneDifferentFromClient = "ServerTimeZoneDifferentFromClient";
public string NewTeslaApi = "NewTeslaApi";
public string MqttNotConnected => "MqttNotConnected";
public string CarSocLimitNotReadable => "CarSocLimitNotReadable";
public string CarSocNotReadable => "CarSocNotReadable";
public string GridPowerNotAvailable => "GridPowerNotAvailable";
public string InverterPowerNotAvailable => "InverterPowerNotAvailable";
public string HomeBatterySocNotAvailable => "HomeBatterySocNotAvailable";
public string HomeBatterySocNotPlausible => "HomeBatterySocNotPlausible";
public string HomeBatteryPowerNotAvailable => "HomeBatteryPowerNotAvailable";
public string HomeBatteryMinimumSocNotConfigured => "HomeBatteryMinimumSocNotConfigured";
public string HomeBatteryChargingPowerNotConfigured => "HomeBatteryChargingPowerNotConfigured";
public string TeslaMateApiNotAvailable => "TeslaMateApiNotAvailable";
public string DatabaseNotAvailable => "DatabaseNotAvailable";
public string GeofenceNotAvailable => "GeofenceNotAvailable";
public string CarIdNotAvailable => "CarIdNotAvailable";
public string VersionNotUpToDate => "VersionNotUpToDate";
public string CorrectionFactorZero => "CorrectionFactorZero";
public string ServerTimeZoneDifferentFromClient => "ServerTimeZoneDifferentFromClient";
public string NewTeslaApiNotUsed => "NewTeslaApiNotUsed";
public string FleetApiTokenNotRequested => "FleetApiTokenNotRequested";
public string FleetApiTokenRequestExpired => "FleetApiTokenRequestExpired";
public string FleetApiTokenNotReceived => "FleetApiTokenNotReceived";
public string FleetApiTokenExpired => "FleetApiTokenExpired";
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,36 @@ public PossibleIssues(IssueKeys issueKeys)
)
},
{
issueKeys.NewTeslaApi, CreateIssue("New cars are currently not supported",
issueKeys.NewTeslaApiNotUsed, CreateIssue("New cars need a new Tesla API. As this is in a very early beta state I highly recommend not using it if your car supports the old API!",
IssueType.Information,
"If there are any updates on this topic, I will post them <a href=\"https://github.com/pkuehnel/TeslaSolarCharger/issues/988\" target=\"_blank\">here</a>.",
"Sorry for this information beeing not removable - it will be possible in a future update."
"To use the new API add <code>UseFleetApi=true</code> as environment variable in your <code>docker-compose.yml</code>",
"Sorry for this information beeing not removable - as switching to the new API in January 2024 as default you won't see this information from then on."
)
},
{
issueKeys.FleetApiTokenNotRequested, CreateIssue("You did not request a Tesla Token, yet.",
IssueType.Error,
"Open the <a href=\"/BaseConfiguration\">Base Configuration</a> and request a new token."
)
},
{
issueKeys.FleetApiTokenNotReceived, CreateIssue("The Tesla token was not received, yet.",
IssueType.Error,
"Getting the Token can take up to five minutes after submitting your password.",
"If waiting five minutes does not help, open the <a href=\"/BaseConfiguration\">Base Configuration</a> and request a new token."
)
},
{
issueKeys.FleetApiTokenRequestExpired, CreateIssue("The Tesla token could not be received.",
IssueType.Error,
"Open the <a href=\"/BaseConfiguration\">Base Configuration</a> and request a new token.",
"If this issue keeps occuring, feel free to open an issue <a href=\"https://github.com/pkuehnel/TeslaSolarCharger/issues\" target=\"_blank\">on Github</a> including the last 5 chars of your installation ID (bottom of the page). Do NOT include the whole ID."
)
},
{
issueKeys.FleetApiTokenExpired, CreateIssue("Your Tesla token has expired, this can occur when you changed your password or did not use the TeslaSolarCharger for too long..",
IssueType.Error,
"Open the <a href=\"/BaseConfiguration\">Base Configuration</a> and request a new token."
)
},
};
Expand Down
44 changes: 38 additions & 6 deletions TeslaSolarCharger/Server/Services/IssueValidationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using TeslaSolarCharger.Model.Contracts;
using TeslaSolarCharger.Server.Contracts;
using TeslaSolarCharger.Server.Resources.PossibleIssues;
using TeslaSolarCharger.Server.Services.Contracts;
using TeslaSolarCharger.Shared.Contracts;
using TeslaSolarCharger.Shared.Dtos;
using TeslaSolarCharger.Shared.Dtos.BaseConfiguration;
Expand All @@ -23,11 +24,12 @@ public class IssueValidationService : IIssueValidationService
private readonly ITeslamateContext _teslamateContext;
private readonly IConstants _constants;
private readonly IDateTimeProvider _dateTimeProvider;
private readonly ITeslaFleetApiService _teslaFleetApiService;

public IssueValidationService(ILogger<IssueValidationService> logger, ISettings settings,
ITeslaMateMqttService teslaMateMqttService, IPossibleIssues possibleIssues, IssueKeys issueKeys,
IConfigurationWrapper configurationWrapper, ITeslamateContext teslamateContext,
IConstants constants, IDateTimeProvider dateTimeProvider)
IConstants constants, IDateTimeProvider dateTimeProvider, ITeslaFleetApiService teslaFleetApiService)
{
_logger = logger;
_settings = settings;
Expand All @@ -38,23 +40,53 @@ public IssueValidationService(ILogger<IssueValidationService> logger, ISettings
_teslamateContext = teslamateContext;
_constants = constants;
_dateTimeProvider = dateTimeProvider;
_teslaFleetApiService = teslaFleetApiService;
}

public async Task<List<Issue>> RefreshIssues(TimeSpan clientTimeZoneId)
{
_logger.LogTrace("{method}()", nameof(RefreshIssues));
var issueList = new List<Issue>
var issueList = new List<Issue>();
if (!_configurationWrapper.UseFleetApi())
{
_possibleIssues.GetIssueByKey(_issueKeys.NewTeslaApi),
};
issueList.Add(_possibleIssues.GetIssueByKey(_issueKeys.NewTeslaApiNotUsed));
}
issueList.AddRange(GetServerConfigurationIssues(clientTimeZoneId));
if (Debugger.IsAttached)
{
return issueList;
//return issueList;
}
issueList.AddRange(GetMqttIssues());
issueList.AddRange(PvValueIssues());
issueList.AddRange(await GetTeslaMateApiIssues().ConfigureAwait(false));
if (!_configurationWrapper.UseFleetApi())
{
issueList.AddRange(await GetTeslaMateApiIssues().ConfigureAwait(false));
}
else
{
var tokenState = (await _teslaFleetApiService.GetFleetApiTokenState().ConfigureAwait(false)).Value;
switch (tokenState)
{
case FleetApiTokenState.NotNeeded:
break;
case FleetApiTokenState.NotRequested:
issueList.Add(_possibleIssues.GetIssueByKey(_issueKeys.FleetApiTokenNotRequested));
break;
case FleetApiTokenState.TokenRequestExpired:
issueList.Add(_possibleIssues.GetIssueByKey(_issueKeys.FleetApiTokenRequestExpired));
break;
case FleetApiTokenState.NotReceived:
issueList.Add(_possibleIssues.GetIssueByKey(_issueKeys.FleetApiTokenNotReceived));
break;
case FleetApiTokenState.Expired:
issueList.Add(_possibleIssues.GetIssueByKey(_issueKeys.FleetApiTokenExpired));
break;
case FleetApiTokenState.UpToDate:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
issueList.AddRange(await GetDatabaseIssues().ConfigureAwait(false));
issueList.AddRange(SofwareIssues());
issueList.AddRange(ConfigurationIssues());
Expand Down
9 changes: 8 additions & 1 deletion TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ public async Task RefreshTokenAsync()
case FleetApiTokenState.NotRequested:
_logger.LogDebug("No token has been requested, yet.");
return;
case FleetApiTokenState.TokenRequestExpired:
_logger.LogError("Your toke request has expired, create a new one.");
return;
case FleetApiTokenState.NotReceived:
break;
case FleetApiTokenState.Expired:
Expand Down Expand Up @@ -221,7 +224,11 @@ public async Task<DtoValue<FleetApiTokenState>> GetFleetApiTokenState()
return new DtoValue<FleetApiTokenState>(FleetApiTokenState.NotRequested);
}
var tokenRequestedDate = DateTime.Parse(tokenRequestedDateString, null, DateTimeStyles.RoundtripKind);
//ToDo: return not requested if request is older than x -> Currently not nown as not known how old a code can be to create a token out of it.
var currentDate = _dateTimeProvider.UtcNow();
if (tokenRequestedDate < currentDate.AddMinutes(-5))
{
return new DtoValue<FleetApiTokenState>(FleetApiTokenState.TokenRequestExpired);
}
return new DtoValue<FleetApiTokenState>(FleetApiTokenState.NotReceived);
}

Expand Down
1 change: 1 addition & 0 deletions TeslaSolarCharger/Shared/Enums/FleetApiTokenState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public enum FleetApiTokenState
{
NotNeeded,
NotRequested,
TokenRequestExpired,
NotReceived,
Expired,
UpToDate,
Expand Down

0 comments on commit 406607f

Please sign in to comment.