-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-MTA.js
176 lines (142 loc) · 4.58 KB
/
MMM-MTA.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
/* global Module */
/* Magic Mirror
* Module: MMM-MTA
*
* By Stefan Krause http://yawns.de
* MIT Licensed.
*/
Module.register('MMM-MTA',{
defaults: {
mtaAPIKey: "",
sStation: "",
units: config.units,
animationSpeed: 1000,
refreshInterval: 1000 * 60, //refresh every minute
updateInterval: 1000 * 3600, //update every hour
timeFormat: config.timeFormat,
lang: config.language,
minimumDelay: 1,
initialLoadDelay: 0, // 0 seconds delay
retryDelay: 2500,
apiBase: "https://traintime.lirr.org/api/Departure",
stationTable: null
},
// Define required scripts.
getScripts: function() {
return ["moment.js"];
},
// Define requird styles
getStyles: function() {
return ["font-awesome.css", 'MMM-MTA.css'];
},
start: function() {
Log.info('Starting module: ' + this.name);
this.loadStationFile((response) => {
this.config.stationTable = JSON.parse(response);
});
this.loaded = false;
this.sendSocketNotification('CONFIG', this.config);
},
getDom: function() {
var wrapper = document.createElement("div");
if (this.config.mtaAPIKey === "") {
wrapper.innerHTML = "Please set the correct <i>API KEY</i> in the config for module: " + this.name + ".";
wrapper.className = "dimmed light small";
return wrapper;
}
if (this.config.sStation === "") {
wrapper.innerHTML = "Please set the <i>sStaion</i> in the config for module: " + this.name + ".";
wrapper.className = "dimmed light small";
return wrapper;
}
if (!this.loaded) {
wrapper.innerHTML = this.translate('LOADING');
wrapper.className = "dimmed light small";
return wrapper;
}
if (this.departures.length) {
for (var i in this.departures) {
var cD = this.departures[i];
var divAlert = document.createElement("div");
divAlert.className = "small thin light";
divAlert.innerHTML = "Train " + cD.lineLabel + " from "" + cD.sStation + "" to "" + cD.eStation + "" is running " + cD.delay + " " + ((cD.delay == 1) ? "minute" : "minutes" ) + " late";
wrapper.appendChild(divAlert);
}
}
return wrapper; },
// ##################################################################################
// Override getHeader method
// ##################################################################################
getHeader: function() {
return this.data.header;
},
/* processDepartures(data)
* Uses the received data to set the various values.
*
*/
processDepartures: function(data) {
if (this.config.stationTable == null) {
// file with station names is not yet loaded
this.config.loaded = false;
return;
}
if (!data.TRAINS) {
// Did not receive usable new data.
// Maybe this needs a better check?
return;
}
this.departures = [];
for (var i in data.TRAINS) {
var t = data.TRAINS[i];
var scheduledCalc = moment(t.SCHED, "MM-DD-YYYY HH:mm:ss");
var actualCalc = moment(t.ETA, "MM-DD-YYYY HH:mm:ss");
var delayMinutes = actualCalc.diff(scheduledCalc, 'minutes');
var scheduledAMPM = ( (scheduledCalc.hour() > 12 ) ? 'PM' : 'AM' );
var actualAMPM = ( (actualCalc.hour() > 12 ) ? 'PM' : 'AM' );
var direction = ( (actualAMPM == 'AM') ? 'W' : 'E' );
var sStation = ( (actualAMPM == 'AM') ? this.config.stationTable[this.config.sStation] : this.config.stationTable[t.DEST] );
var eStation = ( (actualAMPM == 'AM') ? this.config.stationTable[t.DEST] : this.config.stationTable[this.config.sStation] );
// add trains with defined delay only, all others are left out
if (delayMinutes >= this.config.minimumDelay && t.DIR == direction) {
this.departures.push({
time: t.SCHED,
eta: t.ETA,
delay: delayMinutes,
lineLabel: t.TRAIN_ID,
destination: t.DEST,
scheduledAMPM: scheduledAMPM,
actualAMPM: actualAMPM,
sStation: sStation,
eStation: eStation,
direction: t.DIR
});
}
}
this.loaded = true;
this.updateDom(this.config.animationSpeed);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "STARTED") {
this.updateDom();
}
else if (notification === "DATA") {
this.loaded = true;
this.processDepartures(JSON.parse(payload));
this.updateDom();
}
},
hours12: function(date) {
return (date.getHours() + 24) % 12 || 12;
},
loadStationFile: function(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open("GET", this.file("station_list.json"), true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
callback(xobj.responseText);
}
};
xobj.send(null);
},
});