Skip to content

Commit

Permalink
videoanalysis: smart motion sensor now retains the last thumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Dec 20, 2023
1 parent 05751bc commit b3a7d6b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
4 changes: 2 additions & 2 deletions plugins/objectdetector/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/objectdetector/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@scrypted/objectdetector",
"version": "0.1.16",
"version": "0.1.17",
"description": "Scrypted Video Analysis Plugin. Installed alongside a detection service like OpenCV or TensorFlow.",
"author": "Scrypted",
"license": "Apache-2.0",
Expand Down
36 changes: 28 additions & 8 deletions plugins/objectdetector/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,18 @@ class ObjectDetectionMixin extends SettingsMixinDeviceBase<VideoCamera & Camera

const ret: { [key: string]: any } = {};
for (const setting of this.settings) {
ret[setting.key] = (setting.multiple ? safeParseJson(this.storage.getItem(setting.key)) : this.storage.getItem(setting.key))
|| setting.value;
let value: any;
if (setting.multiple) {
value = safeParseJson(this.storage.getItem(setting.key));
if (!value?.length)
value = undefined;
}
else {
value = this.storage.getItem(setting.key);
}
value ||= setting.value;

ret[setting.key] = value;
}

if (this.hasMotionType)
Expand Down Expand Up @@ -656,13 +666,22 @@ class ObjectDetectionMixin extends SettingsMixinDeviceBase<VideoCamera & Camera
}

if (this.settings) {
settings.push(...this.settings.map(setting =>
Object.assign({}, setting, {
settings.push(...this.settings.map(setting => {
let value: any;
if (setting.multiple) {
value = safeParseJson(this.storage.getItem(setting.key));
if (!value?.length)
value = undefined;
}
else {
value = this.storage.getItem(setting.key);
}
value ||= setting.value;
return Object.assign({}, setting, {
placeholder: setting.placeholder?.toString(),
value: (setting.multiple ? safeParseJson(this.storage.getItem(setting.key)) : this.storage.getItem(setting.key))
|| setting.value,
} as Setting))
);
value,
} as Setting);
}));
}

this.storageSettings.settings.motionSensorSupplementation.hide = !this.hasMotionType || !this.mixinDeviceInterfaces.includes(ScryptedInterface.MotionSensor);
Expand Down Expand Up @@ -1170,6 +1189,7 @@ export class ObjectDetectionPlugin extends AutoenableMixinProvider implements Se
name,
type: ScryptedDeviceType.Sensor,
interfaces: [
ScryptedInterface.Camera,
ScryptedInterface.MotionSensor,
ScryptedInterface.Settings,
ScryptedInterface.Readme,
Expand Down
31 changes: 28 additions & 3 deletions plugins/objectdetector/src/smart-motionsensor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sdk, { EventListenerRegister, MotionSensor, ObjectDetector, ObjectsDetected, Readme, ScryptedDevice, ScryptedDeviceBase, ScryptedDeviceType, ScryptedInterface, ScryptedNativeId, Setting, SettingValue, Settings } from "@scrypted/sdk";
import sdk, { Camera, EventListenerRegister, MediaObject, MotionSensor, ObjectDetector, ObjectsDetected, Readme, RequestPictureOptions, ResponsePictureOptions, ScryptedDevice, ScryptedDeviceBase, ScryptedDeviceType, ScryptedInterface, ScryptedNativeId, Setting, SettingValue, Settings } from "@scrypted/sdk";
import { StorageSetting, StorageSettings } from "@scrypted/sdk/storage-settings";
import type { ObjectDetectionPlugin } from "./main";

Expand All @@ -14,7 +14,7 @@ export function createObjectDetectorStorageSetting(): StorageSetting {
};
}

export class SmartMotionSensor extends ScryptedDeviceBase implements Settings, Readme, MotionSensor {
export class SmartMotionSensor extends ScryptedDeviceBase implements Settings, Readme, MotionSensor, Camera {
storageSettings = new StorageSettings(this, {
objectDetector: createObjectDetectorStorageSetting(),
detections: {
Expand All @@ -36,10 +36,11 @@ export class SmartMotionSensor extends ScryptedDeviceBase implements Settings, R
combobox: true,
choices: [
],
}
},
});
listener: EventListenerRegister;
timeout: NodeJS.Timeout;
lastPicture: Promise<MediaObject>;

constructor(public plugin: ObjectDetectionPlugin, nativeId?: ScryptedNativeId) {
super(nativeId);
Expand Down Expand Up @@ -76,6 +77,28 @@ export class SmartMotionSensor extends ScryptedDeviceBase implements Settings, R
};

this.rebind();

if (!this.providedInterfaces.includes(ScryptedInterface.Camera)) {
sdk.deviceManager.onDeviceDiscovered({
name: this.providedName,
nativeId: this.nativeId,
type: this.providedType,
interfaces: [
ScryptedInterface.Camera,
ScryptedInterface.MotionSensor,
ScryptedInterface.Settings,
ScryptedInterface.Readme,
]
})
}
}

async takePicture(options?: RequestPictureOptions): Promise<MediaObject> {
return this.lastPicture;
}

async getPictureOptions(): Promise<ResponsePictureOptions[]> {
return;
}

resetTrigger() {
Expand Down Expand Up @@ -138,6 +161,8 @@ export class SmartMotionSensor extends ScryptedDeviceBase implements Settings, R
if (match) {
if (!this.motionDetected)
console.log('Smart Motion Sensor triggered on', match);
if (detected.detectionId)
this.lastPicture = objectDetector.getDetectionInput(detected.detectionId, details.eventId);
this.trigger();
}
});
Expand Down

0 comments on commit b3a7d6b

Please sign in to comment.