Changes to integrate qconso and fix minor exisiting bugs

This commit is contained in:
aniketSaha 2025-08-06 15:29:41 +02:00
parent 158daccb3e
commit 365162bd65
5 changed files with 42 additions and 17 deletions

16
main.py
View File

@ -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}")

View File

@ -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' }
], {

View File

@ -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);
};
</script>
</body>

View File

@ -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);

View File

@ -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<br>${dsData.TankLevel.toFixed(1)}`;
tankLevelElement.innerHTML = `<br>${dsData.TankLevel.toFixed(1)}`;
} else {
console.error('Element with selector "#TankLevel .monitor-value" not found.');
}
if (qconsoElement) {
qconsoElement.innerHTML = `DS<br>${dsData.Qconso.toFixed(1)} L/h`;
qconsoElement.innerHTML = `<br>${dsData.Qconso.toFixed(1)} L/h`;
} else {
console.error('Element with selector "#Qconso .monitor-value" not found.');
}