// liveRoute module: the call on air as a route, in the direction its audio
// travels. RF: your radio -> this hotspot -> the talkgroup or reflector
// (via its network). Network: the caller -> the talkgroup or reflector
// (via its network) -> this hotspot -> out on RF. Flags and countries
// come from MCP (entry.extra: from, via, viaFlag, to; see qsoroute).
(function() {
'use strict';
var route = document.getElementById('lr-lanes');
if (!route) return;
var config = {}; // modem_config: callsign, frequencies
var ticker = null;
function t(key, fallback) { return i18n(key, fallback); }
function esc(s) {
var d = document.createElement('div');
d.appendChild(document.createTextNode(s == null ? '' : String(s)));
return d.innerHTML;
}
function flag(code) { return code && window.PiStar ? PiStar.flagHTML(code) : ''; }
function country(code) { return code && window.PiStar ? PiStar.countryName(code) : ''; }
function extra(e, k) { return (e.extra && e.extra[k]) || ''; }
// MHz to three decimal places, more only when the frequency needs
// them (443.0 → 443.000, 433.98765 → 433.98765); hz may be in Hz or MHz
function mhz(hz) {
var n = parseFloat(hz);
if (!n) return '';
n = Math.round(n > 1e5 ? n : n * 1e6);
if (window.PiStar && PiStar.formatFrequency) return PiStar.formatFrequency(n);
var s = (n / 1e6).toFixed(6);
while (/0$/.test(s) && s.length - s.indexOf('.') > 4) s = s.slice(0, -1);
return s;
}
function modeKey(mode) { return (mode || '').toLowerCase().replace(/[^a-z0-9]/g, ''); }
function modeLabel(e) {
var m = e.mode || '';
if (m === 'DMR' && e.slot) m += ' TS' + e.slot;
return m;
}
function duration(e) {
var s = e.status === 'in_progress'
? (Date.now() - new Date(e.timestamp).getTime()) / 1000
: (e.duration || 0);
if (s < 0) s = 0;
var m = Math.floor(s / 60), r = s - m * 60;
return (m ? m + ':' + (r < 10 ? '0' : '') + r.toFixed(1) : r.toFixed(1) + 's');
}
// S-meter text for an RF call (VHF/UHF: S9 = -93 dBm, 6 dB an S unit)
function smeter(dbm) {
if (!dbm) return '';
dbm = Math.round(dbm);
var s;
if (dbm >= -93) s = 'S9' + (dbm > -93 ? '+' + (Math.floor((dbm + 93) / 10) * 10 || '') : '');
else s = 'S' + Math.max(0, Math.round(9 - (-93 - dbm) / 6));
return s.replace(/\+$/, '') + ' · ' + dbm + ' dBm';
}
// A stop on the route
// titleId: the caller's ID, in brackets after their callsign
// caller: [callsign, ID] of a caller to look up: the callsign opens
// their look-up menu (pistar.js), and so does the whole node
function node(kind, icon, label, title, titleFlag, sub, subFlag, extraHTML, titleId, caller) {
var name = esc(title);
if (caller && window.PiStar && PiStar.callButton) name = PiStar.callButton(caller[0], caller[1], name);
var area = name.indexOf('call-menu-btn') >= 0 ? ' call-menu-area' : '';
return '
';
}
var ICON = {
radio: '',
hotspot: '',
net: '',
pager: '',
bridge: '',
person: '',
air: ''
};
// A DAPNET (or local) page: the network (and the DAPNET server) -> the
// pager (RIC, and the message) -> this hotspot -> out on POCSAG's
// frequency. No flags: a RIC is no place.
function renderPage(e) {
var msg = extra(e, 'message');
var freq = mhz(extra(e, 'freq'));
var ric = extra(e, 'ric');
return node('origin', ICON.net, t('liveRoute.caller', 'Calling'), e.callsign, '', extra(e, 'server') || '', '') + link() +
node('dest', ICON.pager, t('liveRoute.pager', 'Pager'), ric ? 'RIC ' + ric : (e.target || ''), '', '', '',
msg ? '
' + esc(msg) + '
' : '') + link() +
node('hotspot', ICON.hotspot, t('liveRoute.hotspot', 'This hotspot'), 'POCSAG', '', '') + link() +
node('air', ICON.air, t('liveRoute.onRF', 'On the air'), freq ? freq + ' MHz' : '', '', '', '', '');
}
// The route of one call: its stops, joined by links
function routeHTML(e) {
if (e.mode === 'POCSAG') return renderPage(e);
var live = e.status === 'in_progress';
var rf = e.source === 'rf';
var to = extra(e, 'to'), via = extra(e, 'via'), viaFlag = extra(e, 'viaFlag'), from = extra(e, 'from');
var name = extra(e, 'name'), bridge = extra(e, 'bridge'), farMode = extra(e, 'farMode'), farTarget = extra(e, 'farTarget');
var tgName = extra(e, 'tgName');
// The destination: past a bridge, its far side
var toCall = extra(e, 'toCall');
var destTitle = bridge ? (farTarget || farMode) : (toCall || e.target || '');
// A rewritten talkgroup (TGIF's 31672 dialled as 4031672): the one
// it is on the network
var netTarget = extra(e, 'netTarget');
var destSub = via ? t('liveRoute.via', 'via {name}').replace('{name}', via + (netTarget ? ' \u00b7 ' + netTarget : '')) : (tgName || country(to));
if (e.targetType === 'private' && !bridge) destSub = t('liveRoute.private', 'Private call') + (via ? ' · ' + destSub : '');
var destMore = (tgName && via ? '
' + esc(tgName) + '
' : '');
var destLabel = t('liveRoute.destination', 'Talking to') + (farMode ? ' · ' + farMode : '');
var callerSub = [name, country(from)].filter(Boolean).join(' · ');
// BrandMeister's parrot answers as 9990
var caller = e.callsign;
// The caller's ID (DMR, P25, NXDN), unless it is the callsign
var callerId = extra(e, 'srcId');
callerId = callerId && String(callerId) !== String(caller || '') ? String(callerId) : '';
// Looked up from the On Air card too (not the parrot)
var lookup = [caller, extra(e, 'srcId')];
if (/^999[0-9]$/.test(caller)) { callerSub = caller; caller = t('tg.parrot', 'Parrot'); callerId = ''; lookup = null; }
var stats = '
';
if (rf && e.rssi) stats += '' + esc(smeter(e.rssi)) + '';
if (!live && e.ber != null) stats += '' + esc(t('liveRoute.ber', 'BER {ber}').replace('{ber}', e.ber.toFixed(2) + '%')) + '';
if (!live && e.loss != null) stats += '' + esc(t('liveRoute.loss', 'Loss {loss}').replace('{loss}', e.loss.toFixed(1) + '%')) + '';
// Lost: on RF the signal went; from the network, its end never came
if (e.status === 'lost') stats += '' + esc(rf ? t('liveRoute.lost', 'Signal lost') : t('liveRoute.eotMissed', 'End of transmission missed')) + '';
stats += '
';
var hotspotSub = rf
? t('liveRoute.receiving', 'Receiving on {freq} MHz').replace('{freq}', mhz(config.rxFrequency))
: t('liveRoute.transmitting', 'Transmitting on {freq} MHz').replace('{freq}', mhz(config.txFrequency));
if (!mhz(rf ? config.rxFrequency : config.txFrequency)) hotspotSub = '';
var bridgeNode = bridge
? node('bridge', ICON.bridge, t('liveRoute.crossMode', 'Cross-mode'), bridge, '', rf ? e.mode + ' → ' + farMode : farMode + ' → ' + e.mode, '') + link()
: '';
var dest = node('dest', ICON.net, destLabel, destTitle, to, destSub, via ? viaFlag : '', destMore);
if (rf) {
return node('origin', ICON.radio, t('liveRoute.yourRadio', 'Your radio'), caller, from, callerSub, '', stats, callerId, lookup) + link() +
node('hotspot', ICON.hotspot, t('liveRoute.hotspot', 'This hotspot'), modeLabel(e), '', hotspotSub) + link() +
bridgeNode + dest;
}
return node('origin', ICON.person, t('liveRoute.caller', 'Calling'), caller, from, callerSub, '', '', callerId, lookup) + link() +
dest + link() + bridgeNode +
node('hotspot', ICON.hotspot, t('liveRoute.hotspot', 'This hotspot'), modeLabel(e), '', '') + link() +
node('air', ICON.air, t('liveRoute.onRF', 'On the air'), mhz(config.txFrequency) ? mhz(config.txFrequency) + ' MHz' : '', '', '', '', stats);
}
// ── Lanes ──
// A lane shows one call: the one in progress, else the last. A duplex
// hotspot with DMR carries both DMR timeslots at once, so it has a
// lane for each (TS1, TS2), and one more for the last call in another
// mode once there is one. A simplex hotspot uses one timeslot: one
// lane for everything.
var lanes = {}; // lane key -> entry
function twoSlots() {
return !!(config.duplex && config.modes && config.modes.DMR);
}
function laneOf(e) {
return (twoSlots() && e.mode === 'DMR' && (e.slot === 1 || e.slot === 2)) ? 'TS' + e.slot : 'one';
}
function laneKeys() {
if (!twoSlots()) return ['one'];
return ['TS1', 'TS2', 'one'];
}
// Lane sizes. Both timeslots busy (each used within the last 30 s):
// both compact, sharing the space. One quiet for longer: the latest
// back to full size, the quiet one slim. One lane: full size.
var QUIET_MS = 30000;
var resize = null;
function endedAt(e) {
if (e.status === 'in_progress') return Date.now();
return new Date(e.timestamp).getTime() + (e.duration || 0) * 1000;
}
function shownKeys() {
return laneKeys().filter(function(k) { return lanes[k]; });
}
function quiet(e, now) {
return e.status !== 'in_progress' && now - endedAt(e) > QUIET_MS;
}
function sizes(keys) {
var out = {};
if (keys.length < 2) { keys.forEach(function(k) { out[k] = 'full'; }); return out; }
var now = Date.now(), latest = keys[0];
keys.forEach(function(k) { if (endedAt(lanes[k]) > endedAt(lanes[latest])) latest = k; });
var othersQuiet = keys.every(function(k) { return k === latest || quiet(lanes[k], now); });
keys.forEach(function(k) {
if (k === latest) out[k] = othersQuiet ? 'full' : 'half';
else out[k] = quiet(lanes[k], now) ? 'slim' : 'half';
});
return out;
}
// Look again when the next lane turns quiet
function scheduleResize(keys) {
if (resize) { clearTimeout(resize); resize = null; }
if (keys.length < 2) return;
var now = Date.now(), next = Infinity;
keys.forEach(function(k) {
var e = lanes[k];
if (!quiet(e, now) && e.status !== 'in_progress') next = Math.min(next, endedAt(e) + QUIET_MS - now);
});
if (next !== Infinity) resize = setTimeout(render, Math.max(250, next + 100));
}
function stateText(e) {
if (!e) return '';
var live = e.status === 'in_progress';
// A page has no length: when it went out
var when = e.mode === 'POCSAG'
? new Date(e.timestamp).toLocaleTimeString(document.documentElement.lang || undefined)
: duration(e);
return (live ? t('liveRoute.live', 'Live') : t('liveRoute.lastHeard', 'Last heard')) + ' · ' + when;
}
function laneHTML(key, size) {
var e = lanes[key];
var name = key === 'one' ? (twoSlots() && e ? (e.mode || '') : '') : key;
var cls = 'lr-lane lr-size-' + (size || 'full');
if (e) cls += ' lr-mode-' + modeKey(e.mode) + (e.status === 'in_progress' ? ' lr-live' : ' lr-past') + (e.source === 'rf' ? ' lr-rf' : ' lr-net');
return '
' +
'
' + esc(name) + '' +
'' + esc(stateText(e)) + '
' +
'
' + (e ? routeHTML(e) : '
' + esc(t('liveRoute.waiting', 'Waiting for the first call')) + '
') + '
' +
'
';
}
function render() {
var keys = shownKeys();
if (!keys.length) return;
var sz = sizes(keys);
route.className = 'lr-lanes';
route.innerHTML = keys.map(function(k) { return laneHTML(k, sz[k]); }).join('');
tick();
scheduleResize(keys);
}
// The lanes' times: only their text changes, so the links keep flowing
function tick() {
var anyLive = false;
shownKeys().forEach(function(k) {
var e = lanes[k];
var el = route.querySelector('.lr-lane[data-lane="' + k + '"] .lr-state');
if (el && e) el.textContent = stateText(e);
if (e && e.status === 'in_progress') anyLive = true;
});
if (anyLive && !ticker) ticker = setInterval(tick, 200);
if (!anyLive && ticker) { clearInterval(ticker); ticker = null; }
}
// A call into its lane: one in progress takes the lane; one that
// ended stays until the next, unless the lane has a live call
function place(e) {
var k = laneOf(e), cur = lanes[k];
if (!cur || cur.id === e.id || e.status === 'in_progress' || cur.status !== 'in_progress') lanes[k] = e;
}
function shown(e) {
return e && e.mode && e.mode !== 'CW' && e.callsign;
}
document.addEventListener('pistar:message', function(ev) {
var msg = ev.detail;
if (msg.type === 'modem_config' && msg.config) {
var wasTwo = twoSlots();
config = msg.config;
// Lanes follow duplex: re-sort the calls into them
if (wasTwo !== twoSlots()) {
var all = Object.keys(lanes).map(function(k) { return lanes[k]; });
lanes = {};
all.sort(function(a, b) { return a.timestamp < b.timestamp ? -1 : 1; }).forEach(place);
}
render();
return;
}
if (msg.type === 'activity_batch') {
lanes = {};
(msg.entries || []).forEach(function(e) { if (shown(e)) place(e); });
render();
return;
}
if (msg.type !== 'activity' || !shown(msg.entry)) return;
place(msg.entry);
render();
});
})();