Skip to content

Commit

Permalink
core: move scripts into their own workers.
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Jan 1, 2024
1 parent a2d50d5 commit d7aaf57
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 38 deletions.
4 changes: 2 additions & 2 deletions plugins/core/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"port": 10081,
"request": "attach",
"skipFiles": [
"**/plugin-remote-worker.*",
"**/plugin-console.*",
"<node_internals>/**"
],
"preLaunchTask": "scrypted: deploy+debug",
"sourceMaps": true,
"localRoot": "${workspaceFolder}/out",
"remoteRoot": "/plugin/",
"type": "pwa-node"
"type": "node"
}
]
}
2 changes: 1 addition & 1 deletion plugins/core/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"scrypted.debugHost": "127.0.0.1",
"scrypted.debugHost": "scrypted-server",
}
4 changes: 2 additions & 2 deletions plugins/core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@scrypted/core",
"version": "0.1.150",
"version": "0.2.1",
"description": "Scrypted Core plugin. Provides the UI, websocket, and engine.io APIs.",
"author": "Scrypted",
"license": "Apache-2.0",
Expand Down
3 changes: 0 additions & 3 deletions plugins/core/src/builtins/timer.ts

This file was deleted.

15 changes: 9 additions & 6 deletions plugins/core/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import { AggregateCore, AggregateCoreNativeId } from './aggregate-core';
import { AutomationCore, AutomationCoreNativeId } from './automations-core';
import { LauncherMixin } from './launcher-mixin';
import { MediaCore } from './media-core';
import { ScriptCore, ScriptCoreNativeId } from './script-core';
import { UsersCore, UsersNativeId } from './user';
import { newScript, ScriptCore, ScriptCoreNativeId } from './script-core';
import { TerminalService, TerminalServiceNativeId } from './terminal-service';
import { UsersCore, UsersNativeId } from './user';

const { systemManager, deviceManager, endpointManager } = sdk;

const indexHtml = fs.readFileSync('dist/index.html').toString();

export function getAddresses() {
const addresses: string[] = [];
for (const [iface, nif] of Object.entries(os.networkInterfaces())) {
Expand Down Expand Up @@ -61,10 +59,14 @@ class ScryptedCore extends ScryptedDeviceBase implements HttpRequestHandler, Eng
},
}
});
indexHtml: string;

constructor() {
super();


this.indexHtml = fs.readFileSync('dist/index.html').toString();

(async () => {
await deviceManager.onDeviceDiscovered(
{
Expand Down Expand Up @@ -226,7 +228,7 @@ class ScryptedCore extends ScryptedDeviceBase implements HttpRequestHandler, Eng
const endpoint = await endpointManager.getPublicCloudEndpoint();
const u = new URL(endpoint);

const rewritten = indexHtml
const rewritten = this.indexHtml
.replace('href="manifest.json"', `href="manifest.json${u.search}"`)
.replace('href="img/icons/apple-touch-icon-152x152.png"', `href="img/icons/apple-touch-icon-152x152.png${u.search}"`)
.replace('href="img/icons/safari-pinned-tab.svg"', `href="img/icons/safari-pinned-tab.svg${u.search}"`)
Expand Down Expand Up @@ -269,5 +271,6 @@ export default ScryptedCore;
export async function fork() {
return {
tsCompile,
newScript,
}
}
}
58 changes: 38 additions & 20 deletions plugins/core/src/script-core.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
import { Device, DeviceCreator, DeviceCreatorSettings, DeviceProvider, Readme, ScryptedDeviceBase, ScryptedDeviceType, ScryptedInterface, Setting } from "@scrypted/sdk";
import { Device, DeviceCreator, DeviceCreatorSettings, DeviceProvider, Readme, ScryptedDeviceBase, ScryptedDeviceType, ScryptedInterface, ScryptedNativeId, Setting } from "@scrypted/sdk";
import { Script } from "./script";
import sdk from '@scrypted/sdk';
import { randomBytes } from "crypto";
import fs from 'fs';
import path from "path/posix";
import { Worker } from "worker_threads";

const { deviceManager } = sdk;
export const ScriptCoreNativeId = 'scriptcore';

interface ScriptWorker {
script: Script;
worker: Worker;
}

export class ScriptCore extends ScryptedDeviceBase implements DeviceProvider, DeviceCreator, Readme {
scripts = new Map<string, Promise<Script>>();
scripts = new Map<string, ScriptWorker>();

constructor() {
super(ScriptCoreNativeId);

for (const nativeId of deviceManager.getNativeIds()) {
if (nativeId?.startsWith('script:')) {
const script = new Script(nativeId);
this.scripts.set(nativeId, (async () => {
if (script.providedInterfaces.length > 2) {
await script.run();
}
else {
this.reportScript(nativeId);
}
return script;
})());
}
}
}

async getCreateDeviceSettings(): Promise<Setting[]> {
Expand Down Expand Up @@ -65,7 +56,6 @@ export class ScriptCore extends ScryptedDeviceBase implements DeviceProvider, De
catch (e) {
}
}
this.scripts.set(nativeId, Promise.resolve(script));
return nativeId;
}

Expand All @@ -84,10 +74,38 @@ export class ScriptCore extends ScryptedDeviceBase implements DeviceProvider, De
return await deviceManager.onDeviceDiscovered(device);
}

getDevice(nativeId: string) {
return this.scripts.get(nativeId);
async getDevice(nativeId: string) {
const e = this.scripts.get(nativeId);
if (e)
return e;
let script = new Script(nativeId);
let worker: Worker;
if (script.providedInterfaces.length > 2) {
const fork = sdk.fork<{
newScript: typeof newScript,
}>();
worker = fork.worker
try {
script = await (await fork.result).newScript(nativeId);
await script.run();
}
catch (e) {
worker.terminate();
throw e;
}
}
this.scripts.set(nativeId, {
script,
worker,
});
return script;
}

async releaseDevice(id: string, nativeId: string): Promise<void> {
this.scripts.get(nativeId)?.worker?.terminate();
}
}

export async function newScript(nativeId: ScryptedNativeId) {
return new Script(nativeId);
}
2 changes: 1 addition & 1 deletion plugins/core/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createScriptDevice, ScriptDeviceImpl } from "@scrypted/common/src/eval/
import { ScriptCoreNativeId } from "./script-core";
import { PluginAPIProxy } from "../../../server/src/plugin/plugin-api";

const { log, deviceManager, systemManager } = sdk;
const { deviceManager } = sdk;

export class Script extends ScryptedDeviceBase implements Scriptable, Program, ScriptDeviceImpl {
apiProxy: PluginAPIProxy;
Expand Down
2 changes: 1 addition & 1 deletion plugins/core/ui/src/interfaces/CameraViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export default {
const fs = 20;
const box = `<rect x="${x}" y="${y}" width="${w}" height="${h}" stroke="${s}" stroke-width="2" fill="none" />
const box = `<rect x="${x}" y="${y}" width="${w}" height="${h}" stroke="${s}" stroke-width="1px" fill="none" />
<text x="${x}" y="${y}" font-size="${fs}" dx="0.05em" dy="0.05em" fill="black">${t}</text>
<text x="${x}" y="${y}" font-size="${fs}" fill="white">${t}</text>
`;
Expand Down
2 changes: 1 addition & 1 deletion plugins/core/ui/src/interfaces/ObjectDetection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default {
for (const detection of this.lastDetection.detections || []) {
if (!detection.boundingBox) continue;
const svgScale = this.svgWidth / 1080;
const sw = 6 * svgScale;
const sw = 1;
const s = "red";
const x = detection.boundingBox[0];
const y = detection.boundingBox[1];
Expand Down

0 comments on commit d7aaf57

Please sign in to comment.