UNPKG

flaglib

Version:

Ignition event 15 added.

334 lines (260 loc) 9.04 kB
var util = require('util'); var LatLon = require('./LatLon.js'); var Util = require('./util.js'); var logger = require('./logger'); var swipeType = require('./swipetype.js'); var smsconfig = require('./smsconfig.js'); var IndividualDeviceLogger = require('./IndividualDeviceLogger'); var usercommands = require('./usercommands.js'); var SMS = require('./sms.js'); module.exports = ActiveGroupStudent; var NAME = "0"; var RFID = "1"; var SWIPE_IN = "2"; var SWIPE_OUT = "3"; var SWIPE_MSG = "4"; var MIN_DISTANCE = 1.0; var unknownPoint = "Unknown location"; var utility = new Util(); function ActiveGroupStudent() { logger.info("ActiveGroupStudent 1"); this.name = ""; this.rfid = ""; this.swipe_in = 0; this.swipe_out = 0; this.message = true; this.invalid = false; } /* * add student details */ ActiveGroupStudent.prototype.updatestudent = function(name, rfid, message){ //try to get UserARN from UserRegistry, assign null otherwise this.name = name; this.rfid = rfid; this.message = message; this.invalid = false; } ActiveGroupStudent.prototype.sendMessage = function(rfid, sesionType, groupId, profileName1, smsParams,pushParams, profilePackage,groupName, ppname,id, arn, swipestatus,sms,android,ios){ /* * Log to swipe data * */ var dl = new IndividualDeviceLogger(); dl.writeSwipeLog("[SWIPESENT-B]," + rfid +","+ groupId + "," + id + ",[SWIPESENT-E]"); logger.info("[SWIPESENT-B]," + rfid +","+ groupId + "," + id + ",[SWIPESENT-E]"); if ((arn != null)) { var UserCommands = new usercommands(smsParams, pushParams); var pushMessage = ""; if (sesionType == 'P'){ if (swipestatus == swipeType.SWIPE_IN){ pushMessage = this.name+ " boarded the bus at "+ppname; }else{ pushMessage = this.name+ " unboarded the bus at "+ppname; } } if (sesionType == 'D'){ if (swipestatus == swipeType.SWIPE_IN){ pushMessage = this.name+ " boarded the bus at "+ppname; }else{ pushMessage = this.name+ " unboarded the bus at "+ppname; } } logger.info("swipe alert message: " + pushMessage); pushMessage = UserCommands.getSwipeAlert(profileName1, "", pushMessage, new Date().getTime()); logger.info("swipe alert message 1: " + pushMessage); if (this.message){ UserCommands.sendSwipeAlert(id, arn, 0, pushMessage, this.groupType); } /* * [Madhu] TEMP FIX START: SMS for ALL. */ if ( (this.message) && (sms == smsconfig.SMS_ALWAYS) ){ logger.info(" after push checkDistanceToPointAndAlert in PP2: " + pushParams.aws_access_key_id); var UserCommands = new usercommands(smsParams, pushParams); // get SMS command // [Madhu] replace -1 with profile id. //= function (profile, point, profileId, student_name) var message = UserCommands.getSwipeAlertCommand_Sms( profileName1, ppname, profilePackage, this.name,sesionType, swipestatus,android,ios); // is // in // seconds, // convert // to // minutes. // send SMS var smsGateway = new SMS(smsParams); var number = []; number.push(id); logger.info("Sending SMS with Push: " + message); smsGateway.sendSMS(number, message, 4, null); } /* * [Madhu] TEMP FIX END: SMS for ALL. */ } else if ( (this.message) && (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.getSwipeAlertCommand_Sms( profileName1, ppname, profilePackage, this.name,sesionType, swipestatus,android,ios); // minutes. // send SMS var smsGateway = new SMS(smsParams); var number = []; number.push(id); logger.info("Sending SMS with Push time_: " + 0); logger.info("Sending SMS: " + message); smsGateway.sendSMS(number, message, 4, null); } } /* * 1. ppname = pickup point name. * 2. If swipe happen again in 2 min, ignore it. * lp = location of campus (name, lat, lon) */ ActiveGroupStudent.prototype.checkRFID = function(profile_id, location_id, rfid, sesionType, groupId, profileName1, smsParams,pushParams, profilePackage,groupName, ppPoint,id, arn,updateSwipeData,lat,lon, lp,date,sms,android,ios){ var status = false; var distace_to_campus = new LatLon(lat,lon, "").checkDistance(lp); var distace_to_pp = ppPoint.checkDistance(new LatLon(lat,lon, "")); var atPPLocation = true; var atCampus = true; logger.info("ActiveGroupStudent.prototype.checkRFID: " + distace_to_campus); logger.info("ActiveGroupStudent.prototype.checkRFID: " + distace_to_pp+ " "+ppPoint.getName()); logger.info("ActiveGroupStudent.prototype swipe lat lon: " + lat + ", "+lon); logger.info("ActiveGroupStudent.prototype pp lat lon: " + ppPoint.getLat() + ", "+ppPoint.getLon()); logger.info("ActiveGroupStudent.prototype lp lat lon: " + lp.getLat() + ", "+lp.getLon()); logger.info("ActiveGroupStudent.prototype session type: " + sesionType); logger.info("ActiveGroupStudent.prototype swipe in time: " + this.swipe_in); logger.info("ActiveGroupStudent.prototype swipe out time: " + this.swipe_out); // >1 km if ( (sesionType == 'P') && (distace_to_campus > 1.0)){ atCampus = false } if ( (sesionType == 'P') && (distace_to_pp > 1.0)){ atPPLocation = false } if ( (sesionType == 'D') && (distace_to_pp > 1.0)){ atPPLocation = false } if ( (sesionType == 'D') && (distace_to_campus > 1.0)){ atCampus = false } if (rfid == this.rfid){ logger.info("ActiveGroupStudent.prototype inside rfid equal"); status = true; //swipe in if (this.swipe_in == 0){ this.swipe_in = date; if (updateSwipeData){ updateSwipeData(profile_id, location_id, groupId, groupName, rfid, this.swipe_in, sesionType, swipeType.SWIPE_IN,lat,lon, id, true); } var getInPoint = ""; var getOutPoint = ""; if (sesionType == 'P'){ getInPoint = ppPoint.getName(); getOutPoint = profileName1; if (!atPPLocation){ getInPoint = unknownPoint; } }else if (sesionType == 'D'){ getInPoint = profileName1; getOutPoint = ppPoint.getName(); if (!atCampus){ getInPoint = unknownPoint; } } //send alert to parents, student is boarded the bus this.sendMessage(rfid, sesionType, groupId, profileName1, smsParams,pushParams, profilePackage,groupName, getInPoint,id, arn, swipeType.SWIPE_IN,sms,android,ios); logger.info("ActiveGroupStudent.prototype session - swipe in: " ); }else if ( (this.swipe_in != 0) && (this.swipe_out == 0) ){ logger.info("ActiveGroupStudent.prototype session - swipe out: " ); this.swipe_out = date; var timeDiff = this.swipe_out - this.swipe_in; //convert to minutes timeDiff = timeDiff/(1000*60); //ignore it if the swipe coming with in 3 min again if (timeDiff <=3){ return status; } if (updateSwipeData){ updateSwipeData(profile_id, location_id, groupId, groupName, rfid, this.swipe_out, sesionType, swipeType.SWIPE_OUT,lat,lon,id, true); } var getInPoint = ""; var getOutPoint = ""; if (sesionType == 'P'){ getInPoint = ppPoint.getName(); getOutPoint = profileName1; if (!atCampus){ getOutPoint = unknownPoint; } }else if (sesionType == 'D'){ getInPoint = profileName1; getOutPoint = ppPoint.getName(); if (!atPPLocation){ getOutPoint = unknownPoint; } } //send alert to parents, student is un boarded the bus at "location name". this.sendMessage(rfid, sesionType, groupId, profileName1, smsParams,pushParams, profilePackage,groupName, getOutPoint,id, arn, swipeType.SWIPE_OUT,sms,android,ios); logger.info("ActiveGroupStudent.prototype session - swipe out: " ); } } return status; } ActiveGroupStudent.prototype.getIsBoarded = function(){ return this.swipe_in != 0?true:false; } ActiveGroupStudent.prototype.getIsUnBoarded = function(){ return this.swipe_out != 0?true:false; } ActiveGroupStudent.prototype.isTripCmmplete = function(){ var status = false; if ((this.swipe_in != 0) && (this.swipe_out != 0)){ status = true; } return status; } ActiveGroupStudent.prototype.injectData = function(){ } ActiveGroupStudent.prototype.isInValid = function(){ return this.invalid; } /* * Parse JSON obejct and create objects */ ActiveGroupStudent.prototype.fromJSON = function(data){ if (data == null){ this.invalid = true; return; } var flagJson = null; flagJson = JSON.parse(utility.hex2a(data)); this.name = flagJson[NAME]; this.rfid = flagJson[RFID]; this.swipe_in = flagJson[SWIPE_IN]; this.swipe_out = flagJson[SWIPE_OUT]; this.message = flagJson[SWIPE_MSG]; } /* * Parse it to JSON to store */ ActiveGroupStudent.prototype.toJSON = function(){ var mainObj = {}; mainObj[NAME] = this.name; mainObj[RFID] = this.rfid; mainObj[SWIPE_IN] = this.swipe_in; mainObj[SWIPE_OUT] = this.swipe_out; mainObj[SWIPE_MSG] = this.message; return utility.convertFromAscii2Hexa(JSON.stringify(mainObj)); }