-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
110 lines (95 loc) · 3.3 KB
/
main.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
"use strict";
//
// (c) 2021 Martin Burri
// MIT License - no warranties whatsoever...
// https://github.com/bm98/FSimPngTiles
//
// V 0.9
// Server side Node.JS Code for the map display
// --> In general there is nothing to change in this file for standard usage
//
// This implements a WebServer able to:
// - serve a map from OSM
// - serve a map from local tiles
// - accept a PUT request for an icon to show at lat lon on the map
// - accept a PUT request to center at lat lon on the map
//
// Setup:
// Install Node.JS (https://nodejs.org/en/download/)
// Issue in e.g. powershell (Windows)
// $ npm install
//
// Adjust the server ports below (var port = NNNNN)
//
// Run:
// Issue in e.g. powershell (Windows)
// $ npm run start
// .. to run the server
//
// The listening port of the HTTP Server
const port = 8080;
// web folder structure to be known by the methods below
// The main folder where the public HTML content is located
const publicHtml = 'www';
// Adjust from here but beware..
// Import express und http Module.
const express = require('express');
const path = require('path');
var app = express();
var server = require('http').createServer(app);
// define web root
const webRoot = path.join( __dirname, publicHtml ); // current folder + our public one
// tracking vars
var lat = -6.0;
var lon = 146.0;
var alt = 8000.0;
var hdg = 0.0;
var gs = 120;
var vs = 0;
// Start the simple Webserver
server.listen(port, function () {
// Greeting for service startup
console.log('Docker image: bm98ch/fsimpngtiles');
console.log('Simple Map Server running, listens at port %d', port);
});
// Setup the 'public' WebSite Files path
app.use(express.static(webRoot));
// Return the requested coords for the map script
/*
Route path: /location
Request URL: http://localhost:PORT/api/location
*/
app.get('/api/location' , function (req, res) {
//console.log('DEB: location Called! '); // DEBUG
const retObj = '{"pos_lon":' +lon+ ',"pos_lat":' +lat+ ',"alt_msl_ft":' +alt+ ',"hdg_true_deg":' +hdg+ ',"gs_kt":'+gs+ ',"vs_fpm":' +vs+ '}';
res.send(retObj); // Reply with records
});
// Require a location to track
/*
Route path: /api/track/:ACFT_DATA (lat, lon, alt_msl, true heading, groundspeed, vertical rate)
Get URL: http://localhost:PORT/api/track/12.3,-12.5,2300,256,120,-200
*/
app.get('/api/track/:ACFT_DATA', function (req, rep) {
//console.log('DEB: GET track/__ Called! ' + req.params.ACFT_DATA); // DEBUG
const para = req.params.ACFT_DATA.split(',');
if ( para.length<2) {
console.log('ERR: track - parameter should be at least lat,lon ');
rep.status(500).send('Error: Parameter error - try again');
return;
}
// sanity
if ( isNaN(para[0]) || isNaN(para[1]) ) {
console.log('ERR: track - lat lon paramater should be numbers ');
rep.status(500).send('Error: Parameter lat or lon is not a number - try again');
return;
}
lat = para[0];
lon = para[1];
// sanity for any optional data
if ( para.length>2 && !isNaN(para[2])) alt= para[2];
if ( para.length>3 && !isNaN(para[3])) hdg= para[3];
if ( para.length>4 && !isNaN(para[4])) gs= para[4];
if ( para.length>5 && !isNaN(para[5])) vs= para[5];
//console.log('Center coords received'); // DEBUG
rep.send('Center coords received'); // reply to sender
});