From bd28cd1766dbb4606f3f0b588bfd77abf0c5e4ee Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Sun, 7 Jul 2024 22:28:42 -0700 Subject: [PATCH] sdk: update with createdDevice string --- plugins/objectdetector/src/main.ts | 88 ++++++++++--------- .../scrypted_python/scrypted_sdk/types.py | 14 ++- sdk/types/src/types.input.ts | 5 ++ 3 files changed, 63 insertions(+), 44 deletions(-) diff --git a/plugins/objectdetector/src/main.ts b/plugins/objectdetector/src/main.ts index 6deb7ce89..a28974438 100644 --- a/plugins/objectdetector/src/main.ts +++ b/plugins/objectdetector/src/main.ts @@ -1013,6 +1013,51 @@ export class ObjectDetectionPlugin extends AutoenableMixinProvider implements Se cpuTimer = new CpuTimer(); cpuUsage = 0; + constructor(nativeId?: ScryptedNativeId) { + super(nativeId, 'v5'); + + this.createdDeviceName = 'Smart Motion Sensor'; + + process.nextTick(() => { + sdk.deviceManager.onDeviceDiscovered({ + name: 'FFmpeg Frame Generator', + type: ScryptedDeviceType.Builtin, + interfaces: [ + ScryptedInterface.VideoFrameGenerator, + ], + nativeId: 'ffmpeg', + }) + }); + + setInterval(() => { + this.cpuUsage = this.cpuTimer.sample(); + // this.console.log('cpu usage', Math.round(this.cpuUsage * 100)); + + const runningDetections = this.runningObjectDetections; + + let allowStart = 2; + // always allow 2 cameras to push past cpu throttling + if (runningDetections.length > 2) { + const cpuPerDetector = this.cpuUsage / runningDetections.length; + allowStart = Math.ceil(1 / cpuPerDetector) - runningDetections.length; + if (allowStart <= 0) + return; + } + + const idleDetectors = [...this.currentMixins.values()] + .map(d => [...d.currentMixins.values()].filter(dd => !dd.hasMotionType)).flat() + .filter(c => !c.detectorRunning); + + for (const notRunning of idleDetectors) { + if (notRunning.maybeStartDetection()) { + allowStart--; + if (allowStart <= 0) + return; + } + } + }, 10000) + } + pruneOldStatistics() { const now = Date.now(); for (const [k, v] of this.objectDetectionStatistics.entries()) { @@ -1101,49 +1146,6 @@ export class ObjectDetectionPlugin extends AutoenableMixinProvider implements Se this.statsSnapshotTime = now; } - constructor(nativeId?: ScryptedNativeId) { - super(nativeId, 'v5'); - - process.nextTick(() => { - sdk.deviceManager.onDeviceDiscovered({ - name: 'FFmpeg Frame Generator', - type: ScryptedDeviceType.Builtin, - interfaces: [ - ScryptedInterface.VideoFrameGenerator, - ], - nativeId: 'ffmpeg', - }) - }); - - setInterval(() => { - this.cpuUsage = this.cpuTimer.sample(); - // this.console.log('cpu usage', Math.round(this.cpuUsage * 100)); - - const runningDetections = this.runningObjectDetections; - - let allowStart = 2; - // always allow 2 cameras to push past cpu throttling - if (runningDetections.length > 2) { - const cpuPerDetector = this.cpuUsage / runningDetections.length; - allowStart = Math.ceil(1 / cpuPerDetector) - runningDetections.length; - if (allowStart <= 0) - return; - } - - const idleDetectors = [...this.currentMixins.values()] - .map(d => [...d.currentMixins.values()].filter(dd => !dd.hasMotionType)).flat() - .filter(c => !c.detectorRunning); - - for (const notRunning of idleDetectors) { - if (notRunning.maybeStartDetection()) { - allowStart--; - if (allowStart <= 0) - return; - } - } - }, 10000) - } - async getDevice(nativeId: string): Promise { let ret: any; if (nativeId === 'ffmpeg') diff --git a/sdk/types/scrypted_python/scrypted_sdk/types.py b/sdk/types/scrypted_python/scrypted_sdk/types.py index 4722e6cb4..e842f2a4a 100644 --- a/sdk/types/scrypted_python/scrypted_sdk/types.py +++ b/sdk/types/scrypted_python/scrypted_sdk/types.py @@ -982,6 +982,7 @@ class ColorSettingTemperature: class DeviceCreator: """A DeviceProvider that allows the user to create a device.""" + createdDevice: str # Type of device that will be created by this DeviceCreator. For example: Example Corp Camera or ACME Light Switch. async def createDevice(self, settings: DeviceCreatorSettings) -> str: pass @@ -1756,6 +1757,7 @@ class ScryptedInterfaceProperty(str, Enum): ptzCapabilities = "ptzCapabilities" lockState = "lockState" entryOpen = "entryOpen" + createdDevice = "createdDevice" batteryLevel = "batteryLevel" chargeState = "chargeState" online = "online" @@ -2151,6 +2153,14 @@ class DeviceState: def entryOpen(self, value: bool | Any): self.setScryptedProperty("entryOpen", value) + @property + def createdDevice(self) -> str: + return self.getScryptedProperty("createdDevice") + + @createdDevice.setter + def createdDevice(self, value: str): + self.setScryptedProperty("createdDevice", value) + @property def batteryLevel(self) -> float: return self.getScryptedProperty("batteryLevel") @@ -2717,7 +2727,9 @@ ScryptedInterfaceDescriptors = { "createDevice", "getCreateDeviceSettings" ], - "properties": [] + "properties": [ + "createdDevice" + ] }, "Battery": { "name": "Battery", diff --git a/sdk/types/src/types.input.ts b/sdk/types/src/types.input.ts index bc74f5ef1..82ac295de 100644 --- a/sdk/types/src/types.input.ts +++ b/sdk/types/src/types.input.ts @@ -1037,6 +1037,11 @@ export interface DeviceCreator { * Return the id of the created device. */ createDevice(settings: DeviceCreatorSettings): Promise; + /** + * Type of device that will be created by this DeviceCreator. + * For example: Example Corp Camera or ACME Light Switch. + */ + createdDevice?: string; } export interface DiscoveredDevice { name: string;