-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-BdxBus.js
215 lines (187 loc) · 7.74 KB
/
MMM-BdxBus.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*MIT License
This project is based on https://github.com/ottopaulsen/MMM-NesteBussAtB, modify by Quentin Delahaye.
Copyright (c) 2018 Otto Paulsen
Original name file: MMM-NesteBussAtB.js
Copyright (c) https://github.com/ottopaulsen/MMM-NesteBussAtB
Copyright (c) 2019 Quentin Delahaye
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
Module.register("MMM-BdxBus", {
// Default module config
defaults: {
showIcon: true,
showNumber: true,
showTo: true,
size: "medium",
colorDefault:"",
colorByDst:[],
stopUrl: [],
maxCount: 2, // Max number of next buses per route
stacked: true // Show multiple buses on same row, if same route and destination
},
start: function () {
console.log(this.name + ' started.')
this.buses = [];
this.openBusConnection();
var self = this;
setInterval(function () {
self.updateDom(0);
}, 10000);
},
openBusConnection: function () {
console.log('Sending BDXBUS_CONFIG with config: ', this.config);
this.sendSocketNotification('BDXBUS_CONFIG', this.config);
},
socketNotificationReceived: function (notification, payload) {
if (notification == 'BUS_DATA') {
if (payload != null) {
this.buses = this.config.stacked ? this.stackBuses(payload) : payload;
this.updateDom();
} else {
console.log(this.name + ': BUS_DATA - No payload');
}
}
},
stackBuses: function (buses) {
stackedBuses = [];
buses.sort(function (a, b) {
// return (self.toDate(a.time) - self.toDate(b.time));
return ('' + a.from + a.number + a.to + a.time).localeCompare('' + b.from + b.number + b.to + b.time);
});
var len = buses.length;
var previousStackvalue = '';
var stackedTimes = [];
if (len > 0) {
previousStackvalue = '' + buses[0].from + buses[0].number + buses[0].to;
stackedTimes.push(buses[0].time);
for (var i = 1; i < len; i++) {
stackvalue = '' + buses[i].from + buses[i].number + buses[i].to;
if (stackvalue == previousStackvalue) {
stackedTimes.push(buses[i].time);
} else {
stackedBuses.push({
from: buses[i - 1].from,
number: buses[i - 1].number,
to: buses[i - 1].to,
times: stackedTimes
});
previousStackvalue = stackvalue;
stackedTimes = [];
stackedTimes.push(buses[i].time)
}
}
stackedBuses.push({
from: buses[len - 1].from,
number: buses[len - 1].number,
to: buses[len - 1].to,
times: stackedTimes
});
}
return stackedBuses;
},
getColorByDest: function (destination) {
var result = "";
if(self.config.colorByDst.length > 0)
{
self.config.colorByDst.forEach(function (dst) {
if(dst.length > 1 && destination.includes(dst[0]))
{
result = dst[1];
}
});
}
return result;
},
getDom: function () {
self = this;
var wrapper = document.createElement("table");
wrapper.className = "medium";
var first = true;
if (self.buses.length === 0) {
wrapper.innerHTML = (self.loaded) ? self.translate("EMPTY") : self.translate("LOADING");
wrapper.className = "medium dimmed";
console.log(self.name + ': No buses');
return wrapper;
}
self.buses.forEach(function (bus) {
var now = new Date();
var minutes = '';
if(self.config.stacked) {
if(bus.times.length > 0) {
var busTime = self.toDate(bus.times[0]);
minutes = Math.round((busTime - now) / 60000);
}
for(var i=1; i < bus.times.length; i++){
var busTime = self.toDate(bus.times[i]);
minutes += ', ' + Math.round((busTime - now) / 60000);
}
} else {
var busTime = self.toDate(bus.time);
minutes = Math.round((busTime - now) / 60000);
}
var busWrapper = document.createElement("tr");
busWrapper.className = 'border_bottom ' + self.config.size + (first ? ' border_top' : '');
first = false; // Top border only on the first row
// Get color font
var fontColor = "";
if(self.config.colorDefault !== "") fontColor = self.config.colorDefault;
var fontColorByDst = self.getColorByDest(bus.to);
if(fontColorByDst !== "") fontColor =fontColorByDst ;
// Icon
if (self.config.showIcon) {
var iconWrapper = document.createElement("td");
iconWrapper.innerHTML = '<i class="fa fa-bus" aria-hidden="true"></i>';
iconWrapper.className = "align-right";
iconWrapper.style.color = fontColor;
busWrapper.appendChild(iconWrapper);
}
// Rute
if (self.config.showNumber) {
var numberWrapper = document.createElement("td");
numberWrapper.innerHTML = bus.number;
numberWrapper.className = "atb-number";
numberWrapper.style.color = fontColor;
busWrapper.appendChild(numberWrapper);
}
// Destinasjon
if (self.config.showTo) {
var toWrapper = document.createElement("td");
toWrapper.className = "align-left atb-to";
toWrapper.innerHTML = bus.to;
toWrapper.style.color = fontColor;
busWrapper.appendChild(toWrapper);
}
var minWrapper = document.createElement("td");
minWrapper.className = "align-left";
minWrapper.innerHTML = bus.times[0];
minWrapper.style.color = fontColor;
busWrapper.appendChild(minWrapper);
wrapper.appendChild(busWrapper);
});
return wrapper;
},
toDate: function (s) {
// Translate the API date to Date object
year = s.substring(0, 4);
month = parseInt(s.substring(5, 7)) - 1;
day = s.substring(8, 10);
hour = s.substring(11, 13);
minute = s.substring(14, 16);
time = new Date(year, month, day, hour, minute, 0, 0);
return time;
}
});