157 lines
3.0 KiB
TypeScript
Raw Normal View History

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[];
}
2024-07-30 22:54:47 +02:00
export type RelayID = string;
export interface DeviceRelay {
2024-07-30 22:54:47 +02:00
id: RelayID;
name: string;
enabled: boolean;
priority: number;
consumption: number;
minimal_uptime: number;
minimal_downtime: number;
daily_runtime?: DailyMinRuntime;
2024-07-30 22:54:47 +02:00
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[];
2024-10-05 16:26:07 +02:00
desired_version?: string;
}
2024-07-22 22:19:48 +02:00
export interface UpdatedInfo {
name: string;
description: string;
enabled: boolean;
}
2024-09-09 21:27:15 +02:00
export interface DeviceState {
id: string;
last_ping: number;
online: boolean;
}
export type DevicesState = Map<string, DeviceState>;
2024-07-18 20:06:46 +02:00
export function DeviceURL(d: Device): string {
return `/dev/${encodeURIComponent(d.id)}`;
2024-07-17 23:19:04 +02:00
}
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;
}
2024-07-04 19:52:09 +02:00
/**
* Get the list of validated devices
*/
static async ValidatedList(): Promise<Device[]> {
return (
await APIClient.exec({
uri: "/devices/list_validated",
method: "GET",
})
).data;
}
2024-09-09 21:27:15 +02:00
/**
* 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;
}
2024-07-03 21:32:32 +02:00
/**
* Validate a device
*/
static async Validate(d: Device): Promise<void> {
await APIClient.exec({
uri: `/device/${encodeURIComponent(d.id)}/validate`,
method: "POST",
});
}
2024-07-03 21:10:15 +02:00
2024-07-18 20:06:46 +02:00
/**
* 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;
}
2024-09-09 21:43:57 +02:00
/**
* 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;
}
2024-07-22 22:19:48 +02:00
/**
* 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,
});
}
2024-07-03 21:10:15 +02:00
/**
* Delete a device
*/
static async Delete(d: Device): Promise<void> {
await APIClient.exec({
uri: `/device/${encodeURIComponent(d.id)}`,
method: "DELETE",
});
}
}