156 lines
3.0 KiB
TypeScript

import { APIClient } from "./ApiClient";
export interface DeviceInfo {
reference: string;
version: string;
max_relays: number;
}
export interface DailyMinRuntime {
min_runtime: number;
reset_time: number;
catch_up_hours: number[];
}
export type RelayID = string;
export interface DeviceRelay {
id: RelayID;
name: string;
enabled: boolean;
priority: number;
consumption: number;
minimal_uptime: number;
minimal_downtime: number;
daily_runtime?: DailyMinRuntime;
depends_on: RelayID[];
conflicts_with: RelayID[];
}
export interface Device {
id: string;
info: DeviceInfo;
time_create: number;
time_update: number;
name: string;
description: string;
validated: boolean;
enabled: boolean;
relays: DeviceRelay[];
}
export interface UpdatedInfo {
name: string;
description: string;
enabled: boolean;
}
export interface DeviceState {
id: string;
last_ping: number;
online: boolean;
}
export type DevicesState = Map<string, DeviceState>;
export function DeviceURL(d: Device): string {
return `/dev/${encodeURIComponent(d.id)}`;
}
export class DeviceApi {
/**
* Get the list of pending devices
*/
static async PendingList(): Promise<Device[]> {
return (
await APIClient.exec({
uri: "/devices/list_pending",
method: "GET",
})
).data;
}
/**
* Get the list of validated devices
*/
static async ValidatedList(): Promise<Device[]> {
return (
await APIClient.exec({
uri: "/devices/list_validated",
method: "GET",
})
).data;
}
/**
* Get the state of devices
*/
static async DevicesState(): Promise<DevicesState> {
const devs: DeviceState[] = (
await APIClient.exec({
uri: "/devices/state",
method: "GET",
})
).data;
const m = new Map();
devs.forEach((d) => m.set(d.id, d));
return m;
}
/**
* Validate a device
*/
static async Validate(d: Device): Promise<void> {
await APIClient.exec({
uri: `/device/${encodeURIComponent(d.id)}/validate`,
method: "POST",
});
}
/**
* Get the information about a single device
*/
static async GetSingle(id: string): Promise<Device> {
return (
await APIClient.exec({
uri: `/device/${encodeURIComponent(id)}`,
method: "GET",
})
).data;
}
/**
* Get the current state of a single device
*/
static async GetSingleState(id: string): Promise<DeviceState> {
return (
await APIClient.exec({
uri: `/device/${encodeURIComponent(id)}/state`,
method: "GET",
})
).data;
}
/**
* Update a device general information
*/
static async Update(d: Device, info: UpdatedInfo): Promise<void> {
await APIClient.exec({
uri: `/device/${encodeURIComponent(d.id)}`,
method: "PATCH",
jsonData: info,
});
}
/**
* Delete a device
*/
static async Delete(d: Device): Promise<void> {
await APIClient.exec({
uri: `/device/${encodeURIComponent(d.id)}`,
method: "DELETE",
});
}
}