forked from vikramraja1995/MMM-SFMuniBusTimes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-SFMuniBusTimes.js
executable file
·133 lines (111 loc) · 4.68 KB
/
MMM-SFMuniBusTimes.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
Module.register("MMM-SFMuniBusTimes", {
// Define default config
defaults: {
stops: {
6994: ["J", "KT", "L", "M", "N"],
3328: ["33"],
},
updateInterval: 60000,
},
// Send start notification to node_helper
start: function() {
Log.info("Starting module: " + this.name);
var self = this;
this.getDepartureInfo();
// Schedule update timer.
setInterval(function() {
self.getDepartureInfo()
}, this.config.updateInterval);
// this.sendSocketNotification("START", this.config);
// Log.log(this.name + " has started!");
},
// Load required CSS files
getStyles: function() {
return ["MMM-SFMuniBusTimes.css"];
},
getDepartureInfo: function() {
Log.info("Requesting SF Muni departure times");
this.sendSocketNotification("GET_MUNI_TIMINGS", {
config: this.config
});
},
// Update DOM with appropriate view to display all Train / Bus data
getDom: function() {
const wrapper = document.createElement("div");
wrapper.className = "wrapper";
// If no route data has been fetched, display 'Loading...' message
if (this.schedule === undefined) {
wrapper.innerHTML = "Loading MUNI Times...";
wrapper.className = "dimmed light small";
return wrapper;
}
// Loop through each stop in schedule and populate the UI
for (let stop of this.schedule) {
const stopWrapper = document.createElement("div");
wrapper.appendChild(stopWrapper);
// Create a header element that displays the stop name
const stopName = document.createElement("header");
let headerHTML = stop.stopTitle;
if (stop.directionTitle !== undefined) {
headerHTML = headerHTML + ": " + stop.directionTitle;
}
stopName.innerHTML = headerHTML;
stopName.className = "stop";
stopWrapper.appendChild(stopName);
// Create a table that will hold all train / bus times
const table = document.createElement("table");
table.className = "small";
stopWrapper.appendChild(table);
// Loop through each route for the current stop
for (const routeObj of stop.routes) {
const {
routeTag
} = routeObj;
// Create a row that will hold the route and next 3 bus / train times
const row = document.createElement("tr");
table.appendChild(row);
// Create a route cell that will hold the route number / letter
const routeCell = document.createElement("td");
routeCell.innerHTML = routeTag;
routeCell.className = "route";
row.appendChild(routeCell);
let first = true;
// Loop through each train for the current route
if (routeObj.trains.length > 0) {
for (const train of routeObj.trains) {
const trainCell = document.createElement("td");
// Calculate the minutes remaining for the train / bus to arrive
const mins = Math.floor(train.seconds / 60);
// If first train, display the seconds as well
if (first) {
const secs = "" + (train.seconds % 60);
const secText = secs.length === 1 ? "0" + secs : secs;
trainCell.innerHTML = `${mins}m ${secText}s`;
first = false;
}
// If not, display only minutes
else {
trainCell.innerHTML = `${mins}m`;
}
trainCell.className = "train";
row.appendChild(trainCell);
}
} else {
const trainCell = document.createElement("td");
trainCell.innerHTML = "No predictions";
trainCell.className = "train";
row.appendChild(trainCell);
}
}
}
return wrapper;
},
// Handle messages from node_helper.js
socketNotificationReceived: function(notification, payload) {
if (notification === "MUNI_TIMINGS") {
// Update schedule property with route data received from node_helper and update the DOM
this.schedule = payload;
this.updateDom();
}
},
});