From 2dc15e13bebb218b28834aa46eca2e5813e052e2 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Mon, 21 Mar 2022 09:26:01 -0700 Subject: [PATCH] snapshot: add default snapshot channel option --- plugins/snapshot/package-lock.json | 4 +-- plugins/snapshot/package.json | 2 +- plugins/snapshot/src/main.ts | 43 +++++++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/plugins/snapshot/package-lock.json b/plugins/snapshot/package-lock.json index 99c54ceb4..06a202cd0 100644 --- a/plugins/snapshot/package-lock.json +++ b/plugins/snapshot/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/snapshot", - "version": "0.0.15", + "version": "0.0.16", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/snapshot", - "version": "0.0.15", + "version": "0.0.16", "dependencies": { "@koush/axios-digest-auth": "^0.8.5", "@types/node": "^16.6.1", diff --git a/plugins/snapshot/package.json b/plugins/snapshot/package.json index c5719b042..24498b404 100644 --- a/plugins/snapshot/package.json +++ b/plugins/snapshot/package.json @@ -1,6 +1,6 @@ { "name": "@scrypted/snapshot", - "version": "0.0.15", + "version": "0.0.16", "description": "Snapshot Plugin for Scrypted", "scripts": { "prepublishOnly": "NODE_ENV=production scrypted-webpack", diff --git a/plugins/snapshot/src/main.ts b/plugins/snapshot/src/main.ts index 90580bdd1..b70b68287 100644 --- a/plugins/snapshot/src/main.ts +++ b/plugins/snapshot/src/main.ts @@ -23,6 +23,31 @@ class NeverWaitError extends Error { class SnapshotMixin extends SettingsMixinDeviceBase implements Camera { storageSettings = new StorageSettings(this, { + defaultSnapshotChannel: { + title: 'Default Snapshot Channel', + description: 'The default channel to use for snapshots.', + defaultValue: 'Camera Default', + hide: true, + onGet: async () => { + let psos: PictureOptions[]; + try { + psos = await this.mixinDevice.getPictureOptions(); + } + catch (e) { + } + + if (!psos?.length) + return {}; + + return { + hide: false, + choices: [ + 'Camera Default', + ...psos.map(pso => pso.name), + ], + }; + } + }, snapshotUrl: { title: this.mixinDeviceInterfaces.includes(ScryptedInterface.Camera) ? 'Override Snapshot URL' @@ -77,7 +102,7 @@ class SnapshotMixin extends SettingsMixinDeviceBase implements Camera { } async takePicture(options?: RequestPictureOptions): Promise { - let takePicture: () => Promise; + let takePicture: (options?: PictureOptions) => Promise; if (this.storageSettings.values.snapshotsFromPrebuffer) { try { const realDevice = systemManager.getDeviceById(this.id); @@ -102,11 +127,23 @@ class SnapshotMixin extends SettingsMixinDeviceBase implements Camera { if (!takePicture) { if (!this.storageSettings.values.snapshotUrl) { if (this.mixinDeviceInterfaces.includes(ScryptedInterface.Camera)) { - takePicture = async () => { + takePicture = async (options?: PictureOptions) => { // if operating in full resolution mode, nuke any picture options containing // the requested dimensions that are sent. if (this.storageSettings.values.snapshotResolution === 'Full Resolution' && options) options.picture = undefined; + + if (!options?.id && this.storageSettings.values.defaultSnapshotChannel !== 'Camera Default') { + try { + const psos = await this.mixinDevice.getPictureOptions(); + const pso = psos.find(pso => pso.name === this.storageSettings.values.defaultSnapshotChannel); + if (!options) + options = {}; + options.id = pso.id; + } + catch (e) { + } + } return this.mixinDevice.takePicture(options).then(mo => mediaManager.convertMediaObjectToBuffer(mo, 'image/jpeg')) }; } @@ -264,7 +301,7 @@ class SnapshotMixin extends SettingsMixinDeviceBase implements Camera { } async getPictureOptions() { - return undefined; + return this.mixinDevice.getPictureOptions(); } getMixinSettings(): Promise {