UNPKG

flaglib

Version:

Ignition event 15 added.

189 lines (149 loc) 4.85 kB
var util = require('util'); var LatLon = require('./LatLon.js'); var Util = require('./util.js'); var logger = require('./logger'); var Zone = require('./zone'); module.exports = StudentAttendance; var PROFILE = "1"; var STD_NAME = "2"; var RFID = "3"; var PARENT_MOB = "4"; var ARN = "5"; var PLATFORM = "6"; var IN_ALERT_STATUS = "7"; var OUT_ALERT_STATUS = "8"; var LAST_SWIPE_TIME = "9"; var RESET_COUNT = "10"; //if student is IN, and OUT, it is one reset. var SMS = "11"; var ZONES = "12"; var ANDROID = "13"; var IOS = "14"; //ALERT IN = 0x2, ALERT OUT = 0x4, BOTH = 0x6, NONE = 0x0 var utility = new Util(); function StudentAttendance() { this.profile_name = ""; this.stud_name = ""; this.rfid = ""; this.parent_mob = ""; this.platform = "null"; this.arn = ""; this.in_alert_status = 0; this.out_alert_status = 0; this.resetCount = 0; this.sms = 0; //0 = NEVER send SMS this.invalid = true; this.zones = []; this.android = ""; this.ios = ""; this.last_swipe_time = 0; } /* *if user has no pickupID associated, and if valid lat, lon add the details to , */ StudentAttendance.prototype.updateStudentData = function(profile_name, android, ios,stud_name, rfid, parent_mob, platform, arn,sms,zones){ //try to get UserARN from UserRegistry, assign null otherwise logger.info("updateStudentData"); this.profile_name = profile_name; this.stud_name = stud_name; this.rfid = rfid; this.parent_mob = parent_mob; this.platform = platform; this.arn = arn; this.sms = sms; this.invalid = false; this.android = android; this.ios = ios; console.log("updateStudentData: zones length: "+zones.length); for (var i = 0;i<zones.length;i++){ var zone = new Zone(); zone.updateZoneData(zones[i].zone_id, zones[i].pair_zone_id, zones[i].zone_name, zones[i].alert_parent,zones[i].alert_admin); this.zones.push(zone); } } StudentAttendance.prototype.alertOnSwipe = function(zone_id,rfid,pushParams,smsParams,swipe_time,swipe_gap){ logger.info("alertOnSwipe: "+rfid); if ((rfid == null )|| (rfid == undefined)||(pushParams == null )|| (pushParams == undefined)||(smsParams == null )|| (smsParams == undefined)){ return; } //the time between two gates shoud be < 5 seconds. This helps tp handle the case, in same gate if two pillers has // two different readers and both push the data, then the time stamp is same. logger.info("alertOnSwipe gap: "+(swipe_time - this.last_swipe_time)); if ( (swipe_time - this.last_swipe_time) <= 5*1000){ this.last_swipe_time = swipe_time; return; } this.last_swipe_time = swipe_time; //first act on paired zones for (var i = 0;i<this.zones.length;i++){ var zone = this.zones[i]; zone.alertOnSwipe(zone_id, rfid, swipe_time,swipe_gap,this.profile_name, this.stud_name,this.parent_mob, this.arn,pushParams,smsParams,this.android,this.ios,this.sms); } } StudentAttendance.prototype.isInValid = function(){ return this.invalid; } /* * Parse JSON obejct and create objects */ StudentAttendance.prototype.fromJSON = function(data){ if (data == null){ this.invalid = true; return; } var flagJson = null; flagJson = JSON.parse(utility.hex2a(data)); this.profile_name = flagJson[PROFILE]; this.stud_name = flagJson[STD_NAME]; this.rfid = flagJson[RFID]; this.parent_mob = flagJson[PARENT_MOB]; this.platform = flagJson[PLATFORM]; this.arn = flagJson[ARN]; this.in_alert_status = flagJson[IN_ALERT_STATUS]; this.out_alert_status = flagJson[OUT_ALERT_STATUS]; this.resetCount = flagJson[RESET_COUNT]; this.sms = flagJson[SMS]; this.android = flagJson[ANDROID]; this.ios = flagJson[IOS]; this.last_swipe_time = flagJson[LAST_SWIPE_TIME]; var zones = JSON.parse(flagJson[ZONES]); if ((zones == undefined) || (zones == null)){ this.zones = null; return; } this.zones = []; for(var i=0;i<zones.length;i++){ var zone = new Zone(); zone.fromJSON(zones[i]); this.zones.push(zone); } this.invalid = false; } /* * Parse it to JSON to store */ StudentAttendance.prototype.toJSON = function(){ var mainObj = {}; mainObj[PROFILE] = this.profile_name; mainObj[STD_NAME] = this.stud_name; mainObj[RFID] = this.rfid; mainObj[PARENT_MOB] = this.parent_mob; mainObj[PLATFORM] = this.platform; mainObj[ARN] = this.arn; mainObj[IN_ALERT_STATUS] = this.in_alert_status; mainObj[OUT_ALERT_STATUS] = this.out_alert_status; mainObj[RESET_COUNT] = this.resetCount; mainObj[SMS] = this.sms; mainObj[ANDROID] = this.android; mainObj[IOS] = this.ios; mainObj[LAST_SWIPE_TIME] = this.last_swipe_time; var zones = []; if (this.zones != null){ for(var i=0;i<this.zones.length;i++){ zones.push(this.zones[i].toJSON()); } mainObj[ZONES] = JSON.stringify(zones); }else{ mainObj[ZONES] = ""; } return utility.convertFromAscii2Hexa(JSON.stringify(mainObj)); }