-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-trafficmate.js
executable file
·129 lines (112 loc) · 3.25 KB
/
MMM-trafficmate.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
/* global Module */
/* Magic Mirror
* Module: MMM-trafficmate
*
* By Mark Fodor https://github.com/markfodor
* MIT Licensed.
*/
Module.register("MMM-trafficmate", {
defaults: {
defaultText: config.defaultText || 'Route duration: ',
travelMode: config.travelMode || 'DRIVING',
origin: config.origin,
destination: config.destination,
apiKey: config.apiKey,
updateInterval: 6000,
retryDelay: 5000
},
start: function () {
var self = this;
this.reponse = null;
//Flag for check if module is loaded
this.loaded = false;
//TODO apiKey check
var googleScript = document.createElement("script");
googleScript.src = "https://maps.googleapis.com/maps/api/js?key=" + self.config.apiKey;
document.getElementsByTagName("body")[0].appendChild(googleScript);
// Schedule update timer.
setInterval(function () {
self.getData();
self.updateDom();
}, this.config.updateInterval);
},
getData: function () {
var self = this;
if (google) {
var directionsService = new google.maps.DirectionsService;
directionsService.route({
destination: self.config.destination,
origin: self.config.origin,
travelMode: self.config.travelMode
}, function (response, status) {
if (status === "OK") {
self.response = response;
} else {
Log.error("Directions request failed due to " + status);
}
});
} else {
Log.error("Cloud not load google service.");
}
},
/* scheduleUpdate()
* Schedule next update.
*
* argument delay number - Milliseconds before next update.
* If empty, this.config.updateInterval is used.
*/
scheduleUpdate: function (delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
nextLoad = nextLoad;
var self = this;
setTimeout(function () {
self.getData();
}, nextLoad);
},
getDom: function () {
var duration = this.response ? this.response.routes[0].legs[0].duration.text : "Loading...";
// create element wrapper for show into the module
var wrapper = document.createElement("div");
wrapper.innerHTML = this.config.defaultText + duration;
// Data from helper
if (this.dataNotification) {
var wrapperDataNotification = document.createElement("div");
// translations + datanotification
wrapperDataNotification.innerHTML = this.translate("UPDATE") + ": " + this.dataNotification.date;
wrapper.appendChild(wrapperDataNotification);
}
return wrapper;
},
getScripts: function () {
return [];
},
// Load translations files
getTranslations: function () {
//FIXME: This can be load a one file javascript definition
return {
en: "translations/en.json",
es: "translations/es.json"
};
},
processData: function (data) {
var self = this;
if (this.loaded === false) {
self.updateDom(self.config.animationSpeed);
}
this.loaded = true;
// the data if load
// send notification to helper
this.sendSocketNotification("MMM-trafficmate-NOTIFICATION", data);
},
// socketNotificationReceived from helper
socketNotificationReceived: function (notification, payload) {
if (notification === "MMM-trafficmate-NOTIFICATION") {
// set dataNotification
this.dataNotification = payload;
this.updateDom();
}
},
});