From 90b6fc1e498536315c58ffd553deb62bd5828386 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Tue, 23 Jul 2024 09:22:15 -0700 Subject: [PATCH] core/mqtt: scripting fixes --- common/src/eval/monaco-libs.ts | 4 +- common/src/eval/monaco/script-device.ts | 4 ++ plugins/core/package-lock.json | 4 +- plugins/core/package.json | 2 +- plugins/mqtt/src/api/util.ts | 64 +------------------------ plugins/mqtt/src/monaco.ts | 6 +-- 6 files changed, 13 insertions(+), 71 deletions(-) diff --git a/common/src/eval/monaco-libs.ts b/common/src/eval/monaco-libs.ts index 50c208b59..8901ce5cc 100644 --- a/common/src/eval/monaco-libs.ts +++ b/common/src/eval/monaco-libs.ts @@ -45,7 +45,7 @@ export function createMonacoEvalDefaultsWithLibs(standardLibs: StandardLibs, scr const libs: any = { ...scryptedLibs, - extraLibs, + ...extraLibs, }; const catLibs = Object.values(libs).join('\n'); @@ -65,8 +65,6 @@ export function createMonacoEvalDefaultsWithLibs(standardLibs: StandardLibs, scr const endpointManager: EndpointManager; const mediaManager: MediaManager; const systemManager: SystemManager; - const mqtt: MqttClient; - const device: ScryptedDeviceBase & { pathname : string }; const eventSource: ScryptedDevice; const eventDetails: EventDetails; diff --git a/common/src/eval/monaco/script-device.ts b/common/src/eval/monaco/script-device.ts index 448941367..84b473d3c 100644 --- a/common/src/eval/monaco/script-device.ts +++ b/common/src/eval/monaco/script-device.ts @@ -1,3 +1,5 @@ +import type { ScryptedDeviceBase } from "@scrypted/sdk"; + export interface ScriptDevice { /** * @deprecated Use the default export to specify the device handler. @@ -6,3 +8,5 @@ export interface ScriptDevice { handle(handler?: T & object): void; handleTypes(...interfaces: string[]): void; } + +export declare const device: ScryptedDeviceBase & ScriptDevice; diff --git a/plugins/core/package-lock.json b/plugins/core/package-lock.json index 9330de6fc..bcf04728a 100644 --- a/plugins/core/package-lock.json +++ b/plugins/core/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/core", - "version": "0.3.57", + "version": "0.3.58", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/core", - "version": "0.3.57", + "version": "0.3.58", "license": "Apache-2.0", "dependencies": { "@scrypted/common": "file:../../common", diff --git a/plugins/core/package.json b/plugins/core/package.json index 91f32d815..5f3ca7423 100644 --- a/plugins/core/package.json +++ b/plugins/core/package.json @@ -1,6 +1,6 @@ { "name": "@scrypted/core", - "version": "0.3.57", + "version": "0.3.58", "description": "Scrypted Core plugin. Provides the UI, websocket, and engine.io APIs.", "author": "Scrypted", "license": "Apache-2.0", diff --git a/plugins/mqtt/src/api/util.ts b/plugins/mqtt/src/api/util.ts index 851837e24..c442ea8db 100644 --- a/plugins/mqtt/src/api/util.ts +++ b/plugins/mqtt/src/api/util.ts @@ -1,63 +1,3 @@ -import type { ScryptedDeviceBase } from "@scrypted/sdk"; -import type { MqttClient, MqttEvent, MqttSubscriptions } from "./mqtt-client"; +import type { MqttClient } from "./mqtt-client"; -declare const device: ScryptedDeviceBase; -declare const mqtt: MqttClient; - -export function createSensor(options: { - type: string, - topic: string, - when: (message: MqttEvent) => boolean; - set: (value: boolean) => void, - delay?: number; -}) { - const subscriptions: MqttSubscriptions = {}; - let timeout: NodeJS.Timeout; - subscriptions[options.topic] = message => { - const detected = options.when(message); - - if (!options.delay) { - options.set(detected); - return; - } - - if (!detected) - return; - - options.set(true); - clearTimeout(timeout); - timeout = setTimeout(() => options.set(false), options.delay * 1000); - }; - - mqtt.subscribe(subscriptions); - - mqtt.handleTypes(options.type); -} - -export function createMotionSensor(options: { - topic: string, - when: (message: MqttEvent) => boolean; - delay?: number; -}) { - return createSensor({ - type: "MotionSensor", - topic: options.topic, - set: (value: boolean) => device.motionDetected = value, - when: options.when, - delay: options.delay, - }) -} - -export function createBinarySensor(options: { - topic: string, - when: (message: MqttEvent) => boolean; - delay?: number; -}) { - return createSensor({ - type: "BinarySensor", - topic: options.topic, - set: (value: boolean) => device.binaryState = value, - when: options.when, - delay: options.delay, - }) -} +export declare const mqtt: MqttClient; diff --git a/plugins/mqtt/src/monaco.ts b/plugins/mqtt/src/monaco.ts index 864724201..f67f0edc7 100644 --- a/plugins/mqtt/src/monaco.ts +++ b/plugins/mqtt/src/monaco.ts @@ -1,9 +1,9 @@ import { createMonacoEvalDefaults } from "@scrypted/common/src/eval/scrypted-eval"; const libs = { - script: require("!!raw-loader!@scrypted/common/src/eval/monaco/script-device.ts").default, - client: require("!!raw-loader!./api/mqtt-client.ts").default, - util: require("!!raw-loader!./api/util.ts").default, + '@types/scrypted/common/script-device.d.ts': require("!!raw-loader!@scrypted/common/src/eval/monaco/script-device.ts").default, + '@types/scrypted/mqtt/mqtt-client.d.ts': require("!!raw-loader!./api/mqtt-client.ts").default, + '@types/scrypted/mqtt/util.d.ts': require("!!raw-loader!./api/util.ts").default, }; export const monacoEvalDefaults = createMonacoEvalDefaults(libs);