add stations and an Auto tab following each station's band
Parse N1MM RadioInfo packets, map sender IPs to configured stations, and let the front-end auto-select the rotator for the selected station's current band. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+20
@@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
|
"rotators": {
|
||||||
"rot14": {
|
"rot14": {
|
||||||
"ip": "192.168.33.82",
|
"ip": "192.168.33.82",
|
||||||
"bands": [
|
"bands": [
|
||||||
@@ -17,4 +18,23 @@
|
|||||||
21, 144
|
21, 144
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"stations": {
|
||||||
|
"DELL1": {
|
||||||
|
"ip": "192.168.0.21",
|
||||||
|
"color": "#00ff00"
|
||||||
|
},
|
||||||
|
"FUJI2": {
|
||||||
|
"ip": "192.168.0.22",
|
||||||
|
"color": "#ff0000"
|
||||||
|
},
|
||||||
|
"FUJI3": {
|
||||||
|
"ip": "192.168.0.23",
|
||||||
|
"color": "#FFFF00"
|
||||||
|
},
|
||||||
|
"DELL4": {
|
||||||
|
"ip": "192.168.0.24",
|
||||||
|
"color": "#ffffff"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
## Radio Info
|
||||||
|
```xml
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<RadioInfo>
|
||||||
|
<app>N1MM</app>
|
||||||
|
<StationName>CW-80m</StationName>
|
||||||
|
<RadioNr>1</RadioNr>
|
||||||
|
<Freq>352211</Freq>
|
||||||
|
<TXFreq>352211</TXFreq>
|
||||||
|
<Mode>CW</Mode>
|
||||||
|
<mycall>W1ABC</mycall>
|
||||||
|
<OpCall>W1ABC</OpCall>
|
||||||
|
<IsRunning>False</IsRunning>
|
||||||
|
<FocusEntry>204626</FocusEntry>
|
||||||
|
<EntryWindowHwnd>275678</EntryWindowHwnd>
|
||||||
|
<Antenna>8</Antenna>
|
||||||
|
<Rotors></Rotors>
|
||||||
|
<FocusRadioNr>1</FocusRadioNr>
|
||||||
|
<IsStereo>False</IsStereo>
|
||||||
|
<IsSplit>False</IsSplit>
|
||||||
|
<ActiveRadioNr>1</ActiveRadioNr>
|
||||||
|
<IsTransmitting>False</IsTransmitting>
|
||||||
|
<FunctionKeyCaption></FunctionKeyCaption>
|
||||||
|
<RadioName></RadioName>
|
||||||
|
<AuxAntSelected>-1</AuxAntSelected>
|
||||||
|
<AuxAntSelectedName></AuxAntSelectedName>
|
||||||
|
<IsConnected></IsConnected>
|
||||||
|
</RadioInfo>
|
||||||
|
```
|
||||||
|
|
||||||
|
## Radio Info Notes
|
||||||
|
- **app** is a short name of the logger program sending the packet. Examples: “N1MM”, “DXLab”, “Logger32”, “UcxLog”, “Log4OM”
|
||||||
|
- **StationName** is the NetBios name of the computer that is sending these messages. It is the name used in Multi-Computer networking. Windows limits it to 15 characters. If the computer name is greater than 15 characters long, the first 15 characters will be used.
|
||||||
|
- **RadioNr** is the radio number associated with a specific XML packet – in other words, the source of the information in that packet. When in SO2V or SO2R mode, N1MM+ sends two packets every ten seconds – one packet each from RadioNr1 and RadioNr2
|
||||||
|
- **Freq** is the receive frequency represented as values to the tens digit with no delimiter. For example: 160 meters: 181234; 40 meters: 712345; 10 meters: 2812345; 6 meters: 5012345
|
||||||
|
- **TXFreq** is the transmit frequency represented as values to the tens digit with no delimiter. For example: 160 meters: 181234; 40 meters: 712345; 10 meters: 2812345; 6 meters: 5012345
|
||||||
|
- **Mode** is the logging mode and could be any one of the following: CW, USB, LSB, RTTY, PSK31, PSK63, PSK125, PSK250, QPSK31, QPSK63, QPSK125, QPSK250, FSK, GMSK, DOMINO, HELL, FMHELL, HELL80, MT63, THOR, THRB, THRBX, OLIVIA, MFSK8, MFSK16
|
||||||
|
- **mycall** is the station callsign
|
||||||
|
- **OpCall** is the callsign entered by the operator after OPON (or Ctl-O). Defaults to the station call
|
||||||
|
- **IsRunning** represents the value of the RUN versus S&P radio buttons in the Entry Window. If you are on your CQ frequency, IsRunning will normally be True
|
||||||
|
- **FocusEntry** is the Windows assigned handle of the Entry Window with program focus
|
||||||
|
- **EntryWindowHwnd** allows external software to send commands to a specific EntryWindow (i.e. using AHK to send F1 to EW2 etc.)
|
||||||
|
- **Antenna** is the currently selected antenna for this radio (0-63), from the Antenna tab in the Configurer
|
||||||
|
- **Rotors** is the name of the currently selected rotor from the Antenna table in the Configurer
|
||||||
|
- **FocusRadioNr** Receive Radio Focus or keyboard focus – the Green Dot in the Entry Window. Enables signal switching for SO2R operation, facilitating the routing of Microphone, Audio, PTT, CW signals to/from the selected radio. FocusRadioNr toggles between 1 and 2 when the program, a mouse click, or the \ key selects the opposite Entry Window
|
||||||
|
- **IsStereo** enables audio switching for SO2R operation.The ` key (backquote) toggles its value between True and False
|
||||||
|
- **ActiveRadioNr** Transmit Radio Focus – the Red Dot in the Entry Window. Enables signal switching for SO2R operation, facilitating the routing of Microphone, Audio, PTT, CW signals to/from the selected radio. ActiveRadioNr toggles between 1 and 2 when the program or pressing the [Pause] key selects the opposite Entry Window
|
||||||
|
- **IsSplit** Transceiver is in Split VFO mode (for example: vfoA=Rx, vfoB=Tx). Not available on all – especially older – rigs
|
||||||
|
- **IsTransmitting** The program (as distinct from the radio) is in transmit (True) or receive (False) mode. See IsTransmitting warning, below
|
||||||
|
- **FunctionKeyCaption** The label of the Function Key that was pressed to initiate this transmission
|
||||||
|
- **RadioName** The name of the radio, as shown in the Entry Window (i.e. “TS-590”)
|
||||||
|
- **AuxAntSelected** is normally “-1”, until the user presses an Entry Window function key containing the {auxantsel} macro. A UDP packet containing the selected antenna number is sent once per keypress, after which `<AuxAntSelected>` reverts to “-1”
|
||||||
|
- **AuxAntSelectedName** is normally blank, until the user presses an Entry Window function key containing the {auxantsel} macro. A UDP packet containing the selected antenna name is sent once per keypress, after which `<AuxAntSelectedName>` reverts to a blank field
|
||||||
|
- **IsConnected** indicates whether a radio is connected. It may take some time for a disconnection (False) to be registered
|
||||||
|
|
||||||
|
The program sends Radio Info packets at 10 second intervals, or immediately after an event where any of the information in one of these fields changes (for example: user changes bands, spins the VFO dial, switches from Run to S&P, or selects vfoB).
|
||||||
|
|
||||||
|
## Transmitting with `<IsTransmitting>` = False
|
||||||
|
The IsTransmitting tag in RadioInfo only displays as True when N1MM+ has initiated a transmission. When a transmission is initiated external to N1MM+ – for example, a microphone PTT button or CW paddle key – IsTransmitting will still be displayed as False. Be careful when using IsTransmitting to control external devices that need to know when the radio is in Tx or Rx.
|
||||||
|
|
||||||
|
## RadioInfo broadcasts in SO2V
|
||||||
|
Using RadioInfo broadcasts when the Logger is in SO2V mode may require special consideration. In SO2V, packets in which RadioNr and ActiveRadioNr are different from each other should not be relied upon for information about the transmit frequency.
|
||||||
|
|
||||||
|
Explanation: N1MM+ treats the two VFOs in SO2V similarly to the way it treats the two radios in SO2R. Broadcasts with RadioNr = 1 relate to VFO A (the main receiver in most dual-receiver radios), and broadcasts with RadioNr = 2 relate to VFO B (the subreceiver in radios so equipped). In each of these broadcasts, the exported value of TxFreq is normally the same as the receive frequency (Freq), regardless of which VFO is active (as if RadioNr 1 and RadioNr 2 were two separate radios). The exception is the special case when the program is in Split mode. In this case (and only in this case) TxFreq will indicate the frequency in the other VFO to reflect the Split mode status.
|
||||||
|
|
||||||
|
What this means is that programs receiving RadioInfo broadcast packets and requiring knowledge of the transmit frequency or band (such as programs controlling amplifiers or antenna switches) must take the ActiveRadioNr into account. In SO2V mode, the TxFreq reported in packets in which RadioNr is not the same as ActiveRadioNr will generally not be the actual transmit frequency. This is not likely to be a major problem when the two VFOs are on the same band, but if the two VFOs are on different bands, the TxFreq reported in these inactive radio packets will be misleading. To determine the correct transmit frequency, when N1MM+ is in SO2V such programs should only use TxFreq from packets in which RadioNr and ActiveRadioNr have the same value.
|
||||||
+149
-18
@@ -97,11 +97,48 @@
|
|||||||
.turned-by .source {
|
.turned-by .source {
|
||||||
color: #e0e040;
|
color: #e0e040;
|
||||||
}
|
}
|
||||||
|
.station-bar {
|
||||||
|
display: none;
|
||||||
|
gap: 8px;
|
||||||
|
padding: 8px 12px;
|
||||||
|
background: #0a0a0a;
|
||||||
|
border-bottom: 1px solid #222;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
.station-bar.visible {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
.station-pill {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
padding: 5px 12px;
|
||||||
|
border: 2px solid #333;
|
||||||
|
border-radius: 16px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 14px;
|
||||||
|
color: #888;
|
||||||
|
transition: border-color 0.15s;
|
||||||
|
}
|
||||||
|
.station-pill:hover { border-color: #666; }
|
||||||
|
.station-pill.selected { color: #fff; }
|
||||||
|
.station-pill .dot {
|
||||||
|
width: 10px;
|
||||||
|
height: 10px;
|
||||||
|
border-radius: 50%;
|
||||||
|
}
|
||||||
|
.station-pill .freq {
|
||||||
|
color: #666;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
.station-pill.selected .freq { color: #aaa; }
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<div class="tab-bar" id="tab-bar"></div>
|
<div class="tab-bar" id="tab-bar"></div>
|
||||||
|
<div class="station-bar" id="station-bar"></div>
|
||||||
<div class="rotators" id="rotators"></div>
|
<div class="rotators" id="rotators"></div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
@@ -472,17 +509,26 @@
|
|||||||
constructor() {
|
constructor() {
|
||||||
this.rotators = {};
|
this.rotators = {};
|
||||||
this.labels = [];
|
this.labels = [];
|
||||||
|
this.stations = {};
|
||||||
|
this.stationPills = {};
|
||||||
this.container = document.getElementById('rotators');
|
this.container = document.getElementById('rotators');
|
||||||
this.tabBar = document.getElementById('tab-bar');
|
this.tabBar = document.getElementById('tab-bar');
|
||||||
|
this.stationBar = document.getElementById('station-bar');
|
||||||
this.websocket = null;
|
this.websocket = null;
|
||||||
this.tiled = false;
|
|
||||||
this.activeTab = null;
|
this.activeTab = null;
|
||||||
|
this.autoMode = false;
|
||||||
|
this.selectedStation = null;
|
||||||
|
this.stationRadio = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
async initialize() {
|
async initialize() {
|
||||||
const initState = await this.startWs();
|
const init = await this.startWs();
|
||||||
|
|
||||||
for (const [label, clientState] of Object.entries(initState)) {
|
this.stations = init.stations || {};
|
||||||
|
this.stationRadio = init.stationRadio || {};
|
||||||
|
this.selectedStation = init.clientStation || null;
|
||||||
|
|
||||||
|
for (const [label, clientState] of Object.entries(init.rotators)) {
|
||||||
const state = new RotatorState();
|
const state = new RotatorState();
|
||||||
|
|
||||||
if (clientState.initData) {
|
if (clientState.initData) {
|
||||||
@@ -505,6 +551,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.buildTabBar();
|
this.buildTabBar();
|
||||||
|
this.buildStationBar();
|
||||||
this.applyHash();
|
this.applyHash();
|
||||||
window.addEventListener('hashchange', () => this.applyHash());
|
window.addEventListener('hashchange', () => this.applyHash());
|
||||||
|
|
||||||
@@ -514,6 +561,12 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
buildTabBar() {
|
buildTabBar() {
|
||||||
|
this.autoTab = document.createElement('div');
|
||||||
|
this.autoTab.className = 'tab';
|
||||||
|
this.autoTab.textContent = 'Auto';
|
||||||
|
this.autoTab.addEventListener('click', () => { location.hash = 'auto'; });
|
||||||
|
this.tabBar.appendChild(this.autoTab);
|
||||||
|
|
||||||
for (const label of this.labels) {
|
for (const label of this.labels) {
|
||||||
const tab = document.createElement('div');
|
const tab = document.createElement('div');
|
||||||
tab.className = 'tab';
|
tab.className = 'tab';
|
||||||
@@ -528,50 +581,123 @@
|
|||||||
this.allTab = document.createElement('div');
|
this.allTab = document.createElement('div');
|
||||||
this.allTab.className = 'tab';
|
this.allTab.className = 'tab';
|
||||||
this.allTab.textContent = 'All';
|
this.allTab.textContent = 'All';
|
||||||
this.allTab.addEventListener('click', () => {
|
this.allTab.addEventListener('click', () => { location.hash = 'all'; });
|
||||||
location.hash = 'all';
|
|
||||||
});
|
|
||||||
this.tabBar.appendChild(this.allTab);
|
this.tabBar.appendChild(this.allTab);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buildStationBar() {
|
||||||
|
for (const [name, cfg] of Object.entries(this.stations)) {
|
||||||
|
const pill = document.createElement('div');
|
||||||
|
pill.className = 'station-pill';
|
||||||
|
pill.style.borderColor = this.selectedStation === name ? cfg.color : '';
|
||||||
|
|
||||||
|
const dot = document.createElement('span');
|
||||||
|
dot.className = 'dot';
|
||||||
|
dot.style.background = cfg.color;
|
||||||
|
pill.appendChild(dot);
|
||||||
|
|
||||||
|
const nameSpan = document.createElement('span');
|
||||||
|
nameSpan.textContent = name;
|
||||||
|
pill.appendChild(nameSpan);
|
||||||
|
|
||||||
|
const freq = document.createElement('span');
|
||||||
|
freq.className = 'freq';
|
||||||
|
const radio = this.stationRadio[name];
|
||||||
|
freq.textContent = radio ? this.formatFreq(radio.freq) : '';
|
||||||
|
pill.appendChild(freq);
|
||||||
|
|
||||||
|
pill.addEventListener('click', () => {
|
||||||
|
this.selectedStation = name;
|
||||||
|
this.updateStationSelection();
|
||||||
|
this.applyAutoTab();
|
||||||
|
});
|
||||||
|
|
||||||
|
this.stationBar.appendChild(pill);
|
||||||
|
this.stationPills[name] = { el: pill, freqEl: freq };
|
||||||
|
}
|
||||||
|
this.updateStationSelection();
|
||||||
|
}
|
||||||
|
|
||||||
|
updateStationSelection() {
|
||||||
|
for (const [name, pill] of Object.entries(this.stationPills)) {
|
||||||
|
const selected = name === this.selectedStation;
|
||||||
|
const color = this.stations[name].color;
|
||||||
|
pill.el.classList.toggle('selected', selected);
|
||||||
|
pill.el.style.borderColor = selected ? color : '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
formatFreq(freq) {
|
||||||
|
return (freq / 100000).toFixed(3) + ' MHz';
|
||||||
|
}
|
||||||
|
|
||||||
applyHash() {
|
applyHash() {
|
||||||
const hash = decodeURIComponent(location.hash.slice(1));
|
const hash = decodeURIComponent(location.hash.slice(1));
|
||||||
|
this.autoMode = false;
|
||||||
|
|
||||||
if (hash === 'all' || (hash === '' && this.labels.length <= 1)) {
|
if (hash === 'auto' || hash === '') {
|
||||||
this.tiled = true;
|
this.autoMode = true;
|
||||||
|
this.applyAutoTab();
|
||||||
|
} else if (hash === 'all') {
|
||||||
this.showAll();
|
this.showAll();
|
||||||
} else if (this.rotators[hash]) {
|
} else if (this.rotators[hash]) {
|
||||||
this.tiled = false;
|
|
||||||
this.showTab(hash);
|
this.showTab(hash);
|
||||||
} else {
|
} else {
|
||||||
// default: first tab, or all if single rotator
|
this.autoMode = true;
|
||||||
this.tiled = false;
|
this.applyAutoTab();
|
||||||
this.showTab(this.labels[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.allTab.classList.toggle('active', this.tiled);
|
this.stationBar.classList.toggle('visible', this.autoMode);
|
||||||
|
this.autoTab.classList.toggle('active', this.autoMode);
|
||||||
|
this.allTab.classList.toggle('active', hash === 'all');
|
||||||
}
|
}
|
||||||
|
|
||||||
showTab(label) {
|
applyAutoTab() {
|
||||||
|
const radio = this.selectedStation ? this.stationRadio[this.selectedStation] : null;
|
||||||
|
const match = radio ? this.findRotatorForBand(radio.band) : null;
|
||||||
|
if (match) {
|
||||||
|
this.showTab(match, true);
|
||||||
|
} else {
|
||||||
|
this.showAll(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
findRotatorForBand(band) {
|
||||||
|
for (const [label, rot] of Object.entries(this.rotators)) {
|
||||||
|
if (rot.state.bands.includes(band)) return label;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
handleRadioInfo(station, band, freq) {
|
||||||
|
this.stationRadio[station] = { band, freq };
|
||||||
|
const pill = this.stationPills[station];
|
||||||
|
if (pill) pill.freqEl.textContent = this.formatFreq(freq);
|
||||||
|
if (this.autoMode && station === this.selectedStation) {
|
||||||
|
this.applyAutoTab();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
showTab(label, isAuto = false) {
|
||||||
this.activeTab = label;
|
this.activeTab = label;
|
||||||
for (const l of this.labels) {
|
for (const l of this.labels) {
|
||||||
const rot = this.rotators[l];
|
const rot = this.rotators[l];
|
||||||
const visible = l === label;
|
const visible = l === label;
|
||||||
rot.ui.card.classList.toggle('visible', visible);
|
rot.ui.card.classList.toggle('visible', visible);
|
||||||
rot.tab.classList.toggle('active', visible);
|
rot.tab.classList.toggle('active', !isAuto && visible);
|
||||||
}
|
}
|
||||||
// re-render visible one (canvas may have been hidden)
|
|
||||||
this.rotators[label].ui.renderer.render();
|
this.rotators[label].ui.renderer.render();
|
||||||
}
|
}
|
||||||
|
|
||||||
showAll() {
|
showAll(isAuto = false) {
|
||||||
this.activeTab = null;
|
this.activeTab = null;
|
||||||
for (const l of this.labels) {
|
for (const l of this.labels) {
|
||||||
const rot = this.rotators[l];
|
const rot = this.rotators[l];
|
||||||
rot.ui.card.classList.add('visible');
|
rot.ui.card.classList.add('visible');
|
||||||
rot.tab.classList.remove('active');
|
rot.tab.classList.toggle('active', false);
|
||||||
rot.ui.renderer.render();
|
rot.ui.renderer.render();
|
||||||
}
|
}
|
||||||
|
if (!isAuto) this.allTab.classList.add('active');
|
||||||
}
|
}
|
||||||
|
|
||||||
async startWs() {
|
async startWs() {
|
||||||
@@ -593,6 +719,11 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (data.radioInfo) {
|
||||||
|
this.handleRadioInfo(data.radioInfo.station, data.radioInfo.band, data.radioInfo.freq);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (const [label, rotData] of Object.entries(data)) {
|
for (const [label, rotData] of Object.entries(data)) {
|
||||||
const rot = this.rotators[label];
|
const rot = this.rotators[label];
|
||||||
if (!rot) continue;
|
if (!rot) continue;
|
||||||
|
|||||||
+34
-13
@@ -2,16 +2,22 @@ import dgram from 'node:dgram';
|
|||||||
import { XMLParser } from 'fast-xml-parser';
|
import { XMLParser } from 'fast-xml-parser';
|
||||||
import type { TurnMessageHandler } from './types.js';
|
import type { TurnMessageHandler } from './types.js';
|
||||||
|
|
||||||
interface N1MMRotorXml {
|
interface ParsedXml {
|
||||||
N1MMRotor?: {
|
N1MMRotor?: {
|
||||||
freqband?: string;
|
freqband?: string;
|
||||||
goazi?: string;
|
goazi?: string;
|
||||||
};
|
};
|
||||||
|
RadioInfo?: {
|
||||||
|
Freq?: number;
|
||||||
|
RadioNr?: number;
|
||||||
|
FocusRadioNr?: number;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class N1mmServer {
|
export default class N1mmServer {
|
||||||
private readonly xmlParser = new XMLParser();
|
private readonly xmlParser = new XMLParser();
|
||||||
private readonly turnHandlers: TurnMessageHandler[] = [];
|
private readonly turnHandlers: TurnMessageHandler[] = [];
|
||||||
|
private readonly radioInfoHandlers: Array<(sourceIp: string, band: number, freq: number) => void> = [];
|
||||||
|
|
||||||
start(): void {
|
start(): void {
|
||||||
const server = dgram.createSocket('udp4');
|
const server = dgram.createSocket('udp4');
|
||||||
@@ -27,8 +33,7 @@ export default class N1mmServer {
|
|||||||
});
|
});
|
||||||
|
|
||||||
server.on('message', (msg, rinfo) => {
|
server.on('message', (msg, rinfo) => {
|
||||||
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
|
this.processXml(msg.toString(), rinfo.address);
|
||||||
this.processN1mmXml(msg.toString(), rinfo.address);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
server.bind(12040);
|
server.bind(12040);
|
||||||
@@ -38,20 +43,36 @@ export default class N1mmServer {
|
|||||||
this.turnHandlers.push(handler);
|
this.turnHandlers.push(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
private processN1mmXml(xml: string, sourceIp: string): void {
|
onRadioInfo(handler: (sourceIp: string, band: number, freq: number) => void): void {
|
||||||
try {
|
this.radioInfoHandlers.push(handler);
|
||||||
const parsed = this.xmlParser.parse(xml) as N1MMRotorXml;
|
|
||||||
const band = parseInt(parsed?.N1MMRotor?.freqband?.split(',')[0] ?? '0', 10);
|
|
||||||
const az = parseInt(parsed?.N1MMRotor?.goazi?.split(',')[0] ?? '0', 10);
|
|
||||||
|
|
||||||
if (!band || !az) {
|
|
||||||
console.error('Missing band or azimuth');
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.turnHandlers.forEach(h => h.handleTurnMessage({ band, az, sourceIp }));
|
private processXml(xml: string, sourceIp: string): void {
|
||||||
|
try {
|
||||||
|
const parsed = this.xmlParser.parse(xml) as ParsedXml;
|
||||||
|
if (parsed.N1MMRotor) {
|
||||||
|
this.processRotor(parsed, sourceIp);
|
||||||
|
} else if (parsed.RadioInfo) {
|
||||||
|
this.processRadioInfo(parsed, sourceIp);
|
||||||
|
}
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
console.error(`Failed to parse: ${xml}`, ex);
|
console.error(`Failed to parse: ${xml}`, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private processRotor(parsed: ParsedXml, sourceIp: string): void {
|
||||||
|
const band = parseInt(String(parsed.N1MMRotor?.freqband ?? '').split(',')[0] || '0', 10);
|
||||||
|
const az = parseInt(String(parsed.N1MMRotor?.goazi ?? '').split(',')[0] || '0', 10);
|
||||||
|
if (!band || !az) return;
|
||||||
|
this.turnHandlers.forEach(h => h.handleTurnMessage({ band, az, sourceIp }));
|
||||||
|
}
|
||||||
|
|
||||||
|
private processRadioInfo(parsed: ParsedXml, sourceIp: string): void {
|
||||||
|
const freq = Number(parsed.RadioInfo?.Freq ?? 0);
|
||||||
|
const radioNr = Number(parsed.RadioInfo?.RadioNr ?? 0);
|
||||||
|
const focusRadioNr = Number(parsed.RadioInfo?.FocusRadioNr ?? 0);
|
||||||
|
if (!freq || radioNr !== focusRadioNr) return;
|
||||||
|
const band = Math.floor(freq / 100000);
|
||||||
|
this.radioInfoHandlers.forEach(h => h(sourceIp, band, freq));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+28
-3
@@ -1,6 +1,6 @@
|
|||||||
import { readFileSync } from 'node:fs';
|
import { readFileSync } from 'node:fs';
|
||||||
import RotClient from './RotClient.js';
|
import RotClient from './RotClient.js';
|
||||||
import type { Config, DynamicData, InitialState, TurnMessage, TurnMessageHandler } from './types.js';
|
import type { FullConfig, StationConfig, DynamicData, InitialState, TurnMessage, TurnMessageHandler } from './types.js';
|
||||||
|
|
||||||
export interface TurnEvent {
|
export interface TurnEvent {
|
||||||
label: string;
|
label: string;
|
||||||
@@ -10,8 +10,11 @@ export interface TurnEvent {
|
|||||||
|
|
||||||
export default class RotRouter implements TurnMessageHandler {
|
export default class RotRouter implements TurnMessageHandler {
|
||||||
private readonly clients: Record<string, RotClient> = {};
|
private readonly clients: Record<string, RotClient> = {};
|
||||||
|
private readonly stations: Record<string, StationConfig> = {};
|
||||||
|
private readonly stationsByIp: Record<string, string> = {};
|
||||||
private readonly routerDataHandlers: Array<(rotator: RotClient, data: DynamicData) => void> = [];
|
private readonly routerDataHandlers: Array<(rotator: RotClient, data: DynamicData) => void> = [];
|
||||||
private readonly turnEventHandlers: Array<(event: TurnEvent) => void> = [];
|
private readonly turnEventHandlers: Array<(event: TurnEvent) => void> = [];
|
||||||
|
private readonly radioInfoHandlers: Array<(station: string, band: number, freq: number) => void> = [];
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.loadConfig();
|
this.loadConfig();
|
||||||
@@ -19,12 +22,34 @@ export default class RotRouter implements TurnMessageHandler {
|
|||||||
|
|
||||||
private loadConfig(): void {
|
private loadConfig(): void {
|
||||||
const raw = readFileSync('config.json', 'utf-8');
|
const raw = readFileSync('config.json', 'utf-8');
|
||||||
const parsed = JSON.parse(raw) as Config;
|
const parsed = JSON.parse(raw) as FullConfig;
|
||||||
for (const [label, clientConfig] of Object.entries(parsed)) {
|
for (const [label, clientConfig] of Object.entries(parsed.rotators)) {
|
||||||
const client = new RotClient(label, clientConfig);
|
const client = new RotClient(label, clientConfig);
|
||||||
client.onDynamicDataUpdate(data => this.routerDataHandlers.forEach(h => h(client, data)));
|
client.onDynamicDataUpdate(data => this.routerDataHandlers.forEach(h => h(client, data)));
|
||||||
this.clients[label] = client;
|
this.clients[label] = client;
|
||||||
}
|
}
|
||||||
|
for (const [name, stationConfig] of Object.entries(parsed.stations)) {
|
||||||
|
this.stations[name] = stationConfig;
|
||||||
|
this.stationsByIp[stationConfig.ip] = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resolveStation(ip: string): string | null {
|
||||||
|
return this.stationsByIp[ip] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
getStations(): Record<string, StationConfig> {
|
||||||
|
return this.stations;
|
||||||
|
}
|
||||||
|
|
||||||
|
handleRadioInfo(sourceIp: string, band: number, freq: number): void {
|
||||||
|
const station = this.resolveStation(sourceIp);
|
||||||
|
if (!station) return;
|
||||||
|
this.radioInfoHandlers.forEach(h => h(station, band, freq));
|
||||||
|
}
|
||||||
|
|
||||||
|
onRadioInfo(handler: (station: string, band: number, freq: number) => void): void {
|
||||||
|
this.radioInfoHandlers.push(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
private findClientForBand(band: number): RotClient | null {
|
private findClientForBand(band: number): RotClient | null {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export default class WebsocketManager {
|
|||||||
private readonly clients = new Set<WSContext>();
|
private readonly clients = new Set<WSContext>();
|
||||||
// Map correctly tracks identity — plain object keys would stringify all WSContext to "[object Object]"
|
// Map correctly tracks identity — plain object keys would stringify all WSContext to "[object Object]"
|
||||||
private readonly lastPing = new Map<WSContext, number>();
|
private readonly lastPing = new Map<WSContext, number>();
|
||||||
private initData: unknown = null;
|
private initData: Record<string, unknown> = {};
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
@@ -20,10 +20,10 @@ export default class WebsocketManager {
|
|||||||
}, 10_000);
|
}, 10_000);
|
||||||
}
|
}
|
||||||
|
|
||||||
getHandler() {
|
getHandler(clientStation: string | null = null) {
|
||||||
return {
|
return {
|
||||||
onOpen: (_event: Event, ws: WSContext) => {
|
onOpen: (_event: Event, ws: WSContext) => {
|
||||||
this.joinClient(ws);
|
this.joinClient(ws, clientStation);
|
||||||
},
|
},
|
||||||
onMessage: (event: MessageEvent, ws: WSContext) => {
|
onMessage: (event: MessageEvent, ws: WSContext) => {
|
||||||
if (!event.data) return;
|
if (!event.data) return;
|
||||||
@@ -63,10 +63,10 @@ export default class WebsocketManager {
|
|||||||
this.clients.forEach(client => this.send(client, data));
|
this.clients.forEach(client => this.send(client, data));
|
||||||
}
|
}
|
||||||
|
|
||||||
private joinClient(ws: WSContext): void {
|
private joinClient(ws: WSContext, clientStation: string | null): void {
|
||||||
this.clients.add(ws);
|
this.clients.add(ws);
|
||||||
this.lastPing.set(ws, Date.now());
|
this.lastPing.set(ws, Date.now());
|
||||||
this.send(ws, this.initData);
|
this.send(ws, { ...this.initData, clientStation });
|
||||||
}
|
}
|
||||||
|
|
||||||
private leaveClient(ws: WSContext): void {
|
private leaveClient(ws: WSContext): void {
|
||||||
@@ -74,7 +74,7 @@ export default class WebsocketManager {
|
|||||||
this.lastPing.delete(ws);
|
this.lastPing.delete(ws);
|
||||||
}
|
}
|
||||||
|
|
||||||
setInitData(initData: unknown): void {
|
setInitData(initData: Record<string, unknown>): void {
|
||||||
this.initData = initData;
|
this.initData = initData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-6
@@ -11,6 +11,7 @@ const router = new RotRouter();
|
|||||||
|
|
||||||
const n1mm = new N1mmServer();
|
const n1mm = new N1mmServer();
|
||||||
n1mm.onTurnMessage(router);
|
n1mm.onTurnMessage(router);
|
||||||
|
n1mm.onRadioInfo((sourceIp, band, freq) => router.handleRadioInfo(sourceIp, band, freq));
|
||||||
// n1mm.start();
|
// n1mm.start();
|
||||||
|
|
||||||
const app = new Hono();
|
const app = new Hono();
|
||||||
@@ -19,8 +20,9 @@ app.use('/*', serveStatic({ root: './public' }));
|
|||||||
const websocketManager = new WebsocketManager();
|
const websocketManager = new WebsocketManager();
|
||||||
const { injectWebSocket, upgradeWebSocket } = createNodeWebSocket({ app });
|
const { injectWebSocket, upgradeWebSocket } = createNodeWebSocket({ app });
|
||||||
|
|
||||||
const initialState = await router.readInitialState();
|
const stationRadio: Record<string, { band: number; freq: number }> = {};
|
||||||
websocketManager.setInitData(initialState);
|
const rotators = await router.readInitialState();
|
||||||
|
websocketManager.setInitData({ rotators, stations: router.getStations(), stationRadio });
|
||||||
|
|
||||||
router.onRotatorData((rotator, data) => {
|
router.onRotatorData((rotator, data) => {
|
||||||
websocketManager.broadcast({ [rotator.label]: { dynamic: data } });
|
websocketManager.broadcast({ [rotator.label]: { dynamic: data } });
|
||||||
@@ -28,23 +30,36 @@ router.onRotatorData((rotator, data) => {
|
|||||||
|
|
||||||
router.onTurnEvent((event) => {
|
router.onTurnEvent((event) => {
|
||||||
const setpoint = { targetAz: event.az, sourceIp: event.sourceIp };
|
const setpoint = { targetAz: event.az, sourceIp: event.sourceIp };
|
||||||
const client = initialState[event.label];
|
const client = rotators[event.label];
|
||||||
if (client) client.setpoint = setpoint;
|
if (client) client.setpoint = setpoint;
|
||||||
websocketManager.broadcast({ [event.label]: { turnedBy: event.sourceIp, targetAz: event.az } });
|
websocketManager.broadcast({ [event.label]: { turnedBy: event.sourceIp, targetAz: event.az } });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.onRadioInfo((station, band, freq) => {
|
||||||
|
stationRadio[station] = { band, freq };
|
||||||
|
websocketManager.broadcast({ radioInfo: { station, band, freq } });
|
||||||
|
});
|
||||||
|
|
||||||
|
function resolveClientIp(c: Parameters<typeof getConnInfo>[0]): string {
|
||||||
|
const rawIp = c.req.header('x-forwarded-for') ?? getConnInfo(c).remote.address ?? 'unknown';
|
||||||
|
return rawIp.startsWith('::ffff:') ? rawIp.slice(7) : rawIp;
|
||||||
|
}
|
||||||
|
|
||||||
app.post('/turn/:label', async (c) => {
|
app.post('/turn/:label', async (c) => {
|
||||||
const label = c.req.param('label');
|
const label = c.req.param('label');
|
||||||
const body = await c.req.parseBody();
|
const body = await c.req.parseBody();
|
||||||
const az = Number(body['ROT']);
|
const az = Number(body['ROT']);
|
||||||
if (isNaN(az)) return c.text('Invalid azimuth', 400);
|
if (isNaN(az)) return c.text('Invalid azimuth', 400);
|
||||||
const rawIp = c.req.header('x-forwarded-for') ?? getConnInfo(c).remote.address ?? 'unknown';
|
const sourceIp = resolveClientIp(c);
|
||||||
const sourceIp = rawIp.startsWith('::ffff:') ? rawIp.slice(7) : rawIp;
|
|
||||||
const ok = router.turn(label, az, sourceIp);
|
const ok = router.turn(label, az, sourceIp);
|
||||||
return ok ? c.text(`Turning ${label} to ${az}`) : c.text('Unknown rotator', 404);
|
return ok ? c.text(`Turning ${label} to ${az}`) : c.text('Unknown rotator', 404);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/ws', upgradeWebSocket(() => websocketManager.getHandler()));
|
app.get('/ws', upgradeWebSocket((c) => {
|
||||||
|
const clientIp = resolveClientIp(c);
|
||||||
|
const clientStation = router.resolveStation(clientIp);
|
||||||
|
return websocketManager.getHandler(clientStation);
|
||||||
|
}));
|
||||||
|
|
||||||
const server = serve(
|
const server = serve(
|
||||||
{ fetch: app.fetch, port: 8787 },
|
{ fetch: app.fetch, port: 8787 },
|
||||||
|
|||||||
+9
-1
@@ -13,7 +13,15 @@ export interface ClientConfig {
|
|||||||
bands: number[];
|
bands: number[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Config = Record<string, ClientConfig>;
|
export interface StationConfig {
|
||||||
|
ip: string;
|
||||||
|
color: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FullConfig {
|
||||||
|
rotators: Record<string, ClientConfig>;
|
||||||
|
stations: Record<string, StationConfig>;
|
||||||
|
}
|
||||||
|
|
||||||
export interface DynamicData {
|
export interface DynamicData {
|
||||||
adc: number | null;
|
adc: number | null;
|
||||||
|
|||||||
Reference in New Issue
Block a user