Strictly eliminate fake and stale vitals and enforce machine-to-MRN isolation
This commit is contained in:
+90
-37
@@ -3107,11 +3107,26 @@
|
||||
const readings = await readResp.json();
|
||||
if (readings && readings.length > 0) {
|
||||
const merged = {};
|
||||
const now = Date.now();
|
||||
for (let i = readings.length - 1; i >= 0; i--) {
|
||||
const r = readings[i];
|
||||
for (const k in r) {
|
||||
if (r[k] !== null && r[k] !== undefined) merged[k] = r[k];
|
||||
const rAge = r.timestamp ? (now - new Date(r.timestamp).getTime()) : 9999999;
|
||||
if (rAge <= 25000) {
|
||||
if (r.heart_rate != null) merged.heart_rate = r.heart_rate;
|
||||
if (r.spo2 != null) merged.spo2 = r.spo2;
|
||||
if (r.respiratory_rate != null) merged.respiratory_rate = r.respiratory_rate;
|
||||
if (r.temperature != null) merged.temperature = r.temperature;
|
||||
if (r.temperature2 != null) merged.temperature2 = r.temperature2;
|
||||
if (r.temperature_diff != null) merged.temperature_diff = r.temperature_diff;
|
||||
}
|
||||
if (rAge <= 900000) {
|
||||
if (r.systolic_bp != null) merged.systolic_bp = r.systolic_bp;
|
||||
if (r.diastolic_bp != null) merged.diastolic_bp = r.diastolic_bp;
|
||||
if (r.map_bp != null) merged.map_bp = r.map_bp;
|
||||
}
|
||||
if (r.patient_name) merged.patient_name = r.patient_name;
|
||||
if (r.mrn || r.patient_id) merged.mrn = r.mrn || r.patient_id;
|
||||
if (r.bed_id) merged.bed_id = r.bed_id;
|
||||
}
|
||||
applyVitalsToMachine(merged);
|
||||
return;
|
||||
@@ -3178,18 +3193,33 @@
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Fallback: Fetch latest readings directly from DB (merge up to 10 recent readings so all active parameters display)
|
||||
// 2. Fallback: Fetch latest readings directly from DB (only accept recent readings)
|
||||
if (targetId) {
|
||||
const readResp = await fetch(`${API_BASE}/latest-readings?mrn=${encodeURIComponent(targetId)}&limit=10`);
|
||||
if (readResp.ok) {
|
||||
const readings = await readResp.json();
|
||||
if (readings && readings.length > 0) {
|
||||
const merged = {};
|
||||
const now = Date.now();
|
||||
for (let i = readings.length - 1; i >= 0; i--) {
|
||||
const r = readings[i];
|
||||
for (const k in r) {
|
||||
if (r[k] !== null && r[k] !== undefined) merged[k] = r[k];
|
||||
const rAge = r.timestamp ? (now - new Date(r.timestamp).getTime()) : 9999999;
|
||||
if (rAge <= 25000) {
|
||||
if (r.heart_rate != null) merged.heart_rate = r.heart_rate;
|
||||
if (r.spo2 != null) merged.spo2 = r.spo2;
|
||||
if (r.respiratory_rate != null) merged.respiratory_rate = r.respiratory_rate;
|
||||
if (r.temperature != null) merged.temperature = r.temperature;
|
||||
if (r.temperature2 != null) merged.temperature2 = r.temperature2;
|
||||
if (r.temperature_diff != null) merged.temperature_diff = r.temperature_diff;
|
||||
}
|
||||
if (rAge <= 900000) {
|
||||
if (r.systolic_bp != null) merged.systolic_bp = r.systolic_bp;
|
||||
if (r.diastolic_bp != null) merged.diastolic_bp = r.diastolic_bp;
|
||||
if (r.map_bp != null) merged.map_bp = r.map_bp;
|
||||
}
|
||||
if (r.patient_name) merged.patient_name = r.patient_name;
|
||||
if (r.mrn || r.patient_id) merged.mrn = r.mrn || r.patient_id;
|
||||
if (r.bed_id) merged.bed_id = r.bed_id;
|
||||
}
|
||||
applyVitalsToMachine(merged);
|
||||
}
|
||||
@@ -3204,46 +3234,48 @@
|
||||
if (!data) return;
|
||||
|
||||
// Heart Rate
|
||||
if (data.heart_rate !== undefined && data.heart_rate !== null) {
|
||||
machVitals.heart_rate = (data.heart_rate > 0) ? Math.round(data.heart_rate) : null;
|
||||
if ('heart_rate' in data) {
|
||||
machVitals.heart_rate = (data.heart_rate !== null && data.heart_rate !== undefined && data.heart_rate > 0) ? Math.round(data.heart_rate) : null;
|
||||
}
|
||||
|
||||
// SpO2
|
||||
if (data.spo2 !== undefined && data.spo2 !== null) {
|
||||
machVitals.spo2 = (data.spo2 > 0) ? Math.round(data.spo2) : null;
|
||||
if ('spo2' in data) {
|
||||
machVitals.spo2 = (data.spo2 !== null && data.spo2 !== undefined && data.spo2 > 0) ? Math.round(data.spo2) : null;
|
||||
}
|
||||
|
||||
// Respiration Rate
|
||||
if (data.respiratory_rate !== undefined && data.respiratory_rate !== null) {
|
||||
machVitals.respiratory_rate = (data.respiratory_rate > 0) ? Math.round(data.respiratory_rate) : null;
|
||||
if ('respiratory_rate' in data) {
|
||||
machVitals.respiratory_rate = (data.respiratory_rate !== null && data.respiratory_rate !== undefined && data.respiratory_rate > 0) ? Math.round(data.respiratory_rate) : null;
|
||||
}
|
||||
|
||||
// NIBP
|
||||
if (data.systolic_bp !== undefined && data.systolic_bp !== null) {
|
||||
machVitals.systolic_bp = (data.systolic_bp > 0) ? Math.round(data.systolic_bp) : null;
|
||||
if ('systolic_bp' in data) {
|
||||
machVitals.systolic_bp = (data.systolic_bp !== null && data.systolic_bp !== undefined && data.systolic_bp > 0) ? Math.round(data.systolic_bp) : null;
|
||||
}
|
||||
if (data.diastolic_bp !== undefined && data.diastolic_bp !== null) {
|
||||
machVitals.diastolic_bp = (data.diastolic_bp > 0) ? Math.round(data.diastolic_bp) : null;
|
||||
if ('diastolic_bp' in data) {
|
||||
machVitals.diastolic_bp = (data.diastolic_bp !== null && data.diastolic_bp !== undefined && data.diastolic_bp > 0) ? Math.round(data.diastolic_bp) : null;
|
||||
}
|
||||
if (data.map_bp !== undefined && data.map_bp !== null) {
|
||||
machVitals.map_bp = (data.map_bp > 0) ? Math.round(data.map_bp) : null;
|
||||
if ('map_bp' in data) {
|
||||
machVitals.map_bp = (data.map_bp !== null && data.map_bp !== undefined && data.map_bp > 0) ? Math.round(data.map_bp) : null;
|
||||
}
|
||||
|
||||
// Temperature 1 (T1)
|
||||
if (data.temperature !== undefined && data.temperature !== null) {
|
||||
machVitals.temperature = (data.temperature > 0) ? Number(data.temperature) : null;
|
||||
if ('temperature' in data) {
|
||||
machVitals.temperature = (data.temperature !== null && data.temperature !== undefined && data.temperature > 0) ? Number(data.temperature) : null;
|
||||
}
|
||||
|
||||
// Temperature 2 (T2)
|
||||
if (data.temperature2 !== undefined && data.temperature2 !== null) {
|
||||
machVitals.temperature2 = (data.temperature2 > 0) ? Number(data.temperature2) : null;
|
||||
if ('temperature2' in data) {
|
||||
machVitals.temperature2 = (data.temperature2 !== null && data.temperature2 !== undefined && data.temperature2 > 0) ? Number(data.temperature2) : null;
|
||||
}
|
||||
|
||||
// Temperature Difference (TD)
|
||||
if (data.temperature_diff !== undefined && data.temperature_diff !== null) {
|
||||
machVitals.temperature_diff = Number(data.temperature_diff);
|
||||
if ('temperature_diff' in data) {
|
||||
machVitals.temperature_diff = (data.temperature_diff !== null && data.temperature_diff !== undefined) ? Number(data.temperature_diff) : null;
|
||||
} else if (machVitals.temperature != null && machVitals.temperature2 != null) {
|
||||
machVitals.temperature_diff = Math.round(Math.abs(machVitals.temperature - machVitals.temperature2) * 10) / 10;
|
||||
} else {
|
||||
machVitals.temperature_diff = null;
|
||||
}
|
||||
|
||||
if (data.patient_name && data.patient_name !== 'Unknown' && data.patient_name !== 'UNKNOWN') machVitals.patient_name = data.patient_name;
|
||||
@@ -3601,7 +3633,22 @@
|
||||
|
||||
const readings = await response.json();
|
||||
if (readings.length > 0) {
|
||||
const now = Date.now();
|
||||
readings.forEach(r => {
|
||||
const rAge = r.timestamp ? (now - new Date(r.timestamp).getTime()) : 9999999;
|
||||
if (rAge > 25000) {
|
||||
r.heart_rate = null;
|
||||
r.spo2 = null;
|
||||
r.respiratory_rate = null;
|
||||
r.temperature = null;
|
||||
r.temperature2 = null;
|
||||
r.temperature_diff = null;
|
||||
}
|
||||
if (rAge > 900000) {
|
||||
r.systolic_bp = null;
|
||||
r.diastolic_bp = null;
|
||||
r.map_bp = null;
|
||||
}
|
||||
if (r.id !== lastReadingId) {
|
||||
lastReadingId = r.id;
|
||||
readingCount++;
|
||||
@@ -3674,17 +3721,23 @@
|
||||
const readResp = await fetch(backfillUrl);
|
||||
if (readResp.ok) {
|
||||
const readings = await readResp.json();
|
||||
const now = Date.now();
|
||||
readings.forEach(r => {
|
||||
const devKey = r.device_id;
|
||||
if (devKey && connectedDevices[devKey]) {
|
||||
const dev = connectedDevices[devKey];
|
||||
if ((dev.heart_rate === undefined || dev.heart_rate === null) && r.heart_rate !== null && r.heart_rate !== undefined) dev.heart_rate = r.heart_rate;
|
||||
if ((dev.spo2 === undefined || dev.spo2 === null) && r.spo2 !== null && r.spo2 !== undefined) dev.spo2 = r.spo2;
|
||||
if ((dev.systolic_bp === undefined || dev.systolic_bp === null) && r.systolic_bp !== null && r.systolic_bp !== undefined) dev.systolic_bp = r.systolic_bp;
|
||||
if ((dev.diastolic_bp === undefined || dev.diastolic_bp === null) && r.diastolic_bp !== null && r.diastolic_bp !== undefined) dev.diastolic_bp = r.diastolic_bp;
|
||||
if ((dev.map_bp === undefined || dev.map_bp === null) && r.map_bp !== null && r.map_bp !== undefined) dev.map_bp = r.map_bp;
|
||||
if ((dev.respiratory_rate === undefined || dev.respiratory_rate === null) && r.respiratory_rate !== null && r.respiratory_rate !== undefined) dev.respiratory_rate = r.respiratory_rate;
|
||||
if ((dev.temperature === undefined || dev.temperature === null) && r.temperature !== null && r.temperature !== undefined) dev.temperature = r.temperature;
|
||||
const rAge = r.timestamp ? (now - new Date(r.timestamp).getTime()) : 9999999;
|
||||
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;
|
||||
if ((dev.respiratory_rate === undefined || dev.respiratory_rate === null) && r.respiratory_rate != null) dev.respiratory_rate = r.respiratory_rate;
|
||||
if ((dev.temperature === undefined || dev.temperature === null) && r.temperature != null) dev.temperature = r.temperature;
|
||||
}
|
||||
if (rAge <= 900000) {
|
||||
if ((dev.systolic_bp === undefined || dev.systolic_bp === null) && r.systolic_bp != null) dev.systolic_bp = r.systolic_bp;
|
||||
if ((dev.diastolic_bp === undefined || dev.diastolic_bp === null) && r.diastolic_bp != null) dev.diastolic_bp = r.diastolic_bp;
|
||||
if ((dev.map_bp === undefined || dev.map_bp === null) && r.map_bp != null) dev.map_bp = r.map_bp;
|
||||
}
|
||||
if (!dev.lastSeen && r.timestamp) dev.lastSeen = new Date(r.timestamp);
|
||||
}
|
||||
});
|
||||
@@ -4083,13 +4136,13 @@
|
||||
connectedDevices[devId] = {
|
||||
...existing,
|
||||
...data,
|
||||
heart_rate: retainVital('heart_rate', 60000),
|
||||
spo2: retainVital('spo2', 60000),
|
||||
temperature: retainVital('temperature', 60000),
|
||||
respiratory_rate: retainVital('respiratory_rate', 60000),
|
||||
systolic_bp: retainVital('systolic_bp', 3600000),
|
||||
diastolic_bp: retainVital('diastolic_bp', 3600000),
|
||||
map_bp: retainVital('map_bp', 3600000),
|
||||
heart_rate: retainVital('heart_rate', 15000),
|
||||
spo2: retainVital('spo2', 15000),
|
||||
temperature: retainVital('temperature', 20000),
|
||||
respiratory_rate: retainVital('respiratory_rate', 15000),
|
||||
systolic_bp: retainVital('systolic_bp', 900000),
|
||||
diastolic_bp: retainVital('diastolic_bp', 900000),
|
||||
map_bp: retainVital('map_bp', 900000),
|
||||
patient_id: (data.mrn && data.mrn !== 'UNKNOWN') ? data.mrn : (existing.patient_id || 'UNKNOWN'),
|
||||
mrn: (data.mrn && data.mrn !== 'UNKNOWN') ? data.mrn : (existing.mrn || 'UNKNOWN'),
|
||||
patient_name: (data.patient_name && data.patient_name !== 'Unknown') ? data.patient_name : (existing.patient_name || null),
|
||||
|
||||
Reference in New Issue
Block a user