UNPKG

flaglib

Version:

Ignition event 15 added.

128 lines (87 loc) 2.19 kB
var util = require('util'); var Util = require('./util.js'); var logger = require('./logger'); var Flag = require('./flag.js'); var GeofenceRule = require('./geofencerule.js'); var FlagConstants = require('./flagconstants.js'); module.exports = Rules; var RULES = "1"; var GEOFENCE_RULE = 1; var utility = new Util(); /* * Rules: * If the vehicle is continuously crosses the speed for 1 min, mean 6 updates * then consider this as cutoff crossed. * Ones update, don't update for next 30 min. */ function Rules(rules) { if (rules == null){ this.invalid = true; return; } this.rules = []; var length = rules.length; for (var i = 0; i < length; i++) { var rule = rules[i]; if (rule.rule_type == GEOFENCE_RULE) { console.log("RULE DATA: "+ rule); var gfr = new GeofenceRule(rule); if (gfr.isInValid() == false){ this.rules.push(gfr); } } } } Rules.prototype.checkRules = function(flag, action){ if (this.invalid == true){ return; } var length = this.rules.length; for (var i = 0; i < length; i++) { var rule = this.rules[i]; if (rule.isInValid() == false){ rule.checkRule(flag,action); } } } Rules.prototype.isInValid = function(){ return this.invalid; } Rules.prototype.injectData = function(rules){ this.rules = rules; } /* * Parse JSON obejct and create objects */ Rules.prototype.fromJSON = function(data){ if( (data == null) || (data == "") ){ this.invalid = true; return; } var flagJson = null; flagJson = JSON.parse(utility.hex2a(data)); var rulesArray = JSON.parse(flagJson[RULES]); this.rules = []; for (var i = 0; i < rulesArray.length; i++) { var rule = new GeofenceRule(null); rule.fromJSON(rulesArray[i]); this.rules.push(rule); } this.invalid = false; } /* * Parse it to JSON to store */ Rules.prototype.toJSON = function(){ if (this.invalid == true){ return ""; } var mainObj = {}; var rulesArry = []; if (this.rules) for (var i = 0; i < this.rules.length; i++) { rulesArry.push(this.rules[i].toJSON()); } mainObj[RULES] = JSON.stringify(rulesArry); return utility.convertFromAscii2Hexa(JSON.stringify(mainObj)); }