onvif: logging

This commit is contained in:
Koushik Dutta
2021-09-27 23:55:37 -07:00
parent 672a00b1ad
commit aafb709702
4 changed files with 28 additions and 9 deletions

View File

@@ -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",

View File

@@ -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",

View File

@@ -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<MediaObject> {
@@ -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`;
}
}
}

View File

@@ -34,13 +34,18 @@ export class OnvifCameraAPI {
digestAuth: DigestClient;
mainProfileToken: Promise<string>;
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<string> {
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<Buffer> {
@@ -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);
}