Skip to content

Instantly share code, notes, and snippets.

@lbrenman
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbrenman/cfc11f25666227d34aad to your computer and use it in GitHub Desktop.
Save lbrenman/cfc11f25666227d34aad to your computer and use it in GitHub Desktop.
ArrowDB Block Example - Sample 'Before' Block to be executed before each API call related to the iotdevice model - clear iot device timer
var Arrow = require('arrow'),
server = new Arrow();
var Timers = require('./timers');
// lifecycle examples
server.on('starting', function(){
server.logger.debug('server is starting!');
});
server.on('started', function(){
server.logger.debug('server started!');
var model = Arrow.getModel("iotdevice");
model.findAll(function(err, data){
if(err) {
server.logger.debug('error getting iotdevice database, err = '+err);
} else {
Timers.registerDevices(data);
}
});
});
// start the server
server.start();
{
"type": "api",
"group": "arrow",
"dependencies": {
"connector/appc.arrowdb": "^1.0.51"
}
}
var Arrow = require("arrow");
var Model = Arrow.createModel("iotdevice",{
"fields": {
"deviceOutputVal": {
"type": "Number",
"required": false
},
"deviceId": {
"type": "String",
"required": false
},
"deviceIsOn": {
"type": "Boolean",
"required": false
}
},
"connector": "appc.arrowdb",
"before": "resetHeartBeatTimer",
"actions": [
"create",
"read",
"update",
"delete",
"deleteAll"
],
"singular": "",
"plural": ""
});
module.exports = Model;
var Arrow = require('arrow');
var Timers = require('../timers');
var PreBlock = Arrow.Block.extend({
name: 'resetHeartBeatTimer',
description: 'Reset the HeartBeat timer',
//On a POST or PUT, check if the deviceIsOn field is being written, if so, call method Timer.resetTimer
action: function(req, resp) {
if((req.method==="POST" || req.method==="PUT") && req.body.deviceIsOn) {
Timers.resetTimer(req.params.id);
}
}
});
module.exports = PreBlock;
var Arrow = require('arrow');
var timers = {};
exports.registerDevices = function(data) {
//create a timer for each iot device to clear the deviceIsOn parameter
//the iot device needs to set the deviceIsOn paramter so mobile apps can determine if the iot device is on
data.forEach(function(dataRow){
// exports.createTimer(dataRow.id); //https://jira.appcelerator.org/browse/API-623
exports.createTimer(dataRow.getPrimaryKey());
dataRow.deviceIsOn = false;
dataRow.update();
});
}
exports.createTimer = function(id) {
timers[id] = setInterval(function() {
var model = Arrow.getModel("iotdevice");
model.findOne(id, function(err, data){
if(err) {
console.log('error getting iotdevice database, err = '+err);
} else {
data.deviceIsOn=false;
data.update(); //pretty sure we need to do this for programmatic models
}
});
}, 60000);
}
exports.resetTimer = function(id) {
clearInterval(timers[id]);
exports.createTimer(id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment