diff --git a/src/components/automation/MonacoEditor.vue b/src/components/automation/MonacoEditor.vue index 773f1e0..dc50c04 100644 --- a/src/components/automation/MonacoEditor.vue +++ b/src/components/automation/MonacoEditor.vue @@ -58,7 +58,9 @@ function editorWillMount(monacoInstance: typeof monaco): void { type DeviceAttributeModifier = 'ro' | 'rw' | 'wo'; type DeviceAttributeType = 'bool' | 'list' | 'str' | 'float' | 'int' | 'range'; type DeviceAttributeValue = string | number | boolean | undefined; -interface DeviceAttribute +declare type DeviceEventType = 'deviceConnected' | 'deviceDisconnected' | 'deviceRefreshed'; + +declare interface DeviceAttribute { name: string; label?: string; @@ -66,27 +68,28 @@ interface DeviceAttribute modifier: DeviceAttributeModifier; value: DeviceAttributeValue; } -interface Device -{ - getDeviceId: string - getAttribute(attrName: string): Promise - setAttribute(attrName: string, value: DeviceAttributeValue): Promise + +declare interface Device { + readonly getDeviceId: string; + readonly getDeviceName: string; + getAttribute(name: string): Promise; + setAttribute(name: string, value: DeviceAttributeValue): Promise; } -enum DeviceEventType { - deviceUpdateReceived = "deviceUpdateReceived", - deviceConnected = "deviceConnected", - deviceDisconnected = "deviceDisconnected", - deviceRefreshed = "deviceRefreshed", + +declare interface DeviceEvent { + readonly type: DeviceEventType; + readonly device: Device; } -type DeviceEvent = { type: DeviceEventType, device: Device } -interface DeviceRepositoryInterface -{ + +declare function onEvent(handler: (event: DeviceEvent) => void | Promise): void; +declare function onStart(handler: () => void | Promise): void; +declare function onStop(handler: () => void | Promise): void; + +declare const devices: { + getById(id: string): Device | null; getAll(): Device[]; - getById(uuid: string): Device|null; -} -declare const event: DeviceEvent; -declare const devices: DeviceRepositoryInterface; -declare const context: { [key: string]: any }; +}; + declare const console: { log: (...args: any[]) => void; error: (...args: any[]) => void; diff --git a/src/stores/automation.ts b/src/stores/automation.ts index 50378ad..6501ea9 100644 --- a/src/stores/automation.ts +++ b/src/stores/automation.ts @@ -3,17 +3,26 @@ import { ref, computed } from "vue"; import type AutomationScript from "../model/AutomationScript"; import {apiFetch} from "@/utils/apiFetch"; -const defaultCode = `(async () => { - // Write your automation here. The script will fire on device events: +const defaultCode = `onStart(async () => { + // This function will fire once when your script starts. + // You can initialize variables or set device attributes here. +}); + +onStop(async () => { + // This function will fire once when your script stops. + // You can clean up any resources or reset device attributes here. +}); + +onEvent(async (event) => { + // This function will be called on any device event. Possible events are: // - // - deviceUpdateReceived: A device got updated through the external API // - deviceConnected: A new device was connected to SlvCtrl+ // - deviceDisconnected: A device was disconnected from SlvCtrl+ // - deviceRefreshed: New data has been pulled from a device // - // You can get information about the event and the device that invoked your script through the global "event" - // variable. To access other devices you can use the global variable "devices" -> devices.getById(\`{device uuid}\`) -})(); + // You can get information about the event and the device that invoked your script through the "event" + // variable. To access other devices you can use the global variable "devices" -> devices.getById("{device uuid}") +}); `; export const useAutomationStore = defineStore("automation", () => {