From d84f6337517cd4026d5042418ba0e3d872d77447 Mon Sep 17 00:00:00 2001 From: Michael Oborne Date: Fri, 4 Jun 2021 13:53:37 +1000 Subject: [PATCH] Dowding: add simple test server --- .gitignore | 2 + plugins/Dowding/DowdingUI.cs | 2 +- plugins/Dowding/server/node.js | 62 +++++++++++++++++++++++++++++ plugins/Dowding/server/package.json | 8 ++++ plugins/Dowding/server/runme.bat | 2 + 5 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 plugins/Dowding/server/node.js create mode 100644 plugins/Dowding/server/package.json create mode 100644 plugins/Dowding/server/runme.bat diff --git a/.gitignore b/.gitignore index 60cebc70ae..b12214c14f 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,5 @@ docs/ *.Test/ *.jar *.cache +/Plugins/Dowding/server/node_modules +/Plugins/Dowding/server/package-lock.json diff --git a/plugins/Dowding/DowdingUI.cs b/plugins/Dowding/DowdingUI.cs index d984d9a317..c9def322e8 100644 --- a/plugins/Dowding/DowdingUI.cs +++ b/plugins/Dowding/DowdingUI.cs @@ -366,7 +366,7 @@ private void but_onvif_Click(object sender, EventArgs e) private async void OnvifUpdate(object sender, PointLatLngAlt e) { await device.SetTrack(HomeLoc, - lastplla).ConfigureAwait(false); + e).ConfigureAwait(false); } private async void but_setathome_Click(object sender, EventArgs e) diff --git a/plugins/Dowding/server/node.js b/plugins/Dowding/server/node.js new file mode 100644 index 0000000000..07e66fa019 --- /dev/null +++ b/plugins/Dowding/server/node.js @@ -0,0 +1,62 @@ +console.log('Hello World') + +const url = require("url"); +const http = require('http'); +const WebSocket = require('ws'); + +const requestListener = function (req, res) { + console.log('requestListener'); + const pathname = url.parse(req.url).pathname; + if (pathname=== '/ws'){ + return; + } + + res.setHeader("Content-Type", "application/json"); + res.writeHead(200); + if (pathname=== '/api/1.0/authentication/login') { + res.end('{"token":"test"}'); + return; + } + res.end('[]'); +}; + + +const server = http.createServer(requestListener); + +const wss = new WebSocket.Server({ noServer: true }); + +wss.on('connection', function connection(ws) { + ws.on('message', function incoming(message) { + console.log('received: %s', message); + }); + +}); + +const interval = setInterval(function ping() { + wss.clients.forEach(function each(ws) { + +// var answer = new WebAPIs.Dowding.WSPackaging() { datatype = "vehicle_ticks", operation = "create", data = new VehicleTick(Ts: DateTime.Now.AddYears(1).toUnixTime() * 1000, CorrelationId: "iwashereagain", AgentId: Guid.NewGuid().ToString(), Lat: -35, Lon: 145, Hae: 40, Vn: (decimal)-1.7, Ve: (decimal)0.05, Vd: (decimal)0.019, Vendor: "DJI", Model: "mavic mini", Serial: "iwashere", Id: Guid.NewGuid().ToString(), ContactId: Guid.NewGuid().ToString()) }.ToJSON(); + + time = Math.floor(new Date().getTime()); + console.log('interval'); + ws.send('{ "datatype": "vehicle_ticks", "data": { "ts": '+time+', "correlation_id": "iwashereagain", "agent_id": "025e98ab-3260-4144-918e-4858d0a7b1fe", "lat": -38.1830, "lon": 144.4148, "hae": 40.0, "vn": -1.7, "ve": 0.05, "vd": 0.019, "vendor": "DJI", "model": "mavic mini", "serial": "iwashere", "id": "4b31b0af-feec-4437-942e-29cccd54cbeb", "contact_id": "9a64d68f-5f99-45e8-8bc5-939a5c395499" }, "operation": "create"}'); + ws.send('{ "datatype": "vehicle_ticks", "data": { "ts": '+time+', "correlation_id": "iwashereagain99", "agent_id": "025e98ab-3260-4144-918e-4858d0a7b1fe", "lat": -38.1830, "lon": 144.41485, "hae": 40.0, "vn": -1.7, "ve": 0.05, "vd": 0.019, "vendor": "DJI", "model": "mavic mini", "serial": "iwashere99", "id": "4b31b0af-feec-4437-942e-29cccd54cbec", "contact_id": "9a64d68f-5f99-45e8-8bc5-939a5c395498" }, "operation": "create"}'); + + }); +}, 10000); + + +server.on('upgrade', function upgrade(request, socket, head) { + console.log('upgrade'); + const pathname = url.parse(request.url).pathname; + + if (pathname === '/ws') { + wss.handleUpgrade(request, socket, head, function done(ws) { + wss.emit('connection', ws, request); + }); + } else { + socket.destroy(); + } +}); + +server.listen(80); diff --git a/plugins/Dowding/server/package.json b/plugins/Dowding/server/package.json new file mode 100644 index 0000000000..bfd8d1b13f --- /dev/null +++ b/plugins/Dowding/server/package.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "ws": "^7.4.6" + }, + "optionalDependencies": { + "bufferutil": "^4.0.3" + } +} diff --git a/plugins/Dowding/server/runme.bat b/plugins/Dowding/server/runme.bat new file mode 100644 index 0000000000..bcb20bd319 --- /dev/null +++ b/plugins/Dowding/server/runme.bat @@ -0,0 +1,2 @@ + +"C:\Program Files\nodejs\node.exe" node.js