forked from nayrnet/node-hikvision-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hikvision.js
executable file
·298 lines (263 loc) · 12.8 KB
/
Hikvision.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
const net = require('net');
const events = require('events');
const request = require('request');
// const NetKeepAlive = require('net-keepalive');
const xml2js = require('xml2js');
const parser = new xml2js.Parser();
class Hikvision extends events.EventEmitter {
constructor(options) {
super();
this.options = options;
this.baseURI = 'http://' + options.host + ':' + options.port;
this.activeEvents = {};
this.triggerActive = false;
this.logToConsole = options.log;
this.connect();
}
connect() {
const authHeader = 'Authorization: Basic ' + new Buffer(this.options.user + ':' + this.options.pass).toString('base64');
// Connect
const client = net.connect(this.options, () => {
const header = 'GET /ISAPI/Event/notification/alertStream HTTP/1.1\r\n' +
'Host: ' + this.options.host + ':' + this.options.port + '\r\n' +
authHeader + '\r\n' +
'Accept: multipart/x-mixed-replace\r\n\r\n';
client.write(header);
client.setKeepAlive(true, 1000);
// NetKeepAlive.setKeepAliveInterval(client, 5000); // sets TCP_KEEPINTVL to 5s
// NetKeepAlive.setKeepAliveProbes(client, 12); // 60s and kill the connection.
this.handleConnection(this.options);
});
client.on('data', this.handleData.bind(this));
client.on('close', () => { // Try to reconnect after 30s
setTimeout(() => this.connect(this.options), 30000);
this.handleEnd();
});
client.on('error', this.handleError.bind(this));
}
ptzCommand(cmd, arg1, arg2, arg3, arg4) {
if ((!cmd) || (isNaN(arg1)) || (isNaN(arg2)) || (isNaN(arg3)) || (isNaN(arg4))) {
this.handleError(this, 'INVALID PTZ COMMAND');
return 0
}
request(this.baseURI + '/cgi-bin/ptz.cgi?action=start&channel=0&code=' + cmd + '&arg1=' + arg1 + '&arg2=' + arg2 + '&arg3=' + arg3 + '&arg4=' + arg4, (error, response, body) => {
if ((error) || (response.statusCode !== 200) || (body.trim() !== 'OK')) {
this.emit('error', 'FAILED TO ISSUE PTZ COMMAND');
}
})
}
ptzPreset(preset) {
if (isNaN(preset)) {
this.handleError('INVALID PTZ PRESET');
}
request(this.baseURI + '/cgi-bin/ptz.cgi?action=start&channel=0&code=GotoPreset&arg1=0&arg2=' + preset + '&arg3=0', (error, response, body) => {
if ((error) || (response.statusCode !== 200) || (body.trim() !== 'OK')) {
this.emit('error', 'FAILED TO ISSUE PTZ PRESET');
}
})
}
ptzZoom(multiple) {
if (isNaN(multiple)) {
this.handleError(this, 'INVALID PTZ ZOOM');
}
if (multiple === 0) {
return 0;
}
let cmd;
if (multiple > 0) {
cmd = 'ZoomTele';
}
if (multiple < 0) {
cmd = 'ZoomWide';
}
request(this.baseURI + '/cgi-bin/ptz.cgi?action=start&channel=0&code=' + cmd + '&arg1=0&arg2=' + multiple + '&arg3=0', (error, response, body) => {
if ((error) || (response.statusCode !== 200) || (body.trim() !== 'OK')) {
if (this.logToConsole) {
console.log('FAILED TO ISSUE PTZ ZOOM');
}
this.emit('error', 'FAILED TO ISSUE PTZ ZOOM');
}
})
}
ptzMove(direction, action, speed) {
if (isNaN(speed)) {
this.handleError(this, 'INVALID PTZ SPEED');
}
if ((action !== 'start') || (action !== 'stop')) {
this.handleError(this, 'INVALID PTZ COMMAND');
return 0;
}
if ((direction !== 'Up') || (direction !== 'Down') || (direction !== 'Left') || (direction !== 'Right')
(direction !== 'LeftUp') || (direction !== 'RightUp') || (direction !== 'LeftDown') || (direction !== 'RightDown')) {
this.emit('error', 'INVALID PTZ DIRECTION: ' + direction);
if (this.logToConsole) {
console.log('INVALID PTZ DIRECTION: ' + direction);
}
return 0;
}
request(this.baseURI + '/cgi-bin/ptz.cgi?action=' + action + '&channel=0&code=' + direction + '&arg1=' + speed + '&arg2=' + speed + '&arg3=0', (error, response, body) => {
if ((error) || (response.statusCode !== 200) || (body.trim() !== 'OK')) {
this.emit('error', 'FAILED TO ISSUE PTZ UP COMMAND');
if (this.logToConsole) console.log('FAILED TO ISSUE PTZ UP COMMAND');
}
})
}
ptzStatus() {
request(this.baseURI + '/cgi-bin/ptz.cgi?action=getStatus', (error, response, body) => {
if ((!error) && (response.statusCode === 200)) {
body = body.toString().split('\r\n').trim();
if (this.logToConsole) {
console.log('PTZ STATUS: ' + body);
}
this.emit('ptzStatus', body);
} else {
this.emit('error', 'FAILED TO QUERY STATUS');
if (this.logToConsole) {
console.log('FAILED TO QUERY STATUS');
}
}
})
}
dayProfile() {
request(this.baseURI + '/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=1', (error, response, body) => {
if ((!error) && (response.statusCode === 200)) {
if (body === 'Error') { // Didnt work, lets try another method for older cameras
request(this.baseURI + '/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions[0].NightOptions.SwitchMode=0', (error, response) => {
if ((error) || (response.statusCode !== 200)) {
this.emit('error', 'FAILED TO CHANGE TO DAY PROFILE');
if (this.logToConsole) {
console.log('FAILED TO CHANGE TO DAY PROFILE');
}
}
})
}
} else {
this.emit('error', 'FAILED TO CHANGE TO DAY PROFILE');
if (this.logToConsole) {
console.log('FAILED TO CHANGE TO DAY PROFILE');
}
}
})
}
nightProfile() {
request(this.baseURI + '/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=2', (error, response, body) => {
if ((!error) && (response.statusCode === 200)) {
if (body === 'Error') { // Didnt work, lets try another method for older cameras
request(this.baseURI + '/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions[0].NightOptions.SwitchMode=3', (error, response) => {
if ((error) || (response.statusCode !== 200)) {
this.emit('error', 'FAILED TO CHANGE TO NIGHT PROFILE');
if (this.logToConsole) {
console.log('FAILED TO CHANGE TO NIGHT PROFILE');
}
}
})
}
} else {
this.emit('error', 'FAILED TO CHANGE TO NIGHT PROFILE');
if (this.logToConsole) {
console.log('FAILED TO CHANGE TO NIGHT PROFILE');
}
}
})
}
// Handle alarms
handleData(data) {
parser.parseString(data, (err, result) => {
if (result && 'EventNotificationAlert' in result) {
let code = result['EventNotificationAlert']['eventType'][0];
let action = result['EventNotificationAlert']['eventState'][0];
const index = parseInt('channelID' in result['EventNotificationAlert'] ? result['EventNotificationAlert']['channelID'][0] : result['EventNotificationAlert']['dynChannelID'][0]);
const count = parseInt(result['EventNotificationAlert']['activePostCount'][0]);
// give codes returned by camera prettier and standardized description
if (code === 'IO') code = 'AlarmLocal';
if (code === 'VMD') code = 'VideoMotion';
if (code === 'linedetection') code = 'LineDetection';
if (code === 'videoloss') code = 'VideoLoss';
if (code === 'shelteralarm') code = 'VideoBlind';
if (action === 'active') action = 'Start';
if (action === 'inactive') action = 'Stop';
// create and event identifier for each recieved event
// This allows multiple detection types with multiple indexes for DVR or multihead devices
const eventIdentifier = code + index;
// Count 0 seems to indicate everything is fine and nothing is wrong, used as a heartbeat
// if triggerActive is true, lets step through the activeEvents
// If activeEvents has something, lets end those events and clear activeEvents and reset triggerActive
if (count === 0) {
if (this.triggerActive === true) {
for (let i in this.activeEvents) {
if (this.activeEvents.hasOwnProperty(i)) {
let eventDetails = this.activeEvents[i];
if (this.logToConsole) {
console.log('Ending Event: ' + i + ' - ' + eventDetails['code'] + ' - ' + ((Date.now() - eventDetails['lasttimestamp']) / 1000));
}
this.emit('alarm', eventDetails['code'], 'Stop', eventDetails['index']);
}
}
this.activeEvents = {};
this.triggerActive = false;
} else {
// should be the most common result
// Nothing interesting happening and we haven't seen any events
if (this.logToConsole) {
this.emit('alarm', code, action, index);
}
}
}
// if the first instance of an eventIdentifier, lets emit it,
// add to activeEvents and set triggerActive
else if (this.activeEvents[eventIdentifier] === undefined || this.activeEvents[eventIdentifier] === null) {
const eventDetails = {};
eventDetails['code'] = code;
eventDetails['index'] = index;
eventDetails['lasttimestamp'] = Date.now();
this.activeEvents[eventIdentifier] = eventDetails;
this.emit('alarm', code, action, index);
this.triggerActive = true
// known active events
} else {
if (this.logToConsole) {
console.log(' Skipped Event: ' + code + ' ' + action + ' ' + index + ' ' + count);
}
// Update lasttimestamp
const eventDetails = {};
eventDetails['code'] = code;
eventDetails['index'] = index;
eventDetails['lasttimestamp'] = Date.now();
this.activeEvents[eventIdentifier] = eventDetails;
// step through activeEvents
// if we haven't seen it in more than 2 seconds, lets end it and remove from activeEvents
for (let i in this.activeEvents) {
if (this.activeEvents.hasOwnProperty(i)) {
const eventDetails = this.activeEvents[i];
if (((Date.now() - eventDetails['lasttimestamp']) / 1000) > 2) {
if (this.logToConsole) console.log(' Ending Event: ' + i + ' - ' + eventDetails['code'] + ' - ' + ((Date.now() - eventDetails['lasttimestamp']) / 1000));
this.emit('alarm', eventDetails['code'], 'Stop', eventDetails['index']);
delete this.activeEvents[i]
}
}
}
}
}
});
}
handleConnection(options) {
if (this.logToConsole) {
console.log('Connected to ' + options.host + ':' + options.port);
}
//this.socket = socket;
this.emit('connect');
}
handleEnd() {
if (this.logToConsole) {
console.log('Connection closed!');
}
this.emit('end');
}
handleError(err) {
if (this.logToConsole) {
console.log('Connection error: ' + err);
}
this.emit('error', err);
}
}
module.exports = Hikvision;