91 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			91 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { APIClient } from "./ApiClient";
 | |
| import { Device, DeviceRelay } from "./DeviceApi";
 | |
| 
 | |
| export interface RelayStatus {
 | |
|   id: string;
 | |
|   on: boolean;
 | |
|   for: number;
 | |
| }
 | |
| 
 | |
| 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,
 | |
|     });
 | |
|   }
 | |
| 
 | |
|   /**
 | |
|    * 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;
 | |
|   }
 | |
| }
 |