forked from tbouron/MMM-FlightTracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMM-FlightTracker.js
More file actions
237 lines (219 loc) · 9.55 KB
/
MMM-FlightTracker.js
File metadata and controls
237 lines (219 loc) · 9.55 KB
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
232
233
234
235
236
237
Module.register('MMM-FlightTracker', {
// Default module config.
defaults: {
interval: 1,
animationSpeed: 1000,
passingByThreshold: -1,
latLng: [],
minAltitude: -1,
maxAltitude: -1,
minDistance: -1,
maxDistance: -1,
altitudeUnits: config.units,
speedUnits: config.units,
showAirline: true,
showType: true,
showSpeed: true,
showAltitude: true,
showHeading: true,
showRoute: true
},
aircrafts: [],
isConnected: null,
start: function() {
this.sendSocketNotification('START_TRACKING', this.config);
this.trackPlanes();
setInterval(() => {
this.trackPlanes();
}, this.config.interval * 1000);
},
getStyles: function () {
return [
'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, true));
}
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, false));
}
} else {
wrapper.appendChild(this.getSection(this.aircrafts, false));
}
return wrapper;
},
getSection(aircrafts, showDistance) {
const section = document.createElement('div');
section.append(...aircrafts.map(aircraft => {
const row = document.createElement('div');
row.className = 'aircraft';
const altitude = aircraft.altitude
? Math.floor(aircraft.altitude * (aircraft.unit === 0 && this.config.altitudeUnits === 'metric' ? 0.3040 : 1))
: null;
// Line 1: Callsign / Airline · Type (Route)
const aircraftHeading = document.createElement('div');
aircraftHeading.className = 'aircraft-heading medium';
aircraftHeading.innerHTML = `<span class="bright">${aircraft.callsign}</span>`;
if (this.config.showAirline && aircraft.airline) {
const airlineDisplay = aircraft.airline.substring(0, 25);
aircraftHeading.innerHTML += `<span class="dimmed airline"> / ${airlineDisplay}</span>`;
}
// Show short type code (e.g., A320, B738)
if (this.config.showType && aircraft.type) {
aircraftHeading.innerHTML += `<span class="dimmed type"> · ${aircraft.type}</span>`;
}
// Show route if available (e.g., "BWI-PIT")
if (this.config.showRoute && aircraft.route) {
aircraftHeading.innerHTML += `<span class="dimmed route"> (${aircraft.route})</span>`;
}
row.appendChild(aircraftHeading);
// Line 2: Speed, Altitude, Direction, Distance
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(`<span>${Math.floor(speed)} ${speedUnits}</span>`);
}
if (this.config.showAltitude && aircraft.altitude) {
let altitudeIcon;
if (aircraft.verticalRate < 0) {
altitudeIcon = '↓';
} else if (aircraft.verticalRate > 0) {
altitudeIcon = '↑';
} else {
altitudeIcon = '→';
}
metadata.push(`<span>${altitudeIcon} ${altitude} ${this.config.altitudeUnits === 'metric' ? 'm' : 'ft'}</span>`);
}
if (this.config.showHeading && aircraft.heading) {
metadata.push(`<span>${this.cardinalDirection(aircraft.heading)}</span>`);
}
if (showDistance && aircraft.distance) {
let distance;
let distanceUnits;
// Check if distance is already in nautical miles (tar1090) or meters (network mode)
if (aircraft.distanceUnit === 'nm') {
// Already in nautical miles from tar1090
if (this.config.altitudeUnits === 'metric') {
distance = aircraft.distance * 1.852; // nm to km
distanceUnits = 'km';
} else {
distance = aircraft.distance;
distanceUnits = 'nm';
}
} else {
// In meters from network mode
if (this.config.altitudeUnits === 'metric') {
distance = aircraft.distance / 1000; // meters to km
distanceUnits = 'km';
} else {
distance = aircraft.distance / 1852; // meters to nautical miles
distanceUnits = 'nm';
}
}
metadata.push(`<span>${distance.toFixed(1)} ${distanceUnits}</span>`);
}
if (metadata.length > 0) {
const aircraftMetadata = document.createElement('div');
aircraftMetadata.className = 'aircraft-metadata small dimmed';
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');
}
}
});