forked from saml-dev/MMM-Traffic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
181 lines (164 loc) · 6.81 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* Magic Mirror
* Module: MMM-Traffic
*
* By Sam Lewis https://github.com/SamLewis0602
* MIT Licensed.
*/
var NodeHelper = require('node_helper');
var request = require('request');
var showWeekend = true;
var allTime = true;
module.exports = NodeHelper.create({
start: function () {
console.log('MMM-Traffic helper started ...');
},
getCommute: function(api_url) {
var self = this;
if (this.showWeekend && this.allTime) {
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("Google Maps API-Call Quota reached for today -> no more calls until 0:00 PST");
}
else
{
if (JSON.parse(body).hasOwnProperty('error_message')) {
console.log('ERROR: Google Maps API returned an error message:')
console.log(JSON.parse(body))
return
} else {
console.log('nope')
}
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;
var detailedSummary = self.getDetailedSummary(body);
self.sendSocketNotification('TRAFFIC_COMMUTE', {'commute':commute, 'url':api_url, 'trafficComparison': trafficComparison, 'summary':summary, 'detailedSummary':detailedSummary});
}
}
});
} else {
self.sendSocketNotification('TRAFFIC_COMMUTE', {'commute':'--', 'url':api_url, 'trafficComparison': 0.0, 'summary': '--'});
}
},
getTiming: function(api_url, arrivalTime) {
var self = this;
var newTiming = 0;
if (this.showWeekend && this.allTime) {
request({url: api_url + "&departure_time=now", method: 'GET'}, function(error, response, body) {
if (JSON.parse(body).hasOwnProperty('error_message')) {
console.log('ERROR: Google Maps API returned an error message:')
console.log(JSON.parse(body))
return
}
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);
}
});
} else {
self.sendSocketNotification('TRAFFIC_TIMING', {'commute':'--','summary':'--', 'url':api_url});
}
},
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) {
if (JSON.parse(body).routes[0].legs[0].duration_in_traffic) {
var trafficValue = JSON.parse(body).routes[0].legs[0].duration_in_traffic.value;
} else {
var trafficValue = JSON.parse(body).routes[0].legs[0].duration.value;
}
var summary = JSON.parse(body).routes[0].summary;
var detailedSummary = self.getDetailedSummary(body);
var finalTime = self.timeSub(arrivalTime, trafficValue, 1);
self.sendSocketNotification('TRAFFIC_TIMING', {'commute':finalTime,'summary':summary,'detailedSummary':detailedSummary,'url':api_url});
}
});
},
getDetailedSummary: function(body) {
steps = JSON.parse(body).routes[0].legs[0].steps;
summaryString = '<table id="detailedSummary">';
for (var i = 0; i < steps.length; i++) {
var html_instructions = steps[i].html_instructions.replace(/<div(.*?)<\/div>/g,'').replace(/<\/?b>/g,'');
summaryString += '<tr><td>';
summaryString += steps[i].duration.text + '</td><td>' + steps[i].distance.text + '</td><td>' + html_instructions;
summaryString += '</td></tr>';
}
summaryString += '</table>';
return summaryString;
},
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;
}
},
setTimeConfig: function(timeConfig) {
var date = new Date;
this.showWeekend = timeConfig.showWeekend;
this.allTime = timeConfig.allTime;
if (!this.showWeekend) {
var day = date.getDay();
this.showWeekend = (day > 0 && day < 6);
}
if (!this.allTime) {
var hour = date.getHours();
this.allTime = (hour >= timeConfig.startHr && hour <= timeConfig.endHr);
}
},
getURLParamter: function(param, url) {
return decodeURIComponent((new RegExp('[?|&]' + param + '=' + '([^&;]+?)(&|#|;|$)').exec(url) || [null, ''])[1].replace(/\+/g, '%20')) || null;
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
this.setTimeConfig(payload.timeConfig);
this.mode = this.getURLParamter('mode', payload.url);
if (notification === 'TRAFFIC_URL') {
this.getCommute(payload.url);
} else if (notification === 'LEAVE_BY') {
this.getTiming(payload.url, payload.arrival);
}
}
});