feat(radio): answer the RTL-SDR card through the radio store

SdrCard calls LocalSdrControls through radio store actions instead of
DOM listeners, and renders its gain options from the receiver's own
R820T gain steps with page-language labels. The display and radio
stores share one owner list (createOwners): the newest connected owner
with an action answers it.
This commit is contained in:
Sameh Khamis committed 2026-11-01 10:52:50 -08:00
1 parent e5fa31d01e
commit 61d11ce369
12 files changed
+320 -206

No files matched your search

+57 -110
View File
@@ -4,40 +4,16 @@ import {
localSdrCardView,
localSdrFeedLine,
localSdrFmAudioActive,
localSdrGainOptionLabel,
} from './localSdrPresentation.js';
import { useRadioStore } from './stores/radio.js';
import { applicationPinia } from './vueRuntime.js';
const ELEMENT_IDS = Object.freeze({
card: 'sdr-radio-card',
connectionState: 'sdr-connection-state',
connect: 'sdr-connect-btn',
locate: 'sdr-locate-btn',
changeDevice: 'sdr-change-device-btn',
modeFm: 'sdr-mode-fm-btn',
modeAdsb: 'sdr-mode-adsb-btn',
gain: 'sdr-gain-select',
stats: 'sdr-adsb-stats',
statRate: 'sdr-stat-rate',
statHeard: 'sdr-stat-heard',
statPositioned: 'sdr-stat-positioned',
statIq: 'sdr-stat-iq',
frequency: 'sdr-frequency-input',
tune: 'sdr-tune-btn',
seekBack: 'sdr-seek-back-btn',
seekForward: 'sdr-seek-forward-btn',
volume: 'sdr-volume',
volumeValue: 'sdr-volume-value',
status: 'sdr-status',
feedStatus: 'sdr-feed-status',
});
/**
* Local RTL-SDR card in the Radio panel.
*
* Owns the card's listeners and its receiver/radio subscriptions; the Radio
* view renders the card from what this publishes (`gev-radio` store). The
* Answers the card's controls and owns its receiver/radio subscriptions; the
* Radio view renders the card from what this publishes (`gev-radio` store)
* and sends its controls here through the store's act(). The
* Radio panel is one listening surface: starting local FM stops internet radio, and
* starting internet radio stops local FM.
*/
@@ -67,13 +43,6 @@ export class LocalSdrControls {
this.feeds = feeds;
this.radio = radio;
this.actions = actions;
this.elements = Object.fromEntries(
Object.entries(ELEMENT_IDS).map(([key, id]) => [
key,
doc?.getElementById?.(id) || null,
]),
);
this._listeners = new AbortController();
this._state = receiver.getState();
this._radioState = null;
this._fmAudioActive = localSdrFmAudioActive(this._state);
@@ -81,9 +50,9 @@ export class LocalSdrControls {
this._renderFrame = null;
this._destroyed = false;
this._view = null;
if (!this.elements.card) return;
this._labelGainOptions();
this._bind();
// A page without the card (the Radio view omitted) has no controls.
if (!doc?.getElementById?.('sdr-radio-card')) return;
this._storeToken = store.connect(this._actions());
this._receiverUnsubscribe = receiver.subscribe((state) =>
this._onReceiverState(state),
);
@@ -101,20 +70,6 @@ export class LocalSdrControls {
return Boolean(this._state?.connected);
}
/** Numeric gain options read in the page language; values stay numeric. */
_labelGainOptions() {
for (const option of this.elements.gain?.options || []) {
if (option.value !== 'auto')
option.textContent = localSdrGainOptionLabel(option.value);
}
}
_listen(element, type, handler) {
element?.addEventListener(type, handler, {
signal: this._listeners.signal,
});
}
_stopInternetRadio() {
if (internetRadioAudioActive(this._radioState))
this.radio?.stopPlayback?.({ origin: 'user' });
@@ -126,65 +81,56 @@ export class LocalSdrControls {
await this.actions.setLocalAdsbEnabled?.(true);
}
_bind() {
const el = this.elements;
this._listen(el.connect, 'click', async () => {
const before = this.receiver.getState();
if (before.connected) {
await this.receiver.stop();
return;
}
if (before.mode === 'fm') this._stopInternetRadio();
if (await this.receiver.connect(before.mode))
await this._enableLayerWhenReceiving();
});
this._listen(el.locate, 'click', () => {
void this.receiver.requestReceiverLocation();
});
this._listen(el.changeDevice, 'click', async () => {
if (this.receiver.getState().mode === 'fm') this._stopInternetRadio();
if (await this.receiver.changeDevice())
await this._enableLayerWhenReceiving();
});
this._listen(el.modeFm, 'click', async () => {
this._stopInternetRadio();
if (this.actions.isLocalAdsbEnabled?.())
await this.actions.setLocalAdsbEnabled?.(false);
await this.receiver.setMode('fm');
});
this._listen(el.modeAdsb, 'click', async () => {
if (await this.receiver.setMode('adsb'))
await this._enableLayerWhenReceiving();
});
this._listen(el.gain, 'change', () => {
void this.receiver.setGain(el.gain.value);
});
const tune = () => {
const mhz = Number(el.frequency?.value);
if (!Number.isFinite(mhz)) return;
this._stopInternetRadio();
void this.receiver.tuneFm(mhz * 1_000_000);
};
this._listen(el.tune, 'click', tune);
this._listen(el.frequency, 'keydown', (event) => {
if (event.key !== 'Enter') return;
event.preventDefault();
tune();
});
for (const [button, direction] of [
[el.seekBack, -1],
[el.seekForward, 1],
]) {
this._listen(button, 'click', () => {
/** What the card's controls ask for, by the names the view acts with. */
_actions() {
return {
toggleSdrConnection: async () => {
const before = this.receiver.getState();
if (before.connected) {
await this.receiver.stop();
return;
}
if (before.mode === 'fm') this._stopInternetRadio();
if (await this.receiver.connect(before.mode))
await this._enableLayerWhenReceiving();
},
locateSdr: () => {
void this.receiver.requestReceiverLocation();
},
changeSdrDevice: async () => {
if (this.receiver.getState().mode === 'fm') this._stopInternetRadio();
if (await this.receiver.changeDevice())
await this._enableLayerWhenReceiving();
},
setSdrMode: async (mode) => {
if (mode === 'fm') {
this._stopInternetRadio();
if (this.actions.isLocalAdsbEnabled?.())
await this.actions.setLocalAdsbEnabled?.(false);
await this.receiver.setMode('fm');
} else if (await this.receiver.setMode('adsb'))
await this._enableLayerWhenReceiving();
},
setSdrGain: (value) => {
void this.receiver.setGain(value);
},
tuneSdr: (value) => {
const mhz = Number(value);
if (!Number.isFinite(mhz)) return;
this._stopInternetRadio();
void this.receiver.tuneFm(mhz * 1_000_000);
},
seekSdr: (direction) => {
this._stopInternetRadio();
void this.receiver.seekFm(direction);
});
}
this._listen(el.volume, 'input', () => {
const value = Number(el.volume.value);
this.receiver.setVolume(value / 100);
if (this._view) this._publish({ ...this._view, volumeText: `${value}%` });
});
},
setSdrVolume: (percent) => {
const value = Number(percent);
this.receiver.setVolume(value / 100);
if (this._view)
this._publish({ ...this._view, volumeText: `${value}%` });
},
};
}
_onReceiverState(state) {
@@ -246,10 +192,11 @@ export class LocalSdrControls {
}
}
/** Release listeners and subscriptions; the receiver session is not closed. */
/** Release the controls and subscriptions; the receiver session is not closed. */
destroy() {
this._destroyed = true;
this._listeners.abort();
if (this._storeToken) this.store.detach(this._storeToken);
this._storeToken = null;
this._receiverUnsubscribe?.();
this._radioUnsubscribe?.();
this._feedsUnsubscribe?.();