-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-TMB.js
executable file
·157 lines (130 loc) · 4.55 KB
/
MMM-TMB.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
/* global Module */
/* MagicMirror²
* Module: MMM-TMB
*
* By @jaumebosch
* MIT Licensed.
*/
Module.register("MMM-TMB", {
defaults: {
timeFormat: config.timeFormat,
maxEntries: 5,
refreshInterval: 60000,
retryDelay: 5000,
warningTime: 600,
blinkingTime: 300,
imminentTime: 60,
},
requiresVersion: "2.1.0", // Required version of MagicMirror
// Define required scripts.
getScripts: function() {
return ["moment.js"];
},
// Define requird styles
getStyles: function() {
return ["font-awesome.css", 'MMM-TMB.css'];
},
start: function() {
Log.info('Starting module: ' + this.name);
this.loaded = false;
this.sendSocketNotification('CONFIG', this.config);
},
getDom: function() {
let wrapper = document.createElement("div");
if (this.config.appId === "") {
wrapper.innerHTML = "Please set the correct <i>APP ID</i> in the config for module";
wrapper.className = "dimmed light small";
return wrapper;
}
if (this.config.appKey === "") {
wrapper.innerHTML = "Please set the correct <i>APP KEY</i> in the config for module";
wrapper.className = "dimmed light small";
return wrapper;
}
/* if (this.config.busStationCode === "") {
wrapper.innerHTML = "Please set the <i>Station Code</i> in the config for module";
wrapper.className = "dimmed light small";
return wrapper;
}*/
if (this.config.blinkingTime > this.config.warningTime) {
wrapper.innerHTML = "Please set the <i>blinkingTime</i> value greater or equal than <i>warningTime</i> value for module";
wrapper.className = "dimmed light small";
return wrapper;
}
if (!this.loaded) {
wrapper.innerHTML = this.translate('LOADING');
wrapper.className = "dimmed light small";
return wrapper;
}
let table = document.createElement("table");
table.className = "small";
let maxEntries = this.config.maxEntries;
if (this.iBus.length > 0){
for (let i = 0; i < this.iBus.length; ++i) {
if (this.iBus[i].dataLines.length > 0) {
for (let j = 0; j < this.iBus[i].dataLines.length; ++j) {
let row = document.createElement("tr");
table.appendChild(row);
let lineCell = document.createElement("td");
lineCell.innerHTML = this.iBus[i].dataLines[j].lineCode;
row.appendChild(lineCell);
let stopCell = document.createElement("td");
stopCell.className = "stopName stopCell";
stopCell.innerHTML = this.iBus[i].busStopName;
row.appendChild(stopCell);
let secs = this.iBus[i].dataLines[j].tInS;
let mins = this.iBus[i].dataLines[j].tInMin;
let timeCell = document.createElement("td");
timeCell.className = "timeCell";
timeCell.innerHTML = mins + " min";
switch (true) {
case (secs <= this.config.blinkingTime):
timeCell.className += " arriving blinking";
if (secs <= this.config.imminentTime) {
timeCell.innerHTML = "imminent"
}
break;
case (secs < this.config.warningTime):
timeCell.className += " arriving";
break;
}
row.appendChild(timeCell);
}
}else{
}
}
}else{
let row = document.createElement("tr");
table.appendChild(row);
let noBusCell = document.createElement("td");
noBusCell.className = "dimmed light small";
noBusCell.innerHTML = "No buses at this moment";
row.appendChild(noBusCell);
}
return table;
},
// ##################################################################################
// Override getHeader method
// ##################################################################################
getHeader: function() {
return "<i class='fa fa-fw fa-bus'></i> TMB iBus";
},
/* processResponse(data)
* Uses the received data to set the various values.
*
*/
processResponse: function(data) {
this.iBus = data;
this.loaded = true;
// this.updateDom(this.config.refreshInterval);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "STARTED") {
this.updateDom();
}else if (notification === "DATA") {
this.loaded = true;
this.processResponse(payload);
this.updateDom();
}
},
})