collabSupport/services/dectStatus/buildHandsetContext.js
jmcqueen 2b8c4e06aa Improve /dectstatus with handset RF context and cleaner base cards.
Surface handset registrations and RSSI, tighten reboot health to 7 days,
and consolidate Base / Handsets & RF / Network & RTP sections.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-28 09:02:55 -04:00

70 lines
2 KiB
JavaScript

// services/dectStatus/buildHandsetContext.js
//
// Group Webex DECT handset inventory for /dectstatus rendering.
/**
* @param {object|null} phoneData collectPhoneStatus() output
* @returns {object}
*/
export function buildHandsetContext(phoneData) {
const basestations = Array.isArray(phoneData?.dectBasestations)
? phoneData.dectBasestations
: [];
const handsets = Array.isArray(phoneData?.dectHandsets)
? phoneData.dectHandsets
: [];
const handsetsByWebexId = new Map();
const linesRegisteredByWebexId = new Map();
const webexIdByMac = new Map();
for (const base of basestations) {
if (base.id) {
handsetsByWebexId.set(base.id, []);
linesRegisteredByWebexId.set(base.id, base.linesRegistered ?? null);
}
const mac = normalizeMacLoose(base.mac);
if (base.id && mac) webexIdByMac.set(mac, base.id);
}
const unassignedHandsets = [];
for (const h of handsets) {
const baseId = h.baseStationId || null;
if (baseId && handsetsByWebexId.has(baseId)) {
handsetsByWebexId.get(baseId).push(h);
} else if (!baseId) {
unassignedHandsets.push(h);
} else {
// Assigned to a base id we don't know — treat as unassigned for display.
unassignedHandsets.push(h);
}
}
return {
handsetsByWebexId,
linesRegisteredByWebexId,
webexIdByMac,
unassignedHandsets,
dectNetwork: phoneData?.dectNetwork || null,
};
}
/**
* Handsets registered to a base (by Webex base id).
*
* @param {object} handsetCtx buildHandsetContext() output
* @param {string|null} webexId
* @returns {object[]}
*/
export function handsetsForBase(handsetCtx, webexId) {
if (!webexId || !handsetCtx?.handsetsByWebexId) return [];
return handsetCtx.handsetsByWebexId.get(webexId) || [];
}
function normalizeMacLoose(mac) {
if (!mac || typeof mac !== 'string') return null;
const hex = mac.replace(/[^0-9a-fA-F]/g, '').toLowerCase();
if (hex.length !== 12) return null;
return hex.match(/../g).join(':');
}