-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-SG-Transport.js
153 lines (126 loc) · 4.44 KB
/
MMM-SG-Transport.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
/* Magic Mirror Module Arrival times for bus stops in Singapore */
/* Magic Mirror
* Module: MMM-SG-Transport
*
* By Tan Xuan You
*/
Module.register("MMM-SG-Transport", {
// 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: "BusArrival",
// Intervals
refresh_interval: 10 * 1000, // refresh display every 10 seconds
// Bus Stop IDs and Names to show
bus_stops: [{
id: 43191,
name: "Opp St Mary's"
},
{
id: 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.id] = {
Name: config_bus_stop.name,
Services: null
};
}
// 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) {
// Calculate arrival times
ArrivalTimeArray = [];
if (bus.NextBus.EstimatedArrival !== "") {
ArrivalTimeArray.push(bus.NextBus.EstimatedArrival);
}
if (bus.SubsequentBus.EstimatedArrival !== "") {
ArrivalTimeArray.push(bus.SubsequentBus.EstimatedArrival);
}
if (bus.SubsequentBus3.EstimatedArrival !== "") {
ArrivalTimeArray.push(bus.SubsequentBus3.EstimatedArrival);
}
// 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);
ArrivalTimeArray.forEach(function(ArrivalTime) {
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;
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;
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 {
services.forEach(function(bus) {
table.appendChild(self.createBusRow(bus));
});
}
}
wrapper.appendChild(table);
return wrapper;
},
socketNotificationReceived: function(notification, payload) {
if (notification === "UPDATE") {
this.bus_stops[payload.BusStopID].Services = payload.Services;
}
this.updateDom();
}
});