forked from saml-dev/MMM-Traffic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
115 lines (106 loc) · 4.16 KB
/
node_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Magic Mirror
* Module: MMM-Traffic
*
* By Sam Lewis /~https://github.com/SamLewis0602
* MIT Licensed.
*/
var NodeHelper = require('node_helper');
var request = require('request');
module.exports = NodeHelper.create({
start: function () {
console.log('MMM-Traffic helper started ...');
},
getCommute: function(api_url) {
var self = this;
request({url: api_url + "&departure_time=now", method: 'GET'}, function(error, response, body) {
if (!error && response.statusCode == 200) {
var trafficComparison = 0;
if((JSON.parse(body).status)=='OVER_QUERY_LIMIT')
{
console.log("API-Call Quote reached for today -> no more calls until 0:00 PST");
}
else
{
if (JSON.parse(body).routes[0].legs[0].duration_in_traffic) {
var commute = JSON.parse(body).routes[0].legs[0].duration_in_traffic.text;
var noTrafficValue = JSON.parse(body).routes[0].legs[0].duration.value;
var withTrafficValue = JSON.parse(body).routes[0].legs[0].duration_in_traffic.value;
trafficComparison = parseInt(withTrafficValue)/parseInt(noTrafficValue);
} else {
var commute = JSON.parse(body).routes[0].legs[0].duration.text;
}
var summary = JSON.parse(body).routes[0].summary;
self.sendSocketNotification('TRAFFIC_COMMUTE', {'commute':commute, 'url':api_url, 'trafficComparison': trafficComparison, 'summary':summary});
}
}
});
},
getTiming: function(api_url, arrivalTime) {
var self = this;
var newTiming = 0;
request({url: api_url + "&departure_time=now", method: 'GET'}, function(error, response, body) {
if (!error && response.statusCode == 200) {
var durationValue = JSON.parse(body).routes[0].legs[0].duration.value;
newTiming = self.timeSub(arrivalTime, durationValue, 0);
self.getTimingFinal(api_url, newTiming, arrivalTime);
}
});
},
getTimingFinal: function(api_url, newTiming, arrivalTime) {
var self = this;
request({url: api_url + "&departure_time=" + newTiming, method: 'GET'}, function(error, response, body) {
if (!error && response.statusCode == 200) {
var trafficValue = JSON.parse(body).routes[0].legs[0].duration_in_traffic.value;
var summary = JSON.parse(body).routes[0].summary;
var finalTime = self.timeSub(arrivalTime, trafficValue, 1);
self.sendSocketNotification('TRAFFIC_TIMING', {'commute':finalTime,'summary':summary, 'url':api_url});
}
});
},
timeSub: function(arrivalTime, durationValue, lookPretty) {
var currentDate = new Date();
var nowY = currentDate.getFullYear();
var nowM = (currentDate.getMonth() + 1).toString();
if (nowM.length == 1) {
nowM = "0" + nowM;
}
var nowD = currentDate.getDate();
nowD = nowD.toString();
if (nowD.length == 1) {
nowD = "0" + nowD;
}
var nowH = arrivalTime.substr(0,2);
var nowMin = arrivalTime.substring(2,4);
var testDate = new Date(nowY + "-" + nowM + "-" + nowD + " " + nowH + ":" + nowMin + ":00");
if (lookPretty == 0) {
if (currentDate >= testDate) {
var goodDate = new Date (testDate.getTime() + 86400000 - (durationValue*1000)); // Next day minus uncalibrated duration
return Math.floor(goodDate / 1000);
} else {
var goodDate = new Date (testDate.getTime() - (durationValue*1000)); // Minus uncalibrated duration
return Math.floor(testDate / 1000);
}
} else {
var finalDate = new Date (testDate.getTime() - (durationValue * 1000));
var finalHours = finalDate.getHours();
finalHours = finalHours.toString();
if (finalHours.length == 1) {
finalHours = "0" + finalHours;
}
var finalMins = finalDate.getMinutes();
finalMins = finalMins.toString();
if (finalMins.length == 1) {
finalMins = "0" + finalMins;
}
return finalHours + ":" + finalMins;
}
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === 'TRAFFIC_URL') {
this.getCommute(payload);
} else if (notification === 'LEAVE_BY') {
this.getTiming(payload.url, payload.arrival);
}
}
});