-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
183 lines (157 loc) · 8.18 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
182
183
/* Magic Mirror
* Module: MMM-TrafficCal
*
* 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-TrafficCal helper started ...');
},
getCommute: function(payload) {
var self = this;
var commutes = [];
var trafficComparisons = [];
var summaries = [];
var noTraffics = [];
var withTraffics = [];
var destinations = [];
var startdates = [];
var origins = [];
var modes = [];
var api_urls = [];
var comments = [];
var noTrafficValue = '';
var withTrafficValue = '';
if (payload[0].length === 0) {
self.sendSocketNotification('TRAFFIC_COMMUTE', {
'commutes': commutes,
'trafficComparisons': trafficComparisons,
'summaries': summaries,
'noTraffics': noTraffics,
'withTraffics': withTraffics,
'destinations': destinations,
'startdates' : startdates,
'origins': origins,
'modes': modes,
'api_urls': api_urls,
'comments': comments
});
} else {
for (var e in payload[0]) {
this.api_url = payload[0][e];
console.log(this.api_url);
request({
url: this.api_url,
method: 'GET'
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
var trafficComparison = 0;
if (JSON.parse(body).status == 'OK') {
var lUrl = decodeURI(this.uri.href);
var lQuery = decodeURI(this.uri.query);
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;
var noTraffic = JSON.parse(body).routes[0].legs[0].duration.text;
var withTraffic = JSON.parse(body).routes[0].legs[0].duration_in_traffic.text;
var trafficComparison = parseInt(withTrafficValue) / parseInt(noTrafficValue);
var summary = JSON.parse(body).routes[0].summary;
} else {
var commute = JSON.parse(body).routes[0].legs[0].duration.text;
}
} else {
var commute = JSON.parse(body).error_message;
var summary = JSON.parse(body).status;
}
var queries = lQuery.split("&");
for (var f in queries) {
if (queries[f].indexOf("mode=") != -1) {
modes.push(queries[f].substring(5));
}
if (queries[f].indexOf("comment=") != -1) {
comments.push(queries[f].substring(8));
}
if (queries[f].indexOf("origin=") != -1) {
origins.push(queries[f].substring(7));
}
if (queries[f].indexOf("destination=") != -1) {
destinations.push(queries[f].substring(12));
}
if (queries[f].indexOf("startDate=") != -1) {
console.log('startdate: ' + queries[f].substring(10));
startdates.push(queries[f].substring(10));
}
}
commutes.push(commute);
trafficComparisons.push(trafficComparison);
summaries.push(summary);
noTraffics.push(noTraffic);
withTraffics.push(withTraffic);
api_urls.push(this.lUrl);
if (commutes.length == payload[0].length) {
var list = [];
for (var j in startdates)
list.push({ 'commute': commutes[j],
'trafficComparison': trafficComparisons[j],
'summarie': summaries[j],
'noTraffic': noTraffics[j],
'withTraffic': withTraffics[j],
'destination': destinations[j],
'startdate' : startdates[j],
'origin': origins[j],
'mode': modes[j],
'api_url': api_urls[j],
'comment': comments[j]
});
//2) sort:
list.sort(function(a, b) {
return ((a.startdate < b.startdate) ? -1 : ((a.startdate == b.startdate) ? 0 : 1));
//Sort could be modified to, for example, sort on the age
// if the name is the same.
});
//3) separate them back out:
for (var k = 0; k < list.length; k++) {
commutes[k] = list[k].commute;
trafficComparisons[k] = list[k].trafficComparison;
summaries[k] = list[k].summarie;
noTraffics[k] = list[k].noTraffic;
withTraffics[k] = list[k].withTraffic;
destinations[k] = list[k].destination;
startdates[k] = list[k].startdate;
origins[k] = list[k].origin;
modes[k] = list[k].mode;
api_urls[k] = list[k].api_url;
comments[k] = list[k].comment;
}
self.sendSocketNotification('TRAFFIC_COMMUTE', {
'commutes': commutes,
'trafficComparisons': trafficComparisons,
'summaries': summaries,
'noTraffics': noTraffics,
'withTraffics': withTraffics,
'destinations': destinations,
'startdates' : startdates,
'origins': origins,
'modes': modes,
'api_urls': api_urls,
'comments': comments
});
}
}
}
);
}
}
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
//console.log(notification + payload);
if (notification === 'TRAFFIC_URL') {
this.getCommute(payload);
}
}
});