@using RobotNet10.RobotApp.Client.Clients @using MudBlazor Manual Control @* Enable / Disable PS5 Controller *@ @if (ps5Enabled) { Disable PS5 Controller } else { Enable PS5 Controller } @* Status of PS5 Controller *@ @if (ps5State != null) { PS5 Controller Status State: @ps5State } @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(); 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; } }