From e0f0067e8924b20ebf250792c24c2775b2a3d503 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 25 Sep 2024 21:56:54 +0200 Subject: [PATCH] Get state of relay on device page --- central_backend/src/server/servers.rs | 6 +++- .../src/server/web_api/relays_controller.rs | 12 ++++++- central_frontend/src/api/RelayApi.ts | 12 +++++++ .../src/routes/DeviceRoute/DeviceRelays.tsx | 32 +++++++++++++++++-- .../src/routes/RelaysListRoute.tsx | 13 +------- central_frontend/src/widgets/BoolText.tsx | 11 +++++++ 6 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 central_frontend/src/widgets/BoolText.tsx diff --git a/central_backend/src/server/servers.rs b/central_backend/src/server/servers.rs index 4bb4f51..f6d67be 100644 --- a/central_backend/src/server/servers.rs +++ b/central_backend/src/server/servers.rs @@ -187,7 +187,11 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()> ) .route( "/web_api/relays/status", - web::get().to(relays_controller::get_status_all), + web::get().to(relays_controller::status_all), + ) + .route( + "/web_api/relay/{id}/status", + web::get().to(relays_controller::status_single), ) // Devices API .route( diff --git a/central_backend/src/server/web_api/relays_controller.rs b/central_backend/src/server/web_api/relays_controller.rs index 909d570..6405aeb 100644 --- a/central_backend/src/server/web_api/relays_controller.rs +++ b/central_backend/src/server/web_api/relays_controller.rs @@ -95,8 +95,18 @@ pub async fn delete(actor: WebEnergyActor, path: web::Path) -> Ht } /// Get the status of all relays -pub async fn get_status_all(actor: WebEnergyActor) -> HttpResult { +pub async fn status_all(actor: WebEnergyActor) -> HttpResult { let list = actor.send(energy_actor::GetAllRelaysState).await?; Ok(HttpResponse::Ok().json(list)) } + +/// Get the state of a single relay +pub async fn status_single(actor: WebEnergyActor, path: web::Path) -> HttpResult { + let list = actor.send(energy_actor::GetAllRelaysState).await?; + let Some(state) = list.into_iter().find(|r| r.id == path.id) else { + return Ok(HttpResponse::NotFound().json("Relay not found!")); + }; + + Ok(HttpResponse::Ok().json(state)) +} diff --git a/central_frontend/src/api/RelayApi.ts b/central_frontend/src/api/RelayApi.ts index 6e92d2a..53d68a4 100644 --- a/central_frontend/src/api/RelayApi.ts +++ b/central_frontend/src/api/RelayApi.ts @@ -75,4 +75,16 @@ export class RelayApi { } return map; } + + /** + * Get the status of a single relay + */ + static async SingleStatus(relay: DeviceRelay): Promise { + return ( + await APIClient.exec({ + method: "GET", + uri: `/relay/${relay.id}/state`, + }) + ).data; + } } diff --git a/central_frontend/src/routes/DeviceRoute/DeviceRelays.tsx b/central_frontend/src/routes/DeviceRoute/DeviceRelays.tsx index a0810ad..8ea8ff0 100644 --- a/central_frontend/src/routes/DeviceRoute/DeviceRelays.tsx +++ b/central_frontend/src/routes/DeviceRoute/DeviceRelays.tsx @@ -14,9 +14,12 @@ import { EditDeviceRelaysDialog } from "../../dialogs/EditDeviceRelaysDialog"; import { DeviceRouteCard } from "./DeviceRouteCard"; import { useConfirm } from "../../hooks/context_providers/ConfirmDialogProvider"; import { useLoadingMessage } from "../../hooks/context_providers/LoadingMessageProvider"; -import { RelayApi } from "../../api/RelayApi"; +import { RelayApi, RelayStatus } from "../../api/RelayApi"; import { useSnackbar } from "../../hooks/context_providers/SnackbarProvider"; import { useAlert } from "../../hooks/context_providers/AlertDialogProvider"; +import { AsyncWidget } from "../../widgets/AsyncWidget"; +import { TimeWidget } from "../../widgets/TimeWidget"; +import { BoolText } from "../../widgets/BoolText"; export function DeviceRelays(p: { device: Device; @@ -115,10 +118,35 @@ export function DeviceRelays(p: { } > - + } + /> ))} ); } + +function RelayEntryStatus(p: { relay: DeviceRelay }): React.ReactElement { + const [state, setState] = React.useState(); + + const load = async () => { + setState(await RelayApi.SingleStatus(p.relay)); + }; + + return ( + ( + <> + for{" "} + + + )} + /> + ); +} diff --git a/central_frontend/src/routes/RelaysListRoute.tsx b/central_frontend/src/routes/RelaysListRoute.tsx index 8a43e90..70317bd 100644 --- a/central_frontend/src/routes/RelaysListRoute.tsx +++ b/central_frontend/src/routes/RelaysListRoute.tsx @@ -14,6 +14,7 @@ import React from "react"; import { DeviceRelay } from "../api/DeviceApi"; import { RelayApi, RelaysStatus } from "../api/RelayApi"; import { AsyncWidget } from "../widgets/AsyncWidget"; +import { BoolText } from "../widgets/BoolText"; import { SolarEnergyRouteContainer } from "../widgets/SolarEnergyRouteContainer"; import { TimeWidget } from "../widgets/TimeWidget"; @@ -103,15 +104,3 @@ function RelaysList(p: { ); } - -function BoolText(p: { - val: boolean; - positive: string; - negative: string; -}): React.ReactElement { - return p.val ? ( - {p.positive} - ) : ( - {p.negative} - ); -} diff --git a/central_frontend/src/widgets/BoolText.tsx b/central_frontend/src/widgets/BoolText.tsx new file mode 100644 index 0000000..52eabbd --- /dev/null +++ b/central_frontend/src/widgets/BoolText.tsx @@ -0,0 +1,11 @@ +export function BoolText(p: { + val: boolean; + positive: string; + negative: string; +}): React.ReactElement { + return p.val ? ( + {p.positive} + ) : ( + {p.negative} + ); +}