flaglib
Version:
Ignition event 15 added.
170 lines (130 loc) • 4.05 kB
JavaScript
var Util = require('./util.js');
var logger = require('./logger');
module.exports = ActiveDays;
var ID = "0";
var DAYS = "1";
var TYPE = "2";
var SDAY = "3";
var EDAY = "4";
var HOLIDAY = 100;
var WORKING = 101;
var utility = new Util();
function ActiveDays(pid, result) {
if ((result == null) || (result == undefined)){
this.invalid = true;
return;
}
this.invalid = false;
this.days = [];
var count = result.length;
for (var i = 0;i<count;i++){
var day = {};
day[TYPE] = result[i].day_type;
var str = ""+result[i].holiday_date
day[SDAY] = FormatSQLDateObject(str);
str = ""+result[i].holiday_enddate;
day[EDAY] = FormatSQLDateObject(str);
this.days.push(JSON.stringify(day));
}
}
function FormatSQLDateObject(strDate){
var date = new Date(strDate);
var format = ""+date.getFullYear()+"-"+("0" + (date.getMonth() + 1)).slice(-2)+"-"+("0" + date.getDate()).slice(-2)+" "+"00:00:00";
return format;
}
function createFromMysql(mysql_string)
{
var t, result = null;
if( typeof mysql_string === 'string' )
{
t = mysql_string.split(/[- :]/);
//when t[3], t[4] and t[5] are missing they defaults to zero
result = new Date(t[0], t[1] - 1, t[2], t[3] || 0, t[4] || 0, t[5] || 0);
}
return result;
}
function compareDay(day1, startDate){
return ( (day1.getYear() == startDate.getYear()) && (day1.getMonth() == startDate.getMonth()) && (day1.getDate() == startDate.getDate()))
}
function isToday(date, day){
var startDate = createFromMysql(day[SDAY]);
var endDate = createFromMysql(day[EDAY]);
var day1 = new Date("20"+date.slice(0, 2), date.slice(2, 4)-1, date.slice(4, 6),
date.slice(6, 8), date.slice(8, 10), date.slice(10, 12));
//day = day.getTime();
console.log("start date: "+startDate + "end date: "+endDate+" day1: "+ day1);
if ((startDate != null) && (endDate != null)){
return (day1 >= startDate && day1 <= endDate) || (compareDay(day1, startDate) || compareDay(day1, endDate));
}
if ((startDate != null) && (endDate == null)){
console.log("compareDay : "+compareDay(day1, startDate));
return compareDay(day1, startDate);
}
return false;
}
/*
* Input time = the time from the devcie time format. "YYMMDDHHMMSS"
*/
ActiveDays.prototype.isActive = function(time) {
// check the current day is HOLIDAY or WORKING DAY.
var length = this.days.length;
var status = true;
for (var i = 0; i < length; i++) {
var day = JSON.parse(this.days[i]);
var val = isToday(time, day);
// check if it is fall in special working day or holiday
console.log("isActive : "+val + "day[TYPE]: "+day[TYPE]);
if ((val) && (day[TYPE] == WORKING) ) {
status = true;
return status;
}else if ((val) && (day[TYPE] == HOLIDAY)){
status = false;
}
}
return status;
}
ActiveDays.prototype.getId = function() {
return this.id;
}
/*
* 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
*/
ActiveDays.prototype.isInValid = function() {
return this.invalid;
}
/*
* Parse JSON obejct and create objects
*/
ActiveDays.prototype.fromJSON = function(data) {
if (data == null) {
this.invalid = true;
return;
}
this.days = [];
var flagJson = null;
flagJson = JSON.parse(utility.hex2a(data));
this.id = flagJson[ID];
this.days = [];
var days = JSON.parse(flagJson[DAYS]);
for (var i = 0; i < days.length; i++) {
this.days.push(days[i]);
}
}
/*
* Parse it to JSON to store
*/
ActiveDays.prototype.toJSON = function() {
var days = [];
// ja.put(user.);
var mainObj = {};
mainObj[ID] = this.id;
for (var i = 0; i < this.days.length; i++) {
days.push(this.days[i]);
}
mainObj[DAYS] = JSON.stringify(days);
return utility.convertFromAscii2Hexa(JSON.stringify(mainObj));
}