UNPKG

flaglib

Version:

Ignition event 15 added.

1,178 lines (960 loc) 33.5 kB
var util = require('util'); var ActiveGroupUser = require('./groupuser.js'); var Util = require('./util.js'); var LatLon = require('./LatLon.js'); var usercommands = require('./usercommands.js'); var SMS = require('./sms.js'); var logger = require('./logger'); var smsconfig = require('./smsconfig.js'); var IndividualDeviceLogger = require('./IndividualDeviceLogger'); module.exports = PickUpPoint; var ID = "0"; var POINT = "1"; var USERS = "2"; var COUNT = "3"; var PICKED = "4"; var PRVDIST = "5"; var REACHTIME = "6"; var LEAVETIME = "7"; var PVDIST = "8"; var NEAR_TIME = "9"; //This is used to identify order of points in a route. var ORDER = "10"; //This is used to identify order of points in a route. var self = this; var utility = new Util(); var MIN_DISTANCE = 0.100; var ALERT_DISTANCE = 0.750; var MIN_TIME = 30; function PickUpPoint(pid, order, lat, lon, name) { // logger.info("new instance of PickUpPoint\n\n"); this.point = new LatLon(lat, lon, name); this.picked = false; this.invalid = false; this.alertCount = 0; this.prevDist = 999999; this.timeStamp = 0; this.leaveTime = 0; this.prvVehDist = 0; this.nearTime = ""; if (order == null){ this.order = -1; }else{ this.order = order; } if (pid != -1) { this.id = pid; updatePickUpPoint(); } this.users = []; } function updatePickUpPoint() { // load data from pickup points table for lat, long, name } PickUpPoint.prototype.addUser = function(user) { // check if active group user already exist. var length = this.users.length; for (var i = 0; i < length; i++) { var user1 = this.users[i]; if (user1.getUserID() == user.getUserID()) { return; } } this.users.push(user); } PickUpPoint.prototype.logReachPoint = function(id,vehicledistance, currenLoc){ if ((this.timeStamp == 0) || (this.leaveTime == 0)){ return; } var dist = (vehicledistance-this.prvVehDist); if ((dist == undefined) || (dist == NaN)){ dist = 0; } var dl = new IndividualDeviceLogger(); dl.writePointReachLog("[REACHPOINT-B],"+id + "," + this.point.getName() +","+ this.timeStamp+","+ this.leaveTime+","+ dist+","+currenLoc.getLat()+","+currenLoc.getLon()+",[REACHPOINT-E]",id); } PickUpPoint.prototype.hasUser = function(id) { // check if active group user already exist. var ref = null; var length = this.users.length; for (var i = 0; i < length; i++) { var user1 = this.users[i]; if (user1.getUserID() == id) { ref = user1; break; } } return ref; } PickUpPoint.prototype.getUsers = function() { return this.users; } PickUpPoint.prototype.getId = function() { return this.id; } PickUpPoint.prototype.getPicked = function() { return this.picked; } PickUpPoint.prototype.getLat = function() { // logger.info("PickUpPoint.prototype.getLat\n\n"); return this.point.getLat(); } PickUpPoint.prototype.getPName = function() { // logger.info("PickUpPoint.prototype.getLat\n\n"); return this.point.getName(); } PickUpPoint.prototype.getLon = function() { // logger.info("PickUpPoint.prototype.getLon\n\n"); return this.point.getLon(); } PickUpPoint.prototype.getUserIds = function() { // all user ids seperated with ','. var length = this.users.length; var ids = ""; for (var i; i < length; i++) { if (!ids.isEmpty()) { ids = ids + ","; } ids = ids + this.users.getUserID(); } return ids; } PickUpPoint.prototype.getUserArns = function() { // all user ARNs seperated with ','. var length = this.users.length; var arns = ""; for (var i; i < length; i++) { if (!arns.isEmpty()) { arns = arns + ","; } arns = arns + this.users.getUserArn(); } return arns; } PickUpPoint.prototype.getFinalStatus = function(device_id, profile_id, group_id){ var length = this.users.length; var log = ""; for (var i = 0; i < length; i++) { var user1 = this.users[i]; log = log+user1.getFinalStatus(device_id, profile_id, group_id, this.id, this.point.getName(), this.prevDist); log = log+"\n"; } return log; } /* * This function compare distance between pickup point and current location. */ PickUpPoint.prototype.checkDistance = function(currenLoc) { return this.point.checkDistance(currenLoc); } function convertTimeToNumber(time) { var hms = time || '00:2:00'; // your input string var a = hms.split(':') || '00:2:00'; // split it at the colons // minutes are worth 60 seconds. Hours are worth 60 minutes. var seconds = ((+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2])) || 120; // defaul // to // 00:2:00 return seconds; } function calculateDistance(plat1, plon1, plat2, plon2) { var distance = -1; if ((plat1 == null) || (plon1 == null) || (plat2 == null) || (plon2 == null)) { logger.info("LatLon: checkDistance global start 6: "); return distance; } var lat1 = plat1; var lat2 = plat2; var lon1 = plon1; var lon2 = plon2; var p = 0.017453292519943295; // Math.PI / 180 var c = Math.cos; var a = 0.5 - c((lat2 - lat1) * p) / 2 + c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p)) / 2; distance = 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km return distance; } /* * This function compare distance between pickup point and current location, and * calculates the time to reach the pickup point. * lp = location point(name, lat,lon) */ PickUpPoint.prototype.checkSwipeAndAlert = function(profile_id, location_id, rfid, sesionType, groupId, profileName1, smsParams,pushParams, profilePackage,groupName,updateSwipeData,lat,lon,lp,date,sms,android,ios) { logger.info("checkSwipeAndAlert satrt"); var status = false; for (var i = 0; i < this.users.length; i++) { var user = this.users[i]; var status1 = user.checkSwipeAndAlert(profile_id, location_id,rfid, sesionType, groupId, profileName1, smsParams,pushParams, profilePackage,groupName, this.point,updateSwipeData,lat,lon,lp,date,sms,android,ios); if (status1){ status = true //break; } } return status; } /* * Checks if previous point or previous, prwvious point is alerted or not. * */ PickUpPoint.prototype.areYouAlerted = function(nextPointOrder){ var status = false; if (this.alerted){ var diff = nextPointOrder - this.order; if (diff <0){ diff = diff * -1; } if (diff <= 2){ status = true; } } return status; } PickUpPoint.prototype.isNear = function(latlng){ var currDist = this.point.checkDistance(latlng); var status = false; if (currDist <= MIN_DISTANCE){ status = true; } return status; } PickUpPoint.prototype.isNearAlertDistance = function(sesionType, groupId, deviceId, currenLoc, timeStamp, speed, profileName1, smsParams, pushParams, profilePackage, compass,groupName,vehicledistance,sms,android,ios){ var currDist = this.point.checkDistance(currenLoc); var status = false; if (currDist <= ALERT_DISTANCE){ status = true; } if ((status) && (this.picked == false)) { return this.order; } return -1; } /* * This function called when vehicle entered the minimum distance to aelrt and not alerted the parent yet * */ PickUpPoint.prototype.alertParentAnyway = function(sesionType, groupId, deviceId, currenLoc, timeStamp, speed, profileName1, smsParams, pushParams, profilePackage, compass,groupName,vehicledistance,sms,android,ios) { if (this.picked){ return; } var time = -1; time = this.point.checkTimeToPoint(currenLoc, speed); for (var i = 0; i < this.users.length; i++) { var user = this.users[i]; var alertTime = user.getAlertBefore(); alertTime = convertTimeToNumber(alertTime); pushParams.platform = user.getUserPlatform(); /* * Log to analyse by "Alert Analytics" "[ALERT SENT]:group id, * pickup point id, user mobile, alertBefore, time sent */ var dl = new IndividualDeviceLogger(); dl.writeAlertLog("[ALERTSENT-B]," + groupId + "," + deviceId + "," + this.id + "," + user.getUserID() + "," + alertTime + "," + timeStamp + "," + sesionType + "," + alertPoint["0"] + "," + alertPoint["1"] + "," + "[ALERTSENT-E]"); logger.info("[ALERTSENT-B]," + groupId + "," + deviceId + "," + this.id + "," + user.getUserID() + "," + alertTime + "," + timeStamp + "," + sesionType + "," + "[ALERTSENT-E]"); if ((user.getUserArn() != null)) { var UserCommands = new usercommands(smsParams, pushParams); logger.info("in PP1: " + pushParams.aws_access_key_id); /* * var pushMessage = * UserCommands.getNearByAlert(profileName1+":", time/60);//time * is in seconds, convert to minutes. * UserCommands.sendNearByAlert(user.getUserID(), * user.getUserArn(),time/60,pushMessage, this.groupType); */ // changed NearbyAlert to Simple alert, for reducing noise // var pushMessage = // getNearbyAlertCommand_Push(profileName1+":", // this.point.getName(), time/60,profilePackage);//time is in // seconds, convert to minutes.) var pushMessage = groupName+ ' vehicle ' + time_ + ' minute(s) to ' + pointName + '.'; logger.info("nearby alert message: " + pushMessage); pushMessage = UserCommands.getAlert(profileName1, "", pushMessage, new Date().getTime()); logger.info("nearby alert message 1: " + pushMessage); //UserCommands.sendNearByAlert(user.getUserID(), user // .getUserArn(), parseInt(time_, 10), pushMessage, // this.groupType); /* * [Madhu] TEMP FIX START: SMS for ALL. */ if (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. var message = UserCommands.getNearbyAlertCommand_Sms( profileName1 + ":", pointName, time_, profilePackage, groupName, android, ios);// time // is // in // seconds, // convert // to // minutes. // send SMS var smsGateway = new SMS(smsParams); var number = []; number.push(user.getUserID()); logger.info("Sending SMS with Push time_: " + time_); logger.info("Sending SMS with Push: " + message); //smsGateway.sendSMS(number, message, 4, null); } /* * [Madhu] TEMP FIX END: SMS for ALL. */ } else if ((sms == smsconfig.SMS_NOAPP) || (sms == smsconfig.SMS_ALWAYS)){ logger.info("in PP2: " + pushParams.aws_access_key_id); var UserCommands = new usercommands(smsParams, pushParams); // get SMS command // [Madhu] replace -1 with profile id. var message = UserCommands.getNearbyAlertCommand_Sms( profileName1 + ":", pointName, time_, profilePackage, groupName,android, ios);// time // is // in // seconds, // convert // to // minutes. // send SMS var smsGateway = new SMS(smsParams); var number = []; number.push(user.getUserID()); logger.info("Sending SMS with Push time_: " + time_); logger.info("Sending SMS: " + message); //smsGateway.sendSMS(number, message, 4, null); } // for testing purpose this.alertCount++; user.setAlerted(true); logger.info("Settings user alerted to true: "); alertedCount++; } // if all users alerted, set pickup point as picked if (alertedCount == this.users.length) { logger.info("this pickup point is picked: " + this.id); this.picked = true; // [PENDING]store the pickup point in lastpicked point table, to // restore the pickup point in case of server reboot } } PickUpPoint.prototype.vehicleLeftAlert = function(mob, arn, sesionType, profileName1, smsParams, pushParams, profilePackage ,sms,android,ios,lat1, lon1, lat2, lon2) { var dl = new IndividualDeviceLogger(); dl.writeVehicleLefttLog("[VEHICLE-LEFT-START]"+mob+","+ lat1+","+ lon1+","+ lat2 +","+ lon2+"[VEHICLE-LEFT-END]"); } /* * This function compare distance between pickup point and current location, and * calculates the time to reach the pickup point. */ PickUpPoint.prototype.checkTimeToPointAndAlert = function(sesionType, groupId, deviceId, currenLoc, timeStamp, speed, profileName1, smsParams, pushParams, profilePackage, compass,groupName,vehicledistance,sms,android,ios) { logger.info("time in checkTimeToPointAndAlert start : "); /* * [Madhu] 24 Feb 2018 * This logic is to alert parent that, vehicle left the point. * This consider the point, of vehicle crossing same PP twice and also pick and drop scenarios. * USing variable to store "point reached and leaving". * Based on session type and alert on return value, send alert to user. * TEMP: For now as a temporary solution, store the values in log file, and check the accuracy. If working fine, send alert. */ var vehicleLeftoint = false; /* * Reduce speed 30%, to consider traffic and pickup delays. */ if (speed > 20) { speed = speed - parseInt(speed / 3, 10); } /* * check if the user pickup point alert is in range of the user to send the * alert. */ var time = -1; time = this.point.checkTimeToPoint(currenLoc, speed); var currDist = this.point.checkDistance(currenLoc); /* * If the vehicle is near to PP, store the time stamp. * and give 100 meters. If the vehicle is return to same point, then there might be a chance * that on return the distance to point is not lessthan previous. Due to time of update. * To solve, use 100m range. If vehicle near 100m range, store as near. */ if ((this.prevDist > currDist) || (currDist <=0.100) ){ this.prevDist = currDist; this.nearTime = timeStamp; } var pointName = this.point.getName(); /* * 1. First if the vehicle reached <150m to point, mark as reach point. * 2. Second, if the vehicle crossed >150m to pint, mark as leave point * 3. If both points marked, log it. */ if ((currDist < MIN_DISTANCE) && (this.timeStamp==0)){ this.timeStamp = timeStamp; this.prvVehDist = vehicledistance; } if ((currDist > MIN_DISTANCE)&&(this.timeStamp!=0)){ if ((timeStamp - this.timeStamp)>MIN_TIME){ this.leaveTime = timeStamp; } } if ( (this.timeStamp != 0) && (this.leaveTime!=0) ){ this.logReachPoint(deviceId,vehicledistance,currenLoc); this.leaveTime = 0; this.timeStamp = 0; vehicleLeftoint = true; } if(this.picked){ return; } /* * [Madhu] Temporary fix for "-1 minute to point null" issue. Avoid -1, if * -1 make it 1, avoid null, if it is null, say point. */ if (time == -1) { time = 1; } if (pointName == null) { pointName = "pickup point"; } /* * FIX END */ // convert time to seconds. time = time * 60; // if any issue with getting time to point, simply return. // or if all users picked return; if ((time == -1) || (this.picked)) { return; } // [PENDING]: store the details in the log, for future analysis. var alertedCount = 0; for (var i = 0; i < this.users.length; i++) { var user = this.users[i]; if ( (vehicleLeftoint) && (user.getAlertOnReturn() == 1)){ this.vehicleLeftAlert(user.getUserID(), user.getUserArn(), sesionType, profileName1, smsParams, pushParams, profilePackage ,sms,android,ios,currenLoc.getLat(),currenLoc.getLon(),this.point.getLat(),this.point.getLon()); } // logger.info('user ::', user); // logger.info('--------', user.users); var alertPoint = user.getAlertPoint(); var prevADist = user.getPreviousAlertDistance(); var alertCompass = user.getAlertCourse(); var alertAdjust = user.getAlertAdjust(); var alertTime = user.getAlertBefore(); alertTime = convertTimeToNumber(alertTime); var distanceToAlertPoint = calculateDistance(currenLoc.getLat(), currenLoc.getLon(), alertPoint["0"], alertPoint["1"]); if (distanceToAlertPoint == undefined) { distanceToAlertPoint = -1; } var alertInAlertPoint = false; var userAlertBeforeTime = 0; /* * We have two points, 1. alert point, 2. pickup point. Alert at exact * point, where, 1. pickup point is approaching and alert point is * leaving. (currDist < this.prevDist) is to check the vehicle is * approaching the pickup point. (distanceToAlertPoint > prevADist ) is * to check vehicle is just leaving the alert point. alertOnReturn = 1 * mean, if vehicle approaching pickup point and alert point, whih is * near, alert. */ /* * var alertOnReturn = user.getAlertOnReturn(); if( * (distanceToAlertPoint!=-1)&&( distanceToAlertPoint <= 0.200 * )&&(distanceToAlertPoint > prevADist ) && (currDist < * this.prevDist)){ alertInAlertPoint = true; }else if * ((distanceToAlertPoint!=-1)&&( distanceToAlertPoint <= 0.200 * )&&(distanceToAlertPoint > prevADist ) && (alertOnReturn == 1) ){ * //This condition, to check, all conditions are true and also the * alert on return is 1, mean alert when touching alert point first time // * which might make vehicle leaving the pickup point to get U turn, so * alert. alertInAlertPoint = true; } */ /* * *changed logic from above to depend alert on compass check if the * course value is same as alert course value */ var diff = alertCompass - compass; if (diff < 0) { diff = diff * (-1); } /* * 1. Send alert, in case vehicle missed the alert point, and reached * pickup point. If alert on return is 2, don;t consider the usecase. 2. * 45 degree deviation to handle alert points near turns. */ //[Madhu] 24th July 2017 //if ((distanceToAlertPoint != -1) && (distanceToAlertPoint <= 0.100) && (diff < 45)) { if ((distanceToAlertPoint != -1) && (distanceToAlertPoint <= 0.200) /*&& (diff < 45) */) { alertInAlertPoint = true; if (isNaN(alertAdjust)){ alertTime = alertTime; }else{ alertTime = alertTime + parseInt(alertAdjust, 10); } } /* * prevADist = distanceToAlertPoint; * this.users[i].setPreviousAlertDistance(prevADist); * logger.info("prevADist calculated: "+prevADist); */ var alerted = user.getAlerted(); // if user already alerted, continue with next user. if (alerted) { alertedCount++; continue; } userAlertBeforeTime = alertTime; /* * 1. If no alert point set and vehicle approaching the alert point, * first time alert. 2. If vehicle coming in different route and skipped * alert point, alert when vehicle approaching the point. * [Madhu] 19-6-17, If no alert point, setm make it default 1 km. */ var distance = this.point.checkDistance(currenLoc); if ((distanceToAlertPoint == -1) && (distance < 1.0)) { alertInAlertPoint = true; alertTime = 120; } /* * If vehicle less than 50m to pickup point, reduce the alert on return * to 1. So that next time, when vehicle return to point, it alert, if * by chance it skipped pickup point. */ // calculate distance between, pickup point and alert point. // if both are near, then don;t consider the default .4 meter alert, // wait for vehicle to reach alert point. /* * var alert_to_pickup = * calculateDistance(this.point.getLat(),this.point.getLon(),alertPoint["0"],alertPoint["1"]); * if ( (distanceToAlertPoint != -1) && (alertOnReturn == 1) && * (alert_to_pickup>0.400) && (distance<0.4)){ alertInAlertPoint = * true; alertTime = 100; } */ /* * just tell vehicle reached pickup point. 1. what if admin created * pickup point, and vehicle near reach pickup point is >100 meters. 2. * here prevADist is used to store the vehicle in 100 circle of pickup * point. */ var distCircle = distance * 1000; // convert to meters. //[Madhu] 24th July 2017 //distCircle = distCircle / 100; distCircle = distCircle / 200; distCircle = parseInt(distCircle, 10); if ((prevADist != 0) && (distCircle == 0)) { // means with in 100m // circle. user.setAlertOnReturn(user.getAlertOnReturn() - 1); } prevADist = distCircle; user.setPreviousAlertDistance(prevADist); var alert_Approach = false; /* * 1. if vehicle missed alert point and reaching pickup point, alert. * This is helpful in case of single time touching pickup points. */ if ((distanceToAlertPoint != -1) && (user.getAlertOnReturn() == 0)) { alertInAlertPoint = true; alert_Approach = true; //[Madhu] 24th July 2017 //alertTime = 60; alertTime = 120; } /* * 2. What is vehicle changed the route and entering the point in reverse direction. * It can skip the alert point and approaching the pickup point. * */ if ((distanceToAlertPoint != -1) && (user.getAlertOnReturn() <=0) && (distCircle == 0)) { alertInAlertPoint = true; alert_Approach = true; //[Madhu] 24th July 2017 //alertTime = 60; alertTime = 120; } // if user already not alerted and reached nearby of user set time, then // send alert var time_ = 0; if (alertInAlertPoint) { time_ = alertTime; } else { time_ = alertTime; } time_ = parseInt(time_ / 60, 10); if (isNaN(time_)) { time_ = 0; } /* * TODO, find a mechanism to alert when approching a point, if user is * not alerted at alert point. */ // if( (alertInAlertPoint) || (time <= alertTime) ){ if (alertInAlertPoint) { time = parseInt(alertTime, 10); /*if (user.getUserPlatform()=="GCM"){ pushParams.ANDROID_ARN = user.getUserArn(); }else if (user.getUserPlatform()=="APNS"){ pushParams.IOS_ARN = user.getUserArn(); }*/ pushParams.platform = user.getUserPlatform(); /* * Log to analyse by "Alert Analytics" "[ALERT SENT]:group id, * pickup point id, user mobile, alertBefore, time sent */ var dl = new IndividualDeviceLogger(); dl.writeAlertLog("[ALERTSENT-B]," + groupId + "," + deviceId + "," + this.id + "," + user.getUserID() + "," + alertTime + "," + timeStamp + "," + sesionType + "," + alertPoint["0"] + "," + alertPoint["1"] + "," + "[ALERTSENT-E]"); logger.info("[ALERTSENT-B]," + groupId + "," + deviceId + "," + this.id + "," + user.getUserID() + "," + alertTime + "," + timeStamp + "," + sesionType + "," + "[ALERTSENT-E]"); if ((user.getUserArn() != null)) { var UserCommands = new usercommands(smsParams, pushParams); logger.info("in PP1: " + pushParams.aws_access_key_id); /* * var pushMessage = * UserCommands.getNearByAlert(profileName1+":", time/60);//time * is in seconds, convert to minutes. * UserCommands.sendNearByAlert(user.getUserID(), * user.getUserArn(),time/60,pushMessage, this.groupType); */ // changed NearbyAlert to Simple alert, for reducing noise // var pushMessage = // getNearbyAlertCommand_Push(profileName1+":", // this.point.getName(), time/60,profilePackage);//time is in // seconds, convert to minutes.) var pushMessage = groupName+ ' vehicle ' + time_ + ' minute(s) to ' + pointName + '.'; pushMessage = UserCommands.getAlert(profileName1, "", pushMessage, new Date().getTime()); //UserCommands.sendNearByAlert(user.getUserID(), user // .getUserArn(), parseInt(time_, 10), pushMessage, // this.groupType); /* * [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.getNearbyAlertCommand_Sms( profileName1 + ":", pointName, time_, profilePackage, groupName, android, ios);// time // is // in // seconds, // convert // to // minutes. // send SMS var smsGateway = new SMS(smsParams); var number = []; number.push(user.getUserID()); //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.getNearbyAlertCommand_Sms( profileName1 + ":", pointName, time_, profilePackage, groupName,android, ios);// time // is // in // seconds, // convert // to // minutes. // send SMS var smsGateway = new SMS(smsParams); var number = []; number.push(user.getUserID()); //smsGateway.sendSMS(number, message, 4, null); } // for testing purpose this.alertCount++; user.setAlerted(true); alertedCount++; } // if all users alerted, set pickup point as picked if (alertedCount == this.users.length) { this.picked = true; // [PENDING]store the pickup point in lastpicked point table, to // restore the pickup point in case of server reboot } } } /* * Date: July 26, 2016. This is temporary function added to consider order of * pickup point for alerting. Since the current implementation is not allowed to * insert pickup points to already added route, it might miss last points, * though they are in middle of route. This fix is until automatic order of * pickup points from shortest path algorithm. */ PickUpPoint.prototype.checkDistanceToPointAndAlert = function(groupId, deviceId, currenLoc, timeStamp, speed, profileName1, smsParams, pushParams, profilePackage) { logger.info("checkDistanceToPointAndAlert push: " + pushParams.aws_access_key_id); /* * check if the user pickup point alert is in range of the user to send the * alert. */ var distance = -1; var minDistanceToAlert = 0.4; // 400 meters distance = this.point.checkDistance(currenLoc); // if any issue with getting time to point, simply return. // or if all users picked return; if ((distance == -1) || (this.picked)) { return; } // convert time to meters. distance = distance * 1000; var pointName = this.point.getName(); /* * [Madhu] Temporary fix for "-1 minute to point null" issue. Avoid -1, if * -1 make it 1, avoid null, if it is null, say point. */ if (pointName == null) { pointName = "pickup point"; } /* * FIX END */ // [PENDING]: store the details in the log, for future analysis. var alertedCount = 0; for (var i = 0; i < this.users.length; i++) { var user = this.users[i]; // logger.info('user ::', user); // logger.info('--------', user.users); var alertTime = user.getAlertBefore(); logger.info("checkDistanceToPointAndAlert 2-1: " + alertTime); var alerted = user.getAlerted(); // if user already alerted, continue with next user. if (alerted) { alertedCount++; continue; } // alertTime = convertTimeToNumber(alertTime); // if user already not alerted and reached nearby of user set time, then // send alert logger .info( ' checkDistanceToPointAndAlertbefore sending check distance : distance:', distance, ' minDistanceToAlert ::', minDistanceToAlert); logger.info(' checkDistanceToPointAndAlertcondition is ::', distance <= minDistanceToAlert); if (distance <= minDistanceToAlert) { logger .info(' checkDistanceToPointAndAlertsending alert in pickuppoint::'); pushParams.ANDROID_ARN = user.getUserArn(); /* * Log to analyse by "Alert Analytics" "[ALERTSENT-B],group id, * pickup point id, user mobile, time sent,[ALERTSENT-E] */ logger.info("[ALERTSENT-B]," + groupId + "," + deviceId + "," + this.id + "," + user.getUserID() + "," + timeStamp + "," + "[ALERTSENT-E]"); if ((user.getUserArn() != null)) { var UserCommands = new usercommands(smsParams, pushParams); logger.info(" checkDistanceToPointAndAlert in PP1: " + pushParams.aws_access_key_id); /* * var pushMessage = * UserCommands.getNearByAlert(profileName1+":", time/60);//time * is in seconds, convert to minutes. * UserCommands.sendNearByAlert(user.getUserID(), * user.getUserArn(),time/60,pushMessage, this.groupType); */ // changed NearbyAlert to Simple alert, for reducing noise // var pushMessage = // getNearbyAlertCommand_Push(profileName1+":", // this.point.getName(), time/60,profilePackage);//time is in // seconds, convert to minutes.) var pushMessage = 'Vehicle 2' + ' minute(s) to ' + pointName + '.'; logger .info(" checkDistanceToPointAndAlertnearby alert message: " + pushMessage); pushMessage = UserCommands.getAlert(profileName1, "", pushMessage, new Date().getTime()); logger .info(" checkDistanceToPointAndAlertnearby alert message 1: " + pushMessage); UserCommands.sendNearByAlert(user.getUserID(), user .getUserArn(), 2, pushMessage, this.groupType); /* * [Madhu] TEMP FIX START: SMS for ALL. */ var sms_all = true; if (sms_all) { 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. var message = UserCommands.getNearbyAlertCommand_Sms( profileName1 + ":", pointName, 2, profilePackage);// time // is // in // seconds, // convert // to // minutes. logger .info(" checkDistanceToPointAndAlert in pp2 sms message: " + message); // send SMS var smsGateway = new SMS(smsParams); logger .info("checkDistanceToPointAndAlert in pp2 sms message 1: " + number); var number = []; number.push(user.getUserID()); logger .info("checkDistanceToPointAndAlert in pp2 sms message 2: " + number); smsGateway.sendSMS(number, message, 4, null); } /* * [Madhu] TEMP FIX END: SMS for ALL. */ } else { logger.info(" checkDistanceToPointAndAlert in PP2: " + pushParams.aws_access_key_id); var UserCommands = new usercommands(smsParams, pushParams); // get SMS command // [Madhu] replace -1 with profile id. var message = UserCommands.getNearbyAlertCommand_Sms( profileName1 + ":", pointName, 2, profilePackage);// time // is // in // seconds, // convert // to // minutes. logger .info(" checkDistanceToPointAndAlert in pp2 sms message: " + message); // send SMS var smsGateway = new SMS(smsParams); logger .info("checkDistanceToPointAndAlert in pp2 sms message 1: " + number); var number = []; number.push(user.getUserID()); logger .info("checkDistanceToPointAndAlert in pp2 sms message 2: " + number); smsGateway.sendSMS(number, message, 4, null); } // for testing purpose this.alertCount++; user.setAlerted(true); alertedCount++; } // if all users alerted, set pickup point as picked if (alertedCount == this.users.length) { logger .info("checkDistanceToPointAndAlert this pickup point is picked: " + alertedCount); this.picked = true; // [PENDING]store the pickup point in lastpicked point table, to // restore the pickup point in case of server reboot } } } /* * Date: July 26, 2016. This is temporary function added to consider order of * pickup point for alerting. Since the current implementation is not allowed to * insert pickup points to already added route, it might miss last points, * though they are in middle of route. This fix is until automatic order of * pickup points from shortest path algorithm. END */ PickUpPoint.prototype.isInValid = function() { return this.invalid; } PickUpPoint.prototype.getNearPoint = function(){ var mainObj = {}; mainObj["pid"] = this.id; mainObj["time"] = this.nearTime; return mainObj; } /* * Parse JSON obejct and create objects */ PickUpPoint.prototype.fromJSON = function(data) { if (data == null) { this.invalid = true; return; } this.users = []; var flagJson = null; flagJson = JSON.parse(utility.hex2a(data)); this.id = flagJson[ID]; this.point.fromJSON(flagJson[POINT]); this.alertCount = flagJson[COUNT]; this.picked = flagJson[PICKED]; this.prevDist = flagJson[PRVDIST]; this.timeStamp = flagJson[REACHTIME]; this.leaveTime = flagJson[LEAVETIME]; this.prvVehDist = flagJson[PVDIST]; this.nearTime = flagJson[NEAR_TIME]; this.order = flagJson[ORDER]; var userArray = JSON.parse(flagJson[USERS]); for (var i = 0; i < userArray.length; i++) { var user1 = new ActiveGroupUser(""); user1.fromJSON(userArray[i]); this.users.push(user1); } } /* * Parse it to JSON to store */ PickUpPoint.prototype.toJSON = function() { var usersArry = []; // ja.put(user.); var mainObj = {}; mainObj[ID] = this.id; mainObj[POINT] = this.point.toJSON(); mainObj[COUNT] = this.alertCount; mainObj[PICKED] = this.picked; mainObj[PRVDIST] = this.prevDist; mainObj[REACHTIME] = this.timeStamp; mainObj[LEAVETIME] = this.leaveTime; mainObj[PVDIST] = this.prvVehDist; mainObj[NEAR_TIME] = this.nearTime; mainObj[ORDER] = this.order; for (var i = 0; i < this.users.length; i++) { usersArry.push(this.users[i].toJSON()); } mainObj[USERS] = JSON.stringify(usersArry); return utility.convertFromAscii2Hexa(JSON.stringify(mainObj)); } /* * gets user count */ PickUpPoint.prototype.getUserCount = function() { return this.users.length; } /* * gets alert count. */ PickUpPoint.prototype.getAlertCount = function() { return this.alertCount; }