This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MMM-NLDepartureTimes.js
143 lines (130 loc) · 4.53 KB
/
MMM-NLDepartureTimes.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
Module.register("MMM-NLDepartureTimes", {
statusDom: undefined,
timeTableList: Object,
error: undefined,
defaults: {
updateSpeed: 10,
maxVehics: 5
},
getScripts: function() {
return ['moment.js'];
},
start: function(){
Log.info(`Sarting module ${this.name}`);
this.statusDom = 'Loading';
this.resume();
},
resume: function() {
var self = this;
setInterval(function() {
self.getTable();},
this.config.updateSpeed * 60000); //minute = 60 * 1000 ms.
},
getTable: function() {
this.sendSocketNotification('REQ_TIMETABLE', this.config.tpc);
},
getStyles: function() {
return ["MMM-NLDepartureTimes.css"];
},
getDom: function(){
var self = this;
let wrapper = document.createElement("div");
if(this.config.tpc === undefined){
this.statusDom = 'error';
this.error = 'No TPC in config. See READM.MD for details.';
}
if(this.statusDom === 'Loading'){
this.sendSocketNotification('REQ_TIMETABLE', this.config.tpc);
if(this.statusDom === 'Loading'){
wrapper.innerHTML = "Loading...";
return wrapper;
}
}
if(this.statusDom === 'error'){
wrapper.innerHTML = this.error;
console.log(this.error);
return wrapper;
}
if(this.statusDom === 'newTable'){
const table = document.createElement("table");
table.id = "timeTable";
for(const stopArea in this.timeTableList){
//Fetch the Stoparea.
let row = document.createElement("tr");
let lineHeader = document.createElement("th");
lineHeader.innerHTML = stopArea;
lineHeader.className = "bold";
lineHeader.colSpan = 3;
row.appendChild(lineHeader);
table.appendChild(row);
//Fetch direction
for(const direction in this.timeTableList[stopArea]){
let row = document.createElement("tr");
let lineDirection = document.createElement("td");
lineDirection.innerHTML = direction;
lineDirection.colSpan = 3;
lineDirection.className = "small";
row.appendChild(lineDirection);
table.appendChild(row);
//fetch vehciles
let vehicCounter = 0;
for(const vehicle of this.timeTableList[stopArea][direction]){
console.log(`C: ${vehicCounter} V: ${vehicle.DepTime} ${vehicle.LineName} ${direction}`);
vehicCounter++;
//Create time + delay
let row = document.createElement("tr");
let vehicleTime = document.createElement("td");
if(config.timeFormat === 24){
vehicleTime.innerHTML = moment(vehicle.DepTime).format("HH:mm");
} else {
vehicleTime.innerHTML = moment(vehicle.DepTime).format("hh:mm A");
}
vehicleTime.className = "xsmall light vehicDepTime";
row.appendChild(vehicleTime);
//Create line number + destination
let vehicleLine = document.createElement("td");
vehicleLine.innerHTML = vehicle.LineName;
vehicleLine.className = "xsmall light vehicLine";
row.appendChild(vehicleLine);
let vehicleDestination = document.createElement("td");
vehicleDestination.innerHTML = vehicle.Destination;
vehicleDestination.className = "xsmall light vehicleDestination";
row.appendChild(vehicleDestination);
table.appendChild(row);
if(vehicCounter === this.config.maxVehics){
break;
}
}
}
}
wrapper.appendChild(table);
this.statusDom = 'Request'; //Not used in script. Nice for debugging.
return wrapper;
}
},
socketNotificationReceived: function(notification, payload) {
if(notification === 'TTIMETABLE'){
this.statusDom = 'newTable';
this.timeTableList = payload;
this.updateDom(2000);
//this.printTimeTable(this.timeTableList);
}
if(notification === 'ERROR'){
this.statusDom = 'error';
Log.error(payload);
this.error = payload;
this.updateDom(2000);
}
},
printTimeTable: function(timeTableList){
for(const stopArea in timeTableList){
console.log(`${stopArea}`);
for(const direction in timeTableList[stopArea]){
console.log(` ${direction}`);
for(const vehicle of timeTableList[stopArea][direction]){
console.log(new Date(vehicle.DepTime).toLocaleTimeString('nl-NL') + ' ( ' + vehicle.Delay + ' ) : ' + vehicle.LineName + ' - ' + vehicle.Destination);
}
}
}
},
});