Skip to content

Commit ec725af

Browse files
committedMay 23, 2020
initial commit
0 parents  commit ec725af

File tree

4 files changed

+3298
-0
lines changed

4 files changed

+3298
-0
lines changed
 

‎LICENSE.txt

Whitespace-only changes.

‎dahua.js

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/nodejs
2+
// Dahua HTTP API Module
3+
var events = require('events');
4+
5+
const NetKeepAlive = require('net-keepalive')
6+
const rp = require('request-promise');
7+
const request = require('request');
8+
const debug = require('debug')('http')
9+
10+
class DahuaCam extends events.EventEmitter {
11+
constructor(options) {
12+
super();
13+
14+
this.username = options.username;
15+
this.password = options.password;
16+
17+
this.baseUri = `http://${options.hostname}:${options.port || 80}`;
18+
};
19+
20+
// set up persistent connection to receive alarm events from camera
21+
listenForEvents(eventNames) {
22+
eventNames = eventNames || [
23+
'VideoMotion',
24+
'VideoLoss',
25+
'VideoBlind',
26+
'AlarmLocal',
27+
'CrossLineDetection',
28+
'CrossRegionDetection',
29+
'LeftDetection',
30+
'TakenAwayDetection',
31+
'VideoAbnormalDetection',
32+
'FaceDetection',
33+
'AudioMutation',
34+
'AudioAnomaly',
35+
'VideoUnFocus',
36+
'WanderDetection',
37+
'RioterDetection' ];
38+
39+
let client = request({
40+
url : `${this.baseUri}/cgi-bin/eventManager.cgi?action=attach&codes=[${eventNames.join(",")}]`,
41+
forever : true,
42+
headers: {'Accept':'multipart/x-mixed-replace'},
43+
auth: {
44+
user: this.username,
45+
password: this.password,
46+
sendImmediately: false
47+
}
48+
});
49+
50+
client.on('socket', (socket) => {
51+
NetKeepAlive.setKeepAliveInterval(socket, 1000);
52+
NetKeepAlive.setKeepAliveProbes(socket, 1);
53+
});
54+
55+
client.on('response', () => {
56+
debug(`Connected to ${this.baseUri}`);
57+
this.emit("connect")
58+
});
59+
client.on('error', err => this.emit("error", err));
60+
client.on('data', this.handleDahuaEventData.bind(this));
61+
client.on('close', () => { // Try to reconnect after 30s
62+
() => setTimeout(() => this.listenForEvents(eventNames), 30000);
63+
this.emit("end");
64+
});
65+
};
66+
67+
_req(path) {
68+
return rp({
69+
uri: `${this.baseUri}/cgi-bin/${path}`,
70+
auth: {
71+
user: this.username,
72+
password: this.password,
73+
sendImmediately: false
74+
}
75+
});
76+
}
77+
78+
name() {
79+
return this._req('magicBox.cgi?action=getMachineName').then(d => d.split('=')[1]);
80+
}
81+
82+
snapshot(channel) {
83+
return new Promise((resolve, reject) => {
84+
let chunks = [];
85+
request({'uri' : this.baseUri + '/cgi-bin/snapshot.cgi?' + channel})
86+
.auth(this.camUser, this.camPass, false)
87+
.on('data', chunk => chunks.push(chunk))
88+
.on('end', () => resolve(Buffer.concat(chunks)))
89+
.on('error', reject)
90+
})
91+
}
92+
93+
handleDahuaEventData(data) {
94+
data.toString()
95+
.split('\r\n')
96+
.filter(s => s.startsWith('Code=')) //Code=VideoMotion;action=Start;index=0
97+
.map(s => s.split(';'))
98+
.forEach(segments => {
99+
let [code, action, index] = segments.map(s => s.split('=')[1]);
100+
this.emit("alarm", code, action, index);
101+
})
102+
}
103+
}
104+
105+
class KeepAliveAgent extends require('http').Agent {
106+
constructor() {
107+
super({
108+
keepAlive: true
109+
})
110+
}
111+
112+
createSocket(req, options, cb) {
113+
super.createSocket(req, options, (err, socket) => {
114+
115+
if(!err) {
116+
socket.on('connect', () => {
117+
NetKeepAlive.setKeepAliveInterval(socket, 1000);
118+
NetKeepAlive.setKeepAliveProbes(socket, 1);
119+
});
120+
}
121+
122+
cb(err, socket);
123+
});
124+
}
125+
}
126+
127+
exports.DahuaCam = DahuaCam;

‎package-lock.json

+3,146
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"dependencies": {
3+
"debug": "^4.1.1",
4+
"net-keepalive": "^1.3.0",
5+
"request": "^2.88.2",
6+
"request-promise": "^4.2.5"
7+
},
8+
"description": "Dahua API Module",
9+
"devDependencies": {},
10+
"keywords": [
11+
"Dahua",
12+
"PTZ",
13+
"IPC",
14+
"Security Camera",
15+
"Home Automation"
16+
],
17+
"license": "GPL-3.0",
18+
"main": "dahua.js",
19+
"name": "dahua-cam",
20+
"repository": {
21+
"type": "git",
22+
"url": "git+https://github.com/jpg0/dahua-cam.git"
23+
},
24+
"version": "1.0.0"
25+
}

0 commit comments

Comments
 (0)
Please sign in to comment.