-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzmonvif-events.js
executable file
·136 lines (118 loc) · 3.47 KB
/
zmonvif-events.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/local/bin/node
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
const zmuser = '';
const zmpass = '';
const {Cam} = require('onvif');
const fetch = require('node-fetch');
const {ArgumentParser} = require('argparse');
const pjson = require('./package.json');
class ZoneminderService {
constructor(basePath) {
this.basePath = basePath
}
/**
* @param {number} monitorId
* @param {boolean} state
*/
setAlarm(monitorId, state) {
const date = new Date().toLocaleString();
console.log(`[${date}] Setting monitor ${monitorId} to state ${state}`);
const cmd = state ? 'on' : 'off';
const url = `${this.basePath}api/monitors/alarm/id:${monitorId}/command:${cmd}.json`;
return fetch(url, {
method: 'POST',
body: 'user=' + zmuser + '&pass=' + zmpass,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
});
}
}
let MotionTopic = {
CELL_MOTION_DETECTOR: 'CELL_MOTION_DETECTOR',
MOTION_ALARM: 'MOTION_ALARM',
};
class Monitor {
constructor(id, onvifCam, zoneminder) {
this.id = id;
this.onvifCam = onvifCam;
this.zoneminder = zoneminder;
this.lastMotionDetectedState = null;
this.topic = MotionTopic.MOTION_ALARM;
}
log(msg, ...rest) {
console.log(`[monitor ${this.id}]: ${msg}`, ...rest);
}
async start() {
this.onvifCam.on('event', camMessage => this.onEventReceived(camMessage));
this.log('Started');
}
onEventReceived(camMessage) {
const topic = camMessage.topic._;
if (topic.match(/RuleEngine\/CellMotionDetector\/Motion$/)) {
this.onMotionDetectedEvent(camMessage);
}
}
onMotionDetectedEvent(camMessage) {
const isMotion = camMessage.message.message.data.simpleItem.$.Value;
if (this.lastMotionDetectedState !== isMotion) {
this.log(`CellMotionDetector: Motion Detected: ${isMotion}`);
this.zoneminder.setAlarm(this.id, isMotion);
}
this.lastMotionDetectedState = isMotion
}
static createCamera(conf) {
return new Promise(resolve => {
const cam = new Cam(conf, () => resolve(cam));
})
}
static async create({id, hostname, username, password, port}, zoneminder) {
const cam = await this.createCamera({
hostname,
username,
password,
port
});
return new Monitor(id, cam, zoneminder);
}
}
async function start(args) {
const zoneminder = new ZoneminderService(args.zm_base_url);
const monitor = await Monitor.create({
id: args.zm_monitor_id,
hostname: args.hostname,
username: args.username,
password: args.password,
port: args.port
}, zoneminder);
monitor.start();
}
function main() {
const parser = new ArgumentParser({
addHelp: true,
description: 'ONVIF motion detection events bridge to Zoneminder',
version: pjson.version,
});
parser.addArgument(['-z', '--zm-base-url'], {
help: 'Base URL for the Zoneminder instance (with trailing slash)',
required: true
});
parser.addArgument(['-i', '--zm-monitor-id'], {
help: 'The ID of the monitor in Zoneminder',
required: true
});
parser.addArgument(['-c', '--hostname'], {
help: 'hostname/IP of the ONVIF camera',
required: true
});
parser.addArgument(['-u', '--username'], {
help: 'username for the ONVIF camera'
});
parser.addArgument(['-p', '--password'], {
help: 'password for the ONVIF camera'
});
parser.addArgument(['-o', '--port'], {
help: 'port for the ONVIF camera'
});
const args = parser.parseArgs();
start(args);
}
main();