-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-SeawayLockTraffic.js
231 lines (209 loc) · 7.89 KB
/
MMM-SeawayLockTraffic.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* Magic Mirror
* Module: MMM-SeawayLockTraffic
*
* By ProfKP
*
*/
Module.register("MMM-SeawayLockTraffic", {
// Module config defaults.
defaults: {
lock: "2305",
useHeader: false, // false if you don't want a header
header: "Traffic", // Any text you want lock name will be prepended
maxWidth: "300px",
rotateInterval: 15 * 60 * 1000,
animationSpeed: 3000, // fade in and out speed
initialLoadDelay: 3000,
retryDelay: 2500,
updateInterval: 60 * 15 * 1000,
maxRows: 10 //maximum number of ships to display
},
getScripts: function() {
return ["moment.js", "moment-timezone.js"];
},
getStyles: function() {
return ["MMM-SeawayLockTraffic.css"];
},
start: function() {
Log.info("Starting module: " + this.name);
requiresVersion: "2.1.0",
// Set locale.
this.url = this.config.lock;
this.Traffic = [];
this.activeItem = 0; // <-- starts rotation at item 0 (see Rotation below)
this.rotateInterval = null; // <-- sets rotation time (see below)
this.scheduleUpdate(); // <-- When the module updates (see below)
},
getDom: function() {
// creating the wrapper
var wrapper = document.createElement("div");
wrapper.className = "wrapper";
wrapper.style.maxWidth = this.config.maxWidth;
// The loading sequence
if (!this.loaded) {
wrapper.innerHTML = "Loading Ship Traffic";
wrapper.classList.add("small");
return wrapper;
}
//trying tables
if (this.config.useHeader != false) {
var header = document.createElement("header");
header.classList.add("small", "bright");
var lockName = "Test";
switch (this.config.lock) {
case "2305":
lockName = "ST. LAMBERT LOCK";
break;
case "2950":
lockName = "COTE STE. CATHERINE LOCK";
break;
case "3730":
lockName = "LOCK 3 BEAUHARNOIS";
break;
case "3775":
lockName = "LOCK 4 BEAUHARNOIS";
break;
case "4690":
lockName = "SNELL LOCK";
break;
case "4765":
lockName = "EISENHOWER LOCK";
break;
case "5260":
lockName = "IROQUOIS LOCK";
break;
case "7315":
lockName = "WELLAND LOCK 1";
break;
case "7480":
lockName = "WELLAND LOCK 2";
break;
case "7690":
lockName = "WELLAND LOCK 3";
break;
case "7825":
lockName = "WELLAND LOCK 4 EAST";
break;
case "7840":
lockName = "WELLAND LOCK 4 WEST";
break;
case "7855":
lockName = "WELLAND LOCK 5 EAST";
break;
case "7870":
lockName = "WELLAND LOCK 5 WEST";
break;
case "7885":
lockName = "WELLAND LOCK 6 EAST";
break;
case "7900":
lockName = "WELLAND LOCK 6 WEST";
break;
case "8005":
lockName = "WELLAND LOCK 7";
break;
case "9040":
lockName = "WELLAND LOCK 8";
break;
default:
lockName = "Lock not listed";
}
header.innerHTML = lockName + " " + this.config.header;
wrapper.appendChild(header);
}
var myships = this.Traffic;
// Create the table
// creates a <table> element and a <tbody> element
var tbl = document.createElement("table");
var tblBody = document.createElement("tbody");
tblBody.classList.add("xsmall");
// creater the header row of the table
var row = document.createElement("tr");
var cell = document.createElement("td");
var cellText = document.createTextNode("Vessel Name");
cell.appendChild(cellText);
row.appendChild(cell);
var cell = document.createElement("td");
var cellText = document.createTextNode(" ETA ");
cell.appendChild(cellText);
row.appendChild(cell);
var cell = document.createElement("td");
var cellText = document.createTextNode("Direction");
cell.appendChild(cellText);
row.appendChild(cell);
// add the row to the end of the table body
tblBody.appendChild(row);
// creating cells
// Create a <td> element and a text node, make the text
// node the contents of the <td>, and put the <td> at
// the end of the table row
var i = 0;
var j = 0;
while ((j < this.config.maxRows) && (i < myships.length)) {
if ((moment(myships[i].ETA, "YYYY-MM-DD HH:mm")) > moment()) {
// creates a table row
var row = document.createElement("tr");
var cell = document.createElement("td");
var cellText = document.createTextNode(myships[i].Vessel_Name);
cell.appendChild(cellText);
row.appendChild(cell);
var cell = document.createElement("td");
var cellText = document.createTextNode(myships[i].ETA);
cell.appendChild(cellText);
row.appendChild(cell);
var cell = document.createElement("td");
var cellText = document.createTextNode(myships[i].Direction);
cell.appendChild(cellText);
row.appendChild(cell);
// add the row to the end of the table body
tblBody.appendChild(row);
j++;
}
i++;
};
// put the <tbody> in the <table>
tbl.appendChild(tblBody);
// appends <table> into <body>
wrapper.appendChild(tbl);
// sets the border attribute of tbl to 2;
//tbl.setAttribute("border", "2");
return wrapper;
}, // <-- closes the getDom function from above
// this processes your data
processTraffic: function(data) {
this.Traffic = data;
//console.log("In MMM-SeawayLockTraffic " + this.Traffic); // uncomment to see if you're getting data (in dev console)
this.loaded = true;
},
// this rotates your data
scheduleCarousel: function() {
// console.log("Carousel of SeawayLockTraffic!"); // uncomment to see if data is rotating (in dev console)
this.rotateInterval = setInterval(() => {
this.activeItem++;
this.updateDom(this.config.animationSpeed);
}, this.config.rotateInterval);
},
// this tells module when to update
scheduleUpdate: function() {
setInterval(() => {
this.getTraffic();
}, this.config.updateInterval);
this.getTraffic(this.config.initialLoadDelay);
var self = this;
},
// this asks node_helper for data
getTraffic: function() {
this.sendSocketNotification('GET_Traffic', this.url);
},
// this gets data from node_helper
socketNotificationReceived: function(notification, payload) {
if (notification === "Traffic_RESULT") {
this.processTraffic(payload);
if (this.rotateInterval == null) {
this.scheduleCarousel();
}
this.updateDom(this.config.animationSpeed);
}
this.updateDom(this.config.initialLoadDelay);
}
});