-
Notifications
You must be signed in to change notification settings - Fork 7
/
MMM-GoogleTrafficTimes.js
104 lines (87 loc) · 3.57 KB
/
MMM-GoogleTrafficTimes.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
Module.register("MMM-GoogleTrafficTimes",{
// Module config defaults
defaults : {
key: '',
origin: 'SW1A 1AA',
destination1: 'Work:SW1A 2PW',
destination2: 'Gym:XXX',
destination3: 'School:XXX',
updateInterval: 900000,
AvoidHighways: false,
AvoidTolls: false,
unitSystem: 'METRIC'
},
getStyles: function () {
return ["MMM-GoogleTrafficTimes.css"];
},
start: function() {
var self = this;
Log.info("Starting module: " + this.name);
if (this.config.key === "") {
Log.error("MMM-GoogleTrafficTimes: API key not provided or valid!");
return;
}
setInterval(function() {
self.updateDom();
}, this.config.updateInterval);
},
// Override dom generator.
getDom: function() {
var origin = this.config.origin;
var location1 = this.config.destination1.split(':')[1];
var location2 = this.config.destination2.split(':')[1];
var location3 = this.config.destination3.split(':')[1];
var nameList = [];
nameList[0] = this.config.destination1.split(':')[0];
nameList[1] = this.config.destination2.split(':')[0];
nameList[2] = this.config.destination3.split(':')[0];
var re1 = new RegExp(location1, 'g');
var re2 = new RegExp(location2, 'g');
var re3 = new RegExp(location3, 'g');
var AvoidHighways = this.config.AvoidHighways;
var AvoidTolls = this.config.AvoidTolls;
var wrapper = document.createElement("div");
wrapper.style = "text-align:left;font-size:0.65em;line-height:normal";
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://maps.googleapis.com/maps/api/js?key=" + this.config.key;
script.setAttribute('defer','');
script.setAttribute('async','');
document.body.appendChild(script);
//var self = this;
script.onload = function () {
var geocoder = new google.maps.Geocoder;
var service = new google.maps.DistanceMatrixService;
service.getDistanceMatrix({
origins: [origin],
destinations: [location1, location2, location3],
travelMode: 'DRIVING',
drivingOptions: {
departureTime: new Date(Date.now())
},
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: AvoidHighways,
avoidTolls: AvoidTolls
}, function(response, status) {
if (status !== 'OK') {
Log.error('Error was: ' + status);
} else {
var originList = response.originAddresses;
var destinationList = response.destinationAddresses;
let timings = '';
for (var i = 0; i < originList.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
var trafficIcon = 'CircleIcon';
if(typeof(results[j].duration_in_traffic) != 'undefined') {
if (results[j].duration_in_traffic.value > results[j].duration.value) {trafficIcon = 'RedCircleIcon'};
wrapper.innerHTML += '<span class="' + trafficIcon + '"><span>' + nameList[j] + '<p></p>' + results[j].duration_in_traffic.text + '</span></span>';
}
}
}
}
});
}
return wrapper;
}
});