-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-OsloCityBike.js
138 lines (108 loc) · 3.81 KB
/
MMM-OsloCityBike.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
/* Magic Mirror
* Module: OsloCityBike
*
* By Tobias Lønnerød Madsen (https://github.com/TobbenTM)
* Based on MMM-Ruter by Cato Antonsen (https://github.com/CatoAntonsen)
* MIT Licensed.
*/
Module.register("MMM-OsloCityBike",{
// Default module config.
defaults: {
serviceReloadInterval: 60000, // Refresh rate in MS for how often we call Ruter's web service. NB! Don't set it too low! (default is 60 seconds)
timeReloadInterval: 1000, // Refresh rate how often we check if we need to update the time shown on the mirror (default is every second)
animationSpeed: 0, // How fast the animation changes when updating mirror (default is 0 second)
fade: true, // Set this to true to fade list from light to dark. (default is true)
fadePoint: 0.25 // Start on 1/4th of the list.
},
getStyles: function () {
return ["ruter.css"];
},
getTranslations: function() {
return {
en: "translations/en.json",
nb: "translations/nb.json"
}
},
start: function() {
console.log(this.translate("STARTINGMODULE") + ": " + this.name);
this.stations = [];
var self = this;
// Just to an initial poll. Otherwise we have to wait for the serviceReloadInterval
self.startPolling();
setInterval(function() {
self.startPolling();
}, this.config.serviceReloadInterval);
},
getDom: function() {
if (this.stations && this.stations.length > 0) {
var table = document.createElement("table");
table.className = "ruter small";
table.appendChild(this.getTableHeaderRow());
for(var i = 0; i < this.stations.length; i++) {
var station = this.stations[i];
var tr = this.getTableRow(station);
table.appendChild(tr);
}
return table;
} else {
var wrapper = document.createElement("div");
wrapper.innerHTML = this.translate("LOADING");
wrapper.className = "small dimmed";
}
return wrapper;
},
startPolling: function() {
var self = this;
var request = new Promise((resolve) => {
this.getStations((res) => resolve(res));
})
.then((result) => {
self.stations = result;
this.updateDom(this.config.animationSpeed);
});
},
getStations: function(callback) {
var HttpClient = function() {
this.get = function(requestUrl, requestCallback) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState == 4 && httpRequest.status == 200)
requestCallback(httpRequest.responseText);
}
httpRequest.open( "GET", requestUrl, true );
httpRequest.send( null );
}
}
var conf = this.config;
var url = `https://reisapi.ruter.no/Place/GetCityBikeStations?longmin=${conf.long.min}&longmax=${conf.long.max}&latmin=${conf.lat.min}&latmax=${conf.lat.max}`;
var client = new HttpClient();
client.get(url, function(response) {
callback(JSON.parse(response))
});
},
getTableHeaderRow: function() {
var thStation = document.createElement("th");
thStation.className = "light";
thStation.appendChild(document.createTextNode(this.translate("STATIONHEADER")));
var thBikes = document.createElement("th");
thBikes.className = "light";
thBikes.appendChild(document.createTextNode(this.translate("BIKEHEADER")));
var thead = document.createElement("thead");
thead.addClass = "xsmall dimmed";
thead.appendChild(thStation);
thead.appendChild(thBikes);
return thead;
},
getTableRow: function(station) {
var tdStation = document.createElement("td");
tdStation.className = "station";
tdStation.appendChild(document.createTextNode(station.Title));
var tdBikes = document.createElement("td");
tdBikes.className = "destination bright";
tdBikes.appendChild(document.createTextNode(station.Availability.Bikes));
var tr = document.createElement("tr");
tr.appendChild(tdStation);
tr.appendChild(tdBikes);
return tr;
}
});