From aafb709702e52dbe059bb6423d82ec478c8b63aa Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Mon, 27 Sep 2021 23:55:37 -0700 Subject: [PATCH] onvif: logging --- plugins/onvif/package-lock.json | 4 ++-- plugins/onvif/package.json | 2 +- plugins/onvif/src/main.ts | 15 +++++++++++++-- plugins/onvif/src/onvif-api.ts | 16 ++++++++++++---- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/plugins/onvif/package-lock.json b/plugins/onvif/package-lock.json index e76cbca5d..41ab4c2fe 100644 --- a/plugins/onvif/package-lock.json +++ b/plugins/onvif/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/onvif", - "version": "0.0.11", + "version": "0.0.18", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/onvif", - "version": "0.0.11", + "version": "0.0.18", "license": "Apache", "dependencies": { "@scrypted/sdk": "file:../../sdk", diff --git a/plugins/onvif/package.json b/plugins/onvif/package.json index da948e02f..f18e34f5e 100644 --- a/plugins/onvif/package.json +++ b/plugins/onvif/package.json @@ -1,6 +1,6 @@ { "name": "@scrypted/onvif", - "version": "0.0.11", + "version": "0.0.18", "description": "ONVIF Camera Plugin for Scrypted", "author": "Scrypted", "license": "Apache", diff --git a/plugins/onvif/src/main.ts b/plugins/onvif/src/main.ts index 0fe5712dc..fc9edacb3 100644 --- a/plugins/onvif/src/main.ts +++ b/plugins/onvif/src/main.ts @@ -9,6 +9,7 @@ const { mediaManager } = sdk; class OnvifCamera extends RtspSmartCamera { eventStream: Stream; client: OnvifCameraAPI; + streamUrl; listenEvents(): EventEmitter & Destroyable { (async () => { @@ -32,7 +33,7 @@ class OnvifCamera extends RtspSmartCamera { } createClient() { - return connectCameraAPI(this.storage.getItem('ip'), this.getUsername(), this.getPassword()); + return connectCameraAPI(this.storage.getItem('ip'), this.getUsername(), this.getPassword(), this.console); } async takePicture(): Promise { @@ -42,7 +43,17 @@ class OnvifCamera extends RtspSmartCamera { } async getConstructedStreamUrl() { - return `rtsp://${this.getRtspAddress()}/cam/realmonitor?channel=1&subtype=0`; + try { + if (!this.streamUrl) { + if (!this.client) + this.client = await this.createClient(); + this.streamUrl = await this.client.getStreamUrl(); + } + return this.streamUrl; + } + catch (e) { + return `rtsp://${this.getRtspAddress()}/cam/realmonitor?channel=1&subtype=0`; + } } } diff --git a/plugins/onvif/src/onvif-api.ts b/plugins/onvif/src/onvif-api.ts index 7ae4516aa..728fadd4a 100644 --- a/plugins/onvif/src/onvif-api.ts +++ b/plugins/onvif/src/onvif-api.ts @@ -34,13 +34,18 @@ export class OnvifCameraAPI { digestAuth: DigestClient; mainProfileToken: Promise; - constructor(public cam: any, username: string, password: string) { + constructor(public cam: any, username: string, password: string, public console: Console) { this.digestAuth = new DigestClient(username, password); } listenEvents() { const ret = new EventEmitter(); + this.cam.getEventProperties((err, results) => { + this.console.log(results); + }) + this.cam.on('event', (event: any) => { + this.console.log('onvif event', event); const eventTopic = stripNamespaces(event.topic._) if (event.message.message.data && event.message.message.data.simpleItem) { @@ -75,7 +80,7 @@ export class OnvifCameraAPI { async getStreamUrl(): Promise { const token = await this.getMainProfileToken(); - return new Promise((resolve, reject) => this.cam.getStreamUri({ protocol: 'RTSP', profileToken: token }, (err: Error, uri: string) => err ? reject(err) : resolve(uri))); + return new Promise((resolve, reject) => this.cam.getStreamUri({ protocol: 'RTSP', profileToken: token }, (err: Error, uri: any) => err ? reject(err) : resolve(uri.uri))); } async jpegSnapshot(): Promise { @@ -89,15 +94,18 @@ export class OnvifCameraAPI { } } -export async function connectCameraAPI(hostname: string, username: string, password: string) { +export async function connectCameraAPI(ipAndPort: string, username: string, password: string, console: Console) { + const split = ipAndPort.split(':'); + const [hostname, port] = split; const cam = await new Promise((resolve, reject) => { const cam = new Cam({ hostname, username, password, + port, }, (err: Error) => err ? reject(err) : resolve(cam) ) }); - return new OnvifCameraAPI(cam, username, password); + return new OnvifCameraAPI(cam, username, password, console); } \ No newline at end of file