Files
SolarEnergy/central_frontend/src/api/RelayApi.ts
Pierre HUBERT abdca20a66
All checks were successful
continuous-integration/drone/push Build is passing
Can set relay forced state from UI
2025-10-29 17:49:03 +01:00

114 lines
2.2 KiB
TypeScript

import { APIClient } from "./ApiClient";
import { Device, DeviceRelay } from "./DeviceApi";
export type RelayForcedState =
| { type: "None" }
| { type: "Off" | "On"; until: number };
export type SetRelayForcedState =
| { type: "None" }
| { type: "Off" | "On"; for_secs: number };
export interface RelayStatus {
id: string;
on: boolean;
for: number;
forced_state: RelayForcedState;
}
export type RelaysStatus = Map<string, RelayStatus>;
export class RelayApi {
/**
* Get the full list of relays
*/
static async GetList(): Promise<DeviceRelay[]> {
return (
await APIClient.exec({
method: "GET",
uri: "/relays/list",
})
).data;
}
/**
* Create a new relay
*/
static async Create(device: Device, relay: DeviceRelay): Promise<void> {
await APIClient.exec({
method: "POST",
uri: "/relay/create",
jsonData: {
...relay,
id: undefined,
device_id: device.id,
},
});
}
/**
* Update a relay information
*/
static async Update(relay: DeviceRelay): Promise<void> {
await APIClient.exec({
method: "PUT",
uri: `/relay/${relay.id}`,
jsonData: relay,
});
}
/**
* Set relay forced state
*/
static async SetForcedState(
relay: DeviceRelay,
forced: SetRelayForcedState
): Promise<void> {
await APIClient.exec({
method: "PUT",
uri: `/relay/${relay.id}/forced_state`,
jsonData: forced,
});
}
/**
* Delete a relay configuration
*/
static async Delete(relay: DeviceRelay): Promise<void> {
await APIClient.exec({
method: "DELETE",
uri: `/relay/${relay.id}`,
});
}
/**
* Get the status of all relays
*/
static async GetRelaysStatus(): Promise<RelaysStatus> {
const data: any[] = (
await APIClient.exec({
method: "GET",
uri: `/relays/status`,
})
).data;
const map = new Map();
for (let r of data) {
map.set(r.id, r);
}
return map;
}
/**
* Get the status of a single relay
*/
static async SingleStatus(relay: DeviceRelay): Promise<RelayStatus> {
return (
await APIClient.exec({
method: "GET",
uri: `/relay/${relay.id}/status`,
})
).data;
}
}