Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var CHANNEL, CHUNK_SIZE, CLIENT_NAME, H_RES, PHOTO_INTERVAL, V_RES, child, fs, imageCallback, pn, pnCfg;
fs = require('fs'); // included with nodejs
child = require('child_process'); // included with nodejs
CLIENT_NAME = 'dashcam-001';
CHANNEL = 'dashcam-demo';
CHUNK_SIZE = 1024 * 28;
PHOTO_INTERVAL = 10000;
H_RES = (400).toString();
V_RES = (300).toString();
pnCfg = {
publish_key: 'YOUR_PUB_KEY',
subscribe_key: 'YOUR_SUB_KEY',
uuid: CLIENT_NAME,
ssl: true
};
pn = (require('pubnub')).init(pnCfg);
process.on('SIGINT', function() {
console.log('Dashcam Stopped');
return process.exit();
});
imageCallback = function() {
var args, fname, spawn, ts;
ts = new Date().toISOString().replace(/[:\-]/g, "").replace(/\.\d{3}/g, "");
fname = "photo/image_" + ts + ".jpg";
args = ['-w', H_RES, '-h', V_RES, '-o', fname, '-t', '1', '-vf'];
spawn = child.spawn('raspistill', args);
return spawn.on('exit', function(code) {
var buffer, content, i, imagePt, imgLen, msg, total;
buffer = fs.readFileSync(fname);
content = "data:image/jpeg;base64," + buffer.toString('base64').replace(/\n/g, "");
total = Math.ceil(content.length / CHUNK_SIZE);
console.log(CLIENT_NAME + " publish: " + fname);
i = 0;
while (i < total) {
imgLen = i < total - 1 ? CHUNK_SIZE : content.length % CHUNK_SIZE;
imagePt = content.substring(i * CHUNK_SIZE, (i * CHUNK_SIZE) + imgLen);
msg = {
uuid: CLIENT_NAME,
ts: ts,
i: i,
n: total,
imgPart: imagePt
};
i += 1;
pn.publish({
channel: CHANNEL,
message: msg
});
}
});
};
setInterval(imageCallback, PHOTO_INTERVAL);
console.log('Dashcam Starting');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.