147 lines
4.9 KiB
Plaintext
147 lines
4.9 KiB
Plaintext
@using RobotNet10.RobotApp.Client.Clients
|
|
@using MudBlazor
|
|
|
|
<MudPaper Class="pa-4" Style="height: 100%;">
|
|
<MudStack Spacing="3">
|
|
<MudStack Row="true" Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center">
|
|
<MudText Typo="Typo.h5">Manual Control</MudText>
|
|
<MudIconButton Icon="@Icons.Material.Filled.Refresh"
|
|
Color="Color.Primary"
|
|
Size="Size.Small"
|
|
OnClick="RefreshStatus"
|
|
Disabled="@(isLoading)" />
|
|
</MudStack>
|
|
|
|
@* Enable / Disable PS5 Controller *@
|
|
<MudStack Row="true" Spacing="3" AlignItems="@AlignItems.Center">
|
|
<MudButton Variant="Variant.Filled"
|
|
Color="@(ps5Enabled ? Color.Error : Color.Success)"
|
|
OnClick="TogglePs5Control"
|
|
Disabled="@isLoading">
|
|
@if (ps5Enabled)
|
|
{
|
|
<MudIcon Icon="@Icons.Material.Filled.Stop" Class="mr-2" />
|
|
<MudText>Disable PS5 Controller</MudText>
|
|
}
|
|
else
|
|
{
|
|
<MudIcon Icon="@Icons.Material.Filled.PlayArrow" Class="mr-2" />
|
|
<MudText>Enable PS5 Controller</MudText>
|
|
}
|
|
</MudButton>
|
|
</MudStack>
|
|
|
|
@* Status of PS5 Controller *@
|
|
@if (ps5State != null)
|
|
{
|
|
<MudCard>
|
|
<MudCardContent>
|
|
<MudText Typo="Typo.h6" Class="mb-3">PS5 Controller Status</MudText>
|
|
<MudStack Spacing="2">
|
|
<MudStack Row="true" Justify="Justify.SpaceBetween">
|
|
<MudText>State:</MudText>
|
|
<MudChip T="string" Color="@(ps5Enabled ? Color.Success : Color.Default)" Size="Size.Small">
|
|
@ps5State
|
|
</MudChip>
|
|
</MudStack>
|
|
</MudStack>
|
|
</MudCardContent>
|
|
</MudCard>
|
|
}
|
|
</MudStack>
|
|
</MudPaper>
|
|
|
|
@code {
|
|
[Parameter] public MotionHubClient? HubClient { get; set; }
|
|
[Parameter] public bool IsHubReady { get; set; }
|
|
[Inject] private HttpClient Http { get; set; } = null!;
|
|
[Inject] private ISnackbar Snackbar { get; set; } = null!;
|
|
|
|
private string? ps5State;
|
|
private bool ps5Enabled => string.Equals(ps5State, "Active", StringComparison.OrdinalIgnoreCase);
|
|
private bool isLoading = false;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await RefreshStatus();
|
|
}
|
|
|
|
public async Task RefreshStatus()
|
|
{
|
|
if (isLoading) return;
|
|
|
|
try
|
|
{
|
|
isLoading = true;
|
|
var resp = await Http.GetAsync("/api/motion/ps5/status");
|
|
if (resp.IsSuccessStatusCode)
|
|
{
|
|
var json = await resp.Content.ReadFromJsonAsync<Ps5StatusResponse>();
|
|
ps5State = json?.State ?? "Unknown";
|
|
}
|
|
StateHasChanged();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Snackbar.Add($"Failed to refresh PS controller status: {ex.Message}", Severity.Error);
|
|
}
|
|
finally
|
|
{
|
|
isLoading = false;
|
|
}
|
|
}
|
|
|
|
private async Task TogglePs5Control()
|
|
{
|
|
if (isLoading) return;
|
|
|
|
try
|
|
{
|
|
isLoading = true;
|
|
if (ps5Enabled)
|
|
{
|
|
var resp = await Http.PostAsync("/api/motion/ps5/disable", null);
|
|
if (resp.IsSuccessStatusCode)
|
|
{
|
|
Snackbar.Add("PS controller disabled", Severity.Info);
|
|
ps5State = "Disabled";
|
|
}
|
|
else
|
|
Snackbar.Add("Failed to disable PS controller", Severity.Error);
|
|
}
|
|
else
|
|
{
|
|
var resp = await Http.PostAsync("/api/motion/ps5/enable", null);
|
|
if (resp.IsSuccessStatusCode)
|
|
{
|
|
Snackbar.Add("PS controller enabled", Severity.Success);
|
|
ps5State = "Active";
|
|
}
|
|
else
|
|
Snackbar.Add("Failed to enable PS controller", Severity.Error);
|
|
}
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
await Task.Delay(100);
|
|
await RefreshStatus();
|
|
if (ps5State != null) break;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Snackbar.Add($"Error: {ex.Message}", Severity.Error);
|
|
}
|
|
finally
|
|
{
|
|
isLoading = false;
|
|
StateHasChanged();
|
|
}
|
|
}
|
|
|
|
private sealed class Ps5StatusResponse
|
|
{
|
|
public string State { get; set; } = string.Empty;
|
|
}
|
|
}
|