diff --git a/main.py b/main.py index d23cf92..70d0f1d 100644 --- a/main.py +++ b/main.py @@ -106,12 +106,10 @@ def format_PU_data(data): def format_DS_data(data): return { "timestamp": datetime.datetime.now().isoformat(), - "Q_conso_filt": np.round(data.get("Qdrain_sp", 0.0), 1), + "Qconso": np.round(data.get("Qdrain_sp", 0.0), 1), "TankLevel": np.round(data.get("TankLevel", 0.0), 2), "Qinlet": np.round(data.get("Inlet_flow", 0.0), 1), "Qoutlet": np.round(data.get("Outlet_flow", 0.0), 1), - "Qconso": np.round(data.get("Inlet_flow", 0.0), 1) - - np.round(data.get("Outlet_flow", 0.0), 1), } @@ -313,7 +311,14 @@ async def start_recording(): filepath = os.path.join("recordings", filename) recording_file = open(filepath, "w", newline="") - fieldnames = ["timestamp", "pu", "QSkid"] + list(format_PU_data({}).keys()) + fieldnames_common = ["timestamp", "pu", "QSkid"] + fieldnames_DS = list(format_DS_data({}).keys()) + fieldnames_DS.pop(0) # .pop(0) removing extra timestamp + fieldnames_PUs = list(format_PU_data({}).keys()) + fieldnames_PUs.pop(0) # .pop(0) removing extra timestamp + + fieldnames = fieldnames_common + fieldnames_DS + fieldnames_PUs + recording_writer = csv.DictWriter(recording_file, fieldnames=fieldnames) recording_writer.writeheader() @@ -446,9 +451,6 @@ async def update_latest_flow(): latest_flow = int(data["log"]["flow"]) logging.debug(f"Updated flow: {latest_flow}") latest_data["PatientSkid"]["QSkid"] = latest_flow - # for index in active_PUs : - # logging.debug("PU_"+str(index)) - # latest_data["PU_"+str(index)]["QSkid"] = latest_flow # Adding the data to all actives PUs except Exception as e: logging.error(f"Error fetching flow: {e}") diff --git a/static/monitor_DS.html b/static/monitor_DS.html index 8a2855e..82fc9cc 100644 --- a/static/monitor_DS.html +++ b/static/monitor_DS.html @@ -65,7 +65,7 @@ Plotly.extendTraces('flow-plot', { x: [[t], [t], [t]], - y: [[dsData.Q_conso_filt], [dsData.Qinlet], [dsData.Qoutlet]] + y: [[dsData.Qconso], [dsData.Qinlet], [dsData.Qoutlet]] }, [0, 1, 2], maxPoints); } catch (e) { @@ -97,7 +97,7 @@ }); Plotly.newPlot('flow-plot', [ - { x: time0, y: [0], name: 'Q_conso_filt', mode: 'lines' }, + { x: time0, y: [0], name: 'Qconso', mode: 'lines' }, { x: time0, y: [0], name: 'Qinlet', mode: 'lines' }, { x: time0, y: [0], name: 'Qoutlet', mode: 'lines' } ], { diff --git a/static/monitor_PU.html b/static/monitor_PU.html index 03ed292..eae7603 100644 --- a/static/monitor_PU.html +++ b/static/monitor_PU.html @@ -79,6 +79,7 @@ const allData = await response.json(); const puData = allData[`PU_${puNumber}`]; const SkidData = allData[`PatientSkid`]; + const DSData = allData[`DS`]; const t = new Date(puData.timestamp); Plotly.extendTraces('flow-plot-1', { @@ -87,9 +88,9 @@ }, [0, 1], maxPoints); Plotly.extendTraces('flow-plot-2', { - x: [[t], [t], [t], [t]], - y: [[puData.Qdrain], [puData.Qrecirc], [SkidData.QSkid], [puData.QdrainEDI]] - }, [0, 1, 2, 3], maxPoints); + x: [[t], [t], [t], [t], [t]], + y: [[puData.Qdrain], [puData.Qrecirc], [SkidData.QSkid], [puData.QdrainEDI], [DSData.Qconso]] + }, [0, 1, 2, 3, 4], maxPoints); Plotly.extendTraces('pressure-plot-1', { x: [[t], [t]], @@ -168,7 +169,9 @@ { x: time0, y: [0], name: 'Qdrain', mode: 'lines' }, { x: time0, y: [0], name: 'Qrecirc', mode: 'lines' }, { x: time0, y: [0], name: 'QSkid', mode: 'lines' }, - { x: time0, y: [0], name: 'QdrainEDI', mode: 'lines' } + { x: time0, y: [0], name: 'QdrainEDI', mode: 'lines' }, + { x: time0, y: [0], name: 'Qconso', mode: 'lines' }, + ], { title: 'Other Flows', xaxis: { type: 'date' }, yaxis: { title: 'Flow (L/h)' } }); @@ -247,7 +250,7 @@ window.onload = function () { initPlots(); fetchPUStatus(); - setInterval(fetchPUStatus, 5000); + setInterval(fetchPUStatus, 1000); }; diff --git a/static/multi_pu_dashboard.html b/static/multi_pu_dashboard.html index 49cac05..1744dff 100644 --- a/static/multi_pu_dashboard.html +++ b/static/multi_pu_dashboard.html @@ -94,6 +94,17 @@ function initAllPlots() { }); } + if (plot.id === 'Qdrain-plot') { + data.push({ + x: time0.slice(), + y: zero.slice(), + name: 'Qconso', + mode: 'lines', + line: { color: 'black', width: 2, dash: 'dot' }, + legendgroup: 'Consumption' + }); + } + Plotly.newPlot(plot.id, data, layout); }); } @@ -107,6 +118,7 @@ async function updateAllPlots() { // SkidData is only fetched once const SkidData = allData["PatientSkid"] || {}; + const DSData = allData["DS"]; plots.forEach(plot => { @@ -138,6 +150,14 @@ async function updateAllPlots() { Plotly.extendTraces(plot.id, { x: skidX, y: skidY }, [qSkidTraceIndex], maxPoints); } + if (plot.id === 'Qdrain-plot') { + const Qconso = DSData["Qconso"]; + const consoX = [[timestamp]]; + const consoY = [[Qconso !== undefined ? Qconso : null]]; + const QconsoTraceIndex = puList.length + (plot.ref !== undefined ? 1 : 0); // last trace index + Plotly.extendTraces(plot.id, { x: consoX, y: consoY }, [QconsoTraceIndex], maxPoints); + } + }); } catch (err) { console.error("Failed to update plots:", err); diff --git a/templates/control.html b/templates/control.html index 595c0de..6861483 100644 --- a/templates/control.html +++ b/templates/control.html @@ -486,7 +486,7 @@ } fetchPUStatus(); - setInterval(fetchPUStatus, 5000); + setInterval(fetchPUStatus, 1000); async function updateMonitorData() { const response = await fetch('/monitor'); @@ -543,13 +543,13 @@ const qconsoElement = document.querySelector("#Qconso .monitor-value"); if (tankLevelElement) { - tankLevelElement.innerHTML = `DS
${dsData.TankLevel.toFixed(1)}`; + tankLevelElement.innerHTML = `
${dsData.TankLevel.toFixed(1)}`; } else { console.error('Element with selector "#TankLevel .monitor-value" not found.'); } if (qconsoElement) { - qconsoElement.innerHTML = `DS
${dsData.Qconso.toFixed(1)} L/h`; + qconsoElement.innerHTML = `
${dsData.Qconso.toFixed(1)} L/h`; } else { console.error('Element with selector "#Qconso .monitor-value" not found.'); }