Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions MotionDetector.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
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<string>
) {}

static async create(
id: number,
options: CamOptions
options: CamOptions,
topics: Array<string> = TOPICS
): Promise<MotionDetector> {
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)) {
const motion = message.message.message.data.simpleItem.$.Value;
if (this.topics.includes(message?.topic?._)) {
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._);
}
}
});
Expand Down
18 changes: 10 additions & 8 deletions dist/MotionDetector.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ 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) => {
if (error) {
reject(error);
}
else {
const monitor = new MotionDetector(cam, id);
const monitor = new MotionDetector(cam, id, topics);
resolve(monitor);
}
});
Expand All @@ -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._);
}
}
});
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -22,8 +23,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",
Expand Down
13 changes: 8 additions & 5 deletions types/onvif.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ declare module 'onvif' {
$: object;
source: object;
data: {
simpleItem: {
$: {
Value: boolean;
};
};
simpleItem: SimpleItem | Array<SimpleItem>;
};
};
};
}

interface SimpleItem {
$: {
Name: string;
Value: boolean;
};
}

export interface CamOptions {
hostname: string;
username?: string;
Expand Down