refactor: share typed APRS plugin contracts
This commit is contained in:
@@ -1,5 +1,107 @@
|
||||
"use strict";
|
||||
(() => {
|
||||
// src/plugins/aprs-shared.ts
|
||||
function aprsPacketCategory(packet) {
|
||||
const type = (packet.type ?? "").toLowerCase();
|
||||
const info = (packet.info ?? "").toLowerCase();
|
||||
if (packet.lat != null && packet.lon != null || type.includes("position")) return "position";
|
||||
if (type.includes("message") || info.startsWith(":")) return "message";
|
||||
if (type.includes("weather") || info.startsWith("_")) return "weather";
|
||||
if (type.includes("telemetry") || info.startsWith("t#")) return "telemetry";
|
||||
return "other";
|
||||
}
|
||||
function aprsCategoryLabel(category) {
|
||||
switch (category) {
|
||||
case "position":
|
||||
return "Position";
|
||||
case "message":
|
||||
return "Message";
|
||||
case "weather":
|
||||
return "Weather";
|
||||
case "telemetry":
|
||||
return "Telemetry";
|
||||
default:
|
||||
return "Other";
|
||||
}
|
||||
}
|
||||
function aprsAgeText(timestampMs) {
|
||||
if (typeof timestampMs !== "number" || !Number.isFinite(timestampMs)) return "just now";
|
||||
const seconds = Math.round(Math.max(0, Date.now() - timestampMs) / 1e3);
|
||||
if (seconds < 5) return "just now";
|
||||
if (seconds < 60) return `${String(seconds)}s ago`;
|
||||
const minutes = Math.round(seconds / 60);
|
||||
if (minutes < 60) return `${String(minutes)}m ago`;
|
||||
return `${String(Math.round(minutes / 60))}h ago`;
|
||||
}
|
||||
function aprsPacketSignature(packet) {
|
||||
return [
|
||||
packet.srcCall ?? "",
|
||||
packet.destCall ?? "",
|
||||
packet.path ?? "",
|
||||
packet.info ?? "",
|
||||
packet.type ?? "",
|
||||
packet.lat?.toFixed(4) ?? "",
|
||||
packet.lon?.toFixed(4) ?? ""
|
||||
].join("|");
|
||||
}
|
||||
function collapseAprsDuplicates(packets) {
|
||||
const seen = /* @__PURE__ */ new Set();
|
||||
return packets.filter((packet) => {
|
||||
const signature = aprsPacketSignature(packet);
|
||||
if (seen.has(signature)) return false;
|
||||
seen.add(signature);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
function aprsHexBytes(bytes) {
|
||||
if (!bytes?.length) return "--";
|
||||
return bytes.map((byte) => byte.toString(16).toUpperCase().padStart(2, "0")).join(" ");
|
||||
}
|
||||
function renderAprsInfo(packet) {
|
||||
if (packet.info_bytes?.length) {
|
||||
return packet.info_bytes.map((byte) => renderAprsByte(byte)).join("");
|
||||
}
|
||||
return Array.from(packet.info ?? "", (character) => renderAprsCharacter(character)).join("");
|
||||
}
|
||||
function renderAprsByte(byte) {
|
||||
return byte >= 32 && byte <= 126 ? escapeAprsCharacter(String.fromCharCode(byte)) : `<span class="aprs-byte">0x${byte.toString(16).toUpperCase().padStart(2, "0")}</span>`;
|
||||
}
|
||||
function renderAprsCharacter(character) {
|
||||
const code = character.charCodeAt(0);
|
||||
return code >= 32 && code <= 126 ? escapeAprsCharacter(character) : `<span class="aprs-byte">0x${code.toString(16).toUpperCase().padStart(2, "0")}</span>`;
|
||||
}
|
||||
function escapeAprsCharacter(character) {
|
||||
if (character === "<") return "<";
|
||||
if (character === ">") return ">";
|
||||
if (character === "&") return "&";
|
||||
if (character === '"') return """;
|
||||
return character;
|
||||
}
|
||||
function renderLocalAprsSymbol(packet, escapeHtml) {
|
||||
if (!packet.symbolTable || !packet.symbolCode) return "";
|
||||
const symbol = escapeHtml(packet.symbolCode);
|
||||
const table = packet.symbolTable === "/" ? "Primary" : "Alternate";
|
||||
return `<span class="aprs-symbol aprs-symbol-local" title="${table} APRS symbol ${symbol}">${symbol}</span>`;
|
||||
}
|
||||
function normalizeAprsPacket(packet, receiver) {
|
||||
return {
|
||||
rig_id: packet.rig_id || null,
|
||||
receiver,
|
||||
srcCall: packet.src_call ?? "",
|
||||
destCall: packet.dest_call ?? "",
|
||||
path: packet.path ?? "",
|
||||
info: packet.info ?? "",
|
||||
info_bytes: packet.info_bytes ?? [],
|
||||
type: packet.packet_type ?? "",
|
||||
crcOk: packet.crc_ok ?? false,
|
||||
ts_ms: packet.ts_ms ?? null,
|
||||
lat: packet.lat ?? null,
|
||||
lon: packet.lon ?? null,
|
||||
symbolTable: packet.symbol_table ?? null,
|
||||
symbolCode: packet.symbol_code ?? null
|
||||
};
|
||||
}
|
||||
|
||||
// src/plugins/aprs.ts
|
||||
var aprsWindow = window;
|
||||
var escapeAprsHtml = (input) => aprsWindow.escapeMapHtml?.(input) ?? input.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll('"', """);
|
||||
@@ -48,77 +150,6 @@
|
||||
updateAprsBar();
|
||||
});
|
||||
}
|
||||
function renderAprsInfo(pkt) {
|
||||
const bytes = Array.isArray(pkt.info_bytes) ? pkt.info_bytes : null;
|
||||
if (bytes && bytes.length > 0) {
|
||||
let out2 = "";
|
||||
for (const b of bytes) {
|
||||
if (b >= 32 && b <= 126) {
|
||||
const ch = String.fromCharCode(b);
|
||||
if (ch === "<") out2 += "<";
|
||||
else if (ch === ">") out2 += ">";
|
||||
else if (ch === "&") out2 += "&";
|
||||
else if (ch === '"') out2 += """;
|
||||
else out2 += ch;
|
||||
} else {
|
||||
const hex = b.toString(16).toUpperCase().padStart(2, "0");
|
||||
out2 += `<span class="aprs-byte">0x${hex}</span>`;
|
||||
}
|
||||
}
|
||||
return out2;
|
||||
}
|
||||
const str = pkt.info || "";
|
||||
let out = "";
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
const code = str.charCodeAt(i);
|
||||
if (code >= 32 && code <= 126) {
|
||||
const ch = str.charAt(i);
|
||||
if (ch === "<") out += "<";
|
||||
else if (ch === ">") out += ">";
|
||||
else if (ch === "&") out += "&";
|
||||
else if (ch === '"') out += """;
|
||||
else out += ch;
|
||||
} else {
|
||||
const hex = code.toString(16).toUpperCase().padStart(2, "0");
|
||||
out += `<span class="aprs-byte">0x${hex}</span>`;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
function aprsPacketCategory(pkt) {
|
||||
const type = (pkt.type ?? "").toLowerCase();
|
||||
const info = (pkt.info ?? "").toLowerCase();
|
||||
if (pkt.lat != null && pkt.lon != null || type.includes("position")) return "position";
|
||||
if (type.includes("message") || info.startsWith(":")) return "message";
|
||||
if (type.includes("weather") || info.startsWith("_")) return "weather";
|
||||
if (type.includes("telemetry") || info.startsWith("t#")) return "telemetry";
|
||||
return "other";
|
||||
}
|
||||
function aprsCategoryLabel(category) {
|
||||
switch (category) {
|
||||
case "position":
|
||||
return "Position";
|
||||
case "message":
|
||||
return "Message";
|
||||
case "weather":
|
||||
return "Weather";
|
||||
case "telemetry":
|
||||
return "Telemetry";
|
||||
default:
|
||||
return "Other";
|
||||
}
|
||||
}
|
||||
function aprsAgeText(tsMs) {
|
||||
if (typeof tsMs !== "number" || !Number.isFinite(tsMs)) return "just now";
|
||||
const deltaMs = Math.max(0, Date.now() - tsMs);
|
||||
const seconds = Math.round(deltaMs / 1e3);
|
||||
if (seconds < 5) return "just now";
|
||||
if (seconds < 60) return `${seconds}s ago`;
|
||||
const minutes = Math.round(seconds / 60);
|
||||
if (minutes < 60) return `${minutes}m ago`;
|
||||
const hours = Math.round(minutes / 60);
|
||||
return `${hours}h ago`;
|
||||
}
|
||||
function aprsDistanceText(pkt) {
|
||||
if (aprsWindow.serverLat == null || aprsWindow.serverLon == null || pkt.lat == null || pkt.lon == null || !aprsWindow.haversineKm) return "";
|
||||
const distKm = aprsWindow.haversineKm(aprsWindow.serverLat, aprsWindow.serverLon, pkt.lat, pkt.lon);
|
||||
@@ -126,21 +157,6 @@
|
||||
if (distKm < 1) return `${Math.round(distKm * 1e3)} m from TRX`;
|
||||
return `${distKm.toFixed(1)} km from TRX`;
|
||||
}
|
||||
function aprsPacketSignature(pkt) {
|
||||
return [
|
||||
pkt.srcCall || "",
|
||||
pkt.destCall || "",
|
||||
pkt.path || "",
|
||||
pkt.info || "",
|
||||
pkt.type || "",
|
||||
pkt.lat != null ? pkt.lat.toFixed(4) : "",
|
||||
pkt.lon != null ? pkt.lon.toFixed(4) : ""
|
||||
].join("|");
|
||||
}
|
||||
function aprsHexBytes(bytes) {
|
||||
if (!Array.isArray(bytes) || bytes.length === 0) return "--";
|
||||
return bytes.map((b) => b.toString(16).toUpperCase().padStart(2, "0")).join(" ");
|
||||
}
|
||||
function aprsFilterMatch(pkt) {
|
||||
if (aprsOnlyPos && (pkt.lat == null || pkt.lon == null)) return false;
|
||||
if (aprsHideCrc && !pkt.crcOk) return false;
|
||||
@@ -162,17 +178,6 @@
|
||||
const packets = aprsCollapseDup ? collapseAprsDuplicates(aprsPacketHistory) : aprsPacketHistory;
|
||||
return packets.filter(aprsFilterMatch);
|
||||
}
|
||||
function collapseAprsDuplicates(packets) {
|
||||
const seen = /* @__PURE__ */ new Set();
|
||||
const out = [];
|
||||
for (const pkt of packets) {
|
||||
const key = aprsPacketSignature(pkt);
|
||||
if (seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
out.push(pkt);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
function updateAprsSummary() {
|
||||
const visible = aprsVisiblePackets();
|
||||
if (aprsTotalCountEl) {
|
||||
@@ -210,12 +215,7 @@
|
||||
const categoryClass = `aprs-badge aprs-badge-type aprs-badge-type-${category}`;
|
||||
const pathBadge = pkt.path ? `<span class="aprs-badge">${escapeAprsHtml(pkt.path)}</span>` : "";
|
||||
const crcBadge = pkt.crcOk ? "" : '<span class="aprs-badge aprs-badge-crc">CRC Fail</span>';
|
||||
let symbolHtml = "";
|
||||
if (pkt.symbolTable && pkt.symbolCode) {
|
||||
const symbol = escapeAprsHtml(pkt.symbolCode);
|
||||
const table = pkt.symbolTable === "/" ? "Primary" : "Alternate";
|
||||
symbolHtml = `<span class="aprs-symbol aprs-symbol-local" title="${table} APRS symbol ${symbol}">${symbol}</span>`;
|
||||
}
|
||||
const symbolHtml = renderLocalAprsSymbol(pkt, escapeAprsHtml);
|
||||
const posLink = pkt.lat != null && pkt.lon != null ? `<a class="aprs-pos" href="javascript:void(0)" data-aprs-map="${pkt.lat},${pkt.lon}">${pkt.lat.toFixed(4)}, ${pkt.lon.toFixed(4)}</a>` : "";
|
||||
const distance = aprsDistanceText(pkt);
|
||||
const qrzHref = `https://qrzcq.com/call/${encodeURIComponent(pkt.srcCall || "")}`;
|
||||
@@ -325,22 +325,7 @@
|
||||
scheduleAprsHistoryRender();
|
||||
}
|
||||
function normalizeServerAprsPacket(pkt) {
|
||||
return {
|
||||
rig_id: pkt.rig_id || null,
|
||||
receiver: aprsWindow.getDecodeRigMeta?.() ?? null,
|
||||
srcCall: pkt.src_call ?? "",
|
||||
destCall: pkt.dest_call ?? "",
|
||||
path: pkt.path ?? "",
|
||||
info: pkt.info ?? "",
|
||||
info_bytes: pkt.info_bytes ?? [],
|
||||
type: pkt.packet_type ?? "",
|
||||
crcOk: pkt.crc_ok ?? false,
|
||||
ts_ms: pkt.ts_ms ?? null,
|
||||
lat: pkt.lat ?? null,
|
||||
lon: pkt.lon ?? null,
|
||||
symbolTable: pkt.symbol_table ?? null,
|
||||
symbolCode: pkt.symbol_code ?? null
|
||||
};
|
||||
return normalizeAprsPacket(pkt, aprsWindow.getDecodeRigMeta?.() ?? null);
|
||||
}
|
||||
aprsWindow.onServerAprsBatch = function(packets) {
|
||||
if (!Array.isArray(packets) || packets.length === 0) return;
|
||||
|
||||
Reference in New Issue
Block a user