-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.js
80 lines (69 loc) · 2.04 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
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
let RaspiCam = require('raspicam'),
uuidV4 = require('uuid/v4'),
RADAR_IMAGE_LOCAL_PATH = process.env.RADAR_IMAGE_LOCAL_PATH,
_redis = require('redis');
let redis = {
client: _redis.createClient()
}
redis.client.on('ready', function () {
redis.client.subscribe('motion-channel');
});
redis.client.on('message', function (channel, message) {
console.log('CAMERA: client channel ' + channel + ': ' + message);
// take a picture if more than 3 seconds has gone by, then back off
let uuid = message.split('|')[3];
Camera({uuid: uuid, w: 1920, h: 1080}, function (err, filename) {
if (err) {
return redis.client.set('error', err, _redis.print);
}
console.info('Photo Saved:', message, filename);
});
});
function Camera (config, callback) {
var camera = new RaspiCam({
mode: "photo",
output: `${RADAR_IMAGE_LOCAL_PATH}/image-${config.uuid || uuidV4()}.jpg`,
encoding: config.encoding || "jpg",
timeout: config.tomeout || 2,
w: config.w || 640,
h: config.h || 480
}),
fileName;
function reportError (err, callback) {
if (err) {
if (typeof callback === 'function') {
return callback(err);
}
return console.error(err);
}
}
camera.on("start", function(err, timestamp) {
if (err) {
console.error(err);
reportError(err, callback);
}
console.log("photo started at " + timestamp );
});
camera.on("read", function(err, timestamp, filename) {
if (err) {
console.error(err);
reportError(err, callback);
}
if (filename.indexOf('~') > -1) {
// Rename operation hoses the file capture!
return;
}
console.log("photo image captured with filename: " + filename);
fileName = filename;
});
camera.on("stop", function () {
console.log('Camera stop event');
});
camera.on("exit", function(timestamp) {
console.log("photo child process has exited at " + timestamp);
if (typeof callback === 'function') {
callback(null, fileName);
}
});
camera.start();
}