-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-MTA-NextBus.js
executable file
·160 lines (116 loc) · 3.63 KB
/
MMM-MTA-NextBus.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
/* global Module */
/* Magic Mirror
* Module: MMM-MTA-NextBus
*
* By
* MIT Licensed.
*/
Module.register("MMM-MTA-NextBus", {
defaults: {
timeFormat: config.timeFormat,
maxEntries: 5,
updateInterval: 60000,
retryDelay: 5000
},
requiresVersion: "2.1.0", // Required version of MagicMirror
start: function() {
var self = this;
var dataRequest = null;
var dataNotification = null;
//Flag for check if module is loaded
//this.loaded = false;
console.log(this.config.timeFormat);
this.sendSocketNotification("CONFIG", this.config);
// Schedule update timer.
setInterval(function() {
self.sendSocketNotification("GET_DATA");
}, this.config.updateInterval);
},
getDom: function() {
var self = this;
// create element wrapper for show into the module
var wrapper = document.createElement("div");
// If this.dataRequest is not empty
if (this.dataRequest) {
this.dataRequest.forEach(function(data, i) {
var wrapperDataRequest = document.createElement("div");
wrapperDataRequest.innerHTML = data;
wrapperDataRequest.className = "small";
wrapper.appendChild(wrapperDataRequest);
});
}
return wrapper;
},
getScripts: function() {
return ["moment.js"];
},
getStyles: function () {
return [
"MMM-MTA-NextBus.css",
];
},
processData: function(data) {
var self = this;
this.dataRequest = self.processActionNextBus(data);
self.updateDom(self.config.animationSpeed);
//if (this.loaded === false) { ; }
//this.loaded = true;
// the data if load
// send notification to helper
//this.sendSocketNotification("MMM-MTA-NextBus-NOTIFICATION_TEST", data);
},
processActionNextBus: function(response) {
var result = [];
var serviceDelivery = response.Siri.ServiceDelivery;
var updateTimestampReference = new Date(serviceDelivery.ResponseTimestamp);
//console.log(updateTimestampReference);
// array of buses
var visits = serviceDelivery.StopMonitoringDelivery[0].MonitoredStopVisit;
var visitsCount = Math.min(visits.length, this.config.maxEntries);
for (var i = 0; i < visitsCount; i++) {
r = '';
var journey = visits[i].MonitoredVehicleJourney;
var line = journey.PublishedLineName[0];
var destinationName = journey.DestinationName[0];
if (destinationName.startsWith('LIMITED')) {
line += ' LIMITED';
}
r += line + ', ';
var monitoredCall = journey.MonitoredCall;
// var expectedArrivalTime = new Date(monitoredCall.ExpectedArrivalTime);
if (monitoredCall.ExpectedArrivalTime) {
var mins = this.getArrivalEstimateForDateString(monitoredCall.ExpectedArrivalTime, updateTimestampReference);
r += mins + ', ';
}
var distance = monitoredCall.ArrivalProximityText;
r += distance;
result.push(r);
}
result.push('Last Updated: ' + this.formatTimeString(updateTimestampReference));
return result;
},
getArrivalEstimateForDateString: function(dateString, refDate) {
var d = new Date(dateString);
var mins = Math.floor((d - refDate) / 60 / 1000);
return mins + ' minute' + ((Math.abs(mins) === 1) ? '' : 's');
},
formatTimeString: function(date) {
var m = moment(date);
var hourSymbol = "HH";
var periodSymbol = "";
if (this.config.timeFormat !== 24) {
hourSymbol = "h";
periodSymbol = " A";
}
var format = hourSymbol + ":mm" + periodSymbol;
return m.format(format);
},
// socketNotificationReceived from helper
socketNotificationReceived: function (notification, payload) {
if (notification === "DATA") {
this.processData(payload);
} else if (notification === "ERROR") {
self.updateDom(self.config.animationSpeed);
}
},
});