flaglib
Version:
Ignition event 15 added.
171 lines (129 loc) • 4.21 kB
JavaScript
var util = require('util');
var Util = require('./util.js');
var logger = require('./logger');
var Flag = require('./flag.js');
var FlagConstants = require('./flagconstants.js');
var FlagCategories = require('./flagcategories.js');
module.exports = Vehicle;
var SERIAL_NUMBER = "1";
var SPEED_CUTOFF = "2";
var CUTOFF_CROSSED = "3";
var SPEED_CUTOFF_ALERTED = "4";
var SPEED_CUTOFF_CROSSED_COUNT = "5";
var RFID = "6"
var VID = "7"
var DISTANCE = "8";
var IGNITION_STATUS = "9";
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 Vehicle(vid, sno,scutoff,rfid) {
this.vid = vid;
this.sno = sno;
this.scutoff = scutoff;
this.cutoffCrossed = false;
this.invalid = false;
this.cutoff_alerted = false;
this.cutoffCrossed_count = 0;
this.distance = 0;
this.ignition_status = 0; // 0 = IGNITION_OFF, 1 = IGNITION_OFF
this.rfid = rfid;
}
Vehicle.prototype.getIgnitionStatus = function(){
return this.ignition_status;
}
Vehicle.prototype.setIgnitionStatus = function(status){
this.ignition_status = status;
}
Vehicle.prototype.isSpeedCutoffCrossed = function(flagData){
var flag = new Flag(flagData);
if( flag.getFlagValue(FlagConstants.FLAG_CATEGORY) != FlagCategories.CTG_FLAG_RFID_SWIPE ){
this.distance = flag.getFlagValue(FlagConstants.FLAG_TRACK_DISTANCE);
}
if (this.cutoff_alerted){
if ( this.scutoff > flag.getFlagValue(FlagConstants.FLAG_SPEED) ){
this.cutoff_alerted = false;
this.cutoffCrossed = false;
this.cutoffCrossed_count = 0;
}
return null;
}
if ( (this.scutoff < flag.getFlagValue(FlagConstants.FLAG_SPEED)) ){
if ( (!this.cutoff_alerted) && (this.cutoffCrossed_count >= 5) ){
var data = {userMobile:flag.getFlagValue(FlagConstants.FLAG_SENDER),cutoff:this.scutoff, speed:flag.getFlagValue(FlagConstants.FLAG_SPEED)};
this.cutoff_alerted = true;
this.cutoffCrossed = true;
this.cutoffCrossed_count = 0;
return data;
}else{
this.cutoffCrossed_count++;
return null;
}
}else{
this.cutoff_alerted = false;
this.cutoffCrossed = false;
this.cutoffCrossed_count = 0;
}
return null;
}
Vehicle.prototype.checkRFIDSwipe = function(flagData, callback){
var flag = new Flag(flagData);
var rfid = null;
//check if the flag category is swipe or track
if( flag.getFlagValue(FlagConstants.FLAG_CATEGORY) == FlagCategories.CTG_FLAG_RFID_SWIPE ){
rfid = flag.getFlagValue(FlagConstants.FLAG_RFID_ID);
}
if((rfid) && (rfid == this.rfid) && (callback) ){
var date = flag.getFlagValue(FlagConstants.FLAG_DATE);
var dist = this.distance;
callback(this.vid, date, dist,flag.getFlagValue(FlagConstants.FLAG_LATITUDE),flag.getFlagValue(FlagConstants.FLAG_LONGITUDE));
}
}
Vehicle.prototype.isInValid = function(){
return this.invalid;
}
Vehicle.prototype.injectData = function(sno,scutoff){
this.sno = sno;
this.scutoff = scutoff;
this.invalid = false;
}
/*
* Parse JSON obejct and create objects
*/
Vehicle.prototype.fromJSON = function(data){
if (data == null){
this.invalid = true;
return;
}
var flagJson = null;
flagJson = JSON.parse(utility.hex2a(data));
this.sno = flagJson[SERIAL_NUMBER];
this.scutoff = flagJson[SPEED_CUTOFF];
this.cutoff_alerted = flagJson[SPEED_CUTOFF_ALERTED];
this.cutoffCrossed = flagJson[CUTOFF_CROSSED];
this.cutoffCrossed_count = flagJson[SPEED_CUTOFF_CROSSED_COUNT];
this.rfid = flagJson[RFID];
this.vid = flagJson[VID];
this.distance = flagJson[DISTANCE];
this.ignition_status = flagJson[IGNITION_STATUS];
}
/*
* Parse it to JSON to store
*/
Vehicle.prototype.toJSON = function(){
var mainObj = {};
mainObj[SERIAL_NUMBER] = this.sno;
mainObj[SPEED_CUTOFF] = this.scutoff;
mainObj[SPEED_CUTOFF_ALERTED] = this.cutoff_alerted;
mainObj[CUTOFF_CROSSED] = this.cutoffCrossed;
mainObj[SPEED_CUTOFF_CROSSED_COUNT] = this.cutoffCrossed_count;
mainObj[RFID] = this.rfid;
mainObj[VID] = this.vid;
mainObj[DISTANCE] = this.distance;
mainObj[IGNITION_STATUS] = this.ignition_status;
return utility.convertFromAscii2Hexa(JSON.stringify(mainObj));
}