Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions src/components/automation/MonacoEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,38 @@ 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;
type: DeviceAttributeType;
modifier: DeviceAttributeModifier;
value: DeviceAttributeValue;
}
interface Device
{
getDeviceId: string
getAttribute(attrName: string): Promise<DeviceAttribute | undefined>
setAttribute(attrName: string, value: DeviceAttributeValue): Promise<void>

declare interface Device {
readonly getDeviceId: string;
readonly getDeviceName: string;
getAttribute(name: string): Promise<DeviceAttribute | undefined>;
setAttribute(name: string, value: DeviceAttributeValue): Promise<void>;
}
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>): void;
declare function onStart(handler: () => void | Promise<void>): void;
declare function onStop(handler: () => void | Promise<void>): 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;
Expand Down
21 changes: 15 additions & 6 deletions src/stores/automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Loading