diff --git a/main.py b/main.py index 56368dc..146b0b4 100644 --- a/main.py +++ b/main.py @@ -217,6 +217,7 @@ async def favicon(): + # === WebSocket endpoint for live vitals === @app.websocket("/ws/vitals") async def websocket_vitals(websocket: WebSocket): diff --git a/static/dashboard.html b/static/dashboard.html index 2434271..a85ba7d 100644 --- a/static/dashboard.html +++ b/static/dashboard.html @@ -2559,6 +2559,23 @@ document.body.classList.add('minimal-mode'); } + // Helper: Calculate age in milliseconds robustly against UTC and local timezones + function getTimestampAgeMs(ts) { + if (!ts) return 999999999; + try { + let str = String(ts).trim(); + if (!str.endsWith('Z') && !str.includes('+') && !/[-+]\d{2}:\d{2}$/.test(str)) { + str = str + 'Z'; + } + const time = new Date(str).getTime(); + if (isNaN(time)) return 999999999; + return Math.abs(Date.now() - time); + } catch (e) { + return 999999999; + } + } + + function getTargetPatientBadge() { const items = []; if (targetMrn) items.push(`📋 MRN: ${targetMrn}`); @@ -3127,7 +3144,7 @@ const now = Date.now(); for (let i = readings.length - 1; i >= 0; i--) { const r = readings[i]; - const rAge = r.timestamp ? (now - new Date(r.timestamp).getTime()) : 9999999; + const rAge = getTimestampAgeMs(r.timestamp); if (rAge <= 25000) { if (r.heart_rate != null) merged.heart_rate = r.heart_rate; if (r.spo2 != null) merged.spo2 = r.spo2; @@ -3220,7 +3237,7 @@ const now = Date.now(); for (let i = readings.length - 1; i >= 0; i--) { const r = readings[i]; - const rAge = r.timestamp ? (now - new Date(r.timestamp).getTime()) : 9999999; + const rAge = getTimestampAgeMs(r.timestamp); if (rAge <= 25000) { if (r.heart_rate != null) merged.heart_rate = r.heart_rate; if (r.spo2 != null) merged.spo2 = r.spo2; @@ -3652,7 +3669,7 @@ if (readings.length > 0) { const now = Date.now(); readings.forEach(r => { - const rAge = r.timestamp ? (now - new Date(r.timestamp).getTime()) : 9999999; + const rAge = getTimestampAgeMs(r.timestamp); if (rAge > 25000) { r.heart_rate = null; r.spo2 = null; @@ -3743,7 +3760,7 @@ const devKey = r.device_id; if (devKey && connectedDevices[devKey]) { const dev = connectedDevices[devKey]; - const rAge = r.timestamp ? (now - new Date(r.timestamp).getTime()) : 9999999; + const rAge = getTimestampAgeMs(r.timestamp); if (rAge <= 25000) { if ((dev.heart_rate === undefined || dev.heart_rate === null) && r.heart_rate != null) dev.heart_rate = r.heart_rate; if ((dev.spo2 === undefined || dev.spo2 === null) && r.spo2 != null) dev.spo2 = r.spo2;