From 2c8162cb1adf4e7b09abf88df1de93545d3d738d Mon Sep 17 00:00:00 2001 From: Krzysztof Date: Thu, 17 Aug 2023 11:44:44 +0200 Subject: [PATCH 1/5] build: Unable to resolve dependency tree --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 11c6220..13be76b 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,8 @@ "license": "ISC", "devDependencies": { "@types/node": "^15.0.1", - "@typescript-eslint/eslint-plugin": "^2.31.0", - "@typescript-eslint/parser": "^2.31.0", + "@typescript-eslint/eslint-plugin": "^3.0.0", + "@typescript-eslint/parser": "^3.0.0", "eslint": "^7.0.0", "eslint-config-prettier": "^6.11.0", "eslint-plugin-prettier": "^3.1.3", From 74b2d0c521d6d87e2f20046ee6bbbd5a6a2db247 Mon Sep 17 00:00:00 2001 From: Krzysztof Date: Thu, 20 Jul 2023 20:39:26 +0200 Subject: [PATCH 2/5] build: Add npm build script --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 13be76b..eff71b3 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "An implementation of Node.js that detects the events of your camera that works with the onvif protocol", "main": "dist/index.js", "scripts": { + "build": "tsc --build", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { From 438c46cf03370dac0a4f5cbd2b159adcefdbed71 Mon Sep 17 00:00:00 2001 From: Krzysztof Date: Wed, 21 Jun 2023 16:53:36 +0200 Subject: [PATCH 3/5] feat: Allow listening to custom topics --- MotionDetector.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/MotionDetector.ts b/MotionDetector.ts index d0713dd..f144685 100644 --- a/MotionDetector.ts +++ b/MotionDetector.ts @@ -1,35 +1,40 @@ import { Cam, NotificationMessage, CamOptions } from 'onvif'; -const TOPIC = /RuleEngine\/CellMotionDetector\/Motion$/; +const TOPICS = ['tns1:RuleEngine/CellMotionDetector/Motion']; export class MotionDetector { lastIsMotion: boolean = false; - private constructor(private cam: Cam, private id: number) {} + private constructor( + private cam: Cam, + private id: number, + private topics: Array + ) {} static async create( id: number, - options: CamOptions + options: CamOptions, + topics: Array = TOPICS ): Promise { return new Promise((resolve, reject) => { const cam = new Cam(options, (error) => { if (error) { reject(error); } else { - const monitor = new MotionDetector(cam, id); + const monitor = new MotionDetector(cam, id, topics); resolve(monitor); } }); }); } - listen(onMotion: (motion: boolean, id: number) => void) { + listen(onMotion: (motion: boolean, id: number, topic: string) => void) { this.cam.on('event', (message: NotificationMessage) => { - if (message?.topic?._?.match(TOPIC)) { + if (this.topics.includes(message?.topic?._)) { const motion = message.message.message.data.simpleItem.$.Value; if (motion !== this.lastIsMotion) { this.lastIsMotion = motion; - onMotion(motion, this.id); + onMotion(motion, this.id, message.topic._); } } }); From 349a57b4cec4988ad195e3ceea432d7ef18674cb Mon Sep 17 00:00:00 2001 From: Krzysztof Date: Wed, 21 Jun 2023 19:28:47 +0200 Subject: [PATCH 4/5] fix: Read IsMotion state from array of SimpleItems --- MotionDetector.ts | 5 ++++- types/onvif.d.ts | 13 ++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/MotionDetector.ts b/MotionDetector.ts index f144685..5a9981c 100644 --- a/MotionDetector.ts +++ b/MotionDetector.ts @@ -31,7 +31,10 @@ export class MotionDetector { listen(onMotion: (motion: boolean, id: number, topic: string) => void) { this.cam.on('event', (message: NotificationMessage) => { if (this.topics.includes(message?.topic?._)) { - const motion = message.message.message.data.simpleItem.$.Value; + const simpleItem = message.message.message.data.simpleItem; + const motion = ( + simpleItem instanceof Array ? simpleItem[0] : simpleItem + ).$.Value; if (motion !== this.lastIsMotion) { this.lastIsMotion = motion; onMotion(motion, this.id, message.topic._); diff --git a/types/onvif.d.ts b/types/onvif.d.ts index 0955ca8..dd40923 100644 --- a/types/onvif.d.ts +++ b/types/onvif.d.ts @@ -10,16 +10,19 @@ declare module 'onvif' { $: object; source: object; data: { - simpleItem: { - $: { - Value: boolean; - }; - }; + simpleItem: SimpleItem | Array; }; }; }; } + interface SimpleItem { + $: { + Name: string; + Value: boolean; + }; + } + export interface CamOptions { hostname: string; username?: string; From 7ee24bba5e24470c552644e1e3b122a34da68650 Mon Sep 17 00:00:00 2001 From: Krzysztof Date: Fri, 18 Aug 2023 14:44:59 +0200 Subject: [PATCH 5/5] chore: Add transpiled code --- dist/MotionDetector.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/dist/MotionDetector.js b/dist/MotionDetector.js index 5c62d5f..0480e3b 100644 --- a/dist/MotionDetector.js +++ b/dist/MotionDetector.js @@ -11,14 +11,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge Object.defineProperty(exports, "__esModule", { value: true }); exports.MotionDetector = void 0; const onvif_1 = require("onvif"); -const TOPIC = /RuleEngine\/CellMotionDetector\/Motion$/; +const TOPICS = ['tns1:RuleEngine/CellMotionDetector/Motion']; class MotionDetector { - constructor(cam, id) { + constructor(cam, id, topics) { this.cam = cam; this.id = id; + this.topics = topics; this.lastIsMotion = false; } - static create(id, options) { + static create(id, options, topics = TOPICS) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { const cam = new onvif_1.Cam(options, (error) => { @@ -26,7 +27,7 @@ class MotionDetector { reject(error); } else { - const monitor = new MotionDetector(cam, id); + const monitor = new MotionDetector(cam, id, topics); resolve(monitor); } }); @@ -35,12 +36,13 @@ class MotionDetector { } listen(onMotion) { this.cam.on('event', (message) => { - var _a, _b; - if ((_b = (_a = message === null || message === void 0 ? void 0 : message.topic) === null || _a === void 0 ? void 0 : _a._) === null || _b === void 0 ? void 0 : _b.match(TOPIC)) { - const motion = message.message.message.data.simpleItem.$.Value; + var _a; + if (this.topics.includes((_a = message === null || message === void 0 ? void 0 : message.topic) === null || _a === void 0 ? void 0 : _a._)) { + const simpleItem = message.message.message.data.simpleItem; + const motion = (simpleItem instanceof Array ? simpleItem[0] : simpleItem).$.Value; if (motion !== this.lastIsMotion) { this.lastIsMotion = motion; - onMotion(motion, this.id); + onMotion(motion, this.id, message.topic._); } } });