import { APIClient } from "./ApiClient"; import { Device, DeviceRelay } from "./DeviceApi"; export interface RelayStatus { id: string; on: boolean; for: number; } export type RelaysStatus = Map; export class RelayApi { /** * Get the full list of relays */ static async GetList(): Promise { return ( await APIClient.exec({ method: "GET", uri: "/relays/list", }) ).data; } /** * Create a new relay */ static async Create(device: Device, relay: DeviceRelay): Promise { 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 { await APIClient.exec({ method: "PUT", uri: `/relay/${relay.id}`, jsonData: relay, }); } /** * Delete a relay configuration */ static async Delete(relay: DeviceRelay): Promise { await APIClient.exec({ method: "DELETE", uri: `/relay/${relay.id}`, }); } /** * Get the status of all relays */ static async GetRelaysStatus(): Promise { 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 { return ( await APIClient.exec({ method: "GET", uri: `/relay/${relay.id}/status`, }) ).data; } }