latest version

This commit is contained in:
znetsixe
2025-10-21 12:45:19 +02:00
parent 65807881d5
commit f29aa4f5af
3 changed files with 89 additions and 43 deletions

View File

@@ -88,10 +88,7 @@ class nodeClass {
}
/**
* Register this node as a child upstream and downstream.
* Delayed to avoid Node-RED startup race conditions.
*/
// init registration msg
_registerChild() {
setTimeout(() => {
this.node.send([
@@ -102,12 +99,78 @@ class nodeClass {
}, 100);
}
/**
* Start the periodic tick loop to drive the Measurement class.
*/
_updateNodeStatus() {
const ps = this.source;
try {
// --- Basin & measurements -------------------------------------------------
const maxVolBeforeOverflow = ps.basin?.maxVolOverflow ?? ps.basin?.maxVol ?? 0;
const volumeMeasurement = ps.measurements.type("volume").variant("measured").position("atEquipment");
const currentVolume = volumeMeasurement.getCurrentValue("m3") ?? 0;
const netFlowMeasurement = ps.measurements.type("netFlowRate").variant("predicted").position("atEquipment");
const netFlowM3s = netFlowMeasurement?.getCurrentValue("m3/s") ?? 0;
const netFlowM3h = netFlowM3s * 3600;
const percentFull = ps.measurements.type("volume").variant("procent").position("atEquipment").getCurrentValue() ?? 0;
// --- State information ----------------------------------------------------
const direction = ps.state?.direction || "unknown";
const secondsRemaining = ps.state?.seconds ?? null;
const timeRemaining = secondsRemaining ? `${Math.round(secondsRemaining / 60)}` : 0 + " min";
// --- Icon / colour selection ---------------------------------------------
let symbol = "❔";
let fill = "grey";
switch (direction) {
case "filling":
symbol = "⬆️";
fill = "blue";
break;
case "draining":
symbol = "⬇️";
fill = "orange";
break;
case "stable":
symbol = "⏸️";
fill = "green";
break;
default:
symbol = "❔";
fill = "grey";
break;
}
// --- Status text ----------------------------------------------------------
const textParts = [
`${symbol} ${percentFull.toFixed(1)}%`,
`V=${currentVolume.toFixed(2)} / ${maxVolBeforeOverflow.toFixed(2)}`,
`net=${netFlowM3h.toFixed(1)} m³/h`,
`t≈${timeRemaining}`
];
return {
fill,
shape: "dot",
text: textParts.join(" | ")
};
} catch (error) {
this.node.error("Error in updateNodeStatus: " + error.message);
return { fill: "red", shape: "ring", text: "Status Error" };
}
}
// any time based functions here
_startTickLoop() {
setTimeout(() => {
this._tickInterval = setInterval(() => this._tick(), 1000);
// Update node status on nodered screen every second ( this is not the best way to do this, but it works for now)
this._statusInterval = setInterval(() => {
const status = this._updateNodeStatus();
this.node.status(status);
}, 1000);
}, 1000);
}
@@ -156,7 +219,7 @@ class nodeClass {
_attachCloseHandler() {
this.node.on('close', (done) => {
clearInterval(this._tickInterval);
//clearInterval(this._statusInterval);
clearInterval(this._statusInterval);
done();
});
}