-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMM-AT-Bus.js
128 lines (113 loc) · 3.26 KB
/
MMM-AT-Bus.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
Module.register("MMM-AT-Bus", {
// Default module config.
defaults: {
// User Config
provider: "AT",
routeShortNames: ["NX1"],
stopCode: "7036",
forwardLimitHours: 2, // seconds. Ignore bus trips further in the future
refreshIntervalSeconds: 20, // seconds. Refresh rate
key: "key",
// Populated on first run
stopName: "",
stopId: "",
routeIdToShortName: {},
responseReceived: false
},
start: function () {
Log.info("Starting module: " + this.name);
console.log("Starting module: " + this.name);
var self = this;
let payload = this.config;
//Do this once first
self.sendSocketNotification("START", payload);
setInterval(function () {
self.sendSocketNotification("START", payload);
}, +this.config.refreshIntervalSeconds * 1000);
},
// Override dom generator.
getDom: function () {
Log.log("Updating MMM-AT-Bus DOM.");
var body = "";
if (this.text != null) {
body = this.text;
}
this.config.stopName = body.stopName;
var payloadEmpty = true;
var wrapper = document.createElement("table");
// Title and description
var tr = document.createElement("tr");
var th = document.createElement("th");
var text = "";
if (this.config.bus) {
text = this.config.stopName + " (" + this.config.bus + ")";
} else {
text = this.config.stopName;
}
var text = document.createTextNode(text);
th.appendChild(text);
tr.appendChild(th);
wrapper.appendChild(tr);
// Arrivals with minutes to arrival
if (body.inProgressTrips) {
if (body.inProgressTrips.length > 1) {
payloadEmpty = false;
}
for (let i = 0; i < body.inProgressTrips.length; i++) {
var bus_name = body.inProgressTrips[i].bus_name;
var minutes_to_arrival =
body.inProgressTrips[i].minutes_to_arrival;
var tr = document.createElement("tr");
var td = document.createElement("td");
var arriving_in_text =
minutes_to_arrival > 0
? "Ariving in: " + minutes_to_arrival + " minutes"
: "Ariving now";
var text = document.createTextNode(
bus_name + " | " + arriving_in_text
);
td.appendChild(text);
tr.appendChild(td);
wrapper.appendChild(tr);
}
}
// Arrivals with scheduled time
if (body.scheduledTrips) {
if (body.scheduledTrips.length > 1) {
payloadEmpty = false;
}
for (let i = 0; i < body.scheduledTrips.length; i++) {
var bus_name = body.scheduledTrips[i].bus_name;
var arrival_time = body.scheduledTrips[i].arrival_time;
var tr = document.createElement("tr");
var td = document.createElement("td");
var text = document.createTextNode(
bus_name + " | Scheduled arrival: " + arrival_time
);
td.appendChild(text);
tr.appendChild(td);
wrapper.appendChild(tr);
}
}
if (payloadEmpty) {
wrapper = document.createElement("div");
var text = this.config.responseReceived
? "No Upcoming Arrivals"
: "Finding Bus Arrival Times...";
wrapper.appendChild(
document
.createElement("p")
.appendChild(document.createTextNode(text))
);
}
return wrapper;
},
socketNotificationReceived: function (notification, payload) {
Log.log("MMM-AT-Bus socket received from Node Helper");
if (notification == "AT_GETREQUEST_RESULT") {
this.text = payload;
this.config.responseReceived = true;
this.updateDom();
}
}
});