summaryrefslogtreecommitdiffstats
path: root/middleware/index.js
blob: cc2c6e4eeea634e4ad3375c464e45cbdbc6e0c11 (plain) (blame)
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
// This is a Websocket server that is a middleware to interface wth the new Krakensdr app and future things

const express = require('express')
const ws = require('ws');
const fs = require('fs');

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

const remoteServer = 'kraken.tynet.eu:8044'
//const remoteServer = 'map.krakenrf.com:2096'
const settingsJsonPath = 'settings.json'

let lastDoaUpdate = Date.now()
let settingsJson = {};
let inRemoteMode = false;
let wsClient;
let wsServer;

//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;
}

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

  wsClient.onopen = () => {
    wsClient.send(`{"apikey": "${settingsJson.krakenpro_key}"}`) 
  }
   
  wsClient.onerror = (error) => {
    console.log('WebSocket error:', error)
  }
   
  wsClient.onmessage = (e) => {
    //check what data we got from Server
    var jsn = JSON.parse(e.data);
    if(jsn.function == 'settings'){
      console.log("Got new Settings: "+jsn);
      // read settings fresh from file and set new Settings
      loadSettingsJson();
      settingsJson.center_freq = Number.parseFloat(jsn.freq);      
      fs.writeFileSync(settingsJsonPath, JSON.stringify(settingsJson, null, 2));
    } else {
      console.log(jsn);
    }
  }
} else {
  // when not in Remote mode start websocket server for local connections
  // Websocket that sends incomming Data to App
  wsServer = new ws.Server({ noServer: true });
  wsServer.on('connection', socket => {
    console.log('Got websocket connection!')
    socket.on('message', message => {
        console.log("received: %s",message)
        //socket.send('Connection works to KrakenSDR')
      });
  });

  const server = app.listen(wsport);
  server.on('upgrade', (request, socket, head) => {
    wsServer.handleUpgrade(request, socket, head, socket => {
      wsServer.emit('connection', socket, request);
    });
  });
}

app.use(express.json())

app.get('/', (req, res) => {
    res.send('Hi, this is the KrakenSDR middleware server :)')
})

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));
        }
      })
    } 

  } 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 {
      // 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}`)
})