forked from saml-dev/MMM-Traffic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
44 lines (38 loc) · 1.47 KB
/
Copy pathnode_helper.js
File metadata and controls
44 lines (38 loc) · 1.47 KB
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
/* 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, method: 'GET'}, function(error, response, body) {
if (!error && response.statusCode == 200) {
var trafficComparison = 0;
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});
}
});
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
//console.log(notification);
if (notification === 'TRAFFIC_URL') {
this.getCommute(payload);
}
}
});