-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-Bluebikes.js
167 lines (131 loc) · 4.09 KB
/
MMM-Bluebikes.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* global Module, Log */
/*
* Magic Mirror Module: MMM-Bubi (https://github.com/balassy/MMM-Bubi)
* By György Balássy (https://www.linkedin.com/in/balassy)
* MIT Licensed.
*/
Module.register('MMM-Bluebikes', {
defaults: {
updateInterval: 30000,
showPlaceName: true,
url: "https://gbfs.bluebikes.com/gbfs/en/station_status.json",
places: [
{id: "76", name: "City Hall"},
{id: "116", name: "359 Broadway"},
{id: "107", name: "TPP"}
]
},
viewModel: {
places: []
},
requiresVersion: '2.1.0',
getStyles() {
return [
'MMM-Bluebikes.css'
];
},
start() {
const self = this;
this.hasData = false;
this._getData(() => self.updateDom());
setInterval(() => {
self._getData(() => self.updateDom());
}, this.config.updateInterval);
},
getDom() {
const wrapper = document.createElement('div');
if (this.viewModel) {
const tableEl = document.createElement('table');
tableEl.classList = 'small';
if (this.config.align === 'left') {
tableEl.classList += ' align-left';
}
if (this.config.align === 'right') {
tableEl.classList += ' align-right';
}
const rowEl = document.createElement('tr');
const symbolEl = document.createElement('th');
symbolEl.classList = 'fa fa-bicycle dimmed symbol-cell';
rowEl.appendChild(symbolEl);
const countEl = document.createElement('th');
countEl.classList = 'count-cell';
countEl.innerText = '# Bikes';
rowEl.appendChild(countEl);
const dockEl = document.createElement('th');
dockEl.classList = 'count-cell';
dockEl.innerText = '# Docks';
rowEl.appendChild(dockEl);
const nameEl = document.createElement('th');
nameEl.classList = 'dimmed small name-cell';
nameEl.innerText = 'Location';
rowEl.appendChild(nameEl);
tableEl.appendChild(rowEl);
this.viewModel.places.forEach((place) => {
const rowEl = document.createElement('tr');
const symbolEl = document.createElement('td');
symbolEl.classList = 'fa fa-bicycle dimmed symbol-cell';
rowEl.appendChild(symbolEl);
const countEl = document.createElement('td');
countEl.classList = 'count-cell';
countEl.innerText = place.numberOfBikes;
rowEl.appendChild(countEl);
const dockEl = document.createElement('td');
dockEl.classList = 'count-cell';
dockEl.innerText = place.numberOfDocks;
rowEl.appendChild(dockEl);
const nameEl = document.createElement('td');
nameEl.classList = 'dimmed small name-cell';
nameEl.innerText = place.name;
rowEl.appendChild(nameEl);
tableEl.appendChild(rowEl);
});
wrapper.appendChild(tableEl);
const clearfixEl = document.createElement('div');
clearfixEl.classList = 'clearfix';
wrapper.appendChild(clearfixEl);
} else {
const loadingEl = document.createElement('span');
loadingEl.innerHTML = this.translate('LOADING');
loadingEl.classList = 'dimmed small';
wrapper.appendChild(loadingEl);
}
return wrapper;
},
_getData(onCompleteCallback) {
const self = this;
const xhr = new XMLHttpRequest();
xhr.open('GET', this.config.url, true);
xhr.onreadystatechange = function onReadyStateChange() {
if (this.readyState === 4) {
if (this.status === 200) {
self._processResponse(this.response);
onCompleteCallback();
} else {
Log.error(self.name, `MMM-Bluebikes: Failed to load data. XHR status: ${this.status}`);
}
}
};
xhr.send();
},
_processResponse(responseBody) {
const response = JSON.parse(responseBody);
const places = response.data.stations;
this.viewModel.places = [];
for (let i = 0; i < this.config.places.length; i++) {
const place = places.find(p => p.station_id === this.config.places[i].id);
// const place = places[this.config.places[i].id];
Log.log(place);
this.viewModel.places.push({
name: this.config.showPlaceName
? this.config.places[i].name// || place.name
: null,
numberOfBikes: place.num_bikes_available,
numberOfDocks: place.num_docks_available
});
}
if (!this.hasData) {
this.updateDom();
}
this.hasData = true;
}
});