UNPKG

flaglib

Version:

Ignition event 15 added.

220 lines (167 loc) 5.85 kB
var util = require('util'); var LatLon = require('./LatLon.js'); var Util = require('./util.js'); var logger = require('./logger'); var smsconfig = require('./smsconfig.js'); var IndividualDeviceLogger = require('./IndividualDeviceLogger'); var usercommands = require('./usercommands.js'); var SMS = require('./sms.js'); module.exports = Zone; var ID = "0"; var NAME = "1"; var IN_ALERT_STATUS = "2"; var OUT_ALERT_STATUS = "3"; var RESET_COUNT = "4"; //if student is IN, and OUT, it is one reset. var ALERT_CONFIG_PARENT = "5"; var ALERT_CONFIG_ADMIN = "6"; var LAST_SWIPE_TIME = "7"; var PAIR_ID = "8"; var utility = new Util(); function Zone() { this.id = ""; this.pair_id = ""; this.name = ""; this.in_alert_status = 0; this.out_alert_status = 0; this.alertConfigParent = 0; this.alertConfigAdmin = 0; this.resetCount = 0; this.invalid = true; this.last_swipe_time = 0; } Zone.prototype.updateZoneData = function(id, pair_id,name, alert_config_parent,alert_config_admin){ logger.info("updateZoneData"); this.id = id; this.pair_id = pair_id; this.name = name; this.in_alert_status = 0; this.out_alert_status = 0; this.alertConfigParent = alert_config_parent; this.alertConfigAdmin = alert_config_admin; this.invalid = false; } Zone.prototype.getMessage = function(profile_name, stud_name){ var message = ""; // Already student stepped out, anf agan swiping, means stepped in. So reset both status. if (this.out_alert_status == 1){ this.in_alert_status = 0; this.out_alert_status = 0; } // now stepping in if (this.in_alert_status == 0){ message = stud_name + " enters "+profile_name+"."; this.in_alert_status = 1; }else if (this.out_alert_status == 0){ message = stud_name + " leaves "+profile_name+"."; this.out_alert_status = 1; } return message; } Zone.prototype.alertOnSwipe = function(id, rfid, swipe_time, swipe_gap, profile_name, stud_name,parent_mob, arn,pushParams,smsParams,android,ios,sms){ if ( (id != this.id) && (id != this.pair_id) ) { return; } logger.info("zone alertOnSwipe gap: "+(swipe_time - this.last_swipe_time)); //if second swipe is with in a minute, ignore the swipe if ( (swipe_time - this.last_swipe_time) <= swipe_gap){ this.last_swipe_time = swipe_time; return; } this.last_swipe_time = swipe_time; logger.info("alertOnSwipe: "+profile_name+","+this.name+","+stud_name); //1. get message //2. prepare for alert pushParams.platform = this.platform; var dl = new IndividualDeviceLogger(); dl.writeSwipeLog("[ALERTSWIPE-B]," + profile_name + "," + this.name + "," + stud_name +"[ALERTSWIPE-E]"); console.log("[ALERTSWIPE-B]," + profile_name + "," + this.name + "," + stud_name +"[ALERTSWIPE-E]"); var _message = this.getMessage(profile_name, stud_name); //Based on parent configuration, alert parent. //The system is only alerting parent, not admin. Need update to alert admin. var alertparentStatus = false; //out alert check if ( (this.out_alert_status == 1) && ((this.alertConfigParent == 4) || (this.alertConfigParent == 6))){ alertparentStatus = true; }else if ( (this.in_alert_status == 1) && ((this.alertConfigParent == 2) || (this.alertConfigParent == 6))){ ////in alert check alertparentStatus = true; } if (!alertparentStatus){ return; } if ((arn != null)) { var UserCommands = new usercommands(smsParams, pushParams); pushMessage = UserCommands.getSwipeAlert(profile_name, "", _message, new Date().getTime()); UserCommands.sendSwipeAlert(parent_mob, arn, new Date().getTime(), pushMessage,0); /* * [Madhu] TEMP FIX START: SMS for ALL. */ if (sms == smsconfig.SMS_ALWAYS){ var UserCommands = new usercommands(smsParams, pushParams); // get SMS command // [Madhu] replace -1 with profile id. var message = UserCommands.getZoneSwipeAlertCommand_Sms(_message,android, ios); console.log("SMS:"+message); var smsGateway = new SMS(smsParams); var number = []; number.push(parent_mob); smsGateway.sendSMS(number, message, 4, null); } /* * [Madhu] TEMP FIX END: SMS for ALL. */ } else if ((sms == smsconfig.SMS_NOAPP) || (sms == smsconfig.SMS_ALWAYS)){ var UserCommands = new usercommands(smsParams, pushParams); // get SMS command // [Madhu] replace -1 with profile id. var message = UserCommands.getZoneSwipeAlertCommand_Sms(_message,android, ios); var smsGateway = new SMS(smsParams); var number = []; number.push(parent_mob); smsGateway.sendSMS(number, message, 4, null); } } Zone.prototype.isInValid = function(){ return this.invalid; } /* * Parse JSON obejct and create objects */ Zone.prototype.fromJSON = function(data){ if (data == null){ this.invalid = true; return; } var flagJson = null; flagJson = JSON.parse(utility.hex2a(data)); var mainObj = {}; this.id = flagJson[ID]; this.pair_id = flagJson[PAIR_ID]; this.name = flagJson[NAME]; this.alertConfigParent = flagJson[ALERT_CONFIG_PARENT]; this.alertConfigAdmin = flagJson[ALERT_CONFIG_ADMIN]; this.in_alert_status = flagJson[IN_ALERT_STATUS]; this.out_alert_status = flagJson[OUT_ALERT_STATUS]; this.resetCount = flagJson[RESET_COUNT]; this.last_swipe_time = flagJson[LAST_SWIPE_TIME]; this.invalid = false; } /* * Parse it to JSON to store */ Zone.prototype.toJSON = function(){ var mainObj = {}; mainObj[ID] = this.id; mainObj[PAIR_ID] = this.pair_id; mainObj[NAME] = this.name; mainObj[ALERT_CONFIG_PARENT] = this.alertConfigParent; mainObj[ALERT_CONFIG_ADMIN] = this.alertConfigAdmin; mainObj[IN_ALERT_STATUS] = this.in_alert_status; mainObj[OUT_ALERT_STATUS] = this.out_alert_status; mainObj[RESET_COUNT] = this.resetCount; mainObj[LAST_SWIPE_TIME] = this.last_swipe_time; return utility.convertFromAscii2Hexa(JSON.stringify(mainObj)); }