Skip to content

Commit

Permalink
update middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
krakenrf committed Feb 1, 2023
1 parent dcb45ac commit 9efedc6
Show file tree
Hide file tree
Showing 57 changed files with 1,685 additions and 2,998 deletions.
131 changes: 99 additions & 32 deletions _nodejs/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// This is a Websocket server that is a middleware to interface wth the new Krakensdr app and future things

require('log-timestamp');
const express = require('express')
const ws = require('ws');
const fs = require('fs');

const app = express()
const port = 8042
const wsport = 8021
const doaInterval = 1000 // Interval the clients should get new doa data in ms
const doaInterval = 250 // Interval the clients should get new doa data in ms

//const remoteServer = 'testmap.krakenrf.com:2096'
const remoteServer = 'map.krakenrf.com:2096'
const settingsJsonPath = 'settings.json'

Expand All @@ -17,33 +18,44 @@ let settingsJson = {};
let inRemoteMode = false;
let wsClient;
let wsServer;
let wsPingInterval;

//load doa settings file
function loadSettingsJson (){
let rawdata = fs.readFileSync(settingsJsonPath);
settingsJson = JSON.parse(rawdata);
/*
console.log("Loaded Settings from json")
console.log("Freq: "+settingsJson.center_freq);
console.log("Mode (Data Format): "+settingsJson.doa_data_format);
console.log("Name: "+settingsJson.station_id);
console.log("KrakenPro Key: "+settingsJson.krakenpro_key);
console.log("Lat: "+settingsJson.latitude);
console.log("Lon: "+settingsJson.longitude);
*/
}
loadSettingsJson();

// if in remote mode connect to server view websocket
if(settingsJson.doa_data_format == 'Kraken Pro Remote' && settingsJson.krakenpro_key != '0') {
console.log("Remote mode activated");
inRemoteMode = true;
function websocketPing (){
//console.log("Sending keepalive ping");
loadSettingsJson()
wsClient.send(`{"apikey": "${settingsJson.krakenpro_key}", "type": "ping"}`)
}

if(inRemoteMode){
function websocketConnect (){
wsClient = new ws("wss://"+remoteServer);

wsClient.onopen = () => {
wsClient.send(`{"apikey": "${settingsJson.krakenpro_key}"}`)
wsClient.send(`{"apikey": "${settingsJson.krakenpro_key}"}`)

// start ping interval
wsPingInterval = setInterval(websocketPing, 10000);
}

wsClient.onclose = (e) => {
console.log('Socket is closed. Reconnect will be attempted in 1 second.', e.reason);
setTimeout(websocketConnect, 1000);
};

wsClient.onerror = (error) => {
console.log('WebSocket error:', error)
Expand All @@ -62,6 +74,38 @@ if(inRemoteMode){
console.log(jsn);
}
}
}

function checkForRemoteMode (){
console.log("Checking for Remote Mode");
loadSettingsJson()
// if in remote mode connect to server with websocket
if(settingsJson.doa_data_format == 'Kraken Pro Remote' && settingsJson.krakenpro_key != '0') {
if(inRemoteMode == false){
console.log("Remote mode activated");
inRemoteMode = true;
websocketConnect()
}
setTimeout(checkForRemoteMode, 10000);
} else {
inRemoteMode = false;
if (wsClient) {
wsClient.onclose = function () {};
wsClient.close() //stop connection to remote Server
clearInterval(wsPingInterval)
wsClient = null
}
console.log("Remote mode deactivated, checking again in 10s");
// set 10s timer to poll Settings again and see if they changed to remote mode
setTimeout(checkForRemoteMode, 10000);
}
}

checkForRemoteMode()
/*
if(inRemoteMode){
websocketConnect();
} else {
// when not in Remote mode start websocket server for local connections
// Websocket that sends incomming Data to App
Expand All @@ -81,6 +125,7 @@ if(inRemoteMode){
});
});
}
*/

app.use(express.json())

Expand All @@ -89,32 +134,54 @@ app.get('/', (req, res) => {
})

app.post('/doapost', (req, res) => {
if(Date.now() - lastDoaUpdate > doaInterval){
//console.log(req.body);
lastDoaUpdate = Date.now()
// in remote mode, send data to sdr server backend like the App does
if (inRemoteMode) {
// In remote mode set lat/lon
//req.body.latitude = settingsJson.latitude;
//req.body.longitude = settingsJson.longitude;
//req.body.gpsBearing = settingsJson.heading;
console.log(req.body.latitude);
console.log(req.body.longitude);
wsClient.send(`{"apikey": "${settingsJson.krakenpro_key}", "data": ${JSON.stringify(req.body)}}`)
} else {
// sends data to all websocket clients
wsServer.clients.forEach(function each(client) {
if (client.readyState === ws.OPEN) {
client.send(JSON.stringify(req.body));
}
})
}
if(Date.now() - lastDoaUpdate > doaInterval){
//console.log(req.body);
lastDoaUpdate = Date.now()
// in remote mode, send data to sdr server backend like the App does
if (inRemoteMode) {
// In remote mode set lat/lon
//req.body.latitude = settingsJson.latitude;
//req.body.longitude = settingsJson.longitude;
//req.body.gpsBearing = settingsJson.heading;
//console.log(req.body.latitude);
//console.log(req.body.longitude);
wsClient.send(`{"apikey": "${settingsJson.krakenpro_key}", "data": ${JSON.stringify(req.body)}}`)
} else {
// sends data to all websocket clients
/*
wsServer.clients.forEach(function each(client) {
if (client.readyState === ws.OPEN) {
client.send(JSON.stringify(req.body));
}
})*/
}

} else {
console.log("...");
}
res.sendStatus(200)
});

app.post('/prpost', (req, res) => {
// in remote mode, send data to sdr server backend like the App does
if (inRemoteMode) {
// In remote mode set lat/lon
//req.body.latitude = settingsJson.latitude;
//req.body.longitude = settingsJson.longitude;
//req.body.gpsBearing = settingsJson.heading;
console.log(req.body);
wsClient.send(`{"apikey": "${settingsJson.krakenpro_key}", "type": "pr", "data": ${JSON.stringify(req.body)}}`)
} else {
console.log("...");
}
res.sendStatus(200)
});
// sends data to all websocket clients
/*
wsServer.clients.forEach(function each(client) {
if (client.readyState === ws.OPEN) {
client.send(JSON.stringify(req.body));
}
})*/
}
res.sendStatus(200)
});

app.listen(port, () => {
console.log(`Middleware HTTP Server is listening at http://localhost:${port}, Websocket on ${wsport}`)
Expand Down
72 changes: 15 additions & 57 deletions _nodejs/node_modules/accepts/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 21 additions & 49 deletions _nodejs/node_modules/array-flatten/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9efedc6

Please sign in to comment.