Files
corescope-rx/src/recent.js
efitenandClaude Opus 4.8 d65b923c10 feat: auto-discover + Settings tab + Home monitor, fix stuck-uploads drain (v1.3.0)
Home becomes a pure monitor and a new Settings tab holds config/diagnostics.

- Discover now runs automatically while connected with a 15 s traffic backoff
  (an overheard 2-byte forwarder/advert means a busy channel — don't poll on top
  of it); standstill still suspends it. The Flood probe is removed entirely.
- New Settings tab: CoreScope broker status + "Push pending now", companion info,
  diagnostics (verbose, debug log, share/mail the log via Web Share API).
- Home enriched: nodes/hex/rx counters, status strip (GPS accuracy, pending,
  upload health + last-upload age, capture rate), last-reception SNR peak-hold
  meter, discover status line.
- Fix the +60-pending-on-WiFi bug: publish() now times out instead of hanging on
  a dead socket, drainLoop re-arms in finally so it can never die, MQTT keepalive
  lowered, and a drain is kicked on broker reconnect / window online.

Pure logic in src/monitor.js + src/sharelog.js with tests. SW cache v7→v8.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-20 15:20:35 +02:00

52 lines
2.5 KiB
JavaScript

// Pure merge logic for the "recently heard nodes" list.
//
// A node can be heard under several key representations: the 2-byte path hash
// (rxlog, path[last]) and the pubkey (32-byte advert, or 8-byte discover prefix). In MeshCore the
// path hash is the leading byte(s) of the node's public key, so one key is a prefix of
// the other when they are the same node — exactly how names.js resolves prefixes.
// We collapse prefix-related entries into one row: longest (most-specific) key kept,
// counts summed, newest reception shown. No DOM, no network — unit-testable.
// sameNode reports whether two lowercase-hex heard keys refer to the same node,
// i.e. one is a prefix of the other (or they are equal). Case-insensitive.
export function sameNode(a, b) {
const x = String(a).toLowerCase();
const y = String(b).toLowerCase();
return x === y || x.startsWith(y) || y.startsWith(x);
}
// addNodeKey tracks the set of DISTINCT nodes heard this session for the Home
// counter. Keys that are prefix-related (same node under a path-hash vs pubkey) are
// collapsed onto the longest (most-specific) key. Mutates and returns `keys`.
export function addNodeKey(keys, key) {
const k = String(key).toLowerCase();
for (let i = 0; i < keys.length; i++) {
if (sameNode(keys[i], k)) { if (k.length > keys[i].length) keys[i] = k; return keys; }
}
keys.push(k);
return keys;
}
// upsertHeard returns a NEW list with `reception` merged in, most-recent first,
// capped at `max`. reception = { key, keylen, snr, rssi, src, now }.
// Any existing entries that refer to the same node (prefix-related) are merged into
// a single entry at the front. A carried `name`/`_req` (set by the caller after name
// resolution) is preserved so resolution is not re-triggered needlessly.
export function upsertHeard(list, reception, max) {
const { key, keylen, snr, rssi, src, now } = reception;
const matches = list.filter((e) => sameNode(e.key, key));
const rest = list.filter((e) => !sameNode(e.key, key));
// Identity/count come from the merged set; snr/rssi/src/last are the new reception's.
const merged = { key, keylen, count: 0, snr, rssi, src, last: now };
for (const m of matches) {
merged.count += m.count;
if (m.key.length > merged.key.length) { merged.key = m.key; merged.keylen = m.keylen; }
// A resolved name is per-node, so any match's name is valid; later matches win (harmless).
if (m.name !== undefined) merged.name = m.name;
if (m._req) merged._req = m._req;
}
merged.count += 1;
return [merged, ...rest].slice(0, max);
}