-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMM-SG-Transport-V2.js
203 lines (169 loc) · 6.56 KB
/
MMM-SG-Transport-V2.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
/* Magic Mirror Module Arrival times for bus stops in Singapore */
/* Magic Mirror
* Module: MMM-SG-Transport-V2
*
* By Moses Yong
*/
Module.register("MMM-SG-Transport-V2", {
// Default module config.
defaults: {
// API details, all required
lta_api_key: null, // this must be set
lta_api_url: "http://datamall2.mytransport.sg/ltaodataservice/",
lta_api_bus_arrival_path: "BusArrivalv2",
// Intervals
refresh_interval: 30 * 1000, // refresh display every 30 seconds
// Bus Stop IDs and Names to show
bus_stops: [{
BusStopCode: 43191,
name: "Opp St Mary's",
BusNumbers: [
"157",
"61",
"852"
]
},
{
BusStopCode: 43619,
name: "Opp Caltex"
}
],
},
// Module startup procedure
start: function() {
// Create the main structure that holds the live bus stop data
this.bus_stops = {}
for (var i in this.config.bus_stops) {
var config_bus_stop = this.config.bus_stops[i];
this.bus_stops[config_bus_stop.BusStopCode] = {
Name: config_bus_stop.name,
Services: null,
BusNumbers: config_bus_stop.BusNumbers
};
}
// Share the config with the node_helper
this.sendSocketNotification("CONFIG", this.config);
},
// Include styles:
getStyles: function() {
return [
this.file("css/style.css")
]
},
createBusStopLabelRow: function(label) {
var row = document.createElement("tr");
row.classList.add("sg-transport-bus-stop-label-row");
var element = document.createElement("th");
element.classList.add("sg-transport-bus-stop-label");
element.setAttribute("colspan", 4);
element.innerHTML = label;
row.appendChild(element);
return row;
},
createBusRow: function(bus) {
ArrivalTimeArray = [];
CrowdingLevelArray = []
if (bus.NextBus.EstimatedArrival !== "") {
ArrivalTimeArray.push(bus.NextBus.EstimatedArrival);
CrowdingLevelArray.push(bus.NextBus.Load)
}
if (bus.NextBus2.EstimatedArrival !== "") {
ArrivalTimeArray.push(bus.NextBus2.EstimatedArrival);
CrowdingLevelArray.push(bus.NextBus.Load)
}
if (bus.NextBus3.EstimatedArrival !== "") {
ArrivalTimeArray.push(bus.NextBus3.EstimatedArrival);
CrowdingLevelArray.push(bus.NextBus.Load)
}
// Don't display if no data
if (ArrivalTimeArray.length == 0) {
return document.createElement("div");
}
var row = document.createElement("tr");
var element = document.createElement("td");
element.innerHTML = bus.ServiceNo + ": "
row.appendChild(element);
for (var i = 0; i < ArrivalTimeArray.length; i++) {
var ArrivalTime = ArrivalTimeArray[i];
var CrowdingLevel = CrowdingLevelArray[i];
var time = new Date(ArrivalTime);
var now = new Date();
var arrivalTimeInMinutes = Math.floor((time - now) / (60 * 1000));
if (arrivalTimeInMinutes < 1) {
arrivalTimeInMinutes = "Arr"
} else {
arrivalTimeInMinutes += "m "
}
var element = document.createElement("td");
element.innerHTML = arrivalTimeInMinutes + ": ";
// Location of stickman image
var pathToImage = "modules/MMM-SG-Transport-V2/images/stickman.png";
// Height of image
var height = 17;
var image = "<img src='" + pathToImage + "' height='" + height + "'>"
// Seating available
if (CrowdingLevel == "SEA") {
// print once
element.innerHTML += image;
// Standing available
} else if (CrowdingLevel == "SDA") {
element.innerHTML += image;
element.innerHTML += image;
// Limited standing
} else if (CrowdingLevel == "LSD") {
element.innerHTML += image;
element.innerHTML += image;
element.innerHTML += image;
}
row.appendChild(element);
};
return row;
},
createTextRow: function(text) {
var row = document.createElement("tr");
var element = document.createElement("td");
element.innerHTML = text;
row.appendChild(element);
return row;
},
// Run on display refresh
getDom: function() {
// Display data
var wrapper = document.createElement("div");
wrapper.classList.add("small");
var table = document.createElement("table");
for (var bus_stop_id in this.bus_stops) {
var self = this;
let busNumbers = this.bus_stops[bus_stop_id].BusNumbers;
table.appendChild(this.createBusStopLabelRow(this.bus_stops[bus_stop_id].Name));
var services = this.bus_stops[bus_stop_id].Services;
if (services == null) {
table.appendChild(this.createTextRow("Waiting for update..."));
} else {
var services_exist = false; // flag for services with no more arrivals
services.forEach(function(bus) {
if (busNumbers == null || busNumbers.length == 0) {
table.appendChild(self.createBusRow(bus));
services_exist = true;
} else if (busNumbers.includes(bus.ServiceNo)) {
table.appendChild(self.createBusRow(bus));
services_exist = true;
}
});
// If there are no more arrivals for bus services that we want,
// then the previous block should not have set services_exist to true
if (!services_exist) {
table.appendChild(this.createTextRow("No more buses"));
}
}
}
wrapper.appendChild(table);
return wrapper;
},
socketNotificationReceived: function(notification, payload) {
if (notification === "UPDATE") {
this.bus_stops[payload.BusStopCode].Services = payload.Services;
}
this.updateDom();
}
});