Skip to content

Commit

Permalink
sdk: update with createdDevice string
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Jul 8, 2024
1 parent f5c324b commit bd28cd1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 44 deletions.
88 changes: 45 additions & 43 deletions plugins/objectdetector/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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<any> {
let ret: any;
if (nativeId === 'ffmpeg')
Expand Down
14 changes: 13 additions & 1 deletion sdk/types/scrypted_python/scrypted_sdk/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,7 @@ async def setColorTemperature(self, kelvin: float) -> None:
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

Expand Down Expand Up @@ -1756,6 +1757,7 @@ class ScryptedInterfaceProperty(str, Enum):
ptzCapabilities = "ptzCapabilities"
lockState = "lockState"
entryOpen = "entryOpen"
createdDevice = "createdDevice"
batteryLevel = "batteryLevel"
chargeState = "chargeState"
online = "online"
Expand Down Expand Up @@ -2151,6 +2153,14 @@ def entryOpen(self) -> bool | Any:
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")
Expand Down Expand Up @@ -2717,7 +2727,9 @@ def applicationInfo(self, value: LauncherApplicationInfo):
"createDevice",
"getCreateDeviceSettings"
],
"properties": []
"properties": [
"createdDevice"
]
},
"Battery": {
"name": "Battery",
Expand Down
5 changes: 5 additions & 0 deletions sdk/types/src/types.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,11 @@ export interface DeviceCreator {
* Return the id of the created device.
*/
createDevice(settings: DeviceCreatorSettings): Promise<string>;
/**
* 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;
Expand Down

0 comments on commit bd28cd1

Please sign in to comment.