-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcamera.js
48 lines (38 loc) · 1.35 KB
/
camera.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
const cameraConfig = require('./camera.json');
const request = require('request');
const socket = require('socket.io-client')(cameraConfig.server_url);
let cameraByName = [];
cameraConfig.cams.forEach( function(c, i) {
cameraByName[c.name] = i;
});
socket.on('connect', function() {
console.log('> connected');
socket.emit('register', Object.keys(cameraByName));
});
socket.on('getSnapshot', function(data) {
if( cameraByName[data.camera] !== undefined ) {
let camera = cameraConfig.cams[ cameraByName[data.camera] ];
request.get( {
url: camera.url,
encoding: null
},
function(err, response, body) {
if( response !== undefined ) {
data.image = 'data:' + response.headers['content-type'] + ';base64,' + new Buffer(body).toString('base64');
data.error = 0;
socket.emit('sendSnapshot', data);
} else {
data.error = 1;
socket.emit('sendSnapshot', data);
}
console.log('- sendSnapshot');
}
).auth(camera.login, camera.password, false);
} else {
data.error = 1;
socket.emit('sendSnapshot', data);
}
});
socket.on('disconnect', function() {
console.log('< disconnected');
});