-
Notifications
You must be signed in to change notification settings - Fork 1
/
dahua.js
executable file
·130 lines (111 loc) · 3.4 KB
/
dahua.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
#!/usr/bin/nodejs
// Dahua HTTP API Module
var events = require('events');
const rp = require('request-promise');
const request = require('request');
const debug = require('debug')('http')
class DahuaCam extends events.EventEmitter {
constructor(options) {
super();
this.username = options.username;
this.password = options.password;
this.baseUri = `http://${options.host}:${options.port || 80}`;
};
// set up persistent connection to receive alarm events from camera
listenForEvents(eventNames) {
//defer this module load as it's only for listening
let NetKeepAlive = require('net-keepalive')
eventNames = eventNames || [
'VideoMotion',
'VideoLoss',
'VideoBlind',
'AlarmLocal',
'CrossLineDetection',
'CrossRegionDetection',
'LeftDetection',
'TakenAwayDetection',
'VideoAbnormalDetection',
'FaceDetection',
'AudioMutation',
'AudioAnomaly',
'VideoUnFocus',
'WanderDetection',
'RioterDetection' ];
let client = request({
url : `${this.baseUri}/cgi-bin/eventManager.cgi?action=attach&codes=[${eventNames.join(",")}]`,
forever : true,
headers: {'Accept':'multipart/x-mixed-replace'},
auth: {
user: this.username,
password: this.password,
sendImmediately: false
}
});
client.on('socket', (socket) => {
NetKeepAlive.setKeepAliveInterval(socket, 1000);
NetKeepAlive.setKeepAliveProbes(socket, 1);
});
client.on('response', () => {
debug(`Connected to ${this.baseUri}`);
this.emit("connect")
});
client.on('error', err => this.emit("error", err));
client.on('data', this.handleDahuaEventData.bind(this));
client.on('close', () => { // Try to reconnect after 30s
() => setTimeout(() => this.listenForEvents(eventNames), 30000);
this.emit("end");
});
};
_req(path) {
return rp({
uri: `${this.baseUri}/cgi-bin/${path}`,
auth: {
user: this.username,
password: this.password,
sendImmediately: false
}
});
}
name() {
return this._req('magicBox.cgi?action=getMachineName').then(d => d.split('=')[1].trim());
}
snapshot(channel) {
return new Promise((resolve, reject) => {
let chunks = [];
request({'uri' : this.baseUri + '/cgi-bin/snapshot.cgi?' + channel})
.auth(this.camUser, this.camPass, false)
.on('data', chunk => chunks.push(chunk))
.on('end', () => resolve(Buffer.concat(chunks)))
.on('error', reject)
})
}
handleDahuaEventData(data) {
data.toString()
.split('\r\n')
.filter(s => s.startsWith('Code=')) //Code=VideoMotion;action=Start;index=0
.map(s => s.split(';'))
.forEach(segments => {
let [code, action, index] = segments.map(s => s.split('=')[1]);
this.emit("alarm", code, action, index);
})
}
}
class KeepAliveAgent extends require('http').Agent {
constructor() {
super({
keepAlive: true
})
}
createSocket(req, options, cb) {
super.createSocket(req, options, (err, socket) => {
if(!err) {
socket.on('connect', () => {
NetKeepAlive.setKeepAliveInterval(socket, 1000);
NetKeepAlive.setKeepAliveProbes(socket, 1);
});
}
cb(err, socket);
});
}
}
exports.DahuaCam = DahuaCam;