Skip to content

Commit

Permalink
Merge pull request #1308 from pkuehnel/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
pkuehnel authored Jun 9, 2024
2 parents 85349c7 + 423054f commit e584cd4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@
}
else
{
<MudButton Color="Color.Primary" Variant="Variant.Filled" StartIcon="@StartIcon" OnClick="AddButtonClicked" ButtonType="ButtonType.Button">@ButtonText</MudButton>
<MudButton Disabled="IsLoading" Color="Color.Primary" Variant="Variant.Filled" StartIcon="@StartIcon" OnClick="AddButtonClicked" ButtonType="ButtonType.Button">
@if (IsLoading)
{
<MudProgressCircular Class="ms-n1" Size="Size.Small" Indeterminate="true"/>
<MudText Class="ms-2">Processing</MudText>
}
else
{
<MudText>@ButtonText</MudText>
}

</MudButton>
}
</MudPaper>

Expand All @@ -24,6 +35,9 @@
[Parameter]
public string StartIcon { get; set; }

[Parameter]
public bool IsLoading { get; set; }

[Parameter]
public EventCallback OnButtonClicked { get; set; }

Expand Down
14 changes: 11 additions & 3 deletions TeslaSolarCharger/Client/Pages/CarSettings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ else
<div>
Note: When clicking the pair button the car won't display any feedback. You have to place the card on the center console. Only after doing so, a message will pop up. If you don't see a message, the pairing failed. As the car does not send any feedback, just try a few times, if it still does not work reboot your BLE device.
</div>
<RightAlignedButtonComponent OnButtonClicked="_ => PairCar(carBasicConfiguration.Vin)" ButtonText="BLE Pair"></RightAlignedButtonComponent>
<RightAlignedButtonComponent IsLoading="_loadingVins.Contains(carBasicConfiguration.Vin)" OnButtonClicked="_ => PairCar(carBasicConfiguration.Vin)" ButtonText="BLE Pair"></RightAlignedButtonComponent>
<h5>Test BLE access</h5>
<div>
Before you can test BLE access you must pair the car with TSC. This includes placing the card on your center console and confirming the new "phone key" on the car's screen.
</div>
<div>
After clicking the test button the front lights should flash.
After clicking the test button the front lights should flash. Note: The car needs to be awake for this test.
</div>
@if (_bleTestResults.TryGetValue(carBasicConfiguration.Vin, out var bleResult))
{
Expand All @@ -64,7 +64,7 @@ else
</p>
</div>
}
<RightAlignedButtonComponent OnButtonClicked="_ => TestBle(carBasicConfiguration.Vin)" ButtonText="Flash lights"></RightAlignedButtonComponent>
<RightAlignedButtonComponent IsLoading="_loadingVins.Contains(carBasicConfiguration.Vin)" OnButtonClicked="_ => TestBle(carBasicConfiguration.Vin)" ButtonText="Flash lights"></RightAlignedButtonComponent>

</div>
}
Expand All @@ -78,6 +78,8 @@ else

private Dictionary<string, DtoBleResult> _bleTestResults = new();

private HashSet<string> _loadingVins = new();

protected override async Task OnInitializedAsync()
{
_carBasicConfigurations = await HttpClient.GetFromJsonAsync<List<CarBasicConfiguration>>("/api/Config/GetCarBasicConfigurations").ConfigureAwait(false);
Expand All @@ -100,19 +102,25 @@ else

private async Task PairCar(string vin)
{
_pairingResults.Remove(vin);
_loadingVins.Add(vin);
var result = await HttpClient.GetStringAsync($"/api/Ble/PairKey?vin={vin}").ConfigureAwait(false);
var resultJson = JsonConvert.DeserializeObject<DtoBleResult>(result);
_pairingResults[vin] = resultJson?.Message ?? result;
_loadingVins.Remove(vin);
}

private async Task TestBle(string vin)
{
_bleTestResults.Remove(vin);
_loadingVins.Add(vin);
var result = await HttpClient.GetFromJsonAsync<DtoBleResult>($"/api/Ble/FlashLights?vin={vin}").ConfigureAwait(false) ?? new DtoBleResult { Success = false, Message = "Could not deserialize message from TSC." };
if(result.Success && string.IsNullOrWhiteSpace(result.Message))
{
result.Message = "Ble success seems to work. Please double check if lights were flashing.";
}
_bleTestResults[vin] = result;
_loadingVins.Remove(vin);
}

}
7 changes: 7 additions & 0 deletions TeslaSolarCharger/Server/Dtos/Ble/DtoCommandResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TeslaSolarCharger.Server.Dtos.Ble;

public class DtoCommandResult
{
public string? ResultMessage { get; set; }
public bool Success { get; set; }
}
10 changes: 8 additions & 2 deletions TeslaSolarCharger/Server/Services/TeslaBleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,14 @@ private async Task<DtoBleResult> SendCommandToBle(DtoBleRequest request)
logger.LogError("Failed to send command to BLE. StatusCode: {statusCode} {responseContent}", response.StatusCode, responseContent);
throw new InvalidOperationException();
}
var result = JsonConvert.DeserializeObject<DtoBleResult>(responseContent);
return result ?? throw new InvalidDataException($"Could not parse {responseContent} to {nameof(DtoBleResult)}");
var commandResult = JsonConvert.DeserializeObject<DtoCommandResult>(responseContent) ?? throw new InvalidDataException($"Could not parse {responseContent} to {nameof(DtoCommandResult)}"); ;
var result = new DtoBleResult
{
StatusCode = response.StatusCode,
Message = commandResult.ResultMessage ?? string.Empty,
Success = commandResult.Success,
};
return result;
}
catch (Exception ex)
{
Expand Down

0 comments on commit e584cd4

Please sign in to comment.