Compare commits
7 Commits
52d3889fbc
...
basin-docs
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e2ebb31816 | ||
|
|
6ab585bcc2 | ||
|
|
d8490aa949 | ||
|
|
6b46a8a8f0 | ||
|
|
62bc73f2f9 | ||
|
|
de9a79b888 | ||
|
|
8a6ca1baeb |
57
CONTRACT.md
57
CONTRACT.md
@@ -1,57 +0,0 @@
|
|||||||
# pumpingStation — Contract
|
|
||||||
|
|
||||||
Hand-maintained for Phase 2; the `## Inputs` table is generated from
|
|
||||||
`src/commands/index.js` (see Phase 9 generator). Keep ≤ 80 lines.
|
|
||||||
|
|
||||||
## Inputs (msg.topic on Port 0)
|
|
||||||
|
|
||||||
| Canonical | Aliases (deprecated) | Payload | Effect |
|
|
||||||
|---|---|---|---|
|
|
||||||
| `set.mode` | `changemode` | `string` — one of `manual`, `levelbased`, `flowbased`, `none` | Switches the control strategy. |
|
|
||||||
| `child.register` | `registerChild` | `string` — the child node's Node-RED id | Resolves the child via `RED.nodes.getNode` and registers it through `childRegistrationUtils` at the supplied `msg.positionVsParent`. |
|
|
||||||
| `cmd.calibrate.volume` | `calibratePredictedVolume` | numeric (number or numeric string) — m³ | Resets the predicted-volume series and seeds it with the supplied value; recomputes level. |
|
|
||||||
| `cmd.calibrate.level` | `calibratePredictedLevel` | numeric — metres | Resets the predicted-level series and seeds it with the supplied value; recomputes volume. |
|
|
||||||
| `set.inflow` | `q_in` | number, numeric string, or `{ value, unit, timestamp }` | Pushes a manual inflow measurement onto the predicted-flow series. `unit` may be on the message (`msg.unit`) or inside the object payload. |
|
|
||||||
| `set.demand` | `Qd` | numeric — child setpoint demand | Forwards the demand to direct children (machineGroups / machines / stations). Only honoured in `manual` mode; in other modes the call is logged at `debug` and discarded. |
|
|
||||||
|
|
||||||
Aliases log a one-time deprecation warning the first time they fire.
|
|
||||||
|
|
||||||
## Outputs (msg.topic on Port 0/1/2)
|
|
||||||
|
|
||||||
- **Port 0 (process):** `msg.topic = config.general.name`. Payload built by
|
|
||||||
`outputUtils.formatMsg(..., 'process')` from `getOutput()` — delta-compressed
|
|
||||||
(only changed fields are emitted).
|
|
||||||
- **Port 1 (InfluxDB telemetry):** same shape as Port 0, formatted with the
|
|
||||||
`'influxdb'` formatter.
|
|
||||||
- **Port 2 (registration):** at startup the node sends one
|
|
||||||
`{ topic: 'registerChild', payload: <node.id>, positionVsParent, distance }`
|
|
||||||
to the upstream parent.
|
|
||||||
|
|
||||||
## Events emitted by `source.measurements.emitter`
|
|
||||||
|
|
||||||
The `MeasurementContainer` fires `<type>.<variant>.<position>` whenever
|
|
||||||
the corresponding series receives a new value. Parents subscribe via the
|
|
||||||
generic `child.measurements.emitter.on(eventName, ...)` handshake.
|
|
||||||
pumpingStation publishes:
|
|
||||||
|
|
||||||
- `volume.predicted.atequipment` — basin volume integrator output (m³).
|
|
||||||
- `level.predicted.atequipment` — basin level (m), recomputed from volume.
|
|
||||||
- `flow.predicted.in` (childed `manual-qin`) — manual inflow injections.
|
|
||||||
- `volume.measured.atequipment`, `level.measured.<position>`,
|
|
||||||
`pressure.measured.<position>`, `temperature.measured.atequipment`,
|
|
||||||
`flow.predicted.<in|out>` (childed by upstream child id) — when a
|
|
||||||
matching child measurement arrives.
|
|
||||||
|
|
||||||
The exact set is data-driven by which children register and what they
|
|
||||||
publish; downstream consumers should subscribe by event name, not assume
|
|
||||||
a fixed catalogue.
|
|
||||||
|
|
||||||
## Children registered by this node
|
|
||||||
|
|
||||||
pumpingStation acts as a parent for `measurement`, `machine`, `machinegroup`,
|
|
||||||
and `pumpingstation` software types. Position labels accepted from
|
|
||||||
children are `upstream`, `downstream`, `atequipment` (and the synonyms
|
|
||||||
`in` / `out` for predicted-flow children). Child-registration plumbing is
|
|
||||||
documented in `MODULE_SPLIT.md`; this node does not receive children
|
|
||||||
through Port 0 input — registration arrives on Port 2 from the child via
|
|
||||||
the standard `childRegistrationUtils` handshake.
|
|
||||||
@@ -5,5 +5,6 @@ Wet-well basin model and pump orchestration node for EVOLV.
|
|||||||
The detailed documentation lives in [`wiki/`](wiki/):
|
The detailed documentation lives in [`wiki/`](wiki/):
|
||||||
|
|
||||||
- [`wiki/functional-description.md`](wiki/functional-description.md) defines the shared basin model, pipe reference semantics, safety points, net-flow selection, and child registration behaviour.
|
- [`wiki/functional-description.md`](wiki/functional-description.md) defines the shared basin model, pipe reference semantics, safety points, net-flow selection, and child registration behaviour.
|
||||||
- [`wiki/modes/`](wiki/modes/) documents control-mode-specific behaviour such as the level-linear `startLevel` demand ramp.
|
- [`wiki/modes/`](wiki/modes/) documents control-mode-specific behaviour. For v1.0 the editor exposes `levelbased` and `manual`; levelbased supports linear and log curves with separate rising/falling ramp semantics.
|
||||||
- [`wiki/diagrams/basin-model.drawio.svg`](wiki/diagrams/basin-model.drawio.svg) is the current source of truth for the generic basin model.
|
- [`wiki/diagrams/basin-model.drawio.svg`](wiki/diagrams/basin-model.drawio.svg) is the current source of truth for the generic basin model.
|
||||||
|
- [`examples/basic-dashboard.flow.json`](examples/basic-dashboard.flow.json) provides a simple Node-RED Dashboard 2 flow with level, volume, demand, net-flow, and safety-state trends.
|
||||||
|
|||||||
589
examples/basic-dashboard.flow.json
Normal file
589
examples/basic-dashboard.flow.json
Normal file
@@ -0,0 +1,589 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"id": "ps_tab_basic_dashboard",
|
||||||
|
"type": "tab",
|
||||||
|
"label": "PumpingStation Dashboard",
|
||||||
|
"disabled": false,
|
||||||
|
"info": "Basic level-based pumpingStation dashboard with basin trends and safety state."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ui_base_ps_basic",
|
||||||
|
"type": "ui-base",
|
||||||
|
"name": "EVOLV Demo",
|
||||||
|
"path": "/dashboard",
|
||||||
|
"appIcon": "",
|
||||||
|
"includeClientData": true,
|
||||||
|
"acceptsClientConfig": [
|
||||||
|
"ui-notification",
|
||||||
|
"ui-control"
|
||||||
|
],
|
||||||
|
"showPathInSidebar": false,
|
||||||
|
"headerContent": "page",
|
||||||
|
"navigationStyle": "default",
|
||||||
|
"titleBarStyle": "default"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ui_theme_ps_basic",
|
||||||
|
"type": "ui-theme",
|
||||||
|
"name": "EVOLV Pumping Theme",
|
||||||
|
"colors": {
|
||||||
|
"surface": "#ffffff",
|
||||||
|
"primary": "#0c99d9",
|
||||||
|
"bgPage": "#f1f3f5",
|
||||||
|
"groupBg": "#ffffff",
|
||||||
|
"groupOutline": "#cfd7de"
|
||||||
|
},
|
||||||
|
"sizes": {
|
||||||
|
"density": "default",
|
||||||
|
"pagePadding": "14px",
|
||||||
|
"groupGap": "14px",
|
||||||
|
"groupBorderRadius": "6px",
|
||||||
|
"widgetGap": "12px"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ui_page_ps_basic",
|
||||||
|
"type": "ui-page",
|
||||||
|
"name": "PumpingStation",
|
||||||
|
"ui": "ui_base_ps_basic",
|
||||||
|
"path": "/pumping-station",
|
||||||
|
"icon": "water_drop",
|
||||||
|
"layout": "grid",
|
||||||
|
"theme": "ui_theme_ps_basic",
|
||||||
|
"breakpoints": [
|
||||||
|
{
|
||||||
|
"name": "Default",
|
||||||
|
"px": "0",
|
||||||
|
"cols": "12"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"order": 1,
|
||||||
|
"className": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ui_group_ps_inputs",
|
||||||
|
"type": "ui-group",
|
||||||
|
"name": "Simulation Inputs",
|
||||||
|
"page": "ui_page_ps_basic",
|
||||||
|
"width": "4",
|
||||||
|
"height": "1",
|
||||||
|
"order": 1,
|
||||||
|
"showTitle": true,
|
||||||
|
"className": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ui_group_ps_trends",
|
||||||
|
"type": "ui-group",
|
||||||
|
"name": "Basin Trends",
|
||||||
|
"page": "ui_page_ps_basic",
|
||||||
|
"width": "8",
|
||||||
|
"height": "1",
|
||||||
|
"order": 2,
|
||||||
|
"showTitle": true,
|
||||||
|
"className": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ui_group_ps_state",
|
||||||
|
"type": "ui-group",
|
||||||
|
"name": "State",
|
||||||
|
"page": "ui_page_ps_basic",
|
||||||
|
"width": "12",
|
||||||
|
"height": "1",
|
||||||
|
"order": 3,
|
||||||
|
"showTitle": true,
|
||||||
|
"className": ""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_node_basic",
|
||||||
|
"type": "pumpingStation",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"name": "PS Dashboard Demo",
|
||||||
|
"basinVolume": 50,
|
||||||
|
"basinHeight": 5,
|
||||||
|
"inflowLevel": 3,
|
||||||
|
"outflowLevel": 0.2,
|
||||||
|
"overflowLevel": 4.5,
|
||||||
|
"defaultFluid": "wastewater",
|
||||||
|
"inletPipeDiameter": 0.4,
|
||||||
|
"outletPipeDiameter": 0.3,
|
||||||
|
"pipelineLength": 80,
|
||||||
|
"maxDischargeHead": 24,
|
||||||
|
"staticHead": 12,
|
||||||
|
"maxInflowRate": 200,
|
||||||
|
"temperatureReferenceDegC": 15,
|
||||||
|
"timeleftToFullOrEmptyThresholdSeconds": 0,
|
||||||
|
"enableDryRunProtection": true,
|
||||||
|
"enableHighVolumeSafety": true,
|
||||||
|
"enableOverfillProtection": true,
|
||||||
|
"dryRunThresholdPercent": 2,
|
||||||
|
"highVolumeSafetyThresholdPercent": 98,
|
||||||
|
"overfillThresholdPercent": 98,
|
||||||
|
"minHeightBasedOn": "outlet",
|
||||||
|
"processOutputFormat": "process",
|
||||||
|
"dbaseOutputFormat": "influxdb",
|
||||||
|
"refHeight": "NAP",
|
||||||
|
"basinBottomRef": 0,
|
||||||
|
"unit": "m3/h",
|
||||||
|
"enableLog": false,
|
||||||
|
"logLevel": "error",
|
||||||
|
"positionVsParent": "atEquipment",
|
||||||
|
"positionIcon": "",
|
||||||
|
"hasDistance": false,
|
||||||
|
"distance": 0,
|
||||||
|
"distanceUnit": "m",
|
||||||
|
"distanceDescription": "",
|
||||||
|
"controlMode": "levelbased",
|
||||||
|
"levelCurveType": "linear",
|
||||||
|
"logCurveFactor": 9,
|
||||||
|
"minLevel": 1,
|
||||||
|
"startLevel": 2,
|
||||||
|
"maxLevel": 4,
|
||||||
|
"x": 720,
|
||||||
|
"y": 260,
|
||||||
|
"wires": [
|
||||||
|
[
|
||||||
|
"ps_parse_output"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"ps_debug_influx"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"ps_debug_parent"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_calibrate_initial",
|
||||||
|
"type": "inject",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"name": "Set start level 2 m",
|
||||||
|
"props": [
|
||||||
|
{
|
||||||
|
"p": "topic",
|
||||||
|
"vt": "str"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"p": "payload"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"repeat": "",
|
||||||
|
"crontab": "",
|
||||||
|
"once": true,
|
||||||
|
"onceDelay": "0.5",
|
||||||
|
"topic": "calibratePredictedLevel",
|
||||||
|
"payload": "2",
|
||||||
|
"payloadType": "num",
|
||||||
|
"x": 180,
|
||||||
|
"y": 180,
|
||||||
|
"wires": [
|
||||||
|
[
|
||||||
|
"ps_node_basic"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_auto_inflow",
|
||||||
|
"type": "inject",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"name": "Auto inflow 0.008 m3/s",
|
||||||
|
"props": [
|
||||||
|
{
|
||||||
|
"p": "payload"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"repeat": "1",
|
||||||
|
"crontab": "",
|
||||||
|
"once": true,
|
||||||
|
"onceDelay": "1",
|
||||||
|
"topic": "",
|
||||||
|
"payload": "0.008",
|
||||||
|
"payloadType": "num",
|
||||||
|
"x": 180,
|
||||||
|
"y": 240,
|
||||||
|
"wires": [
|
||||||
|
[
|
||||||
|
"ps_build_qin"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_inflow_input",
|
||||||
|
"type": "ui-number-input",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"group": "ui_group_ps_inputs",
|
||||||
|
"name": "Inflow",
|
||||||
|
"label": "Inflow (m3/s)",
|
||||||
|
"order": 1,
|
||||||
|
"width": "4",
|
||||||
|
"height": "1",
|
||||||
|
"passthru": true,
|
||||||
|
"topic": "",
|
||||||
|
"min": 0,
|
||||||
|
"max": 0.05,
|
||||||
|
"step": 0.001,
|
||||||
|
"x": 190,
|
||||||
|
"y": 300,
|
||||||
|
"wires": [
|
||||||
|
[
|
||||||
|
"ps_build_qin"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_build_qin",
|
||||||
|
"type": "function",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"name": "Build q_in",
|
||||||
|
"func": "msg.topic = 'q_in';\nmsg.unit = 'm3/s';\nmsg.payload = Number(msg.payload);\nreturn Number.isFinite(msg.payload) ? msg : null;",
|
||||||
|
"outputs": 1,
|
||||||
|
"noerr": 0,
|
||||||
|
"initialize": "",
|
||||||
|
"finalize": "",
|
||||||
|
"libs": [],
|
||||||
|
"x": 440,
|
||||||
|
"y": 260,
|
||||||
|
"wires": [
|
||||||
|
[
|
||||||
|
"ps_node_basic"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_outflow_input",
|
||||||
|
"type": "ui-number-input",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"group": "ui_group_ps_inputs",
|
||||||
|
"name": "Outflow",
|
||||||
|
"label": "Outflow (m3/s)",
|
||||||
|
"order": 2,
|
||||||
|
"width": "4",
|
||||||
|
"height": "1",
|
||||||
|
"passthru": true,
|
||||||
|
"topic": "",
|
||||||
|
"min": 0,
|
||||||
|
"max": 0.05,
|
||||||
|
"step": 0.001,
|
||||||
|
"x": 190,
|
||||||
|
"y": 360,
|
||||||
|
"wires": [
|
||||||
|
[
|
||||||
|
"ps_build_qout"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_build_qout",
|
||||||
|
"type": "function",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"name": "Build q_out",
|
||||||
|
"func": "msg.topic = 'q_out';\nmsg.unit = 'm3/s';\nmsg.payload = Number(msg.payload);\nreturn Number.isFinite(msg.payload) ? msg : null;",
|
||||||
|
"outputs": 1,
|
||||||
|
"noerr": 0,
|
||||||
|
"initialize": "",
|
||||||
|
"finalize": "",
|
||||||
|
"libs": [],
|
||||||
|
"x": 440,
|
||||||
|
"y": 360,
|
||||||
|
"wires": [
|
||||||
|
[
|
||||||
|
"ps_node_basic"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_parse_output",
|
||||||
|
"type": "function",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"name": "Parse PS output",
|
||||||
|
"func": "// MeasurementContainer flat keys are `${type}.${variant}.${position}.${childId}`.\n// When PS writes without an explicit .child(), the childId is the literal\n// string 'default' — DON'T strip it. See generalFunctions/src/measurements/\n// MeasurementContainer.js getFlattenedOutput for details.\nconst fields = (msg.payload && typeof msg.payload === 'object') ? msg.payload : {};\nconst snapshot = Object.assign({}, context.get('snapshot') || {}, fields);\ncontext.set('snapshot', snapshot);\nconst firstFinite = (...keys) => {\n for (const key of keys) {\n const value = Number(snapshot[key]);\n if (Number.isFinite(value)) return value;\n }\n return null;\n};\nconst level = firstFinite('level.predicted.atequipment.default', 'level.measured.atequipment.default');\nconst volume = firstFinite('volume.predicted.atequipment.default', 'volume.measured.atequipment.default');\nconst netFlow = firstFinite('netFlowRate.predicted.atequipment.default', 'netFlowRate.measured.atequipment.default');\nconst demand = firstFinite('percControl');\nconst safety = snapshot.safetyState || 'normal';\nconst direction = snapshot.direction || 'unknown';\nconst overflow = snapshot.isOverflowing === true || snapshot.isOverflowing === 'true';\nconst timeleft = Number(snapshot.timeleft);\nconst fmt = (value, digits = 2) => Number.isFinite(value) ? value.toFixed(digits) : '-';\nreturn [\n level == null ? null : { topic: 'level', payload: level },\n volume == null ? null : { topic: 'volume', payload: volume },\n demand == null ? null : { topic: 'demand', payload: demand },\n netFlow == null ? null : { topic: 'net_flow', payload: netFlow },\n { topic: 'safety', payload: `${safety} | overflowing=${overflow}` },\n { topic: 'snapshot', payload: `level=${fmt(level)} m | volume=${fmt(volume)} m3 | demand=${fmt(demand, 0)}% | direction=${direction} | t=${Number.isFinite(timeleft) ? Math.round(timeleft) + ' s' : '-'}` }\n];",
|
||||||
|
"outputs": 6,
|
||||||
|
"noerr": 0,
|
||||||
|
"initialize": "",
|
||||||
|
"finalize": "",
|
||||||
|
"libs": [],
|
||||||
|
"x": 980,
|
||||||
|
"y": 220,
|
||||||
|
"wires": [
|
||||||
|
[
|
||||||
|
"ps_chart_level"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"ps_chart_volume"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"ps_chart_demand"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"ps_chart_netflow"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"ps_text_safety"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"ps_text_snapshot"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_chart_level",
|
||||||
|
"type": "ui-chart",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"group": "ui_group_ps_trends",
|
||||||
|
"name": "Level",
|
||||||
|
"label": "Level (m)",
|
||||||
|
"order": 1,
|
||||||
|
"width": 4,
|
||||||
|
"height": 4,
|
||||||
|
"chartType": "line",
|
||||||
|
"category": "topic",
|
||||||
|
"xAxisType": "time",
|
||||||
|
"yAxisLabel": "m",
|
||||||
|
"removeOlder": "15",
|
||||||
|
"removeOlderUnit": "60",
|
||||||
|
"x": 1230,
|
||||||
|
"y": 140,
|
||||||
|
"wires": [],
|
||||||
|
"showLegend": false,
|
||||||
|
"categoryType": "msg",
|
||||||
|
"xAxisProperty": "",
|
||||||
|
"xAxisPropertyType": "timestamp",
|
||||||
|
"xAxisFormat": "",
|
||||||
|
"xAxisFormatType": "auto",
|
||||||
|
"yAxisProperty": "payload",
|
||||||
|
"yAxisPropertyType": "msg",
|
||||||
|
"xmin": "",
|
||||||
|
"xmax": "",
|
||||||
|
"ymin": "0",
|
||||||
|
"ymax": "5",
|
||||||
|
"bins": 10,
|
||||||
|
"action": "append",
|
||||||
|
"stackSeries": false,
|
||||||
|
"pointShape": "circle",
|
||||||
|
"pointRadius": 4,
|
||||||
|
"interpolation": "linear",
|
||||||
|
"className": "",
|
||||||
|
"colors": [
|
||||||
|
"#0c99d9"
|
||||||
|
],
|
||||||
|
"textColor": [
|
||||||
|
"#666666"
|
||||||
|
],
|
||||||
|
"textColorDefault": true,
|
||||||
|
"gridColor": [
|
||||||
|
"#e5e5e5"
|
||||||
|
],
|
||||||
|
"gridColorDefault": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_chart_volume",
|
||||||
|
"type": "ui-chart",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"group": "ui_group_ps_trends",
|
||||||
|
"name": "Volume",
|
||||||
|
"label": "Volume (m3)",
|
||||||
|
"order": 2,
|
||||||
|
"width": 4,
|
||||||
|
"height": 4,
|
||||||
|
"chartType": "line",
|
||||||
|
"category": "topic",
|
||||||
|
"xAxisType": "time",
|
||||||
|
"yAxisLabel": "m3",
|
||||||
|
"removeOlder": "15",
|
||||||
|
"removeOlderUnit": "60",
|
||||||
|
"x": 1230,
|
||||||
|
"y": 200,
|
||||||
|
"wires": [],
|
||||||
|
"showLegend": false,
|
||||||
|
"categoryType": "msg",
|
||||||
|
"xAxisProperty": "",
|
||||||
|
"xAxisPropertyType": "timestamp",
|
||||||
|
"xAxisFormat": "",
|
||||||
|
"xAxisFormatType": "auto",
|
||||||
|
"yAxisProperty": "payload",
|
||||||
|
"yAxisPropertyType": "msg",
|
||||||
|
"xmin": "",
|
||||||
|
"xmax": "",
|
||||||
|
"ymin": "0",
|
||||||
|
"ymax": "50",
|
||||||
|
"bins": 10,
|
||||||
|
"action": "append",
|
||||||
|
"stackSeries": false,
|
||||||
|
"pointShape": "circle",
|
||||||
|
"pointRadius": 4,
|
||||||
|
"interpolation": "linear",
|
||||||
|
"className": "",
|
||||||
|
"colors": [
|
||||||
|
"#2ca02c"
|
||||||
|
],
|
||||||
|
"textColor": [
|
||||||
|
"#666666"
|
||||||
|
],
|
||||||
|
"textColorDefault": true,
|
||||||
|
"gridColor": [
|
||||||
|
"#e5e5e5"
|
||||||
|
],
|
||||||
|
"gridColorDefault": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_chart_demand",
|
||||||
|
"type": "ui-chart",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"group": "ui_group_ps_trends",
|
||||||
|
"name": "Demand",
|
||||||
|
"label": "Demand (%)",
|
||||||
|
"order": 3,
|
||||||
|
"width": 4,
|
||||||
|
"height": 4,
|
||||||
|
"chartType": "line",
|
||||||
|
"category": "topic",
|
||||||
|
"xAxisType": "time",
|
||||||
|
"yAxisLabel": "%",
|
||||||
|
"removeOlder": "15",
|
||||||
|
"removeOlderUnit": "60",
|
||||||
|
"x": 1230,
|
||||||
|
"y": 260,
|
||||||
|
"wires": [],
|
||||||
|
"showLegend": false,
|
||||||
|
"categoryType": "msg",
|
||||||
|
"xAxisProperty": "",
|
||||||
|
"xAxisPropertyType": "timestamp",
|
||||||
|
"xAxisFormat": "",
|
||||||
|
"xAxisFormatType": "auto",
|
||||||
|
"yAxisProperty": "payload",
|
||||||
|
"yAxisPropertyType": "msg",
|
||||||
|
"xmin": "",
|
||||||
|
"xmax": "",
|
||||||
|
"ymin": "0",
|
||||||
|
"ymax": "120",
|
||||||
|
"bins": 10,
|
||||||
|
"action": "append",
|
||||||
|
"stackSeries": false,
|
||||||
|
"pointShape": "circle",
|
||||||
|
"pointRadius": 4,
|
||||||
|
"interpolation": "linear",
|
||||||
|
"className": "",
|
||||||
|
"colors": [
|
||||||
|
"#d68910"
|
||||||
|
],
|
||||||
|
"textColor": [
|
||||||
|
"#666666"
|
||||||
|
],
|
||||||
|
"textColorDefault": true,
|
||||||
|
"gridColor": [
|
||||||
|
"#e5e5e5"
|
||||||
|
],
|
||||||
|
"gridColorDefault": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_chart_netflow",
|
||||||
|
"type": "ui-chart",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"group": "ui_group_ps_trends",
|
||||||
|
"name": "Net Flow",
|
||||||
|
"label": "Net flow (m3/s)",
|
||||||
|
"order": 4,
|
||||||
|
"width": 4,
|
||||||
|
"height": 4,
|
||||||
|
"chartType": "line",
|
||||||
|
"category": "topic",
|
||||||
|
"xAxisType": "time",
|
||||||
|
"yAxisLabel": "m3/s",
|
||||||
|
"removeOlder": "15",
|
||||||
|
"removeOlderUnit": "60",
|
||||||
|
"x": 1240,
|
||||||
|
"y": 320,
|
||||||
|
"wires": [],
|
||||||
|
"showLegend": false,
|
||||||
|
"categoryType": "msg",
|
||||||
|
"xAxisProperty": "",
|
||||||
|
"xAxisPropertyType": "timestamp",
|
||||||
|
"xAxisFormat": "",
|
||||||
|
"xAxisFormatType": "auto",
|
||||||
|
"yAxisProperty": "payload",
|
||||||
|
"yAxisPropertyType": "msg",
|
||||||
|
"xmin": "",
|
||||||
|
"xmax": "",
|
||||||
|
"ymin": "",
|
||||||
|
"ymax": "",
|
||||||
|
"bins": 10,
|
||||||
|
"action": "append",
|
||||||
|
"stackSeries": false,
|
||||||
|
"pointShape": "circle",
|
||||||
|
"pointRadius": 4,
|
||||||
|
"interpolation": "linear",
|
||||||
|
"className": "",
|
||||||
|
"colors": [
|
||||||
|
"#9467bd"
|
||||||
|
],
|
||||||
|
"textColor": [
|
||||||
|
"#666666"
|
||||||
|
],
|
||||||
|
"textColorDefault": true,
|
||||||
|
"gridColor": [
|
||||||
|
"#e5e5e5"
|
||||||
|
],
|
||||||
|
"gridColorDefault": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_text_safety",
|
||||||
|
"type": "ui-text",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"group": "ui_group_ps_state",
|
||||||
|
"name": "Safety",
|
||||||
|
"label": "Safety",
|
||||||
|
"order": 1,
|
||||||
|
"width": 4,
|
||||||
|
"height": 1,
|
||||||
|
"format": "{{msg.payload}}",
|
||||||
|
"layout": "row-spread",
|
||||||
|
"x": 1230,
|
||||||
|
"y": 380,
|
||||||
|
"wires": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_text_snapshot",
|
||||||
|
"type": "ui-text",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"group": "ui_group_ps_state",
|
||||||
|
"name": "Snapshot",
|
||||||
|
"label": "Snapshot",
|
||||||
|
"order": 2,
|
||||||
|
"width": 8,
|
||||||
|
"height": 1,
|
||||||
|
"format": "{{msg.payload}}",
|
||||||
|
"layout": "row-spread",
|
||||||
|
"x": 1240,
|
||||||
|
"y": 440,
|
||||||
|
"wires": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_debug_influx",
|
||||||
|
"type": "debug",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"name": "Influx output",
|
||||||
|
"active": false,
|
||||||
|
"tosidebar": true,
|
||||||
|
"console": false,
|
||||||
|
"tostatus": false,
|
||||||
|
"complete": "true",
|
||||||
|
"targetType": "full",
|
||||||
|
"x": 980,
|
||||||
|
"y": 320,
|
||||||
|
"wires": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ps_debug_parent",
|
||||||
|
"type": "debug",
|
||||||
|
"z": "ps_tab_basic_dashboard",
|
||||||
|
"name": "Parent output",
|
||||||
|
"active": false,
|
||||||
|
"tosidebar": true,
|
||||||
|
"console": false,
|
||||||
|
"tostatus": false,
|
||||||
|
"complete": "true",
|
||||||
|
"targetType": "full",
|
||||||
|
"x": 980,
|
||||||
|
"y": 380,
|
||||||
|
"wires": []
|
||||||
|
}
|
||||||
|
]
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
/**
|
|
||||||
* Standalone PumpingStation demo — run with `node examples/standalone-demo.js`.
|
|
||||||
* Builds a station + one pump, calibrates predicted volume, ticks once.
|
|
||||||
* Useful for sanity-checking the orchestrator without Node-RED.
|
|
||||||
*/
|
|
||||||
const PumpingStation = require('../src/specificClass');
|
|
||||||
const RotatingMachine = require('../../rotatingMachine/src/specificClass');
|
|
||||||
|
|
||||||
function createPumpingStationConfig(name) {
|
|
||||||
return {
|
|
||||||
general: {
|
|
||||||
logging: { enabled: true, logLevel: 'debug' },
|
|
||||||
name,
|
|
||||||
id: `${name}-${Date.now()}`,
|
|
||||||
flowThreshold: 1e-4,
|
|
||||||
},
|
|
||||||
functionality: { softwareType: 'pumpingStation', role: 'stationcontroller' },
|
|
||||||
basin: { volume: 43.75, height: 10, inflowLevel: 3, outflowLevel: 0.2, overflowLevel: 3.2 },
|
|
||||||
hydraulics: { refHeight: 'NAP', basinBottomRef: 0 },
|
|
||||||
safety: { enableDryRunProtection: false, enableOverfillProtection: false },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function createMachineConfig(name, position) {
|
|
||||||
return {
|
|
||||||
general: { name, logging: { enabled: false, logLevel: 'debug' } },
|
|
||||||
functionality: { softwareType: 'machine', positionVsParent: position },
|
|
||||||
asset: { supplier: 'Hydrostal', type: 'pump', category: 'centrifugal', model: 'hidrostal-H05K-S03R' },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function createMachineStateConfig() {
|
|
||||||
return {
|
|
||||||
general: { logging: { enabled: true, logLevel: 'debug' } },
|
|
||||||
movement: { speed: 1 },
|
|
||||||
time: { starting: 2, warmingup: 3, stopping: 2, coolingdown: 3 },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
(async function demo() {
|
|
||||||
const station = new PumpingStation(createPumpingStationConfig('PumpingStationDemo'));
|
|
||||||
const pump1 = new RotatingMachine(createMachineConfig('Pump1', 'downstream'), createMachineStateConfig());
|
|
||||||
|
|
||||||
station.childRegistrationUtils.registerChild(pump1, 'machine');
|
|
||||||
|
|
||||||
setInterval(() => station.tick(), 1000);
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
||||||
|
|
||||||
console.log('Initial state:', station.state);
|
|
||||||
station.setManualInflow(300, Date.now(), 'l/s');
|
|
||||||
station.calibratePredictedVolume(3.4);
|
|
||||||
|
|
||||||
console.log('Station state:', station.state);
|
|
||||||
console.log('Station output:', station.getOutput());
|
|
||||||
})().catch((err) => {
|
|
||||||
console.error('Demo failed:', err);
|
|
||||||
});
|
|
||||||
@@ -10,7 +10,16 @@
|
|||||||
-->
|
-->
|
||||||
<script src="/pumpingStation/menu.js"></script> <!-- Load the menu script for dynamic dropdowns -->
|
<script src="/pumpingStation/menu.js"></script> <!-- Load the menu script for dynamic dropdowns -->
|
||||||
<script src="/pumpingStation/configData.js"></script> <!-- Load the config script for node information -->
|
<script src="/pumpingStation/configData.js"></script> <!-- Load the config script for node information -->
|
||||||
<script src="/pumpingStation/editor.js"></script> <!-- Load the basin-diagram editor logic -->
|
|
||||||
|
<!-- Editor JS modules — see nodes/pumpingStation/src/editor/. Loaded in
|
||||||
|
dependency order: index.js (namespace + helpers) → diagrams → handlers. -->
|
||||||
|
<script src="/pumpingStation/editor/index.js"></script>
|
||||||
|
<script src="/pumpingStation/editor/bounds.js"></script>
|
||||||
|
<script src="/pumpingStation/editor/basin-diagram.js"></script>
|
||||||
|
<script src="/pumpingStation/editor/mode-preview.js"></script>
|
||||||
|
<script src="/pumpingStation/editor/hover-couple.js"></script>
|
||||||
|
<script src="/pumpingStation/editor/oneditprepare.js"></script>
|
||||||
|
<script src="/pumpingStation/editor/oneditsave.js"></script>
|
||||||
|
|
||||||
<script>//test
|
<script>//test
|
||||||
RED.nodes.registerType("pumpingStation", {
|
RED.nodes.registerType("pumpingStation", {
|
||||||
@@ -23,8 +32,8 @@
|
|||||||
simulator: { value: false },
|
simulator: { value: false },
|
||||||
basinVolume: { value: 1 }, // m³, total empty basin
|
basinVolume: { value: 1 }, // m³, total empty basin
|
||||||
basinHeight: { value: 1 }, // m, floor to top
|
basinHeight: { value: 1 }, // m, floor to top
|
||||||
inflowLevel: { value: 0.8 }, // m, centre of inlet pipe above floor
|
inflowLevel: { value: 0.8 }, // m, bottom/invert of inlet pipe above floor
|
||||||
outflowLevel: { value: 0.2 }, // m, centre of outlet pipe above floor
|
outflowLevel: { value: 0.2 }, // m, top of outlet/suction pipe above floor
|
||||||
overflowLevel: { value: 0.9 }, // m, overflow elevation
|
overflowLevel: { value: 0.9 }, // m, overflow elevation
|
||||||
defaultFluid: { value: "wastewater" },
|
defaultFluid: { value: "wastewater" },
|
||||||
inletPipeDiameter: { value: 0.3 }, // m
|
inletPipeDiameter: { value: 0.3 }, // m
|
||||||
@@ -36,9 +45,11 @@
|
|||||||
temperatureReferenceDegC: { value: 15 },
|
temperatureReferenceDegC: { value: 15 },
|
||||||
timeleftToFullOrEmptyThresholdSeconds:{value:0}, // time threshold to safeguard starting or stopping pumps in seconds
|
timeleftToFullOrEmptyThresholdSeconds:{value:0}, // time threshold to safeguard starting or stopping pumps in seconds
|
||||||
enableDryRunProtection: { value: true },
|
enableDryRunProtection: { value: true },
|
||||||
enableOverfillProtection: { value: true },
|
enableHighVolumeSafety: { value: true },
|
||||||
|
enableOverfillProtection: { value: true }, // deprecated alias
|
||||||
dryRunThresholdPercent: { value: 2 },
|
dryRunThresholdPercent: { value: 2 },
|
||||||
overfillThresholdPercent: { value: 98 },
|
highVolumeSafetyThresholdPercent: { value: 98 },
|
||||||
|
overfillThresholdPercent: { value: 98 }, // deprecated alias
|
||||||
minHeightBasedOn: { value: "outlet" }, // basis for minimum height check: inlet or outlet
|
minHeightBasedOn: { value: "outlet" }, // basis for minimum height check: inlet or outlet
|
||||||
processOutputFormat: { value: "process" },
|
processOutputFormat: { value: "process" },
|
||||||
dbaseOutputFormat: { value: "influxdb" },
|
dbaseOutputFormat: { value: "influxdb" },
|
||||||
@@ -68,8 +79,14 @@
|
|||||||
distanceDescription: { value: "" },
|
distanceDescription: { value: "" },
|
||||||
|
|
||||||
// control strategy
|
// control strategy
|
||||||
controlMode: { value: "none" },
|
controlMode: { value: "levelbased" },
|
||||||
|
levelCurveType: { value: "linear" },
|
||||||
|
logCurveFactor: { value: 9 },
|
||||||
|
enableShiftedRamp: { value: false },
|
||||||
|
shiftLevel: { value: 0 },
|
||||||
|
shiftArmPercent: { value: 95 },
|
||||||
startLevel: { value: null },
|
startLevel: { value: null },
|
||||||
|
stopLevel: { value: null },
|
||||||
minLevel: { value: null },
|
minLevel: { value: null },
|
||||||
maxLevel: { value: null },
|
maxLevel: { value: null },
|
||||||
flowSetpoint: { value: null },
|
flowSetpoint: { value: null },
|
||||||
@@ -88,13 +105,10 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
oneditprepare: function () {
|
oneditprepare: function () {
|
||||||
window.EVOLV?.nodes?.pumpingStation?.editor?.init(this);
|
window.PSEditor.oneditprepare.call(this);
|
||||||
},
|
},
|
||||||
oneditsave: function () {
|
oneditsave: function () {
|
||||||
const node = this;
|
window.PSEditor.oneditsave.call(this);
|
||||||
window.EVOLV?.nodes?.pumpingStation?.loggerMenu?.saveEditor?.(node);
|
|
||||||
window.EVOLV?.nodes?.pumpingStation?.positionMenu?.saveEditor?.(node);
|
|
||||||
window.EVOLV?.nodes?.pumpingStation?.editor?.save(node);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -114,123 +128,204 @@
|
|||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
<h4>Basin parameters</h4>
|
<h4>Basin parameters</h4>
|
||||||
<p style="font-size:12px;color:#777;margin:0 0 8px 0;">Heights are measured from the basin floor (0 m). Enter values next to each line — the diagram scales to whatever you enter.</p>
|
<p style="font-size:12px;color:#777;margin:0 0 8px 0;">Heights are measured from the basin floor (0 m). Each input on the left controls a line in the diagram on the right — hover an input to highlight its line.</p>
|
||||||
|
<div id="ps-basin-validation" style="display:none;color:#C0392B;font-size:11px;margin:0 0 8px 0;border:1px solid #C0392B;border-radius:3px;padding:6px 8px;background:#FDECEA;"></div>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
#ps-basin-diagram input[type=number] {
|
/* Two-column layout: stacked colour-coded inputs on the left,
|
||||||
width: 100%; height: 20px; box-sizing: border-box;
|
SVG on the right. Hover an input row → its paired SVG line
|
||||||
font-size: 11px; padding: 1px 4px; margin: 0;
|
(referenced by data-couples-line) gets a thicker stroke. */
|
||||||
border: 1px solid #ccc; border-radius: 3px; background: #fff;
|
.ps-diag { display:flex; gap:28px; align-items:flex-start; margin:0 0 14px 0; }
|
||||||
|
.ps-diag-side { width: 220px; flex: 0 0 220px; display:flex; flex-direction:column; gap:6px; }
|
||||||
|
.ps-diag-side .ps-row {
|
||||||
|
display:grid; grid-template-columns: minmax(0,1fr) 70px 16px; align-items:center;
|
||||||
|
gap:6px; padding:4px 6px 4px 10px; border-left:4px solid #ccc;
|
||||||
|
background:#fafafa; border-radius:3px; font-size:11px; cursor:pointer;
|
||||||
|
min-width:0;
|
||||||
}
|
}
|
||||||
#ps-basin-diagram input[type=number]:focus { outline: 1px solid #0c99d9; border-color: #0c99d9; }
|
.ps-diag-side .ps-row:hover { background:#f0f0f0; }
|
||||||
|
.ps-diag-side .ps-row.ps-readonly { background:#fff; cursor:default; opacity:0.85; }
|
||||||
|
.ps-diag-side .ps-row label { font-weight:600; margin:0; line-height:1.2; }
|
||||||
|
.ps-diag-side .ps-row .ps-sub { grid-column:1; font-size:10px; color:#888; font-weight:400; }
|
||||||
|
.ps-diag-side .ps-row input[type=number] {
|
||||||
|
width:100%; height:22px; box-sizing:border-box; font-size:11px;
|
||||||
|
padding:1px 4px; margin:0; border:1px solid #ccc; border-radius:3px;
|
||||||
|
background:#fff;
|
||||||
|
}
|
||||||
|
.ps-diag-side .ps-row input[type=number]:focus { outline:1px solid #0c99d9; border-color:#0c99d9; }
|
||||||
|
.ps-diag-side .ps-row .ps-readonly-val {
|
||||||
|
font-family:monospace; font-size:11px; color:#666; text-align:right;
|
||||||
|
padding-right:4px;
|
||||||
|
}
|
||||||
|
.ps-diag-side .ps-row .ps-unit { color:#888; font-size:10px; }
|
||||||
|
.ps-diag-svg { flex:1; min-width:0; }
|
||||||
|
/* Border colours matched to each SVG line stroke. */
|
||||||
|
.ps-row[data-stroke="#333"] { border-left-color:#333; }
|
||||||
|
.ps-row[data-stroke="#C0392B"] { border-left-color:#C0392B; }
|
||||||
|
.ps-row[data-stroke="#1E8449"] { border-left-color:#1E8449; }
|
||||||
|
.ps-row[data-stroke="#1F4E79"] { border-left-color:#1F4E79; }
|
||||||
|
.ps-row[data-stroke="#D68910"] { border-left-color:#D68910; }
|
||||||
|
.ps-row[data-stroke="#888"] { border-left-color:#888; }
|
||||||
|
.ps-row[data-stroke="#333"] label { color:#333; }
|
||||||
|
.ps-row[data-stroke="#C0392B"] label { color:#C0392B; }
|
||||||
|
.ps-row[data-stroke="#1E8449"] label { color:#1E8449; }
|
||||||
|
.ps-row[data-stroke="#1F4E79"] label { color:#1F4E79; }
|
||||||
|
.ps-row[data-stroke="#D68910"] label { color:#D68910; }
|
||||||
|
.ps-row[data-stroke="#888"] label { color:#888; }
|
||||||
|
/* Highlight class applied to the SVG line during input row hover. */
|
||||||
|
.ps-line-highlight { stroke-width:3.5 !important; opacity:1 !important; }
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<svg id="ps-basin-diagram" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 520 430"
|
<!--
|
||||||
style="display:block;width:100%;max-width:540px;margin:0 0 12px 0;background:#fff;border:1px solid #e5e5e5;border-radius:4px;"
|
============================================================
|
||||||
font-family="Arial,sans-serif" font-size="11">
|
BASIN DIAGRAM (ps-basin-diagram)
|
||||||
<defs>
|
============================================================
|
||||||
<marker id="ps-arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
Coordinate system: SVG viewBox is 520 (wide) × 430 (tall).
|
||||||
<path d="M 0 0 L 10 5 L 0 10 z" fill="#1F4E79" />
|
Origin (0,0) is top-left. +x goes right. +y goes DOWN.
|
||||||
</marker>
|
Bigger y = lower on screen.
|
||||||
</defs>
|
|
||||||
|
|
||||||
<!-- Tank body -->
|
X-LANES (all viewBox units, edit any of these to shift a column):
|
||||||
<rect x="200" y="40" width="120" height="340" fill="#F0F8FF" stroke="#333" stroke-width="1.5" />
|
x ≈ 5..75 left input column (inlet number input)
|
||||||
<!-- Dead-volume band (y + height updated dynamically below outflowLevel) -->
|
x = 80 inlet unit "m"
|
||||||
<rect id="ps-deadvol" x="201" width="118" fill="#AACCE0" />
|
x = 135 inlet text labels (right-aligned, anchor at x)
|
||||||
<!-- basinVolume — pinned above the rim -->
|
x = 140..200 inlet arrow (line + arrow head into tank)
|
||||||
<text id="ps-label-basinVolume" x="330" y="19" fill="#333" font-weight="600">basin volume</text>
|
x = 200..320 tank body (rect.x=200 width=120) — interior 201..319
|
||||||
<foreignObject id="ps-fo-basinVolume" x="425" y="4" width="70" height="22">
|
x = 195/325 threshold tick lines (extend 5 px outside tank)
|
||||||
<input xmlns="http://www.w3.org/1999/xhtml" type="number" id="node-input-basinVolume" min="0" step="0.1" />
|
x = 260 mid-tank zone labels (centered)
|
||||||
</foreignObject>
|
x = 320..360 outlet arrow
|
||||||
<text id="ps-unit-basinVolume" x="500" y="19" fill="#555">m³</text>
|
x = 330 right-side label column ("overflowLevel", "Outlet", …)
|
||||||
|
x = 365 outlet sub-text column
|
||||||
|
x = 425..495 right input column (foreignObject inputs, width=70)
|
||||||
|
x = 500 right unit column ("m", "m³")
|
||||||
|
|
||||||
<!-- Zone labels (mid-tank italic, positioned dynamically at midpoint between adjacent thresholds) -->
|
Y-COORDINATES:
|
||||||
<text id="ps-zone-spare" x="260" text-anchor="middle" fill="#B78200" font-size="10" font-style="italic" visibility="hidden">Spare volume before spilling</text>
|
y = 40 tank rim (basinHeight line)
|
||||||
<text id="ps-zone-sewage" x="260" text-anchor="middle" fill="#1F4E79" font-size="10" font-style="italic" visibility="hidden">Sewage + tank buffer</text>
|
y = 380 tank floor / datum
|
||||||
<text id="ps-zone-buffer1" x="260" text-anchor="middle" fill="#1F4E79" font-size="10" font-style="italic" visibility="hidden">Tank buffer</text>
|
y = 410 ordering warning ribbon
|
||||||
<text id="ps-zone-buffer2" x="260" text-anchor="middle" fill="#1F4E79" font-size="10" font-style="italic" visibility="hidden">Tank buffer</text>
|
y = 19,44 "basin volume" / "basinHeight" labels (static)
|
||||||
<text id="ps-zone-dead" x="260" text-anchor="middle" fill="#444" font-size="10" font-style="italic" visibility="hidden">Dead volume</text>
|
Threshold rows (overflowLevel, highVolumeSafetyLevel, inflowLevelGuide,
|
||||||
|
dryRunLevel, outflowLevel, basinHeight tick) get y assigned
|
||||||
|
DYNAMICALLY by the redraw() function around line 250-340 below.
|
||||||
|
Their input row may be NUDGED off ideal-y to avoid overlap; a leader
|
||||||
|
line (ps-leader-*) is then drawn between threshold y and input y.
|
||||||
|
Zone-label rows (ps-zone-*) get y assigned dynamically to the midpoint
|
||||||
|
between adjacent thresholds; they hide if the gap is too small.
|
||||||
|
|
||||||
|
HOW TO NUDGE OVERLAPPING LABELS:
|
||||||
|
- For STATIC y values (hardcoded below): edit the inline y attribute.
|
||||||
|
- For DYNAMIC y values: search redraw() for the element id and adjust
|
||||||
|
the layout math (e.g. NUDGE_PX or the threshold-stack ordering).
|
||||||
|
- For x: every label column above can be shifted by editing the inline
|
||||||
|
x attribute on the relevant <text>/<line>/<foreignObject>.
|
||||||
|
|
||||||
<!-- basinHeight — always at tank rim (y=40 in viewBox coords) -->
|
Note: dynamic line/label positioning lives in oneditprepare → redraw()
|
||||||
<line id="ps-line-basinHeight" x1="195" y1="40" x2="325" y2="40" stroke="#333" stroke-width="1.5" />
|
further up in this file. Changing only the inline y here will be
|
||||||
<text id="ps-label-basinHeight" x="330" y="44" fill="#333">basinHeight</text>
|
overridden on first redraw for any element whose id appears in redraw().
|
||||||
<foreignObject id="ps-fo-basinHeight" x="425" y="29" width="70" height="22">
|
============================================================
|
||||||
<input xmlns="http://www.w3.org/1999/xhtml" type="number" id="node-input-basinHeight" min="0" step="0.1" />
|
-->
|
||||||
</foreignObject>
|
<div class="ps-diag" id="ps-basin-wrap">
|
||||||
<text id="ps-unit-basinHeight" x="500" y="44" fill="#555">m</text>
|
<!-- LEFT: stacked colour-coded inputs. Hover a row → its paired SVG
|
||||||
|
line (data-couples-line) is highlighted in the diagram. -->
|
||||||
|
<div class="ps-diag-side">
|
||||||
|
<div class="ps-row" data-stroke="#333" style="cursor:default;">
|
||||||
|
<div><label>basinVolume</label><div class="ps-sub">total empty volume (no marker)</div></div>
|
||||||
|
<input type="number" id="node-input-basinVolume" min="0" step="0.1" />
|
||||||
|
<span class="ps-unit">m³</span>
|
||||||
|
</div>
|
||||||
|
<div class="ps-row" data-stroke="#333" data-couples-line="ps-line-basinHeight">
|
||||||
|
<div><label>basinHeight</label><div class="ps-sub">floor → rim</div></div>
|
||||||
|
<input type="number" id="node-input-basinHeight" min="0" step="0.1" />
|
||||||
|
<span class="ps-unit">m</span>
|
||||||
|
</div>
|
||||||
|
<div class="ps-row" data-stroke="#C0392B" data-couples-line="ps-line-overflowLevel">
|
||||||
|
<div><label>overflowLevel</label><div class="ps-sub">spill height</div></div>
|
||||||
|
<input type="number" id="node-input-overflowLevel" min="0" step="0.01" />
|
||||||
|
<span class="ps-unit">m</span>
|
||||||
|
</div>
|
||||||
|
<div class="ps-row ps-readonly" data-stroke="#D68910" data-couples-line="ps-line-highVolumeSafetyLevel">
|
||||||
|
<div><label>highVolumeSafety</label><div class="ps-sub">derived (overflow × %)</div></div>
|
||||||
|
<span id="derived-highVolumeSafetyLevel" class="ps-readonly-val">— m</span>
|
||||||
|
<span class="ps-unit">m</span>
|
||||||
|
</div>
|
||||||
|
<div class="ps-row" data-stroke="#1F4E79" data-couples-line="ps-line-inflowLevel">
|
||||||
|
<div><label>inflowLevel</label><div class="ps-sub">bottom of inlet pipe</div></div>
|
||||||
|
<input type="number" id="node-input-inflowLevel" min="0" step="0.01" />
|
||||||
|
<span class="ps-unit">m</span>
|
||||||
|
</div>
|
||||||
|
<div class="ps-row ps-readonly" data-stroke="#C0392B" data-couples-line="ps-line-dryRunLevel">
|
||||||
|
<div><label>dryRunLevel</label><div class="ps-sub">derived (outflow × dry%)</div></div>
|
||||||
|
<span id="derived-dryRunLevel" class="ps-readonly-val">— m</span>
|
||||||
|
<span class="ps-unit">m</span>
|
||||||
|
</div>
|
||||||
|
<div class="ps-row" data-stroke="#1F4E79" data-couples-line="ps-line-outflowLevel">
|
||||||
|
<div><label>outflowLevel</label><div class="ps-sub">top of outlet pipe</div></div>
|
||||||
|
<input type="number" id="node-input-outflowLevel" min="0" step="0.01" />
|
||||||
|
<span class="ps-unit">m</span>
|
||||||
|
</div>
|
||||||
|
<div class="ps-row ps-readonly" data-stroke="#888" style="cursor:default;">
|
||||||
|
<div><label>basinBottomRef</label><div class="ps-sub">floor above NAP (no marker)</div></div>
|
||||||
|
<input type="number" id="node-input-basinBottomRef" step="0.01" />
|
||||||
|
<span class="ps-unit">m</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<!-- RIGHT: SVG. The viewBox is now narrower (320 wide) since the right
|
||||||
|
input column is gone — labels render inside the tank's right margin. -->
|
||||||
|
<svg id="ps-basin-diagram" class="ps-diag-svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 360 430"
|
||||||
|
style="display:block;width:100%;max-width:360px;background:#fff;border:1px solid #e5e5e5;border-radius:4px;"
|
||||||
|
font-family="Arial,sans-serif" font-size="11">
|
||||||
|
<defs>
|
||||||
|
<marker id="ps-arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
||||||
|
<path d="M 0 0 L 10 5 L 0 10 z" fill="#1F4E79" />
|
||||||
|
</marker>
|
||||||
|
</defs>
|
||||||
|
<!-- Tank body — shifted right (x=145, width=110) to give the inlet
|
||||||
|
sub-label "bottom of pipe" room on the left without clipping.
|
||||||
|
Threshold tick lines extend 5 px outside the tank walls. -->
|
||||||
|
<rect x="145" y="40" width="110" height="340" fill="#F0F8FF" stroke="#333" stroke-width="1.5" />
|
||||||
|
<rect id="ps-deadvol" x="146" width="108" fill="#AACCE0" />
|
||||||
|
|
||||||
<!-- overflowLevel -->
|
<!-- Mid-tank zone labels — centred at x=200 (tank centre). -->
|
||||||
<line id="ps-line-overflowLevel" x1="195" x2="325" stroke="#C0392B" stroke-dasharray="4 2" stroke-width="1.5" />
|
<text id="ps-zone-spare" x="200" text-anchor="middle" fill="#B78200" font-size="10" font-style="italic" visibility="hidden">Spare</text>
|
||||||
<text id="ps-label-overflowLevel" x="330" fill="#C0392B">overflowLevel</text>
|
<text id="ps-zone-sewage" x="200" text-anchor="middle" fill="#1F4E79" font-size="10" font-style="italic" visibility="hidden">Sewage + buffer</text>
|
||||||
<foreignObject id="ps-fo-overflowLevel" x="425" width="70" height="22">
|
<text id="ps-zone-buffer1" x="200" text-anchor="middle" fill="#1F4E79" font-size="10" font-style="italic" visibility="hidden">Buffer</text>
|
||||||
<input xmlns="http://www.w3.org/1999/xhtml" type="number" id="node-input-overflowLevel" min="0" step="0.01" />
|
<text id="ps-zone-buffer2" x="200" text-anchor="middle" fill="#1F4E79" font-size="10" font-style="italic" visibility="hidden">Buffer</text>
|
||||||
</foreignObject>
|
<text id="ps-zone-dead" x="200" text-anchor="middle" fill="#444" font-size="10" font-style="italic" visibility="hidden">Dead vol</text>
|
||||||
<text id="ps-unit-overflowLevel" x="500" fill="#555">m</text>
|
|
||||||
|
|
||||||
<!-- maxLevel -->
|
<!-- basinHeight tick at tank rim (y=40, static). -->
|
||||||
<line id="ps-line-maxLevel" x1="195" x2="325" stroke="#D68910" stroke-dasharray="4 2" stroke-width="1.5" />
|
<line id="ps-line-basinHeight" x1="140" y1="40" x2="260" y2="40" stroke="#333" stroke-width="1.5" />
|
||||||
<text id="ps-label-maxLevel" x="330" fill="#D68910">maxLevel</text>
|
<text id="ps-label-basinHeight" x="265" y="44" fill="#333">basinHeight</text>
|
||||||
<foreignObject id="ps-fo-maxLevel" x="425" width="70" height="22">
|
|
||||||
<input xmlns="http://www.w3.org/1999/xhtml" type="number" id="node-input-maxLevel" min="0" step="0.01" />
|
|
||||||
</foreignObject>
|
|
||||||
<text id="ps-unit-maxLevel" x="500" fill="#555">m</text>
|
|
||||||
|
|
||||||
<!-- startLevel -->
|
<line id="ps-line-overflowLevel" x1="140" x2="260" stroke="#C0392B" stroke-dasharray="4 2" stroke-width="1.5" />
|
||||||
<line id="ps-line-startLevel" x1="195" x2="325" stroke="#1E8449" stroke-dasharray="4 2" stroke-width="1.5" />
|
<text id="ps-label-overflowLevel" x="265" fill="#C0392B">overflowLevel</text>
|
||||||
<text id="ps-label-startLevel" x="330" fill="#1E8449">startLevel</text>
|
|
||||||
<foreignObject id="ps-fo-startLevel" x="425" width="70" height="22">
|
|
||||||
<input xmlns="http://www.w3.org/1999/xhtml" type="number" id="node-input-startLevel" min="0" step="0.01" />
|
|
||||||
</foreignObject>
|
|
||||||
<text id="ps-unit-startLevel" x="500" fill="#555">m</text>
|
|
||||||
|
|
||||||
<!-- Inlet — arrow + input on the left -->
|
<line id="ps-line-highVolumeSafetyLevel" x1="140" x2="260" stroke="#D68910" stroke-dasharray="1 2" stroke-width="1" opacity="0.7" />
|
||||||
<line id="ps-line-inflowLevel" x1="140" x2="200" stroke="#1F4E79" stroke-width="2" marker-end="url(#ps-arrow)" />
|
<text id="ps-label-highVolumeSafetyLevel" x="265" fill="#D68910" font-size="10" font-style="italic">highVolSafety</text>
|
||||||
<text id="ps-label-inflowLevel" x="135" text-anchor="end" fill="#1F4E79" font-weight="bold">Inlet</text>
|
|
||||||
<text id="ps-sub-inflowLevel" x="135" text-anchor="end" fill="#777" font-size="9">bottom of pipe</text>
|
|
||||||
<foreignObject id="ps-fo-inflowLevel" x="5" width="70" height="22">
|
|
||||||
<input xmlns="http://www.w3.org/1999/xhtml" type="number" id="node-input-inflowLevel" min="0" step="0.01" />
|
|
||||||
</foreignObject>
|
|
||||||
<text id="ps-unit-inflowLevel" x="80" fill="#555">m</text>
|
|
||||||
|
|
||||||
<!-- minLevel -->
|
<line id="ps-line-inflowLevelGuide" x1="145" x2="255" stroke="#1F4E79" stroke-dasharray="2 3" stroke-width="1" opacity="0.55" />
|
||||||
<line id="ps-line-minLevel" x1="195" x2="325" stroke="#6C3483" stroke-dasharray="4 2" stroke-width="1.5" />
|
<text id="ps-label-inflowLevelGuide" x="265" fill="#1F4E79" font-size="10" font-style="italic">inlet invert</text>
|
||||||
<text id="ps-label-minLevel" x="330" fill="#6C3483">minLevel</text>
|
|
||||||
<foreignObject id="ps-fo-minLevel" x="425" width="70" height="22">
|
|
||||||
<input xmlns="http://www.w3.org/1999/xhtml" type="number" id="node-input-minLevel" min="0" step="0.01" />
|
|
||||||
</foreignObject>
|
|
||||||
<text id="ps-unit-minLevel" x="500" fill="#555">m</text>
|
|
||||||
|
|
||||||
<!-- dryRunLevel (derived, read-only) -->
|
<line id="ps-line-inflowLevel" x1="85" x2="145" stroke="#1F4E79" stroke-width="2" marker-end="url(#ps-arrow)" />
|
||||||
<line id="ps-line-dryRunLevel" x1="195" x2="325" stroke="#C0392B" stroke-dasharray="1 2" stroke-width="1" opacity="0.6" />
|
<text id="ps-label-inflowLevel" x="80" text-anchor="end" fill="#1F4E79" font-weight="bold">Inlet</text>
|
||||||
<text id="ps-label-dryRunLevel" x="330" fill="#C0392B" font-size="10" font-style="italic">dryRunLevel ≈ — m (safety — from %)</text>
|
<text id="ps-sub-inflowLevel" x="80" text-anchor="end" fill="#777" font-size="9">bottom of pipe</text>
|
||||||
|
|
||||||
<!-- Outlet — arrow on right, input below the threshold column -->
|
<line id="ps-line-dryRunLevel" x1="140" x2="260" stroke="#C0392B" stroke-dasharray="1 2" stroke-width="1" opacity="0.6" />
|
||||||
<line id="ps-line-outflowLevel" x1="320" x2="360" stroke="#1F4E79" stroke-width="2" marker-end="url(#ps-arrow)" />
|
<text id="ps-label-dryRunLevel" x="265" fill="#C0392B" font-size="10" font-style="italic">dryRunLevel</text>
|
||||||
<text id="ps-label-outflowLevel" x="365" fill="#1F4E79" font-weight="bold">Outlet</text>
|
|
||||||
<text id="ps-sub-outflowLevel" x="365" fill="#777" font-size="9">top of pipe</text>
|
|
||||||
<foreignObject id="ps-fo-outflowLevel" x="425" width="70" height="22">
|
|
||||||
<input xmlns="http://www.w3.org/1999/xhtml" type="number" id="node-input-outflowLevel" min="0" step="0.01" />
|
|
||||||
</foreignObject>
|
|
||||||
<text id="ps-unit-outflowLevel" x="500" fill="#555">m</text>
|
|
||||||
|
|
||||||
<!-- Floor / datum -->
|
<line id="ps-line-outflowLevel" x1="255" x2="295" stroke="#1F4E79" stroke-width="2" marker-end="url(#ps-arrow)" />
|
||||||
<line x1="195" y1="380" x2="325" y2="380" stroke="#000" stroke-width="2" />
|
<text id="ps-label-outflowLevel" x="300" fill="#1F4E79" font-weight="bold">Outlet</text>
|
||||||
<text x="330" y="384" fill="#000">0 m (datum)</text>
|
<text id="ps-sub-outflowLevel" x="300" fill="#777" font-size="9">top of pipe</text>
|
||||||
|
|
||||||
<!-- Leader lines: shown when the input row had to be nudged off its threshold's ideal y -->
|
<!-- Floor / datum — datum label sits BELOW the tank (y=395) so it
|
||||||
<line id="ps-leader-basinHeight" x1="0" y1="0" x2="0" y2="0" stroke="#bbb" stroke-width="0.6" stroke-dasharray="2 2" visibility="hidden" />
|
never collides with the Outlet / top-of-pipe sub-label when
|
||||||
<line id="ps-leader-overflowLevel" x1="0" y1="0" x2="0" y2="0" stroke="#bbb" stroke-width="0.6" stroke-dasharray="2 2" visibility="hidden" />
|
outflowLevel is near the floor. -->
|
||||||
<line id="ps-leader-maxLevel" x1="0" y1="0" x2="0" y2="0" stroke="#bbb" stroke-width="0.6" stroke-dasharray="2 2" visibility="hidden" />
|
<line x1="140" y1="380" x2="260" y2="380" stroke="#000" stroke-width="2" />
|
||||||
<line id="ps-leader-startLevel" x1="0" y1="0" x2="0" y2="0" stroke="#bbb" stroke-width="0.6" stroke-dasharray="2 2" visibility="hidden" />
|
<text x="200" y="395" text-anchor="middle" fill="#000" font-size="10">0 m (datum)</text>
|
||||||
<line id="ps-leader-minLevel" x1="0" y1="0" x2="0" y2="0" stroke="#bbb" stroke-width="0.6" stroke-dasharray="2 2" visibility="hidden" />
|
|
||||||
<line id="ps-leader-dryRunLevel" x1="0" y1="0" x2="0" y2="0" stroke="#bbb" stroke-width="0.6" stroke-dasharray="2 2" visibility="hidden" />
|
<!-- Ordering-warning ribbon -->
|
||||||
<line id="ps-leader-outflowLevel" x1="0" y1="0" x2="0" y2="0" stroke="#bbb" stroke-width="0.6" stroke-dasharray="2 2" visibility="hidden" />
|
<text id="ps-warning" x="200" y="410" text-anchor="middle" fill="#C0392B" font-size="10" font-style="italic" visibility="hidden"></text>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- Ordering-warning ribbon -->
|
|
||||||
<text id="ps-warning" x="260" y="410" text-anchor="middle" fill="#C0392B" font-size="10" font-style="italic" visibility="hidden"></text>
|
|
||||||
</svg>
|
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
@@ -238,39 +333,187 @@
|
|||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-controlMode"><i class="fa fa-sliders"></i> Control mode</label>
|
<label for="node-input-controlMode"><i class="fa fa-sliders"></i> Control mode</label>
|
||||||
<select id="node-input-controlMode">
|
<select id="node-input-controlMode">
|
||||||
<option value="none">None / Manual</option>
|
|
||||||
<option value="levelbased">Level-based</option>
|
<option value="levelbased">Level-based</option>
|
||||||
<option value="flowbased">Flow-based</option>
|
<option value="manual">Manual</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="ps-mode-levelbased" class="ps-mode-section">
|
<div id="ps-mode-levelbased" class="ps-mode-section">
|
||||||
<p style="font-size:12px;color:#777;margin:0;">Level-based uses <code>minLevel</code> / <code>startLevel</code> / <code>maxLevel</code> from the diagram above.</p>
|
<div class="form-row">
|
||||||
|
<label for="node-input-levelCurveType">Curve</label>
|
||||||
|
<select id="node-input-levelCurveType" style="width:60%;">
|
||||||
|
<option value="linear">Linear</option>
|
||||||
|
<option value="log">Log - fast early response</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="form-row" id="ps-log-factor-row" style="display:none;">
|
||||||
|
<label for="node-input-logCurveFactor">Log shape factor</label>
|
||||||
|
<input type="number" id="node-input-logCurveFactor" min="0.001" step="0.1" style="width:100px;" />
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-enableShiftedRamp" style="width:auto;">
|
||||||
|
<input type="checkbox" id="node-input-enableShiftedRamp" style="width:auto;vertical-align:middle;margin-right:6px;" />
|
||||||
|
Enable shifted ramp (hysteresis)
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<div id="ps-mode-validation" style="display:none;color:#C0392B;font-size:11px;margin:4px 0 8px 0;border:1px solid #C0392B;border-radius:3px;padding:6px 8px;background:#FDECEA;"></div>
|
||||||
|
<!--
|
||||||
|
============================================================
|
||||||
|
LEVEL-BASED MODE PREVIEW (ps-levelbased-mode-diagram)
|
||||||
|
============================================================
|
||||||
|
Coordinate system: SVG viewBox is 430 (wide) × 185 (tall).
|
||||||
|
Origin (0,0) top-left. +x right. +y DOWN (so y=24 is HIGH on screen,
|
||||||
|
y=158 is at the baseline).
|
||||||
|
|
||||||
|
X-AXIS (level, in viewBox px) — controlled by redrawModeDiagram() in
|
||||||
|
the oneditprepare script above. The function maps the user's
|
||||||
|
startLevel/inflowLevel/maxLevel/shiftLevel onto the px window
|
||||||
|
x0=52 (left axis) → x1=390 (right end of plot).
|
||||||
|
DO NOT hardcode x for ps-mode-line-* / ps-mode-label-*; they're
|
||||||
|
rewritten on every input change.
|
||||||
|
|
||||||
|
Y-AXIS (process demand %):
|
||||||
|
y=24 100% (top of plot)
|
||||||
|
y=140 0% (baseline / x-axis)
|
||||||
|
y=160 OFF baseline (pink dashed)
|
||||||
|
y=180 axis labels under the plot ("dry run","start","inlet","max","overflow","shift")
|
||||||
|
y=205 legend captions (one row, BELOW axis labels — moved here to stop
|
||||||
|
colliding with the title row at y=14)
|
||||||
|
y=14 curve-type title only ("linear curve" / "log curve"), centered.
|
||||||
|
|
||||||
|
WHAT IS STATIC vs DYNAMIC:
|
||||||
|
STATIC (edit inline below): viewBox bounds, axis lines, "0%"/"100%"
|
||||||
|
tick labels, in-plot caption x/y, axis-label y=176.
|
||||||
|
DYNAMIC (edit in JS): every ps-mode-line-*, ps-mode-label-* x;
|
||||||
|
ps-mode-curve-up/down points; visibility of shift elements.
|
||||||
|
|
||||||
|
HOW TO NUDGE OVERLAPPING TEXT:
|
||||||
|
- Move the curve-type caption: edit the x="220" y="18" on
|
||||||
|
#ps-mode-curve-label.
|
||||||
|
- Move axis labels (start/inlet/max/shift) UP or DOWN: edit y="176".
|
||||||
|
(To move them left/right relative to the line, edit redrawModeDiagram
|
||||||
|
in the script — the x is set there.)
|
||||||
|
- Move the legend captions: edit x="280" y="54" / y="72" on
|
||||||
|
#ps-mode-curve-up-label / #ps-mode-curve-down-label.
|
||||||
|
- To resize the plot box, change viewBox + the x0/x1/y0/y1 constants
|
||||||
|
in redrawModeDiagram() to match.
|
||||||
|
============================================================
|
||||||
|
-->
|
||||||
|
<div class="ps-diag" id="ps-mode-wrap">
|
||||||
|
<!-- LEFT side-panel: only the level-based mode's editable inputs +
|
||||||
|
read-only displays for derived/related levels (so user has all
|
||||||
|
level context in one column). Hover-coupled to the SVG markers. -->
|
||||||
|
<div class="ps-diag-side">
|
||||||
|
<div class="ps-row ps-readonly" data-stroke="#C0392B" data-couples-line="ps-mode-line-dryRunLevel">
|
||||||
|
<div><label>dryRunLevel</label><div class="ps-sub">derived</div></div>
|
||||||
|
<span id="ps-mode-readout-dryRun" class="ps-readonly-val">— m</span>
|
||||||
|
<span class="ps-unit">m</span>
|
||||||
|
</div>
|
||||||
|
<div class="ps-row" data-stroke="#1E8449" data-couples-line="ps-mode-line-startLevel">
|
||||||
|
<div><label>startLevel</label><div class="ps-sub">pump-on threshold</div></div>
|
||||||
|
<input type="number" id="node-input-startLevel" min="0" step="0.01" />
|
||||||
|
<span class="ps-unit">m</span>
|
||||||
|
</div>
|
||||||
|
<div class="ps-row" data-stroke="#7D3C98" data-couples-line="ps-mode-line-stopLevel">
|
||||||
|
<div><label>stopLevel</label><div class="ps-sub">pump-off threshold (optional, ≤ startLevel)</div></div>
|
||||||
|
<input type="number" id="node-input-stopLevel" min="0" step="0.01" />
|
||||||
|
<span class="ps-unit">m</span>
|
||||||
|
</div>
|
||||||
|
<div class="ps-row ps-readonly" data-stroke="#1F4E79" data-couples-line="ps-mode-line-inflowLevel">
|
||||||
|
<div><label>inflowLevel</label><div class="ps-sub">from basin above</div></div>
|
||||||
|
<span id="ps-mode-readout-inflow" class="ps-readonly-val">— m</span>
|
||||||
|
<span class="ps-unit">m</span>
|
||||||
|
</div>
|
||||||
|
<div class="ps-row" data-stroke="#D68910" data-couples-line="ps-mode-line-maxLevel">
|
||||||
|
<div><label>maxLevel</label><div class="ps-sub">100% saturation</div></div>
|
||||||
|
<input type="number" id="node-input-maxLevel" min="0" step="0.01" />
|
||||||
|
<span class="ps-unit">m</span>
|
||||||
|
</div>
|
||||||
|
<div class="ps-row" id="ps-shiftLevel-row" data-stroke="#D68910" data-couples-line="ps-mode-line-shiftLevel" style="display:none;">
|
||||||
|
<div><label>shiftLevel</label><div class="ps-sub">held output drops here</div></div>
|
||||||
|
<input type="number" id="node-input-shiftLevel" min="0" step="0.01" />
|
||||||
|
<span class="ps-unit">m</span>
|
||||||
|
</div>
|
||||||
|
<div class="ps-row" id="ps-shiftArmPercent-row" data-stroke="#D68910" data-couples-line="ps-mode-line-armPercent" style="display:none;">
|
||||||
|
<div><label>shiftArmPercent</label><div class="ps-sub">arms when output % crosses this</div></div>
|
||||||
|
<input type="number" id="node-input-shiftArmPercent" min="0" max="100" step="1" />
|
||||||
|
<span class="ps-unit">%</span>
|
||||||
|
</div>
|
||||||
|
<div class="ps-row ps-readonly" data-stroke="#C0392B" data-couples-line="ps-mode-line-overflowLevel">
|
||||||
|
<div><label>overflowLevel</label><div class="ps-sub">from basin above</div></div>
|
||||||
|
<span id="ps-mode-readout-overflow" class="ps-readonly-val">— m</span>
|
||||||
|
<span class="ps-unit">m</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<svg id="ps-levelbased-mode-diagram" class="ps-diag-svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 430 215"
|
||||||
|
style="display:block;width:100%;max-width:540px;background:#fff;border:1px solid #e5e5e5;border-radius:4px;"
|
||||||
|
font-family="Arial,sans-serif" font-size="11">
|
||||||
|
<!-- ZONE BANDS — drawn FIRST so they sit behind axes and curves.
|
||||||
|
x is set DYNAMICALLY by redrawModeDiagram(); y/height span the full plot (24..160).
|
||||||
|
Order from leftmost to rightmost: dryRun (red) | safetyLow (orange) | safe (green) |
|
||||||
|
safetyHigh (orange) | overflow (red).
|
||||||
|
-->
|
||||||
|
<rect id="ps-zone-dryRun" y="24" height="136" fill="#fdecea" />
|
||||||
|
<rect id="ps-zone-safetyLow" y="24" height="136" fill="#fef5e7" />
|
||||||
|
<rect id="ps-zone-safe" y="24" height="136" fill="#eafaf1" />
|
||||||
|
<rect id="ps-zone-safetyHigh" y="24" height="136" fill="#fef5e7" />
|
||||||
|
<rect id="ps-zone-overflow" y="24" height="136" fill="#fdecea" />
|
||||||
|
<!-- X-axis (0% baseline) at y=140; y axis at x=52 (top y=24). Plot range: y=24..140. -->
|
||||||
|
<line x1="52" y1="140" x2="402" y2="140" stroke="#333" />
|
||||||
|
<line x1="52" y1="140" x2="52" y2="24" stroke="#333" />
|
||||||
|
<!-- OFF tier baseline at y=160 (20px below 0% baseline). pink line drawn dynamically by curve. -->
|
||||||
|
<line x1="52" y1="160" x2="402" y2="160" stroke="#E08080" stroke-dasharray="2 3" />
|
||||||
|
<!-- Y-axis tick labels (x=4, right-aligned via text-anchor="end" at x=50 for tighter alignment). -->
|
||||||
|
<text x="50" y="27" text-anchor="end" fill="#333">100%</text>
|
||||||
|
<text x="50" y="143" text-anchor="end" fill="#333">0%</text>
|
||||||
|
<text x="50" y="163" text-anchor="end" fill="#E08080">OFF</text>
|
||||||
|
<!-- Plot title above 100% line. -->
|
||||||
|
<text id="ps-mode-curve-label" x="220" y="14" text-anchor="middle" fill="#555">linear curve</text>
|
||||||
|
<!-- Curves drawn dynamically. Up curve foot=inlet→top=max. Down curve foot=start→top=shiftLevel (visible when shift enabled). -->
|
||||||
|
<polyline id="ps-mode-curve-up" fill="none" stroke="#1E8449" stroke-width="2.5" points="" />
|
||||||
|
<polyline id="ps-mode-curve-down" fill="none" stroke="#D68910" stroke-width="2" stroke-dasharray="5 3" points="" style="display:none;" />
|
||||||
|
<!-- Vertical level-marker lines — span y=24..140 (top to baseline only, NOT into OFF tier). x set dynamically. -->
|
||||||
|
<line id="ps-mode-line-dryRunLevel" y1="24" y2="140" stroke="#C0392B" stroke-dasharray="2 2" />
|
||||||
|
<line id="ps-mode-line-startLevel" y1="24" y2="140" stroke="#1E8449" stroke-dasharray="2 2" />
|
||||||
|
<line id="ps-mode-line-stopLevel" y1="24" y2="140" stroke="#7D3C98" stroke-dasharray="2 2" />
|
||||||
|
<line id="ps-mode-line-inflowLevel" y1="24" y2="140" stroke="#1F4E79" stroke-dasharray="2 2" />
|
||||||
|
<line id="ps-mode-line-maxLevel" y1="24" y2="140" stroke="#D68910" stroke-dasharray="2 2" />
|
||||||
|
<line id="ps-mode-line-overflowLevel" y1="24" y2="140" stroke="#C0392B" stroke-dasharray="2 2" />
|
||||||
|
<line id="ps-mode-line-shiftLevel" y1="24" y2="140" stroke="#D68910" stroke-dasharray="2 2" style="display:none;" />
|
||||||
|
<!-- Horizontal arming-% line — y is set DYNAMICALLY by the JS to the
|
||||||
|
shiftArmPercent value (in plot-y space). Spans full plot width. -->
|
||||||
|
<line id="ps-mode-line-armPercent" x1="52" x2="392" stroke="#D68910" stroke-dasharray="4 3" stroke-width="1" opacity="0.7" style="display:none;" />
|
||||||
|
<text id="ps-mode-label-armPercent" x="394" text-anchor="start" fill="#D68910" font-size="9" style="display:none;">arm%</text>
|
||||||
|
<!-- Axis labels under the plot were removed — they crowded each other
|
||||||
|
when levels were close. Identification comes from the line colour
|
||||||
|
(matched to the side-panel input row) and hover-coupling. -->
|
||||||
|
<!-- Empty <text> stubs kept for the redraw loop's getElementById calls
|
||||||
|
(cheaper than guarding each one). They're hidden via display:none. -->
|
||||||
|
<text id="ps-mode-label-dryRunLevel" style="display:none;"></text>
|
||||||
|
<text id="ps-mode-label-startLevel" style="display:none;"></text>
|
||||||
|
<text id="ps-mode-label-stopLevel" style="display:none;"></text>
|
||||||
|
<text id="ps-mode-label-inflowLevel" style="display:none;"></text>
|
||||||
|
<text id="ps-mode-label-maxLevel" style="display:none;"></text>
|
||||||
|
<text id="ps-mode-label-overflowLevel" style="display:none;"></text>
|
||||||
|
<text id="ps-mode-label-shiftLevel" style="display:none;"></text>
|
||||||
|
<!-- Legend captions — placed BELOW the axis labels (y=200) on their own row,
|
||||||
|
so they never collide with the title (y=14). Up-caption left-aligned at
|
||||||
|
x=60; down-caption to its right at x=210. Both font-size 10. -->
|
||||||
|
<text id="ps-mode-curve-up-label" x="60" y="205" fill="#1E8449" font-size="10">— ramp inlet→max</text>
|
||||||
|
<text id="ps-mode-curve-down-label" x="210" y="205" fill="#D68910" font-size="10" style="display:none;">— shifted (held @100% then ramp shift→start)</text>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="ps-mode-flowbased" class="ps-mode-section" style="display:none">
|
<div id="ps-mode-manual" class="ps-mode-section" style="display:none">
|
||||||
<div class="form-row">
|
<p style="font-size:12px;color:#777;margin:0;">Manual mode accepts external <code>Qd</code> demand commands and does not compute demand from basin level.</p>
|
||||||
<label for="node-input-flowSetpoint">Flow setpoint</label>
|
|
||||||
<input type="number" id="node-input-flowSetpoint" placeholder="m3/h" />
|
|
||||||
</div>
|
|
||||||
<div class="form-row">
|
|
||||||
<label for="node-input-flowDeadband">Deadband</label>
|
|
||||||
<input type="number" id="node-input-flowDeadband" placeholder="m3/h" />
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
<h4>Reference</h4>
|
<h4>Reference</h4>
|
||||||
|
|
||||||
<!-- Reference data -->
|
<!-- Reference data — basinBottomRef moved into basin side-panel above. -->
|
||||||
<div class="form-row">
|
|
||||||
<label for="node-input-minHeightBasedOn"><i class="fa fa-arrows-v"></i> Minimum Height Based On</label>
|
|
||||||
<select id="node-input-minHeightBasedOn" style="width:60%;">
|
|
||||||
<option value="inlet">Inlet Elevation</option>
|
|
||||||
<option value="outlet">Outlet Elevation</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-refHeight"><i class="fa fa-map-marker"></i> Reference height</label>
|
<label for="node-input-refHeight"><i class="fa fa-map-marker"></i> Reference height</label>
|
||||||
<select id="node-input-refHeight" style="width:60%;">
|
<select id="node-input-refHeight" style="width:60%;">
|
||||||
@@ -278,21 +521,11 @@
|
|||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-row">
|
|
||||||
<label for="node-input-basinBottomRef"><i class="fa fa-level-down"></i> Basin floor above datum (m)</label>
|
|
||||||
<input type="number" id="node-input-basinBottomRef" step="0.01" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
<h4>Safety</h4>
|
<h4>Safety</h4>
|
||||||
|
|
||||||
<!-- Safety settings -->
|
<!-- Safety settings -->
|
||||||
<div class="form-row">
|
|
||||||
<label for="node-input-timeleftToFullOrEmptyThresholdSeconds"><i class="fa fa-clock-o"></i> Time To Empty/Full (s)</label>
|
|
||||||
<input type="number" id="node-input-timeleftToFullOrEmptyThresholdSeconds" min="0" step="1" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-enableDryRunProtection">
|
<label for="node-input-enableDryRunProtection">
|
||||||
<i class="fa fa-shield"></i> Dry-run Protection
|
<i class="fa fa-shield"></i> Dry-run Protection
|
||||||
@@ -307,16 +540,16 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-enableOverfillProtection">
|
<label for="node-input-enableHighVolumeSafety">
|
||||||
<i class="fa fa-exclamation-triangle"></i> Overfill Protection
|
<i class="fa fa-exclamation-triangle"></i> High-volume Safety
|
||||||
</label>
|
</label>
|
||||||
<input type="checkbox" id="node-input-enableOverfillProtection" style="width:20px;vertical-align:baseline;" />
|
<input type="checkbox" id="node-input-enableHighVolumeSafety" style="width:20px;vertical-align:baseline;" />
|
||||||
<span>Stop filling when approaching overflow</span>
|
<span>Act before physical overflow</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-overfillThresholdPercent" style="padding-left:20px;">High Volume Threshold (%)</label>
|
<label for="node-input-highVolumeSafetyThresholdPercent" style="padding-left:20px;">High-volume Safety (%)</label>
|
||||||
<input type="number" id="node-input-overfillThresholdPercent" min="0" max="100" step="0.1" style="width:80px;" />
|
<input type="number" id="node-input-highVolumeSafetyThresholdPercent" min="0" max="100" step="0.1" style="width:80px;" />
|
||||||
<span id="derived-overfillLevel" style="margin-left:8px;color:#777;font-size:12px;">→ overfillLevel ≈ — m</span>
|
<span id="derived-highVolumeSafetyLevel" style="margin-left:8px;color:#777;font-size:12px;">→ highVolumeSafetyLevel ≈ — m</span>
|
||||||
</div>
|
</div>
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const nameOfNode = 'pumpingStation'; // this is the name of the node, it should match the file name and the node type in Node-RED
|
const nameOfNode = 'pumpingStation'; // this is the name of the node, it should match the file name and the node type in Node-RED
|
||||||
|
const path = require('path');
|
||||||
const nodeClass = require('./src/nodeClass.js'); // this is the specific node class
|
const nodeClass = require('./src/nodeClass.js'); // this is the specific node class
|
||||||
const { MenuManager, configManager } = require('generalFunctions');
|
const { MenuManager, configManager } = require('generalFunctions');
|
||||||
|
|
||||||
@@ -37,16 +38,16 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Editor.js — extracted SVG basin-diagram + oneditprepare/oneditsave logic.
|
// Editor JS modules — loaded by pumpingStation.html via <script src=...> tags.
|
||||||
RED.httpAdmin.get(`/${nameOfNode}/editor.js`, (req, res) => {
|
// Files live in src/editor/. Filename is restricted to a safe charset to
|
||||||
try {
|
// prevent path-traversal.
|
||||||
const fs = require('fs');
|
RED.httpAdmin.get(`/${nameOfNode}/editor/:file`, (req, res) => {
|
||||||
const path = require('path');
|
const safe = String(req.params.file || '').replace(/[^a-zA-Z0-9._-]/g, '');
|
||||||
const script = fs.readFileSync(path.join(__dirname, 'src/editor.js'), 'utf8');
|
if (!safe.endsWith('.js')) return res.status(400).send('// invalid');
|
||||||
res.type('application/javascript').send(script);
|
res.type('application/javascript');
|
||||||
} catch (err) {
|
res.sendFile(path.join(__dirname, 'src', 'editor', safe), (err) => {
|
||||||
res.status(500).send(`// Error loading editor.js: ${err.message}`);
|
if (err && !res.headersSent) res.status(404).send('// editor module not found');
|
||||||
}
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -49,6 +49,7 @@ module.exports = {
|
|||||||
| `max_level_bounded` | max level across the run must be `≤ value` |
|
| `max_level_bounded` | max level across the run must be `≤ value` |
|
||||||
| `min_level_bounded` | min level across the run must be `≥ value` |
|
| `min_level_bounded` | min level across the run must be `≥ value` |
|
||||||
| `max_demand_bounded` | max percControl must be `≤ value` |
|
| `max_demand_bounded` | max percControl must be `≤ value` |
|
||||||
|
| `max_demand_gt` | max percControl must be `> value` |
|
||||||
| `safety_trips_eq` | total ticks with `safetyActive` must equal `value` |
|
| `safety_trips_eq` | total ticks with `safetyActive` must equal `value` |
|
||||||
| `safety_trips_gt` | total ticks with `safetyActive` must be `> value` |
|
| `safety_trips_gt` | total ticks with `safetyActive` must be `> value` |
|
||||||
| `end_state_eq` | final record's `field` must equal `value` |
|
| `end_state_eq` | final record's `field` must equal `value` |
|
||||||
|
|||||||
@@ -54,6 +54,10 @@ function evalExpectation(ex, records) {
|
|||||||
const v = Math.max(...demands);
|
const v = Math.max(...demands);
|
||||||
return { ok: v <= ex.value, msg: `max demand = ${v.toFixed(0)} % (bound: ≤ ${ex.value})` };
|
return { ok: v <= ex.value, msg: `max demand = ${v.toFixed(0)} % (bound: ≤ ${ex.value})` };
|
||||||
}
|
}
|
||||||
|
case 'max_demand_gt': {
|
||||||
|
const v = Math.max(...demands);
|
||||||
|
return { ok: v > ex.value, msg: `max demand = ${v.toFixed(0)} % (expected > ${ex.value})` };
|
||||||
|
}
|
||||||
case 'safety_trips_eq': {
|
case 'safety_trips_eq': {
|
||||||
const n = records.filter((r) => r.safetyActive).length;
|
const n = records.filter((r) => r.safetyActive).length;
|
||||||
return { ok: n === ex.value, msg: `${n} ticks with safetyActive (expected ${ex.value})` };
|
return { ok: n === ex.value, msg: `${n} ticks with safetyActive (expected ${ex.value})` };
|
||||||
|
|||||||
@@ -2,30 +2,30 @@
|
|||||||
//
|
//
|
||||||
// Expectation: with a stable inflow of 0.008 m³/s and a pump bank with
|
// Expectation: with a stable inflow of 0.008 m³/s and a pump bank with
|
||||||
// max capacity 0.012 m³/s, the level settles in the RAMP zone (between
|
// max capacity 0.012 m³/s, the level settles in the RAMP zone (between
|
||||||
// startLevel and maxLevel) at roughly the point where demand matches
|
// inflowLevel and maxLevel while filling) at roughly the point where demand matches
|
||||||
// inflow. No safety trips should fire.
|
// inflow. No safety trips should fire.
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'levelbased-steady',
|
name: 'levelbased-steady',
|
||||||
description: 'Constant sewer inflow below pump capacity; level converges inside the RAMP zone with demand matching inflow.',
|
description: 'Constant sewer inflow below pump capacity; level converges inside the RAMP zone with demand matching inflow.',
|
||||||
durationSec: 1200,
|
durationSec: 3600,
|
||||||
|
|
||||||
config: {
|
config: {
|
||||||
general: { name: 'EvalSteady', id: 'eval-steady', unit: 'm3/h',
|
general: { name: 'EvalSteady', id: 'eval-steady', unit: 'm3/h',
|
||||||
logging: { enabled: false, logLevel: 'error' } },
|
logging: { enabled: false, logLevel: 'error' } },
|
||||||
functionality: { softwareType: 'pumpingStation', role: 'stationcontroller', positionVsParent: 'atEquipment' },
|
functionality: { softwareType: 'pumpingStation', role: 'stationcontroller', positionVsParent: 'atEquipment' },
|
||||||
basin: { volume: 50, height: 5, inflowLevel: 3, outflowLevel: 0.2, overflowLevel: 4.5 },
|
basin: { volume: 50, height: 5, inflowLevel: 3, outflowLevel: 0.2, overflowLevel: 4.5, inletPipeDiameter: 0.4, outletPipeDiameter: 0.3 },
|
||||||
hydraulics: { refHeight: 'NAP', basinBottomRef: 0, minHeightBasedOn: 'outlet' },
|
hydraulics: { refHeight: 'NAP', basinBottomRef: 0, minHeightBasedOn: 'outlet' },
|
||||||
control: {
|
control: {
|
||||||
mode: 'levelbased',
|
mode: 'levelbased',
|
||||||
allowedModes: new Set(['levelbased']),
|
allowedModes: new Set(['levelbased']),
|
||||||
levelbased: { minLevel: 1, startLevel: 2, maxLevel: 4 },
|
levelbased: { minLevel: 1, startLevel: 2, maxLevel: 4, curveType: 'linear', logCurveFactor: 9 },
|
||||||
},
|
},
|
||||||
safety: {
|
safety: {
|
||||||
enableDryRunProtection: true,
|
enableDryRunProtection: true,
|
||||||
dryRunThresholdPercent: 2,
|
dryRunThresholdPercent: 2,
|
||||||
enableOverfillProtection: true,
|
enableHighVolumeSafety: true,
|
||||||
overfillThresholdPercent: 98,
|
highVolumeSafetyThresholdPercent: 98,
|
||||||
timeleftToFullOrEmptyThresholdSeconds: 0,
|
timeleftToFullOrEmptyThresholdSeconds: 0,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -44,7 +44,7 @@ module.exports = {
|
|||||||
ps.measurements.type('flow').variant('predicted').position('out').child('mgc1').value(outflow, Date.now(), 'm3/s');
|
ps.measurements.type('flow').variant('predicted').position('out').child('mgc1').value(outflow, Date.now(), 'm3/s');
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
ps.calibratePredictedLevel(2.0); // start at the bottom of the RAMP zone
|
ps.calibratePredictedLevel(2.0); // start at the mode start level, below the rising ramp
|
||||||
},
|
},
|
||||||
|
|
||||||
inputs: (t, ps) => {
|
inputs: (t, ps) => {
|
||||||
@@ -55,6 +55,7 @@ module.exports = {
|
|||||||
{ name: 'no safety trips', type: 'safety_trips_eq', value: 0 },
|
{ name: 'no safety trips', type: 'safety_trips_eq', value: 0 },
|
||||||
{ name: 'level stays below overflow', type: 'max_level_bounded', value: 4.5 },
|
{ name: 'level stays below overflow', type: 'max_level_bounded', value: 4.5 },
|
||||||
{ name: 'level stays above outflow', type: 'min_level_bounded', value: 0.2 },
|
{ name: 'level stays above outflow', type: 'min_level_bounded', value: 0.2 },
|
||||||
|
{ name: 'rising ramp engages after inlet level', type: 'max_demand_gt', value: 0 },
|
||||||
{ name: 'no threshold issues on init', type: 'threshold_issues_eq', value: 0 },
|
{ name: 'no threshold issues on init', type: 'threshold_issues_eq', value: 0 },
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,31 +1,31 @@
|
|||||||
// Storm surge — inflow triples briefly, pumps should saturate at 100%,
|
// Storm surge — inflow triples briefly, pumps should increase demand as
|
||||||
// level rises toward overflow then recedes.
|
// the level enters the rising ramp.
|
||||||
//
|
//
|
||||||
// Expectation: during the surge (t=300..600), demand reaches 100% and
|
// Expectation: during the surge (t=300..600), demand rises but remains
|
||||||
// level may transiently climb above maxLevel. Overflow safety should
|
// bounded. High-volume safety should fire if the surge overwhelms pump
|
||||||
// fire if the surge overwhelms pump capacity; dry-run should not fire.
|
// capacity; dry-run should not fire.
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'levelbased-storm',
|
name: 'levelbased-storm',
|
||||||
description: 'Sewer inflow triples from 0.008 → 0.024 m³/s for 5 minutes then returns to baseline. Overfill safety may engage.',
|
description: 'Sewer inflow triples from 0.008 → 0.024 m³/s for 5 minutes then returns to baseline. High-volume safety may engage.',
|
||||||
durationSec: 1500,
|
durationSec: 1500,
|
||||||
|
|
||||||
config: {
|
config: {
|
||||||
general: { name: 'EvalStorm', id: 'eval-storm', unit: 'm3/h',
|
general: { name: 'EvalStorm', id: 'eval-storm', unit: 'm3/h',
|
||||||
logging: { enabled: false, logLevel: 'error' } },
|
logging: { enabled: false, logLevel: 'error' } },
|
||||||
functionality: { softwareType: 'pumpingStation', role: 'stationcontroller', positionVsParent: 'atEquipment' },
|
functionality: { softwareType: 'pumpingStation', role: 'stationcontroller', positionVsParent: 'atEquipment' },
|
||||||
basin: { volume: 50, height: 5, inflowLevel: 3, outflowLevel: 0.2, overflowLevel: 4.5 },
|
basin: { volume: 50, height: 5, inflowLevel: 3, outflowLevel: 0.2, overflowLevel: 4.5, inletPipeDiameter: 0.4, outletPipeDiameter: 0.3 },
|
||||||
hydraulics: { refHeight: 'NAP', basinBottomRef: 0, minHeightBasedOn: 'outlet' },
|
hydraulics: { refHeight: 'NAP', basinBottomRef: 0, minHeightBasedOn: 'outlet' },
|
||||||
control: {
|
control: {
|
||||||
mode: 'levelbased',
|
mode: 'levelbased',
|
||||||
allowedModes: new Set(['levelbased']),
|
allowedModes: new Set(['levelbased']),
|
||||||
levelbased: { minLevel: 1, startLevel: 2, maxLevel: 4 },
|
levelbased: { minLevel: 1, startLevel: 2, maxLevel: 4, curveType: 'linear', logCurveFactor: 9 },
|
||||||
},
|
},
|
||||||
safety: {
|
safety: {
|
||||||
enableDryRunProtection: true,
|
enableDryRunProtection: true,
|
||||||
dryRunThresholdPercent: 2,
|
dryRunThresholdPercent: 2,
|
||||||
enableOverfillProtection: true,
|
enableHighVolumeSafety: true,
|
||||||
overfillThresholdPercent: 95,
|
highVolumeSafetyThresholdPercent: 95,
|
||||||
timeleftToFullOrEmptyThresholdSeconds: 0,
|
timeleftToFullOrEmptyThresholdSeconds: 0,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -55,6 +55,6 @@ module.exports = {
|
|||||||
{ name: 'dry-run never trips', type: 'end_state_eq', field: 'safetyActive', value: false },
|
{ name: 'dry-run never trips', type: 'end_state_eq', field: 'safetyActive', value: false },
|
||||||
// Level may exceed maxLevel transiently but must stay under basinHeight
|
// Level may exceed maxLevel transiently but must stay under basinHeight
|
||||||
{ name: 'level never breaches physical basin', type: 'max_level_bounded', value: 5.0 },
|
{ name: 'level never breaches physical basin', type: 'max_level_bounded', value: 5.0 },
|
||||||
{ name: 'demand saturates at 100% during surge', type: 'max_demand_bounded', value: 100 },
|
{ name: 'demand remains bounded during surge', type: 'max_demand_bounded', value: 100 },
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -12,18 +12,18 @@ module.exports = {
|
|||||||
general: { name: 'EvalDryRun', id: 'eval-dry-run', unit: 'm3/h',
|
general: { name: 'EvalDryRun', id: 'eval-dry-run', unit: 'm3/h',
|
||||||
logging: { enabled: false, logLevel: 'error' } },
|
logging: { enabled: false, logLevel: 'error' } },
|
||||||
functionality: { softwareType: 'pumpingStation', role: 'stationcontroller', positionVsParent: 'atEquipment' },
|
functionality: { softwareType: 'pumpingStation', role: 'stationcontroller', positionVsParent: 'atEquipment' },
|
||||||
basin: { volume: 50, height: 5, inflowLevel: 3, outflowLevel: 0.2, overflowLevel: 4.5 },
|
basin: { volume: 50, height: 5, inflowLevel: 3, outflowLevel: 0.2, overflowLevel: 4.5, inletPipeDiameter: 0.4, outletPipeDiameter: 0.3 },
|
||||||
hydraulics: { refHeight: 'NAP', basinBottomRef: 0, minHeightBasedOn: 'outlet' },
|
hydraulics: { refHeight: 'NAP', basinBottomRef: 0, minHeightBasedOn: 'outlet' },
|
||||||
control: {
|
control: {
|
||||||
mode: 'manual',
|
mode: 'manual',
|
||||||
allowedModes: new Set(['levelbased', 'manual']),
|
allowedModes: new Set(['levelbased', 'manual']),
|
||||||
levelbased: { minLevel: 0.5, startLevel: 2, maxLevel: 4 },
|
levelbased: { minLevel: 0.5, startLevel: 2, maxLevel: 4, curveType: 'linear', logCurveFactor: 9 },
|
||||||
},
|
},
|
||||||
safety: {
|
safety: {
|
||||||
enableDryRunProtection: true,
|
enableDryRunProtection: true,
|
||||||
dryRunThresholdPercent: 50,
|
dryRunThresholdPercent: 50,
|
||||||
enableOverfillProtection: false,
|
enableHighVolumeSafety: false,
|
||||||
overfillThresholdPercent: 98,
|
highVolumeSafetyThresholdPercent: 98,
|
||||||
timeleftToFullOrEmptyThresholdSeconds: 0,
|
timeleftToFullOrEmptyThresholdSeconds: 0,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,91 +0,0 @@
|
|||||||
// Basin geometry for a wet-well pumping station.
|
|
||||||
//
|
|
||||||
// Models the basin as a rectangular prism (constant cross-section), so
|
|
||||||
// volume = level × surfaceArea. Owns the level↔volume conversions and the
|
|
||||||
// derived threshold volumes used by control + safety. Pure domain — no
|
|
||||||
// Node-RED, no logger, no side effects beyond construction.
|
|
||||||
|
|
||||||
class BasinGeometry {
|
|
||||||
/**
|
|
||||||
* @param {object} basinConfig - { volume, height, inflowLevel, outflowLevel, overflowLevel }
|
|
||||||
* @param {object} hydraulicsConfig - { minHeightBasedOn: 'inlet' | 'outlet' }
|
|
||||||
*/
|
|
||||||
constructor(basinConfig, hydraulicsConfig) {
|
|
||||||
const volEmptyBasin = basinConfig.volume;
|
|
||||||
const heightBasin = basinConfig.height;
|
|
||||||
const inflowLevel = basinConfig.inflowLevel;
|
|
||||||
const outflowLevel = basinConfig.outflowLevel;
|
|
||||||
const overflowLevel = basinConfig.overflowLevel;
|
|
||||||
const minHeightBasedOn = hydraulicsConfig?.minHeightBasedOn;
|
|
||||||
|
|
||||||
const surfaceArea = volEmptyBasin / heightBasin;
|
|
||||||
|
|
||||||
// maxVol ≡ volEmptyBasin under the constant cross-section assumption;
|
|
||||||
// kept as a separate field for naming symmetry with the trigger volumes.
|
|
||||||
const maxVol = heightBasin * surfaceArea;
|
|
||||||
const maxVolAtOverflow = overflowLevel * surfaceArea;
|
|
||||||
const minVolAtOutflow = outflowLevel * surfaceArea;
|
|
||||||
const minVolAtInflow = inflowLevel * surfaceArea;
|
|
||||||
const minVol = minHeightBasedOn === 'inlet' ? minVolAtInflow : minVolAtOutflow;
|
|
||||||
|
|
||||||
this._volEmptyBasin = volEmptyBasin;
|
|
||||||
this._heightBasin = heightBasin;
|
|
||||||
this._inflowLevel = inflowLevel;
|
|
||||||
this._outflowLevel = outflowLevel;
|
|
||||||
this._overflowLevel = overflowLevel;
|
|
||||||
this._surfaceArea = surfaceArea;
|
|
||||||
this._maxVol = maxVol;
|
|
||||||
this._maxVolAtOverflow = maxVolAtOverflow;
|
|
||||||
this._minVolAtInflow = minVolAtInflow;
|
|
||||||
this._minVolAtOutflow = minVolAtOutflow;
|
|
||||||
this._minVol = minVol;
|
|
||||||
this._minHeightBasedOn = minHeightBasedOn;
|
|
||||||
}
|
|
||||||
|
|
||||||
get volEmptyBasin() { return this._volEmptyBasin; }
|
|
||||||
get heightBasin() { return this._heightBasin; }
|
|
||||||
get inflowLevel() { return this._inflowLevel; }
|
|
||||||
get outflowLevel() { return this._outflowLevel; }
|
|
||||||
get overflowLevel() { return this._overflowLevel; }
|
|
||||||
get surfaceArea() { return this._surfaceArea; }
|
|
||||||
get maxVol() { return this._maxVol; }
|
|
||||||
get maxVolAtOverflow() { return this._maxVolAtOverflow; }
|
|
||||||
get minVolAtInflow() { return this._minVolAtInflow; }
|
|
||||||
get minVolAtOutflow() { return this._minVolAtOutflow; }
|
|
||||||
get minVol() { return this._minVol; }
|
|
||||||
get minHeightBasedOn() { return this._minHeightBasedOn; }
|
|
||||||
|
|
||||||
/** Convert level (m from floor) → volume (m3). Negative levels clamp to 0. */
|
|
||||||
volumeFromLevel(level) {
|
|
||||||
return Math.max(level, 0) * this._surfaceArea;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Convert volume (m3) → level (m from floor). Negative volumes clamp to 0. */
|
|
||||||
levelFromVolume(volume) {
|
|
||||||
return Math.max(volume, 0) / this._surfaceArea;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Plain-object snapshot mirroring the legacy `this.basin` shape so
|
|
||||||
* getOutput / status code can keep using the same field names without
|
|
||||||
* caring whether it's holding a class instance or a plain object.
|
|
||||||
*/
|
|
||||||
snapshot() {
|
|
||||||
return {
|
|
||||||
volEmptyBasin: this._volEmptyBasin,
|
|
||||||
heightBasin: this._heightBasin,
|
|
||||||
inflowLevel: this._inflowLevel,
|
|
||||||
outflowLevel: this._outflowLevel,
|
|
||||||
overflowLevel: this._overflowLevel,
|
|
||||||
surfaceArea: this._surfaceArea,
|
|
||||||
maxVol: this._maxVol,
|
|
||||||
maxVolAtOverflow: this._maxVolAtOverflow,
|
|
||||||
minVolAtInflow: this._minVolAtInflow,
|
|
||||||
minVolAtOutflow: this._minVolAtOutflow,
|
|
||||||
minVol: this._minVol,
|
|
||||||
minHeightBasedOn: this._minHeightBasedOn,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = BasinGeometry;
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
// Threshold-ordering validator for the pumpingStation basin + control +
|
|
||||||
// safety config. Pure: returns the issues array, never logs or throws.
|
|
||||||
// The caller decides what to do (warn, surface to status badge, fail tests).
|
|
||||||
//
|
|
||||||
// Invariants enforced (level-space, bottom → top):
|
|
||||||
// 0 < outflowLevel < inflowLevel < overflowLevel ≤ basinHeight
|
|
||||||
// dryRunLevel ≤ minLevel ≤ startLevel < maxLevel ≤ overfillLevel
|
|
||||||
//
|
|
||||||
// dryRunLevel and overfillLevel are DERIVED from safety percentages — the
|
|
||||||
// validator recomputes them so a config that places minLevel below the
|
|
||||||
// effective dry-run trigger (a no-op control band) is caught here.
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {object} basin - BasinGeometry instance OR plain {inflowLevel, outflowLevel, overflowLevel, heightBasin, minHeightBasedOn}
|
|
||||||
* @param {object} levelbased - config.control.levelbased ({ minLevel, startLevel, maxLevel })
|
|
||||||
* @param {object} safety - config.safety ({ dryRunThresholdPercent, overfillThresholdPercent })
|
|
||||||
* @returns {Array<{aName, a, op, bName, b, msg}>}
|
|
||||||
*/
|
|
||||||
function validateThresholdOrdering(basin, levelbased, safety) {
|
|
||||||
const lvl = levelbased || {};
|
|
||||||
const sfy = safety || {};
|
|
||||||
|
|
||||||
const dryRunPct = Number(sfy.dryRunThresholdPercent) || 0;
|
|
||||||
const overfillPct = Number(sfy.overfillThresholdPercent) || 100;
|
|
||||||
const refLowLevel = basin.minHeightBasedOn === 'inlet' ? basin.inflowLevel : basin.outflowLevel;
|
|
||||||
const dryRunLevel = refLowLevel * (1 + dryRunPct / 100);
|
|
||||||
const overfillLevel = basin.overflowLevel * (overfillPct / 100);
|
|
||||||
|
|
||||||
const checks = [
|
|
||||||
['outflowLevel', basin.outflowLevel, '<', 'inflowLevel', basin.inflowLevel],
|
|
||||||
['inflowLevel', basin.inflowLevel, '<', 'overflowLevel', basin.overflowLevel],
|
|
||||||
['overflowLevel', basin.overflowLevel, '<=', 'basinHeight', basin.heightBasin],
|
|
||||||
['dryRunLevel', dryRunLevel, '<=', 'minLevel', lvl.minLevel],
|
|
||||||
['minLevel', lvl.minLevel, '<=', 'startLevel', lvl.startLevel],
|
|
||||||
['startLevel', lvl.startLevel, '<', 'maxLevel', lvl.maxLevel],
|
|
||||||
['maxLevel', lvl.maxLevel, '<=', 'overfillLevel', overfillLevel],
|
|
||||||
];
|
|
||||||
|
|
||||||
const issues = [];
|
|
||||||
for (const [aName, a, op, bName, b] of checks) {
|
|
||||||
if (!Number.isFinite(a) || !Number.isFinite(b)) continue;
|
|
||||||
const ok = op === '<' ? a < b : a <= b;
|
|
||||||
if (!ok) {
|
|
||||||
issues.push({
|
|
||||||
aName,
|
|
||||||
a,
|
|
||||||
op,
|
|
||||||
bName,
|
|
||||||
b,
|
|
||||||
msg: `Threshold invariant violated: ${aName} (${a}) must be ${op} ${bName} (${b})`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return issues;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = { validateThresholdOrdering };
|
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
// Handler functions for pumpingStation commands. Each handler receives:
|
|
||||||
// source: the domain (specificClass) instance — has the public methods
|
|
||||||
// (changeMode, calibratePredicted*, setManualInflow, ...).
|
|
||||||
// msg: the Node-RED input message.
|
|
||||||
// ctx: { node, RED, send, logger } — provided by BaseNodeAdapter.
|
|
||||||
//
|
|
||||||
// Handlers are pure functions: they don't keep state. Validation that goes
|
|
||||||
// beyond the registry's typeof-check ladder lives here.
|
|
||||||
|
|
||||||
function _logger(source, ctx) {
|
|
||||||
return ctx?.logger || source?.logger || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.setMode = (source, msg) => {
|
|
||||||
source.changeMode(msg.payload);
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.registerChild = (source, msg, ctx) => {
|
|
||||||
const log = _logger(source, ctx);
|
|
||||||
const childId = msg.payload;
|
|
||||||
const childObj = ctx?.RED?.nodes?.getNode?.(childId);
|
|
||||||
if (!childObj || !childObj.source) {
|
|
||||||
log?.warn?.(`registerChild: child '${childId}' not found or has no .source`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
source.childRegistrationUtils.registerChild(childObj.source, msg.positionVsParent);
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.calibrateVolume = (source, msg, ctx) => {
|
|
||||||
const log = _logger(source, ctx);
|
|
||||||
const v = parseFloat(msg.payload);
|
|
||||||
if (!Number.isFinite(v)) {
|
|
||||||
log?.warn?.(`cmd.calibrate.volume: non-numeric payload '${msg.payload}'`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
source.calibratePredictedVolume(v);
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.calibrateLevel = (source, msg, ctx) => {
|
|
||||||
const log = _logger(source, ctx);
|
|
||||||
const v = parseFloat(msg.payload);
|
|
||||||
if (!Number.isFinite(v)) {
|
|
||||||
log?.warn?.(`cmd.calibrate.level: non-numeric payload '${msg.payload}'`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
source.calibratePredictedLevel(v);
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.setInflow = (source, msg) => {
|
|
||||||
// Payload is either a number (legacy q_in shape) or
|
|
||||||
// { value, unit, timestamp } (richer object form).
|
|
||||||
const p = msg.payload;
|
|
||||||
let value;
|
|
||||||
let unit;
|
|
||||||
let timestamp;
|
|
||||||
if (p !== null && typeof p === 'object') {
|
|
||||||
value = Number(p.value);
|
|
||||||
unit = p.unit;
|
|
||||||
timestamp = p.timestamp || Date.now();
|
|
||||||
} else {
|
|
||||||
value = Number(p);
|
|
||||||
unit = msg?.unit;
|
|
||||||
timestamp = msg?.timestamp || Date.now();
|
|
||||||
}
|
|
||||||
source.setManualInflow(value, timestamp, unit);
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.setDemand = (source, msg, ctx) => {
|
|
||||||
const log = _logger(source, ctx);
|
|
||||||
const demand = Number(msg.payload);
|
|
||||||
if (!Number.isFinite(demand)) {
|
|
||||||
log?.warn?.(`set.demand: invalid Qd value '${msg.payload}'`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (source.mode !== 'manual') {
|
|
||||||
log?.debug?.(
|
|
||||||
`set.demand ignored in '${source.mode}' mode; switch to manual to use the demand slider`
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// forwardDemandToChildren returns a promise — surface failures via logger.
|
|
||||||
Promise.resolve(source.forwardDemandToChildren(demand)).catch((err) => {
|
|
||||||
log?.error?.(`set.demand: failed to forward demand: ${err && err.message}`);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
// pumpingStation command registry. Consumed by BaseNodeAdapter via
|
|
||||||
// `static commands = require('./commands')`. Each descriptor maps a
|
|
||||||
// canonical msg.topic to its handler; legacy names are listed under
|
|
||||||
// `aliases` and emit a one-time deprecation warning at runtime.
|
|
||||||
|
|
||||||
const handlers = require('./handlers');
|
|
||||||
|
|
||||||
module.exports = [
|
|
||||||
{
|
|
||||||
topic: 'set.mode',
|
|
||||||
aliases: ['changemode'],
|
|
||||||
payloadSchema: { type: 'string' },
|
|
||||||
handler: handlers.setMode,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
topic: 'child.register',
|
|
||||||
aliases: ['registerChild'],
|
|
||||||
// payload is the Node-RED id (string) of the child node.
|
|
||||||
payloadSchema: { type: 'string' },
|
|
||||||
handler: handlers.registerChild,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
topic: 'cmd.calibrate.volume',
|
|
||||||
aliases: ['calibratePredictedVolume'],
|
|
||||||
// any: payload may be a number or numeric string.
|
|
||||||
payloadSchema: { type: 'any' },
|
|
||||||
handler: handlers.calibrateVolume,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
topic: 'cmd.calibrate.level',
|
|
||||||
aliases: ['calibratePredictedLevel'],
|
|
||||||
payloadSchema: { type: 'any' },
|
|
||||||
handler: handlers.calibrateLevel,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
topic: 'set.inflow',
|
|
||||||
aliases: ['q_in'],
|
|
||||||
// any: number, numeric string, or { value, unit, timestamp } object.
|
|
||||||
payloadSchema: { type: 'any' },
|
|
||||||
handler: handlers.setInflow,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
topic: 'set.demand',
|
|
||||||
aliases: ['Qd'],
|
|
||||||
payloadSchema: { type: 'any' },
|
|
||||||
handler: handlers.setDemand,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
// Placeholder — flow-based control mode is not yet implemented.
|
|
||||||
// The dispatcher routes here when config.control.mode === 'flowbased',
|
|
||||||
// at which point a real implementation should land in this file.
|
|
||||||
async function run(ctx) {
|
|
||||||
ctx?.logger?.debug?.('flow-based mode not yet implemented');
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
name: 'flowbased',
|
|
||||||
run,
|
|
||||||
};
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
const levelBased = require('./levelBased');
|
|
||||||
const flowBased = require('./flowBased');
|
|
||||||
const manual = require('./manual');
|
|
||||||
|
|
||||||
const strategies = {
|
|
||||||
[levelBased.name]: levelBased,
|
|
||||||
[flowBased.name]: flowBased,
|
|
||||||
[manual.name]: manual,
|
|
||||||
};
|
|
||||||
|
|
||||||
function dispatch(mode, ctx, controlState) {
|
|
||||||
const s = strategies[mode];
|
|
||||||
if (!s) {
|
|
||||||
ctx.logger?.warn?.(`Unsupported control mode: ${mode}`);
|
|
||||||
return Promise.resolve();
|
|
||||||
}
|
|
||||||
return s.run(ctx, controlState);
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = { strategies, dispatch, manual };
|
|
||||||
@@ -1,92 +0,0 @@
|
|||||||
const { interpolation } = require('generalFunctions');
|
|
||||||
|
|
||||||
const _interp = new interpolation();
|
|
||||||
|
|
||||||
// Maps [startLevel..maxLevel] → [0..100]. Outside the range,
|
|
||||||
// interpolate_lin_single_point clamps to o_min / o_max.
|
|
||||||
function _scaleLevelToFlowPercent(level, levelbased, logger) {
|
|
||||||
const { startLevel, maxLevel } = levelbased;
|
|
||||||
logger?.debug?.(`Scaling startLevel=${startLevel} maxLevel=${maxLevel}`);
|
|
||||||
return _interp.interpolate_lin_single_point(level, startLevel, maxLevel, 0, 100);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function _applyMachineGroupLevelControl(machineGroups, percentControl, logger) {
|
|
||||||
if (!machineGroups || Object.keys(machineGroups).length === 0) return;
|
|
||||||
await Promise.all(
|
|
||||||
Object.values(machineGroups).map((group) =>
|
|
||||||
group.handleInput('parent', percentControl).catch((err) => {
|
|
||||||
logger?.error?.(`Failed to send level control to group "${group.config?.general?.name}": ${err.message}`);
|
|
||||||
})
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function _applyMachineLevelControl(machines, percentControl, logger) {
|
|
||||||
const filtered = Object.values(machines).filter((machine) => {
|
|
||||||
const pos = machine?.config?.functionality?.positionVsParent;
|
|
||||||
return (pos === 'downstream' || pos === 'atequipment');
|
|
||||||
});
|
|
||||||
if (!filtered.length) return;
|
|
||||||
|
|
||||||
const perMachine = percentControl / filtered.length;
|
|
||||||
for (const machine of filtered) {
|
|
||||||
try {
|
|
||||||
await machine.handleInput('parent', 'execSequence', 'startup');
|
|
||||||
await machine.handleInput('parent', 'execMovement', perMachine);
|
|
||||||
} catch (err) {
|
|
||||||
logger?.error?.(`Failed to start machine "${machine.config?.general?.name}": ${err.message}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function _pickVariant(measurements, type, variants, position, unit) {
|
|
||||||
for (const variant of variants) {
|
|
||||||
const val = measurements.type(type).variant(variant).position(position).getCurrentValue(unit);
|
|
||||||
if (!Number.isFinite(val)) continue;
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function run(ctx, controlState) {
|
|
||||||
const { measurements, config, logger, machineGroups, levelVariants } = ctx;
|
|
||||||
const { startLevel, minLevel } = config.control.levelbased;
|
|
||||||
const levelUnit = measurements.getUnit('level');
|
|
||||||
|
|
||||||
const variants = levelVariants || ['measured', 'predicted'];
|
|
||||||
const level = _pickVariant(measurements, 'level', variants, 'atequipment', levelUnit);
|
|
||||||
if (level == null) {
|
|
||||||
logger?.warn?.('No valid level found');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Three-zone level control:
|
|
||||||
// level < minLevel → STOP (unconditional MGC shutdown)
|
|
||||||
// minLevel ≤ level < startLevel → DEAD ZONE (no-op)
|
|
||||||
// level ≥ startLevel → RUN (linear ramp → MGC)
|
|
||||||
if (level < minLevel) {
|
|
||||||
controlState.percControl = 0;
|
|
||||||
Object.values(machineGroups || {}).forEach((group) => group.turnOffAllMachines());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (level < startLevel) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const rawPercControl = _scaleLevelToFlowPercent(level, config.control.levelbased, logger);
|
|
||||||
const percControl = Math.max(0, rawPercControl);
|
|
||||||
controlState.percControl = percControl;
|
|
||||||
logger?.debug?.(`Level-based control: level=${level} percControl=${percControl}`);
|
|
||||||
|
|
||||||
await _applyMachineGroupLevelControl(machineGroups, percControl, logger);
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
name: 'levelbased',
|
|
||||||
run,
|
|
||||||
// Exposed for future reuse / tests; not part of the strategy contract.
|
|
||||||
_scaleLevelToFlowPercent,
|
|
||||||
_applyMachineGroupLevelControl,
|
|
||||||
_applyMachineLevelControl,
|
|
||||||
};
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
async function run() {
|
|
||||||
// No-op: manual mode is event-driven via set.demand → forwardDemand,
|
|
||||||
// not tick-driven.
|
|
||||||
}
|
|
||||||
|
|
||||||
async function forwardDemand(ctx, demand) {
|
|
||||||
const { machineGroups, machines, logger } = ctx;
|
|
||||||
logger?.info?.(`Manual demand forwarded: ${demand}`);
|
|
||||||
|
|
||||||
if (machineGroups && Object.keys(machineGroups).length > 0) {
|
|
||||||
await Promise.all(
|
|
||||||
Object.values(machineGroups).map((group) =>
|
|
||||||
group.handleInput('parent', demand).catch((err) => {
|
|
||||||
logger?.error?.(`Failed to forward demand to group: ${err.message}`);
|
|
||||||
})
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (machines && Object.keys(machines).length > 0) {
|
|
||||||
const perMachine = demand / Object.keys(machines).length;
|
|
||||||
for (const machine of Object.values(machines)) {
|
|
||||||
try {
|
|
||||||
await machine.handleInput('parent', 'execMovement', perMachine);
|
|
||||||
} catch (err) {
|
|
||||||
logger?.error?.(`Failed to forward demand to machine: ${err.message}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
name: 'manual',
|
|
||||||
run,
|
|
||||||
forwardDemand,
|
|
||||||
};
|
|
||||||
281
src/editor.js
281
src/editor.js
@@ -1,281 +0,0 @@
|
|||||||
(function () {
|
|
||||||
// Namespace declaration — Node-RED admin scripts share window state.
|
|
||||||
window.EVOLV = window.EVOLV || {};
|
|
||||||
window.EVOLV.nodes = window.EVOLV.nodes || {};
|
|
||||||
window.EVOLV.nodes.pumpingStation = window.EVOLV.nodes.pumpingStation || {};
|
|
||||||
|
|
||||||
// SVG diagram constants — viewBox-coordinate top/bottom of the tank rect.
|
|
||||||
const DIAG = { topY: 40, botY: 380 };
|
|
||||||
|
|
||||||
const fNum = (id) => {
|
|
||||||
const v = parseFloat(document.getElementById(`node-input-${id}`)?.value);
|
|
||||||
return Number.isFinite(v) ? v : null;
|
|
||||||
};
|
|
||||||
|
|
||||||
const yForLevel = (val, basinH) => {
|
|
||||||
if (val == null || !basinH) return null;
|
|
||||||
const y = DIAG.botY - (val / basinH) * (DIAG.botY - DIAG.topY);
|
|
||||||
return Math.max(DIAG.topY - 8, Math.min(DIAG.botY + 8, y));
|
|
||||||
};
|
|
||||||
|
|
||||||
// Position a row — line, label, input, unit all share the same y.
|
|
||||||
const placeItem = (id, y) => {
|
|
||||||
const line = document.getElementById(`ps-line-${id}`);
|
|
||||||
const label = document.getElementById(`ps-label-${id}`);
|
|
||||||
const unit = document.getElementById(`ps-unit-${id}`);
|
|
||||||
const fo = document.getElementById(`ps-fo-${id}`);
|
|
||||||
const sub = document.getElementById(`ps-sub-${id}`);
|
|
||||||
const lead = document.getElementById(`ps-leader-${id}`);
|
|
||||||
if (line) { line.setAttribute('y1', y); line.setAttribute('y2', y); }
|
|
||||||
if (label) label.setAttribute('y', y + 4);
|
|
||||||
if (unit) unit.setAttribute('y', y + 4);
|
|
||||||
if (fo) fo.setAttribute('y', y - 11);
|
|
||||||
if (sub) sub.setAttribute('y', y + 15);
|
|
||||||
if (lead) lead.setAttribute('visibility', 'hidden');
|
|
||||||
};
|
|
||||||
|
|
||||||
const placeZone = (zoneId, topId, botId, items) => {
|
|
||||||
const el = document.getElementById(`ps-zone-${zoneId}`);
|
|
||||||
if (!el) return;
|
|
||||||
const top = items.find(it => it.id === topId);
|
|
||||||
const bot = items.find(it => it.id === botId);
|
|
||||||
if (!top || !bot || (bot.y - top.y) < 14) {
|
|
||||||
el.setAttribute('visibility', 'hidden'); return;
|
|
||||||
}
|
|
||||||
el.setAttribute('y', (top.y + bot.y) / 2 + 3);
|
|
||||||
el.setAttribute('visibility', 'visible');
|
|
||||||
};
|
|
||||||
|
|
||||||
const computeStack = (basinH) => {
|
|
||||||
const basedOn = document.getElementById('node-input-minHeightBasedOn')?.value || 'outlet';
|
|
||||||
const refLow = basedOn === 'inlet' ? fNum('inflowLevel') : fNum('outflowLevel');
|
|
||||||
const dryPct = fNum('dryRunThresholdPercent');
|
|
||||||
const ovfPct = fNum('overfillThresholdPercent');
|
|
||||||
const ovf = fNum('overflowLevel');
|
|
||||||
const dryLvl = (refLow != null && dryPct != null) ? refLow * (1 + dryPct / 100) : null;
|
|
||||||
const ovfLvl = (ovf != null && ovfPct != null) ? ovf * (ovfPct / 100) : null;
|
|
||||||
|
|
||||||
// Right-column stack. TWO anchors: basinHeight at the rim (top),
|
|
||||||
// outflowLevel at its proportional y (bottom). Two passes nudge
|
|
||||||
// intermediate items by GAP so dashed lines keep their value-order.
|
|
||||||
const items = [
|
|
||||||
{ id: 'basinHeight', yIdeal: DIAG.topY, pinned: true },
|
|
||||||
{ id: 'overflowLevel', yIdeal: yForLevel(fNum('overflowLevel'), basinH) },
|
|
||||||
{ id: 'maxLevel', yIdeal: yForLevel(fNum('maxLevel'), basinH) },
|
|
||||||
{ id: 'startLevel', yIdeal: yForLevel(fNum('startLevel'), basinH) },
|
|
||||||
{ id: 'minLevel', yIdeal: yForLevel(fNum('minLevel'), basinH) },
|
|
||||||
{ id: 'dryRunLevel', yIdeal: yForLevel(dryLvl, basinH) },
|
|
||||||
{ id: 'outflowLevel', yIdeal: yForLevel(fNum('outflowLevel'), basinH), pinned: true },
|
|
||||||
].filter(it => it.yIdeal != null);
|
|
||||||
|
|
||||||
const GAP = 36;
|
|
||||||
items.sort((a, b) => a.yIdeal - b.yIdeal);
|
|
||||||
for (const it of items) it.y = it.yIdeal;
|
|
||||||
for (let i = 1; i < items.length; i++) {
|
|
||||||
if (items[i].pinned) continue;
|
|
||||||
items[i].y = Math.max(items[i].y, items[i - 1].y + GAP);
|
|
||||||
}
|
|
||||||
for (let i = items.length - 2; i >= 0; i--) {
|
|
||||||
if (items[i].pinned) continue;
|
|
||||||
items[i].y = Math.min(items[i].y, items[i + 1].y - GAP);
|
|
||||||
}
|
|
||||||
return { items, dryLvl, ovfLvl };
|
|
||||||
};
|
|
||||||
|
|
||||||
const drawInflow = (basinH) => {
|
|
||||||
const inflowY = yForLevel(fNum('inflowLevel'), basinH);
|
|
||||||
if (inflowY == null) return;
|
|
||||||
const line = document.getElementById('ps-line-inflowLevel');
|
|
||||||
const lbl = document.getElementById('ps-label-inflowLevel');
|
|
||||||
const sub = document.getElementById('ps-sub-inflowLevel');
|
|
||||||
const fo = document.getElementById('ps-fo-inflowLevel');
|
|
||||||
const unit = document.getElementById('ps-unit-inflowLevel');
|
|
||||||
if (line) { line.setAttribute('y1', inflowY); line.setAttribute('y2', inflowY); }
|
|
||||||
if (lbl) lbl.setAttribute('y', inflowY - 4);
|
|
||||||
if (sub) sub.setAttribute('y', inflowY + 8);
|
|
||||||
if (fo) fo.setAttribute('y', inflowY - 11);
|
|
||||||
if (unit) unit.setAttribute('y', inflowY + 4);
|
|
||||||
};
|
|
||||||
|
|
||||||
const drawOrderingWarning = () => {
|
|
||||||
const warn = document.getElementById('ps-warning');
|
|
||||||
if (!warn) return;
|
|
||||||
const issues = [];
|
|
||||||
const pairs = [
|
|
||||||
['outflowLevel', 'inflowLevel', '<'],
|
|
||||||
['inflowLevel', 'overflowLevel', '<'],
|
|
||||||
['minLevel', 'startLevel', '<='],
|
|
||||||
['startLevel', 'maxLevel', '<'],
|
|
||||||
['maxLevel', 'overflowLevel', '<='],
|
|
||||||
];
|
|
||||||
for (const [a, b, op] of pairs) {
|
|
||||||
const av = fNum(a), bv = fNum(b);
|
|
||||||
if (av == null || bv == null) continue;
|
|
||||||
if (op === '<' ? !(av < bv) : !(av <= bv)) issues.push(`${a} ${op} ${b}`);
|
|
||||||
}
|
|
||||||
if (issues.length) {
|
|
||||||
warn.setAttribute('visibility', 'visible');
|
|
||||||
warn.textContent = `⚠ Check ordering: ${issues.join(', ')}`;
|
|
||||||
} else {
|
|
||||||
warn.setAttribute('visibility', 'hidden');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const redraw = () => {
|
|
||||||
const basinH = fNum('basinHeight') || 5;
|
|
||||||
const { items, dryLvl, ovfLvl } = computeStack(basinH);
|
|
||||||
for (const it of items) placeItem(it.id, it.y);
|
|
||||||
|
|
||||||
placeZone('spare', 'overflowLevel', 'maxLevel', items);
|
|
||||||
placeZone('sewage', 'maxLevel', 'startLevel', items);
|
|
||||||
placeZone('buffer1', 'startLevel', 'minLevel', items);
|
|
||||||
placeZone('buffer2', 'minLevel', 'dryRunLevel', items);
|
|
||||||
|
|
||||||
// "Dead volume" sits inside the blue band between outflowLevel and the floor.
|
|
||||||
const outflowPinned = items.find(it => it.id === 'outflowLevel');
|
|
||||||
const deadLbl = document.getElementById('ps-zone-dead');
|
|
||||||
if (deadLbl && outflowPinned && (DIAG.botY - outflowPinned.y) > 14) {
|
|
||||||
deadLbl.setAttribute('y', (outflowPinned.y + DIAG.botY) / 2 + 3);
|
|
||||||
deadLbl.setAttribute('visibility', 'visible');
|
|
||||||
} else if (deadLbl) {
|
|
||||||
deadLbl.setAttribute('visibility', 'hidden');
|
|
||||||
}
|
|
||||||
|
|
||||||
drawInflow(basinH);
|
|
||||||
|
|
||||||
// Dead-volume band: from the (possibly nudged) outflow line down to the floor.
|
|
||||||
const outflowItem = items.find(it => it.id === 'outflowLevel');
|
|
||||||
const deadvol = document.getElementById('ps-deadvol');
|
|
||||||
if (deadvol && outflowItem) {
|
|
||||||
deadvol.setAttribute('y', outflowItem.y);
|
|
||||||
deadvol.setAttribute('height', Math.max(0, DIAG.botY - outflowItem.y));
|
|
||||||
}
|
|
||||||
|
|
||||||
const dryLbl = document.getElementById('ps-label-dryRunLevel');
|
|
||||||
if (dryLbl) dryLbl.textContent = dryLvl != null
|
|
||||||
? `dryRunLevel ≈ ${dryLvl.toFixed(2)} m (safety — from %)`
|
|
||||||
: 'dryRunLevel ≈ — m (safety — from %)';
|
|
||||||
|
|
||||||
const d1 = document.getElementById('derived-dryRunLevel');
|
|
||||||
if (d1) d1.textContent = dryLvl != null ? `→ dryRunLevel ≈ ${dryLvl.toFixed(2)} m` : '→ dryRunLevel ≈ — m';
|
|
||||||
const d2 = document.getElementById('derived-overfillLevel');
|
|
||||||
if (d2) d2.textContent = ovfLvl != null ? `→ overfillLevel ≈ ${ovfLvl.toFixed(2)} m` : '→ overfillLevel ≈ — m';
|
|
||||||
|
|
||||||
drawOrderingWarning();
|
|
||||||
};
|
|
||||||
|
|
||||||
const wireProtectionToggle = (toggleEl, inputEl) => {
|
|
||||||
if (!toggleEl || !inputEl) return;
|
|
||||||
const apply = () => {
|
|
||||||
inputEl.disabled = !toggleEl.checked;
|
|
||||||
inputEl.parentElement.classList.toggle('disabled', inputEl.disabled);
|
|
||||||
};
|
|
||||||
toggleEl.addEventListener('change', apply);
|
|
||||||
apply();
|
|
||||||
};
|
|
||||||
|
|
||||||
const toggleModeSections = (val) => {
|
|
||||||
document.querySelectorAll('.ps-mode-section').forEach((el) => el.style.display = 'none');
|
|
||||||
const active = document.getElementById(`ps-mode-${val}`);
|
|
||||||
if (active) active.style.display = '';
|
|
||||||
};
|
|
||||||
|
|
||||||
const setNumberField = (id, val) => {
|
|
||||||
const el = document.getElementById(id);
|
|
||||||
if (el) el.value = Number.isFinite(val) ? val : '';
|
|
||||||
};
|
|
||||||
|
|
||||||
const editor = {
|
|
||||||
init(node) {
|
|
||||||
// Defer asset/menu init until shared menu data is loaded.
|
|
||||||
const waitForMenuData = () => {
|
|
||||||
if (window.EVOLV?.nodes?.pumpingStation?.initEditor) {
|
|
||||||
window.EVOLV.nodes.pumpingStation.initEditor(node);
|
|
||||||
} else {
|
|
||||||
setTimeout(waitForMenuData, 50);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
waitForMenuData();
|
|
||||||
|
|
||||||
const refHeightEl = document.getElementById('node-input-refHeight');
|
|
||||||
if (refHeightEl) refHeightEl.value = node.refHeight || 'NAP';
|
|
||||||
|
|
||||||
const minHeightBasedOnEl = document.getElementById('node-input-minHeightBasedOn');
|
|
||||||
if (minHeightBasedOnEl) minHeightBasedOnEl.value = node.minHeightBasedOn;
|
|
||||||
|
|
||||||
const dryRunToggle = document.getElementById('node-input-enableDryRunProtection');
|
|
||||||
const dryRunPercent = document.getElementById('node-input-dryRunThresholdPercent');
|
|
||||||
const overfillToggle = document.getElementById('node-input-enableOverfillProtection');
|
|
||||||
const overfillPercent = document.getElementById('node-input-overfillThresholdPercent');
|
|
||||||
|
|
||||||
if (dryRunToggle && dryRunPercent) {
|
|
||||||
dryRunToggle.checked = !!node.enableDryRunProtection;
|
|
||||||
dryRunPercent.value = Number.isFinite(node.dryRunThresholdPercent) ? node.dryRunThresholdPercent : 2;
|
|
||||||
wireProtectionToggle(dryRunToggle, dryRunPercent);
|
|
||||||
}
|
|
||||||
if (overfillToggle && overfillPercent) {
|
|
||||||
overfillToggle.checked = !!node.enableOverfillProtection;
|
|
||||||
overfillPercent.value = Number.isFinite(node.overfillThresholdPercent) ? node.overfillThresholdPercent : 98;
|
|
||||||
wireProtectionToggle(overfillToggle, overfillPercent);
|
|
||||||
}
|
|
||||||
|
|
||||||
const timeLeftInput = document.getElementById('node-input-timeleftToFullOrEmptyThresholdSeconds');
|
|
||||||
if (timeLeftInput) {
|
|
||||||
timeLeftInput.value = Number.isFinite(node.timeleftToFullOrEmptyThresholdSeconds)
|
|
||||||
? node.timeleftToFullOrEmptyThresholdSeconds
|
|
||||||
: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
const modeSelect = document.getElementById('node-input-controlMode');
|
|
||||||
if (modeSelect) {
|
|
||||||
modeSelect.value = node.controlMode || 'none';
|
|
||||||
toggleModeSections(modeSelect.value);
|
|
||||||
modeSelect.addEventListener('change', (e) => toggleModeSections(e.target.value));
|
|
||||||
}
|
|
||||||
|
|
||||||
setNumberField('node-input-startLevel', node.startLevel);
|
|
||||||
setNumberField('node-input-minLevel', node.minLevel);
|
|
||||||
setNumberField('node-input-maxLevel', node.maxLevel);
|
|
||||||
setNumberField('node-input-flowSetpoint', node.flowSetpoint);
|
|
||||||
setNumberField('node-input-flowDeadband', node.flowDeadband);
|
|
||||||
|
|
||||||
const watched = ['basinHeight','overflowLevel','maxLevel','startLevel','minLevel','inflowLevel','outflowLevel',
|
|
||||||
'dryRunThresholdPercent','overfillThresholdPercent','minHeightBasedOn'];
|
|
||||||
for (const id of watched) {
|
|
||||||
const el = document.getElementById(`node-input-${id}`);
|
|
||||||
if (el) { el.addEventListener('input', redraw); el.addEventListener('change', redraw); }
|
|
||||||
}
|
|
||||||
setTimeout(redraw, 60);
|
|
||||||
},
|
|
||||||
|
|
||||||
save(node) {
|
|
||||||
node.refHeight = document.getElementById('node-input-refHeight').value || 'NAP';
|
|
||||||
node.minHeightBasedOn = document.getElementById('node-input-minHeightBasedOn').value || 'outlet';
|
|
||||||
node.simulator = document.getElementById('node-input-simulator').checked;
|
|
||||||
|
|
||||||
const numericFields = ['basinVolume','basinHeight','inflowLevel','outflowLevel','overflowLevel',
|
|
||||||
'basinBottomRef','timeleftToFullOrEmptyThresholdSeconds',
|
|
||||||
'dryRunThresholdPercent','overfillThresholdPercent'];
|
|
||||||
for (const field of numericFields) {
|
|
||||||
node[field] = parseFloat(document.getElementById(`node-input-${field}`).value) || 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Original code reassigned refHeight here with default '' instead of 'NAP'.
|
|
||||||
// Preserve that behaviour byte-for-byte so saved node JSON is identical.
|
|
||||||
node.refHeight = document.getElementById('node-input-refHeight').value || '';
|
|
||||||
node.enableDryRunProtection = document.getElementById('node-input-enableDryRunProtection').checked;
|
|
||||||
node.enableOverfillProtection = document.getElementById('node-input-enableOverfillProtection').checked;
|
|
||||||
|
|
||||||
node.controlMode = document.getElementById('node-input-controlMode').value || 'none';
|
|
||||||
|
|
||||||
const parseNum = (id) => parseFloat(document.getElementById(id)?.value);
|
|
||||||
node.startLevel = parseNum('node-input-startLevel');
|
|
||||||
node.minLevel = parseNum('node-input-minLevel');
|
|
||||||
node.maxLevel = parseNum('node-input-maxLevel');
|
|
||||||
node.flowSetpoint = parseNum('node-input-flowSetpoint');
|
|
||||||
node.flowDeadband = parseNum('node-input-flowDeadband');
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
window.EVOLV.nodes.pumpingStation.editor = editor;
|
|
||||||
})();
|
|
||||||
191
src/editor/basin-diagram.js
Normal file
191
src/editor/basin-diagram.js
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
// PumpingStation editor — interactive basin SVG (top of the editor).
|
||||||
|
// Places threshold lines, derived safety levels, zone labels, dead-volume
|
||||||
|
// band, and ordering warnings. Same formulas as
|
||||||
|
// specificClass._validateThresholdOrdering.
|
||||||
|
|
||||||
|
(function () {
|
||||||
|
const ns = window.PSEditor = window.PSEditor || {};
|
||||||
|
const fNum = (id) => ns.fNum(id);
|
||||||
|
|
||||||
|
// viewBox y bounds of the tank rect (now 120,40)..(240,380); width
|
||||||
|
// shrunk to 360 in the new side-panel layout. y-bounds unchanged.
|
||||||
|
const DIAG = { topY: 40, botY: 380 };
|
||||||
|
|
||||||
|
const yForLevel = (val, basinH) => {
|
||||||
|
if (val == null || !basinH) return null;
|
||||||
|
const y = DIAG.botY - (val / basinH) * (DIAG.botY - DIAG.topY);
|
||||||
|
return Math.max(DIAG.topY - 8, Math.min(DIAG.botY + 8, y));
|
||||||
|
};
|
||||||
|
|
||||||
|
// Place a row — line, label, input, unit all share the same y.
|
||||||
|
const placeItem = (id, y) => {
|
||||||
|
const line = document.getElementById(`ps-line-${id}`);
|
||||||
|
const label = document.getElementById(`ps-label-${id}`);
|
||||||
|
const unit = document.getElementById(`ps-unit-${id}`);
|
||||||
|
const fo = document.getElementById(`ps-fo-${id}`);
|
||||||
|
const sub = document.getElementById(`ps-sub-${id}`);
|
||||||
|
const lead = document.getElementById(`ps-leader-${id}`);
|
||||||
|
if (line) { line.setAttribute('y1', y); line.setAttribute('y2', y); }
|
||||||
|
if (label) label.setAttribute('y', y + 4);
|
||||||
|
if (unit) unit.setAttribute('y', y + 4);
|
||||||
|
if (fo) fo.setAttribute('y', y - 11);
|
||||||
|
if (sub) sub.setAttribute('y', y + 15);
|
||||||
|
if (lead) lead.setAttribute('visibility', 'hidden');
|
||||||
|
};
|
||||||
|
|
||||||
|
ns.basinDiagram = {
|
||||||
|
redraw() {
|
||||||
|
const basinH = fNum('basinHeight') || 5;
|
||||||
|
|
||||||
|
const refLow = fNum('outflowLevel');
|
||||||
|
const dryPct = fNum('dryRunThresholdPercent');
|
||||||
|
const highPct = fNum('highVolumeSafetyThresholdPercent');
|
||||||
|
const ovf = fNum('overflowLevel');
|
||||||
|
const dryLvl = (refLow != null && dryPct != null) ? refLow * (1 + dryPct / 100) : null;
|
||||||
|
const highLvl = (ovf != null && highPct != null) ? ovf * (highPct / 100) : null;
|
||||||
|
|
||||||
|
// Right-column stack. TWO anchors: basinHeight pinned at the rim,
|
||||||
|
// outflowLevel pinned at its proportional y. Two passes (top-down +
|
||||||
|
// bottom-up) maintain a minimum vertical gap.
|
||||||
|
const items = [
|
||||||
|
{ id: 'basinHeight', yIdeal: DIAG.topY, pinned: true },
|
||||||
|
{ id: 'overflowLevel', yIdeal: yForLevel(fNum('overflowLevel'), basinH) },
|
||||||
|
{ id: 'highVolumeSafetyLevel', yIdeal: yForLevel(highLvl, basinH) },
|
||||||
|
{ id: 'inflowLevelGuide', yIdeal: yForLevel(fNum('inflowLevel'), basinH) },
|
||||||
|
{ id: 'dryRunLevel', yIdeal: yForLevel(dryLvl, basinH) },
|
||||||
|
{ id: 'outflowLevel', yIdeal: yForLevel(fNum('outflowLevel'), basinH), pinned: true },
|
||||||
|
].filter(it => it.yIdeal != null);
|
||||||
|
|
||||||
|
const GAP = 36;
|
||||||
|
items.sort((a, b) => a.yIdeal - b.yIdeal);
|
||||||
|
for (const it of items) it.y = it.yIdeal;
|
||||||
|
for (let i = 1; i < items.length; i++) {
|
||||||
|
if (items[i].pinned) continue;
|
||||||
|
items[i].y = Math.max(items[i].y, items[i - 1].y + GAP);
|
||||||
|
}
|
||||||
|
for (let i = items.length - 2; i >= 0; i--) {
|
||||||
|
if (items[i].pinned) continue;
|
||||||
|
items[i].y = Math.min(items[i].y, items[i + 1].y - GAP);
|
||||||
|
}
|
||||||
|
for (const it of items) placeItem(it.id, it.y);
|
||||||
|
|
||||||
|
// Zone labels show only when the gap between the bracketing
|
||||||
|
// thresholds is at least MIN_ZONE_GAP px high — otherwise the label
|
||||||
|
// collides with one of the threshold labels (which sit at threshold
|
||||||
|
// y ±6 px text-height). 28 px keeps a 6 px clear gap above and
|
||||||
|
// below the zone label.
|
||||||
|
const MIN_ZONE_GAP = 28;
|
||||||
|
const placeZone = (zoneId, topId, botId) => {
|
||||||
|
const el = document.getElementById(`ps-zone-${zoneId}`);
|
||||||
|
if (!el) return;
|
||||||
|
const top = items.find(it => it.id === topId);
|
||||||
|
const bot = items.find(it => it.id === botId);
|
||||||
|
if (!top || !bot || (bot.y - top.y) < MIN_ZONE_GAP) {
|
||||||
|
el.setAttribute('visibility', 'hidden'); return;
|
||||||
|
}
|
||||||
|
el.setAttribute('y', (top.y + bot.y) / 2 + 3);
|
||||||
|
el.setAttribute('visibility', 'visible');
|
||||||
|
};
|
||||||
|
placeZone('spare', 'overflowLevel', 'highVolumeSafetyLevel');
|
||||||
|
placeZone('sewage', 'highVolumeSafetyLevel', 'inflowLevelGuide');
|
||||||
|
placeZone('buffer1', 'inflowLevelGuide', 'dryRunLevel');
|
||||||
|
placeZone('buffer2', 'dryRunLevel', 'outflowLevel');
|
||||||
|
const outflowPinned = items.find(it => it.id === 'outflowLevel');
|
||||||
|
const deadLbl = document.getElementById('ps-zone-dead');
|
||||||
|
if (deadLbl && outflowPinned && (DIAG.botY - outflowPinned.y) > 14) {
|
||||||
|
deadLbl.setAttribute('y', (outflowPinned.y + DIAG.botY) / 2 + 3);
|
||||||
|
deadLbl.setAttribute('visibility', 'visible');
|
||||||
|
} else if (deadLbl) {
|
||||||
|
deadLbl.setAttribute('visibility', 'hidden');
|
||||||
|
}
|
||||||
|
|
||||||
|
const inflowY = yForLevel(fNum('inflowLevel'), basinH);
|
||||||
|
if (inflowY != null) {
|
||||||
|
const line = document.getElementById('ps-line-inflowLevel');
|
||||||
|
const lbl = document.getElementById('ps-label-inflowLevel');
|
||||||
|
const sub = document.getElementById('ps-sub-inflowLevel');
|
||||||
|
const fo = document.getElementById('ps-fo-inflowLevel');
|
||||||
|
const unit = document.getElementById('ps-unit-inflowLevel');
|
||||||
|
if (line) { line.setAttribute('y1', inflowY); line.setAttribute('y2', inflowY); }
|
||||||
|
if (lbl) lbl.setAttribute('y', inflowY - 4);
|
||||||
|
if (sub) sub.setAttribute('y', inflowY + 8);
|
||||||
|
if (fo) fo.setAttribute('y', inflowY - 11);
|
||||||
|
if (unit) unit.setAttribute('y', inflowY + 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
const outflowItem = items.find(it => it.id === 'outflowLevel');
|
||||||
|
const deadvol = document.getElementById('ps-deadvol');
|
||||||
|
if (deadvol && outflowItem) {
|
||||||
|
deadvol.setAttribute('y', outflowItem.y);
|
||||||
|
deadvol.setAttribute('height', Math.max(0, DIAG.botY - outflowItem.y));
|
||||||
|
}
|
||||||
|
|
||||||
|
// SVG labels — keep them short, side panel shows the numeric value.
|
||||||
|
const dryLbl = document.getElementById('ps-label-dryRunLevel');
|
||||||
|
if (dryLbl) dryLbl.textContent = 'dryRunLevel';
|
||||||
|
const highLbl = document.getElementById('ps-label-highVolumeSafetyLevel');
|
||||||
|
if (highLbl) highLbl.textContent = 'highVolumeSafety';
|
||||||
|
|
||||||
|
// Side-panel read-only displays — number only ("m" is shown in the unit span).
|
||||||
|
const fmt = (v) => Number.isFinite(v) ? v.toFixed(2) : '—';
|
||||||
|
const d1 = document.getElementById('derived-dryRunLevel');
|
||||||
|
if (d1) d1.textContent = fmt(dryLvl);
|
||||||
|
const d2 = document.getElementById('derived-highVolumeSafetyLevel');
|
||||||
|
if (d2) d2.textContent = fmt(highLvl);
|
||||||
|
|
||||||
|
// Hierarchy validation. Soft '≤' relations follow the user's choice:
|
||||||
|
// start ≤ inflow, max ≤ overflow, overflow ≤ basinHeight (equality OK).
|
||||||
|
// dryRunLevel must be < startLevel strictly (otherwise the runtime
|
||||||
|
// would trip dry-run before it could ramp).
|
||||||
|
// Re-read the raw value (basinH falls back to 5 for diagram scaling;
|
||||||
|
// here we want null when the user hasn't entered anything so the
|
||||||
|
// ≤-checks below are skipped rather than false-flagged).
|
||||||
|
const basinHraw = fNum('basinHeight');
|
||||||
|
const start = fNum('startLevel');
|
||||||
|
const inlet = fNum('inflowLevel');
|
||||||
|
const max = fNum('maxLevel');
|
||||||
|
const ovfl = fNum('overflowLevel');
|
||||||
|
const issues = [];
|
||||||
|
const ok = (a, b, op) => {
|
||||||
|
if (!Number.isFinite(a) || !Number.isFinite(b)) return true;
|
||||||
|
return op === '<' ? a < b : a <= b;
|
||||||
|
};
|
||||||
|
if (Number.isFinite(refLow) && refLow <= 0)
|
||||||
|
issues.push('outflowLevel must be > 0');
|
||||||
|
if (!ok(dryLvl, start, '<'))
|
||||||
|
issues.push(`dryRunLevel (${(dryLvl ?? NaN).toFixed(2)} m, derived) must be < startLevel — lower dryRun% or raise startLevel`);
|
||||||
|
if (!ok(start, inlet, '<='))
|
||||||
|
issues.push('startLevel must be ≤ inflowLevel');
|
||||||
|
if (!ok(inlet, max, '<='))
|
||||||
|
issues.push('inflowLevel must be ≤ maxLevel');
|
||||||
|
if (!ok(max, ovfl, '<='))
|
||||||
|
issues.push('maxLevel must be ≤ overflowLevel');
|
||||||
|
if (!ok(ovfl, basinHraw, '<='))
|
||||||
|
issues.push('overflowLevel must be ≤ basinHeight');
|
||||||
|
|
||||||
|
// Visible ribbon above the basin diagram.
|
||||||
|
const warnDiv = document.getElementById('ps-basin-validation');
|
||||||
|
if (warnDiv) {
|
||||||
|
if (issues.length) {
|
||||||
|
warnDiv.innerHTML = '⚠ Fix before deploy:<ul style="margin:4px 0 0 18px;padding:0;">'
|
||||||
|
+ issues.map((i) => `<li>${i}</li>`).join('') + '</ul>';
|
||||||
|
warnDiv.style.display = '';
|
||||||
|
} else {
|
||||||
|
warnDiv.style.display = 'none';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Legacy in-SVG warning text — kept for the small reminder inside
|
||||||
|
// the diagram. Only shows the count.
|
||||||
|
const warn = document.getElementById('ps-warning');
|
||||||
|
if (warn) {
|
||||||
|
if (issues.length) {
|
||||||
|
warn.setAttribute('visibility', 'visible');
|
||||||
|
warn.textContent = `⚠ ${issues.length} ordering issue${issues.length > 1 ? 's' : ''}`;
|
||||||
|
} else {
|
||||||
|
warn.setAttribute('visibility', 'hidden');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
window._psBasinValidationIssues = issues;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
})();
|
||||||
96
src/editor/bounds.js
Normal file
96
src/editor/bounds.js
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
// PumpingStation editor — dynamic input bounds.
|
||||||
|
// Sets HTML5 min/max attributes on every level and percent input based on
|
||||||
|
// the current values of related inputs, so the up/down arrows stop at
|
||||||
|
// values that respect the basin hierarchy:
|
||||||
|
//
|
||||||
|
// 0 < outflowLevel < dryRunLevel < startLevel ≤ inflowLevel
|
||||||
|
// ≤ shiftLevel ≤ maxLevel ≤ overflowLevel ≤ basinHeight
|
||||||
|
//
|
||||||
|
// The user can still type out-of-range values via the keyboard (HTML5
|
||||||
|
// min/max only constrain the spinner). The validation ribbons in
|
||||||
|
// basin-diagram.js and mode-preview.js catch typed violations and the
|
||||||
|
// oneditsave handler blocks Deploy until they're resolved.
|
||||||
|
|
||||||
|
(function () {
|
||||||
|
const ns = window.PSEditor = window.PSEditor || {};
|
||||||
|
const fNum = (id) => ns.fNum(id);
|
||||||
|
const EPS = 0.001; // smallest meaningful step (mm-precision)
|
||||||
|
|
||||||
|
const setBounds = (id, min, max) => {
|
||||||
|
const el = document.getElementById(`node-input-${id}`);
|
||||||
|
if (!el) return;
|
||||||
|
if (Number.isFinite(min)) el.setAttribute('min', String(min));
|
||||||
|
else el.removeAttribute('min');
|
||||||
|
if (Number.isFinite(max)) el.setAttribute('max', String(max));
|
||||||
|
else el.removeAttribute('max');
|
||||||
|
};
|
||||||
|
|
||||||
|
ns.bounds = {
|
||||||
|
apply() {
|
||||||
|
const basinHeight = fNum('basinHeight');
|
||||||
|
const outflow = fNum('outflowLevel');
|
||||||
|
const dryPct = fNum('dryRunThresholdPercent');
|
||||||
|
const start = fNum('startLevel');
|
||||||
|
const inlet = fNum('inflowLevel');
|
||||||
|
const max = fNum('maxLevel');
|
||||||
|
const overflow = fNum('overflowLevel');
|
||||||
|
const shiftEnabled = !!document.getElementById('node-input-enableShiftedRamp')?.checked;
|
||||||
|
|
||||||
|
// Derived dryRunLevel (lower bound for startLevel).
|
||||||
|
const dryRun = (Number.isFinite(outflow) && Number.isFinite(dryPct))
|
||||||
|
? outflow * (1 + dryPct / 100) : null;
|
||||||
|
|
||||||
|
// Geometry — basin envelope.
|
||||||
|
setBounds('basinHeight', EPS, undefined);
|
||||||
|
setBounds('basinVolume', EPS, undefined);
|
||||||
|
|
||||||
|
// Levels (each capped by the next-higher level if defined).
|
||||||
|
setBounds('outflowLevel', EPS,
|
||||||
|
Number.isFinite(start) && Number.isFinite(dryPct)
|
||||||
|
? start / (1 + dryPct / 100) - EPS // keep dryRun < start
|
||||||
|
: (start ?? inlet ?? max ?? overflow ?? basinHeight));
|
||||||
|
|
||||||
|
setBounds('startLevel',
|
||||||
|
Number.isFinite(dryRun) ? dryRun + EPS : EPS,
|
||||||
|
inlet ?? max ?? overflow ?? basinHeight);
|
||||||
|
|
||||||
|
setBounds('inflowLevel',
|
||||||
|
start ?? EPS,
|
||||||
|
max ?? overflow ?? basinHeight);
|
||||||
|
|
||||||
|
setBounds('maxLevel',
|
||||||
|
inlet ?? start ?? EPS,
|
||||||
|
overflow ?? basinHeight);
|
||||||
|
|
||||||
|
setBounds('overflowLevel',
|
||||||
|
max ?? inlet ?? start ?? EPS,
|
||||||
|
basinHeight);
|
||||||
|
|
||||||
|
// stopLevel — explicit pump-off threshold. Must sit between
|
||||||
|
// dryRunLevel and startLevel (so it can be reached during draining
|
||||||
|
// before pumps re-engage).
|
||||||
|
setBounds('stopLevel',
|
||||||
|
Number.isFinite(dryRun) ? dryRun + EPS : EPS,
|
||||||
|
start ?? inlet ?? max ?? overflow ?? basinHeight);
|
||||||
|
|
||||||
|
// Shift inputs (only relevant when shifted ramp enabled).
|
||||||
|
if (shiftEnabled) {
|
||||||
|
setBounds('shiftLevel',
|
||||||
|
Number.isFinite(start) ? start : EPS,
|
||||||
|
max ?? overflow ?? basinHeight);
|
||||||
|
setBounds('shiftArmPercent', 1, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Percentages.
|
||||||
|
// dryRun% capped so dryRunLevel ≤ startLevel.
|
||||||
|
let dryMax = 99;
|
||||||
|
if (Number.isFinite(start) && Number.isFinite(outflow) && outflow > 0) {
|
||||||
|
dryMax = Math.max(0, Math.min(99, ((start / outflow) - 1) * 100));
|
||||||
|
}
|
||||||
|
setBounds('dryRunThresholdPercent', 0, dryMax);
|
||||||
|
|
||||||
|
// highVol% bounded (1, 100). Equal to 100 means no margin to overflow.
|
||||||
|
setBounds('highVolumeSafetyThresholdPercent', 1, 100);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
})();
|
||||||
29
src/editor/hover-couple.js
Normal file
29
src/editor/hover-couple.js
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
// PumpingStation editor — hover-coupling between side-panel input rows
|
||||||
|
// and the SVG markers they control. Each .ps-row that carries
|
||||||
|
// data-couples-line="<svg-element-id>" highlights that SVG line on
|
||||||
|
// mouseenter and clears the highlight on mouseleave.
|
||||||
|
|
||||||
|
(function () {
|
||||||
|
const ns = window.PSEditor = window.PSEditor || {};
|
||||||
|
|
||||||
|
ns.hoverCouple = {
|
||||||
|
init() {
|
||||||
|
document.querySelectorAll('.ps-diag-side .ps-row[data-couples-line]').forEach((row) => {
|
||||||
|
const targetId = row.getAttribute('data-couples-line');
|
||||||
|
const target = document.getElementById(targetId);
|
||||||
|
if (!target) return;
|
||||||
|
const enter = () => target.classList.add('ps-line-highlight');
|
||||||
|
const leave = () => target.classList.remove('ps-line-highlight');
|
||||||
|
row.addEventListener('mouseenter', enter);
|
||||||
|
row.addEventListener('mouseleave', leave);
|
||||||
|
// Also highlight while the input inside the row has focus, so
|
||||||
|
// the user keeps the visual feedback while typing.
|
||||||
|
const input = row.querySelector('input');
|
||||||
|
if (input) {
|
||||||
|
input.addEventListener('focus', enter);
|
||||||
|
input.addEventListener('blur', leave);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
})();
|
||||||
30
src/editor/index.js
Normal file
30
src/editor/index.js
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
// PumpingStation editor — shared namespace + helpers.
|
||||||
|
// Loaded first by pumpingStation.html via /pumpingStation/editor/index.js.
|
||||||
|
// Each sibling module attaches additional members to window.PSEditor.
|
||||||
|
|
||||||
|
(function () {
|
||||||
|
const ns = window.PSEditor = window.PSEditor || {};
|
||||||
|
|
||||||
|
// Read a numeric value from an input by node-input-<id>; null if blank/NaN.
|
||||||
|
ns.fNum = (id) => {
|
||||||
|
const v = parseFloat(document.getElementById(`node-input-${id}`)?.value);
|
||||||
|
return Number.isFinite(v) ? v : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Set a numeric input's value, or blank if not finite.
|
||||||
|
ns.setNumberField = (id, val) => {
|
||||||
|
const el = document.getElementById(id);
|
||||||
|
if (el) el.value = Number.isFinite(val) ? val : '';
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add input + change listeners to a list of node-input-* ids.
|
||||||
|
ns.bindRedraw = (ids, handler) => {
|
||||||
|
ids.forEach((id) => {
|
||||||
|
const el = document.getElementById(`node-input-${id}`);
|
||||||
|
if (el) {
|
||||||
|
el.addEventListener('input', handler);
|
||||||
|
el.addEventListener('change', handler);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
})();
|
||||||
286
src/editor/mode-preview.js
Normal file
286
src/editor/mode-preview.js
Normal file
@@ -0,0 +1,286 @@
|
|||||||
|
// PumpingStation editor — level-based mode preview SVG.
|
||||||
|
// Draws zone bands, level markers, the up curve (inflowLevel→maxLevel) and
|
||||||
|
// the optional shifted-down curve (startLevel→shiftLevel). Computes
|
||||||
|
// validation issues and stashes them on window._psModeValidationIssues
|
||||||
|
// for oneditsave to read.
|
||||||
|
|
||||||
|
(function () {
|
||||||
|
const ns = window.PSEditor = window.PSEditor || {};
|
||||||
|
const fNum = (id) => ns.fNum(id);
|
||||||
|
|
||||||
|
// Derive dryRunLevel the same way the basin diagram does.
|
||||||
|
// dryRunLevel = outflowLevel × (1 + dryRunThresholdPercent/100).
|
||||||
|
// Returns null if either input is missing.
|
||||||
|
ns.deriveDryRunLevel = () => {
|
||||||
|
const refLow = fNum('outflowLevel');
|
||||||
|
const dryPct = fNum('dryRunThresholdPercent');
|
||||||
|
if (refLow == null || dryPct == null) return null;
|
||||||
|
return refLow * (1 + dryPct / 100);
|
||||||
|
};
|
||||||
|
|
||||||
|
ns.modePreview = {
|
||||||
|
redraw() {
|
||||||
|
const svg = document.getElementById('ps-levelbased-mode-diagram');
|
||||||
|
if (!svg) return;
|
||||||
|
const start = fNum('startLevel');
|
||||||
|
const inlet = fNum('inflowLevel');
|
||||||
|
const max = fNum('maxLevel');
|
||||||
|
// Optional stopLevel — explicit pump-off threshold. Drawn as its
|
||||||
|
// own marker line; does NOT shift the ramp foot. Must be < startLevel
|
||||||
|
// for the marker to render.
|
||||||
|
const stopRaw = fNum('stopLevel');
|
||||||
|
const stop = Number.isFinite(stopRaw) && stopRaw >= 0 && Number.isFinite(start) && stopRaw < start ? stopRaw : null;
|
||||||
|
// dryRunLevel is derived from the basin's outflowLevel + dryRun%
|
||||||
|
// (no separate input). Below dryRunLevel the runtime hard-stops;
|
||||||
|
// we draw it as the leftmost vertical marker so the user sees
|
||||||
|
// exactly where it lands.
|
||||||
|
const dryRun = ns.deriveDryRunLevel();
|
||||||
|
const overflow = fNum('overflowLevel');
|
||||||
|
const shiftEnabled = !!document.getElementById('node-input-enableShiftedRamp')?.checked;
|
||||||
|
const shiftRaw = fNum('shiftLevel');
|
||||||
|
const shift = Number.isFinite(shiftRaw) && shiftRaw > 0 ? Math.min(shiftRaw, max ?? shiftRaw) : null;
|
||||||
|
const armRaw = fNum('shiftArmPercent');
|
||||||
|
const armPct = Number.isFinite(armRaw) ? Math.max(0, Math.min(100, armRaw)) : 95;
|
||||||
|
const curveType = document.getElementById('node-input-levelCurveType')?.value || 'linear';
|
||||||
|
const factorRaw = parseFloat(document.getElementById('node-input-logCurveFactor')?.value);
|
||||||
|
const factor = Number.isFinite(factorRaw) && factorRaw > 0 ? factorRaw : 9;
|
||||||
|
|
||||||
|
// Plot window is FIXED relative to basin geometry so that moving any
|
||||||
|
// single level slides only that line, not all the others. Lower bound
|
||||||
|
// is the basin floor (0); upper bound is overflowLevel (or maxLevel
|
||||||
|
// if overflow isn't set) plus a small margin.
|
||||||
|
const upperRefs = [max, overflow].filter(Number.isFinite);
|
||||||
|
const upperBase = upperRefs.length ? Math.max(...upperRefs) : 1;
|
||||||
|
const pad = Math.max(upperBase * 0.05, 0.1);
|
||||||
|
const levelMin = 0;
|
||||||
|
const levelMax = upperBase + pad;
|
||||||
|
|
||||||
|
// Plot rectangle (viewBox px).
|
||||||
|
const x0 = 52, x1 = 390, y0 = 140, y1 = 24;
|
||||||
|
const yOffPx = 160;
|
||||||
|
const yOffPct = -((yOffPx - y0) / (y0 - y1)) * 100;
|
||||||
|
const xFor = (level) => x0 + ((level - levelMin) / (levelMax - levelMin)) * (x1 - x0);
|
||||||
|
const yForPct = (pct) => y0 - (pct / 100) * (y0 - y1);
|
||||||
|
const scale = (x) => {
|
||||||
|
const clamped = Math.max(0, Math.min(1, x));
|
||||||
|
if (curveType === 'log') return Math.log1p(factor * clamped) / Math.log1p(factor);
|
||||||
|
return clamped;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Path with three flat regions and a ramp:
|
||||||
|
// [levelMin..startX] OFF (pump off; below startLevel)
|
||||||
|
// [startX..footX] 0 % (system armed but not yet ramping)
|
||||||
|
// [footX..topX] ramp (linear or log scaled 0..100 %)
|
||||||
|
// [topX..levelMax] 100 % (saturated)
|
||||||
|
// Up curve: startX=startLevel, footX=inflowLevel, topX=maxLevel.
|
||||||
|
// Shifted-down: startX=footX=startLevel, topX=shiftLevel.
|
||||||
|
const buildPath = (startX, footX, topX) => {
|
||||||
|
if (![startX, footX, topX].every(Number.isFinite) || topX <= footX) return '';
|
||||||
|
const pts = [];
|
||||||
|
pts.push(`${xFor(levelMin)},${yForPct(yOffPct)}`);
|
||||||
|
pts.push(`${xFor(startX)},${yForPct(yOffPct)}`);
|
||||||
|
pts.push(`${xFor(startX)},${yForPct(0)}`);
|
||||||
|
if (footX > startX) pts.push(`${xFor(footX)},${yForPct(0)}`);
|
||||||
|
for (let i = 0; i <= 24; i++) {
|
||||||
|
const t = i / 24;
|
||||||
|
const level = footX + t * (topX - footX);
|
||||||
|
pts.push(`${xFor(level)},${yForPct(scale(t) * 100)}`);
|
||||||
|
}
|
||||||
|
pts.push(`${xFor(levelMax)},${yForPct(100)}`);
|
||||||
|
return pts.join(' ');
|
||||||
|
};
|
||||||
|
|
||||||
|
// Up curve. Foot is startLevel (the configured pump-on threshold and
|
||||||
|
// ramp foot per the runtime in _controlLevelBased). The OFF baseline
|
||||||
|
// is drawn for level < startLevel; at startLevel demand jumps from
|
||||||
|
// OFF to 0 % and ramps up to 100 % at maxLevel.
|
||||||
|
const up = document.getElementById('ps-mode-curve-up');
|
||||||
|
const down = document.getElementById('ps-mode-curve-down');
|
||||||
|
const downLabel = document.getElementById('ps-mode-curve-down-label');
|
||||||
|
if (up) up.setAttribute('points', buildPath(start, start, max));
|
||||||
|
|
||||||
|
// Shifted-DOWN curve (only when shift enabled): represents the
|
||||||
|
// worst-case held-then-ramp path drawn for hold=100 % (the SVG
|
||||||
|
// ideal). Geometry: 100 % flat from levelMax back to shiftLevel,
|
||||||
|
// then linear/log ramp from (shiftLevel, 100 %) down to
|
||||||
|
// (startLevel, 0 %), then OFF below startLevel.
|
||||||
|
// Real runtime hold value depends on where direction flips, so the
|
||||||
|
// preview shows the maximum extent.
|
||||||
|
const buildShiftedDown = () => {
|
||||||
|
if (![start, shift].every(Number.isFinite) || shift <= start) return '';
|
||||||
|
const pts = [];
|
||||||
|
// OFF baseline far-left to startLevel
|
||||||
|
pts.push(`${xFor(levelMin)},${yForPct(yOffPct)}`);
|
||||||
|
pts.push(`${xFor(start)},${yForPct(yOffPct)}`);
|
||||||
|
// Jump 0 % at startLevel
|
||||||
|
pts.push(`${xFor(start)},${yForPct(0)}`);
|
||||||
|
// Ramp start→shift = 0..100 % (peak hold = 100 % for this preview)
|
||||||
|
for (let i = 0; i <= 24; i++) {
|
||||||
|
const t = i / 24;
|
||||||
|
const lvl = start + t * (shift - start);
|
||||||
|
pts.push(`${xFor(lvl)},${yForPct(scale(t) * 100)}`);
|
||||||
|
}
|
||||||
|
// Held at 100 % from shift → far-right
|
||||||
|
pts.push(`${xFor(levelMax)},${yForPct(100)}`);
|
||||||
|
return pts.join(' ');
|
||||||
|
};
|
||||||
|
if (down) {
|
||||||
|
if (shiftEnabled) {
|
||||||
|
down.setAttribute('points', buildShiftedDown());
|
||||||
|
down.style.display = '';
|
||||||
|
if (downLabel) downLabel.style.display = '';
|
||||||
|
} else {
|
||||||
|
down.setAttribute('points', '');
|
||||||
|
down.style.display = 'none';
|
||||||
|
if (downLabel) downLabel.style.display = 'none';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Horizontal arming-% line — only meaningful when shift enabled.
|
||||||
|
const armLine = document.getElementById('ps-mode-line-armPercent');
|
||||||
|
const armLabel = document.getElementById('ps-mode-label-armPercent');
|
||||||
|
if (armLine && armLabel) {
|
||||||
|
if (shiftEnabled) {
|
||||||
|
const yArm = yForPct(armPct);
|
||||||
|
armLine.setAttribute('y1', yArm);
|
||||||
|
armLine.setAttribute('y2', yArm);
|
||||||
|
armLabel.setAttribute('y', yArm - 2);
|
||||||
|
armLabel.textContent = `arm ${Math.round(armPct)}%`;
|
||||||
|
armLine.style.display = '';
|
||||||
|
armLabel.style.display = '';
|
||||||
|
} else {
|
||||||
|
armLine.style.display = 'none';
|
||||||
|
armLabel.style.display = 'none';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vertical level markers — line only. Axis labels were removed;
|
||||||
|
// identification comes from line colour + side-panel labels +
|
||||||
|
// hover coupling.
|
||||||
|
[
|
||||||
|
['dryRunLevel', dryRun],
|
||||||
|
['startLevel', start],
|
||||||
|
['stopLevel', stop],
|
||||||
|
['inflowLevel', inlet],
|
||||||
|
['maxLevel', max],
|
||||||
|
['overflowLevel', overflow],
|
||||||
|
].forEach(([id, level]) => {
|
||||||
|
const line = document.getElementById(`ps-mode-line-${id}`);
|
||||||
|
if (!line) return;
|
||||||
|
if (!Number.isFinite(level)) {
|
||||||
|
line.style.display = 'none';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const x = xFor(level);
|
||||||
|
line.style.display = '';
|
||||||
|
line.setAttribute('x1', x); line.setAttribute('x2', x);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Background zone bands.
|
||||||
|
const plotL = xFor(levelMin);
|
||||||
|
const plotR = xFor(levelMax);
|
||||||
|
const setBand = (id, a, b) => {
|
||||||
|
const r = document.getElementById(id);
|
||||||
|
if (!r) return;
|
||||||
|
if (!Number.isFinite(a) || !Number.isFinite(b) || b <= a) {
|
||||||
|
r.setAttribute('x', 0); r.setAttribute('width', 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
r.setAttribute('x', a);
|
||||||
|
r.setAttribute('width', b - a);
|
||||||
|
};
|
||||||
|
const xMin = Number.isFinite(dryRun) ? xFor(dryRun) : plotL;
|
||||||
|
const xStart = Number.isFinite(start) ? xFor(start) : xMin;
|
||||||
|
const xMax = Number.isFinite(max) ? xFor(max) : plotR;
|
||||||
|
const xOvf = Number.isFinite(overflow) ? xFor(overflow) : xMax;
|
||||||
|
setBand('ps-zone-dryRun', plotL, xMin);
|
||||||
|
setBand('ps-zone-safetyLow', xMin, xStart);
|
||||||
|
setBand('ps-zone-safe', xStart, xMax);
|
||||||
|
setBand('ps-zone-safetyHigh', xMax, xOvf);
|
||||||
|
setBand('ps-zone-overflow', xOvf, plotR);
|
||||||
|
|
||||||
|
// Shift level marker (line only).
|
||||||
|
const shiftLine = document.getElementById('ps-mode-line-shiftLevel');
|
||||||
|
if (shiftLine) {
|
||||||
|
if (shiftEnabled && Number.isFinite(shift)) {
|
||||||
|
const x = xFor(shift);
|
||||||
|
shiftLine.setAttribute('x1', x); shiftLine.setAttribute('x2', x);
|
||||||
|
shiftLine.style.display = '';
|
||||||
|
} else {
|
||||||
|
shiftLine.style.display = 'none';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Title + row visibility.
|
||||||
|
const curveLabel = document.getElementById('ps-mode-curve-label');
|
||||||
|
if (curveLabel) curveLabel.textContent = curveType === 'log' ? 'log curve: fast early response' : 'linear curve';
|
||||||
|
const shiftRow = document.getElementById('ps-shiftLevel-row');
|
||||||
|
if (shiftRow) shiftRow.style.display = shiftEnabled ? '' : 'none';
|
||||||
|
const armRow = document.getElementById('ps-shiftArmPercent-row');
|
||||||
|
if (armRow) armRow.style.display = shiftEnabled ? '' : 'none';
|
||||||
|
const logRow = document.getElementById('ps-log-factor-row');
|
||||||
|
if (logRow) logRow.style.display = curveType === 'log' ? '' : 'none';
|
||||||
|
|
||||||
|
// Auto-default shiftLevel when shift is enabled and current value
|
||||||
|
// is missing/out-of-range. Visible default avoids a hidden ramp.
|
||||||
|
const shiftInput = document.getElementById('node-input-shiftLevel');
|
||||||
|
if (shiftEnabled && shiftInput && Number.isFinite(max)) {
|
||||||
|
const cur = parseFloat(shiftInput.value);
|
||||||
|
if (!Number.isFinite(cur) || cur <= 0 || cur >= max) {
|
||||||
|
shiftInput.value = (max * 0.9).toFixed(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Auto-default shiftArmPercent to 95 % when shift is enabled and the
|
||||||
|
// current value is missing / out of [0, 100].
|
||||||
|
const armInput = document.getElementById('node-input-shiftArmPercent');
|
||||||
|
if (shiftEnabled && armInput) {
|
||||||
|
const cur = parseFloat(armInput.value);
|
||||||
|
if (!Number.isFinite(cur) || cur < 0 || cur > 100) {
|
||||||
|
armInput.value = 95;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validation: only mode-specific (shift) ordering. Basin-level
|
||||||
|
// hierarchy (start ≤ inlet ≤ max ≤ overflow ≤ basinHeight,
|
||||||
|
// dryRun < start) is owned by basin-diagram.js so it shows in the
|
||||||
|
// basin section near the offending inputs.
|
||||||
|
const issues = [];
|
||||||
|
if (shiftEnabled) {
|
||||||
|
const shiftVal = Number(shiftInput?.value);
|
||||||
|
if (Number.isFinite(shiftVal)) {
|
||||||
|
if (Number.isFinite(start) && shiftVal <= start)
|
||||||
|
issues.push('shiftLevel must be > startLevel');
|
||||||
|
if (Number.isFinite(max) && shiftVal > max)
|
||||||
|
issues.push('shiftLevel must be ≤ maxLevel');
|
||||||
|
} else {
|
||||||
|
issues.push('shiftLevel is required when shifted ramp is enabled');
|
||||||
|
}
|
||||||
|
const armVal = Number(armInput?.value);
|
||||||
|
if (!Number.isFinite(armVal) || armVal <= 0 || armVal > 100)
|
||||||
|
issues.push('shiftArmPercent must be in (0, 100]');
|
||||||
|
}
|
||||||
|
const warnBox = document.getElementById('ps-mode-validation');
|
||||||
|
if (warnBox) {
|
||||||
|
if (issues.length) {
|
||||||
|
warnBox.innerHTML = '⚠ Fix before deploy:<ul style="margin:4px 0 0 18px;padding:0;">'
|
||||||
|
+ issues.map((i) => `<li>${i}</li>`).join('') + '</ul>';
|
||||||
|
warnBox.style.display = '';
|
||||||
|
} else {
|
||||||
|
warnBox.style.display = 'none';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
window._psModeValidationIssues = issues;
|
||||||
|
|
||||||
|
// Read-only readouts in the side panel — number only; the row's
|
||||||
|
// .ps-unit span already shows "m".
|
||||||
|
const fmt = (v) => Number.isFinite(v) ? v.toFixed(2) : '—';
|
||||||
|
const setText = (id, val) => {
|
||||||
|
const el = document.getElementById(id);
|
||||||
|
if (el) el.textContent = fmt(val);
|
||||||
|
};
|
||||||
|
setText('ps-mode-readout-dryRun', dryRun);
|
||||||
|
setText('ps-mode-readout-inflow', inlet);
|
||||||
|
setText('ps-mode-readout-overflow', overflow);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
})();
|
||||||
114
src/editor/oneditprepare.js
Normal file
114
src/editor/oneditprepare.js
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
// PumpingStation editor — oneditprepare entry. Wires up form-field
|
||||||
|
// initialization, control-mode toggle, safety toggles, and binds
|
||||||
|
// redraws for the basin diagram + level-based mode preview.
|
||||||
|
|
||||||
|
(function () {
|
||||||
|
const ns = window.PSEditor = window.PSEditor || {};
|
||||||
|
|
||||||
|
ns.oneditprepare = function () {
|
||||||
|
const node = this;
|
||||||
|
|
||||||
|
// Wait for menu data (asset/logger/position dropdowns) before init.
|
||||||
|
const waitForMenuData = () => {
|
||||||
|
if (window.EVOLV?.nodes?.pumpingStation?.initEditor) {
|
||||||
|
window.EVOLV.nodes.pumpingStation.initEditor(node);
|
||||||
|
} else {
|
||||||
|
setTimeout(waitForMenuData, 50);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
waitForMenuData();
|
||||||
|
|
||||||
|
const refHeightEl = document.getElementById('node-input-refHeight');
|
||||||
|
if (refHeightEl) refHeightEl.value = node.refHeight || 'NAP';
|
||||||
|
|
||||||
|
// Safety toggle pairs — each toggle enables/disables its threshold input.
|
||||||
|
const dryRunToggle = document.getElementById('node-input-enableDryRunProtection');
|
||||||
|
const dryRunPercent = document.getElementById('node-input-dryRunThresholdPercent');
|
||||||
|
const highVolumeToggle = document.getElementById('node-input-enableHighVolumeSafety');
|
||||||
|
const highVolumePercent = document.getElementById('node-input-highVolumeSafetyThresholdPercent');
|
||||||
|
|
||||||
|
const toggleInput = (toggleEl, inputEl) => {
|
||||||
|
if (!toggleEl || !inputEl) return;
|
||||||
|
inputEl.disabled = !toggleEl.checked;
|
||||||
|
inputEl.parentElement.classList.toggle('disabled', inputEl.disabled);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (dryRunToggle && dryRunPercent) {
|
||||||
|
dryRunToggle.checked = !!node.enableDryRunProtection;
|
||||||
|
dryRunPercent.value = Number.isFinite(node.dryRunThresholdPercent) ? node.dryRunThresholdPercent : 2;
|
||||||
|
dryRunToggle.addEventListener('change', () => toggleInput(dryRunToggle, dryRunPercent));
|
||||||
|
toggleInput(dryRunToggle, dryRunPercent);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (highVolumeToggle && highVolumePercent) {
|
||||||
|
highVolumeToggle.checked = node.enableHighVolumeSafety !== undefined
|
||||||
|
? !!node.enableHighVolumeSafety
|
||||||
|
: !!node.enableOverfillProtection;
|
||||||
|
const highVolumePct = node.highVolumeSafetyThresholdPercent ?? node.overfillThresholdPercent;
|
||||||
|
highVolumePercent.value = Number.isFinite(highVolumePct) ? highVolumePct : 98;
|
||||||
|
highVolumeToggle.addEventListener('change', () => toggleInput(highVolumeToggle, highVolumePercent));
|
||||||
|
toggleInput(highVolumeToggle, highVolumePercent);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Control-mode section toggle (levelbased / manual).
|
||||||
|
const toggleModeSections = (val) => {
|
||||||
|
document.querySelectorAll('.ps-mode-section').forEach((el) => el.style.display = 'none');
|
||||||
|
const active = document.getElementById(`ps-mode-${val}`);
|
||||||
|
if (active) active.style.display = '';
|
||||||
|
};
|
||||||
|
const modeSelect = document.getElementById('node-input-controlMode');
|
||||||
|
if (modeSelect) {
|
||||||
|
modeSelect.value = node.controlMode === 'manual' ? 'manual' : 'levelbased';
|
||||||
|
toggleModeSections(modeSelect.value);
|
||||||
|
modeSelect.addEventListener('change', (e) => toggleModeSections(e.target.value));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Numeric field defaults.
|
||||||
|
ns.setNumberField('node-input-startLevel', node.startLevel);
|
||||||
|
ns.setNumberField('node-input-maxLevel', node.maxLevel);
|
||||||
|
ns.setNumberField('node-input-logCurveFactor', node.logCurveFactor);
|
||||||
|
ns.setNumberField('node-input-shiftLevel', node.shiftLevel);
|
||||||
|
ns.setNumberField('node-input-shiftArmPercent', Number.isFinite(node.shiftArmPercent) ? node.shiftArmPercent : 95);
|
||||||
|
ns.setNumberField('node-input-flowSetpoint', node.flowSetpoint);
|
||||||
|
ns.setNumberField('node-input-flowDeadband', node.flowDeadband);
|
||||||
|
|
||||||
|
const curveSelect = document.getElementById('node-input-levelCurveType');
|
||||||
|
if (curveSelect) curveSelect.value = node.levelCurveType || node.curveType || 'linear';
|
||||||
|
const shiftCheckbox = document.getElementById('node-input-enableShiftedRamp');
|
||||||
|
if (shiftCheckbox) shiftCheckbox.checked = !!node.enableShiftedRamp;
|
||||||
|
|
||||||
|
// Bind redraws to the inputs each diagram cares about.
|
||||||
|
ns.bindRedraw(
|
||||||
|
['basinHeight', 'overflowLevel', 'inflowLevel', 'outflowLevel',
|
||||||
|
'dryRunThresholdPercent', 'highVolumeSafetyThresholdPercent'],
|
||||||
|
ns.basinDiagram.redraw
|
||||||
|
);
|
||||||
|
ns.bindRedraw(
|
||||||
|
// dryRunLevel is derived (outflowLevel + dryRunThresholdPercent),
|
||||||
|
// so the mode preview must redraw when either of those change.
|
||||||
|
['startLevel', 'maxLevel', 'inflowLevel', 'outflowLevel', 'overflowLevel',
|
||||||
|
'dryRunThresholdPercent',
|
||||||
|
'levelCurveType', 'logCurveFactor', 'enableShiftedRamp', 'shiftLevel',
|
||||||
|
'shiftArmPercent'],
|
||||||
|
ns.modePreview.redraw
|
||||||
|
);
|
||||||
|
|
||||||
|
// Whenever any level/percent input changes, refresh the bounds first
|
||||||
|
// so the next redraw + validation sees the correct min/max attrs.
|
||||||
|
ns.bindRedraw(
|
||||||
|
['basinHeight', 'basinVolume', 'overflowLevel', 'maxLevel',
|
||||||
|
'inflowLevel', 'startLevel', 'outflowLevel',
|
||||||
|
'dryRunThresholdPercent', 'highVolumeSafetyThresholdPercent',
|
||||||
|
'enableShiftedRamp', 'shiftLevel', 'shiftArmPercent'],
|
||||||
|
() => ns.bounds?.apply()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Initial render + hover-couple wiring once the DOM is settled.
|
||||||
|
setTimeout(() => {
|
||||||
|
ns.bounds?.apply();
|
||||||
|
ns.basinDiagram.redraw();
|
||||||
|
ns.modePreview.redraw();
|
||||||
|
ns.hoverCouple?.init();
|
||||||
|
}, 60);
|
||||||
|
};
|
||||||
|
})();
|
||||||
69
src/editor/oneditsave.js
Normal file
69
src/editor/oneditsave.js
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
// PumpingStation editor — oneditsave handler. Validates, saves shared
|
||||||
|
// menu sections (logger/position), then persists pumpingStation-specific
|
||||||
|
// fields onto the node. Throws if validation fails to keep the editor open.
|
||||||
|
|
||||||
|
(function () {
|
||||||
|
const ns = window.PSEditor = window.PSEditor || {};
|
||||||
|
|
||||||
|
const parseNum = (id) => parseFloat(document.getElementById(id)?.value);
|
||||||
|
|
||||||
|
ns.oneditsave = function () {
|
||||||
|
const node = this;
|
||||||
|
|
||||||
|
// Block save if EITHER validator surfaced any issues. basin-diagram
|
||||||
|
// owns hierarchy issues (start ≤ inlet ≤ max ≤ overflow ≤ basinHeight,
|
||||||
|
// dryRun < start). mode-preview owns shift-specific issues.
|
||||||
|
const basinIssues = window._psBasinValidationIssues || [];
|
||||||
|
const modeIssues = window._psModeValidationIssues || [];
|
||||||
|
const issues = [...basinIssues, ...modeIssues];
|
||||||
|
if (issues.length) {
|
||||||
|
if (typeof RED !== 'undefined' && RED.notify) {
|
||||||
|
RED.notify('PumpingStation config invalid:<br>• ' + issues.join('<br>• '),
|
||||||
|
{ type: 'error', timeout: 6000 });
|
||||||
|
}
|
||||||
|
throw new Error('PumpingStation: invalid config — ' + issues.join('; '));
|
||||||
|
}
|
||||||
|
|
||||||
|
window.EVOLV?.nodes?.pumpingStation?.loggerMenu?.saveEditor?.(node);
|
||||||
|
window.EVOLV?.nodes?.pumpingStation?.positionMenu?.saveEditor?.(node);
|
||||||
|
|
||||||
|
node.refHeight = document.getElementById('node-input-refHeight').value || 'NAP';
|
||||||
|
node.simulator = document.getElementById('node-input-simulator').checked;
|
||||||
|
|
||||||
|
[
|
||||||
|
'basinVolume', 'basinHeight', 'inflowLevel', 'outflowLevel', 'overflowLevel',
|
||||||
|
'basinBottomRef',
|
||||||
|
'dryRunThresholdPercent', 'highVolumeSafetyThresholdPercent',
|
||||||
|
].forEach((field) => {
|
||||||
|
const el = document.getElementById(`node-input-${field}`);
|
||||||
|
if (el) node[field] = parseFloat(el.value) || 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
node.enableDryRunProtection = document.getElementById('node-input-enableDryRunProtection').checked;
|
||||||
|
node.enableHighVolumeSafety = document.getElementById('node-input-enableHighVolumeSafety').checked;
|
||||||
|
// Deprecated aliases kept for existing runtime/schema compatibility.
|
||||||
|
node.enableOverfillProtection = node.enableHighVolumeSafety;
|
||||||
|
node.overfillThresholdPercent = node.highVolumeSafetyThresholdPercent;
|
||||||
|
|
||||||
|
node.controlMode = document.getElementById('node-input-controlMode').value || 'levelbased';
|
||||||
|
node.levelCurveType = document.getElementById('node-input-levelCurveType')?.value || 'linear';
|
||||||
|
node.logCurveFactor = parseNum('node-input-logCurveFactor');
|
||||||
|
node.startLevel = parseNum('node-input-startLevel');
|
||||||
|
node.maxLevel = parseNum('node-input-maxLevel');
|
||||||
|
// minLevel is no longer a user input — it's the derived dryRunLevel
|
||||||
|
// (outflowLevel × (1 + dryRunThresholdPercent/100)). The runtime still
|
||||||
|
// uses node.minLevel as the unconditional STOP threshold; we set it
|
||||||
|
// here so that semantic survives the UI change.
|
||||||
|
const _dryRun = ns.deriveDryRunLevel?.();
|
||||||
|
if (Number.isFinite(_dryRun)) node.minLevel = _dryRun;
|
||||||
|
node.enableShiftedRamp = !!document.getElementById('node-input-enableShiftedRamp')?.checked;
|
||||||
|
const shiftLevelVal = parseNum('node-input-shiftLevel');
|
||||||
|
node.shiftLevel = Number.isFinite(shiftLevelVal) ? shiftLevelVal : 0;
|
||||||
|
const armPctVal = parseNum('node-input-shiftArmPercent');
|
||||||
|
node.shiftArmPercent = Number.isFinite(armPctVal) ? armPctVal : 95;
|
||||||
|
const flowSetpoint = parseNum('node-input-flowSetpoint');
|
||||||
|
const flowDeadband = parseNum('node-input-flowDeadband');
|
||||||
|
if (Number.isFinite(flowSetpoint)) node.flowSetpoint = flowSetpoint;
|
||||||
|
if (Number.isFinite(flowDeadband)) node.flowDeadband = flowDeadband;
|
||||||
|
};
|
||||||
|
})();
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
// Calibration helpers for the pumping-station predicted volume / level
|
|
||||||
// streams. Pure functions over a context bag holding the live
|
|
||||||
// MeasurementContainer + basin geometry. After every calibration the
|
|
||||||
// integrator state is reset so the next tick starts from the new anchor.
|
|
||||||
|
|
||||||
function _resetFlowState(ctx, timestamp) {
|
|
||||||
if (ctx.flowAggregator?.resetState) {
|
|
||||||
ctx.flowAggregator.resetState(timestamp);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ctx._predictedFlowState = { inflow: 0, outflow: 0, lastTimestamp: timestamp };
|
|
||||||
}
|
|
||||||
|
|
||||||
function _clearSeries(measurements, type) {
|
|
||||||
const series = measurements.type(type).variant('predicted').position('atequipment');
|
|
||||||
if (series.exists()) {
|
|
||||||
const m = series.get();
|
|
||||||
if (m) {
|
|
||||||
m.values = [];
|
|
||||||
m.timestamps = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function _levelFromVolume(basin, volume) {
|
|
||||||
const area = basin.surfaceArea;
|
|
||||||
return area > 0 ? Math.max(volume, 0) / area : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
function _volumeFromLevel(basin, level) {
|
|
||||||
const area = basin.surfaceArea;
|
|
||||||
return area > 0 ? Math.max(level, 0) * area : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
function calibratePredictedVolume(ctx, calibratedVol, timestamp = Date.now()) {
|
|
||||||
if (!ctx?.measurements || !ctx.basin) {
|
|
||||||
throw new Error('calibratePredictedVolume: ctx.measurements and ctx.basin required');
|
|
||||||
}
|
|
||||||
const { measurements, basin } = ctx;
|
|
||||||
|
|
||||||
_clearSeries(measurements, 'volume');
|
|
||||||
_clearSeries(measurements, 'level');
|
|
||||||
|
|
||||||
measurements.type('volume').variant('predicted').position('atequipment')
|
|
||||||
.value(calibratedVol, timestamp, 'm3').unit('m3');
|
|
||||||
measurements.type('level').variant('predicted').position('atequipment')
|
|
||||||
.value(_levelFromVolume(basin, calibratedVol), timestamp, 'm');
|
|
||||||
|
|
||||||
_resetFlowState(ctx, timestamp);
|
|
||||||
}
|
|
||||||
|
|
||||||
function calibratePredictedLevel(ctx, level, timestamp = Date.now(), unit = 'm') {
|
|
||||||
if (!ctx?.measurements || !ctx.basin) {
|
|
||||||
throw new Error('calibratePredictedLevel: ctx.measurements and ctx.basin required');
|
|
||||||
}
|
|
||||||
const { measurements, basin } = ctx;
|
|
||||||
|
|
||||||
_clearSeries(measurements, 'volume');
|
|
||||||
_clearSeries(measurements, 'level');
|
|
||||||
|
|
||||||
measurements.type('level').variant('predicted').position('atequipment')
|
|
||||||
.value(level, timestamp, unit);
|
|
||||||
measurements.type('volume').variant('predicted').position('atequipment')
|
|
||||||
.value(_volumeFromLevel(basin, level), timestamp, 'm3');
|
|
||||||
|
|
||||||
_resetFlowState(ctx, timestamp);
|
|
||||||
}
|
|
||||||
|
|
||||||
function setManualInflow(ctx, value, timestamp = Date.now(), unit = 'm3/s') {
|
|
||||||
if (!ctx?.measurements) throw new Error('setManualInflow: ctx.measurements required');
|
|
||||||
const num = Number(value);
|
|
||||||
ctx.measurements.type('flow').variant('predicted').position('in').child('manual-qin')
|
|
||||||
.value(num, timestamp, unit);
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
calibratePredictedVolume,
|
|
||||||
calibratePredictedLevel,
|
|
||||||
setManualInflow,
|
|
||||||
};
|
|
||||||
@@ -1,180 +0,0 @@
|
|||||||
// FlowAggregator — owns the predicted-volume integrator + net-flow selection
|
|
||||||
// + remaining-time projection for the pumping-station basin.
|
|
||||||
//
|
|
||||||
// Pure domain. Takes a context bag with the live MeasurementContainer, the
|
|
||||||
// basin geometry, and the merged config; mutates measurements in place and
|
|
||||||
// keeps a tiny piece of integrator state internally.
|
|
||||||
|
|
||||||
const { interpolation } = require('generalFunctions');
|
|
||||||
|
|
||||||
const DEFAULT_FLOW_THRESHOLD = 1e-4;
|
|
||||||
const DEFAULT_FLOW_VARIANTS = ['measured', 'predicted'];
|
|
||||||
const DEFAULT_LEVEL_VARIANTS = ['measured', 'predicted'];
|
|
||||||
const DEFAULT_FLOW_POSITIONS = {
|
|
||||||
inflow: ['in', 'upstream'],
|
|
||||||
outflow: ['out', 'downstream'],
|
|
||||||
};
|
|
||||||
|
|
||||||
class FlowAggregator {
|
|
||||||
constructor(ctx = {}) {
|
|
||||||
if (!ctx.measurements) throw new Error('FlowAggregator: ctx.measurements is required');
|
|
||||||
if (!ctx.basin) throw new Error('FlowAggregator: ctx.basin is required');
|
|
||||||
|
|
||||||
this.measurements = ctx.measurements;
|
|
||||||
this.basin = ctx.basin;
|
|
||||||
this.config = ctx.config || {};
|
|
||||||
this.logger = ctx.logger || null;
|
|
||||||
this._interp = ctx.interpolation || new interpolation();
|
|
||||||
|
|
||||||
this.flowVariants = ctx.flowVariants || DEFAULT_FLOW_VARIANTS;
|
|
||||||
this.levelVariants = ctx.levelVariants || DEFAULT_LEVEL_VARIANTS;
|
|
||||||
this.flowPositions = ctx.flowPositions || DEFAULT_FLOW_POSITIONS;
|
|
||||||
|
|
||||||
const cfgThresh = Number(this.config?.general?.flowThreshold);
|
|
||||||
this.flowThreshold = Number.isFinite(ctx.flowThreshold)
|
|
||||||
? ctx.flowThreshold
|
|
||||||
: (Number.isFinite(cfgThresh) ? cfgThresh : DEFAULT_FLOW_THRESHOLD);
|
|
||||||
|
|
||||||
this._predictedFlowState = null;
|
|
||||||
this._lastNetFlow = { value: 0, source: null, direction: 'steady' };
|
|
||||||
this._lastRemaining = { seconds: null, source: null };
|
|
||||||
}
|
|
||||||
|
|
||||||
resetState(timestamp = Date.now()) {
|
|
||||||
this._predictedFlowState = { inflow: 0, outflow: 0, lastTimestamp: timestamp };
|
|
||||||
}
|
|
||||||
|
|
||||||
update() {
|
|
||||||
const flowUnit = 'm3/s';
|
|
||||||
const now = Date.now();
|
|
||||||
|
|
||||||
const inflow = this.measurements.sum('flow', 'predicted', this.flowPositions.inflow, flowUnit) || 0;
|
|
||||||
const outflow = this.measurements.sum('flow', 'predicted', this.flowPositions.outflow, flowUnit) || 0;
|
|
||||||
|
|
||||||
if (!this._predictedFlowState) this._predictedFlowState = { inflow, outflow, lastTimestamp: now };
|
|
||||||
|
|
||||||
const tPrev = this._predictedFlowState.lastTimestamp ?? now;
|
|
||||||
const dt = Math.max((now - tPrev) / 1000, 0);
|
|
||||||
const dV = dt > 0 ? (inflow - outflow) * dt : 0;
|
|
||||||
|
|
||||||
const volSeries = this.measurements.type('volume').variant('predicted').position('atequipment');
|
|
||||||
const currentVol = volSeries.getCurrentValue('m3');
|
|
||||||
const nextVol = (currentVol ?? this.basin.minVol ?? 0) + dV;
|
|
||||||
const writeTs = tPrev + dt * 1000;
|
|
||||||
|
|
||||||
volSeries.value(nextVol, writeTs, 'm3').unit('m3');
|
|
||||||
|
|
||||||
const surfaceArea = this.basin.surfaceArea;
|
|
||||||
const nextLevel = surfaceArea > 0 ? Math.max(nextVol, 0) / surfaceArea : 0;
|
|
||||||
this.measurements.type('level').variant('predicted').position('atequipment')
|
|
||||||
.value(nextLevel, writeTs, 'm').unit('m');
|
|
||||||
|
|
||||||
const percent = this._interp.interpolate_lin_single_point(
|
|
||||||
nextVol, this.basin.minVol, this.basin.maxVolAtOverflow, 0, 100
|
|
||||||
);
|
|
||||||
this.measurements.type('volumePercent').variant('predicted').position('atequipment')
|
|
||||||
.value(percent, writeTs, '%');
|
|
||||||
|
|
||||||
this._predictedFlowState = { inflow, outflow, lastTimestamp: writeTs };
|
|
||||||
}
|
|
||||||
|
|
||||||
selectBestNetFlow() {
|
|
||||||
const type = 'flow';
|
|
||||||
const unit = this.measurements.getUnit(type) || 'm3/s';
|
|
||||||
|
|
||||||
for (const variant of this.flowVariants) {
|
|
||||||
const bucket = this.measurements.measurements?.[type]?.[variant];
|
|
||||||
if (!bucket || Object.keys(bucket).length === 0) continue;
|
|
||||||
|
|
||||||
const inflow = this.measurements.sum(type, variant, this.flowPositions.inflow, unit) || 0;
|
|
||||||
const outflow = this.measurements.sum(type, variant, this.flowPositions.outflow, unit) || 0;
|
|
||||||
if (Math.abs(inflow) < this.flowThreshold && Math.abs(outflow) < this.flowThreshold) continue;
|
|
||||||
|
|
||||||
const net = inflow - outflow;
|
|
||||||
this.measurements.type('netFlowRate').variant(variant).position('atequipment')
|
|
||||||
.value(net, Date.now(), unit);
|
|
||||||
const result = { value: net, source: variant, direction: this.deriveDirection(net) };
|
|
||||||
this._lastNetFlow = result;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const variant of this.levelVariants) {
|
|
||||||
const rate = this._levelRate(variant);
|
|
||||||
if (!Number.isFinite(rate)) continue;
|
|
||||||
const net = rate * this.basin.surfaceArea;
|
|
||||||
const result = { value: net, source: `level:${variant}`, direction: this.deriveDirection(net) };
|
|
||||||
this._lastNetFlow = result;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.logger) this.logger.warn('No usable measurements to compute net flow; assuming steady.');
|
|
||||||
const result = { value: 0, source: null, direction: 'steady' };
|
|
||||||
this._lastNetFlow = result;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
computeRemainingTime(netFlow) {
|
|
||||||
if (!netFlow || Math.abs(netFlow.value) < this.flowThreshold) {
|
|
||||||
this._lastRemaining = { seconds: null, source: null };
|
|
||||||
return this._lastRemaining;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { overflowLevel, outflowLevel, surfaceArea } = this.basin;
|
|
||||||
if (!Number.isFinite(surfaceArea) || surfaceArea <= 0) {
|
|
||||||
this._lastRemaining = { seconds: null, source: null };
|
|
||||||
return this._lastRemaining;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const variant of this.levelVariants) {
|
|
||||||
const lvl = this.measurements.type('level').variant(variant).position('atequipment').getCurrentValue('m');
|
|
||||||
if (!Number.isFinite(lvl)) continue;
|
|
||||||
|
|
||||||
const remainingHeight = netFlow.value > 0
|
|
||||||
? Math.max(overflowLevel - lvl, 0)
|
|
||||||
: Math.max(lvl - outflowLevel, 0);
|
|
||||||
const seconds = (remainingHeight * surfaceArea) / Math.abs(netFlow.value);
|
|
||||||
if (!Number.isFinite(seconds)) continue;
|
|
||||||
|
|
||||||
this._lastRemaining = { seconds, source: `${netFlow.source}/${variant}` };
|
|
||||||
return this._lastRemaining;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._lastRemaining = { seconds: null, source: netFlow.source };
|
|
||||||
return this._lastRemaining;
|
|
||||||
}
|
|
||||||
|
|
||||||
deriveDirection(netFlow) {
|
|
||||||
if (netFlow > this.flowThreshold) return 'filling';
|
|
||||||
if (netFlow < -this.flowThreshold) return 'draining';
|
|
||||||
return 'steady';
|
|
||||||
}
|
|
||||||
|
|
||||||
tick() {
|
|
||||||
this.update();
|
|
||||||
const netFlow = this.selectBestNetFlow();
|
|
||||||
const remaining = this.computeRemainingTime(netFlow);
|
|
||||||
return { netFlow, remaining };
|
|
||||||
}
|
|
||||||
|
|
||||||
snapshot() {
|
|
||||||
return {
|
|
||||||
direction: this._lastNetFlow.direction,
|
|
||||||
netFlow: this._lastNetFlow.value,
|
|
||||||
flowSource: this._lastNetFlow.source,
|
|
||||||
secondsRemaining: this._lastRemaining.seconds,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
_levelRate(variant) {
|
|
||||||
const m = this.measurements.type('level').variant(variant).position('atequipment').get();
|
|
||||||
if (!m || !m.values || m.values.length < 2) return null;
|
|
||||||
const current = m.getLaggedSample?.(0);
|
|
||||||
const previous = m.getLaggedSample?.(1);
|
|
||||||
if (!current || !previous || previous.timestamp == null) return null;
|
|
||||||
const dt = (current.timestamp - previous.timestamp) / 1000;
|
|
||||||
if (!Number.isFinite(dt) || dt <= 0) return null;
|
|
||||||
return (current.value - previous.value) / dt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = FlowAggregator;
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
// MeasurementRouter — dispatches incoming measurement updates by type and
|
|
||||||
// derives downstream measurements (volume from level, predicted level from
|
|
||||||
// pressure). Pure domain over a context bag; no Node-RED dependency.
|
|
||||||
|
|
||||||
const { coolprop, interpolation } = require('generalFunctions');
|
|
||||||
|
|
||||||
const G = 9.80665;
|
|
||||||
const ASSUMED_TEMPERATURE_C = 15;
|
|
||||||
const ATMOSPHERIC_PRESSURE_PA = 101325;
|
|
||||||
|
|
||||||
class MeasurementRouter {
|
|
||||||
constructor(ctx = {}) {
|
|
||||||
if (!ctx.measurements) throw new Error('MeasurementRouter: ctx.measurements is required');
|
|
||||||
if (!ctx.basin) throw new Error('MeasurementRouter: ctx.basin is required');
|
|
||||||
|
|
||||||
this.measurements = ctx.measurements;
|
|
||||||
this.basin = ctx.basin;
|
|
||||||
this.logger = ctx.logger || null;
|
|
||||||
this._interp = ctx.interpolation || new interpolation();
|
|
||||||
}
|
|
||||||
|
|
||||||
route(measurementType, value, position, eventData = {}) {
|
|
||||||
switch (measurementType) {
|
|
||||||
case 'level':
|
|
||||||
this.onLevelMeasurement(position, value, eventData);
|
|
||||||
return true;
|
|
||||||
case 'pressure':
|
|
||||||
this.onPressureMeasurement(position, value, eventData);
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
onLevelMeasurement(position, value, context = {}) {
|
|
||||||
this.measurements.type('level').variant('measured').position(position)
|
|
||||||
.value(value).unit(context.unit);
|
|
||||||
|
|
||||||
const series = this.measurements.type('level').variant('measured').position(position);
|
|
||||||
const levelMeters = series.getCurrentValue('m');
|
|
||||||
if (levelMeters == null) return;
|
|
||||||
|
|
||||||
const surfaceArea = this.basin.surfaceArea;
|
|
||||||
const volume = surfaceArea > 0 ? Math.max(levelMeters, 0) * surfaceArea : 0;
|
|
||||||
const percent = this._interp.interpolate_lin_single_point(
|
|
||||||
volume, this.basin.minVol, this.basin.maxVolAtOverflow, 0, 100
|
|
||||||
);
|
|
||||||
|
|
||||||
this.measurements.type('volume').variant('measured').position('atequipment')
|
|
||||||
.value(volume, context.timestamp, 'm3');
|
|
||||||
this.measurements.type('volumePercent').variant('measured').position('atequipment')
|
|
||||||
.value(percent, context.timestamp, '%');
|
|
||||||
}
|
|
||||||
|
|
||||||
onPressureMeasurement(position, value, context = {}) {
|
|
||||||
let kelvin = this.measurements
|
|
||||||
.type('temperature').variant('measured').position('atequipment')
|
|
||||||
.getCurrentValue('K') ?? null;
|
|
||||||
|
|
||||||
if (kelvin === null) {
|
|
||||||
if (this.logger) {
|
|
||||||
this.logger.warn('No temperature measurement; assuming 15C for pressure to level conversion.');
|
|
||||||
}
|
|
||||||
this.measurements.type('temperature').variant('assumed').position('atequipment')
|
|
||||||
.value(ASSUMED_TEMPERATURE_C, Date.now(), 'C');
|
|
||||||
kelvin = this.measurements.type('temperature').variant('assumed').position('atequipment')
|
|
||||||
.getCurrentValue('K');
|
|
||||||
}
|
|
||||||
if (kelvin == null) return;
|
|
||||||
|
|
||||||
const density = coolprop.PropsSI('D', 'T', kelvin, 'P', ATMOSPHERIC_PRESSURE_PA, 'Water');
|
|
||||||
const pressurePa = this.measurements.type('pressure').variant('measured').position(position)
|
|
||||||
.getCurrentValue('Pa');
|
|
||||||
if (!Number.isFinite(pressurePa) || !Number.isFinite(density)) return;
|
|
||||||
|
|
||||||
const level = pressurePa / (density * G);
|
|
||||||
this.measurements.type('level').variant('predicted').position(position)
|
|
||||||
.value(level, context.timestamp, 'm');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = MeasurementRouter;
|
|
||||||
282
src/nodeClass.js
282
src/nodeClass.js
@@ -1,44 +1,288 @@
|
|||||||
const { BaseNodeAdapter } = require('generalFunctions');
|
|
||||||
const PumpingStation = require('./specificClass');
|
|
||||||
const commands = require('./commands');
|
|
||||||
|
|
||||||
class nodeClass extends BaseNodeAdapter {
|
const { outputUtils, configManager } = require('generalFunctions');
|
||||||
static DomainClass = PumpingStation;
|
const Specific = require("./specificClass");
|
||||||
static commands = commands;
|
|
||||||
// Tick-driven: predicted-volume integrator needs delta-time per second.
|
|
||||||
static tickInterval = 1000;
|
|
||||||
static statusInterval = 1000;
|
|
||||||
|
|
||||||
buildDomainConfig(uiConfig) {
|
class nodeClass {
|
||||||
return {
|
/**
|
||||||
|
* Create a node.
|
||||||
|
* @param {object} uiConfig - Node-RED node configuration.
|
||||||
|
* @param {object} RED - Node-RED runtime API.
|
||||||
|
* @param {object} nodeInstance - The Node-RED node instance.
|
||||||
|
* @param {string} nameOfNode - The name of the node, used for
|
||||||
|
*/
|
||||||
|
constructor(uiConfig, RED, nodeInstance, nameOfNode) {
|
||||||
|
|
||||||
|
// Preserve RED reference for HTTP endpoints if needed
|
||||||
|
this.node = nodeInstance;
|
||||||
|
this.RED = RED;
|
||||||
|
this.name = nameOfNode;
|
||||||
|
|
||||||
|
// Load default & UI config
|
||||||
|
this._loadConfig(uiConfig,this.node);
|
||||||
|
|
||||||
|
// Instantiate core class
|
||||||
|
this._setupSpecificClass();
|
||||||
|
|
||||||
|
// Wire up event and lifecycle handlers
|
||||||
|
this._bindEvents();
|
||||||
|
this._registerChild();
|
||||||
|
this._startTickLoop();
|
||||||
|
this._attachInputHandler();
|
||||||
|
this._attachCloseHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load and merge default config with user-defined settings.
|
||||||
|
* @param {object} uiConfig - Raw config from Node-RED UI.
|
||||||
|
*/
|
||||||
|
_loadConfig(uiConfig,node) {
|
||||||
|
const cfgMgr = new configManager();
|
||||||
|
this.defaultConfig = cfgMgr.getConfig(this.name);
|
||||||
|
|
||||||
|
// Build config: base sections + pumpingStation-specific domain config
|
||||||
|
this.config = cfgMgr.buildConfig(this.name, uiConfig, node.id, {
|
||||||
basin: {
|
basin: {
|
||||||
volume: uiConfig.basinVolume,
|
volume: uiConfig.basinVolume,
|
||||||
height: uiConfig.basinHeight,
|
height: uiConfig.basinHeight,
|
||||||
inflowLevel: uiConfig.inflowLevel,
|
inflowLevel: uiConfig.inflowLevel,
|
||||||
outflowLevel: uiConfig.outflowLevel,
|
outflowLevel: uiConfig.outflowLevel,
|
||||||
overflowLevel: uiConfig.overflowLevel,
|
overflowLevel: uiConfig.overflowLevel,
|
||||||
|
inletPipeDiameter: uiConfig.inletPipeDiameter,
|
||||||
|
outletPipeDiameter: uiConfig.outletPipeDiameter,
|
||||||
},
|
},
|
||||||
hydraulics: {
|
hydraulics: {
|
||||||
refHeight: uiConfig.refHeight,
|
refHeight: uiConfig.refHeight,
|
||||||
minHeightBasedOn: uiConfig.minHeightBasedOn,
|
minHeightBasedOn: uiConfig.minHeightBasedOn,
|
||||||
basinBottomRef: uiConfig.basinBottomRef,
|
basinBottomRef: uiConfig.basinBottomRef,
|
||||||
|
maxInflowRate: uiConfig.maxInflowRate,
|
||||||
|
staticHead: uiConfig.staticHead,
|
||||||
|
maxDischargeHead: uiConfig.maxDischargeHead,
|
||||||
|
pipelineLength: uiConfig.pipelineLength,
|
||||||
|
defaultFluid: uiConfig.defaultFluid,
|
||||||
|
temperatureReferenceDegC: uiConfig.temperatureReferenceDegC,
|
||||||
},
|
},
|
||||||
control: {
|
control:{
|
||||||
mode: uiConfig.controlMode,
|
mode: uiConfig.controlMode,
|
||||||
levelbased: {
|
levelbased:{
|
||||||
minLevel: uiConfig.minLevel,
|
minLevel:uiConfig.minLevel,
|
||||||
startLevel: uiConfig.startLevel,
|
startLevel:uiConfig.startLevel,
|
||||||
maxLevel: uiConfig.maxLevel,
|
stopLevel: uiConfig.stopLevel,
|
||||||
},
|
maxLevel:uiConfig.maxLevel,
|
||||||
|
curveType: uiConfig.levelCurveType || uiConfig.curveType,
|
||||||
|
logCurveFactor: uiConfig.logCurveFactor,
|
||||||
|
enableShiftedRamp: uiConfig.enableShiftedRamp,
|
||||||
|
shiftLevel: uiConfig.shiftLevel,
|
||||||
|
shiftArmPercent: uiConfig.shiftArmPercent
|
||||||
|
}
|
||||||
},
|
},
|
||||||
safety: {
|
safety:{
|
||||||
enableDryRunProtection: uiConfig.enableDryRunProtection,
|
enableDryRunProtection: uiConfig.enableDryRunProtection,
|
||||||
dryRunThresholdPercent: uiConfig.dryRunThresholdPercent,
|
dryRunThresholdPercent: uiConfig.dryRunThresholdPercent,
|
||||||
|
enableHighVolumeSafety: uiConfig.enableHighVolumeSafety ?? uiConfig.enableOverfillProtection,
|
||||||
|
highVolumeSafetyThresholdPercent: uiConfig.highVolumeSafetyThresholdPercent ?? uiConfig.overfillThresholdPercent,
|
||||||
enableOverfillProtection: uiConfig.enableOverfillProtection,
|
enableOverfillProtection: uiConfig.enableOverfillProtection,
|
||||||
overfillThresholdPercent: uiConfig.overfillThresholdPercent,
|
overfillThresholdPercent: uiConfig.overfillThresholdPercent,
|
||||||
timeleftToFullOrEmptyThresholdSeconds: uiConfig.timeleftToFullOrEmptyThresholdSeconds,
|
timeleftToFullOrEmptyThresholdSeconds: uiConfig.timeleftToFullOrEmptyThresholdSeconds
|
||||||
},
|
},
|
||||||
|
output: {
|
||||||
|
process: uiConfig.processOutputFormat,
|
||||||
|
dbase: uiConfig.dbaseOutputFormat
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Utility for formatting outputs
|
||||||
|
this._output = new outputUtils();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiate the core logic and store as source.
|
||||||
|
*/
|
||||||
|
_setupSpecificClass() {
|
||||||
|
this.source = new Specific(this.config);
|
||||||
|
this.node.source = this.source; // Store the source in the node instance for easy access
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind Node-RED status updates.
|
||||||
|
*/
|
||||||
|
_bindEvents() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// init registration msg
|
||||||
|
_registerChild() {
|
||||||
|
setTimeout(() => {
|
||||||
|
this.node.send([
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
{ topic: 'registerChild', payload: this.node.id , positionVsParent: this.config?.functionality?.positionVsParent || 'atEquipment' , distance: this.config?.functionality?.distance || null},
|
||||||
|
]);
|
||||||
|
}, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
_updateNodeStatus() {
|
||||||
|
const ps = this.source;
|
||||||
|
|
||||||
|
const pickVariant = (type, prefer = ['measured', 'predicted'], position = 'atEquipment', unit) => {
|
||||||
|
for (const variant of prefer) {
|
||||||
|
const chain = ps.measurements.type(type).variant(variant).position(position);
|
||||||
|
const value = unit ? chain.getCurrentValue(unit) : chain.getCurrentValue();
|
||||||
|
if (value != null) return { value, variant };
|
||||||
|
}
|
||||||
|
return { value: null, variant: null };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const vol = pickVariant('volume', ['measured', 'predicted'], 'atEquipment', 'm3');
|
||||||
|
const volPercent = pickVariant('volumePercent', ['measured','predicted'], 'atEquipment'); // already unitless
|
||||||
|
const level = pickVariant('level', ['measured', 'predicted'], 'atEquipment', 'm');
|
||||||
|
const netFlow = pickVariant('netFlowRate', ['measured', 'predicted'], 'atEquipment', 'm3/h');
|
||||||
|
|
||||||
|
const maxVolBeforeOverflow = ps.basin?.maxVolAtOverflow ?? ps.basin?.maxVol ?? 0;
|
||||||
|
const currentVolume = vol.value ?? 0;
|
||||||
|
const currentvolPercent = volPercent.value ?? 0;
|
||||||
|
const netFlowM3h = netFlow.value ?? 0;
|
||||||
|
|
||||||
|
const direction = ps.state?.direction ?? 'unknown';
|
||||||
|
const secondsRemaining = ps.state?.seconds ?? null;
|
||||||
|
const timeRemainingMinutes = secondsRemaining != null ? Math.round(secondsRemaining / 60) : null;
|
||||||
|
|
||||||
|
const badgePieces = [];
|
||||||
|
badgePieces.push(`${currentvolPercent.toFixed(1)}% `);
|
||||||
|
badgePieces.push(
|
||||||
|
`V=${currentVolume.toFixed(2)} / ${maxVolBeforeOverflow.toFixed(2)} m³`
|
||||||
|
);
|
||||||
|
badgePieces.push(`net: ${netFlowM3h.toFixed(0)} m³/h`);
|
||||||
|
if (timeRemainingMinutes != null) {
|
||||||
|
badgePieces.push(`t≈${timeRemainingMinutes} min)`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { symbol, fill } = (() => {
|
||||||
|
switch (direction) {
|
||||||
|
case 'filling': return { symbol: '⬆️', fill: 'blue' };
|
||||||
|
case 'draining': return { symbol: '⬇️', fill: 'orange' };
|
||||||
|
case 'steady': return { symbol: '⏸️', fill: 'green' };
|
||||||
|
default: return { symbol: '❔', fill: 'grey' };
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
badgePieces[0] = `${symbol} ${badgePieces[0]}`;
|
||||||
|
|
||||||
|
return {
|
||||||
|
fill,
|
||||||
|
shape: 'dot',
|
||||||
|
text: badgePieces.join(' | ')
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a single tick: update measurement, format and send outputs.
|
||||||
|
*/
|
||||||
|
_tick() {
|
||||||
|
|
||||||
|
//pumping station needs time based ticks to recalc level when predicted
|
||||||
|
this.source.tick();
|
||||||
|
const raw = this.source.getOutput();
|
||||||
|
const processMsg = this._output.formatMsg(raw, this.source.config, 'process');
|
||||||
|
const influxMsg = this._output.formatMsg(raw, this.source.config, 'influxdb');
|
||||||
|
|
||||||
|
// Send only updated outputs on ports 0 & 1
|
||||||
|
this.node.send([processMsg, influxMsg]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attach the node's input handler, routing control messages to the class.
|
||||||
|
*/
|
||||||
|
_attachInputHandler() {
|
||||||
|
this.node.on('input', (msg, send, done) => {
|
||||||
|
switch (msg.topic) {
|
||||||
|
//example
|
||||||
|
case 'changemode':
|
||||||
|
this.source.changeMode(msg.payload);
|
||||||
|
break;
|
||||||
|
case 'registerChild': {
|
||||||
|
// Register this node as a child of the parent node
|
||||||
|
const childId = msg.payload;
|
||||||
|
const childObj = this.RED.nodes.getNode(childId);
|
||||||
|
this.source.childRegistrationUtils.registerChild(childObj.source, msg.positionVsParent);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'calibratePredictedVolume': {
|
||||||
|
const injectedVol = parseFloat(msg.payload);
|
||||||
|
this.source.calibratePredictedVolume(injectedVol);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'calibratePredictedLevel': {
|
||||||
|
const injectedLevel = parseFloat(msg.payload);
|
||||||
|
this.source.calibratePredictedLevel(injectedLevel);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'q_in': {
|
||||||
|
// payload can be number or { value, unit, timestamp }
|
||||||
|
const val = Number(msg.payload);
|
||||||
|
const unit = msg?.unit;
|
||||||
|
const ts = msg?.timestamp || Date.now();
|
||||||
|
this.source.setManualInflow(val, ts, unit);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'q_out': {
|
||||||
|
const val = Number(msg.payload);
|
||||||
|
const unit = msg?.unit;
|
||||||
|
const ts = msg?.timestamp || Date.now();
|
||||||
|
this.source.setManualOutflow(val, ts, unit);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'Qd': {
|
||||||
|
// Manual demand: operator sets the target output via a
|
||||||
|
// dashboard slider. Only accepted when PS is in 'manual'
|
||||||
|
// mode — mirrors how rotatingMachine gates commands by
|
||||||
|
// mode (virtualControl vs auto).
|
||||||
|
const demand = Number(msg.payload);
|
||||||
|
if (!Number.isFinite(demand)) {
|
||||||
|
this.source.logger.warn(`Invalid Qd value: ${msg.payload}`);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (this.source.mode === 'manual') {
|
||||||
|
this.source.forwardDemandToChildren(demand).catch((err) =>
|
||||||
|
this.source.logger.error(`Failed to forward demand: ${err.message}`)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
this.source.logger.debug(
|
||||||
|
`Qd ignored in ${this.source.mode} mode. Switch to manual to use the demand slider.`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean up timers and intervals when Node-RED stops the node.
|
||||||
|
*/
|
||||||
|
_attachCloseHandler() {
|
||||||
|
this.node.on('close', (done) => {
|
||||||
|
clearInterval(this._tickInterval);
|
||||||
|
clearInterval(this._statusInterval);
|
||||||
|
this.node.status({}); // clear node status badge
|
||||||
|
done();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,153 +0,0 @@
|
|||||||
// Safety controller for the pumping-station basin.
|
|
||||||
//
|
|
||||||
// Two hard rules, applied independently every tick:
|
|
||||||
//
|
|
||||||
// 1. DRY-RUN (volume below minVol while draining): pumps must stop.
|
|
||||||
// Shuts down all DOWNSTREAM machines + machine groups + child
|
|
||||||
// stations. Sets blocked=true so the orchestrator skips control
|
|
||||||
// logic — only a manual override or estop can restart pumps.
|
|
||||||
//
|
|
||||||
// 2. OVERFILL (volume above overflow level while filling): pumps must
|
|
||||||
// keep running. Shuts down UPSTREAM equipment only (stop more water
|
|
||||||
// coming in) and child stations. Does NOT touch machine groups or
|
|
||||||
// downstream pumps — they must keep draining. blocked stays false
|
|
||||||
// so level-based control keeps demanding maximum throughput.
|
|
||||||
//
|
|
||||||
// A third path: if no volume reading is available, panic — shut down
|
|
||||||
// every machine and block control.
|
|
||||||
|
|
||||||
function pickVariant(measurements, type, variants, position, unit) {
|
|
||||||
for (const variant of variants) {
|
|
||||||
const v = measurements.type(type).variant(variant).position(position).getCurrentValue(unit);
|
|
||||||
if (Number.isFinite(v)) return v;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
class SafetyController {
|
|
||||||
/**
|
|
||||||
* @param {object} ctx
|
|
||||||
* @param {object} ctx.measurements MeasurementContainer-like instance
|
|
||||||
* @param {object} ctx.basin BasinGeometry snapshot ({maxVolAtOverflow, minVol, ...})
|
|
||||||
* @param {object} ctx.config pumpingStation config (uses .safety subtree)
|
|
||||||
* @param {object} ctx.logger generalFunctions logger
|
|
||||||
* @param {object} ctx.machines map of childId → rotatingMachine
|
|
||||||
* @param {object} ctx.stations map of childId → child pumpingStation
|
|
||||||
* @param {object} ctx.machineGroups map of childId → machineGroupControl
|
|
||||||
* @param {string[]} [ctx.volVariants] order of volume variants to try
|
|
||||||
*/
|
|
||||||
constructor(ctx) {
|
|
||||||
this.ctx = ctx;
|
|
||||||
this.volVariants = ctx.volVariants || ['measured', 'predicted'];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Run the dry-run + overfill rules against the current measurement state.
|
|
||||||
*
|
|
||||||
* @param {object} flowSnapshot { direction: 'filling'|'draining'|'steady',
|
|
||||||
* secondsRemaining: number|null }
|
|
||||||
* @returns {{blocked:boolean, reason:string|null, triggered:string[]}}
|
|
||||||
*/
|
|
||||||
evaluate(flowSnapshot) {
|
|
||||||
const { measurements, basin, config, logger, machines } = this.ctx;
|
|
||||||
const direction = flowSnapshot?.direction ?? 'steady';
|
|
||||||
const secondsRemaining = flowSnapshot?.secondsRemaining ?? null;
|
|
||||||
|
|
||||||
const volUnit = measurements.getUnit('volume');
|
|
||||||
const vol = pickVariant(measurements, 'volume', this.volVariants, 'atequipment', volUnit);
|
|
||||||
|
|
||||||
if (vol == null) {
|
|
||||||
Object.values(machines).forEach((m) => m.handleInput('parent', 'execSequence', 'shutdown'));
|
|
||||||
logger.warn('No volume data available to safe guard system; shutting down all machines.');
|
|
||||||
return { blocked: true, reason: 'no-volume-data', triggered: ['no-volume-data'] };
|
|
||||||
}
|
|
||||||
|
|
||||||
const triggered = [];
|
|
||||||
let blocked = false;
|
|
||||||
let reason = null;
|
|
||||||
|
|
||||||
const dry = this._dryRunRule(vol, direction, secondsRemaining);
|
|
||||||
if (dry.triggered) {
|
|
||||||
this._shutdownDownstream(vol, secondsRemaining);
|
|
||||||
blocked = true;
|
|
||||||
reason = 'dry-run';
|
|
||||||
triggered.push(...dry.flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
const over = this._overfillRule(vol, direction, secondsRemaining);
|
|
||||||
if (over.triggered) {
|
|
||||||
this._shutdownUpstream(vol, secondsRemaining);
|
|
||||||
// Overfill never sets blocked — control keeps running.
|
|
||||||
if (reason == null) reason = 'overfill';
|
|
||||||
triggered.push(...over.flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
return { blocked, reason, triggered };
|
|
||||||
}
|
|
||||||
|
|
||||||
_safetyConfig() {
|
|
||||||
return this.ctx.config.safety || {};
|
|
||||||
}
|
|
||||||
|
|
||||||
_dryRunRule(vol, direction, secondsRemaining) {
|
|
||||||
if (direction !== 'draining') return { triggered: false, flags: [] };
|
|
||||||
const s = this._safetyConfig();
|
|
||||||
const dryRunEnabled = Boolean(s.enableDryRunProtection);
|
|
||||||
const timeProtectionEnabled = s.timeleftToFullOrEmptyThresholdSeconds > 0;
|
|
||||||
const triggerLowVol = this.ctx.basin.minVol * (1 + ((Number(s.dryRunThresholdPercent) || 0) / 100));
|
|
||||||
|
|
||||||
const flags = [];
|
|
||||||
if (dryRunEnabled && vol < triggerLowVol) flags.push('dry-run-volume');
|
|
||||||
if (timeProtectionEnabled && secondsRemaining != null && secondsRemaining < s.timeleftToFullOrEmptyThresholdSeconds) {
|
|
||||||
flags.push('time-remaining');
|
|
||||||
}
|
|
||||||
return { triggered: flags.length > 0, flags };
|
|
||||||
}
|
|
||||||
|
|
||||||
_overfillRule(vol, direction, secondsRemaining) {
|
|
||||||
if (direction !== 'filling') return { triggered: false, flags: [] };
|
|
||||||
const s = this._safetyConfig();
|
|
||||||
const overfillEnabled = Boolean(s.enableOverfillProtection);
|
|
||||||
const timeProtectionEnabled = s.timeleftToFullOrEmptyThresholdSeconds > 0;
|
|
||||||
const triggerHighVol = this.ctx.basin.maxVolAtOverflow * ((Number(s.overfillThresholdPercent) || 0) / 100);
|
|
||||||
|
|
||||||
const flags = [];
|
|
||||||
if (overfillEnabled && vol > triggerHighVol) flags.push('overfill-volume');
|
|
||||||
if (timeProtectionEnabled && secondsRemaining != null && secondsRemaining < s.timeleftToFullOrEmptyThresholdSeconds) {
|
|
||||||
flags.push('time-remaining');
|
|
||||||
}
|
|
||||||
return { triggered: flags.length > 0, flags };
|
|
||||||
}
|
|
||||||
|
|
||||||
_shutdownDownstream(vol, secondsRemaining) {
|
|
||||||
const { machines, machineGroups, stations, logger } = this.ctx;
|
|
||||||
Object.values(machines).forEach((machine) => {
|
|
||||||
const pos = machine?.config?.functionality?.positionVsParent;
|
|
||||||
if ((pos === 'downstream' || pos === 'atequipment') && machine._isOperationalState()) {
|
|
||||||
machine.handleInput('parent', 'execSequence', 'shutdown');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
Object.values(stations).forEach((st) => st.handleInput('parent', 'execSequence', 'shutdown'));
|
|
||||||
Object.values(machineGroups).forEach((g) => g.turnOffAllMachines());
|
|
||||||
logger.warn(
|
|
||||||
`Dry-run safety: vol=${vol.toFixed(2)} m3, remainingTime=${secondsRemaining ? secondsRemaining.toFixed(1) : 'N/A'} s; shutting down downstream equipment`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
_shutdownUpstream(vol, secondsRemaining) {
|
|
||||||
const { machines, stations, logger } = this.ctx;
|
|
||||||
Object.values(machines).forEach((machine) => {
|
|
||||||
const pos = machine?.config?.functionality?.positionVsParent;
|
|
||||||
if (pos === 'upstream' && machine._isOperationalState()) {
|
|
||||||
machine.handleInput('parent', 'execSequence', 'shutdown');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
Object.values(stations).forEach((st) => st.handleInput('parent', 'execSequence', 'shutdown'));
|
|
||||||
// Machine groups intentionally NOT shut down — they must keep draining.
|
|
||||||
logger.warn(
|
|
||||||
`Overfill safety: vol=${vol.toFixed(2)} m3, remainingTime=${secondsRemaining ? secondsRemaining.toFixed(1) : 'N/A'} s; shutting down upstream equipment only — pumps keep running`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = SafetyController;
|
|
||||||
1545
src/specificClass.js
1545
src/specificClass.js
File diff suppressed because it is too large
Load Diff
@@ -1,106 +0,0 @@
|
|||||||
// Basic unit tests for BasinGeometry.
|
|
||||||
// Run with: node --test test/basic/BasinGeometry.basic.test.js
|
|
||||||
|
|
||||||
const test = require('node:test');
|
|
||||||
const assert = require('node:assert/strict');
|
|
||||||
|
|
||||||
const BasinGeometry = require('../../src/basin/BasinGeometry');
|
|
||||||
|
|
||||||
function makeBasin(overrides = {}) {
|
|
||||||
const basin = {
|
|
||||||
volume: 50,
|
|
||||||
height: 5,
|
|
||||||
inflowLevel: 3,
|
|
||||||
outflowLevel: 0.2,
|
|
||||||
overflowLevel: 4.5,
|
|
||||||
...overrides.basin,
|
|
||||||
};
|
|
||||||
const hydraulics = {
|
|
||||||
minHeightBasedOn: 'outlet',
|
|
||||||
...overrides.hydraulics,
|
|
||||||
};
|
|
||||||
return new BasinGeometry(basin, hydraulics);
|
|
||||||
}
|
|
||||||
|
|
||||||
test('constructor produces correct surfaceArea = volume / height', () => {
|
|
||||||
const g = makeBasin();
|
|
||||||
assert.equal(g.surfaceArea, 10); // 50 / 5
|
|
||||||
assert.equal(g.heightBasin, 5);
|
|
||||||
assert.equal(g.volEmptyBasin, 50);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('maxVolAtOverflow equals overflowLevel × surfaceArea', () => {
|
|
||||||
const g = makeBasin();
|
|
||||||
assert.equal(g.maxVolAtOverflow, 4.5 * 10); // 45
|
|
||||||
assert.equal(g.minVolAtInflow, 3 * 10); // 30
|
|
||||||
assert.equal(g.minVolAtOutflow, 0.2 * 10); // 2
|
|
||||||
assert.equal(g.maxVol, 50);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("minVol selects outlet-based when minHeightBasedOn = 'outlet'", () => {
|
|
||||||
const g = makeBasin();
|
|
||||||
assert.equal(g.minVol, g.minVolAtOutflow);
|
|
||||||
assert.equal(g.minHeightBasedOn, 'outlet');
|
|
||||||
});
|
|
||||||
|
|
||||||
test("minVol selects inlet-based when minHeightBasedOn = 'inlet'", () => {
|
|
||||||
const g = makeBasin({ hydraulics: { minHeightBasedOn: 'inlet' } });
|
|
||||||
assert.equal(g.minVol, g.minVolAtInflow);
|
|
||||||
assert.equal(g.minHeightBasedOn, 'inlet');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('volumeFromLevel(0) returns 0; negative level clamps to 0', () => {
|
|
||||||
const g = makeBasin();
|
|
||||||
assert.equal(g.volumeFromLevel(0), 0);
|
|
||||||
assert.equal(g.volumeFromLevel(-1), 0);
|
|
||||||
assert.equal(g.volumeFromLevel(-1e9), 0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('volumeFromLevel(positive) is level × surfaceArea', () => {
|
|
||||||
const g = makeBasin();
|
|
||||||
assert.equal(g.volumeFromLevel(2.5), 25);
|
|
||||||
assert.equal(g.volumeFromLevel(5), 50);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('levelFromVolume(maxVol) returns heightBasin', () => {
|
|
||||||
const g = makeBasin();
|
|
||||||
assert.equal(g.levelFromVolume(g.maxVol), g.heightBasin);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('levelFromVolume(0) returns 0; negative volume clamps to 0', () => {
|
|
||||||
const g = makeBasin();
|
|
||||||
assert.equal(g.levelFromVolume(0), 0);
|
|
||||||
assert.equal(g.levelFromVolume(-10), 0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('round-trip: volumeFromLevel(levelFromVolume(v)) ≈ v for v in range', () => {
|
|
||||||
const g = makeBasin();
|
|
||||||
for (const v of [0, 0.001, 1, 12.34, 25, 49.999, 50]) {
|
|
||||||
const back = g.volumeFromLevel(g.levelFromVolume(v));
|
|
||||||
assert.ok(Math.abs(back - v) < 1e-9, `round-trip failed for v=${v}, got ${back}`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test('round-trip: levelFromVolume(volumeFromLevel(L)) ≈ L for L in range', () => {
|
|
||||||
const g = makeBasin();
|
|
||||||
for (const L of [0, 0.05, 1, 2.5, 4.5, 5]) {
|
|
||||||
const back = g.levelFromVolume(g.volumeFromLevel(L));
|
|
||||||
assert.ok(Math.abs(back - L) < 1e-9, `round-trip failed for L=${L}, got ${back}`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test('snapshot() exposes legacy this.basin field names', () => {
|
|
||||||
const g = makeBasin();
|
|
||||||
const s = g.snapshot();
|
|
||||||
const expectedKeys = [
|
|
||||||
'volEmptyBasin', 'heightBasin', 'inflowLevel', 'outflowLevel',
|
|
||||||
'overflowLevel', 'surfaceArea', 'maxVol', 'maxVolAtOverflow',
|
|
||||||
'minVolAtInflow', 'minVolAtOutflow', 'minVol', 'minHeightBasedOn',
|
|
||||||
];
|
|
||||||
for (const k of expectedKeys) {
|
|
||||||
assert.ok(k in s, `snapshot missing key: ${k}`);
|
|
||||||
}
|
|
||||||
assert.equal(s.volEmptyBasin, 50);
|
|
||||||
assert.equal(s.surfaceArea, 10);
|
|
||||||
assert.equal(s.minHeightBasedOn, 'outlet');
|
|
||||||
});
|
|
||||||
@@ -1,106 +0,0 @@
|
|||||||
// Basic tests for the calibration helpers.
|
|
||||||
|
|
||||||
const test = require('node:test');
|
|
||||||
const assert = require('node:assert/strict');
|
|
||||||
|
|
||||||
const { MeasurementContainer } = require('generalFunctions');
|
|
||||||
const {
|
|
||||||
calibratePredictedVolume,
|
|
||||||
calibratePredictedLevel,
|
|
||||||
setManualInflow,
|
|
||||||
} = require('../../src/measurement/calibration');
|
|
||||||
|
|
||||||
function makeBasin() {
|
|
||||||
return {
|
|
||||||
surfaceArea: 10,
|
|
||||||
minVol: 2,
|
|
||||||
maxVol: 50,
|
|
||||||
maxVolAtOverflow: 45,
|
|
||||||
overflowLevel: 4.5,
|
|
||||||
outflowLevel: 0.2,
|
|
||||||
inflowLevel: 3,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeCtx(seedVolume = null) {
|
|
||||||
const measurements = new MeasurementContainer({
|
|
||||||
autoConvert: true,
|
|
||||||
preferredUnits: { flow: 'm3/s', level: 'm', volume: 'm3' },
|
|
||||||
});
|
|
||||||
const basin = makeBasin();
|
|
||||||
if (seedVolume != null) {
|
|
||||||
measurements.type('volume').variant('predicted').position('atequipment')
|
|
||||||
.value(seedVolume, Date.now() - 5_000, 'm3').unit('m3');
|
|
||||||
}
|
|
||||||
const ctx = { measurements, basin };
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
|
|
||||||
test('calibratePredictedVolume clears prior series and writes new value', async () => {
|
|
||||||
const ctx = makeCtx(12);
|
|
||||||
const before = ctx.measurements.type('volume').variant('predicted').position('atequipment')
|
|
||||||
.getCurrentValue('m3');
|
|
||||||
assert.ok(Math.abs(before - 12) < 1e-9);
|
|
||||||
|
|
||||||
const ts = Date.now();
|
|
||||||
calibratePredictedVolume(ctx, 30, ts);
|
|
||||||
|
|
||||||
const m = ctx.measurements.type('volume').variant('predicted').position('atequipment').get();
|
|
||||||
assert.equal(m.values.length, 1, 'series should hold exactly the calibration point');
|
|
||||||
assert.ok(Math.abs(m.getCurrentValue() - 30) < 1e-9);
|
|
||||||
|
|
||||||
// Level was derived: 30 / 10 = 3 m.
|
|
||||||
const lvl = ctx.measurements.type('level').variant('predicted').position('atequipment')
|
|
||||||
.getCurrentValue('m');
|
|
||||||
assert.ok(Math.abs(lvl - 3) < 1e-9, `derived level was ${lvl}`);
|
|
||||||
|
|
||||||
assert.equal(ctx._predictedFlowState.lastTimestamp, ts);
|
|
||||||
assert.equal(ctx._predictedFlowState.inflow, 0);
|
|
||||||
assert.equal(ctx._predictedFlowState.outflow, 0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('calibratePredictedLevel writes both level and derived volume', async () => {
|
|
||||||
const ctx = makeCtx(2);
|
|
||||||
calibratePredictedLevel(ctx, 4.0, Date.now(), 'm');
|
|
||||||
|
|
||||||
const lvl = ctx.measurements.type('level').variant('predicted').position('atequipment')
|
|
||||||
.getCurrentValue('m');
|
|
||||||
assert.ok(Math.abs(lvl - 4.0) < 1e-9);
|
|
||||||
|
|
||||||
const vol = ctx.measurements.type('volume').variant('predicted').position('atequipment')
|
|
||||||
.getCurrentValue('m3');
|
|
||||||
assert.ok(Math.abs(vol - 40) < 1e-9, `derived volume was ${vol}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('setManualInflow writes to flow.predicted.in.manual-qin', async () => {
|
|
||||||
const ctx = makeCtx();
|
|
||||||
const ts = Date.now();
|
|
||||||
setManualInflow(ctx, 0.025, ts, 'm3/s');
|
|
||||||
|
|
||||||
const series = ctx.measurements.type('flow').variant('predicted').position('in').child('manual-qin');
|
|
||||||
const val = series.getCurrentValue('m3/s');
|
|
||||||
assert.ok(Math.abs(val - 0.025) < 1e-9, `manual-qin value was ${val}`);
|
|
||||||
|
|
||||||
// It must NOT collide with the default child bucket.
|
|
||||||
const defaultBucket = ctx.measurements.measurements?.flow?.predicted?.in?.default;
|
|
||||||
assert.equal(defaultBucket, undefined);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('calibration uses ctx.flowAggregator.resetState when present', async () => {
|
|
||||||
const ctx = makeCtx(5);
|
|
||||||
let resetCalled = null;
|
|
||||||
ctx.flowAggregator = { resetState: (ts) => { resetCalled = ts; } };
|
|
||||||
|
|
||||||
const ts = 1234567890;
|
|
||||||
calibratePredictedVolume(ctx, 20, ts);
|
|
||||||
|
|
||||||
assert.equal(resetCalled, ts);
|
|
||||||
// The plain bag should NOT be touched when the aggregator hook is present.
|
|
||||||
assert.equal(ctx._predictedFlowState, undefined);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('calibratePredictedVolume rejects bad context', async () => {
|
|
||||||
assert.throws(() => calibratePredictedVolume({}, 10));
|
|
||||||
assert.throws(() => calibratePredictedLevel({}, 1.0));
|
|
||||||
assert.throws(() => setManualInflow({}, 0.01));
|
|
||||||
});
|
|
||||||
@@ -1,178 +0,0 @@
|
|||||||
// Basic tests for the pumpingStation commands registry.
|
|
||||||
// Run with: node --test test/basic/commands.basic.test.js
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
const test = require('node:test');
|
|
||||||
const assert = require('node:assert/strict');
|
|
||||||
|
|
||||||
const { createRegistry } = require('generalFunctions');
|
|
||||||
const commands = require('../../src/commands');
|
|
||||||
|
|
||||||
// --- helpers ---------------------------------------------------------------
|
|
||||||
|
|
||||||
function makeLogger() {
|
|
||||||
const calls = { warn: [], error: [], info: [], debug: [] };
|
|
||||||
return {
|
|
||||||
calls,
|
|
||||||
warn: (m) => calls.warn.push(String(m)),
|
|
||||||
error: (m) => calls.error.push(String(m)),
|
|
||||||
info: (m) => calls.info.push(String(m)),
|
|
||||||
debug: (m) => calls.debug.push(String(m)),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeSource({ mode = 'manual' } = {}) {
|
|
||||||
const calls = {
|
|
||||||
changeMode: [],
|
|
||||||
calibratePredictedVolume: [],
|
|
||||||
calibratePredictedLevel: [],
|
|
||||||
setManualInflow: [],
|
|
||||||
forwardDemandToChildren: [],
|
|
||||||
registerChild: [],
|
|
||||||
};
|
|
||||||
const source = {
|
|
||||||
mode,
|
|
||||||
logger: makeLogger(),
|
|
||||||
changeMode: (m) => calls.changeMode.push(m),
|
|
||||||
calibratePredictedVolume: (v) => calls.calibratePredictedVolume.push(v),
|
|
||||||
calibratePredictedLevel: (v) => calls.calibratePredictedLevel.push(v),
|
|
||||||
setManualInflow: (v, ts, u) => calls.setManualInflow.push({ v, ts, u }),
|
|
||||||
forwardDemandToChildren: async (d) => { calls.forwardDemandToChildren.push(d); },
|
|
||||||
childRegistrationUtils: {
|
|
||||||
registerChild: (childSource, position) =>
|
|
||||||
calls.registerChild.push({ childSource, position }),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
return { source, calls };
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeCtx({ child = null, logger = makeLogger() } = {}) {
|
|
||||||
return {
|
|
||||||
logger,
|
|
||||||
RED: { nodes: { getNode: (id) => (child && child.id === id ? child : undefined) } },
|
|
||||||
node: {},
|
|
||||||
send: () => {},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeRegistry(logger) {
|
|
||||||
return createRegistry(commands, { logger });
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- tests -----------------------------------------------------------------
|
|
||||||
|
|
||||||
test('canonical topics dispatch to their handlers', async () => {
|
|
||||||
const { source, calls } = makeSource();
|
|
||||||
const reg = makeRegistry(makeLogger());
|
|
||||||
|
|
||||||
await reg.dispatch({ topic: 'set.mode', payload: 'levelbased' }, source, makeCtx());
|
|
||||||
assert.deepEqual(calls.changeMode, ['levelbased']);
|
|
||||||
|
|
||||||
await reg.dispatch({ topic: 'cmd.calibrate.volume', payload: '12.5' }, source, makeCtx());
|
|
||||||
assert.deepEqual(calls.calibratePredictedVolume, [12.5]);
|
|
||||||
|
|
||||||
await reg.dispatch({ topic: 'cmd.calibrate.level', payload: 1.25 }, source, makeCtx());
|
|
||||||
assert.deepEqual(calls.calibratePredictedLevel, [1.25]);
|
|
||||||
|
|
||||||
await reg.dispatch({ topic: 'set.inflow', payload: 0.5, unit: 'm3/s' }, source, makeCtx());
|
|
||||||
assert.equal(calls.setManualInflow.length, 1);
|
|
||||||
assert.equal(calls.setManualInflow[0].v, 0.5);
|
|
||||||
assert.equal(calls.setManualInflow[0].u, 'm3/s');
|
|
||||||
|
|
||||||
await reg.dispatch({ topic: 'set.demand', payload: 100 }, source, makeCtx());
|
|
||||||
assert.deepEqual(calls.forwardDemandToChildren, [100]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('child.register canonical resolves child via RED.nodes.getNode', async () => {
|
|
||||||
const { source, calls } = makeSource();
|
|
||||||
const child = { id: 'child-1', source: { tag: 'child-domain' } };
|
|
||||||
const reg = makeRegistry(makeLogger());
|
|
||||||
|
|
||||||
await reg.dispatch(
|
|
||||||
{ topic: 'child.register', payload: 'child-1', positionVsParent: 'upstream' },
|
|
||||||
source,
|
|
||||||
makeCtx({ child })
|
|
||||||
);
|
|
||||||
assert.equal(calls.registerChild.length, 1);
|
|
||||||
assert.equal(calls.registerChild[0].childSource, child.source);
|
|
||||||
assert.equal(calls.registerChild[0].position, 'upstream');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('aliases dispatch to the same handler and log a one-time deprecation', async () => {
|
|
||||||
const { source, calls } = makeSource();
|
|
||||||
const ctxLogger = makeLogger();
|
|
||||||
const reg = makeRegistry(ctxLogger);
|
|
||||||
|
|
||||||
await reg.dispatch({ topic: 'changemode', payload: 'manual' }, source, makeCtx({ logger: ctxLogger }));
|
|
||||||
await reg.dispatch({ topic: 'changemode', payload: 'manual' }, source, makeCtx({ logger: ctxLogger }));
|
|
||||||
|
|
||||||
assert.deepEqual(calls.changeMode, ['manual', 'manual']);
|
|
||||||
const deprecWarns = ctxLogger.calls.warn.filter((m) => m.includes("'changemode' is deprecated"));
|
|
||||||
assert.equal(deprecWarns.length, 1, 'deprecation warning should log exactly once');
|
|
||||||
assert.equal(reg.deprecationStats().changemode, 2);
|
|
||||||
|
|
||||||
// q_in alias also routes to setInflow.
|
|
||||||
await reg.dispatch({ topic: 'q_in', payload: 0.25, unit: 'm3/s' }, source, makeCtx({ logger: ctxLogger }));
|
|
||||||
assert.equal(calls.setManualInflow.length, 1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('child.register with unknown child id logs warn and does not throw', async () => {
|
|
||||||
const { source, calls } = makeSource();
|
|
||||||
const ctxLogger = makeLogger();
|
|
||||||
const reg = makeRegistry(makeLogger());
|
|
||||||
|
|
||||||
await assert.doesNotReject(() =>
|
|
||||||
reg.dispatch(
|
|
||||||
{ topic: 'child.register', payload: 'missing-id', positionVsParent: 'atEquipment' },
|
|
||||||
source,
|
|
||||||
makeCtx({ logger: ctxLogger })
|
|
||||||
)
|
|
||||||
);
|
|
||||||
assert.equal(calls.registerChild.length, 0);
|
|
||||||
assert.ok(
|
|
||||||
ctxLogger.calls.warn.some((m) => m.includes('registerChild') && m.includes('missing-id')),
|
|
||||||
`expected warn about missing child, got: ${JSON.stringify(ctxLogger.calls.warn)}`
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('set.inflow accepts number payload and { value, unit, timestamp } object payload', async () => {
|
|
||||||
const { source, calls } = makeSource();
|
|
||||||
const reg = makeRegistry(makeLogger());
|
|
||||||
|
|
||||||
await reg.dispatch({ topic: 'set.inflow', payload: 0.5, unit: 'm3/s', timestamp: 1000 }, source, makeCtx());
|
|
||||||
assert.deepEqual(calls.setManualInflow[0], { v: 0.5, ts: 1000, u: 'm3/s' });
|
|
||||||
|
|
||||||
await reg.dispatch(
|
|
||||||
{ topic: 'set.inflow', payload: { value: 2, unit: 'm3/h', timestamp: 2000 } },
|
|
||||||
source,
|
|
||||||
makeCtx()
|
|
||||||
);
|
|
||||||
assert.deepEqual(calls.setManualInflow[1], { v: 2, ts: 2000, u: 'm3/h' });
|
|
||||||
});
|
|
||||||
|
|
||||||
test('set.demand in non-manual mode logs debug and does not call forwardDemandToChildren', async () => {
|
|
||||||
const { source, calls } = makeSource({ mode: 'levelbased' });
|
|
||||||
const ctxLogger = makeLogger();
|
|
||||||
const reg = makeRegistry(makeLogger());
|
|
||||||
|
|
||||||
await reg.dispatch({ topic: 'set.demand', payload: 50 }, source, makeCtx({ logger: ctxLogger }));
|
|
||||||
assert.equal(calls.forwardDemandToChildren.length, 0);
|
|
||||||
assert.ok(
|
|
||||||
ctxLogger.calls.debug.some((m) => m.includes('set.demand') && m.includes('levelbased')),
|
|
||||||
`expected debug about ignoring demand, got: ${JSON.stringify(ctxLogger.calls.debug)}`
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('set.demand with non-numeric payload logs warn and does not call', async () => {
|
|
||||||
const { source, calls } = makeSource({ mode: 'manual' });
|
|
||||||
const ctxLogger = makeLogger();
|
|
||||||
const reg = makeRegistry(makeLogger());
|
|
||||||
|
|
||||||
await reg.dispatch({ topic: 'set.demand', payload: 'oops' }, source, makeCtx({ logger: ctxLogger }));
|
|
||||||
assert.equal(calls.forwardDemandToChildren.length, 0);
|
|
||||||
assert.ok(
|
|
||||||
ctxLogger.calls.warn.some((m) => m.includes('set.demand') && m.includes('oops')),
|
|
||||||
`expected warn about invalid Qd, got: ${JSON.stringify(ctxLogger.calls.warn)}`
|
|
||||||
);
|
|
||||||
});
|
|
||||||
@@ -1,126 +0,0 @@
|
|||||||
// Unit tests for the level-based control strategy.
|
|
||||||
// Run with: node --test test/basic/control-levelBased.basic.test.js
|
|
||||||
|
|
||||||
const test = require('node:test');
|
|
||||||
const assert = require('node:assert/strict');
|
|
||||||
|
|
||||||
const levelBased = require('../../src/control/levelBased');
|
|
||||||
|
|
||||||
function makeMeasurements(levelMeters) {
|
|
||||||
// Minimal MeasurementContainer stand-in. The strategy only calls
|
|
||||||
// getUnit('level') and a chain ending in getCurrentValue(unit).
|
|
||||||
const chain = {
|
|
||||||
type() { return chain; },
|
|
||||||
variant() { return chain; },
|
|
||||||
position() { return chain; },
|
|
||||||
getCurrentValue() {
|
|
||||||
return Number.isFinite(levelMeters) ? levelMeters : null;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
getUnit: () => 'm',
|
|
||||||
type: () => chain,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeGroup(name) {
|
|
||||||
const calls = { handleInput: [], turnOff: 0 };
|
|
||||||
return {
|
|
||||||
config: { general: { name } },
|
|
||||||
handleInput: async (...args) => { calls.handleInput.push(args); },
|
|
||||||
turnOffAllMachines: () => { calls.turnOff += 1; },
|
|
||||||
_calls: calls,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeCtx(levelMeters, opts = {}) {
|
|
||||||
const groups = {
|
|
||||||
a: makeGroup('A'),
|
|
||||||
b: makeGroup('B'),
|
|
||||||
c: makeGroup('C'),
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
measurements: makeMeasurements(levelMeters),
|
|
||||||
config: {
|
|
||||||
control: { levelbased: { minLevel: 1, startLevel: 2, maxLevel: 4, ...(opts.levelbased || {}) } },
|
|
||||||
},
|
|
||||||
logger: { warn: () => {}, debug: () => {}, info: () => {}, error: () => {} },
|
|
||||||
machineGroups: groups,
|
|
||||||
machines: {},
|
|
||||||
levelVariants: ['measured', 'predicted'],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
test('level < minLevel → STOP: turnOffAllMachines on every group, percControl = 0', async () => {
|
|
||||||
const ctx = makeCtx(0.5);
|
|
||||||
const state = { percControl: 42 };
|
|
||||||
await levelBased.run(ctx, state);
|
|
||||||
|
|
||||||
assert.equal(state.percControl, 0);
|
|
||||||
for (const g of Object.values(ctx.machineGroups)) {
|
|
||||||
assert.equal(g._calls.turnOff, 1, 'turnOffAllMachines called once per group');
|
|
||||||
assert.equal(g._calls.handleInput.length, 0, 'no demand sent in stop zone');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test('minLevel ≤ level < startLevel → DEAD ZONE: no calls, percControl unchanged', async () => {
|
|
||||||
const ctx = makeCtx(1.5);
|
|
||||||
const state = { percControl: 17 };
|
|
||||||
await levelBased.run(ctx, state);
|
|
||||||
|
|
||||||
assert.equal(state.percControl, 17, 'percControl untouched in dead zone');
|
|
||||||
for (const g of Object.values(ctx.machineGroups)) {
|
|
||||||
assert.equal(g._calls.turnOff, 0);
|
|
||||||
assert.equal(g._calls.handleInput.length, 0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test('level == startLevel → percControl == 0 (lower edge of ramp)', async () => {
|
|
||||||
const ctx = makeCtx(2);
|
|
||||||
const state = { percControl: null };
|
|
||||||
await levelBased.run(ctx, state);
|
|
||||||
assert.equal(state.percControl, 0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('level == maxLevel → percControl == 100 (upper edge of ramp)', async () => {
|
|
||||||
const ctx = makeCtx(4);
|
|
||||||
const state = { percControl: null };
|
|
||||||
await levelBased.run(ctx, state);
|
|
||||||
assert.equal(state.percControl, 100);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('level above maxLevel → percControl clamped at 100 (interpolation limit_input behaviour)', async () => {
|
|
||||||
const ctx = makeCtx(10);
|
|
||||||
const state = { percControl: null };
|
|
||||||
await levelBased.run(ctx, state);
|
|
||||||
// interpolate_lin_single_point clamps via limit_input(o_min, o_max).
|
|
||||||
assert.equal(state.percControl, 100);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('percControl forwarded to every group via handleInput("parent", percControl)', async () => {
|
|
||||||
const ctx = makeCtx(3); // halfway between startLevel=2 and maxLevel=4 → 50%
|
|
||||||
const state = { percControl: null };
|
|
||||||
await levelBased.run(ctx, state);
|
|
||||||
|
|
||||||
assert.equal(state.percControl, 50);
|
|
||||||
for (const g of Object.values(ctx.machineGroups)) {
|
|
||||||
assert.equal(g._calls.handleInput.length, 1, 'one forward per group');
|
|
||||||
assert.deepEqual(g._calls.handleInput[0], ['parent', 50]);
|
|
||||||
assert.equal(g._calls.turnOff, 0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test('no valid level → warns and returns without mutating percControl or calling groups', async () => {
|
|
||||||
const ctx = makeCtx(NaN);
|
|
||||||
let warned = false;
|
|
||||||
ctx.logger.warn = () => { warned = true; };
|
|
||||||
const state = { percControl: 7 };
|
|
||||||
await levelBased.run(ctx, state);
|
|
||||||
|
|
||||||
assert.equal(warned, true);
|
|
||||||
assert.equal(state.percControl, 7);
|
|
||||||
for (const g of Object.values(ctx.machineGroups)) {
|
|
||||||
assert.equal(g._calls.turnOff, 0);
|
|
||||||
assert.equal(g._calls.handleInput.length, 0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
// Unit tests for the manual control strategy.
|
|
||||||
// Run with: node --test test/basic/control-manual.basic.test.js
|
|
||||||
|
|
||||||
const test = require('node:test');
|
|
||||||
const assert = require('node:assert/strict');
|
|
||||||
|
|
||||||
const manual = require('../../src/control/manual');
|
|
||||||
|
|
||||||
function makeGroup(name) {
|
|
||||||
const calls = { handleInput: [] };
|
|
||||||
return {
|
|
||||||
config: { general: { name } },
|
|
||||||
handleInput: async (...args) => { calls.handleInput.push(args); },
|
|
||||||
_calls: calls,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeMachine(name) {
|
|
||||||
const calls = { handleInput: [] };
|
|
||||||
return {
|
|
||||||
config: { general: { name } },
|
|
||||||
handleInput: async (...args) => { calls.handleInput.push(args); },
|
|
||||||
_calls: calls,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeLogger() {
|
|
||||||
return { info: () => {}, debug: () => {}, warn: () => {}, error: () => {} };
|
|
||||||
}
|
|
||||||
|
|
||||||
test('forwardDemand calls handleInput("parent", demand) on every machine group', async () => {
|
|
||||||
const groups = { a: makeGroup('A'), b: makeGroup('B'), c: makeGroup('C') };
|
|
||||||
const ctx = { machineGroups: groups, machines: {}, logger: makeLogger() };
|
|
||||||
|
|
||||||
await manual.forwardDemand(ctx, 50);
|
|
||||||
|
|
||||||
for (const g of Object.values(groups)) {
|
|
||||||
assert.equal(g._calls.handleInput.length, 1);
|
|
||||||
assert.deepEqual(g._calls.handleInput[0], ['parent', 50]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test('forwardDemand with no machineGroups but direct machines splits demand evenly', async () => {
|
|
||||||
const machines = { m1: makeMachine('M1'), m2: makeMachine('M2'), m3: makeMachine('M3'), m4: makeMachine('M4') };
|
|
||||||
const ctx = { machineGroups: {}, machines, logger: makeLogger() };
|
|
||||||
|
|
||||||
await manual.forwardDemand(ctx, 80);
|
|
||||||
|
|
||||||
for (const m of Object.values(machines)) {
|
|
||||||
assert.equal(m._calls.handleInput.length, 1);
|
|
||||||
assert.deepEqual(m._calls.handleInput[0], ['parent', 'execMovement', 20]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test('run() is a no-op (manual mode is event-driven)', async () => {
|
|
||||||
const groups = { a: makeGroup('A') };
|
|
||||||
const ctx = { machineGroups: groups, machines: {}, logger: makeLogger() };
|
|
||||||
await manual.run(ctx, { percControl: 0 });
|
|
||||||
assert.equal(groups.a._calls.handleInput.length, 0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('manual exports name === "manual"', () => {
|
|
||||||
assert.equal(manual.name, 'manual');
|
|
||||||
});
|
|
||||||
@@ -1,141 +0,0 @@
|
|||||||
// Basic tests for FlowAggregator. Pure node:test, no Node-RED runtime.
|
|
||||||
|
|
||||||
const test = require('node:test');
|
|
||||||
const assert = require('node:assert/strict');
|
|
||||||
|
|
||||||
const { MeasurementContainer } = require('generalFunctions');
|
|
||||||
const FlowAggregator = require('../../src/measurement/flowAggregator');
|
|
||||||
|
|
||||||
function makeBasin() {
|
|
||||||
// Constant-cross-section basin: 50 m3 / 5 m height ⇒ surfaceArea = 10 m2.
|
|
||||||
const surfaceArea = 10;
|
|
||||||
return {
|
|
||||||
surfaceArea,
|
|
||||||
minVol: 2,
|
|
||||||
maxVol: 50,
|
|
||||||
maxVolAtOverflow: 45, // overflow at 4.5 m
|
|
||||||
minVolAtOutflow: 2,
|
|
||||||
minVolAtInflow: 30,
|
|
||||||
overflowLevel: 4.5,
|
|
||||||
outflowLevel: 0.2,
|
|
||||||
inflowLevel: 3,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeMeasurements() {
|
|
||||||
return new MeasurementContainer({
|
|
||||||
autoConvert: true,
|
|
||||||
preferredUnits: { flow: 'm3/s', netFlowRate: 'm3/s', level: 'm', volume: 'm3' },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeAggregator(overrides = {}) {
|
|
||||||
const measurements = overrides.measurements || makeMeasurements();
|
|
||||||
const basin = overrides.basin || makeBasin();
|
|
||||||
// Seed predicted volume at minVol so update() has a starting point.
|
|
||||||
measurements.type('volume').variant('predicted').position('atequipment')
|
|
||||||
.value(basin.minVol).unit('m3');
|
|
||||||
const fa = new FlowAggregator({ measurements, basin, flowThreshold: 1e-4 });
|
|
||||||
return { fa, measurements, basin };
|
|
||||||
}
|
|
||||||
|
|
||||||
test('FlowAggregator.update integrates inflow-outflow over delta-t', async () => {
|
|
||||||
const { fa, measurements } = makeAggregator();
|
|
||||||
// Net flow = 0.01 m3/s (in) - 0.005 m3/s (out) = 0.005 m3/s.
|
|
||||||
const t0 = Date.now() - 10_000; // 10 s ago
|
|
||||||
measurements.type('flow').variant('predicted').position('in').child('src')
|
|
||||||
.value(0.01, t0, 'm3/s');
|
|
||||||
measurements.type('flow').variant('predicted').position('out').child('snk')
|
|
||||||
.value(0.005, t0, 'm3/s');
|
|
||||||
|
|
||||||
// Force the integrator to know we are starting 10 s in the past.
|
|
||||||
fa._predictedFlowState = { inflow: 0, outflow: 0, lastTimestamp: t0 };
|
|
||||||
fa.update();
|
|
||||||
|
|
||||||
const vol = measurements.type('volume').variant('predicted').position('atequipment')
|
|
||||||
.getCurrentValue('m3');
|
|
||||||
// Expect minVol(2) + 0.005 * ~10 ≈ 2.05 m3. Allow slack for clock jitter.
|
|
||||||
assert.ok(vol > 2.04 && vol < 2.06, `volume after integration was ${vol}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('FlowAggregator.selectBestNetFlow prefers measured over predicted', async () => {
|
|
||||||
const { fa, measurements } = makeAggregator();
|
|
||||||
measurements.type('flow').variant('measured').position('in').child('m')
|
|
||||||
.value(0.02, Date.now(), 'm3/s');
|
|
||||||
measurements.type('flow').variant('measured').position('out').child('m')
|
|
||||||
.value(0.01, Date.now(), 'm3/s');
|
|
||||||
measurements.type('flow').variant('predicted').position('in').child('p')
|
|
||||||
.value(0.5, Date.now(), 'm3/s');
|
|
||||||
measurements.type('flow').variant('predicted').position('out').child('p')
|
|
||||||
.value(0.0, Date.now(), 'm3/s');
|
|
||||||
|
|
||||||
const r = fa.selectBestNetFlow();
|
|
||||||
assert.equal(r.source, 'measured');
|
|
||||||
assert.ok(Math.abs(r.value - 0.01) < 1e-9);
|
|
||||||
assert.equal(r.direction, 'filling');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('FlowAggregator.selectBestNetFlow falls back to level rate when no flow', async () => {
|
|
||||||
const { fa, measurements, basin } = makeAggregator();
|
|
||||||
// Seed two level samples 2 s apart, rising 0.1 m → rate 0.05 m/s
|
|
||||||
// → net flow = 0.05 * 10 m2 = 0.5 m3/s (filling).
|
|
||||||
const t0 = Date.now() - 2_000;
|
|
||||||
const t1 = Date.now();
|
|
||||||
measurements.type('level').variant('measured').position('atequipment').child('default')
|
|
||||||
.value(1.0, t0, 'm');
|
|
||||||
measurements.type('level').variant('measured').position('atequipment').child('default')
|
|
||||||
.value(1.1, t1, 'm');
|
|
||||||
|
|
||||||
const r = fa.selectBestNetFlow();
|
|
||||||
assert.ok(r.source.startsWith('level:'), `source was ${r.source}`);
|
|
||||||
assert.equal(r.direction, 'filling');
|
|
||||||
assert.ok(Math.abs(r.value - basin.surfaceArea * 0.05) < 1e-3, `net flow was ${r.value}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('FlowAggregator.deriveDirection threshold semantics', async () => {
|
|
||||||
const { fa } = makeAggregator();
|
|
||||||
assert.equal(fa.deriveDirection(0), 'steady');
|
|
||||||
assert.equal(fa.deriveDirection(fa.flowThreshold * 2), 'filling');
|
|
||||||
assert.equal(fa.deriveDirection(-fa.flowThreshold * 2), 'draining');
|
|
||||||
assert.equal(fa.deriveDirection(fa.flowThreshold * 0.5), 'steady');
|
|
||||||
assert.equal(fa.deriveDirection(-fa.flowThreshold * 0.5), 'steady');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('FlowAggregator.computeRemainingTime — filling uses overflow ceiling', async () => {
|
|
||||||
const { fa, measurements, basin } = makeAggregator();
|
|
||||||
measurements.type('level').variant('predicted').position('atequipment')
|
|
||||||
.value(2.0, Date.now(), 'm');
|
|
||||||
// Net 0.05 m3/s upward; remaining height = 4.5 - 2.0 = 2.5 m.
|
|
||||||
// seconds = 2.5 * 10 / 0.05 = 500 s.
|
|
||||||
const r = fa.computeRemainingTime({ value: 0.05, source: 'measured', direction: 'filling' });
|
|
||||||
assert.ok(Math.abs(r.seconds - 500) < 1e-6, `seconds was ${r.seconds}`);
|
|
||||||
assert.equal(typeof r.source, 'string');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('FlowAggregator.computeRemainingTime — draining uses outflow floor', async () => {
|
|
||||||
const { fa, measurements } = makeAggregator();
|
|
||||||
measurements.type('level').variant('predicted').position('atequipment')
|
|
||||||
.value(1.0, Date.now(), 'm');
|
|
||||||
// Net -0.05 m3/s; remaining height = 1.0 - 0.2 = 0.8 m.
|
|
||||||
// seconds = 0.8 * 10 / 0.05 = 160 s.
|
|
||||||
const r = fa.computeRemainingTime({ value: -0.05, source: 'measured', direction: 'draining' });
|
|
||||||
assert.ok(Math.abs(r.seconds - 160) < 1e-6, `seconds was ${r.seconds}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('FlowAggregator.snapshot exposes the expected shape', async () => {
|
|
||||||
const { fa, measurements } = makeAggregator();
|
|
||||||
measurements.type('flow').variant('measured').position('in').child('m')
|
|
||||||
.value(0.02, Date.now(), 'm3/s');
|
|
||||||
fa.tick();
|
|
||||||
const snap = fa.snapshot();
|
|
||||||
assert.ok(Object.prototype.hasOwnProperty.call(snap, 'direction'));
|
|
||||||
assert.ok(Object.prototype.hasOwnProperty.call(snap, 'netFlow'));
|
|
||||||
assert.ok(Object.prototype.hasOwnProperty.call(snap, 'flowSource'));
|
|
||||||
assert.ok(Object.prototype.hasOwnProperty.call(snap, 'secondsRemaining'));
|
|
||||||
});
|
|
||||||
|
|
||||||
test('FlowAggregator.computeRemainingTime — below threshold returns null seconds', async () => {
|
|
||||||
const { fa } = makeAggregator();
|
|
||||||
const r = fa.computeRemainingTime({ value: 0, source: null, direction: 'steady' });
|
|
||||||
assert.equal(r.seconds, null);
|
|
||||||
});
|
|
||||||
@@ -1,106 +0,0 @@
|
|||||||
// Basic tests for MeasurementRouter.
|
|
||||||
|
|
||||||
const test = require('node:test');
|
|
||||||
const assert = require('node:assert/strict');
|
|
||||||
|
|
||||||
const { MeasurementContainer, coolprop } = require('generalFunctions');
|
|
||||||
const MeasurementRouter = require('../../src/measurement/measurementRouter');
|
|
||||||
|
|
||||||
// CoolProp is async-init; ensure it's warm before any pressure-conversion
|
|
||||||
// test runs.
|
|
||||||
test.before(async () => {
|
|
||||||
await coolprop.init({ refrigerant: 'Water' });
|
|
||||||
});
|
|
||||||
|
|
||||||
function makeBasin() {
|
|
||||||
return {
|
|
||||||
surfaceArea: 10,
|
|
||||||
minVol: 2,
|
|
||||||
maxVol: 50,
|
|
||||||
maxVolAtOverflow: 45,
|
|
||||||
overflowLevel: 4.5,
|
|
||||||
outflowLevel: 0.2,
|
|
||||||
inflowLevel: 3,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeMeasurements() {
|
|
||||||
return new MeasurementContainer({
|
|
||||||
autoConvert: true,
|
|
||||||
preferredUnits: { flow: 'm3/s', level: 'm', volume: 'm3' },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function fakeLogger() {
|
|
||||||
const calls = { warn: [], info: [], error: [], debug: [] };
|
|
||||||
return {
|
|
||||||
warn: (m) => calls.warn.push(m),
|
|
||||||
info: (m) => calls.info.push(m),
|
|
||||||
error: (m) => calls.error.push(m),
|
|
||||||
debug: (m) => calls.debug.push(m),
|
|
||||||
_calls: calls,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
test('onLevelMeasurement writes volume + percent', async () => {
|
|
||||||
const measurements = makeMeasurements();
|
|
||||||
const basin = makeBasin();
|
|
||||||
const router = new MeasurementRouter({ measurements, basin });
|
|
||||||
|
|
||||||
router.onLevelMeasurement('atequipment', 2.5, { unit: 'm', timestamp: Date.now() });
|
|
||||||
|
|
||||||
const lvl = measurements.type('level').variant('measured').position('atequipment').getCurrentValue('m');
|
|
||||||
assert.ok(Math.abs(lvl - 2.5) < 1e-9);
|
|
||||||
|
|
||||||
const vol = measurements.type('volume').variant('measured').position('atequipment').getCurrentValue('m3');
|
|
||||||
// 2.5 m * 10 m² = 25 m3.
|
|
||||||
assert.ok(Math.abs(vol - 25) < 1e-9, `volume was ${vol}`);
|
|
||||||
|
|
||||||
const pct = measurements.type('volumePercent').variant('measured').position('atequipment').getCurrentValue('%');
|
|
||||||
// (25 - 2) / (45 - 2) * 100 ≈ 53.488...
|
|
||||||
assert.ok(pct > 53 && pct < 54, `percent was ${pct}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('onPressureMeasurement falls back to assumed temperature and warns', async () => {
|
|
||||||
const measurements = makeMeasurements();
|
|
||||||
const basin = makeBasin();
|
|
||||||
const logger = fakeLogger();
|
|
||||||
const router = new MeasurementRouter({ measurements, basin, logger });
|
|
||||||
|
|
||||||
// No temperature seeded — must fall back to assumed 15C.
|
|
||||||
measurements.type('pressure').variant('measured').position('atequipment')
|
|
||||||
.value(20000, Date.now(), 'Pa');
|
|
||||||
router.onPressureMeasurement('atequipment', 20000, { unit: 'Pa', timestamp: Date.now() });
|
|
||||||
|
|
||||||
const warned = logger._calls.warn.some((m) => /assuming 15C|temperature/i.test(m));
|
|
||||||
assert.ok(warned, 'expected a warn about missing temperature');
|
|
||||||
|
|
||||||
const assumedT = measurements.type('temperature').variant('assumed').position('atequipment')
|
|
||||||
.getCurrentValue('K');
|
|
||||||
assert.ok(Number.isFinite(assumedT), 'assumed temperature was not stored');
|
|
||||||
|
|
||||||
const lvl = measurements.type('level').variant('predicted').position('atequipment')
|
|
||||||
.getCurrentValue('m');
|
|
||||||
// 20000 Pa / (~999 kg/m³ * 9.80665) ≈ 2.04 m.
|
|
||||||
assert.ok(lvl > 1.9 && lvl < 2.2, `derived level was ${lvl}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('route() dispatches by measurement type', async () => {
|
|
||||||
const measurements = makeMeasurements();
|
|
||||||
const basin = makeBasin();
|
|
||||||
const router = new MeasurementRouter({ measurements, basin });
|
|
||||||
|
|
||||||
const handledLevel = router.route('level', 1.5, 'atequipment', { unit: 'm' });
|
|
||||||
assert.equal(handledLevel, true);
|
|
||||||
const lvl = measurements.type('level').variant('measured').position('atequipment').getCurrentValue('m');
|
|
||||||
assert.ok(Math.abs(lvl - 1.5) < 1e-9);
|
|
||||||
|
|
||||||
// Unknown type returns false (no dispatch).
|
|
||||||
const handledOther = router.route('flow', 0.1, 'in', {});
|
|
||||||
assert.equal(handledOther, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('constructor rejects missing context fields', async () => {
|
|
||||||
assert.throws(() => new MeasurementRouter({}));
|
|
||||||
assert.throws(() => new MeasurementRouter({ measurements: makeMeasurements() }));
|
|
||||||
});
|
|
||||||
74
test/basic/nodeClass-config.test.js
Normal file
74
test/basic/nodeClass-config.test.js
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
|
||||||
|
const NodeClass = require('../../src/nodeClass');
|
||||||
|
|
||||||
|
function loadConfig(uiConfig = {}) {
|
||||||
|
const ctx = { name: 'pumpingStation' };
|
||||||
|
NodeClass.prototype._loadConfig.call(ctx, {
|
||||||
|
name: 'PS Config Test',
|
||||||
|
basinVolume: 80,
|
||||||
|
basinHeight: 8,
|
||||||
|
inflowLevel: 3.2,
|
||||||
|
outflowLevel: 0.4,
|
||||||
|
overflowLevel: 7.4,
|
||||||
|
inletPipeDiameter: 0.5,
|
||||||
|
outletPipeDiameter: 0.35,
|
||||||
|
refHeight: 'NAP',
|
||||||
|
minHeightBasedOn: 'outlet',
|
||||||
|
basinBottomRef: -1.2,
|
||||||
|
maxInflowRate: 300,
|
||||||
|
staticHead: 11,
|
||||||
|
maxDischargeHead: 22,
|
||||||
|
pipelineLength: 120,
|
||||||
|
defaultFluid: 'wastewater',
|
||||||
|
temperatureReferenceDegC: 16,
|
||||||
|
controlMode: 'levelbased',
|
||||||
|
minLevel: 0.8,
|
||||||
|
startLevel: 2,
|
||||||
|
maxLevel: 6.5,
|
||||||
|
levelCurveType: 'log',
|
||||||
|
logCurveFactor: 7,
|
||||||
|
enableDryRunProtection: true,
|
||||||
|
dryRunThresholdPercent: 3,
|
||||||
|
enableHighVolumeSafety: true,
|
||||||
|
highVolumeSafetyThresholdPercent: 96,
|
||||||
|
timeleftToFullOrEmptyThresholdSeconds: 60,
|
||||||
|
processOutputFormat: 'process',
|
||||||
|
dbaseOutputFormat: 'influxdb',
|
||||||
|
...uiConfig,
|
||||||
|
}, { id: 'node-1' });
|
||||||
|
return ctx.config;
|
||||||
|
}
|
||||||
|
|
||||||
|
test('nodeClass config mapping — basin, hydraulics, mode and safety fields', () => {
|
||||||
|
const cfg = loadConfig();
|
||||||
|
|
||||||
|
assert.equal(cfg.basin.inletPipeDiameter, 0.5);
|
||||||
|
assert.equal(cfg.basin.outletPipeDiameter, 0.35);
|
||||||
|
assert.equal(cfg.hydraulics.maxInflowRate, 300);
|
||||||
|
assert.equal(cfg.hydraulics.staticHead, 11);
|
||||||
|
assert.equal(cfg.hydraulics.maxDischargeHead, 22);
|
||||||
|
assert.equal(cfg.hydraulics.pipelineLength, 120);
|
||||||
|
assert.equal(cfg.hydraulics.defaultFluid, 'wastewater');
|
||||||
|
assert.equal(cfg.hydraulics.temperatureReferenceDegC, 16);
|
||||||
|
assert.equal(cfg.control.mode, 'levelbased');
|
||||||
|
assert.equal(cfg.control.levelbased.curveType, 'log');
|
||||||
|
assert.equal(cfg.control.levelbased.logCurveFactor, 7);
|
||||||
|
assert.equal(cfg.safety.enableHighVolumeSafety, true);
|
||||||
|
assert.equal(cfg.safety.highVolumeSafetyThresholdPercent, 96);
|
||||||
|
assert.equal(cfg.output.process, 'process');
|
||||||
|
assert.equal(cfg.output.dbase, 'influxdb');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('nodeClass config mapping — accepts deprecated overfill UI fields', () => {
|
||||||
|
const cfg = loadConfig({
|
||||||
|
enableHighVolumeSafety: undefined,
|
||||||
|
highVolumeSafetyThresholdPercent: undefined,
|
||||||
|
enableOverfillProtection: false,
|
||||||
|
overfillThresholdPercent: 91,
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.equal(cfg.safety.enableHighVolumeSafety, false);
|
||||||
|
assert.equal(cfg.safety.highVolumeSafetyThresholdPercent, 91);
|
||||||
|
});
|
||||||
@@ -1,230 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
const test = require('node:test');
|
|
||||||
const assert = require('node:assert');
|
|
||||||
const SafetyController = require('../../src/safety/safetyController');
|
|
||||||
|
|
||||||
// --------------------------- fakes ---------------------------
|
|
||||||
|
|
||||||
function fakeMeasurements(values) {
|
|
||||||
// values keyed by `${type}.${variant}.${position}` → number|null
|
|
||||||
return {
|
|
||||||
getUnit: (_type) => 'm3',
|
|
||||||
type(t) {
|
|
||||||
return {
|
|
||||||
variant(v) {
|
|
||||||
return {
|
|
||||||
position(p) {
|
|
||||||
return {
|
|
||||||
getCurrentValue() {
|
|
||||||
const k = `${t}.${v}.${p}`;
|
|
||||||
return values[k];
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeMachine(positionVsParent, operational = true) {
|
|
||||||
const calls = [];
|
|
||||||
return {
|
|
||||||
config: { functionality: { positionVsParent } },
|
|
||||||
_isOperationalState: () => operational,
|
|
||||||
handleInput: (...args) => calls.push(args),
|
|
||||||
calls,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeStation() {
|
|
||||||
const calls = [];
|
|
||||||
return {
|
|
||||||
handleInput: (...args) => calls.push(args),
|
|
||||||
calls,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeGroup() {
|
|
||||||
const calls = [];
|
|
||||||
return {
|
|
||||||
turnOffAllMachines: () => calls.push(['turnOffAllMachines']),
|
|
||||||
calls,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeLogger() {
|
|
||||||
const warns = [];
|
|
||||||
return {
|
|
||||||
warn: (msg) => warns.push(msg),
|
|
||||||
info: () => {},
|
|
||||||
error: () => {},
|
|
||||||
debug: () => {},
|
|
||||||
warns,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeCtx({
|
|
||||||
vol = 50,
|
|
||||||
basin = { minVol: 10, maxVolAtOverflow: 90 },
|
|
||||||
safety = {
|
|
||||||
enableDryRunProtection: true,
|
|
||||||
enableOverfillProtection: true,
|
|
||||||
dryRunThresholdPercent: 10,
|
|
||||||
overfillThresholdPercent: 95,
|
|
||||||
timeleftToFullOrEmptyThresholdSeconds: 0,
|
|
||||||
},
|
|
||||||
machines = {},
|
|
||||||
stations = {},
|
|
||||||
machineGroups = {},
|
|
||||||
} = {}) {
|
|
||||||
const measurements = fakeMeasurements({
|
|
||||||
'volume.measured.atequipment': vol,
|
|
||||||
'volume.predicted.atequipment': vol,
|
|
||||||
});
|
|
||||||
const logger = makeLogger();
|
|
||||||
return {
|
|
||||||
ctx: { measurements, basin, config: { safety }, logger, machines, stations, machineGroups },
|
|
||||||
logger,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------------------- tests ---------------------------
|
|
||||||
|
|
||||||
test('normal volume + filling → not blocked, no shutdowns', () => {
|
|
||||||
const m = makeMachine('downstream');
|
|
||||||
const { ctx } = makeCtx({ vol: 50, machines: { m } });
|
|
||||||
const sc = new SafetyController(ctx);
|
|
||||||
const r = sc.evaluate({ direction: 'filling', secondsRemaining: 1000 });
|
|
||||||
assert.deepStrictEqual(r, { blocked: false, reason: null, triggered: [] });
|
|
||||||
assert.strictEqual(m.calls.length, 0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('dry-run trigger: low volume + draining → blocked, downstream shut down', () => {
|
|
||||||
const down = makeMachine('downstream');
|
|
||||||
const at = makeMachine('atequipment');
|
|
||||||
const up = makeMachine('upstream');
|
|
||||||
const station = makeStation();
|
|
||||||
const group = makeGroup();
|
|
||||||
const { ctx } = makeCtx({
|
|
||||||
vol: 5, // below 10 * (1 + 10/100) = 11
|
|
||||||
machines: { down, at, up },
|
|
||||||
stations: { station },
|
|
||||||
machineGroups: { group },
|
|
||||||
});
|
|
||||||
const sc = new SafetyController(ctx);
|
|
||||||
const r = sc.evaluate({ direction: 'draining', secondsRemaining: 1000 });
|
|
||||||
assert.strictEqual(r.blocked, true);
|
|
||||||
assert.strictEqual(r.reason, 'dry-run');
|
|
||||||
assert.ok(r.triggered.includes('dry-run-volume'));
|
|
||||||
assert.deepStrictEqual(down.calls[0], ['parent', 'execSequence', 'shutdown']);
|
|
||||||
assert.deepStrictEqual(at.calls[0], ['parent', 'execSequence', 'shutdown']);
|
|
||||||
assert.strictEqual(up.calls.length, 0, 'upstream untouched in dry-run');
|
|
||||||
assert.deepStrictEqual(station.calls[0], ['parent', 'execSequence', 'shutdown']);
|
|
||||||
assert.deepStrictEqual(group.calls[0], ['turnOffAllMachines']);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('dry-run does NOT trigger when filling', () => {
|
|
||||||
const down = makeMachine('downstream');
|
|
||||||
const { ctx } = makeCtx({ vol: 5, machines: { down } });
|
|
||||||
const sc = new SafetyController(ctx);
|
|
||||||
const r = sc.evaluate({ direction: 'filling', secondsRemaining: 1000 });
|
|
||||||
// Filling at vol=5 (below overfill threshold 85.5) → no trigger at all.
|
|
||||||
assert.strictEqual(r.blocked, false);
|
|
||||||
assert.strictEqual(r.reason, null);
|
|
||||||
assert.strictEqual(down.calls.length, 0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('overfill trigger: high volume + filling → not blocked, only upstream + station shut down', () => {
|
|
||||||
const down = makeMachine('downstream');
|
|
||||||
const at = makeMachine('atequipment');
|
|
||||||
const up = makeMachine('upstream');
|
|
||||||
const station = makeStation();
|
|
||||||
const group = makeGroup();
|
|
||||||
const { ctx } = makeCtx({
|
|
||||||
vol: 88, // above 90 * 0.95 = 85.5
|
|
||||||
machines: { down, at, up },
|
|
||||||
stations: { station },
|
|
||||||
machineGroups: { group },
|
|
||||||
});
|
|
||||||
const sc = new SafetyController(ctx);
|
|
||||||
const r = sc.evaluate({ direction: 'filling', secondsRemaining: 1000 });
|
|
||||||
assert.strictEqual(r.blocked, false, 'overfill must NOT block control');
|
|
||||||
assert.strictEqual(r.reason, 'overfill');
|
|
||||||
assert.ok(r.triggered.includes('overfill-volume'));
|
|
||||||
assert.deepStrictEqual(up.calls[0], ['parent', 'execSequence', 'shutdown']);
|
|
||||||
assert.strictEqual(down.calls.length, 0, 'downstream must keep running');
|
|
||||||
assert.strictEqual(at.calls.length, 0, 'atequipment must keep running');
|
|
||||||
assert.deepStrictEqual(station.calls[0], ['parent', 'execSequence', 'shutdown']);
|
|
||||||
assert.strictEqual(group.calls.length, 0, 'machine groups must keep draining');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('no volume data → blocked, all machines shut down (panic)', () => {
|
|
||||||
const a = makeMachine('downstream');
|
|
||||||
const b = makeMachine('upstream');
|
|
||||||
const c = makeMachine('atequipment');
|
|
||||||
// override measurements to return null
|
|
||||||
const measurements = {
|
|
||||||
getUnit: () => 'm3',
|
|
||||||
type: () => ({ variant: () => ({ position: () => ({ getCurrentValue: () => null }) }) }),
|
|
||||||
};
|
|
||||||
const ctx = {
|
|
||||||
measurements,
|
|
||||||
basin: { minVol: 10, maxVolAtOverflow: 90 },
|
|
||||||
config: { safety: { enableDryRunProtection: true, enableOverfillProtection: true, dryRunThresholdPercent: 10, overfillThresholdPercent: 95 } },
|
|
||||||
logger: makeLogger(),
|
|
||||||
machines: { a, b, c },
|
|
||||||
stations: {},
|
|
||||||
machineGroups: {},
|
|
||||||
};
|
|
||||||
const sc = new SafetyController(ctx);
|
|
||||||
const r = sc.evaluate({ direction: 'steady', secondsRemaining: null });
|
|
||||||
assert.strictEqual(r.blocked, true);
|
|
||||||
assert.strictEqual(r.reason, 'no-volume-data');
|
|
||||||
assert.deepStrictEqual(a.calls[0], ['parent', 'execSequence', 'shutdown']);
|
|
||||||
assert.deepStrictEqual(b.calls[0], ['parent', 'execSequence', 'shutdown']);
|
|
||||||
assert.deepStrictEqual(c.calls[0], ['parent', 'execSequence', 'shutdown']);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('time-based protection: short remainingTime while draining triggers dry-run shutdowns', () => {
|
|
||||||
const down = makeMachine('downstream');
|
|
||||||
const { ctx } = makeCtx({
|
|
||||||
vol: 50, // well above dry-run vol threshold
|
|
||||||
safety: {
|
|
||||||
enableDryRunProtection: false, // volume rule disabled
|
|
||||||
enableOverfillProtection: false,
|
|
||||||
dryRunThresholdPercent: 10,
|
|
||||||
overfillThresholdPercent: 95,
|
|
||||||
timeleftToFullOrEmptyThresholdSeconds: 60,
|
|
||||||
},
|
|
||||||
machines: { down },
|
|
||||||
});
|
|
||||||
const sc = new SafetyController(ctx);
|
|
||||||
const r = sc.evaluate({ direction: 'draining', secondsRemaining: 30 });
|
|
||||||
assert.strictEqual(r.blocked, true);
|
|
||||||
assert.strictEqual(r.reason, 'dry-run');
|
|
||||||
assert.ok(r.triggered.includes('time-remaining'));
|
|
||||||
assert.deepStrictEqual(down.calls[0], ['parent', 'execSequence', 'shutdown']);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('disabled rules: enableDryRunProtection=false + draining low → no trigger', () => {
|
|
||||||
const down = makeMachine('downstream');
|
|
||||||
const { ctx } = makeCtx({
|
|
||||||
vol: 5, // would normally trigger dry-run
|
|
||||||
safety: {
|
|
||||||
enableDryRunProtection: false,
|
|
||||||
enableOverfillProtection: false,
|
|
||||||
dryRunThresholdPercent: 10,
|
|
||||||
overfillThresholdPercent: 95,
|
|
||||||
timeleftToFullOrEmptyThresholdSeconds: 0,
|
|
||||||
},
|
|
||||||
machines: { down },
|
|
||||||
});
|
|
||||||
const sc = new SafetyController(ctx);
|
|
||||||
const r = sc.evaluate({ direction: 'draining', secondsRemaining: 1000 });
|
|
||||||
assert.strictEqual(r.blocked, false);
|
|
||||||
assert.strictEqual(r.reason, null);
|
|
||||||
assert.strictEqual(down.calls.length, 0);
|
|
||||||
});
|
|
||||||
@@ -27,6 +27,8 @@ function makeConfig(overrides = {}) {
|
|||||||
inflowLevel: 3,
|
inflowLevel: 3,
|
||||||
outflowLevel: 0.2,
|
outflowLevel: 0.2,
|
||||||
overflowLevel: 4.5,
|
overflowLevel: 4.5,
|
||||||
|
inletPipeDiameter: 0.4,
|
||||||
|
outletPipeDiameter: 0.3,
|
||||||
},
|
},
|
||||||
hydraulics: {
|
hydraulics: {
|
||||||
refHeight: 'NAP',
|
refHeight: 'NAP',
|
||||||
@@ -36,12 +38,13 @@ function makeConfig(overrides = {}) {
|
|||||||
control: {
|
control: {
|
||||||
mode: 'levelbased',
|
mode: 'levelbased',
|
||||||
allowedModes: new Set(['levelbased', 'manual']),
|
allowedModes: new Set(['levelbased', 'manual']),
|
||||||
levelbased: { minLevel: 1, startLevel: 2, maxLevel: 4 },
|
levelbased: { minLevel: 1, startLevel: 2, maxLevel: 4, curveType: 'linear', logCurveFactor: 9 },
|
||||||
},
|
},
|
||||||
safety: {
|
safety: {
|
||||||
enableDryRunProtection: false,
|
enableDryRunProtection: false,
|
||||||
enableOverfillProtection: false,
|
enableOverfillProtection: false,
|
||||||
dryRunThresholdPercent: 2,
|
dryRunThresholdPercent: 2,
|
||||||
|
highVolumeSafetyThresholdPercent: 98,
|
||||||
overfillThresholdPercent: 98,
|
overfillThresholdPercent: 98,
|
||||||
timeleftToFullOrEmptyThresholdSeconds: 0,
|
timeleftToFullOrEmptyThresholdSeconds: 0,
|
||||||
},
|
},
|
||||||
@@ -80,6 +83,10 @@ test('Basin geometry — derived values', async (t) => {
|
|||||||
const ps2 = new PumpingStation(makeConfig({ hydraulics: { minHeightBasedOn: 'inlet' } }));
|
const ps2 = new PumpingStation(makeConfig({ hydraulics: { minHeightBasedOn: 'inlet' } }));
|
||||||
assert.equal(ps2.basin.minVol, 30);
|
assert.equal(ps2.basin.minVol, 30);
|
||||||
});
|
});
|
||||||
|
await t.test('pipe diameters are part of basin contract', () => {
|
||||||
|
assert.equal(ps.basin.inletPipeDiameter, 0.4);
|
||||||
|
assert.equal(ps.basin.outletPipeDiameter, 0.3);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Level ↔ volume roundtrip', async (t) => {
|
test('Level ↔ volume roundtrip', async (t) => {
|
||||||
@@ -131,6 +138,17 @@ test('Threshold guardrails — _validateThresholdOrdering', async (t) => {
|
|||||||
assert.ok(ps.thresholdIssues.some((i) => i.aName === 'startLevel'));
|
assert.ok(ps.thresholdIssues.some((i) => i.aName === 'startLevel'));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await t.test('startLevel > inflowLevel flagged for levelbased rising hold zone', () => {
|
||||||
|
const ps = new PumpingStation(makeConfig({
|
||||||
|
control: {
|
||||||
|
mode: 'levelbased',
|
||||||
|
allowedModes: new Set(['levelbased']),
|
||||||
|
levelbased: { minLevel: 1, startLevel: 3.5, maxLevel: 4, curveType: 'linear' },
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
assert.ok(ps.thresholdIssues.some((i) => i.aName === 'startLevel' && i.bName === 'inflowLevel'));
|
||||||
|
});
|
||||||
|
|
||||||
await t.test('outflowLevel >= inflowLevel flagged', () => {
|
await t.test('outflowLevel >= inflowLevel flagged', () => {
|
||||||
const ps = new PumpingStation(makeConfig({
|
const ps = new PumpingStation(makeConfig({
|
||||||
basin: { volume: 50, height: 5, inflowLevel: 0.1, outflowLevel: 0.5, overflowLevel: 4.5 },
|
basin: { volume: 50, height: 5, inflowLevel: 0.1, outflowLevel: 0.5, overflowLevel: 4.5 },
|
||||||
@@ -223,20 +241,22 @@ test('Levelbased control zones — _controlLevelBased', async (t) => {
|
|||||||
assert.equal(turnOffCalls, 1);
|
assert.equal(turnOffCalls, 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
await t.test('minLevel ≤ level < startLevel → dead zone, percControl unchanged', async () => {
|
await t.test('minLevel ≤ level < active ramp start → commands 0% without shutdown', async () => {
|
||||||
const ps = new PumpingStation(makeConfig());
|
const ps = new PumpingStation(makeConfig());
|
||||||
ps.percControl = 42; // simulated previous demand
|
ps.percControl = 42; // simulated previous demand
|
||||||
|
const demands = [];
|
||||||
ps.machineGroups['mgc1'] = {
|
ps.machineGroups['mgc1'] = {
|
||||||
config: { general: { name: 'mgc1' } },
|
config: { general: { name: 'mgc1' } },
|
||||||
turnOffAllMachines: () => {},
|
turnOffAllMachines: () => {},
|
||||||
handleInput: async () => { throw new Error('should not be called in dead zone'); },
|
handleInput: async (_src, d) => { demands.push(d); },
|
||||||
};
|
};
|
||||||
ps.calibratePredictedLevel(1.5); // between minLevel=1 and startLevel=2
|
ps.calibratePredictedLevel(1.5); // between minLevel=1 and startLevel=2
|
||||||
await ps._controlLevelBased();
|
await ps._controlLevelBased();
|
||||||
assert.equal(ps.percControl, 42); // unchanged
|
assert.equal(ps.percControl, 0);
|
||||||
|
assert.equal(demands[0], 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
await t.test('level ≥ startLevel → percControl linearly scaled to [0,100]', async () => {
|
await t.test('filling: level between startLevel and inflowLevel commands 0%', async () => {
|
||||||
const ps = new PumpingStation(makeConfig());
|
const ps = new PumpingStation(makeConfig());
|
||||||
const demands = [];
|
const demands = [];
|
||||||
ps.machineGroups['mgc1'] = {
|
ps.machineGroups['mgc1'] = {
|
||||||
@@ -244,14 +264,144 @@ test('Levelbased control zones — _controlLevelBased', async (t) => {
|
|||||||
turnOffAllMachines: () => {},
|
turnOffAllMachines: () => {},
|
||||||
handleInput: async (_src, d) => { demands.push(d); },
|
handleInput: async (_src, d) => { demands.push(d); },
|
||||||
};
|
};
|
||||||
ps.calibratePredictedLevel(3); // midpoint of startLevel=2 and maxLevel=4
|
ps.calibratePredictedLevel(2.5); // startLevel=2, inflowLevel=3
|
||||||
await ps._controlLevelBased();
|
await ps._controlLevelBased('filling');
|
||||||
// lerp(3, [2,4], [0,100]) = 50
|
assert.equal(ps.percControl, 0);
|
||||||
|
assert.equal(demands[0], 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.test('filling: level ≥ inflowLevel → percControl linearly scaled to [0,100]', async () => {
|
||||||
|
const ps = new PumpingStation(makeConfig());
|
||||||
|
const demands = [];
|
||||||
|
ps.machineGroups['mgc1'] = {
|
||||||
|
config: { general: { name: 'mgc1' } },
|
||||||
|
turnOffAllMachines: () => {},
|
||||||
|
handleInput: async (_src, d) => { demands.push(d); },
|
||||||
|
};
|
||||||
|
ps.calibratePredictedLevel(3.5); // midpoint of inflowLevel=3 and maxLevel=4
|
||||||
|
await ps._controlLevelBased('filling');
|
||||||
|
// lerp(3.5, [3,4], [0,100]) = 50
|
||||||
assert.ok(Math.abs(ps.percControl - 50) < 1e-9);
|
assert.ok(Math.abs(ps.percControl - 50) < 1e-9);
|
||||||
assert.equal(demands.length, 1);
|
assert.equal(demands.length, 1);
|
||||||
assert.ok(Math.abs(demands[0] - 50) < 1e-9);
|
assert.ok(Math.abs(demands[0] - 50) < 1e-9);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await t.test('shift disabled (default): foot stays at inflowLevel even after fall', async () => {
|
||||||
|
const ps = new PumpingStation(makeConfig());
|
||||||
|
ps.machineGroups['mgc1'] = {
|
||||||
|
config: { general: { name: 'mgc1' } },
|
||||||
|
turnOffAllMachines: () => {},
|
||||||
|
handleInput: async () => {},
|
||||||
|
};
|
||||||
|
// Climb past inflowLevel and beyond, then fall to a level inside [start..inflow].
|
||||||
|
ps.calibratePredictedLevel(3.8);
|
||||||
|
await ps._controlLevelBased();
|
||||||
|
assert.ok(ps.percControl > 0);
|
||||||
|
ps.calibratePredictedLevel(2.5); // between startLevel=2 and inflowLevel=3
|
||||||
|
await ps._controlLevelBased();
|
||||||
|
// Without shift the foot is inflowLevel → 0% in the hold zone.
|
||||||
|
assert.equal(ps.percControl, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.test('shift enabled: arming on % threshold + hold-then-ramp on draining', async () => {
|
||||||
|
// Geometry: inflow=3, max=4 → up curve goes 0%@3 to 100%@4.
|
||||||
|
// shiftArmPercent=80 ⇒ arms when up curve ≥ 80 % i.e. level ≥ 3.8.
|
||||||
|
// shiftLevel=3.5 ⇒ held output starts ramping down at this level.
|
||||||
|
const ps = new PumpingStation(makeConfig({
|
||||||
|
control: {
|
||||||
|
mode: 'levelbased',
|
||||||
|
allowedModes: new Set(['levelbased']),
|
||||||
|
levelbased: {
|
||||||
|
minLevel: 1, startLevel: 2, maxLevel: 4, curveType: 'linear', logCurveFactor: 9,
|
||||||
|
enableShiftedRamp: true, shiftLevel: 3.5, shiftArmPercent: 80,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
ps.machineGroups['mgc1'] = {
|
||||||
|
config: { general: { name: 'mgc1' } },
|
||||||
|
turnOffAllMachines: () => {},
|
||||||
|
handleInput: async () => {},
|
||||||
|
};
|
||||||
|
// Filling at level=3.5 ⇒ up curve = 50 %, below arm threshold ⇒ not armed.
|
||||||
|
ps.calibratePredictedLevel(3.5);
|
||||||
|
await ps._controlLevelBased('filling');
|
||||||
|
assert.equal(ps._shiftArmed, false);
|
||||||
|
assert.ok(Math.abs(ps.percControl - 50) < 1e-9);
|
||||||
|
// Filling at level=3.85 ⇒ up curve = 85 % ≥ arm threshold ⇒ ARM.
|
||||||
|
ps.calibratePredictedLevel(3.85);
|
||||||
|
await ps._controlLevelBased('filling');
|
||||||
|
assert.equal(ps._shiftArmed, true);
|
||||||
|
assert.ok(Math.abs(ps.percControl - 85) < 1e-9); // still up curve while filling
|
||||||
|
// Direction flips to draining at the same level ⇒ capture hold ≈ 85 %.
|
||||||
|
await ps._controlLevelBased('draining');
|
||||||
|
assert.ok(Math.abs(ps._shiftHoldValue - 85) < 1e-6);
|
||||||
|
// While draining and level ≥ shiftLevel ⇒ output stays at hold (≈85 %).
|
||||||
|
ps.calibratePredictedLevel(3.6);
|
||||||
|
await ps._controlLevelBased('draining');
|
||||||
|
assert.ok(Math.abs(ps.percControl - 85) < 1e-6);
|
||||||
|
// Below shiftLevel: ramp [shift, hold] → [start, 0]. At level=2.75
|
||||||
|
// (midpoint of [2, 3.5]), x=0.5, output ≈ 85 × 0.5 = 42.5 %.
|
||||||
|
ps.calibratePredictedLevel(2.75);
|
||||||
|
await ps._controlLevelBased('draining');
|
||||||
|
assert.ok(Math.abs(ps.percControl - 42.5) < 1e-6);
|
||||||
|
// Below startLevel ⇒ output 0 % AND disarm.
|
||||||
|
ps.calibratePredictedLevel(1.9);
|
||||||
|
await ps._controlLevelBased('draining');
|
||||||
|
assert.equal(ps.percControl, 0);
|
||||||
|
assert.equal(ps._shiftArmed, false);
|
||||||
|
assert.equal(ps._shiftHoldValue, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.test('shift enabled: returning to filling clears hold; new hold captured on next drain', async () => {
|
||||||
|
const ps = new PumpingStation(makeConfig({
|
||||||
|
control: {
|
||||||
|
mode: 'levelbased',
|
||||||
|
allowedModes: new Set(['levelbased']),
|
||||||
|
levelbased: {
|
||||||
|
minLevel: 1, startLevel: 2, maxLevel: 4, curveType: 'linear', logCurveFactor: 9,
|
||||||
|
enableShiftedRamp: true, shiftLevel: 3.5, shiftArmPercent: 80,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
ps.machineGroups['mgc1'] = {
|
||||||
|
config: { general: { name: 'mgc1' } },
|
||||||
|
turnOffAllMachines: () => {},
|
||||||
|
handleInput: async () => {},
|
||||||
|
};
|
||||||
|
ps.calibratePredictedLevel(3.85);
|
||||||
|
await ps._controlLevelBased('filling');
|
||||||
|
await ps._controlLevelBased('draining');
|
||||||
|
assert.ok(Math.abs(ps._shiftHoldValue - 85) < 1e-6);
|
||||||
|
// Direction back to filling ⇒ up curve, hold cleared, still armed.
|
||||||
|
ps.calibratePredictedLevel(3.9);
|
||||||
|
await ps._controlLevelBased('filling');
|
||||||
|
assert.equal(ps._shiftHoldValue, null);
|
||||||
|
assert.equal(ps._shiftArmed, true);
|
||||||
|
assert.ok(Math.abs(ps.percControl - 90) < 1e-6); // up curve at 3.9 = 90 %
|
||||||
|
// Flip to draining again at higher level ⇒ new hold ≈ 90 %.
|
||||||
|
await ps._controlLevelBased('draining');
|
||||||
|
assert.ok(Math.abs(ps._shiftHoldValue - 90) < 1e-6);
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.test('log curve has fast early response', async () => {
|
||||||
|
const ps = new PumpingStation(makeConfig({
|
||||||
|
control: {
|
||||||
|
mode: 'levelbased',
|
||||||
|
allowedModes: new Set(['levelbased']),
|
||||||
|
levelbased: { minLevel: 1, startLevel: 2, maxLevel: 4, curveType: 'log', logCurveFactor: 9 },
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
ps.machineGroups['mgc1'] = {
|
||||||
|
config: { general: { name: 'mgc1' } },
|
||||||
|
turnOffAllMachines: () => {},
|
||||||
|
handleInput: async () => {},
|
||||||
|
};
|
||||||
|
ps.calibratePredictedLevel(3.5); // x=0.5 on filling ramp [3,4]
|
||||||
|
await ps._controlLevelBased('filling');
|
||||||
|
assert.ok(ps.percControl > 50);
|
||||||
|
assert.ok(ps.percControl < 100);
|
||||||
|
});
|
||||||
|
|
||||||
await t.test('level > maxLevel → percControl ≥ 100 (MGC clamps internally)', async () => {
|
await t.test('level > maxLevel → percControl ≥ 100 (MGC clamps internally)', async () => {
|
||||||
const ps = new PumpingStation(makeConfig());
|
const ps = new PumpingStation(makeConfig());
|
||||||
ps.machineGroups['mgc1'] = {
|
ps.machineGroups['mgc1'] = {
|
||||||
@@ -275,6 +425,10 @@ test('getOutput — flattens basin + state + demand', async (t) => {
|
|||||||
assert.equal(out.maxVolAtOverflow, 45);
|
assert.equal(out.maxVolAtOverflow, 45);
|
||||||
assert.equal(out.minVolAtInflow, 30);
|
assert.equal(out.minVolAtInflow, 30);
|
||||||
assert.ok(Math.abs(out.minVolAtOutflow - 2) < 1e-9);
|
assert.ok(Math.abs(out.minVolAtOutflow - 2) < 1e-9);
|
||||||
|
assert.equal(out.inletPipeDiameter, 0.4);
|
||||||
|
assert.equal(out.outletPipeDiameter, 0.3);
|
||||||
|
assert.ok(Math.abs(out.highVolumeSafetyLevel - 4.41) < 1e-9);
|
||||||
|
assert.ok(Math.abs(out.dryRunLevel - 0.204) < 1e-9);
|
||||||
});
|
});
|
||||||
await t.test('includes state fields (direction, flowSource, timeleft)', () => {
|
await t.test('includes state fields (direction, flowSource, timeleft)', () => {
|
||||||
const out = ps.getOutput();
|
const out = ps.getOutput();
|
||||||
@@ -293,3 +447,155 @@ test('Manual inflow — setManualInflow stores predicted inflow', async (t) => {
|
|||||||
const v = ps.measurements.type('flow').variant('predicted').position('in').child('manual-qin').getCurrentValue('m3/s');
|
const v = ps.measurements.type('flow').variant('predicted').position('in').child('manual-qin').getCurrentValue('m3/s');
|
||||||
assert.ok(Math.abs(v - 0.05) < 1e-9);
|
assert.ok(Math.abs(v - 0.05) < 1e-9);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// _updatePredictedVolume now clamps [dryRunSafetyVol, maxVolAtOverflow] and
|
||||||
|
// tracks any excess as cumulative `overflowVolume` plus a synthetic
|
||||||
|
// `flow.predicted.out.overflow` rate so net-flow balance stays at ~0 while
|
||||||
|
// pinned. We drive ticks manually with monotonic timestamps to keep tests
|
||||||
|
// deterministic (Date.now() in the integrator can step by 0 ms in fast loops).
|
||||||
|
test('Predicted volume — overflow clamp and spill tracking', async (t) => {
|
||||||
|
const ps = new PumpingStation(makeConfig({
|
||||||
|
safety: { enableDryRunProtection: false, enableHighVolumeSafety: false, dryRunThresholdPercent: 0 },
|
||||||
|
}));
|
||||||
|
// Seed predicted volume just below the spill point.
|
||||||
|
// maxVolAtOverflow = overflowLevel × area = 4.5 × 10 = 45 m³.
|
||||||
|
const t0 = 1_700_000_000_000;
|
||||||
|
ps.calibratePredictedVolume(44, t0);
|
||||||
|
// Heavy inflow, no real outflow (no pumps wired).
|
||||||
|
ps.setManualInflow(2, t0, 'm3/s'); // 2 m³/s, dt=1s → 2 m³/tick
|
||||||
|
|
||||||
|
await t.test('first overflow tick clamps volume and records spill increment', () => {
|
||||||
|
ps._predictedFlowState = { inflow: 2, outflow: 0, lastTimestamp: t0 };
|
||||||
|
Date.now = () => t0 + 1000;
|
||||||
|
ps._updatePredictedVolume();
|
||||||
|
const vol = ps.measurements.type('volume').variant('predicted').position('atequipment').getCurrentValue('m3');
|
||||||
|
assert.equal(vol, 45); // pinned at overflow
|
||||||
|
const cumulative = ps.measurements.type('overflowVolume').variant('predicted').position('atequipment').getCurrentValue('m3');
|
||||||
|
assert.equal(cumulative, 1); // proposed=44+2=46, excess=1 m³ this tick
|
||||||
|
const spill = ps.measurements.type('flow').variant('predicted').position('overflow').getCurrentValue('m3/s');
|
||||||
|
assert.equal(spill, 2); // instantaneous balance: inflow − outflowReal
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.test('subsequent ticks accumulate full inflow as spill (stable)', () => {
|
||||||
|
Date.now = () => t0 + 2000;
|
||||||
|
ps._updatePredictedVolume();
|
||||||
|
const vol = ps.measurements.type('volume').variant('predicted').position('atequipment').getCurrentValue('m3');
|
||||||
|
assert.equal(vol, 45);
|
||||||
|
const cumulative = ps.measurements.type('overflowVolume').variant('predicted').position('atequipment').getCurrentValue('m3');
|
||||||
|
assert.equal(cumulative, 3); // 1 + 2
|
||||||
|
const spill = ps.measurements.type('flow').variant('predicted').position('overflow').getCurrentValue('m3/s');
|
||||||
|
assert.equal(spill, 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.test('predicted net flow reads ~0 while pinned at overflow', () => {
|
||||||
|
const net = ps._selectBestNetFlow();
|
||||||
|
// inflow=2, outflow_total=2 (synthetic spill), net = 0
|
||||||
|
assert.ok(Math.abs(net.value) < 1e-9);
|
||||||
|
assert.equal(net.source, 'predicted');
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.test('once inflow stops, spill flow clears and clamp releases', () => {
|
||||||
|
ps.setManualInflow(0, t0 + 2000, 'm3/s');
|
||||||
|
ps._predictedFlowState = { inflow: 0, outflow: 0, lastTimestamp: t0 + 2000 };
|
||||||
|
Date.now = () => t0 + 3000;
|
||||||
|
ps._updatePredictedVolume();
|
||||||
|
const spill = ps.measurements.type('flow').variant('predicted').position('overflow').getCurrentValue('m3/s');
|
||||||
|
assert.equal(spill, 0);
|
||||||
|
// Volume stays at 45 (no draining force) but is no longer "pinned".
|
||||||
|
const vol = ps.measurements.type('volume').variant('predicted').position('atequipment').getCurrentValue('m3');
|
||||||
|
assert.equal(vol, 45);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Predicted volume — dry-run lower clamp', async (t) => {
|
||||||
|
const ps = new PumpingStation(makeConfig({
|
||||||
|
// dryRunSafetyVol = minVolAtOutflow × (1 + 5/100) = 2 × 1.05 = 2.1 m³
|
||||||
|
safety: { enableDryRunProtection: true, dryRunThresholdPercent: 5 },
|
||||||
|
}));
|
||||||
|
const t0 = 1_700_000_000_000;
|
||||||
|
|
||||||
|
await t.test('initial seed below dryRunSafetyVol is left alone (no upward bump)', () => {
|
||||||
|
// Seed defaults to minVol=2 (below dryRunSafetyVol=2.1).
|
||||||
|
ps._predictedFlowState = { inflow: 0, outflow: 0, lastTimestamp: t0 };
|
||||||
|
Date.now = () => t0 + 1000;
|
||||||
|
ps._updatePredictedVolume();
|
||||||
|
const vol = ps.measurements.type('volume').variant('predicted').position('atequipment').getCurrentValue('m3');
|
||||||
|
assert.equal(vol, 2); // unchanged — clamp doesn't fire because we started below it
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.test('drain across dryRunSafetyVol clamps at the threshold', () => {
|
||||||
|
// Calibrate well above, then push outflow that would cross the threshold.
|
||||||
|
ps.calibratePredictedVolume(3, t0 + 1000);
|
||||||
|
// outflow=2 m³/s for 1s → would drop to 1; clamp catches at 2.1.
|
||||||
|
ps.setManualOutflow(2, t0 + 1000, 'm3/s');
|
||||||
|
ps._predictedFlowState = { inflow: 0, outflow: 2, lastTimestamp: t0 + 1000 };
|
||||||
|
Date.now = () => t0 + 2000;
|
||||||
|
ps._updatePredictedVolume();
|
||||||
|
const vol = ps.measurements.type('volume').variant('predicted').position('atequipment').getCurrentValue('m3');
|
||||||
|
assert.ok(Math.abs(vol - 2.1) < 1e-9);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getOutput — exposes predictedOverflowVolume / predictedOverflowRate', () => {
|
||||||
|
const ps = new PumpingStation(makeConfig());
|
||||||
|
// Seed an overflow scenario.
|
||||||
|
const t0 = 1_700_000_000_000;
|
||||||
|
ps.calibratePredictedVolume(44, t0);
|
||||||
|
ps.setManualInflow(2, t0, 'm3/s');
|
||||||
|
ps._predictedFlowState = { inflow: 2, outflow: 0, lastTimestamp: t0 };
|
||||||
|
Date.now = () => t0 + 1000;
|
||||||
|
ps._updatePredictedVolume();
|
||||||
|
const out = ps.getOutput();
|
||||||
|
assert.equal(out.predictedOverflowVolume, 1);
|
||||||
|
assert.equal(out.predictedOverflowRate, 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Hard physical floor at 0. The dryRunSafetyVol clamp only fires on transition
|
||||||
|
// from above, so a basin seeded below + continued outflow used to integrate
|
||||||
|
// the volume arbitrarily negative. The level helper masked this by flooring
|
||||||
|
// at 0 in _calcLevelFromVolume — fix is to floor the integrator itself.
|
||||||
|
test('Predicted volume — physical floor at 0 (underflow track)', async (t) => {
|
||||||
|
const ps = new PumpingStation(makeConfig({
|
||||||
|
safety: { enableDryRunProtection: true, dryRunThresholdPercent: 5 },
|
||||||
|
}));
|
||||||
|
const t0 = 1_700_000_000_000;
|
||||||
|
|
||||||
|
await t.test('seeded below dryRun + continued outflow does NOT go negative', () => {
|
||||||
|
ps.calibratePredictedVolume(0.5, t0); // below dryRunSafetyVol (2.1)
|
||||||
|
ps.setManualOutflow(2, t0, 'm3/s'); // 2 m³/s for 1s → would drop to -1.5
|
||||||
|
ps._predictedFlowState = { inflow: 0, outflow: 2, lastTimestamp: t0 };
|
||||||
|
Date.now = () => t0 + 1000;
|
||||||
|
ps._updatePredictedVolume();
|
||||||
|
const vol = ps.measurements.type('volume').variant('predicted').position('atequipment').getCurrentValue('m3');
|
||||||
|
assert.equal(vol, 0); // floored at 0, not -1.5
|
||||||
|
const underflow = ps.measurements
|
||||||
|
.type('underflowVolume').variant('predicted').position('atequipment').getCurrentValue('m3');
|
||||||
|
assert.equal(underflow, 1.5); // tracked as diagnostic
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.test('subsequent ticks accumulate underflow while outflow continues', () => {
|
||||||
|
Date.now = () => t0 + 2000;
|
||||||
|
ps._predictedFlowState = { inflow: 0, outflow: 2, lastTimestamp: t0 + 1000 };
|
||||||
|
ps._updatePredictedVolume();
|
||||||
|
const vol = ps.measurements.type('volume').variant('predicted').position('atequipment').getCurrentValue('m3');
|
||||||
|
assert.equal(vol, 0);
|
||||||
|
const underflow = ps.measurements
|
||||||
|
.type('underflowVolume').variant('predicted').position('atequipment').getCurrentValue('m3');
|
||||||
|
assert.equal(underflow, 3.5); // 1.5 + 2.0
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.test('getOutput exposes predictedUnderflowVolume', () => {
|
||||||
|
const out = ps.getOutput();
|
||||||
|
assert.equal(out.predictedUnderflowVolume, 3.5);
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.test('inflow returns and basin refills from 0 (no jump to dryRunSafetyVol)', () => {
|
||||||
|
ps.setManualInflow(1, t0 + 2000, 'm3/s');
|
||||||
|
ps.setManualOutflow(0, t0 + 2000, 'm3/s');
|
||||||
|
ps._predictedFlowState = { inflow: 1, outflow: 0, lastTimestamp: t0 + 2000 };
|
||||||
|
Date.now = () => t0 + 3000;
|
||||||
|
ps._updatePredictedVolume();
|
||||||
|
const vol = ps.measurements.type('volume').variant('predicted').position('atequipment').getCurrentValue('m3');
|
||||||
|
assert.ok(Math.abs(vol - 1) < 1e-9); // 0 + 1 = 1, NOT pinned to 2.1
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,123 +0,0 @@
|
|||||||
// Basic unit tests for thresholdValidator.
|
|
||||||
// Run with: node --test test/basic/thresholdValidator.basic.test.js
|
|
||||||
|
|
||||||
const test = require('node:test');
|
|
||||||
const assert = require('node:assert/strict');
|
|
||||||
|
|
||||||
const { validateThresholdOrdering } = require('../../src/basin/thresholdValidator');
|
|
||||||
const BasinGeometry = require('../../src/basin/BasinGeometry');
|
|
||||||
|
|
||||||
// A valid baseline: outlet 0.2 < inflow 3 < overflow 4.5 ≤ height 5,
|
|
||||||
// dryRun = 0.2 * 1.10 = 0.22 ≤ minLevel 1 ≤ start 2 < max 4 ≤ overfill 4.275.
|
|
||||||
function validBasinAndCfg() {
|
|
||||||
const basin = new BasinGeometry(
|
|
||||||
{ volume: 50, height: 5, inflowLevel: 3, outflowLevel: 0.2, overflowLevel: 4.5 },
|
|
||||||
{ minHeightBasedOn: 'outlet' }
|
|
||||||
);
|
|
||||||
const levelbased = { minLevel: 1, startLevel: 2, maxLevel: 4 };
|
|
||||||
const safety = { dryRunThresholdPercent: 10, overfillThresholdPercent: 95 };
|
|
||||||
return { basin, levelbased, safety };
|
|
||||||
}
|
|
||||||
|
|
||||||
test('valid ordering returns empty array', () => {
|
|
||||||
const { basin, levelbased, safety } = validBasinAndCfg();
|
|
||||||
const issues = validateThresholdOrdering(basin, levelbased, safety);
|
|
||||||
assert.deepEqual(issues, []);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('outflowLevel >= inflowLevel triggers issue with correct shape', () => {
|
|
||||||
const basin = new BasinGeometry(
|
|
||||||
// outflow 3.5 > inflow 3 — invariant broken.
|
|
||||||
{ volume: 50, height: 5, inflowLevel: 3, outflowLevel: 3.5, overflowLevel: 4.5 },
|
|
||||||
{ minHeightBasedOn: 'outlet' }
|
|
||||||
);
|
|
||||||
const issues = validateThresholdOrdering(basin, { minLevel: 1, startLevel: 2, maxLevel: 4 }, { dryRunThresholdPercent: 0, overfillThresholdPercent: 100 });
|
|
||||||
const hit = issues.find((i) => i.aName === 'outflowLevel' && i.bName === 'inflowLevel');
|
|
||||||
assert.ok(hit, 'expected an outflowLevel < inflowLevel issue');
|
|
||||||
assert.equal(hit.op, '<');
|
|
||||||
assert.equal(hit.a, 3.5);
|
|
||||||
assert.equal(hit.b, 3);
|
|
||||||
assert.match(hit.msg, /outflowLevel.*<.*inflowLevel/);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('maxLevel >= overfillLevel triggers issue', () => {
|
|
||||||
const { basin } = validBasinAndCfg();
|
|
||||||
// overfillLevel = overflowLevel × overfillPct/100 = 4.5 × 0.80 = 3.6.
|
|
||||||
// maxLevel 4 > 3.6 → expect a `maxLevel <= overfillLevel` issue.
|
|
||||||
const issues = validateThresholdOrdering(
|
|
||||||
basin,
|
|
||||||
{ minLevel: 1, startLevel: 2, maxLevel: 4 },
|
|
||||||
{ dryRunThresholdPercent: 10, overfillThresholdPercent: 80 }
|
|
||||||
);
|
|
||||||
const hit = issues.find((i) => i.aName === 'maxLevel' && i.bName === 'overfillLevel');
|
|
||||||
assert.ok(hit, 'expected a maxLevel <= overfillLevel issue');
|
|
||||||
assert.equal(hit.op, '<=');
|
|
||||||
assert.equal(hit.a, 4);
|
|
||||||
assert.ok(Math.abs(hit.b - 3.6) < 1e-9);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('NaN / undefined values are skipped, not flagged as issues', () => {
|
|
||||||
const { basin } = validBasinAndCfg();
|
|
||||||
const issues = validateThresholdOrdering(
|
|
||||||
basin,
|
|
||||||
{ minLevel: undefined, startLevel: NaN, maxLevel: 4 },
|
|
||||||
{ dryRunThresholdPercent: 10, overfillThresholdPercent: 95 }
|
|
||||||
);
|
|
||||||
// dryRunLevel <= minLevel skipped (minLevel undefined → NaN)
|
|
||||||
// minLevel <= startLevel skipped (both NaN-ish)
|
|
||||||
// startLevel < maxLevel skipped (startLevel NaN)
|
|
||||||
// maxLevel <= overfillLevel still checked → 4 ≤ 4.275 OK.
|
|
||||||
// Geometry checks also OK.
|
|
||||||
assert.deepEqual(issues, []);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('multiple violations produce multiple issues in stable order', () => {
|
|
||||||
// Build a basin with two geometry violations.
|
|
||||||
const basin = new BasinGeometry(
|
|
||||||
// outflow 4 > inflow 3 (broken) AND overflow 6 > height 5 (broken)
|
|
||||||
{ volume: 50, height: 5, inflowLevel: 3, outflowLevel: 4, overflowLevel: 6 },
|
|
||||||
{ minHeightBasedOn: 'outlet' }
|
|
||||||
);
|
|
||||||
const issues = validateThresholdOrdering(
|
|
||||||
basin,
|
|
||||||
{ minLevel: 1, startLevel: 2, maxLevel: 4 },
|
|
||||||
{ dryRunThresholdPercent: 0, overfillThresholdPercent: 100 }
|
|
||||||
);
|
|
||||||
// Expect at least the two geometry issues, in declaration order:
|
|
||||||
// outflowLevel < inflowLevel comes before overflowLevel <= basinHeight.
|
|
||||||
const idxOutflow = issues.findIndex((i) => i.aName === 'outflowLevel');
|
|
||||||
const idxOverflow = issues.findIndex((i) => i.aName === 'overflowLevel' && i.bName === 'basinHeight');
|
|
||||||
assert.ok(idxOutflow >= 0, 'expected outflowLevel issue');
|
|
||||||
assert.ok(idxOverflow >= 0, 'expected overflowLevel <= basinHeight issue');
|
|
||||||
assert.ok(idxOutflow < idxOverflow, 'issues should be in check-declaration order');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('accepts a plain basin object (duck-typed via getters)', () => {
|
|
||||||
const plainBasin = {
|
|
||||||
volEmptyBasin: 50,
|
|
||||||
heightBasin: 5,
|
|
||||||
inflowLevel: 3,
|
|
||||||
outflowLevel: 0.2,
|
|
||||||
overflowLevel: 4.5,
|
|
||||||
surfaceArea: 10,
|
|
||||||
maxVol: 50,
|
|
||||||
maxVolAtOverflow: 45,
|
|
||||||
minVolAtInflow: 30,
|
|
||||||
minVolAtOutflow: 2,
|
|
||||||
minVol: 2,
|
|
||||||
minHeightBasedOn: 'outlet',
|
|
||||||
};
|
|
||||||
const issues = validateThresholdOrdering(
|
|
||||||
plainBasin,
|
|
||||||
{ minLevel: 1, startLevel: 2, maxLevel: 4 },
|
|
||||||
{ dryRunThresholdPercent: 10, overfillThresholdPercent: 95 }
|
|
||||||
);
|
|
||||||
assert.deepEqual(issues, []);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('omitted levelbased / safety objects are tolerated', () => {
|
|
||||||
const { basin } = validBasinAndCfg();
|
|
||||||
// No control or safety supplied → only geometry checks run; valid basin geometry → []
|
|
||||||
const issues = validateThresholdOrdering(basin, undefined, undefined);
|
|
||||||
assert.deepEqual(issues, []);
|
|
||||||
});
|
|
||||||
94
test/integration/basic-dashboard-flow.test.js
Normal file
94
test/integration/basic-dashboard-flow.test.js
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
const fs = require('node:fs');
|
||||||
|
const path = require('node:path');
|
||||||
|
|
||||||
|
function loadDashboardFlow() {
|
||||||
|
const flowPath = path.join(__dirname, '../../examples/basic-dashboard.flow.json');
|
||||||
|
return JSON.parse(fs.readFileSync(flowPath, 'utf8'));
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeContextStub() {
|
||||||
|
const store = {};
|
||||||
|
return {
|
||||||
|
get(key) {
|
||||||
|
return store[key];
|
||||||
|
},
|
||||||
|
set(key, value) {
|
||||||
|
store[key] = value;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
test('basic dashboard flow contains the pumpingStation node and trend widgets', () => {
|
||||||
|
const flow = loadDashboardFlow();
|
||||||
|
const ps = flow.find((n) => n.id === 'ps_node_basic');
|
||||||
|
const parser = flow.find((n) => n.id === 'ps_parse_output');
|
||||||
|
const levelChart = flow.find((n) => n.id === 'ps_chart_level');
|
||||||
|
const demandChart = flow.find((n) => n.id === 'ps_chart_demand');
|
||||||
|
|
||||||
|
assert.ok(ps, 'ps_node_basic should exist');
|
||||||
|
assert.equal(ps.type, 'pumpingStation');
|
||||||
|
assert.equal(ps.controlMode, 'levelbased');
|
||||||
|
assert.equal(ps.levelCurveType, 'linear');
|
||||||
|
assert.equal(ps.inletPipeDiameter, 0.4);
|
||||||
|
assert.equal(ps.outletPipeDiameter, 0.3);
|
||||||
|
assert.ok(parser, 'ps_parse_output should exist');
|
||||||
|
assert.equal(parser.outputs, 6);
|
||||||
|
assert.equal(levelChart.type, 'ui-chart');
|
||||||
|
assert.equal(demandChart.type, 'ui-chart');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('basic dashboard parser routes process fields to charts and state text', () => {
|
||||||
|
const flow = loadDashboardFlow();
|
||||||
|
const parser = flow.find((n) => n.id === 'ps_parse_output');
|
||||||
|
assert.ok(parser, 'ps_parse_output should exist');
|
||||||
|
|
||||||
|
const func = new Function('msg', 'context', 'node', parser.func);
|
||||||
|
const context = makeContextStub();
|
||||||
|
const node = { send() {} };
|
||||||
|
|
||||||
|
// Flatten format is `${type}.${variant}.${position}.${childId}`. When the
|
||||||
|
// runtime writes without an explicit .child(), childId='default'. Mirror
|
||||||
|
// the real shape here. (See generalFunctions/src/measurements/
|
||||||
|
// MeasurementContainer.js getFlattenedOutput.)
|
||||||
|
const out = func({
|
||||||
|
payload: {
|
||||||
|
'level.predicted.atequipment.default': 3.25,
|
||||||
|
'volume.predicted.atequipment.default': 32.5,
|
||||||
|
'netFlowRate.predicted.atequipment.default': 0.003,
|
||||||
|
percControl: 25,
|
||||||
|
direction: 'filling',
|
||||||
|
safetyState: 'normal',
|
||||||
|
isOverflowing: false,
|
||||||
|
timeleft: 400,
|
||||||
|
},
|
||||||
|
}, context, node);
|
||||||
|
|
||||||
|
assert.ok(Array.isArray(out));
|
||||||
|
assert.equal(out.length, 6);
|
||||||
|
assert.equal(out[0].topic, 'level');
|
||||||
|
assert.equal(out[0].payload, 3.25);
|
||||||
|
assert.equal(out[1].topic, 'volume');
|
||||||
|
assert.equal(out[1].payload, 32.5);
|
||||||
|
assert.equal(out[2].topic, 'demand');
|
||||||
|
assert.equal(out[2].payload, 25);
|
||||||
|
assert.equal(out[3].topic, 'net_flow');
|
||||||
|
assert.equal(out[3].payload, 0.003);
|
||||||
|
assert.match(out[4].payload, /normal/);
|
||||||
|
assert.match(out[5].payload, /level=3.25 m/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('basic dashboard parser keeps previous values when process output sends only changed fields', () => {
|
||||||
|
const flow = loadDashboardFlow();
|
||||||
|
const parser = flow.find((n) => n.id === 'ps_parse_output');
|
||||||
|
const func = new Function('msg', 'context', 'node', parser.func);
|
||||||
|
const context = makeContextStub();
|
||||||
|
const node = { send() {} };
|
||||||
|
|
||||||
|
func({ payload: { 'level.predicted.atequipment.default': 3.1, percControl: 10 } }, context, node);
|
||||||
|
const out = func({ payload: { percControl: 20 } }, context, node);
|
||||||
|
|
||||||
|
assert.equal(out[0].payload, 3.1);
|
||||||
|
assert.equal(out[2].payload, 20);
|
||||||
|
});
|
||||||
198
test/integration/shifted-ramp-end-to-end.test.js
Normal file
198
test/integration/shifted-ramp-end-to-end.test.js
Normal file
@@ -0,0 +1,198 @@
|
|||||||
|
// End-to-end test for the level-armed hysteresis (shifted ramp) cycle.
|
||||||
|
// Drives a full fill→arm→drain cycle through the same code path the
|
||||||
|
// dashboard exercises (manual Q_IN / Q_OUT + tick), and asserts the
|
||||||
|
// hold-then-ramp output behaviour.
|
||||||
|
//
|
||||||
|
// Run with: node --test test/integration/shifted-ramp-end-to-end.test.js
|
||||||
|
|
||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
|
||||||
|
const PumpingStation = require('../../src/specificClass');
|
||||||
|
|
||||||
|
const SURFACE_AREA = 10; // basin volume / height = 50/5
|
||||||
|
const TICK_MS = 1000; // simulate 1 s per tick
|
||||||
|
|
||||||
|
function makeConfig() {
|
||||||
|
return {
|
||||||
|
general: {
|
||||||
|
name: 'TestPS',
|
||||||
|
id: 'ps-e2e',
|
||||||
|
unit: 'm3/h',
|
||||||
|
logging: { enabled: false, logLevel: 'error' },
|
||||||
|
flowThreshold: 1e-4,
|
||||||
|
},
|
||||||
|
functionality: {
|
||||||
|
softwareType: 'pumpingStation',
|
||||||
|
role: 'stationcontroller',
|
||||||
|
positionVsParent: 'atEquipment',
|
||||||
|
},
|
||||||
|
basin: {
|
||||||
|
volume: 50, height: 5,
|
||||||
|
inflowLevel: 3, outflowLevel: 0.2, overflowLevel: 4.5,
|
||||||
|
inletPipeDiameter: 0.4, outletPipeDiameter: 0.3,
|
||||||
|
},
|
||||||
|
hydraulics: { refHeight: 'NAP', basinBottomRef: 0, minHeightBasedOn: 'outlet' },
|
||||||
|
control: {
|
||||||
|
mode: 'levelbased',
|
||||||
|
allowedModes: new Set(['levelbased', 'manual']),
|
||||||
|
levelbased: {
|
||||||
|
minLevel: 1, startLevel: 2, maxLevel: 4,
|
||||||
|
curveType: 'linear', logCurveFactor: 9,
|
||||||
|
enableShiftedRamp: true, shiftLevel: 3.5, shiftArmPercent: 80,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
safety: {
|
||||||
|
enableDryRunProtection: false, enableOverfillProtection: false,
|
||||||
|
dryRunThresholdPercent: 2, highVolumeSafetyThresholdPercent: 98,
|
||||||
|
overfillThresholdPercent: 98, timeleftToFullOrEmptyThresholdSeconds: 0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build a PS with a fake MGC that captures every demand sent to it,
|
||||||
|
// and a clock we control so _updatePredictedVolume integrates over a
|
||||||
|
// known dt regardless of wall-clock.
|
||||||
|
function buildHarness() {
|
||||||
|
const ps = new PumpingStation(makeConfig());
|
||||||
|
const demands = [];
|
||||||
|
ps.machineGroups['mgc1'] = {
|
||||||
|
config: { general: { name: 'mgc1' } },
|
||||||
|
turnOffAllMachines: () => {},
|
||||||
|
handleInput: async (_src, d) => { demands.push(d); },
|
||||||
|
};
|
||||||
|
// Seed level at startLevel so the run begins idle.
|
||||||
|
ps.calibratePredictedLevel(2.0);
|
||||||
|
// Override Date.now via a controllable clock that advances `step()`.
|
||||||
|
let now = ps._predictedFlowState.lastTimestamp || 0;
|
||||||
|
ps._fakeNow = () => now;
|
||||||
|
ps._fakeAdvance = (ms) => { now += ms; };
|
||||||
|
// Patch global Date.now JUST inside the scope of these tests.
|
||||||
|
const realNow = Date.now;
|
||||||
|
Date.now = ps._fakeNow;
|
||||||
|
// Restore on completion.
|
||||||
|
ps._restore = () => { Date.now = realNow; };
|
||||||
|
return { ps, demands };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function step(ps, qIn, qOut) {
|
||||||
|
// Apply the manual Q_IN / Q_OUT (mirroring the dashboard's q_in / q_out
|
||||||
|
// topic handlers in nodeClass.js), advance time, then tick once.
|
||||||
|
if (Number.isFinite(qIn)) ps.setManualInflow(qIn, Date.now(), 'm3/s');
|
||||||
|
if (Number.isFinite(qOut)) ps.setManualOutflow(qOut, Date.now(), 'm3/s');
|
||||||
|
ps._fakeAdvance(TICK_MS);
|
||||||
|
ps.tick();
|
||||||
|
}
|
||||||
|
|
||||||
|
function levelOf(ps) {
|
||||||
|
return ps.measurements.type('level').variant('predicted').position('atequipment').getCurrentValue('m');
|
||||||
|
}
|
||||||
|
|
||||||
|
test('shifted ramp e2e: arm → hold → ramp-down → disarm', async () => {
|
||||||
|
const { ps } = buildHarness();
|
||||||
|
try {
|
||||||
|
// ─── PHASE A: fill from start (2.0) up past the arm point ──────────
|
||||||
|
// Q_IN = 0.05 m3/s, Q_OUT = 0 → net = 0.05 m3/s. Level rises by
|
||||||
|
// 0.05/SURFACE_AREA = 0.005 m per second.
|
||||||
|
let armedAt = null;
|
||||||
|
for (let i = 0; i < 600 && levelOf(ps) < 3.95; i++) {
|
||||||
|
await step(ps, 0.05, 0);
|
||||||
|
if (!armedAt && ps._shiftArmed) armedAt = { level: levelOf(ps), pct: ps.percControl };
|
||||||
|
}
|
||||||
|
assert.ok(armedAt, 'shift should arm during fill');
|
||||||
|
// Should arm right around level=3.8 (up curve = 80 %). Allow ±0.05 m
|
||||||
|
// jitter for time-discretization.
|
||||||
|
assert.ok(Math.abs(armedAt.level - 3.8) < 0.05,
|
||||||
|
`expected arm near level=3.8, got ${armedAt.level}`);
|
||||||
|
assert.ok(armedAt.pct >= 80 - 1e-6,
|
||||||
|
`at arm point output should be ≥ shiftArmPercent, got ${armedAt.pct}`);
|
||||||
|
|
||||||
|
// While still filling and armed, output should track the up curve
|
||||||
|
// (not jump to 100 %). At level ~ 3.95, up curve = 95 %.
|
||||||
|
const fillingPct = ps.percControl;
|
||||||
|
assert.ok(fillingPct < 100 + 1e-6 && fillingPct >= 80 - 1e-6,
|
||||||
|
`filling-armed output should still be on up curve, got ${fillingPct}`);
|
||||||
|
// No hold captured yet (still filling).
|
||||||
|
assert.equal(ps._shiftHoldValue, null);
|
||||||
|
|
||||||
|
// ─── PHASE B: flip to draining ─────────────────────────────────────
|
||||||
|
// First drain tick captures the hold. We need direction='draining' as
|
||||||
|
// determined by _selectBestNetFlow → so q_in - q_out must be negative
|
||||||
|
// by more than the dead-band (1e-4).
|
||||||
|
await step(ps, 0, 0.05); // net = -0.05
|
||||||
|
assert.equal(ps.state.direction, 'draining');
|
||||||
|
// Hold captured = up curve at the level when direction flipped. The
|
||||||
|
// captured value is recorded BEFORE this drain tick lowered the level
|
||||||
|
// further, so it should match the last filling tick's output (within
|
||||||
|
// the per-tick step size 0.5 % ~ 0.005 m × 100 / 1 m).
|
||||||
|
assert.ok(ps._shiftHoldValue >= 80 - 1e-6,
|
||||||
|
`hold should be at least the arm threshold, got ${ps._shiftHoldValue}`);
|
||||||
|
const hold = ps._shiftHoldValue;
|
||||||
|
|
||||||
|
// ─── PHASE C: drain while level still ≥ shiftLevel — output HELD ───
|
||||||
|
// Drain until level just above shiftLevel=3.5. Output stays = hold.
|
||||||
|
let held = true;
|
||||||
|
for (let i = 0; i < 200 && levelOf(ps) > 3.51; i++) {
|
||||||
|
await step(ps, 0, 0.05);
|
||||||
|
if (Math.abs(ps.percControl - hold) > 1e-6) { held = false; break; }
|
||||||
|
}
|
||||||
|
assert.ok(held, 'output should HOLD at the captured value while level > shiftLevel');
|
||||||
|
assert.ok(Math.abs(ps.percControl - hold) < 1e-6,
|
||||||
|
`still expected hold=${hold}, got ${ps.percControl}`);
|
||||||
|
|
||||||
|
// ─── PHASE D: drain past shiftLevel — output ramps hold→0 ──────────
|
||||||
|
// Drain until clearly below shiftLevel (level ≤ 3.45). Output should drop.
|
||||||
|
while (levelOf(ps) > 3.45) await step(ps, 0, 0.05);
|
||||||
|
const justBelow = ps.percControl;
|
||||||
|
assert.ok(justBelow < hold,
|
||||||
|
`output should start dropping below shiftLevel, got ${justBelow} vs hold ${hold}`);
|
||||||
|
// Ramp midpoint: level=2.75 (midway in [2, 3.5]). Output ≈ hold × 0.5.
|
||||||
|
while (levelOf(ps) > 2.78 && levelOf(ps) > 2.0) await step(ps, 0, 0.05);
|
||||||
|
const mid = ps.percControl;
|
||||||
|
assert.ok(Math.abs(mid - hold * 0.5) < hold * 0.05,
|
||||||
|
`at level≈2.75 expected ≈ hold/2 (${hold * 0.5}), got ${mid}`);
|
||||||
|
|
||||||
|
// ─── PHASE E: level drops to startLevel — DISARM, output 0 ─────────
|
||||||
|
while (levelOf(ps) > 1.95) await step(ps, 0, 0.05);
|
||||||
|
assert.equal(ps._shiftArmed, false, 'should disarm when level reaches startLevel');
|
||||||
|
assert.equal(ps._shiftHoldValue, null);
|
||||||
|
assert.equal(ps.percControl, 0);
|
||||||
|
} finally {
|
||||||
|
ps._restore();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('shifted ramp e2e: bounce — fill, drain a bit, refill, drain — captures fresh hold', async () => {
|
||||||
|
const { ps } = buildHarness();
|
||||||
|
try {
|
||||||
|
// Fill to arm + some headroom.
|
||||||
|
while (levelOf(ps) < 3.85) await step(ps, 0.05, 0);
|
||||||
|
assert.equal(ps._shiftArmed, true);
|
||||||
|
|
||||||
|
// First drain transition → hold #1.
|
||||||
|
await step(ps, 0, 0.05);
|
||||||
|
const hold1 = ps._shiftHoldValue;
|
||||||
|
assert.ok(hold1 >= 80 - 1e-6);
|
||||||
|
|
||||||
|
// Drain a tiny bit (level still > shiftLevel) → output stays at hold1.
|
||||||
|
for (let i = 0; i < 5; i++) await step(ps, 0, 0.05);
|
||||||
|
assert.ok(Math.abs(ps.percControl - hold1) < 1e-6);
|
||||||
|
|
||||||
|
// Flip back to filling at higher rate; up curve resumes; hold cleared.
|
||||||
|
await step(ps, 0.05, 0);
|
||||||
|
assert.equal(ps._shiftHoldValue, null);
|
||||||
|
assert.equal(ps._shiftArmed, true, 'should stay armed across the bounce');
|
||||||
|
|
||||||
|
// Fill higher than before (output goes higher).
|
||||||
|
while (levelOf(ps) < 3.95) await step(ps, 0.05, 0);
|
||||||
|
const fillingPct = ps.percControl;
|
||||||
|
assert.ok(fillingPct > hold1, `bounce should rise above first hold; got ${fillingPct} vs ${hold1}`);
|
||||||
|
|
||||||
|
// Drain again → fresh hold #2 = current up curve %.
|
||||||
|
await step(ps, 0, 0.05);
|
||||||
|
const hold2 = ps._shiftHoldValue;
|
||||||
|
assert.ok(hold2 > hold1, `second hold (${hold2}) should be > first (${hold1})`);
|
||||||
|
} finally {
|
||||||
|
ps._restore();
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -51,9 +51,10 @@ Use a descriptive `alt` text; it's the fallback if the SVG fails and it shows up
|
|||||||
| Diagram | Shows |
|
| Diagram | Shows |
|
||||||
|---|---|
|
|---|---|
|
||||||
| `basin-model` | Shared physical basin cross-section — walls, pipe reference heights, derived safety zones, storage/dead volumes |
|
| `basin-model` | Shared physical basin cross-section — walls, pipe reference heights, derived safety zones, storage/dead volumes |
|
||||||
| `modes/basin-mode-level-linear` | Level-linear control mode — `startLevel`, demand ramp, threshold-shift behaviour |
|
| `modes/level-based/basin-mode-level-linear` | Level-based linear control curve — rising ramp starts at inlet level, falling ramp shifts to `startLevel` |
|
||||||
|
| `modes/level-based/basin-mode-level-log` | Level-based logarithmic control curve — fast early response, falling ramp shifts to `startLevel` |
|
||||||
| `control-zones` | Legacy vertical level axis ("thermometer") for `levelbased` mode — STOP / DEAD ZONE / RUN with demand ramp |
|
| `control-zones` | Legacy vertical level axis ("thermometer") for `levelbased` mode — STOP / DEAD ZONE / RUN with demand ramp |
|
||||||
| `safety-rules` | Dry-run vs overfill rule asymmetry — which children stop, which keep running |
|
| `safety-rules` | Dry-run vs high-volume safety rule asymmetry — which children stop, which keep running |
|
||||||
|
|
||||||
## Making a brand-new diagram
|
## Making a brand-new diagram
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
|
Before Width: | Height: | Size: 271 KiB |
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 319 KiB |
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 256 KiB |
@@ -79,7 +79,7 @@ The current runtime still uses the level fields directly for its volume math. Pi
|
|||||||
| **Time To Empty/Full (s)** | `0` | If > 0, triggers safety when predicted time-to-overflow or time-to-empty falls below this value. `0` disables time-based protection. |
|
| **Time To Empty/Full (s)** | `0` | If > 0, triggers safety when predicted time-to-overflow or time-to-empty falls below this value. `0` disables time-based protection. |
|
||||||
| **Enable Dry-Run Protection** | `true` | If on, pumps are shut down once volume drops below the dry-run threshold while draining. |
|
| **Enable Dry-Run Protection** | `true` | If on, pumps are shut down once volume drops below the dry-run threshold while draining. |
|
||||||
| **Low Volume Threshold (%)** | `2` | Safety margin above the configured minimum volume: `dryRunSafetyVol = minVol × (1 + pct/100)`. This creates `dryRunLevel`; it is derived, not a separately entered basin height. |
|
| **Low Volume Threshold (%)** | `2` | Safety margin above the configured minimum volume: `dryRunSafetyVol = minVol × (1 + pct/100)`. This creates `dryRunLevel`; it is derived, not a separately entered basin height. |
|
||||||
| **Enable Overfill Protection** | `true` | If on, upstream inflows are shut down once volume climbs above the high-volume safety point while filling. |
|
| **Enable High-volume Safety** | `true` | If on, upstream inflows are shut down once volume climbs above the high-volume safety point while filling. |
|
||||||
| **High Volume Threshold (%)** | `98` | Safety margin below physical overflow: `highVolumeSafetyVol = maxVolAtOverflow × pct/100`. Actual overflowing is still the boolean condition `level >= overflowLevel`. |
|
| **High Volume Threshold (%)** | `98` | Safety margin below physical overflow: `highVolumeSafetyVol = maxVolAtOverflow × pct/100`. Actual overflowing is still the boolean condition `level >= overflowLevel`. |
|
||||||
|
|
||||||
### Output formats
|
### Output formats
|
||||||
@@ -152,12 +152,18 @@ Delta-compressed payload (only changed fields per tick). Keys follow the standar
|
|||||||
| `volumePercent.predicted.atequipment.default` | `(vol - minVol) / (maxVolAtOverflow - minVol) × 100` (%). |
|
| `volumePercent.predicted.atequipment.default` | `(vol - minVol) / (maxVolAtOverflow - minVol) × 100` (%). |
|
||||||
| `flow.predicted.in.<childId>` | Inflow contribution from a registered child (m³/s internally; editor unit on output). |
|
| `flow.predicted.in.<childId>` | Inflow contribution from a registered child (m³/s internally; editor unit on output). |
|
||||||
| `flow.predicted.out.<childId>` | Outflow contribution from a registered child. |
|
| `flow.predicted.out.<childId>` | Outflow contribution from a registered child. |
|
||||||
|
| `flow.predicted.overflow.default` | Synthetic spill rate over the weir while predicted volume is pinned at `maxVolAtOverflow` (m³/s). Zero when not spilling. Lives at its own position (not under `out`) so the operational outflow sum stays clean; `_selectBestNetFlow` folds it into the outflow side for net-flow balance, where it reads ~0 while pinned. |
|
||||||
| `flow.measured.<position>.<childId>` | Flow sensor reading. |
|
| `flow.measured.<position>.<childId>` | Flow sensor reading. |
|
||||||
| `netFlowRate.<variant>.atequipment.default` | Net flow used for control (inflow − outflow). |
|
| `netFlowRate.<variant>.atequipment.default` | Net flow used for control (inflow − outflow). |
|
||||||
|
| `overflowVolume.predicted.atequipment.default` | Cumulative predicted spill volume (m³) — for compliance reporting via InfluxDB. Monotonically non-decreasing. |
|
||||||
|
| `underflowVolume.predicted.atequipment.default` | Cumulative volume the integrator tried to drive below 0 m³ (m³). Diagnostic only, NOT compliance — a non-zero value indicates a flow-balance error (over-reported outflow / missing inflow source / pump curve too optimistic). |
|
||||||
| `direction` | `filling` / `draining` / `steady` / `unknown`. |
|
| `direction` | `filling` / `draining` / `steady` / `unknown`. |
|
||||||
| `flowSource` | Which variant drove the current control cycle (`measured`, `predicted`, `level:predicted`, `null`). |
|
| `flowSource` | Which variant drove the current control cycle (`measured`, `predicted`, `level:predicted`, `null`). |
|
||||||
| `timeleft` | Predicted seconds to overflow (while filling) or to dry-run (while draining). |
|
| `timeleft` | Predicted seconds to overflow (while filling) or to dry-run (while draining). |
|
||||||
| `volEmptyBasin`, `inflowLevel`, `overflowLevel`, `maxVol`, `maxVolAtOverflow`, `minVol`, `minVolAtInflow`, `minVolAtOutflow`, `minHeightBasedOn` | Echoes of the basin geometry for dashboards. |
|
| `volEmptyBasin`, `inflowLevel`, `overflowLevel`, `maxVol`, `maxVolAtOverflow`, `minVol`, `minVolAtInflow`, `minVolAtOutflow`, `minHeightBasedOn` | Echoes of the basin geometry for dashboards. |
|
||||||
|
| `predictedOverflowVolume` | Convenience top-level mirror of `overflowVolume.predicted.atequipment.default` (m³). |
|
||||||
|
| `predictedOverflowRate` | Convenience top-level mirror of `flow.predicted.overflow.default` (m³/s). |
|
||||||
|
| `predictedUnderflowVolume` | Convenience top-level mirror of `underflowVolume.predicted.atequipment.default` (m³). |
|
||||||
| `percControl` | Last demand (0–100+ %) forwarded to the machine group during level-based control. |
|
| `percControl` | Last demand (0–100+ %) forwarded to the machine group during level-based control. |
|
||||||
|
|
||||||
Consumers must cache and merge deltas — the example dashboard flows include a reusable function node that does exactly this.
|
Consumers must cache and merge deltas — the example dashboard flows include a reusable function node that does exactly this.
|
||||||
@@ -178,9 +184,9 @@ The basin is modelled as a rectangular prism with constant cross-section. Everyt
|
|||||||
|
|
||||||
*Editable source: [`diagrams/basin-model.drawio.svg`](diagrams/basin-model.drawio.svg) (drag into draw.io; the SVG embeds the editable source). See [`diagrams/README.md`](diagrams/README.md) for the edit-and-export workflow.*
|
*Editable source: [`diagrams/basin-model.drawio.svg`](diagrams/basin-model.drawio.svg) (drag into draw.io; the SVG embeds the editable source). See [`diagrams/README.md`](diagrams/README.md) for the edit-and-export workflow.*
|
||||||
|
|
||||||
**Generic basin ordering** (bottom → top): `outflowLevel ≤ dryRunLevel ≤ minLevel < inflowLevel < maxLevel ≤ highVolumeSafetyLevel < overflowLevel ≤ basinHeight`.
|
**Generic basin ordering** (bottom → top): `outflowLevel ≤ dryRunLevel < inflowLevel ≤ highVolumeSafetyLevel < overflowLevel ≤ basinHeight`.
|
||||||
|
|
||||||
`startLevel` is deliberately not part of this generic basin diagram. It belongs to a control mode. For the current level-linear mode, see [`diagrams/modes/basin-mode-level-linear.drawio.svg`](diagrams/modes/basin-mode-level-linear.drawio.svg).
|
`minLevel`, `startLevel`, and `maxLevel` are deliberately not part of this generic basin diagram. They belong to a control mode. For the current level-based mode variants, see [`diagrams/modes/level-based/`](diagrams/modes/level-based/).
|
||||||
|
|
||||||
The pipe labels are intentional:
|
The pipe labels are intentional:
|
||||||
|
|
||||||
@@ -215,6 +221,23 @@ The high-volume safety point exists so the station can still react before the ba
|
|||||||
|
|
||||||
The rectangular approximation is acceptable for this node's first basin model because operational level is always in metres from the basin floor, while calculated m³ can tolerate small shape errors. A later upgrade can replace `volume = level × surfaceArea` with a level-volume curve for benching, sumps, sediment/dead zones, and irregular wet-well geometry.
|
The rectangular approximation is acceptable for this node's first basin model because operational level is always in metres from the basin floor, while calculated m³ can tolerate small shape errors. A later upgrade can replace `volume = level × surfaceArea` with a level-volume curve for benching, sumps, sediment/dead zones, and irregular wet-well geometry.
|
||||||
|
|
||||||
|
### Predicted-volume bounds
|
||||||
|
|
||||||
|
The predicted-volume integrator is clamped between two physical limits. **Measured** values are never clamped — only a real sensor can show level outside this range (e.g. inflow exceeds pump+weir capacity and the basin pressurises against the ceiling).
|
||||||
|
|
||||||
|
**Upper bound — `maxVolAtOverflow`.** Once the integrator would push past the weir crest, the predicted level pins at `overflowLevel`. The excess is recorded two ways every tick it spills:
|
||||||
|
|
||||||
|
- **Cumulative `overflowVolume.predicted.atequipment.default`** — running total of spill in m³, for compliance reporting via InfluxDB.
|
||||||
|
- **Synthetic `flow.predicted.out.overflow`** — instantaneous spill rate (m³/s) equal to `inflow − real_outflow`. Registered as a predicted outflow contribution so `_selectBestNetFlow` sees a balanced ledger and reports `netFlowRate ≈ 0` while pinned. The integrator subtracts this synthetic flow before integrating so the spill never feeds back into the volume math.
|
||||||
|
|
||||||
|
The `isOverflowing` flag (true when `level >= overflowLevel`) is what tells operators why net flow reads zero even though water is still moving through the basin.
|
||||||
|
|
||||||
|
**Lower bound — `dryRunSafetyVol`.** The integrator can't drain below the dry-run threshold because pumps physically can't pump that low (the safety controller would shut them off, and even with safety disabled the suction loses prime). The clamp only fires on the transition — if the basin starts (or is calibrated) below `dryRunSafetyVol` it's left alone; inflow is what brings it back up.
|
||||||
|
|
||||||
|
### Level-rate fallback during overflow
|
||||||
|
|
||||||
|
When the chosen flow source is `level:measured` or `level:predicted` (priorities 3–4 in the ladder below), `dL/dt × surfaceArea` *is* the net flow. While level is pinned at `overflowLevel`, `dL/dt = 0` collapses the signal even though water is still moving. In that case `_selectBestNetFlow` holds the last known non-zero net flow until level starts dropping again — so dashboards keep a usable "this is roughly what's coming in" reading. The held value is refreshed any tick the level rate is meaningful, so it auto-updates once the basin un-pins.
|
||||||
|
|
||||||
## Net-flow selection
|
## Net-flow selection
|
||||||
|
|
||||||
Every tick, `_selectBestNetFlow()` walks a priority ladder and returns the first net flow that clears the dead-band (`|flow| ≥ flowThreshold`):
|
Every tick, `_selectBestNetFlow()` walks a priority ladder and returns the first net flow that clears the dead-band (`|flow| ≥ flowThreshold`):
|
||||||
@@ -245,7 +268,7 @@ flowPositions = { inflow: ['in', 'upstream'], outflow: ['out', 'downstream'] }
|
|||||||
|
|
||||||
The `pumpingStation` supports multiple control modes. Each mode is a **policy that maps basin state to demand (0-100 %)**. `levelbased` uses `minLevel`, `startLevel`, and `maxLevel`; other modes may use different thresholds or compute them dynamically.
|
The `pumpingStation` supports multiple control modes. Each mode is a **policy that maps basin state to demand (0-100 %)**. `levelbased` uses `minLevel`, `startLevel`, and `maxLevel`; other modes may use different thresholds or compute them dynamically.
|
||||||
|
|
||||||
The basin model owns the shared physical and safety references: pipe elevations, `dryRunLevel`, `highVolumeSafetyLevel`, and `overflowLevel`. `startLevel` is mode-specific and is documented with the mode diagrams, not the generic basin drawing.
|
The basin model owns the shared physical and safety references: pipe elevations, `dryRunLevel`, `highVolumeSafetyLevel`, and `overflowLevel`. `minLevel`, `startLevel`, and `maxLevel` are mode-specific and are documented with the mode diagrams, not the generic basin drawing.
|
||||||
|
|
||||||
Every mode gets its own page under [`modes/`](modes/README.md) with a consistent layout (inputs, threshold policy, demand formula, edge cases) so they can be compared side-by-side. Currently:
|
Every mode gets its own page under [`modes/`](modes/README.md) with a consistent layout (inputs, threshold policy, demand formula, edge cases) so they can be compared side-by-side. Currently:
|
||||||
|
|
||||||
@@ -261,7 +284,7 @@ See [`modes/README.md`](modes/README.md) for the index and page template.
|
|||||||
|
|
||||||
`_safetyController` runs **before** `_controlLogic` every tick. Two rules, deliberately asymmetric — *dry-run protects the pumps from running themselves into air*, *high-volume protection tries to preserve distance to actual overflow*.
|
`_safetyController` runs **before** `_controlLogic` every tick. Two rules, deliberately asymmetric — *dry-run protects the pumps from running themselves into air*, *high-volume protection tries to preserve distance to actual overflow*.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
During high-volume or overflow conditions, level-based control naturally commands >=100 % on the downstream MGC because the level is above `maxLevel`.
|
During high-volume or overflow conditions, level-based control naturally commands >=100 % on the downstream MGC because the level is above `maxLevel`.
|
||||||
|
|
||||||
@@ -319,8 +342,10 @@ The canonical end-to-end demo lives in the EVOLV superproject at [`examples/pump
|
|||||||
| "No volume data available to safe guard system; shutting down all machines." in logs | No measured level, predicted volume not calibrated, and no inflow/outflow samples yet. | Issue `calibratePredictedVolume` (or `calibratePredictedLevel`) once at startup, or wire a level sensor. |
|
| "No volume data available to safe guard system; shutting down all machines." in logs | No measured level, predicted volume not calibrated, and no inflow/outflow samples yet. | Issue `calibratePredictedVolume` (or `calibratePredictedLevel`) once at startup, or wire a level sensor. |
|
||||||
| `flowSource: null` and `direction: 'steady'` forever | Every flow / level signal falls inside the dead-band (default `1e-4 m³/s`). | Confirm flows are non-zero, or lower `config.general.flowThreshold` for a small-scale demo. |
|
| `flowSource: null` and `direction: 'steady'` forever | Every flow / level signal falls inside the dead-band (default `1e-4 m³/s`). | Confirm flows are non-zero, or lower `config.general.flowThreshold` for a small-scale demo. |
|
||||||
| `Qd` ignored | Station is not in `manual` mode. | Send `{ topic: 'changemode', payload: 'manual' }` first, or fall back to level-based control. |
|
| `Qd` ignored | Station is not in `manual` mode. | Send `{ topic: 'changemode', payload: 'manual' }` first, or fall back to level-based control. |
|
||||||
| Pumps keep running during overfill | Intended — overfill safety only stops **upstream** equipment; downstream pumps must drain. | To override, switch to `manual` and set `Qd = 0`, or issue an emergency-stop at the MGC. |
|
| Pumps keep running during high-volume safety | Intended — high-volume safety only stops **upstream** equipment; downstream pumps must drain. | To override, switch to `manual` and set `Qd = 0`, or issue an emergency-stop at the MGC. |
|
||||||
| Predicted volume drifts away from measured | Flow integrator has no reference — flows might have the wrong sign, or `unit` is mis-declared. | Call `calibratePredictedVolume` periodically from a measured level. |
|
| Predicted volume drifts away from measured | Flow integrator has no reference — flows might have the wrong sign, or `unit` is mis-declared. | Call `calibratePredictedVolume` periodically from a measured level. |
|
||||||
|
| Predicted level pinned at `overflowLevel` and `netFlowRate` reads ~0 | Intended while spilling — the synthetic `flow.predicted.out.overflow` balances the ledger so net is 0. Watch `isOverflowing`, `predictedOverflowRate`, and the cumulative `predictedOverflowVolume` instead. | Lower inflow (or raise pump capacity / `maxLevel`) to clear the overflow condition; level un-pins automatically. |
|
||||||
|
| Measured level above `overflowLevel` | Real-world ceiling-pressure case — inflow is exceeding pump *and* weir capacity. | This is the only path to "above overflow" in the model; predicted is clamped. Trust the sensor; treat as an alarmable event. |
|
||||||
|
|
||||||
## Running it locally
|
## Running it locally
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ updated: 2026-04-22
|
|||||||
|
|
||||||
# Level-based mode
|
# Level-based mode
|
||||||
|
|
||||||
The simplest and most widely deployed control strategy. Demand is a direct, *static* piecewise-linear function of basin level — no feedback loop, no predictions beyond the level measurement itself. This page uses the [shared basin model](../functional-description.md#basin-model); see [`modes/README.md`](README.md) for the template other mode pages follow.
|
The simplest and most widely deployed control strategy. Demand is a direct, static function of basin level — no feedback loop, no predictions beyond the level measurement itself. This page uses the [shared basin model](../functional-description.md#basin-model); see [`modes/README.md`](README.md) for the template other mode pages follow.
|
||||||
|
|
||||||
## At a glance
|
## At a glance
|
||||||
|
|
||||||
@@ -20,9 +20,9 @@ The simplest and most widely deployed control strategy. Demand is a direct, *sta
|
|||||||
|
|
||||||
## Diagram
|
## Diagram
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
*Editable source: [`../diagrams/modes/basin-mode-level-linear.drawio.svg`](../diagrams/modes/basin-mode-level-linear.drawio.svg) (drag into [draw.io](https://app.diagrams.net/) — it round-trips).*
|
*Editable sources: [`../diagrams/modes/level-based/basin-mode-level-linear.drawio.svg`](../diagrams/modes/level-based/basin-mode-level-linear.drawio.svg) and [`../diagrams/modes/level-based/basin-mode-level-log.drawio.svg`](../diagrams/modes/level-based/basin-mode-level-log.drawio.svg) (drag into [draw.io](https://app.diagrams.net/) — they round-trip).*
|
||||||
|
|
||||||
## Inputs
|
## Inputs
|
||||||
|
|
||||||
@@ -30,10 +30,11 @@ The simplest and most widely deployed control strategy. Demand is a direct, *sta
|
|||||||
|---|---|---|
|
|---|---|---|
|
||||||
| current level | `measurement` child (`measured`) → predicted from volume integrator (fallback) | X-axis of the transfer function |
|
| current level | `measurement` child (`measured`) → predicted from volume integrator (fallback) | X-axis of the transfer function |
|
||||||
| `config.control.levelbased.minLevel` | editor, static | below → pumps hard OFF |
|
| `config.control.levelbased.minLevel` | editor, static | below → pumps hard OFF |
|
||||||
| `config.control.levelbased.startLevel` | editor, static | where demand-ramp starts |
|
| `config.control.levelbased.startLevel` | editor, static | falling ramp reaches 0 % here; rising demand holds 0 % until the inlet level |
|
||||||
| `config.control.levelbased.maxLevel` | editor, static | where demand saturates at 100 % |
|
| `config.control.levelbased.maxLevel` | editor, static | where demand saturates at 100 % |
|
||||||
|
| `config.control.levelbased.curveType` | editor, static | `linear` or `log`; log is fast early response |
|
||||||
|
|
||||||
The three control thresholds are the **only** mode-specific configuration. Nothing here is recomputed at runtime.
|
The three control thresholds plus curve type are the mode-specific configuration. Nothing here is recomputed at runtime.
|
||||||
|
|
||||||
## Threshold policy
|
## Threshold policy
|
||||||
|
|
||||||
@@ -42,6 +43,7 @@ The three control thresholds are the **only** mode-specific configuration. Nothi
|
|||||||
| `minLevel` | `config.control.levelbased.minLevel` | No |
|
| `minLevel` | `config.control.levelbased.minLevel` | No |
|
||||||
| `startLevel` | `config.control.levelbased.startLevel` | No |
|
| `startLevel` | `config.control.levelbased.startLevel` | No |
|
||||||
| `maxLevel` | `config.control.levelbased.maxLevel` | No |
|
| `maxLevel` | `config.control.levelbased.maxLevel` | No |
|
||||||
|
| `curveType` | `config.control.levelbased.curveType` | No |
|
||||||
|
|
||||||
That this policy is trivial (all static) is **the defining simplicity of this mode**. Modes like `powerBased` or future `weather-aware` variants will recompute these thresholds on the fly.
|
That this policy is trivial (all static) is **the defining simplicity of this mode**. Modes like `powerBased` or future `weather-aware` variants will recompute these thresholds on the fly.
|
||||||
|
|
||||||
@@ -51,15 +53,15 @@ That this policy is trivial (all static) is **the defining simplicity of this mo
|
|||||||
if level < minLevel:
|
if level < minLevel:
|
||||||
demand = 0
|
demand = 0
|
||||||
MGC → turnOffAllMachines() # explicit shutdown, not just "0 %"
|
MGC → turnOffAllMachines() # explicit shutdown, not just "0 %"
|
||||||
elif level < startLevel:
|
elif direction == filling:
|
||||||
demand = <previous demand> # dead zone — hold last command (hysteresis)
|
demand = curve(level, [inflowLevel, maxLevel], [0 %, 100 %])
|
||||||
elif level <= maxLevel:
|
elif direction == draining:
|
||||||
demand = lerp(level, [startLevel, maxLevel], [0 %, 100 %])
|
demand = curve(level, [startLevel, maxLevel], [0 %, 100 %])
|
||||||
else:
|
else:
|
||||||
demand = 100 % # saturated; MGC clamps internally if overshoot
|
demand = previous demand
|
||||||
```
|
```
|
||||||
|
|
||||||
Where `lerp` is linear interpolation. The MGC is free to distribute the demand across its pumps however its own policy dictates (equal split, lead-lag, staging — that's the MGC's business).
|
Below the active lower ramp point, demand is 0 %. Above `maxLevel`, demand is 100 %. `curve` is either linear or logarithmic; the log variant rises faster early in the ramp. The MGC is free to distribute the demand across its pumps however its own policy dictates (equal split, lead-lag, staging — that's the MGC's business).
|
||||||
|
|
||||||
## Edge cases
|
## Edge cases
|
||||||
|
|
||||||
|
|||||||
@@ -67,11 +67,11 @@ demandCap = min(100, 100 × maxPowerKW(t) / nominalStationPower)
|
|||||||
demand = min(rawDemand, demandCap)
|
demand = min(rawDemand, demandCap)
|
||||||
```
|
```
|
||||||
|
|
||||||
When `demandCap < rawDemand`, the mode sacrifices drainage rate to stay within power budget. Level may rise — the overfill safety layer still applies as the last line of defence.
|
When `demandCap < rawDemand`, the mode sacrifices drainage rate to stay within power budget. Level may rise — the high-volume safety layer still applies as the last line of defence before physical overflow.
|
||||||
|
|
||||||
## Edge cases
|
## Edge cases
|
||||||
|
|
||||||
- **Peak hour with rising level.** demandCap drops faster than level rises → demand gets clipped; level approaches `overflowLevel`. If overfill safety trips, it overrides the clip (safety wins).
|
- **Peak hour with rising level.** demandCap drops faster than level rises → demand gets clipped; level approaches `overflowLevel`. If high-volume safety trips, it overrides the clip (safety wins).
|
||||||
- **Power signal dropout.** Fall back to static `maxPowerKW` from config; log warning.
|
- **Power signal dropout.** Fall back to static `maxPowerKW` from config; log warning.
|
||||||
- **Grid exit from peak while basin is nearly full.** demandCap jumps back to 100; PID is memoryless so demand rises in one tick to match rawDemand.
|
- **Grid exit from peak while basin is nearly full.** demandCap jumps back to 100; PID is memoryless so demand rises in one tick to match rawDemand.
|
||||||
- **Measured vs predicted pump power.** Cap is enforced on predicted (decisions are made before the pump responds). Reconcile against measured for logging/diagnostics.
|
- **Measured vs predicted pump power.** Cap is enforced on predicted (decisions are made before the pump responds). Reconcile against measured for logging/diagnostics.
|
||||||
|
|||||||
Reference in New Issue
Block a user