-
Notifications
You must be signed in to change notification settings - Fork 10
/
MMM-FlightTracker.js
219 lines (199 loc) · 8.99 KB
/
MMM-FlightTracker.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
Module.register('MMM-FlightTracker', {
// Default module config.
defaults: {
interval: 1,
animationSpeed: 1000,
passingByThreshold: -1,
latLng: [],
altitudeUnits: config.units,
speedUnits: config.units,
showAirline: true,
showType: true,
showSpeed: true,
showAltitude: true,
showHeading: true
},
aircrafts: [],
isConnected: null,
start: function() {
this.sendSocketNotification('START_TRACKING', this.config);
this.trackPlanes();
setInterval(() => {
this.trackPlanes();
}, this.config.interval * 1000);
},
getStyles: function () {
return [
'font-awesome.css',
'MMM-FlightTracker.css'
];
},
socketNotificationReceived: function (id, payload) {
if (id === 'SET_IS_CONNECTED') {
this.isConnected = payload;
this.updateDom(this.config.animationSpeed);
}
if (id === 'SET_AIRCRAFTS') {
const animate = this.aircrafts.length !== payload.length;
const isUpdated = JSON.stringify(this.aircrafts) !== JSON.stringify(payload);
if (isUpdated) {
this.aircrafts = payload;
this.updateDom(animate ? this.config.animationSpeed : undefined);
}
}
},
trackPlanes: function() {
if (this.isConnected === null) {
Log.log('Node helper not connected yet to the ADS-B client. Waiting...');
this.sendSocketNotification('GET_IS_CONNECTED');
} else {
this.sendSocketNotification('GET_AIRCRAFTS', this.config);
}
},
getDom: function() {
const wrapper = document.createElement('div');
wrapper.className = 'flight-tracker';
if (this.isConnected === null) {
wrapper.className = 'light small dimmed';
wrapper.innerHTML = this.translate('Connecting tracker');
return wrapper;
}
if (this.isConnected === false) {
wrapper.className = 'light small dimmed';
wrapper.innerHTML = this.translate('Failed to start. Please check the logs');
return wrapper;
}
if (this.aircrafts.length === 0) {
wrapper.className = 'light small dimmed';
wrapper.innerHTML = this.translate('No planes nearby');
return wrapper;
}
if (this.config.passingByThreshold > 0) {
const windowPlanes = this.aircrafts.filter(aircraft => aircraft.altitude * (aircraft.unit === 0 && this.config.altitudeUnits === 'metric' ? 0.3040 : 1) <= this.config.passingByThreshold);
if (windowPlanes.length > 0) {
wrapper.appendChild(this.getSection(windowPlanes, this.translate('At the window')));
}
const passingByPlanes = this.aircrafts.filter(aircraft => aircraft.altitude * (aircraft.unit === 0 && this.config.altitudeUnits === 'metric' ? 0.3040 : 1) > this.config.passingByThreshold);
if (passingByPlanes.length > 0) {
wrapper.appendChild(this.getSection(passingByPlanes, this.translate('Passing by')));
}
} else {
wrapper.appendChild(this.getSection(this.aircrafts));
}
return wrapper;
},
getSection(aircrafts, label) {
const section = document.createElement('div');
if (label) {
section.innerHTML = `<p class="light small dimmed label">${label}</p>`;
}
section.append(...aircrafts.map(aircraft => {
const row = document.createElement('div');
row.className = 'aircraft';
const aircraftHeading = document.createElement('div');
aircraftHeading.className = 'aircraft-heading medium';
aircraftHeading.innerHTML = `<span class="bright">${aircraft.callsign}</span>`;
if (this.config.showAirline && aircraft.airline) {
aircraftHeading.innerHTML += ` <span class="small dimmed airline">/${aircraft.airline}</span>`
}
row.appendChild(aircraftHeading);
const altitude = aircraft.altitude
? Math.floor(aircraft.altitude * (aircraft.unit === 0 && this.config.altitudeUnits === 'metric' ? 0.3040 : 1))
: null;
const subHeading = [];
if (this.config.showType && aircraft.type) {
subHeading.push(`<span>${aircraft.type}</span>`);
}
if (this.config.latLng && altitude < this.config.passingByThreshold && aircraft.distance) {
const distance = aircraft.distance * (this.config.altitudeUnits === 'metric' ? 1 : 3.28084);
subHeading.push(`<span><i class="fas fa-location-arrow dimmed"></i>${Math.floor(distance)}<sup>${this.config.altitudeUnits === 'metric' ? 'm' : 'ft'}</sup></span>`);
if (aircraft.direction) {
subHeading.push(`<span>${this.cardinalDirection(aircraft.direction)}</span>`);
}
}
if (subHeading.length > 0) {
const aircraftSubHeading = document.createElement('div');
aircraftSubHeading.className = 'aircraft-subheading xsmall dimmed';
aircraftSubHeading.innerHTML = subHeading.join('');
row.appendChild(aircraftSubHeading);
}
const metadata = [];
if (this.config.showSpeed && aircraft.speed) {
let speed;
let speedUnits;
switch (this.config.speedUnits) {
case 'metric':
speed = aircraft.speed * 1.8520008892119;
speedUnits = 'km/h';
break;
case 'imperial':
speed = aircraft.speed * 1.15078;
speedUnits = 'mph';
break;
case 'knots':
default:
speed = aircraft.speed;
speedUnits = this.translate('knots');
}
metadata.push(`<small><i class="fas fa-wind dimmed"></i>${Math.floor(speed)}<sup>${speedUnits}</sup></small>`);
}
if (this.config.showAltitude && aircraft.altitude) {
let altitudeIconId;
if (aircraft.verticalRate < 0) {
altitudeIconId = 'fa-angle-double-down';
} else if (aircraft.verticalRate > 0) {
altitudeIconId = 'fa-angle-double-up';
} else {
altitudeIconId = 'fa-arrows-alt-h';
}
metadata.push(`<small><i class="fas ${altitudeIconId} dimmed"></i>${altitude}<sup>${this.config.altitudeUnits === 'metric' ? 'm' : 'ft'}</sup></small>`);
}
if (this.config.showHeading && aircraft.heading) {
metadata.push(`<small><i class="far fa-compass dimmed"></i>${Math.floor(aircraft.heading)}<sup>○</sup></small>`);
}
if (metadata.length > 0) {
const aircraftMetadata = document.createElement('div');
aircraftMetadata.className = 'aircraft-metadata medium normal';
aircraftMetadata.innerHTML = metadata.join('');
row.appendChild(aircraftMetadata);
}
return row;
}));
return section;
},
cardinalDirection(direction) {
if (direction> 11.25 && direction<= 33.75){
return this.translate('NNE');
} else if (direction> 33.75 && direction<= 56.25) {
return this.translate('NE');
} else if (direction> 56.25 && direction<= 78.75) {
return this.translate('ENE');
} else if (direction> 78.75 && direction<= 101.25) {
return this.translate('E');
} else if (direction> 101.25 && direction<= 123.75) {
return this.translate('ESE');
} else if (direction> 123.75 && direction<= 146.25) {
return this.translate('SE');
} else if (direction> 146.25 && direction<= 168.75) {
return this.translate('SSE');
} else if (direction> 168.75 && direction<= 191.25) {
return this.translate('S');
} else if (direction> 191.25 && direction<= 213.75) {
return this.translate('SSW');
} else if (direction> 213.75 && direction<= 236.25) {
return this.translate('SW');
} else if (direction> 236.25 && direction<= 258.75) {
return this.translate('WSW');
} else if (direction> 258.75 && direction<= 281.25) {
return this.translate('W');
} else if (direction> 281.25 && direction<= 303.75) {
return this.translate('WNW');
} else if (direction> 303.75 && direction<= 326.25) {
return this.translate('NW');
} else if (direction> 326.25 && direction<= 348.75) {
return this.translate('NNW');
} else {
return this.translate('N');
}
}
});