send full rotator state to new clients over a typed WebSocket protocol

- Build the init message per connection from live state. Dynamic data is
  only broadcast on change, so new browsers showed rotators as OFFLINE
  until something moved.
- Load init data once a rotator is reachable and push it to clients;
  rotators offline at startup used to stay without name/map/offsets.
- Replace label-keyed messages (which could collide with 'radioInfo') with
  typed ServerMessage variants: init, initData, dynamic, setpoint, radioInfo.
- Keep setpoints in RotClient and station radio state in RotRouter instead
  of mutating a snapshot in index.ts.
- Include the resolved station in setpoints; the UI shows the station name
  in its colour instead of the source IP.
- Close stale WebSocket clients; drop the empty handleMessage stub.
- Replace TurnMessageHandler with plain callbacks; rename config types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 03:00:34 +00:00
co-authored by Claude Opus 5.5
parent db328336f1
commit 1fa6f2251c
6 changed files with 334 additions and 330 deletions
+118 -137
View File
@@ -38,7 +38,6 @@
color: #fff;
border-bottom-color: #4a9eff;
}
.tab-spacer { flex: 1; }
.rotators {
display: flex;
flex-wrap: wrap;
@@ -158,6 +157,7 @@
this.adc = 0;
this.online = false;
this.bands = [];
this.targetAngle = null;
}
}
@@ -500,8 +500,10 @@
: " | <span class=\"text-red\">&bull;</span> Offline";
}
updateTurnedBy(sourceIp, targetAz) {
this.els.turnedBy.textContent = `${sourceIp} \u2192 ${targetAz}\u00B0`;
updateTurnedBy(setpoint, station) {
const el = this.els.turnedBy;
el.textContent = `${setpoint.station ?? setpoint.sourceIp} \u2192 ${setpoint.targetAz}\u00B0`;
el.style.color = station ? station.color : '';
}
}
@@ -511,85 +513,68 @@
this.labels = [];
this.stations = {};
this.stationPills = {};
this.stationRadio = {};
this.selectedStation = null;
this.autoMode = false;
this.container = document.getElementById('rotators');
this.tabBar = document.getElementById('tab-bar');
this.stationBar = document.getElementById('station-bar');
this.websocket = null;
this.activeTab = null;
this.autoMode = false;
this.selectedStation = null;
this.stationRadio = {};
}
async initialize() {
const init = await this.startWs();
this.stations = init.stations || {};
this.stationRadio = init.stationRadio || {};
this.selectedStation = init.clientStation || null;
this.stations = init.stations;
this.stationRadio = init.stationRadio;
this.selectedStation = init.clientStation;
for (const [label, clientState] of Object.entries(init.rotators)) {
for (const [label, rotState] of Object.entries(init.rotators)) {
const state = new RotatorState();
if (clientState.initData) {
for (const [key, value] of Object.entries(clientState.initData)) {
state[key] = value;
}
}
state.bands = clientState.bands || [];
state.bands = rotState.bands;
const ui = new RotatorUI(label, this.container, state);
ui.updateStatic();
if (clientState.setpoint) {
state.targetAngle = clientState.setpoint.targetAz;
ui.updateTurnedBy(clientState.setpoint.sourceIp, clientState.setpoint.targetAz);
}
this.rotators[label] = { state, ui };
this.labels.push(label);
if (rotState.initData) this.applyInitData(label, rotState.initData);
if (rotState.dynamic) this.applyDynamic(label, rotState.dynamic);
if (rotState.setpoint) this.applySetpoint(label, rotState.setpoint);
}
this.buildTabBar();
this.buildStationBar();
this.updateTitle();
this.applyHash();
window.addEventListener('hashchange', () => this.applyHash());
}
updateTitle() {
document.title = Object.values(this.rotators)
.map(r => r.state.antName || "Rotator")
.join(" / ");
}
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);
this.autoTab = this.createTab('Auto', 'auto');
for (const label of this.labels) {
const tab = document.createElement('div');
tab.className = 'tab';
tab.textContent = this.rotators[label].state.antName || label;
tab.addEventListener('click', () => {
location.hash = encodeURIComponent(label);
});
this.rotators[label].tab = tab;
this.tabBar.appendChild(tab);
this.rotators[label].tab = this.createTab(this.rotators[label].state.antName || label, label);
}
this.allTab = this.createTab('All', 'all');
}
this.allTab = document.createElement('div');
this.allTab.className = 'tab';
this.allTab.textContent = 'All';
this.allTab.addEventListener('click', () => { location.hash = 'all'; });
this.tabBar.appendChild(this.allTab);
createTab(text, hash) {
const tab = document.createElement('div');
tab.className = 'tab';
tab.textContent = text;
tab.addEventListener('click', () => { location.hash = encodeURIComponent(hash); });
this.tabBar.appendChild(tab);
return tab;
}
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';
@@ -603,7 +588,7 @@
const freq = document.createElement('span');
freq.className = 'freq';
const radio = this.stationRadio[name];
freq.textContent = radio ? this.formatFreq(radio.freq) : '';
freq.textContent = radio ? formatFreq(radio.freq) : '';
pill.appendChild(freq);
pill.addEventListener('click', () => {
@@ -621,33 +606,21 @@
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 : '';
pill.el.style.borderColor = selected ? this.stations[name].color : '';
}
}
formatFreq(freq) {
return (freq / 100000).toFixed(3) + ' MHz';
}
applyHash() {
const hash = decodeURIComponent(location.hash.slice(1));
this.autoMode = false;
this.autoMode = hash !== 'all' && !this.rotators[hash];
if (hash === 'auto' || hash === '') {
this.autoMode = true;
this.applyAutoTab();
} else if (hash === 'all') {
this.showAll();
} else if (this.rotators[hash]) {
this.showTab(hash);
} else {
this.autoMode = true;
this.applyAutoTab();
}
if (this.autoMode) this.applyAutoTab();
else if (hash === 'all') this.showAll();
else this.showTab(hash);
this.stationBar.classList.toggle('visible', this.autoMode);
const hasStations = Object.keys(this.stations).length > 0;
this.stationBar.classList.toggle('visible', this.autoMode && hasStations);
this.autoTab.classList.toggle('active', this.autoMode);
this.allTab.classList.toggle('active', hash === 'all');
}
@@ -655,112 +628,120 @@
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);
}
if (match) this.showTab(match, true);
else this.showAll();
}
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();
}
return this.labels.find(l => this.rotators[l].state.bands.includes(band)) ?? null;
}
showTab(label, isAuto = false) {
this.activeTab = label;
for (const l of this.labels) {
const rot = this.rotators[l];
const visible = l === label;
rot.ui.card.classList.toggle('visible', visible);
rot.tab.classList.toggle('active', !isAuto && visible);
}
// re-render: the canvas may have been hidden
this.rotators[label].ui.renderer.render();
}
showAll(isAuto = false) {
this.activeTab = null;
showAll() {
for (const l of this.labels) {
const rot = this.rotators[l];
rot.ui.card.classList.add('visible');
rot.tab.classList.toggle('active', false);
rot.tab.classList.remove('active');
rot.ui.renderer.render();
}
if (!isAuto) this.allTab.classList.add('active');
}
async startWs() {
applyInitData(label, initData) {
const rot = this.rotators[label];
for (const [key, value] of Object.entries(initData)) {
if (value !== null) rot.state[key] = value;
}
rot.ui.updateStatic();
if (rot.tab) rot.tab.textContent = rot.state.antName || label;
this.updateTitle();
}
applyDynamic(label, { azimuth, status, adc, online }) {
const rot = this.rotators[label];
const needsRender = rot.state.azimuth !== azimuth
|| rot.state.status !== status
|| rot.state.online !== online;
rot.state.adc = adc;
rot.state.azimuth = azimuth;
rot.state.status = status;
rot.state.online = online;
if (needsRender) rot.ui.renderer.render();
rot.ui.updateDynamic();
}
applySetpoint(label, setpoint) {
const rot = this.rotators[label];
rot.state.targetAngle = setpoint.targetAz;
rot.ui.renderer.render();
rot.ui.updateTurnedBy(setpoint, this.stations[setpoint.station]);
}
handleRadioInfo({ station, band, freq }) {
this.stationRadio[station] = { band, freq };
const pill = this.stationPills[station];
if (pill) pill.freqEl.textContent = formatFreq(freq);
if (this.autoMode && station === this.selectedStation) this.applyAutoTab();
}
handleMessage(msg) {
if (msg.type === 'radioInfo') {
this.handleRadioInfo(msg);
return;
}
if (!this.rotators[msg.label]) return;
switch (msg.type) {
case 'initData': this.applyInitData(msg.label, msg.initData); break;
case 'dynamic': this.applyDynamic(msg.label, msg.dynamic); break;
case 'setpoint': this.applySetpoint(msg.label, msg.setpoint); break;
}
}
startWs() {
return new Promise((resolve) => {
this.websocket = new WebSocket(location.origin.replace(/^http/, 'ws') + location.pathname.replace(/\/?$/, '/') + 'ws');
let resolved = false;
const ws = new WebSocket(wsUrl());
this.websocket = ws;
this.websocket.addEventListener("message", (e) => {
if (!e || !e.data) return;
const parsed = JSON.parse(e.data);
if (!parsed || !parsed.data) return;
const data = parsed.data;
if (!resolved) {
resolved = true;
resolve(data);
ws.addEventListener("message", (e) => {
let msg;
try {
msg = JSON.parse(e.data);
} catch {
return;
}
if (data.radioInfo) {
this.handleRadioInfo(data.radioInfo.station, data.radioInfo.band, data.radioInfo.freq);
return;
}
for (const [label, rotData] of Object.entries(data)) {
const rot = this.rotators[label];
if (!rot) continue;
if (rotData.dynamic) {
const { azimuth, status, adc, online } = rotData.dynamic;
let needsRender = rot.state.azimuth !== azimuth
|| rot.state.status !== status
|| rot.state.online !== online;
rot.state.adc = adc;
rot.state.azimuth = azimuth;
rot.state.status = status;
rot.state.online = online;
if (needsRender) rot.ui.renderer.render();
rot.ui.updateDynamic();
}
if (rotData.turnedBy) {
rot.state.targetAngle = rotData.targetAz;
rot.ui.renderer.render();
rot.ui.updateTurnedBy(rotData.turnedBy, rotData.targetAz);
}
}
if (msg.type === 'init') resolve(msg);
else this.handleMessage(msg);
});
setInterval(() => {
this.websocket.send(JSON.stringify({ ping: 1 }));
ws.send(JSON.stringify({ ping: 1 }));
}, 2000);
});
}
}
function wsUrl() {
return location.origin.replace(/^http/, 'ws') + location.pathname.replace(/\/?$/, '/') + 'ws';
}
/** N1MM frequencies are in tens of Hz. */
function formatFreq(freq) {
return (freq / 100000).toFixed(3) + ' MHz';
}
document.addEventListener("DOMContentLoaded", () => {
const app = new RotatorApp();
app.initialize();
new RotatorApp().initialize();
});
</script>
</body>