-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
549 lines (496 loc) · 16.1 KB
/
server.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
const express = require('express');
const favicon = require('serve-favicon');
const cors = require('cors');
const url = require('url');
const sqlite3 = require("sqlite3");
const Math = require("math");
const fs = require("fs");
const WebSocket = require('ws');
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const { XMLParser } = require('fast-xml-parser');
const { unzip, unzipSync } = require('zlib');
const { ping, probe } = require('tcp-ping-sync');
const { execSync, exec } = require('child_process');
/**
* These objects are used by the XMLParser to convert XML to JSON.
* The alwaysArray object makes the parser translate sky_condition
* XML attributes as an array of values... which is good.
*/
const alwaysArray = [
"response.data.METAR.sky_condition",
"response.data.PIREP.sky_condition"
];
const xmlParseOptions = {
ignoreAttributes : false,
attributeNamePrefix : "",
allowBooleanAttributes: true,
ignoreDeclaration: true,
isArray: (name, jpath, isLeafNode, isAttribute) => {
if( alwaysArray.indexOf(jpath) !== -1) return true;
}
};
/**
* now the actual parser object is instantiated with the above options
*/
const xmlparser = new XMLParser(xmlParseOptions);
/**
* Global variables
*/
let airports = {};
let MessageTypes = {};
let wss;
let connections = new Map();
let DB_PATH = `${__dirname}/charts`;
/**
* Get the global settings JSON object
*/
let rawdata = fs.readFileSync(`${__dirname}/settings.json`);
let settings = JSON.parse(rawdata);
/******************************************************
if running in a docker container, check to see if an
external volume for the database folder exists, if so,
use it
*******************************************************/
function isRunningInDocker() {
let dkresults = `${__dirname}/isdocker.txt`;
let dcmd = `cat /proc/1/cgroup > ${dkresults}`;
execSync(dcmd);
let txtresult = fs.readFileSync(dkresults, { encoding: 'utf8', flag: 'r' });
let isdocker = (txtresult.toString().search("/docker/") > -1)
fs.rmSync(dkresults);
if (isdocker) {
console.log("Running in docker!");
}
return isdocker;
}
if (isRunningInDocker()) {
if (fs.existsSync(`${__dirname}/externalcharts`)) {
DB_PATH = `${__dirname}/externalcharts`;
}
}
else {
if (settings.externalcharts.length > 0) {
if (fs.existsSync(settings.externalcharts)) {
DB_PATH = settings.externalcharts;
}
}
}
let histdb;
const databaselist = new Map();
const databases = new Map();
const metadatasets = new Map();
/*
* Load airports.json for immediate sending to client later upon winsock connection
*/
(() => {
// check for internet access to see if OSM online maps can be used
if (!settings.useOSMonlinemap) {
settings.useOSMonlinemap = probe('google.com');
fs.writeFileSync(`${__dirname}/settings.json`, JSON.stringify(settings,null," "));
}
MessageTypes = settings.messagetypes;
try {
let dbfiles = fs.readdirSync(DB_PATH);
dbfiles.forEach((dbname) => {
if (dbname.endsWith(".db") || dbname.endsWith(".mbtiles")) {
var key = dbname.toLowerCase().split(".")[0];
var dbfile = `${DB_PATH}/${dbname}`;
databaselist.set(key, dbfile);
}
});
}
catch(err) {
console.log("NO CHART DATABASES FOUND!!");
}
rawdata = fs.readFileSync(`${__dirname}/airports.json`);
airports = JSON.parse(rawdata);
wss = new WebSocket.Server({ port: settings.wsport });
try {
wss.on('connection', (ws) => {
const id = Date.now();
connections.set(ws, id);
console.log(`Websocket connected, id: ${id}`);
setTimeout(() => {
let msg = {
type: "airports",
payload: JSON.stringify(airports)
};
ws.send(JSON.stringify(msg));
runDownloads();
}, 200);
ws.on('close', function() {
connections.delete(ws);
console.log("connection closed");
});
ws.on('message', (data) => { });
});
}
catch (err) {
console.log(err);
}
})();
loadDatabases();
loadMetadatasets();
function loadDatabases() {
try {
databaselist.forEach((dbfile, key) => {
var db = new sqlite3.Database(dbfile, sqlite3.OPEN_READONLY, (err) => {
if (err) {
console.log(`Failed to load: ${key}: ${err}`);
throw err;
}
});
databases.set(key, db);
});
}
catch(err) {
console.log(err.message);
}
histdb = new sqlite3.Database(`${__dirname}/${settings.historyDb}`, sqlite3.OPEN_READWRITE, (err) => {
if (err){
console.log(`Failed to load: ${settings.historyDb}: ${err}`);
}
});
}
/**
* Start the express web server
*/
let app = express();
try {
app.use(express.urlencoded({ extended: true }));
app.use(express.json({}));
app.use(cors());
app.use(favicon(`${__dirname }/images/favicon.png`));
app.use(express.static('public'))
console.log(`Server listening on port ${settings.httpport}`);
app.listen(settings.httpport, '0.0.0.0');
let appOptions = {
dotfiles: 'ignore',
etag: false,
extensions: ['html'],
index: false,
redirect: false,
setHeaders: function (res, path, stat) {
res.set('x-timestamp', Date.now());
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader('Access-Control-Allow-Methods', '*');
res.setHeader("Access-Control-Allow-Headers", "*");
}
};
app.use(express.static(`${__dirname}/public`, appOptions));
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/public/index.html`);
});
app.get("/getsettings", (req, res) => {
let rawdata = fs.readFileSync(`${__dirname}/settings.json`);
res.writeHead(200);
res.write(rawdata);
res.end();
});
app.get("/databaselist", (req, res) => {
let obj = [];
databaselist.forEach((value, key) => {
obj.push(key);
});
let rawdata = JSON.stringify(obj);
res.writeHead(200);
res.write(rawdata);
res.end();
});
app.get("/metadatasets", (req, res) => {
let dbs = [];
console.log("metadatasets count = ", metadatasets.size);
metadatasets.forEach((item, key) => {
let lineitem = {};
lineitem["key"] = key;
lineitem["value"] = item;
dbs.push(lineitem);
});
let rawdata = JSON.stringify(dbs);
res.writeHead(200);
res.write(rawdata);
res.end();
});
app.get("/tiles/*", (req, res) => {
let parts = req.url.split("/");
let db = databases.get(parts[2]);
handleTile(req, res, db);
});
app.get("/gethistory", (req,res) => {
getPositionHistory(res);
});
app.post("/savehistory", (req, res) => {
savePositionHistory(req.body);
res.writeHead(200);
res.end();
});
}
catch (err) {
console.log(err);
}
/**
* Get the last recorded ownship position from the position history database
* @param {response} http response
*/
function getPositionHistory(response) {
let sql = "SELECT * FROM position_history WHERE id IN ( SELECT max( id ) FROM position_history )";
histdb.get(sql, (err, row) => {
if (!err) {
if (row != undefined) {
let obj = {};
obj["longitude"] = row.longitude;
obj["latitude"] = row.latitude;
obj["heading"] = row.heading;
response.writeHead(200);
response.write(JSON.stringify(obj));
response.end();
}
}
else
{
console.log(err);
response.writeHead(500);
response.end();
}
});
}
/**
* Update the position history database with current position data
* @param {json object} data, contains date, longitude, latitude, heading, and altitude
*/
function savePositionHistory(data) {
let datetime = new Date().toISOString();
let position = `'${datetime}', ${data.longitude}, ${data.latitude}, ${data.heading}, ${data.altitude}`;
let sql = `INSERT INTO position_history (datetime, longitude, latitude, heading, gpsaltitude) ` +
`VALUES (${position})`;
histdb.run(sql, function(err) {
if (err) {
console.log(err);
}
else {
console.log(`position: ${position}`);
}
});
}
/**
* Parse the z,x,y integers, validate, and pass along to loadTile
* @param {request} http request
* @param {response} http response
* @param {db} database
* @returns the results of calling loadTile
*/
function handleTile(request, response, db) {
let x = 0;
let y = 0;
let z = 0;
let idx = -1;
let parts = request.url.split("/");
if (parts.length < 5) {
return
}
try {
idx = parts.length - 1;
let yparts = parts[idx].split(".");
y = parseInt(yparts[0])
}
catch(err) {
res.writeHead(500, "Failed to parse y");
response.end();
return;
}
idx--
x = parseInt(parts[idx]);
idx--
z = parseInt(parts[idx]);
idx--
loadTile(z, x, y, response, db);
}
/**
* Get all tiles from the passed database that match the supplied
* z,x,y indices and then send them back to the requesting client
* @param {integer} z
* @param {integer} x
* @param {integer} y
* @param {http response} http response object
* @param {database} sqlite database
*/
function loadTile(z, x, y, response, db) {
let sql = `SELECT tile_data FROM tiles WHERE zoom_level=${z} AND tile_column=${x} AND tile_row=${y}`;
db.get(sql, (err, row) => {
if (!err) {
if (row == undefined) {
response.writeHead(200);
response.end();
}
else {
if (row.tile_data != undefined) {
let png = row.tile_data;
response.writeHead(200);
response.write(png);
response.end();
}
}
}
else {
console.log(err);
response.writeHead(500, err.message);
response.end();
}
});
}
/**
* Get Map object filled with metadata sets for all mbtiles databases
*/
function loadMetadatasets() {
let sql = `SELECT name, value FROM metadata UNION SELECT 'minzoom', min(zoom_level) FROM tiles ` +
`WHERE NOT EXISTS (SELECT * FROM metadata WHERE name='minzoom') UNION SELECT 'maxzoom', max(zoom_level) FROM tiles ` +
`WHERE NOT EXISTS (SELECT * FROM metadata WHERE name='maxzoom')`;
databases.forEach((db, key) => {
let item = {};
item["bounds"] = "";
item["attribution"] = "";
let found = false;
db.all(sql, [], (err, rows) => {
if (!err) {
rows.forEach((row) => {
if (row.value != null) {
item[row.name] = row.value;
}
if (row.name === "maxzoom" && row.value != null) { // && !found) {
let maxZoomInt = parseInt(row.value);
sql = `SELECT min(tile_column) as xmin, min(tile_row) as ymin, ` +
`max(tile_column) as xmax, max(tile_row) as ymax ` +
`FROM tiles WHERE zoom_level=?`;
db.get(sql, [maxZoomInt], (err, row) => {
let xmin = row.xmin;
let ymin = row.ymin;
let xmax = row.xmax;
let ymax = row.ymax;
llmin = tileToDegree(maxZoomInt, xmin, ymin);
llmax = tileToDegree(maxZoomInt, xmax+1, ymax+1);
retarray = `${llmin[0]}, ${llmin[1]}, ${llmax[0]}, ${llmax[1]}`;
item["bounds"] = retarray;
found = true;
});
}
});
metadatasets.set(key, item);
}
else {
console.log(err);
}
});
});
}
/**
* Get the longitude and latitude for a given pixel position on the map
* @param {integer} z - the zoom level
* @param {integer} x - the horizontal index
* @param {integer} y - the vertical index
* @returns 2 element array - [longitude, latitude]
*/
function tileToDegree(z, x, y) {
y = (1 << z) - y - 1
let n = Math.PI - 2.0*Math.PI*y/Math.pow(2, z);
lat = 180.0 / Math.PI * Math.atan(0.5*(Math.exp(n)-Math.exp(-n)));
lon = x/Math.pow(2, z)*360.0 - 180.0;
return [lon, lat]
}
/**
* Recursively run the file downloads from the ADDS server for
* metars, tafs, & pireps which will then be sent to client(s)
*/
async function runDownloads() {
downloadXmlFile(MessageTypes.metars);
downloadXmlFile(MessageTypes.tafs);
downloadXmlFile(MessageTypes.pireps);
setTimeout(() => {
runDownloads();
}, settings.wxupdateintervalmsec);
}
/**
* Download an ADDS weather service file
* @param {source} the type of file to download (metar, taf, or pirep)
*/
async function downloadXmlFile(source) {
let xhr = new XMLHttpRequest();
let url = settings.addscurrentxmlurl.replace(source.token, source.type);
xhr.open('GET', url, true);
xhr.setRequestHeader('Content-Type', 'text/csv');
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
xhr.setRequestHeader('Access-Control-Allow-Methods', '*');
xhr.setRequestHeader("Access-Control-Allow-Headers", "*");
xhr.responseType = 'document';
xhr.onload = () => {
if (xhr.readyState == 4 && xhr.status == 200) {
let response = xhr.responseText;
//var mxml = unzipSync(new Buffer.From(response).toString('base64'));
let messageJSON = xmlparser.parse(response);
switch(source.type) {
case "tafs":
processTafJsonObjects(messageJSON);
break;
case "metars":
processMetarJsonObjects(messageJSON);
break;
case "aircraftreports":
processPirepJsonObjects(messageJSON);
break;
}
}
};
try {
xhr.send();
}
catch (err) {
console.log(`Error getting message type ${xmlmessage.type}: ${err}`);
}
}
/**
* Process the received downloaded tafs data and send to client(s)
* @param {object} tafs json object
*/
async function processTafJsonObjects(tafs) {
let payload = JSON.stringify(tafs);
let message = {
type: MessageTypes.tafs.type,
payload: payload
};
const json = JSON.stringify(message);
sendMessageToClients(json);
}
/**
* Process the received downloaded metars data and send to client(s)
* @param {object} metars json object
*/
async function processMetarJsonObjects(metars) {
let payload = JSON.stringify(metars);
let message = {
type: MessageTypes.metars.type,
payload: payload
};
const json = JSON.stringify(message);
sendMessageToClients(json);
}
/**
* Process the received downloaded pireps data and send to client(s)
* @param {object} pireps json object
*/
async function processPirepJsonObjects(pireps) {
let payload = JSON.stringify(pireps);
let message = {
type: MessageTypes.pireps.type,
payload: payload
}
const json = JSON.stringify(message);
sendMessageToClients(json);
}
/**
* Iterate through any/all connected clients and send data
* @param {string} stringified json message
*/
async function sendMessageToClients(jsonmessage) {
[...connections.keys()].forEach((client) => {
client.send(jsonmessage);
});
}