const utils = require('../utils');
const actions = require('../utils/actions');
const location = require('../utils/location');
const ifttt = require('../utils/ifttt');
const tplink = require('../utils/tplink');
let onActionOverride = false;
let off_action_override = false;
actions.register(function(invoke) {
if (!location.num_home().curr && location.num_home().prev && !onActionOverride) {
invoke();
}
}, function() {
off_action_override = false;
tplink.set_devices("camera", true).then(function(stateChanged) {
if (stateChanged) {
ifttt({
event: "notification",
data: "Cameras activated"
});
}
});
}, {
id: "Cameras on when no one is home",
type: "location"
});
actions.register(function(invoke) {
if (location.num_home().curr && !location.num_home().prev && !off_action_override) {
invoke();
}
}, function() {
onActionOverride = false;
tplink.set_devices("camera", false).then(function(stateChanged) {
if (stateChanged) {
ifttt({
event: "notification",
data: "Cameras deactivated"
});
}
});
}, {
id: "Cameras off when arriving home",
type: "location"
});
module.exports = {
post: {
"cameras": {
"off": function(req, res) {
onActionOverride = true;
tplink.set_devices("camera", false).then(function() {
res.send(utils.ok());
}).catch(function(error) {
console.error(error);
res.send(utils.error());
});
},
"on": function(req, res) {
off_action_override = true;
tplink.set_devices("camera", true).then(function() {
res.send(utils.ok());
}).catch(function(error) {
console.error(error);
res.send(utils.error());
});
}
}
}
};