mirror of
https://github.com/Pax1601/DCSOlympus.git
synced 2025-10-29 16:56:34 +00:00
First tests and integration of radio panel
This commit is contained in:
@@ -5,6 +5,7 @@ import logger = require("morgan");
|
||||
import fs = require("fs");
|
||||
import bodyParser = require("body-parser");
|
||||
import cors = require("cors");
|
||||
import { AudioBackend } from "./audio/audiobackend";
|
||||
|
||||
/* Load the proxy middleware plugin */
|
||||
import httpProxyMiddleware = require("http-proxy-middleware");
|
||||
@@ -83,5 +84,10 @@ module.exports = function (configLocation, viteProxy) {
|
||||
});
|
||||
}
|
||||
|
||||
if (config["audio"]) {
|
||||
let audioBackend = new AudioBackend(config["audio"]["SRSPort"], config["audio"]["WSPort"]);
|
||||
audioBackend.start();
|
||||
}
|
||||
|
||||
return app;
|
||||
};
|
||||
|
||||
27
frontend/server/src/audio/audiobackend.ts
Normal file
27
frontend/server/src/audio/audiobackend.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { WebSocketServer } from "ws";
|
||||
import { SRSHandler } from "./srshandler";
|
||||
|
||||
export class AudioBackend {
|
||||
SRSPort: number = 5002;
|
||||
WSPort: number = 4000;
|
||||
handlers: SRSHandler[] = [];
|
||||
|
||||
constructor(SRSPort, WSPort) {
|
||||
this.SRSPort = SRSPort ?? this.SRSPort;
|
||||
this.WSPort = WSPort ?? this.WSPort;
|
||||
}
|
||||
|
||||
start() {
|
||||
const wss = new WebSocketServer({ port: this.WSPort });
|
||||
|
||||
wss.on("connection", (ws) => {
|
||||
this.handlers.push(new SRSHandler(ws, this.SRSPort));
|
||||
});
|
||||
|
||||
wss.on("disconnection", (ws) => {
|
||||
this.handlers = this.handlers.filter((handler) => {
|
||||
handler.ws != ws;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
112
frontend/server/src/audio/defaultdata.ts
Normal file
112
frontend/server/src/audio/defaultdata.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
export var defaultSRSData = {
|
||||
ClientGuid: "",
|
||||
Name: "",
|
||||
Seat: 0,
|
||||
Coalition: 0,
|
||||
AllowRecord: false,
|
||||
RadioInfo: {
|
||||
radios: [
|
||||
{
|
||||
enc: false,
|
||||
encKey: 0,
|
||||
freq: 1.0,
|
||||
modulation: 3,
|
||||
secFreq: 1.0,
|
||||
retransmit: false,
|
||||
},
|
||||
{
|
||||
enc: false,
|
||||
encKey: 0,
|
||||
freq: 1.0,
|
||||
modulation: 3,
|
||||
secFreq: 1.0,
|
||||
retransmit: false,
|
||||
},
|
||||
{
|
||||
enc: false,
|
||||
encKey: 0,
|
||||
freq: 1.0,
|
||||
modulation: 3,
|
||||
secFreq: 1.0,
|
||||
retransmit: false,
|
||||
},
|
||||
{
|
||||
enc: false,
|
||||
encKey: 0,
|
||||
freq: 1.0,
|
||||
modulation: 3,
|
||||
secFreq: 1.0,
|
||||
retransmit: false,
|
||||
},
|
||||
{
|
||||
enc: false,
|
||||
encKey: 0,
|
||||
freq: 1.0,
|
||||
modulation: 3,
|
||||
secFreq: 1.0,
|
||||
retransmit: false,
|
||||
},
|
||||
{
|
||||
enc: false,
|
||||
encKey: 0,
|
||||
freq: 1.0,
|
||||
modulation: 3,
|
||||
secFreq: 1.0,
|
||||
retransmit: false,
|
||||
},
|
||||
{
|
||||
enc: false,
|
||||
encKey: 0,
|
||||
freq: 1.0,
|
||||
modulation: 3,
|
||||
secFreq: 1.0,
|
||||
retransmit: false,
|
||||
},
|
||||
{
|
||||
enc: false,
|
||||
encKey: 0,
|
||||
freq: 1.0,
|
||||
modulation: 3,
|
||||
secFreq: 1.0,
|
||||
retransmit: false,
|
||||
},
|
||||
{
|
||||
enc: false,
|
||||
encKey: 0,
|
||||
freq: 1.0,
|
||||
modulation: 3,
|
||||
secFreq: 1.0,
|
||||
retransmit: false,
|
||||
},
|
||||
{
|
||||
enc: false,
|
||||
encKey: 0,
|
||||
freq: 1.0,
|
||||
modulation: 3,
|
||||
secFreq: 1.0,
|
||||
retransmit: false,
|
||||
},
|
||||
{
|
||||
enc: false,
|
||||
encKey: 0,
|
||||
freq: 1.0,
|
||||
modulation: 3,
|
||||
secFreq: 1.0,
|
||||
retransmit: false,
|
||||
},
|
||||
],
|
||||
unit: "",
|
||||
unitId: 0,
|
||||
iff: {
|
||||
control: 2,
|
||||
mode1: -1,
|
||||
mode2: -1,
|
||||
mode3: -1,
|
||||
mode4: false,
|
||||
mic: -1,
|
||||
status: 0,
|
||||
},
|
||||
ambient: { vol: 1.0, abType: "" },
|
||||
},
|
||||
LatLngPosition: { lat: 0.0, lng: 0.0, alt: 0.0 },
|
||||
};
|
||||
84
frontend/server/src/audio/srshandler.ts
Normal file
84
frontend/server/src/audio/srshandler.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { defaultSRSData } from "./defaultdata";
|
||||
var net = require("net");
|
||||
|
||||
const SRS_VERSION = "2.1.0.10";
|
||||
|
||||
var globalIndex = 1;
|
||||
|
||||
enum MessageType {
|
||||
audio,
|
||||
settings,
|
||||
}
|
||||
|
||||
function makeID(length) {
|
||||
let result = "";
|
||||
const characters =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
const charactersLength = characters.length;
|
||||
let counter = 0;
|
||||
while (counter < length) {
|
||||
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
||||
counter += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export class SRSHandler {
|
||||
ws: any;
|
||||
tcp = new net.Socket();
|
||||
udp = require("dgram").createSocket("udp4");
|
||||
data = JSON.parse(JSON.stringify(defaultSRSData));
|
||||
syncInterval: any;
|
||||
|
||||
constructor(ws, SRSPort) {
|
||||
this.data.ClientGuid = "ImF72dh9EYcIDyYRGaF9S9";
|
||||
this.data.Name = `Olympus${globalIndex}`;
|
||||
globalIndex += 1;
|
||||
|
||||
/* Websocket */
|
||||
this.ws = ws;
|
||||
this.ws.on("error", console.error);
|
||||
this.ws.on("message", (data) => {
|
||||
switch (data[0]) {
|
||||
case MessageType.audio:
|
||||
this.udp.send(data.slice(1), 5002, "localhost", function (error) {
|
||||
if (error) {
|
||||
console.log("Error!!!");
|
||||
} else {
|
||||
console.log("Data sent");
|
||||
}
|
||||
});
|
||||
break;
|
||||
case MessageType.settings:
|
||||
let message = JSON.parse(data.slice(1));
|
||||
message.settings.forEach((setting, idx) => {
|
||||
this.data.RadioInfo.radios[idx].freq = setting.frequency;
|
||||
this.data.RadioInfo.radios[idx].modulation = setting.modulation;
|
||||
})
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
this.ws.on("close", () => {
|
||||
this.tcp.end();
|
||||
});
|
||||
|
||||
/* TCP */
|
||||
this.tcp.connect(SRSPort, "localhost", () => {
|
||||
console.log("Connected");
|
||||
|
||||
this.syncInterval = setInterval(() => {
|
||||
let SYNC = {
|
||||
Client: this.data,
|
||||
MsgType: 2,
|
||||
Version: SRS_VERSION,
|
||||
};
|
||||
|
||||
if (this.tcp.readyState == "open")
|
||||
this.tcp.write(`${JSON.stringify(SYNC)}\n`);
|
||||
else clearInterval(this.syncInterval);
|
||||
}, 1000);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ module.exports = function (configLocation) {
|
||||
if (fs.existsSync(configLocation)) {
|
||||
let rawdata = fs.readFileSync(configLocation, "utf-8");
|
||||
const config = JSON.parse(rawdata);
|
||||
res.send(JSON.stringify(config.frontend));
|
||||
res.send(JSON.stringify({...config.frontend, ...(config.audio ?? {}) }));
|
||||
res.end()
|
||||
} else {
|
||||
res.sendStatus(404);
|
||||
|
||||
Reference in New Issue
Block a user