Skip to content

Commit

Permalink
If alarm system goes off, send photo along with the message (desktop …
Browse files Browse the repository at this point in the history
…notification + pushover). Still bug in pushover, image corrupted - but getting close
  • Loading branch information
imbrianj committed Feb 20, 2018
1 parent b86048f commit ebce1b2
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 54 deletions.
50 changes: 41 additions & 9 deletions apps/homeWatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = (function () {
'use strict';

return {
version : 20161101,
version : 20180219,

homeWatch : function (deviceId, command, controllers, values, config) {
var notify = require(__dirname + '/../lib/notify'),
Expand All @@ -54,33 +54,65 @@ module.exports = (function () {

return found;
},
isSecure = function () {
isArmed = function () {
var deviceState = require(__dirname + '/../lib/deviceState'),
smartThingsState = deviceState.getDeviceState(deviceId),
isSecure = false;
isArmed = false;

if ((smartThingsState) && (smartThingsState.value) && (smartThingsState.value.mode)) {
if (secureModes.indexOf(smartThingsState.value.mode) !== -1) {
isSecure = true;
isArmed = true;
}
}

return isSecure;
return isArmed;
},
findCamera = function() {
var device,
camera;

for (device in controllers) {
if ((controllers[device]) && (controllers[device].config) && (controllers[device].config.typeClass === 'foscam')) {
camera = device;
break;
}
}

return camera;
};

trigger = isIn(command, config.motion, 'subdevice-state-motion-') ||
isIn(command, config.contact, 'subdevice-state-contact-');

if (trigger && isSecure()) {
if (trigger && isArmed()) {
// We use a delay here of a few seconds, as Presence sensors may take a
// few seconds to register and update the mode. We want to avoid false
// positives as much as possible.
setTimeout(function () {
if(isSecure()) {
console.log('\x1b[35m' + controllers[deviceId].config.title + '\x1b[0m: Home Watch found something suspicious');
var camera = findCamera(controllers),
runCommand,
callback;

if(isArmed()) {
console.log('\x1b[35m' + controllers[deviceId].config.title + '\x1b[0m: Home Watch found something suspicious');
message = translate.translate('{{i18n_HOME_WATCH}}', 'smartthings', controllers.config.language).split('{{LABEL}}').join(trigger);
notify.notify(message, controllers, deviceId);

if (camera) {
runCommand = require(__dirname + '/../lib/runCommand');

callback = function(err, dataReply) {
var rawImage = dataReply.rawImage,
fileName = dataReply.fileName;

notify.notify(message, controllers, camera, fileName, rawImage);
};

runCommand.runCommand(camera, 'TAKE', deviceId, null, null, callback);
}

else {
notify.notify(message, controllers, deviceId);
}
}
}, delay);
}
Expand Down
2 changes: 1 addition & 1 deletion cache/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1518939337696
1519085681168
2 changes: 1 addition & 1 deletion config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ exports.config = {
motion : ['Living Room Motion'],
secureModes : ['Away'],
delay : 15,
controllerIds : ['pushover', 'sms', 'clientSpeech', 'clientNotify', 'gerty'] }
controllerIds : ['foscam', 'pushover', 'sms', 'clientSpeech', 'clientNotify', 'gerty'] }
},
*/
className : { Goblin : 'fa-female' },
Expand Down
4 changes: 2 additions & 2 deletions devices/clientNotify/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = (function () {
* clients.
*/
return {
version : 20151007,
version : 20180219,

inputs : ['text'],

Expand Down Expand Up @@ -58,7 +58,7 @@ module.exports = (function () {
clientNotify = {};

clientNotify.options = {};
clientNotify.options.icon = config.device.image || '/images/icons/apple-touch-icon.png';
clientNotify.options.icon = config.payload || '/images/icons/apple-touch-icon.png';
clientNotify.options.body = config.text || '';
clientNotify.title = config.device.title || 'SwitchBoard';
clientNotify.deviceId = config.source || '';
Expand Down
95 changes: 58 additions & 37 deletions devices/foscam/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = (function () {
* @fileoverview Basic control of Foscam IP camera.
*/
return {
version : 20180214,
version : 20180219,

inputs : ['command', 'list'],

Expand Down Expand Up @@ -106,6 +106,32 @@ module.exports = (function () {
return clean;
},

getRawPhoto : function (foscam, fileName) {
var title = foscam.title,
http = require('http'),
postData = this.postPrepare(foscam),
imageUrl = 'http://' + postData.host + ':' + postData.port + postData.path,
request,
dataReply = '',
path = '/images/foscam/photos/';

request = http.request(imageUrl).on('response', function (response) {
response.setEncoding('binary');

response.on('data', function (response) {
dataReply += response;
});

response.once('end', function () {
console.log('\x1b[35m' + title + '\x1b[0m: Read raw image');

foscam.callback(null, { rawImage : dataReply, fileName : path + fileName }, true);
});
});

request.end();
},

/**
* Grab all thumbnail path and assume corresponding screenshot and videos.
*/
Expand Down Expand Up @@ -162,7 +188,7 @@ module.exports = (function () {
getStoredPhotos : function (foscam) {
var fs = require('fs'),
deviceState = require(__dirname + '/../../lib/deviceState'),
path = 'images/foscam/photos',
path = '/images/foscam/photos/',
that = this,
photos = [],
filename;
Expand All @@ -182,7 +208,7 @@ module.exports = (function () {
if (filename.indexOf(foscam.deviceId + '-') === 0) {
photos.push({
name : filename,
photo : path + '/' + filename + '.jpg'
photo : path + filename + '.jpg'
});
}
}
Expand Down Expand Up @@ -243,6 +269,10 @@ module.exports = (function () {
var http = require('http'),
fs = require('fs'),
that = this,
foscam = {},
dataReply = '',
request,
title = config.device.title,
now = new Date(),
year = now.getFullYear(),
month = ('0' + (now.getMonth() + 1)).slice(-2),
Expand All @@ -251,14 +281,11 @@ module.exports = (function () {
minute = ('0' + (now.getMinutes())).slice(-2),
second = ('0' + (now.getSeconds())).slice(-2),
localPath = __dirname + '/../../images/foscam/photos/',
fileName = localPath + '/' + config.device.deviceId + '-' + year + '-' + month + '-' + day + '-' + hour + '-' + minute + '-' + second + '.jpg',
imageUrl = '',
foscam = {},
dataReply = '',
request,
title = config.device.title;
fileName = config.device.deviceId + '-' + year + '-' + month + '-' + day + '-' + hour + '-' + minute + '-' + second + '.jpg',
callback = config.callback || function () {};

foscam.deviceId = config.device.deviceId;
foscam.title = config.device.title;
foscam.deviceIp = config.device.deviceIp;
foscam.timeout = config.device.localTimeout || config.config.localTimeout;
foscam.username = config.device.username;
Expand All @@ -267,40 +294,34 @@ module.exports = (function () {
foscam.command = config.command || '';
foscam.list = config.list || '';
foscam.devicePort = config.device.devicePort || 80;
foscam.callback = config.callback || function () {};
foscam.callback = callback;

if (foscam.list) {
this.getStoredVideos(foscam);
}

else if (foscam.command === 'TAKE') {
fs.stat(fileName, function(err, data) {
var http,
postData;

if (foscam.command === 'TAKE') {
fs.stat(localPath + fileName, function(err, data) {
if (err) {
http = require('http');
postData = that.postPrepare(foscam);
imageUrl = 'http://' + postData.host + ':' + postData.port + postData.path;
request = http.request(imageUrl).on('response', function (response) {
response.setEncoding('binary');

response.on('data', function (response) {
dataReply += response;
});

response.once('end', function () {
console.log('\x1b[35m' + title + '\x1b[0m: Saved image for ' + fileName);

fs.writeFile(fileName, dataReply, 'binary', function(err) {
if (err) {
console.log('\x1b[31m' + title + '\x1b[0m: Unable to save ' + fileName);
}
});
});
});

request.end();
foscam.callback = function(err, dataReply) {
var rawImage = dataReply.rawImage;

if (rawImage) {
fs.writeFile(localPath + fileName, rawImage, 'binary', function(err) {
if (err) {
console.log('\x1b[31m' + title + '\x1b[0m: Unable to save ' + fileName);
}

else {
console.log('\x1b[35m' + title + '\x1b[0m: Image saved as ' + fileName);
}
});
}

callback(err, dataReply);
};

that.getRawPhoto(foscam, fileName);
}

else if (data) {
Expand Down
2 changes: 1 addition & 1 deletion js/combo.min.js

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions lib/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,35 @@ module.exports = (function () {
'use strict';

return {
version : 20160314,
version : 20180218,

/**
* Accepts a message and object comprised of device controllers. If any of
* the controllers are conidered a notification means, the message is sent
* to them.
*/
notify : function (message, controllers, source) {
notify : function (message, controllers, source, fileName, payload) {
var runCommand = require(__dirname + '/../lib/runCommand'),
deviceId;

for (deviceId in controllers) {
if ((controllers[deviceId].config) && (deviceId !== source)) {
switch (controllers[deviceId].config.typeClass) {
case 'clientNotify' :
runCommand.runCommand(deviceId, 'text-' + message, source, null, null, null, null, fileName);
break;

case 'clientSpeech' :
case 'pushover' :
case 'sms' :
case 'speech' :
case 'gerty' :
runCommand.runCommand(deviceId, 'text-' + message, source);
break;

case 'pushover' :
runCommand.runCommand(deviceId, 'text-' + message, source, null, null, null, null, payload);
break;

case 'clientVibrate' :
runCommand.runCommand(deviceId, 'text-5', source);
break;
Expand Down
1 change: 1 addition & 0 deletions tests/unit/devices/foscam/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ exports.foscamControllerTest = {
controller : { inputs : ['state'],
send : function (request) { return request; } },
config : { deviceId : 'FOO',
title : 'Foscam',
deviceIp : '127.0.0.1',
username : 'USERNAME',
password : 'PASSWORD' } } };
Expand Down

0 comments on commit ebce1b2

Please sign in to comment.