forked from qistoph/MMM-MyCommute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
116 lines (91 loc) · 3.41 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
/* Magic Mirror
* Module: mrx-work-traffic
*
* By Dominic Marx
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
var request = require("request");
var moment = require("moment");
module.exports = NodeHelper.create({
start: function() {
console.log("====================== Starting node_helper for module [" + this.name + "]");
},
// subclass socketNotificationReceived
socketNotificationReceived: function(notification, payload){
if (notification === "GOOGLE_TRAFFIC_GET") {
//first data opull after new config
this.getPredictions(payload);
}
},
getPredictions: function(payload) {
var self = this;
var returned = 0;
var predictions = new Array();
payload.destinations.forEach(function(dest, index) {
request({url: dest.url, method: "GET"}, function(error, response, body) {
var prediction = new Object({
config: dest.config
});
if(!error && response.statusCode == 200){
var data = JSON.parse(body);
if (data.error_message) {
console.log("MMM-MyCommute: " + data.error_message);
prediction.error = true;
} else if (data.status !== "OK") {
console.log("MMM-MyCommute: " + data.status);
prediction.error = true;
} else {
var routeList = new Array();
for (var i = 0; i < data.routes.length; i++) {
var r = data.routes[i];
var routeObj = new Object({
summary: r.summary,
time: r.legs[0].duration.value
});
if (r.legs[0].duration_in_traffic) {
routeObj.timeInTraffic = r.legs[0].duration_in_traffic.value;
}
if (dest.config.mode && dest.config.mode == "transit") {
var transitInfo = new Array();
var gotFirstTransitLeg = false;
for (var j = 0; j < r.legs[0].steps.length; j++) {
var s = r.legs[0].steps[j];
if (s.transit_details) {
var arrivalTime = "";
if (!gotFirstTransitLeg && dest.config.showNextVehicleDeparture) {
gotFirstTransitLeg = true;
// arrivalTime = ' <span class="transit-arrival-time">(next at ' + s.transit_details.departure_time.text + ')</span>';
arrivalTime = moment(s.transit_details.departure_time.value * 1000);
}
transitInfo.push({routeLabel: s.transit_details.line.short_name ? s.transit_details.line.short_name : s.transit_details.line.name, vehicle: s.transit_details.line.vehicle.type, arrivalTime: arrivalTime});
}
}
routeObj.transitInfo = transitInfo;
if (transitInfo.length <= 0) {
var travelModes = r.legs[0].steps.map(s => s.travel_mode).join(", ");
console.log("MMM-MyCommute: transit directrions does not contain any transits (" + travelModes + ")");
prediction.error = true;
}
}
routeList.push(routeObj);
}
prediction.routes = routeList;
}
} else {
prediction.error = true;
if (response !== undefined) {
console.log( "Error getting traffic prediction: " + response.statusCode );
} else {
console.log( "Error getting traffic prediction: " + error );
}
}
predictions[index] = prediction;
returned++;
if (returned == payload.destinations.length) {
self.sendSocketNotification("GOOGLE_TRAFFIC_RESPONSE" + payload.instanceId, predictions);
}
});
});
}
});