-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
89 lines (77 loc) · 2.54 KB
/
index.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
'use strict';
var express = require('express'),
app = express(),
http = require('http').Server(app),
io = require('socket.io')(http),
fs = require('fs'),
path = require('path'),
kill = require('tree-kill'),
cp = require('child_process'),
mqtt = require('mqtt'),
motion, mqttClient, currentSocket,
Camelittle = require('camelittle'),
clInstance = new Camelittle({
resolution: '640x480',
'no-banner': null,
frames: 1
});
app.use('/', express.static(path.join(__dirname, 'stream')));
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
function sendImage() {
kill(motion.pid, 'SIGKILL', function(err) {
if (!err) {
clInstance.grab(function(err, image) {
if (!err) {
fs.writeFile('./stream/image.jpg', image, 'binary', function() {
motion = cp.spawn('motion');
currentSocket.emit('refresh');
fs.readFile('./stream/image.jpg', function(err, data) {
if (err) {
console.error(err);
} else {
var image = new Buffer(data).toString('base64');
mqttConnect();
mqttClient.publish('reekoh/data', JSON.stringify({
image: image
}), function() {
console.log('sent');
});
}
});
});
} else
console.error(err);
});
} else
console.error(err);
});
}
function mqttConnect(){
mqttClient = mqtt.connect('mqtt://iotexpo.reekoh.com:19001', {
clientId: 'raspi1',
keepalive: 64800
});
mqttClient.subscribe('raspi1');
mqttClient.on('message', function(topic, payload) {
if (payload.toString() === 'capture-image') {
currentSocket.emit('commandReceived');
mqttClient.end();
sendImage();
}
});
console.log('connected to mqtt');
}
io.on('connection', function(socket) {
currentSocket = socket;
socket.on('capture-image', function() {
mqttClient.end();
sendImage();
});
});
http.listen(3000, function() {
console.log('listening on *:3000');
motion = cp.spawn('motion');
mqttConnect();
});