/** * Shared CWMP event normalization and priority resolution utilities. * Single source of truth for event code handling (fixes MINEUR-9). */ export const EVENT_PRIORITY: Record = { 'BOOTSTRAP': 100, 'BOOT': 90, 'CONNECTION REQUEST': 80, 'PERIODIC': 70, 'VALUE CHANGE': 60, 'DIAGNOSTICS COMPLETE': 50, 'TRANSFER COMPLETE': 40, 'SCHEDULE': 30, } /** * Normalize an EventCode from an Inform message. * Strips numeric prefix and normalizes the label. * Examples: '0 BOOTSTRAP' → 'BOOTSTRAP', '1 BOOT' → 'BOOT', '6' → 'CONNECTION REQUEST' */ export function normalizeEventCode(raw: string): string { const trimmed = raw.trim().toUpperCase() // Already a known label without numeric prefix if (EVENT_PRIORITY[trimmed] !== undefined) return trimmed // Full format like "0 BOOTSTRAP" → strip numeric prefix const match = trimmed.match(/^\d+\s+(.+)$/) if (match) return match[1] // Numeric-only → expand to standard label switch (trimmed) { case '0': return 'BOOTSTRAP' case '1': return 'BOOT' case '2': return 'PERIODIC' case '3': return 'SCHEDULED' case '4': return 'VALUE CHANGE' case '5': return 'KICKED' case '6': return 'CONNECTION REQUEST' case '7': return 'TRANSFER COMPLETE' case '8': return 'DIAGNOSTICS COMPLETE' default: return trimmed } } /** * Resolve the highest priority event from a list of raw event codes. * Returns the normalized name of the highest-priority event. */ export function resolveHighestPriorityEvent(events: string[]): string { if (events.length === 0) return '' let highest = '' let highestPriority = -1 for (const raw of events) { const normalized = normalizeEventCode(raw) const priority = EVENT_PRIORITY[normalized] ?? 0 if (priority > highestPriority) { highestPriority = priority highest = normalized } } return highest }