Compare commits

..

8 Commits

Author SHA1 Message Date
lzm
d7f6613892 refactor(measurement): modularize editor JS
Move inline <script> from measurement.html into 8 modules under
src/editor/. measurement.js adds the static-file routes that serve them
to Node-RED.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 09:10:05 +02:00
znetsixe
36eaa2f859 test(edge): align invalid-payload test with object-payload accept behaviour
The runtime handler accepts both bare numbers and {value} object payloads
(matches the contract's units: {measure, default} pre-dispatch shape).
The edge test was still asserting the old "object payloads are ignored"
behaviour; update it to the current contract.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 15:29:46 +02:00
znetsixe
5d79314229 feat(units) + style: command unit-handling, frost dbase option, palette #D4A02E
measurement.html:
  • sidebar swatch → #D4A02E (amber, sensor family) — EVOLV palette redesign
    2026-05-21 (see superproject .claude/rules/node-red-flow-layout.md §10.0).
  • Add "frost" option to dbaseOutputFormat dropdown (CoreSync FROST handoff).

src/commands/handlers.js + test/basic/commands-units.basic.test.js:
  • Unit handling for data.measurement command. Analog + digital modes both
    accept scalar / object / per-channel-map payloads; supplied units are
    converted into the channel's configured (dropdown) unit.

CONTRACT.md: document the unit semantics.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 15:06:37 +02:00
znetsixe
b0e8bbb95d docs(wiki): regenerate topic-contract AUTOGEN block via wiki-gen
Replaces the agent-written placeholder inside Reference-Contracts.md with
the authoritative table generated from src/commands/index.js. Both the
BEGIN and END markers are normalized to the canonical form used by
`@evolv/wiki-gen`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-19 10:11:48 +02:00
znetsixe
1a16f9c4f1 docs(wiki): full 5-page wiki matching the rotatingMachine reference format
Replaces the prior stub/partial wiki with a Home + Reference-{Architecture,
Contracts,Examples,Limitations} + _Sidebar structure. Topic-contract and
data-model sections wrapped in AUTOGEN markers for the future wiki-gen tool.
Source-vs-spec contradictions surfaced and flagged inline (not silently
fixed). Pending-review notes mark sections that need a full node review.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-19 09:42:10 +02:00
znetsixe
b884c0f085 docs: add Folder & File Layout section per EVOLV convention
Each repo can now be read standalone for the file-naming convention. Full rule:
.claude/rules/node-architecture.md in the EVOLV superproject.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-18 21:30:23 +02:00
znetsixe
ffc03584ed wiki: rewrite Home.md per visual-first 14-section template
- Run npm run wiki:all (wiki:contract + wiki:datamodel both wrote cleanly)
- Remove section 10 (State chart) — measurement is stateless, no FSM
- Renumber sections 11→10, 12→11, 13→12, 14→13 for correct 13-section layout
- Update banner git hash from afc304b to 125f964

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 21:03:23 +02:00
znetsixe
125f964d31 P11.6 wiki regen + Phase 10 private-test rewrites where applicable
For all 11 nodes with auto-gen markers: wiki/Home.md sections 5 (topic
contract) and 9 (data model) regenerated via npm run wiki:all. New
Unit column shows '<measure> (default <unit>)' for declared topics,
'—' otherwise. Effect column now uses descriptor.description (P11.2
field) overriding the generic per-prefix fallback.

For rotatingMachine + reactor: Phase 10 test rewrites — 3 + 8 files
moved off private nodeClass internals (_attachInputHandler, _commands,
_pendingExtras, _registerChild, _tick, etc.) to the public
BaseNodeAdapter surface (node.handlers.input, node.source.*).
+6 / +7 net new tests.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 19:44:04 +02:00
21 changed files with 2952 additions and 504 deletions

View File

@@ -21,3 +21,20 @@ Key points for this node:
- Stack same-level siblings vertically.
- Parent/children sit on adjacent lanes (children one lane left, parent one lane right).
- Wrap in a Node-RED group box coloured `#a9daee` (Control Module).
## Folder & File Layout
Every per-node file MUST use the folder name (`measurement`) **exactly**, case-sensitive. Full rule: [`.claude/rules/node-architecture.md`](https://gitea.wbd-rd.nl/RnD/EVOLV/src/branch/development/.claude/rules/node-architecture.md) in the EVOLV superproject.
| Path | Required name |
|---|---|
| Entry file | `measurement.js` |
| Editor HTML | `measurement.html` |
| Node adapter | `src/nodeClass.js` |
| Domain logic | `src/specificClass.js` |
| Editor JS modules | `src/editor/*.js` (extract when inline editor JS exceeds ~50 lines) |
| Tests | `test/{basic,integration,edge}/*.test.js` |
| Example flows | `examples/*.flow.json` |
When adding new files, read the rule above first to avoid drift.

View File

@@ -10,10 +10,29 @@ Hand-maintained for Phase 3; the `## Inputs` table is generated from
| `set.simulator` | `simulator` | none (payload ignored) | Toggles `source.toggleSimulation()` — flips `config.simulation.enabled`. |
| `set.outlier-detection` | `outlierDetection` | none (payload ignored) | Toggles `source.toggleOutlierDetection()` — flips `config.outlierDetection.enabled`. |
| `cmd.calibrate` | `calibrate` | none | Calls `source.calibrate()` — captures the current input as the zero/reference offset. |
| `data.measurement` | `measurement` | mode-dependent — see below | Pushes a sensor reading into the pipeline. Analog: numeric scalar (number or numeric string)`source.inputValue`. Digital: object payload keyed by channel name`source.handleDigitalPayload(payload)`. Wrong shape for the configured mode logs a helpful warning suggesting the other mode. |
| `data.measurement` | `measurement` | mode-dependent — see **Payload shape** below | Pushes a sensor reading into the pipeline. Analog → `source.inputValue`; digital → `source.handleDigitalPayload(<flat map>)`. Wrong shape for the configured mode logs a helpful warning suggesting the other mode. |
Aliases log a one-time deprecation warning the first time they fire.
### `data.measurement` payload shape
Both modes accept the same three forms, mirroring pumpingStation's
`set.inflow` contract:
- **Bare scalar** — `msg.payload = 12.5` (number or numeric string). The unit
falls back to `msg.unit`, and finally to the channel's configured unit
(the dropdown selection in the node editor).
- **Rich object** — `msg.payload = { value, unit?, timestamp? }`. Used per-
call to declare the unit of a single sample.
- **Digital map** (digital mode only) — `msg.payload = { <channelKey>: <bare scalar | rich object>, … }`. Each entry follows the rules above independently, so different channels in one message may carry different units.
When a supplied unit differs from the channel's configured unit, the value
is converted into the channel unit via `generalFunctions.convert` before it
enters the outlier / scaling / smoothing pipeline. If the supplied unit is
unknown or belongs to a different measure (e.g. `kg` on a `pressure`
channel), the handler logs a warning and uses the raw value treated as the
channel unit — the sample is not silently dropped.
## Outputs (msg.topic on Port 0/1/2)
- **Port 0 (process):** `msg.topic = config.general.name`. Payload built by

View File

@@ -8,13 +8,23 @@
| **Control Module** | `#a9daee` | zwart |
-->
<script src="/measurement/menu.js"></script> <!-- Load the menu script for dynamic dropdowns -->
<script src="/measurement/configData.js"></script> <!-- Load the config script for node information -->
<script src="/measurement/menu.js"></script> <!-- Load the menu script for dynamic dropdowns -->
<script src="/measurement/configData.js"></script> <!-- Load the config script for node information -->
<!-- Editor JS modules — see nodes/measurement/src/editor/. Loaded in
dependency order: index.js (namespace + helpers) → visuals → handlers. -->
<script src="/measurement/editor/index.js"></script>
<script src="/measurement/editor/hover-couple.js"></script>
<script src="/measurement/editor/pipeline-diagram.js"></script>
<script src="/measurement/editor/scaling-chart.js"></script>
<script src="/measurement/editor/smoothing-sparkline.js"></script>
<script src="/measurement/editor/digital-channels.js"></script>
<script src="/measurement/editor/oneditprepare.js"></script>
<script src="/measurement/editor/oneditsave.js"></script>
<script>
RED.nodes.registerType("measurement", {
category: "EVOLV",
color: "#a9daee", // color for the node based on the S88 schema
color: "#D4A02E",
defaults: {
// Define default properties
@@ -71,186 +81,9 @@
const modeTag = this.mode === 'digital' ? ' [digital]' : '';
return (this.positionIcon || "") + " " + (this.assetType || "Measurement") + modeTag;
},
oneditprepare: function() {
const node = this;
// === Asset / logger / position placeholders (dynamic menus) ===
// Kick these off FIRST so that any error in the downstream mode
// logic can never block the shared menus. Historical regression:
// a ReferenceError in the mode block aborted oneditprepare and
// stopped the asset menu from rendering at all.
const waitForMenuData = () => {
if (window.EVOLV?.nodes?.measurement?.initEditor) {
window.EVOLV.nodes.measurement.initEditor(node);
} else {
setTimeout(waitForMenuData, 50);
}
};
waitForMenuData();
// IMPORTANT: all DOM references are resolved up front so helper
// functions called during initial applyMode() don't trip over the
// Temporal Dead Zone on later `const` declarations.
const modeSelect = document.getElementById('node-input-mode');
const analogBlock = document.getElementById('analog-only-fields');
const digitalBlock = document.getElementById('digital-only-fields');
const modeHint = document.getElementById('mode-hint');
const channelsArea = document.getElementById('node-input-channels');
const channelsHint = document.getElementById('channels-validation');
// Initialise the mode <select> from the saved node.mode. Legacy
// nodes (saved before the mode field existed) fall back to
// 'analog' so they keep behaving exactly like before.
const initialMode = (node.mode === 'digital' || node.mode === 'analog') ? node.mode : 'analog';
if (modeSelect) modeSelect.value = initialMode;
// Populate the channels textarea from the saved node.channels
// (stored as a raw JSON string; parsing happens server-side).
if (channelsArea && typeof node.channels === 'string') {
channelsArea.value = node.channels;
}
function validateChannelsJson() {
if (!channelsHint) return;
if (!modeSelect || modeSelect.value !== 'digital') {
channelsHint.textContent = '';
return;
}
const raw = (channelsArea && channelsArea.value || '').trim();
if (!raw || raw === '[]') {
channelsHint.innerHTML = '<span style="color:#b45309;">Digital mode with no channels — no measurements will be emitted.</span>';
return;
}
try {
const parsed = JSON.parse(raw);
if (!Array.isArray(parsed)) throw new Error('must be an array');
const missing = parsed
.map((c, i) => (c && c.key && c.type ? null : 'entry ' + i + ': missing key or type'))
.filter(Boolean);
if (missing.length) {
channelsHint.innerHTML = '<span style="color:#b45309;">' + missing.join('; ') + '</span>';
} else {
channelsHint.innerHTML = '<span style="color:#047857;">' + parsed.length + ' channel(s) defined: ' + parsed.map((c) => c.key).join(', ') + '</span>';
}
} catch (e) {
channelsHint.innerHTML = '<span style="color:#b91c1c;">Invalid JSON: ' + e.message + '</span>';
}
}
function applyMode(mode) {
const isDigital = mode === 'digital';
if (analogBlock) analogBlock.style.display = isDigital ? 'none' : 'block';
if (digitalBlock) digitalBlock.style.display = isDigital ? 'block' : 'none';
if (modeHint) {
modeHint.textContent = isDigital
? 'msg.payload must be an OBJECT, e.g. {"temperature": 22.5, "humidity": 45}. Define each key below.'
: 'msg.payload must be a NUMBER (or numeric string). Configure scaling/smoothing below.';
}
validateChannelsJson();
}
if (modeSelect) modeSelect.addEventListener('change', (e) => applyMode(e.target.value));
if (channelsArea) channelsArea.addEventListener('input', validateChannelsJson);
try { applyMode(initialMode); } catch (e) {
console.error('measurement: applyMode failed', e);
}
// === Smoothing method dropdown (analog only) ===
const smoothMethodSelect = document.getElementById('node-input-smooth_method');
const options = window.EVOLV?.nodes?.measurement?.config?.smoothing?.smoothMethod?.rules?.values || [];
smoothMethodSelect.innerHTML = '';
const emptyOption = document.createElement('option');
emptyOption.value = '';
emptyOption.textContent = 'Select method...';
smoothMethodSelect.appendChild(emptyOption);
options.forEach(option => {
const optionElement = document.createElement('option');
optionElement.value = option.value;
optionElement.textContent = option.value;
optionElement.title = option.description;
smoothMethodSelect.appendChild(optionElement);
});
if (node.smooth_method) smoothMethodSelect.value = node.smooth_method;
// === Scale rows toggle (analog only) ===
const chk = document.getElementById('node-input-scaling');
const rowMin = document.getElementById('row-input-i_min');
const rowMax = document.getElementById('row-input-i_max');
function toggleScalingRows() {
const show = chk.checked;
rowMin.style.display = show ? 'block' : 'none';
rowMax.style.display = show ? 'block' : 'none';
}
chk.addEventListener('change', toggleScalingRows);
toggleScalingRows();
//------------------- END OF CUSTOM config UI ELEMENTS ------------------- //
},
oneditsave: function () {
const node = this;
// Validate asset properties using the asset menu
if (window.EVOLV?.nodes?.measurement?.assetMenu?.saveEditor) {
success = window.EVOLV.nodes.measurement.assetMenu.saveEditor(this);
}
// Validate logger properties using the logger menu
if (window.EVOLV?.nodes?.measurement?.loggerMenu?.saveEditor) {
success = window.EVOLV.nodes.measurement.loggerMenu.saveEditor(node);
}
// save position field
if (window.EVOLV?.nodes?.measurement?.positionMenu?.saveEditor) {
window.EVOLV.nodes.measurement.positionMenu.saveEditor(this);
}
// Mode is the top-level switch. Always save it first; its value
// drives which other fields are meaningful.
node.mode = document.getElementById('node-input-mode').value || 'analog';
// Channels JSON (digital). We store the raw string and let the
// server-side nodeClass.js parse it so we can surface parse errors
// at deploy time instead of silently dropping bad config.
node.channels = document.getElementById('node-input-channels').value || '[]';
// Analog smoothing method.
node.smooth_method = document.getElementById('node-input-smooth_method').value || '';
// Save checkbox properties (always safe to read regardless of mode;
// these elements exist in the DOM even when their section is hidden).
["scaling", "simulator"].forEach(
(field) => (node[field] = document.getElementById(`node-input-${field}`).checked)
);
["i_min", "i_max", "i_offset", "o_min", "o_max", "count"].forEach(
(field) => (node[field] = parseFloat(document.getElementById(`node-input-${field}`).value) || 0)
);
// Calibration stability threshold: 0 is a valid (very strict) value, so
// fall back to the default 0.01 only when the field is empty / NaN.
const stRaw = document.getElementById('node-input-stabilityThreshold').value;
const stParsed = parseFloat(stRaw);
node.stabilityThreshold = Number.isFinite(stParsed) ? stParsed : 0.01;
// Mode-dependent validation. In digital mode we don't care about
// scaling completeness (the channels have their own per-channel
// scaling); in analog mode we still warn about half-filled ranges.
if (node.mode === 'analog' && node.scaling && (isNaN(node.i_min) || isNaN(node.i_max))) {
RED.notify("Scaling enabled, but input range is incomplete!", "error");
}
if (node.mode === 'digital') {
try {
const parsed = JSON.parse(node.channels || '[]');
if (!Array.isArray(parsed) || parsed.length === 0) {
RED.notify("Digital mode: no channels defined. The node will emit nothing.", "warning");
}
} catch (e) {
RED.notify("Digital mode: Channels JSON is invalid (" + e.message + ")", "error");
}
}
},
oneditprepare: function () { window.MeasEditor.oneditprepare.call(this); },
oneditsave: function () { window.MeasEditor.oneditsave.call(this); },
});
</script>
@@ -258,121 +91,515 @@
<script type="text/html" data-template-name="measurement">
<!-- Input mode -->
<div class="form-row">
<label for="node-input-mode"><i class="fa fa-exchange"></i> Input Mode</label>
<select id="node-input-mode" style="width:60%;">
<option value="analog">analog one scalar per msg.payload (classic PLC)</option>
<option value="digital">digital object payload with many channel keys (MQTT/IoT)</option>
</select>
<style>
/* === Section headers ============================================== */
.meas-section { margin-top: 8px; }
.meas-section h4 { margin: 14px 0 6px 0; }
.meas-help {
font-size: 12px; color: #777; margin: 0 0 8px 0;
}
/* === Mode cards =================================================== */
.meas-mode-cards { display: flex; gap: 10px; margin: 6px 0 8px 0; }
.meas-mode-card {
flex: 1; cursor: pointer;
border: 2px solid #ccc; border-radius: 6px;
padding: 10px 12px; background: #fff;
transition: border-color 80ms, background 80ms;
}
.meas-mode-card:hover { border-color: #888; background: #fafafa; }
.meas-mode-card.meas-mode-active {
border-color: #0c99d9; background: #f0f8ff;
}
.meas-mode-card .meas-mode-title {
font-weight: 600; font-size: 13px; color: #222;
}
.meas-mode-card .meas-mode-sub {
font-size: 11px; color: #666; margin-top: 4px;
}
.meas-mode-card .meas-mode-payload {
font-family: monospace; font-size: 11px; color: #1F4E79;
margin-top: 4px; background: #f4f8fc; padding: 2px 6px;
border-radius: 3px; display: inline-block;
}
/* === Pipeline diagram ============================================= */
.meas-pipeline-svg {
display: block; width: 100%; max-width: 720px;
background: #fff; border: 1px solid #e5e5e5; border-radius: 4px;
}
.meas-stage rect {
transition: opacity 80ms, stroke-width 80ms;
}
.meas-stage-disabled rect { opacity: 0.35; }
.meas-stage-disabled text { opacity: 0.5; }
.meas-stage-highlight rect {
stroke-width: 3 !important; stroke: #0c99d9 !important;
}
/* === Two-column diag layout (used by scaling chart) =============== */
.meas-diag { display: flex; gap: 24px; align-items: flex-start; margin: 0 0 10px 0; flex-wrap: wrap; }
.meas-diag-side { width: 250px; flex: 0 0 250px; display: flex; flex-direction: column; gap: 5px; }
.meas-diag-side .meas-row {
display: grid; grid-template-columns: minmax(0, 1fr) 80px 16px; align-items: center;
gap: 6px; padding: 4px 6px 4px 10px; border-left: 4px solid #ccc;
background: #fafafa; border-radius: 3px; font-size: 11px;
min-width: 0;
}
.meas-diag-side .meas-row:hover { background: #f0f0f0; }
.meas-diag-side .meas-row label { font-weight: 600; margin: 0; line-height: 1.2; }
.meas-diag-side .meas-row .meas-sub {
grid-column: 1; font-size: 10px; color: #888; font-weight: 400;
}
.meas-diag-side .meas-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;
}
.meas-diag-side .meas-row input[type=number]:focus {
outline: 1px solid #0c99d9; border-color: #0c99d9;
}
.meas-diag-side .meas-row .meas-unit { color: #888; font-size: 10px; }
.meas-diag-svg-wrap { flex: 1; min-width: 240px; }
/* Border colour per stage so the side-row matches its SVG stage. */
.meas-row[data-stroke="#1F4E79"] { border-left-color: #1F4E79; }
.meas-row[data-stroke="#1E8449"] { border-left-color: #1E8449; }
.meas-row[data-stroke="#D68910"] { border-left-color: #D68910; }
.meas-row[data-stroke="#7D3C98"] { border-left-color: #7D3C98; }
.meas-row[data-stroke="#C0392B"] { border-left-color: #C0392B; }
/* === Digital channel cards ======================================= */
.meas-ch-empty {
font-size: 12px; color: #888; font-style: italic;
padding: 10px 12px; background: #fafafa; border: 1px dashed #ddd;
border-radius: 4px;
}
.meas-ch-card {
border: 1px solid #ddd; border-radius: 4px;
background: #fff; margin-bottom: 6px;
}
.meas-ch-head {
display: grid;
grid-template-columns: 36px minmax(0, 1fr) 110px 110px minmax(0, 1fr) 70px 28px;
gap: 6px; align-items: center;
padding: 6px 8px;
}
.meas-ch-num-badge {
font-size: 10px; color: #888; font-family: monospace;
text-align: center;
}
.meas-ch-input {
height: 24px; box-sizing: border-box; font-size: 12px;
padding: 2px 5px; margin: 0; border: 1px solid #ccc; border-radius: 3px;
background: #fff; min-width: 0;
}
.meas-ch-input:focus { outline: 1px solid #0c99d9; border-color: #0c99d9; }
.meas-ch-input.meas-ch-err { border-color: #C0392B; background: #fdecea; }
.meas-ch-num { width: 100%; }
/* Unit cell wraps either a <select> (canonical type) or a free-text
<input> (custom type). Type-change swaps the wrapper's contents
without rerendering the rest of the card. Make the inner element
fill the grid cell. */
.meas-ch-unit-cell { min-width: 0; }
.meas-ch-unit-cell > * { width: 100%; }
.meas-ch-btn {
height: 24px; box-sizing: border-box;
padding: 0 8px; border: 1px solid #ccc; border-radius: 3px;
background: #f5f5f5; cursor: pointer; font-size: 11px;
}
.meas-ch-btn:hover { background: #ececec; }
.meas-ch-btn-del {
width: 28px; padding: 0; color: #C0392B; font-weight: bold;
}
.meas-ch-btn-del:hover { background: #fdecea; }
.meas-ch-adv {
display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px;
padding: 8px 10px 10px 44px; border-top: 1px solid #eee;
background: #fafbfd;
}
.meas-ch-sub {
background: #fff; border: 1px solid #eee; border-radius: 3px;
padding: 6px 8px;
}
.meas-ch-sub-title {
font-size: 11px; font-weight: 600; color: #444; margin-bottom: 4px;
}
.meas-ch-sub-grid {
display: grid; grid-template-columns: auto 1fr; gap: 4px 6px;
align-items: center;
}
.meas-ch-sub-grid label { font-size: 10px; color: #666; margin: 0; }
.meas-ch-sub-grid.meas-ch-dim { opacity: 0.4; pointer-events: none; }
.meas-ch-cb {
font-size: 11px; font-weight: 600; color: #444;
display: inline-flex; align-items: center; gap: 4px; margin: 0;
}
.meas-ch-actions {
display: flex; gap: 8px; align-items: center; margin: 8px 0;
}
.meas-ch-actions .meas-ch-btn-add {
background: #1E8449; color: #fff; border-color: #186b3a;
}
.meas-ch-actions .meas-ch-btn-add:hover { background: #186b3a; }
</style>
<!-- ================================================================ -->
<!-- INPUT MODE -->
<!-- ================================================================ -->
<div class="meas-section">
<h4>Input mode</h4>
<p class="meas-help">Pick how this node should interpret <code>msg.payload</code>. Click a card to switch the dropdown stays in sync.</p>
<div class="meas-mode-cards">
<div class="meas-mode-card" data-mode="analog">
<div class="meas-mode-title"><i class="fa fa-tachometer"></i> Analog</div>
<div class="meas-mode-sub">One scalar per message (classic PLC / 420 mA).</div>
<div class="meas-mode-payload">msg.payload = 22.5</div>
</div>
<div class="meas-mode-card" data-mode="digital">
<div class="meas-mode-title"><i class="fa fa-sitemap"></i> Digital</div>
<div class="meas-mode-sub">Object payload, many channels per message (MQTT / IoT).</div>
<div class="meas-mode-payload">msg.payload = {"temperature": 22.5, "humidity": 45}</div>
</div>
</div>
<div class="form-row">
<label for="node-input-mode"><i class="fa fa-exchange"></i> Mode</label>
<select id="node-input-mode" style="width:60%;">
<option value="analog">analog one scalar per msg.payload (classic PLC)</option>
<option value="digital">digital object payload with many channel keys (MQTT/IoT)</option>
</select>
</div>
<div class="form-row" id="mode-hint" style="margin-left:105px; font-size:12px; color:#666;"></div>
</div>
<!-- ================================================================ -->
<!-- ANALOG PIPELINE DIAGRAM (top of the analog block) -->
<!-- ================================================================ -->
<div id="meas-pipeline-wrap" class="meas-section">
<h4>Signal pipeline</h4>
<p class="meas-help">
Each incoming value flows through these stages. Stages dim when they're
switched off below. Hover an input row (offset / scale / smoothing) to
highlight the matching stage.
</p>
<!--
============================================================
PIPELINE FLOW SVG
============================================================
viewBox 720 x 140. Six stages, equal width, horizontal arrows.
Stage stroke + sub-label are updated by pipelineDiagram.redraw().
Hover-couple targets the <g class="meas-stage" id="meas-stage-*"> group.
============================================================
-->
<svg class="meas-pipeline-svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 720 140"
font-family="Arial,sans-serif" font-size="11">
<defs>
<marker id="meas-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="#555" />
</marker>
</defs>
<!-- Six stage boxes at x = 8, 128, 248, 368, 488, 608 (width=104, gap=16) -->
<g class="meas-stage" id="meas-stage-input">
<rect x="8" y="35" width="104" height="70" rx="6" fill="#eef6fb" stroke="#1F4E79" stroke-width="1.5" />
<text x="60" y="60" text-anchor="middle" fill="#1F4E79" font-weight="bold">msg.payload</text>
<text x="60" y="78" text-anchor="middle" fill="#555" font-size="10">number</text>
<text x="60" y="94" text-anchor="middle" fill="#888" font-size="9">topic: measurement</text>
</g>
<g class="meas-stage" id="meas-stage-offset">
<rect x="128" y="35" width="104" height="70" rx="6" fill="#fdf4e7" stroke="#D68910" stroke-width="1.5" />
<text x="180" y="60" text-anchor="middle" fill="#D68910" font-weight="bold">+ offset</text>
<text id="meas-stage-offset-sub" x="180" y="78" text-anchor="middle" fill="#555" font-size="10">off</text>
<text x="180" y="94" text-anchor="middle" fill="#888" font-size="9">additive bias</text>
</g>
<g class="meas-stage" id="meas-stage-scale">
<rect x="248" y="35" width="104" height="70" rx="6" fill="#eafaf1" stroke="#1E8449" stroke-width="1.5" />
<text x="300" y="60" text-anchor="middle" fill="#1E8449" font-weight="bold">scale</text>
<text id="meas-stage-scale-sub" x="300" y="78" text-anchor="middle" fill="#555" font-size="10">off</text>
<text x="300" y="94" text-anchor="middle" fill="#888" font-size="9">[in]→[out]</text>
</g>
<g class="meas-stage" id="meas-stage-smooth">
<rect x="368" y="35" width="104" height="70" rx="6" fill="#eef2fb" stroke="#1F4E79" stroke-width="1.5" />
<text x="420" y="60" text-anchor="middle" fill="#1F4E79" font-weight="bold">smooth</text>
<text id="meas-stage-smooth-sub" x="420" y="78" text-anchor="middle" fill="#555" font-size="10">off</text>
<text x="420" y="94" text-anchor="middle" fill="#888" font-size="9">rolling window</text>
</g>
<g class="meas-stage" id="meas-stage-outlier">
<rect x="488" y="35" width="104" height="70" rx="6" fill="#fdecea" stroke="#C0392B" stroke-width="1.5" />
<text x="540" y="60" text-anchor="middle" fill="#C0392B" font-weight="bold">outlier</text>
<text x="540" y="78" text-anchor="middle" fill="#555" font-size="10">runtime toggle</text>
<text x="540" y="94" text-anchor="middle" fill="#888" font-size="9">topic: outlierDetection</text>
</g>
<g class="meas-stage" id="meas-stage-output">
<rect x="608" y="35" width="104" height="70" rx="6" fill="#f4f4f4" stroke="#333" stroke-width="1.5" />
<text x="660" y="60" text-anchor="middle" fill="#333" font-weight="bold">output</text>
<text id="meas-stage-output-sub" x="660" y="78" text-anchor="middle" fill="#555" font-size="10">process / influxdb</text>
<text x="660" y="94" text-anchor="middle" fill="#888" font-size="9">port 0 / port 1</text>
</g>
<!-- Arrows between stages -->
<line x1="112" y1="70" x2="128" y2="70" stroke="#555" marker-end="url(#meas-arrow)" />
<line x1="232" y1="70" x2="248" y2="70" stroke="#555" marker-end="url(#meas-arrow)" />
<line x1="352" y1="70" x2="368" y2="70" stroke="#555" marker-end="url(#meas-arrow)" />
<line x1="472" y1="70" x2="488" y2="70" stroke="#555" marker-end="url(#meas-arrow)" />
<line x1="592" y1="70" x2="608" y2="70" stroke="#555" marker-end="url(#meas-arrow)" />
<!-- Top caption -->
<text x="360" y="20" text-anchor="middle" fill="#444" font-size="11" font-style="italic">
analog signal pipeline (digital mode runs one pipeline per channel)
</text>
<!-- Hover hint -->
<text x="360" y="128" text-anchor="middle" fill="#888" font-size="10">
hover an input row below → its stage highlights
</text>
</svg>
</div>
<div class="form-row" id="mode-hint" style="margin-left:105px; font-size:12px; color:#666;"></div>
<!-- ===================== DIGITAL MODE FIELDS ===================== -->
<div id="digital-only-fields">
<div class="form-row" id="row-input-channels">
<label for="node-input-channels"><i class="fa fa-list"></i> Channels (JSON)</label>
<textarea id="node-input-channels" rows="6" style="width:60%; font-family:monospace;" placeholder='[{"key":"temperature","type":"temperature","position":"atEquipment","unit":"C","scaling":{"enabled":false,"inputMin":0,"inputMax":1,"absMin":-50,"absMax":150,"offset":0},"smoothing":{"smoothWindow":5,"smoothMethod":"mean"}}]'></textarea>
<div class="form-tips">One entry per payload key. Each channel has its own type / position / unit / scaling / smoothing / outlier detection. See README for the full schema.</div>
<div id="digital-only-fields" class="meas-section">
<h4>Digital channels</h4>
<p class="meas-help">
Define one entry per key in <code>msg.payload</code>. Each channel has its
own type, position, unit, and optional scaling / smoothing / outlier
detection (click <b>▾ more</b> to reveal). The analog settings further
down are ignored in digital mode.
</p>
<!-- Row editor — rendered by src/editor/digital-channels.js. The raw
textarea below is kept in sync on every edit (it remains the source
of truth on the node). -->
<div id="meas-channels-rows"></div>
<div class="meas-ch-actions">
<button type="button" id="meas-channels-add" class="meas-ch-btn meas-ch-btn-add">
+ Add channel
</button>
<button type="button" id="meas-channels-raw-toggle" class="meas-ch-btn">
▾ Show raw JSON
</button>
</div>
<!-- Raw JSON escape-hatch. Hidden by default; toggle button reveals it
for power-users that want to paste / bulk-edit. Validation below
(channels-validation) fires on every textarea input event. -->
<div id="meas-channels-raw" style="display:none;">
<div class="form-row" id="row-input-channels">
<label for="node-input-channels"><i class="fa fa-code"></i> Channels (JSON)</label>
<textarea id="node-input-channels" rows="8" style="width:60%; font-family:monospace;"
placeholder='[{"key":"temperature","type":"temperature","position":"atEquipment","unit":"C","scaling":{"enabled":false,"inputMin":0,"inputMax":1,"absMin":-50,"absMax":150,"offset":0},"smoothing":{"smoothWindow":5,"smoothMethod":"mean"}}]'></textarea>
<div class="form-tips">The row editor above mirrors edits into this field — usually you won't need to touch it directly.</div>
</div>
</div>
<div class="form-row" id="channels-validation" style="margin-left:105px; font-size:12px;"></div>
</div>
<!-- ===================== ANALOG MODE FIELDS ===================== -->
<div id="analog-only-fields">
<hr>
<!-- Scaling Checkbox -->
<div class="form-row">
<label for="node-input-scaling"
><i class="fa fa-compress"></i> Scaling</label>
<input type="checkbox" id="node-input-scaling" style="width:20px; vertical-align:baseline;"/>
<span>Enable input scaling?</span>
<!-- ============================================================ -->
<!-- SCALING -->
<!-- ============================================================ -->
<div class="meas-section">
<h4>Scaling</h4>
<p class="meas-help">
Map the raw input range (e.g. 420 mA, 03000 counts) to a physical
process range (e.g. 010 bar). Apply an offset first to zero-correct
the sensor.
</p>
<div class="form-row">
<label for="node-input-scaling"><i class="fa fa-compress"></i> Scaling</label>
<input type="checkbox" id="node-input-scaling" style="width:20px; vertical-align:baseline;" />
<span>Enable linear input scaling</span>
</div>
<div class="form-row">
<label for="node-input-i_offset"><i class="fa fa-adjust"></i> Input Offset</label>
<input type="number" id="node-input-i_offset" placeholder="0" />
<div class="form-tips">Applied before scaling (additive bias).</div>
</div>
<div class="meas-diag" id="meas-scaling-wrap">
<div class="meas-diag-side" id="meas-scaling-inputs">
<div class="meas-row" data-stroke="#1F4E79" data-couples-line="meas-scale-input-axis">
<div><label>Source Min</label><div class="meas-sub">raw input low</div></div>
<input type="number" id="node-input-i_min" placeholder="0" />
<span class="meas-unit">raw</span>
</div>
<div class="meas-row" data-stroke="#1F4E79" data-couples-line="meas-scale-input-axis">
<div><label>Source Max</label><div class="meas-sub">raw input high</div></div>
<input type="number" id="node-input-i_max" placeholder="3000" />
<span class="meas-unit">raw</span>
</div>
<div class="meas-row" data-stroke="#1E8449" data-couples-line="meas-scale-output-axis">
<div><label>Process Min</label><div class="meas-sub">scaled output low</div></div>
<input type="number" id="node-input-o_min" placeholder="0" />
<span class="meas-unit">eng</span>
</div>
<div class="meas-row" data-stroke="#1E8449" data-couples-line="meas-scale-output-axis">
<div><label>Process Max</label><div class="meas-sub">scaled output high</div></div>
<input type="number" id="node-input-o_max" placeholder="1" />
<span class="meas-unit">eng</span>
</div>
</div>
<!--
============================================================
SCALING LINEAR-TRANSFORM CHART
============================================================
viewBox 300 x 180. Axes at left=44, right=286, top=14, bot=156.
Line endpoints are placed by scalingChart.redraw().
============================================================
-->
<div class="meas-diag-svg-wrap">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 180"
style="display:block;width:100%;max-width:320px;background:#fff;border:1px solid #e5e5e5;border-radius:4px;"
font-family="Arial,sans-serif" font-size="10">
<!-- Plot frame -->
<rect x="44" y="14" width="242" height="142" fill="#fafcff" stroke="#e5e5e5" />
<!-- Axes -->
<line id="meas-scale-input-axis" x1="44" y1="156" x2="286" y2="156" stroke="#1F4E79" stroke-width="1.5" />
<line id="meas-scale-output-axis" x1="44" y1="156" x2="44" y2="14" stroke="#1E8449" stroke-width="1.5" />
<!-- Tick labels -->
<text id="meas-scale-x-min" x="44" y="170" text-anchor="middle" fill="#1F4E79">0</text>
<text id="meas-scale-x-max" x="286" y="170" text-anchor="middle" fill="#1F4E79">1</text>
<text id="meas-scale-y-min" x="40" y="159" text-anchor="end" fill="#1E8449">0</text>
<text id="meas-scale-y-max" x="40" y="17" text-anchor="end" fill="#1E8449">1</text>
<!-- Axis titles -->
<text x="165" y="178" text-anchor="middle" fill="#1F4E79" font-style="italic">raw input (Source Min Source Max)</text>
<text x="14" y="85" text-anchor="middle" fill="#1E8449" font-style="italic" transform="rotate(-90 14 85)">process value (Process Min Process Max)</text>
<!-- The transform line -->
<polyline id="meas-scale-line" fill="none" stroke="#0c99d9" stroke-width="2.5" points="44,156 286,14" />
<!-- Offset readout -->
<text id="meas-scale-offset-label" x="165" y="10" text-anchor="middle" fill="#D68910" font-size="10">offset: 0 (no shift)</text>
</svg>
</div>
</div>
</div>
<!-- Source Min/Max (only if scaling is true) -->
<div class="form-row" id="row-input-i_min">
<label for="node-input-i_min"><i class="fa fa-arrow-down"></i> Source Min</label>
<input type="number" id="node-input-i_min" placeholder="0" />
<!-- ============================================================ -->
<!-- SMOOTHING -->
<!-- ============================================================ -->
<div class="meas-section">
<h4>Smoothing</h4>
<p class="meas-help">
Reduce noise on the scaled signal. Each method behaves differently
the preview below shows the result on a fixed noisy test signal.
</p>
<div class="meas-diag">
<div class="meas-diag-side">
<div class="meas-row" data-stroke="#1F4E79" data-couples-line="meas-stage-smooth">
<div><label>Method</label><div class="meas-sub">none / mean / median / kalman / </div></div>
<select id="node-input-smooth_method" style="grid-column: 2 / span 2; width: 100%;"></select>
</div>
<div class="meas-row" data-stroke="#1F4E79" data-couples-line="meas-stage-smooth">
<div><label>Window</label><div class="meas-sub">sample count</div></div>
<input type="number" id="node-input-count" placeholder="10" />
<span class="meas-unit">n</span>
</div>
</div>
<!--
============================================================
SMOOTHING SPARKLINE
============================================================
viewBox 390 x 100. Plot range left=10, right=380, top=8, bot=92.
============================================================
-->
<div class="meas-diag-svg-wrap" id="meas-smooth-wrap">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 390 110"
style="display:block;width:100%;max-width:420px;background:#fff;border:1px solid #e5e5e5;border-radius:4px;"
font-family="Arial,sans-serif" font-size="10">
<!-- Plot frame -->
<rect x="10" y="8" width="370" height="84" fill="#fafcff" stroke="#e5e5e5" />
<!-- Series -->
<polyline id="meas-smooth-raw" fill="none" stroke="#aaa" stroke-width="1" points="" />
<polyline id="meas-smooth-smoothed" fill="none" stroke="#1E8449" stroke-width="1.8" points="" />
<!-- Legend -->
<line x1="18" y1="103" x2="36" y2="103" stroke="#aaa" />
<text x="40" y="106" fill="#888">raw (noisy)</text>
<line x1="120" y1="103" x2="138" y2="103" stroke="#1E8449" stroke-width="1.8" />
<text x="142" y="106" fill="#1E8449">smoothed</text>
<!-- Method/window readout -->
<text id="meas-smooth-label" x="375" y="106" text-anchor="end" fill="#555" font-style="italic">no smoothing raw value passed through</text>
</svg>
</div>
</div>
</div>
<div class="form-row" id="row-input-i_max">
<label for="node-input-i_max"><i class="fa fa-arrow-up"></i> Source Max</label>
<input type="number" id="node-input-i_max" placeholder="3000" />
<!-- ============================================================ -->
<!-- SIMULATION -->
<!-- ============================================================ -->
<div class="meas-section">
<h4>Simulation</h4>
<div class="form-row">
<label for="node-input-simulator"><i class="fa fa-cog"></i> Simulator</label>
<input type="checkbox" id="node-input-simulator" style="width:20px; vertical-align:baseline;" />
<span>Replace the real input with an internal random-walk source (toggle at runtime via topic <code>simulator</code>).</span>
</div>
</div>
<!-- Offset -->
<div class="form-row">
<label for="node-input-i_offset"><i class="fa fa-adjust"></i> Input Offset</label>
<input type="number" id="node-input-i_offset" placeholder="0" />
<!-- ============================================================ -->
<!-- CALIBRATION -->
<!-- ============================================================ -->
<div class="meas-section">
<h4>Calibration</h4>
<p class="meas-help">
The <code>calibrate</code> topic shifts the offset so the current
output matches the configured low end. It only fires when the rolling
window is "stable enough" define what that means here.
</p>
<div class="form-row">
<label for="node-input-stabilityThreshold"><i class="fa fa-balance-scale"></i> Stability</label>
<input type="number" id="node-input-stabilityThreshold" placeholder="0.01" step="any" style="width:100px;" />
<span style="margin-left:6px; color:#666;">scaling-units</span>
<div class="form-tips">Maximum rolling-window standard deviation that still counts as stable. Default 0.01.</div>
</div>
</div>
<!-- Output / Process Min/Max -->
<div class="form-row">
<label for="node-input-o_min"><i class="fa fa-tag"></i> Process Min</label>
<input type="number" id="node-input-o_min" placeholder="0" />
</div>
<div class="form-row">
<label for="node-input-o_max"><i class="fa fa-tag"></i> Process Max</label>
<input type="number" id="node-input-o_max" placeholder="1" />
</div>
</div>
<!-- Simulator Checkbox -->
<!-- ================================================================ -->
<!-- OUTPUT FORMATS -->
<!-- ================================================================ -->
<div class="meas-section">
<h4>Output formats</h4>
<p class="meas-help">
Process port (0) drives downstream control nodes; database port (1)
feeds telemetry/historian. Pick the encoding each consumer expects.
</p>
<div class="form-row">
<label for="node-input-simulator"><i class="fa fa-cog"></i> Simulator</label>
<input type="checkbox" id="node-input-simulator" style="width:20px; vertical-align:baseline;"/>
<span>Activate internal simulation?</span>
</div>
<!-- Smoothing Method -->
<div class="form-row">
<label for="node-input-smooth_method"><i class="fa fa-line-chart"></i> Smoothing</label>
<select id="node-input-smooth_method" style="width:60%;">
<label for="node-input-processOutputFormat"><i class="fa fa-random"></i> Process port</label>
<select id="node-input-processOutputFormat" style="width:60%;">
<option value="process">process</option>
<option value="json">json</option>
<option value="csv">csv</option>
</select>
</div>
<!-- Smoothing Window -->
<div class="form-row">
<label for="node-input-count">Window</label>
<input type="number" id="node-input-count" placeholder="10" style="width:60px;"/>
<div class="form-tips">Number of samples for smoothing</div>
</div>
<!-- Calibration Stability Threshold -->
<div class="form-row">
<label for="node-input-stabilityThreshold"><i class="fa fa-balance-scale"></i> Stability Threshold</label>
<input type="number" id="node-input-stabilityThreshold" placeholder="0.01" step="any" style="width:100px;"/>
<span style="margin-left:6px; color:#666;">(scaling-units)</span>
<div class="form-tips">Maximum stdDev of the rolling window for calibrate() and evaluateRepeatability() to accept the buffer as stable. Default 0.01.</div>
<label for="node-input-dbaseOutputFormat"><i class="fa fa-database"></i> Database port</label>
<select id="node-input-dbaseOutputFormat" style="width:60%;">
<option value="influxdb">influxdb</option>
<option value="frost">frost</option>
<option value="json">json</option>
<option value="csv">csv</option>
</select>
</div>
</div>
<hr>
<h3>Output Formats</h3>
<div class="form-row">
<label for="node-input-processOutputFormat"><i class="fa fa-random"></i> Process Output</label>
<select id="node-input-processOutputFormat" style="width:60%;">
<option value="process">process</option>
<option value="json">json</option>
<option value="csv">csv</option>
</select>
</div>
<div class="form-row">
<label for="node-input-dbaseOutputFormat"><i class="fa fa-database"></i> Database Output</label>
<select id="node-input-dbaseOutputFormat" style="width:60%;">
<option value="influxdb">influxdb</option>
<option value="json">json</option>
<option value="csv">csv</option>
</select>
</div>
<!-- Optional Extended Fields: supplier, cat, type, model, unit -->
<!-- Asset fields will be injected here -->
<!-- Shared asset/logger/position menus (injected by /measurement/menu.js) -->
<div id="asset-fields-placeholder"></div>
<!-- loglevel checkbox -->
<div id="logger-fields-placeholder"></div>
<!-- Position fields will be injected here -->
<div id="position-fields-placeholder"></div>
</script>

View File

@@ -1,4 +1,5 @@
const nameOfNode = 'measurement'; // 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 { MenuManager, configManager, assetApiConfig } = require('generalFunctions');
const assetUtils = require('generalFunctions/assetUtils');
@@ -38,6 +39,18 @@ module.exports = function(RED) {
}
});
// Editor JS modules — loaded by measurement.html via <script src=...> tags.
// Files live in src/editor/. Filename is restricted to a safe charset to
// prevent path-traversal. Mirror of the pumpingStation pattern.
RED.httpAdmin.get(`/${nameOfNode}/editor/:file`, (req, res) => {
const safe = String(req.params.file || '').replace(/[^a-zA-Z0-9._-]/g, '');
if (!safe.endsWith('.js')) return res.status(400).send('// invalid');
res.type('application/javascript');
res.sendFile(path.join(__dirname, 'src', 'editor', safe), (err) => {
if (err && !res.headersSent) res.status(404).send('// editor module not found');
});
});
RED.httpAdmin.post(`/${nameOfNode}/asset-reg`, async (req, res) => {
const body = req.body || {};
const assetPayload = body.asset;

View File

@@ -3,13 +3,15 @@
// Handler functions for measurement commands. Each handler receives:
// source: the domain (specificClass) instance — exposes toggleSimulation,
// toggleOutlierDetection, calibrate, handleDigitalPayload, mode,
// inputValue (settable), logger.
// inputValue (settable), analogChannel, channels (Map), logger.
// msg: the Node-RED input message.
// ctx: { node, RED, send, logger } — provided by BaseNodeAdapter.
//
// Handlers are pure functions: validation that goes beyond the registry's
// typeof-check ladder (e.g. mode-dependent dispatch for data.measurement)
// lives here.
// typeof-check ladder (e.g. mode-dependent dispatch for data.measurement,
// unit conversion into the channel's configured unit) lives here.
const { convert } = require('generalFunctions');
function _logger(source, ctx) {
return ctx?.logger || source?.logger || null;
@@ -36,39 +38,116 @@ exports.dataMeasurement = (source, msg, ctx) => {
return _handleAnalog(source, msg, log);
};
// --- shared payload helpers ------------------------------------------------
// Extract { value, unit, timestamp } from a per-call item that may be
// - a bare number / numeric string (unit falls back to msgUnit, then channel)
// - an object { value, unit?, timestamp? } (pumpingStation set.inflow shape)
// Returns null when the shape is neither.
function _extractValueAndUnit(item, msgUnit) {
if (item !== null && typeof item === 'object' && !Array.isArray(item)) {
return {
value: Number(item.value),
unit: _trimmedString(item.unit),
timestamp: item.timestamp,
};
}
if (typeof item === 'number' || (typeof item === 'string' && item.trim() !== '')) {
return {
value: Number(item),
unit: _trimmedString(msgUnit),
timestamp: undefined,
};
}
return null;
}
function _trimmedString(v) {
return typeof v === 'string' && v.trim() ? v.trim() : null;
}
// Convert `value` from `suppliedUnit` into `channelUnit`. When the supplied
// unit is missing or already matches, returns the value untouched. When the
// units are incompatible (different measures, unsupported abbr), logs a
// warning and returns the raw value treated as if it were channelUnit — the
// sender keeps responsibility for picking the right unit, but the pipeline
// does not silently drop the sample.
function _convertToChannelUnit(value, suppliedUnit, channelUnit, log, label) {
if (!suppliedUnit || !channelUnit || suppliedUnit === channelUnit) return value;
try {
return convert(value).from(suppliedUnit).to(channelUnit);
} catch (err) {
log?.warn?.(
`${label}: unit '${suppliedUnit}' is incompatible with channel unit '${channelUnit}' ` +
`(${err.message}). Using raw value as if it were ${channelUnit}.`
);
return value;
}
}
// Distinguish a "rich" analog payload ({value, unit?, timestamp?}) from an
// object that almost certainly indicates the sender meant digital mode (a
// bag of channel-name keys). Used only for the helpful switch-mode warning.
function _looksLikeRichPayload(obj) {
return obj.value !== undefined || obj.unit !== undefined || obj.timestamp !== undefined;
}
// --- mode handlers ---------------------------------------------------------
function _handleAnalog(source, msg, log) {
const p = msg.payload;
if (p !== null && typeof p === 'object' && !Array.isArray(p) && !_looksLikeRichPayload(p)) {
const keys = Object.keys(p).slice(0, 3).join(', ');
log?.warn?.(
`analog mode received an object payload (keys: ${keys}). ` +
`Switch Input Mode to 'digital' in the editor and define channels, or feed a numeric payload.`
);
return;
}
const extracted = _extractValueAndUnit(p, msg?.unit);
if (!extracted || !Number.isFinite(extracted.value)) {
log?.warn?.(`Invalid analog measurement payload: ${JSON.stringify(p)}`);
return;
}
const channelUnit = source.analogChannel?.unit || null;
source.inputValue = _convertToChannelUnit(
extracted.value,
extracted.unit,
channelUnit,
log,
'data.measurement',
);
}
function _handleDigital(source, msg, log) {
const p = msg.payload;
if (p && typeof p === 'object' && !Array.isArray(p)) {
return source.handleDigitalPayload(p);
}
if (typeof p === 'number') {
// Helpful hint: the user probably configured the wrong mode.
log?.warn?.(
`digital mode received a number (${p}); expected an object like {key: value, ...}. ` +
`Switch Input Mode to 'analog' in the editor or send an object payload.`
);
return;
}
log?.warn?.(`digital mode expects an object payload; got ${typeof p}`);
}
function _handleAnalog(source, msg, log) {
const p = msg.payload;
if (typeof p === 'number' || (typeof p === 'string' && p.trim() !== '')) {
const parsed = Number(p);
if (!Number.isNaN(parsed)) {
source.inputValue = parsed;
return;
}
log?.warn?.(`Invalid numeric measurement payload: ${p}`);
if (!p || typeof p !== 'object' || Array.isArray(p)) {
log?.warn?.(`digital mode expects an object payload; got ${typeof p}`);
return;
}
if (p && typeof p === 'object' && !Array.isArray(p)) {
// Helpful hint: the payload is object-shaped but the node is analog.
const keys = Object.keys(p).slice(0, 3).join(', ');
log?.warn?.(
`analog mode received an object payload (keys: ${keys}). ` +
`Switch Input Mode to 'digital' in the editor and define channels, or feed a numeric payload.`
const flat = {};
for (const [key, item] of Object.entries(p)) {
const extracted = _extractValueAndUnit(item, msg?.unit);
if (!extracted || !Number.isFinite(extracted.value)) {
log?.warn?.(`digital channel '${key}' has invalid payload: ${JSON.stringify(item)}`);
continue;
}
const channelUnit = source.channels?.get?.(key)?.unit || null;
flat[key] = _convertToChannelUnit(
extracted.value,
extracted.unit,
channelUnit,
log,
`data.measurement[${key}]`,
);
}
return source.handleDigitalPayload(flat);
}

View File

@@ -0,0 +1,431 @@
// Measurement editor — digital-mode channel row editor.
//
// Replaces the raw JSON textarea with a repeatable card UI. The textarea
// remains the source of truth on the node (node.channels is still a JSON
// string), so server-side parsing in nodeClass.js is untouched.
//
// IMPORTANT ARCHITECTURE NOTE:
// Field edits (typing in key/unit, changing a dropdown, ticking a checkbox)
// MUST NOT trigger a full rerender. A full rebuild destroys the input you
// are typing into and the next keystroke is lost (one-letter-then-stop bug).
// We split the two paths explicitly:
// - commitFieldEdit() — state + textarea sync + targeted DOM updates
// (duplicate-key red borders). Use for every per-
// field edit.
// - rerenderAll() — full rebuild. Use only for structural changes:
// add channel, delete channel, expand/collapse,
// raw-JSON toggle, init.
(function () {
const ns = window.MeasEditor = window.MeasEditor || {};
// --- Option sources ---------------------------------------------------
// Canonical types map 1:1 to MeasurementContainer axes; for those, the
// conversion machinery in generalFunctions expects unit ∈ a known set.
// Free-text units would silently break conversion, so the unit field
// becomes a select for canonical types. Custom types (humidity, co2,
// voc, …) bypass conversion per the docs, so unit stays free text.
const TYPE_OPTIONS = [
'pressure', 'flow', 'power', 'temperature',
'volume', 'length', 'mass', 'energy',
'humidity', 'co2', 'voc',
];
const POSITION_OPTIONS = ['upstream', 'atEquipment', 'downstream'];
const OUTLIER_METHODS = ['zScore', 'iqr', 'modifiedZScore'];
// Per-type unit suggestions. The list is curated to the most common units
// from generalFunctions/src/convert/definitions/<type>.js; users who need
// exotic units can fall back to raw-JSON view.
const UNIT_OPTIONS = {
pressure: ['Pa', 'kPa', 'MPa', 'hPa', 'bar', 'mbar', 'torr', 'psi'],
flow: ['m³/s', 'm³/h', 'L/s', 'L/min', 'gpm'],
power: ['W', 'kW', 'MW', 'hp'],
temperature: ['C', 'K', 'F', 'R'],
volume: ['mL', 'L', 'm³', 'gal'],
length: ['mm', 'cm', 'm', 'km', 'in', 'ft'],
mass: ['mg', 'g', 'kg', 't', 'oz', 'lb'],
energy: ['Wh', 'kWh', 'J', 'kJ', 'MJ'],
// custom types intentionally omitted → unit becomes free text
};
const isCanonicalType = (t) => Object.prototype.hasOwnProperty.call(UNIT_OPTIONS, t);
const getSmoothMethods = () => {
const arr = window.EVOLV?.nodes?.measurement?.config?.smoothing?.smoothMethod?.rules?.values || [];
const names = arr.map((o) => o.value);
return names.length ? names
: ['none', 'mean', 'min', 'max', 'sd', 'median', 'weightedMovingAverage',
'lowPass', 'highPass', 'bandPass', 'kalman', 'savitzkyGolay'];
};
const newChannel = () => ({
key: '',
type: 'pressure',
position: 'atEquipment',
unit: UNIT_OPTIONS.pressure[0],
scaling: { enabled: false, inputMin: 0, inputMax: 1, absMin: 0, absMax: 100, offset: 0 },
smoothing: { smoothWindow: 10, smoothMethod: 'mean' },
outlierDetection: { enabled: false, method: 'zScore', threshold: 3 },
});
const mergeDefaults = (raw) => {
const d = newChannel();
return {
key: raw.key ?? '',
type: raw.type ?? d.type,
position: raw.position ?? d.position,
unit: raw.unit ?? '',
distance: raw.distance ?? null,
scaling: { ...d.scaling, ...(raw.scaling || {}) },
smoothing: { ...d.smoothing, ...(raw.smoothing || {}) },
outlierDetection: { ...d.outlierDetection, ...(raw.outlierDetection || {}) },
};
};
// --- State ------------------------------------------------------------
let _channels = [];
const _expanded = new Set();
let _jsonMode = false;
// --- Small DOM helpers ------------------------------------------------
const el = (tag, attrs = {}, children = []) => {
const e = document.createElement(tag);
for (const [k, v] of Object.entries(attrs)) {
if (v == null) continue;
if (k === 'class') e.className = v;
else if (k === 'style' && typeof v === 'object') Object.assign(e.style, v);
else if (k.startsWith('on') && typeof v === 'function') e.addEventListener(k.slice(2), v);
else e.setAttribute(k, v);
}
for (const c of (Array.isArray(children) ? children : [children])) {
if (c == null || c === false) continue;
e.appendChild(typeof c === 'string' || typeof c === 'number'
? document.createTextNode(String(c)) : c);
}
return e;
};
const selectFrom = (opts, value, onChange, extraClass) => {
const sel = el('select', { class: 'meas-ch-input' + (extraClass ? ' ' + extraClass : '') });
const optsWithValue = value && !opts.includes(value) ? [...opts, value] : opts;
for (const o of optsWithValue) sel.appendChild(el('option', { value: o }, o));
sel.value = value || '';
sel.addEventListener('change', () => onChange(sel.value));
return sel;
};
const numInput = (value, onChange, opts = {}) => {
const inp = el('input', {
type: 'number', class: 'meas-ch-input meas-ch-num',
step: opts.step ?? 'any',
placeholder: opts.placeholder ?? '',
value: (value === '' || value == null || Number.isNaN(value)) ? '' : value,
});
inp.addEventListener('input', () => {
const v = parseFloat(inp.value);
onChange(Number.isFinite(v) ? v : (opts.allowNull ? null : 0));
});
return inp;
};
const textInput = (value, onChange, placeholder) => {
const inp = el('input', { type: 'text', class: 'meas-ch-input', value: value || '', placeholder: placeholder || '' });
inp.addEventListener('input', () => onChange(inp.value));
return inp;
};
const checkbox = (checked, onChange, labelText) => {
const cb = el('input', { type: 'checkbox' });
cb.checked = !!checked;
cb.addEventListener('change', () => onChange(cb.checked));
return el('label', { class: 'meas-ch-cb' }, [cb, ' ', labelText]);
};
// --- Sync + targeted updates -----------------------------------------
const serialize = () => JSON.stringify(_channels, null, 2);
const syncTextarea = () => {
const ta = document.getElementById('node-input-channels');
if (!ta) return;
ta.value = serialize();
ta.dispatchEvent(new Event('input', { bubbles: true }));
};
const refreshKeyValidationClasses = () => {
const { dupes, blanks } = keyValidation();
document.querySelectorAll('#meas-channels-rows [data-role="ch-key"]').forEach((inp) => {
const i = parseInt(inp.dataset.idx, 10);
const bad = dupes.has(i) || blanks.has(i);
inp.classList.toggle('meas-ch-err', bad);
});
};
// Single entry point for every per-field edit. Does NOT rerender.
const commitFieldEdit = () => {
syncTextarea();
refreshKeyValidationClasses();
};
// --- Validation -------------------------------------------------------
const keyValidation = () => {
const seen = new Map();
const dupes = new Set();
const blanks = new Set();
_channels.forEach((c, i) => {
const k = (c.key || '').trim();
if (!k) { blanks.add(i); return; }
if (seen.has(k)) { dupes.add(i); dupes.add(seen.get(k)); }
else seen.set(k, i);
});
return { dupes, blanks };
};
// --- Unit cell (type-driven, swapped in-place on type change) --------
const renderUnitCell = (channel, cardIndex) => {
const cell = el('div', { class: 'meas-ch-unit-cell', 'data-role': 'ch-unit-cell', 'data-idx': String(cardIndex) });
if (isCanonicalType(channel.type)) {
const opts = UNIT_OPTIONS[channel.type];
const sel = selectFrom(opts, channel.unit || opts[0], (v) => {
channel.unit = v;
commitFieldEdit();
});
cell.appendChild(sel);
} else {
const inp = textInput(channel.unit, (v) => {
channel.unit = v;
commitFieldEdit();
}, 'unit (free text)');
cell.appendChild(inp);
}
return cell;
};
// Replace just the unit cell inside one card. No full rerender → focus
// on the type select is preserved.
const swapUnitCell = (cardIndex, channel) => {
const old = document.querySelector(`#meas-channels-rows [data-role="ch-unit-cell"][data-idx="${cardIndex}"]`);
if (!old) return;
old.replaceWith(renderUnitCell(channel, cardIndex));
};
// --- Render: advanced sub-sections ------------------------------------
// These call commitFieldEdit() on edits (no rerender). The only
// exceptions are the enabled-toggle checkboxes: ticking them dims the
// sub-grid, which requires re-rendering JUST that card. We accept the
// tiny focus blip on a checkbox click — focus on a checkbox after a
// click isn't ergonomically important.
const renderScalingSection = (channel, cardIndex) => {
const sc = channel.scaling;
return el('div', { class: 'meas-ch-sub' }, [
el('div', { class: 'meas-ch-sub-title' }, [
checkbox(sc.enabled, (v) => { sc.enabled = v; rerenderCard(cardIndex); }, 'Scaling'),
]),
el('div', { class: 'meas-ch-sub-grid' + (sc.enabled ? '' : ' meas-ch-dim') }, [
el('label', {}, 'input min'), numInput(sc.inputMin, (v) => { sc.inputMin = v; commitFieldEdit(); }),
el('label', {}, 'input max'), numInput(sc.inputMax, (v) => { sc.inputMax = v; commitFieldEdit(); }),
el('label', {}, 'output min'), numInput(sc.absMin, (v) => { sc.absMin = v; commitFieldEdit(); }),
el('label', {}, 'output max'), numInput(sc.absMax, (v) => { sc.absMax = v; commitFieldEdit(); }),
el('label', {}, 'offset'), numInput(sc.offset, (v) => { sc.offset = v; commitFieldEdit(); }),
]),
]);
};
const renderSmoothingSection = (channel) => {
const sm = channel.smoothing;
return el('div', { class: 'meas-ch-sub' }, [
el('div', { class: 'meas-ch-sub-title' }, 'Smoothing'),
el('div', { class: 'meas-ch-sub-grid' }, [
el('label', {}, 'method'),
selectFrom(getSmoothMethods(), sm.smoothMethod || 'mean', (v) => { sm.smoothMethod = v; commitFieldEdit(); }),
el('label', {}, 'window'),
numInput(sm.smoothWindow, (v) => { sm.smoothWindow = v; commitFieldEdit(); }, { step: 1, placeholder: '10' }),
]),
]);
};
const renderOutlierSection = (channel, cardIndex) => {
const od = channel.outlierDetection;
return el('div', { class: 'meas-ch-sub' }, [
el('div', { class: 'meas-ch-sub-title' }, [
checkbox(od.enabled, (v) => { od.enabled = v; rerenderCard(cardIndex); }, 'Outlier detection'),
]),
el('div', { class: 'meas-ch-sub-grid' + (od.enabled ? '' : ' meas-ch-dim') }, [
el('label', {}, 'method'),
selectFrom(OUTLIER_METHODS, od.method || 'zScore', (v) => { od.method = v; commitFieldEdit(); }),
el('label', {}, 'threshold'),
numInput(od.threshold, (v) => { od.threshold = v; commitFieldEdit(); }, { placeholder: '3' }),
]),
]);
};
// --- Render: one card -------------------------------------------------
const renderCard = (channel, index) => {
const isExpanded = _expanded.has(index);
// Key input — tagged with data-role + data-idx so the validation pass
// can find and re-class it without rebuilding the card.
const keyInput = textInput(channel.key, (v) => {
channel.key = v;
commitFieldEdit();
}, 'e.g. temperature');
keyInput.dataset.role = 'ch-key';
keyInput.dataset.idx = String(index);
// Type select — on change: update unit (reset to first unit of the new
// type if previous unit isn't valid there), swap the unit cell in
// place, and sync. No card rebuild.
const typeSelect = selectFrom(TYPE_OPTIONS, channel.type, (v) => {
channel.type = v;
if (isCanonicalType(v)) {
const validUnits = UNIT_OPTIONS[v];
if (!validUnits.includes(channel.unit)) channel.unit = validUnits[0];
}
swapUnitCell(index, channel);
commitFieldEdit();
}, 'meas-ch-w-110');
const posSelect = selectFrom(POSITION_OPTIONS, channel.position, (v) => {
channel.position = v;
commitFieldEdit();
}, 'meas-ch-w-110');
const unitCell = renderUnitCell(channel, index);
const head = el('div', { class: 'meas-ch-head', 'data-card-idx': String(index) }, [
el('span', { class: 'meas-ch-num-badge' }, '#' + (index + 1)),
keyInput, typeSelect, posSelect, unitCell,
el('button', {
type: 'button',
class: 'meas-ch-btn meas-ch-btn-toggle',
title: isExpanded ? 'Hide advanced' : 'Show advanced (scaling / smoothing / outlier)',
onclick: () => {
if (isExpanded) _expanded.delete(index); else _expanded.add(index);
rerenderAll();
},
}, isExpanded ? '▴ less' : '▾ more'),
el('button', {
type: 'button',
class: 'meas-ch-btn meas-ch-btn-del',
title: 'Remove this channel',
onclick: () => {
_channels.splice(index, 1);
const next = new Set();
_expanded.forEach((i) => { if (i < index) next.add(i); else if (i > index) next.add(i - 1); });
_expanded.clear(); next.forEach((i) => _expanded.add(i));
syncTextarea();
rerenderAll();
},
}, '×'),
]);
const card = el('div', { class: 'meas-ch-card', 'data-card-idx': String(index) }, [head]);
if (isExpanded) {
card.appendChild(el('div', { class: 'meas-ch-adv' }, [
renderScalingSection(channel, index),
renderSmoothingSection(channel),
renderOutlierSection(channel, index),
]));
}
return card;
};
// Rebuild a single card in place. Used by the enabled-toggle handlers
// that need to flip the dim class on a sub-grid.
const rerenderCard = (index) => {
const existing = document.querySelector(`#meas-channels-rows .meas-ch-card[data-card-idx="${index}"]`);
if (!existing) { rerenderAll(); return; }
const replacement = renderCard(_channels[index], index);
existing.replaceWith(replacement);
syncTextarea();
};
// --- Render: full list ------------------------------------------------
const rerenderAll = () => {
const host = document.getElementById('meas-channels-rows');
if (!host) return;
host.innerHTML = '';
if (_channels.length === 0) {
host.appendChild(el('div', { class: 'meas-ch-empty' },
'No channels yet. Click "+ Add channel" to define the first one.'));
} else {
_channels.forEach((c, i) => host.appendChild(renderCard(c, i)));
}
refreshKeyValidationClasses();
updateRawToggleButtonLabel();
};
const updateRawToggleButtonLabel = () => {
const btn = document.getElementById('meas-channels-raw-toggle');
const raw = document.getElementById('meas-channels-raw');
if (!btn || !raw) return;
raw.style.display = _jsonMode ? '' : 'none';
btn.textContent = _jsonMode ? '▴ Hide raw JSON' : '▾ Show raw JSON';
};
// --- Public API -------------------------------------------------------
ns.digitalChannels = {
init(node) {
const host = document.getElementById('meas-channels-rows');
if (!host) return;
const ta = document.getElementById('node-input-channels');
const raw = (ta?.value || node?.channels || '[]').trim() || '[]';
try {
const parsed = JSON.parse(raw);
_channels = Array.isArray(parsed) ? parsed.map(mergeDefaults) : [];
} catch {
_channels = [];
}
const addBtn = document.getElementById('meas-channels-add');
if (addBtn && !addBtn.dataset.bound) {
addBtn.dataset.bound = '1';
addBtn.addEventListener('click', () => {
_channels.push(newChannel());
_expanded.add(_channels.length - 1);
syncTextarea();
rerenderAll();
});
}
const rawBtn = document.getElementById('meas-channels-raw-toggle');
if (rawBtn && !rawBtn.dataset.bound) {
rawBtn.dataset.bound = '1';
rawBtn.addEventListener('click', () => {
if (_jsonMode) {
try {
const parsed = JSON.parse((ta?.value || '[]').trim() || '[]');
if (!Array.isArray(parsed)) throw new Error('not an array');
_channels = parsed.map(mergeDefaults);
} catch (e) {
if (typeof RED !== 'undefined' && RED.notify) {
RED.notify('Channels JSON is invalid: ' + e.message
+ ' — stay in JSON view to fix.', 'error');
}
return;
}
}
_jsonMode = !_jsonMode;
rerenderAll();
});
}
if (ta && !ta.dataset.boundBlur) {
ta.dataset.boundBlur = '1';
ta.addEventListener('blur', () => {
if (!_jsonMode) return;
try {
const parsed = JSON.parse(ta.value.trim() || '[]');
if (Array.isArray(parsed)) _channels = parsed.map(mergeDefaults);
} catch {
/* leave alone */
}
});
}
syncTextarea();
rerenderAll();
},
commit() { syncTextarea(); },
};
})();

View File

@@ -0,0 +1,28 @@
// Measurement editor — hover-coupling between side-panel input rows
// and SVG elements they describe. Each .meas-row that carries
// data-couples-line="<svg-element-id>" highlights that SVG element on
// mouseenter and clears the highlight on mouseleave. Mirrors the
// pumpingStation pattern so the visual idiom is consistent across nodes.
(function () {
const ns = window.MeasEditor = window.MeasEditor || {};
ns.hoverCouple = {
init() {
document.querySelectorAll('.meas-diag-side .meas-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('meas-stage-highlight');
const leave = () => target.classList.remove('meas-stage-highlight');
row.addEventListener('mouseenter', enter);
row.addEventListener('mouseleave', leave);
const input = row.querySelector('input, select');
if (input) {
input.addEventListener('focus', enter);
input.addEventListener('blur', leave);
}
});
},
};
})();

42
src/editor/index.js Normal file
View File

@@ -0,0 +1,42 @@
// Measurement editor — shared namespace + helpers.
// Loaded first by measurement.html via /measurement/editor/index.js.
// Each sibling module attaches additional members to window.MeasEditor.
(function () {
const ns = window.MeasEditor = window.MeasEditor || {};
// 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;
};
// Read the value of a select/text input by id; '' if absent.
ns.fStr = (id) => document.getElementById(`node-input-${id}`)?.value || '';
// Read a checkbox by id; false if absent.
ns.fBool = (id) => !!document.getElementById(`node-input-${id}`)?.checked;
// Set a numeric input by id (no node-input- prefix); 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);
}
});
};
// Show/hide an element by id (block when shown).
ns.toggle = (id, show) => {
const el = document.getElementById(id);
if (el) el.style.display = show ? '' : 'none';
};
})();

160
src/editor/oneditprepare.js Normal file
View File

@@ -0,0 +1,160 @@
// Measurement editor — oneditprepare. Replaces the inline script that used
// to live in measurement.html. Initialises form fields, mode toggle,
// scaling toggle, the smoothing dropdown (populated from configData), and
// wires the three live previews (pipeline / scaling / smoothing).
(function () {
const ns = window.MeasEditor = window.MeasEditor || {};
ns.oneditprepare = function () {
const node = this;
// Kick off shared menu init (asset/logger/position) first so a downstream
// error never blocks the menus. Historical regression: a ReferenceError
// in the mode block once aborted oneditprepare and stopped the asset menu
// from rendering at all.
const waitForMenuData = () => {
if (window.EVOLV?.nodes?.measurement?.initEditor) {
window.EVOLV.nodes.measurement.initEditor(node);
} else {
setTimeout(waitForMenuData, 50);
}
};
waitForMenuData();
// === Resolve DOM up front ===
const modeSelect = document.getElementById('node-input-mode');
const modeCards = document.querySelectorAll('.meas-mode-card');
const analogBlock = document.getElementById('analog-only-fields');
const digitalBlock = document.getElementById('digital-only-fields');
const modeHint = document.getElementById('mode-hint');
const channelsArea = document.getElementById('node-input-channels');
const channelsHint = document.getElementById('channels-validation');
const scalingChk = document.getElementById('node-input-scaling');
const scaleRows = document.getElementById('meas-scaling-inputs');
// === Initial values ===
const initialMode = (node.mode === 'digital' || node.mode === 'analog') ? node.mode : 'analog';
if (modeSelect) modeSelect.value = initialMode;
if (channelsArea && typeof node.channels === 'string') channelsArea.value = node.channels;
// === Mode cards (visual two-option picker) ===
const setActiveCard = (mode) => {
modeCards.forEach((c) => c.classList.toggle('meas-mode-active', c.dataset.mode === mode));
};
const applyMode = (mode) => {
const isDigital = mode === 'digital';
if (analogBlock) analogBlock.style.display = isDigital ? 'none' : '';
if (digitalBlock) digitalBlock.style.display = isDigital ? '' : 'none';
if (modeHint) {
modeHint.textContent = isDigital
? 'msg.payload must be an OBJECT, e.g. {"temperature": 22.5, "humidity": 45}. Define each key below.'
: 'msg.payload must be a NUMBER (or numeric string). Configure offset / scaling / smoothing below.';
}
setActiveCard(mode);
validateChannelsJson();
ns.pipelineDiagram?.redraw();
};
function validateChannelsJson() {
if (!channelsHint) return;
if (!modeSelect || modeSelect.value !== 'digital') { channelsHint.textContent = ''; return; }
const raw = (channelsArea && channelsArea.value || '').trim();
if (!raw || raw === '[]') {
channelsHint.innerHTML = '<span style="color:#b45309;">Digital mode with no channels — no measurements will be emitted.</span>';
return;
}
try {
const parsed = JSON.parse(raw);
if (!Array.isArray(parsed)) throw new Error('must be an array');
const missing = parsed
.map((c, i) => (c && c.key && c.type ? null : 'entry ' + i + ': missing key or type'))
.filter(Boolean);
if (missing.length) {
channelsHint.innerHTML = '<span style="color:#b45309;">' + missing.join('; ') + '</span>';
} else {
channelsHint.innerHTML = '<span style="color:#047857;">' + parsed.length + ' channel(s) defined: ' + parsed.map((c) => c.key).join(', ') + '</span>';
}
} catch (e) {
channelsHint.innerHTML = '<span style="color:#b91c1c;">Invalid JSON: ' + e.message + '</span>';
}
}
if (modeSelect) modeSelect.addEventListener('change', (e) => applyMode(e.target.value));
modeCards.forEach((card) => {
card.addEventListener('click', () => {
const m = card.dataset.mode;
if (modeSelect) modeSelect.value = m;
applyMode(m);
});
});
if (channelsArea) channelsArea.addEventListener('input', validateChannelsJson);
// === Smoothing method dropdown — populated from configData ===
const smoothMethodSelect = document.getElementById('node-input-smooth_method');
if (smoothMethodSelect) {
const options = window.EVOLV?.nodes?.measurement?.config?.smoothing?.smoothMethod?.rules?.values || [];
smoothMethodSelect.innerHTML = '';
const emptyOption = document.createElement('option');
emptyOption.value = '';
emptyOption.textContent = 'Select method...';
smoothMethodSelect.appendChild(emptyOption);
options.forEach((opt) => {
const o = document.createElement('option');
o.value = opt.value;
o.textContent = opt.value;
o.title = opt.description;
smoothMethodSelect.appendChild(o);
});
if (node.smooth_method) smoothMethodSelect.value = node.smooth_method;
}
// === Scaling toggle ===
const toggleScalingRows = () => {
const show = !!scalingChk?.checked;
if (scaleRows) scaleRows.style.display = show ? '' : 'none';
ns.scalingChart?.redraw();
ns.pipelineDiagram?.redraw();
};
if (scalingChk) scalingChk.addEventListener('change', toggleScalingRows);
// === Bind redraws ===
// Pipeline diagram listens to anything that flips a stage on/off or
// changes a sub-label.
ns.bindRedraw(
['mode', 'scaling', 'i_min', 'i_max', 'i_offset', 'o_min', 'o_max',
'smooth_method', 'count', 'processOutputFormat', 'dbaseOutputFormat'],
() => ns.pipelineDiagram?.redraw(),
);
// Scaling chart listens to its own four inputs + offset.
ns.bindRedraw(
['scaling', 'i_min', 'i_max', 'i_offset', 'o_min', 'o_max'],
() => ns.scalingChart?.redraw(),
);
// Smoothing sparkline listens to the method dropdown + window.
ns.bindRedraw(
['smooth_method', 'count'],
() => ns.smoothingSparkline?.redraw(),
);
// Initial render once the DOM has settled.
setTimeout(() => {
try {
applyMode(initialMode);
toggleScalingRows();
// Build the digital-channel row UI from node.channels. Safe to call
// even when the user opens the editor in analog mode — the rows
// host is hidden by applyMode() and init() is a no-op when no host
// is present.
ns.digitalChannels?.init(node);
ns.pipelineDiagram?.redraw();
ns.scalingChart?.redraw();
ns.smoothingSparkline?.redraw();
ns.hoverCouple?.init();
} catch (e) {
console.error('measurement editor: initial render failed', e);
}
}, 60);
};
})();

69
src/editor/oneditsave.js Normal file
View File

@@ -0,0 +1,69 @@
// Measurement editor — oneditsave. Validates and persists fields onto the
// node. Behaviour preserved 1:1 from the previous inline script: mode-aware
// channel JSON validation, scaling completeness warning, calibration
// stability threshold default.
(function () {
const ns = window.MeasEditor = window.MeasEditor || {};
ns.oneditsave = function () {
const node = this;
// Shared menu sections.
if (window.EVOLV?.nodes?.measurement?.assetMenu?.saveEditor) {
window.EVOLV.nodes.measurement.assetMenu.saveEditor(node);
}
if (window.EVOLV?.nodes?.measurement?.loggerMenu?.saveEditor) {
window.EVOLV.nodes.measurement.loggerMenu.saveEditor(node);
}
if (window.EVOLV?.nodes?.measurement?.positionMenu?.saveEditor) {
window.EVOLV.nodes.measurement.positionMenu.saveEditor(node);
}
// Mode is the top-level switch.
node.mode = document.getElementById('node-input-mode')?.value || 'analog';
// Channels JSON (digital). Stored as a raw string; server-side parses it.
// Make sure the row editor's in-memory state is flushed into the textarea
// before we read it — defensive, edits already sync on every change but
// commit() is idempotent.
ns.digitalChannels?.commit?.();
node.channels = document.getElementById('node-input-channels')?.value || '[]';
// Analog smoothing method.
node.smooth_method = document.getElementById('node-input-smooth_method')?.value || '';
// Checkboxes.
['scaling', 'simulator'].forEach((field) => {
const el = document.getElementById(`node-input-${field}`);
if (el) node[field] = el.checked;
});
// Numeric fields (NaN → 0).
['i_min', 'i_max', 'i_offset', 'o_min', 'o_max', 'count'].forEach((field) => {
const el = document.getElementById(`node-input-${field}`);
if (el) node[field] = parseFloat(el.value) || 0;
});
// Calibration stability threshold: 0 is a valid (very strict) value, so
// fall back to the default 0.01 only when the field is empty / NaN.
const stRaw = document.getElementById('node-input-stabilityThreshold')?.value;
const stParsed = parseFloat(stRaw);
node.stabilityThreshold = Number.isFinite(stParsed) ? stParsed : 0.01;
// Mode-dependent validation.
if (node.mode === 'analog' && node.scaling && (isNaN(node.i_min) || isNaN(node.i_max))) {
RED.notify('Scaling enabled, but input range is incomplete!', 'error');
}
if (node.mode === 'digital') {
try {
const parsed = JSON.parse(node.channels || '[]');
if (!Array.isArray(parsed) || parsed.length === 0) {
RED.notify('Digital mode: no channels defined. The node will emit nothing.', 'warning');
}
} catch (e) {
RED.notify('Digital mode: Channels JSON is invalid (' + e.message + ')', 'error');
}
}
};
})();

View File

@@ -0,0 +1,91 @@
// Measurement editor — analog signal-pipeline diagram.
//
// Renders a horizontal flow:
// msg.payload → +offset → scale → smooth → outlier → output (port 0/1)
//
// Each stage dims when its feature is disabled (scaling unchecked,
// smoothMethod === '' or 'none', outlierDetection toggled off). Hover-coupling
// (via data-couples-line on the side-panel rows) thickens the active stage
// stroke so the user sees which control affects which stage.
(function () {
const ns = window.MeasEditor = window.MeasEditor || {};
// SVG element IDs that the hover-couple module targets. Kept in one place
// so the HTML template + hover wiring + redraw all use the same names.
const STAGE_IDS = {
input: 'meas-stage-input',
offset: 'meas-stage-offset',
scale: 'meas-stage-scale',
smooth: 'meas-stage-smooth',
outlier: 'meas-stage-outlier',
output: 'meas-stage-output',
};
const setEnabled = (id, on) => {
const el = document.getElementById(id);
if (!el) return;
el.classList.toggle('meas-stage-disabled', !on);
};
const setLabel = (id, text) => {
const el = document.getElementById(id);
if (el) el.textContent = text;
};
ns.pipelineDiagram = {
stageIds: STAGE_IDS,
redraw() {
const mode = ns.fStr('mode') || 'analog';
const wrap = document.getElementById('meas-pipeline-wrap');
if (!wrap) return;
// The pipeline only describes the analog pipeline. In digital mode
// each channel has its own pipeline, so we hide the diagram and let
// the channels block speak for itself.
wrap.style.display = mode === 'digital' ? 'none' : '';
if (mode === 'digital') return;
const scalingOn = ns.fBool('scaling');
const offset = ns.fNum('i_offset');
const offsetOn = offset != null && offset !== 0;
const method = ns.fStr('smooth_method');
const smoothingOn = method && method.toLowerCase() !== 'none';
// Outlier detection has no static UI control on the analog page (it's
// toggled at runtime via the outlierDetection topic). We still draw
// its stage as a runtime-only hint so users see the full pipeline.
setEnabled(STAGE_IDS.offset, offsetOn);
setEnabled(STAGE_IDS.scale, scalingOn);
setEnabled(STAGE_IDS.smooth, !!smoothingOn);
// 'outlier' and 'input'/'output' always rendered enabled.
// Update sub-labels with the live values so the diagram doubles as a
// configuration summary.
if (offsetOn) setLabel('meas-stage-offset-sub', `+${offset}`);
else setLabel('meas-stage-offset-sub', 'off');
if (scalingOn) {
const iMin = ns.fNum('i_min');
const iMax = ns.fNum('i_max');
const oMin = ns.fNum('o_min');
const oMax = ns.fNum('o_max');
const lhs = `[${iMin ?? '?'},${iMax ?? '?'}]`;
const rhs = `[${oMin ?? '?'},${oMax ?? '?'}]`;
setLabel('meas-stage-scale-sub', `${lhs}${rhs}`);
} else {
setLabel('meas-stage-scale-sub', 'off');
}
if (smoothingOn) {
const win = ns.fNum('count');
setLabel('meas-stage-smooth-sub', `${method} · win=${win ?? '?'}`);
} else {
setLabel('meas-stage-smooth-sub', 'off');
}
const procFmt = ns.fStr('processOutputFormat') || 'process';
const dbFmt = ns.fStr('dbaseOutputFormat') || 'influxdb';
setLabel('meas-stage-output-sub', `${procFmt} / ${dbFmt}`);
},
};
})();

View File

@@ -0,0 +1,82 @@
// Measurement editor — scaling line chart.
//
// Plots the analog scaling transform: input axis [i_min, i_max] mapped to
// output axis [o_min, o_max], with the configured i_offset applied before
// scaling (matching specificClass: v = value + offset, then clamp+lerp).
//
// Hidden when scaling is disabled; the static "no scaling" badge in the UI
// already communicates that state.
(function () {
const ns = window.MeasEditor = window.MeasEditor || {};
// Plot bounds in viewBox coords; matches the inline SVG in measurement.html.
const VB = { left: 44, right: 286, top: 14, bot: 156 };
const setAttrs = (el, attrs) => {
if (!el) return;
for (const [k, v] of Object.entries(attrs)) el.setAttribute(k, v);
};
const fmt = (n) => (Number.isFinite(n) ? (Math.abs(n) >= 100 ? n.toFixed(0) : n.toFixed(2)) : '?');
ns.scalingChart = {
redraw() {
const wrap = document.getElementById('meas-scaling-wrap');
if (!wrap) return;
const enabled = ns.fBool('scaling');
wrap.style.display = enabled ? '' : 'none';
if (!enabled) return;
const iMin = ns.fNum('i_min');
const iMax = ns.fNum('i_max');
const oMin = ns.fNum('o_min');
const oMax = ns.fNum('o_max');
const offset = ns.fNum('i_offset') || 0;
// Axis tick labels
setAttrs(document.getElementById('meas-scale-x-min'), { });
const xMinEl = document.getElementById('meas-scale-x-min');
const xMaxEl = document.getElementById('meas-scale-x-max');
const yMinEl = document.getElementById('meas-scale-y-min');
const yMaxEl = document.getElementById('meas-scale-y-max');
if (xMinEl) xMinEl.textContent = fmt(iMin);
if (xMaxEl) xMaxEl.textContent = fmt(iMax);
if (yMinEl) yMinEl.textContent = fmt(oMin);
if (yMaxEl) yMaxEl.textContent = fmt(oMax);
// If the input domain is degenerate we still draw the box but skip
// the line — same defensive choice _applyScaling makes at runtime.
if (iMin == null || iMax == null || oMin == null || oMax == null) return;
if (iMax === iMin) return;
// Map data → plot pixel.
const xPx = (val) => VB.left + ((val - iMin) / (iMax - iMin)) * (VB.right - VB.left);
const yPx = (val) => VB.bot - ((val - oMin) / (oMax - oMin)) * (VB.bot - VB.top);
// The scaling clamps input to [iMin, iMax] before lerping. We draw
// three segments: a flat clamp on the left, the diagonal mapping,
// and a flat clamp on the right. The offset shifts the input axis,
// so the diagonal endpoints map at (iMin - offset) and (iMax - offset)
// in raw-input space, but we plot in offset-applied space (the value
// that enters _applyScaling) — which is just [iMin, iMax]. So no
// additional shift is needed inside the plot; we annotate the offset
// separately.
const line = document.getElementById('meas-scale-line');
if (line) {
const points = [
`${xPx(iMin)},${yPx(oMin)}`,
`${xPx(iMax)},${yPx(oMax)}`,
].join(' ');
line.setAttribute('points', points);
}
// Offset readout — small label under the chart.
const offEl = document.getElementById('meas-scale-offset-label');
if (offEl) {
if (offset === 0) offEl.textContent = 'offset: 0 (no shift)';
else offEl.textContent = `offset: +${offset} (applied before scaling)`;
}
},
};
})();

View File

@@ -0,0 +1,158 @@
// Measurement editor — smoothing sparkline.
//
// Renders a synthetic noisy signal (gray) and the same signal after the
// selected smoothing method + window (green) so the user can see what each
// method does before deploying. The smoothing math here is a small,
// browser-side mirror of src/channel.js. Drift risk: if you add a method
// there, add it here (and vice-versa). Keep parameters identical (e.g. the
// 0.2 lowPass alpha) so the preview matches runtime behaviour.
(function () {
const ns = window.MeasEditor = window.MeasEditor || {};
// Plot box in viewBox coords (matches the inline SVG in measurement.html).
const VB = { left: 10, right: 380, top: 8, bot: 92 };
const N = 80; // sample count
// Deterministic noisy signal: low-freq sine + medium-freq sine + a small
// pseudo-random component using a fixed seed so the preview never jitters
// between renders.
const buildSignal = () => {
const out = new Array(N);
let seed = 0xC0FFEE;
const rand = () => {
// mulberry32
seed |= 0; seed = (seed + 0x6D2B79F5) | 0;
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
for (let i = 0; i < N; i++) {
const base = 0.6 * Math.sin(i * 0.18) + 0.25 * Math.sin(i * 0.55);
// Inject an outlier at sample 40 so the median/iqr cases look different
const spike = (i === 40) ? 2.2 : 0;
const noise = (rand() - 0.5) * 0.7;
out[i] = base + noise + spike;
}
return out;
};
// --- Smoothing math (mirror of src/channel.js, kept self-contained) ---
const mean = (a) => a.reduce((s, v) => s + v, 0) / a.length;
const median = (a) => {
const s = [...a].sort((x, y) => x - y);
const mid = Math.floor(s.length / 2);
return s.length % 2 ? s[mid] : (s[mid - 1] + s[mid]) / 2;
};
const stdDev = (a) => {
if (a.length <= 1) return 0;
const m = mean(a);
return Math.sqrt(a.reduce((s, v) => s + (v - m) ** 2, 0) / (a.length - 1));
};
const wma = (a) => {
let num = 0, den = 0;
for (let i = 0; i < a.length; i++) { num += a[i] * (i + 1); den += (i + 1); }
return num / den;
};
const lowPass = (a) => {
let out = a[0];
for (let i = 1; i < a.length; i++) out = 0.2 * a[i] + 0.8 * out;
return out;
};
const highPass = (a) => {
const f = [a[0]];
for (let i = 1; i < a.length; i++) f[i] = 0.8 * (f[i - 1] + a[i] - a[i - 1]);
return f[f.length - 1];
};
const bandPass = (a) => {
const lp = lowPass(a), hp = highPass(a);
return a.map((v) => lp + hp - v).pop();
};
const kalman = (a) => {
let e = a[0];
const gain = 0.1 / (0.1 + 1);
for (let i = 1; i < a.length; i++) e = e + gain * (a[i] - e);
return e;
};
const savitzkyGolay = (a) => {
const c = [-3, 12, 17, 12, -3];
const norm = c.reduce((s, v) => s + v, 0);
if (a.length < c.length) return a[a.length - 1];
let s = 0;
for (let i = 0; i < c.length; i++) s += a[a.length - c.length + i] * c[i];
return s / norm;
};
const applyMethod = (window, method) => {
const m = (method || '').toLowerCase();
switch (m) {
case '':
case 'none': return window[window.length - 1];
case 'mean': return mean(window);
case 'min': return Math.min(...window);
case 'max': return Math.max(...window);
case 'sd': return stdDev(window);
case 'median': return median(window);
case 'weightedmovingaverage': return wma(window);
case 'lowpass': return lowPass(window);
case 'highpass': return highPass(window);
case 'bandpass': return bandPass(window);
case 'kalman': return kalman(window);
case 'savitzkygolay': return savitzkyGolay(window);
default: return window[window.length - 1];
}
};
// --- Render ---
// Cache the synthetic signal so we don't rebuild it on every keystroke.
let _signalCache = null;
const getSignal = () => { _signalCache = _signalCache || buildSignal(); return _signalCache; };
ns.smoothingSparkline = {
redraw() {
const wrap = document.getElementById('meas-smooth-wrap');
if (!wrap) return;
const method = ns.fStr('smooth_method');
const win = Math.max(1, ns.fNum('count') || 1);
const raw = getSignal();
const smoothed = new Array(raw.length);
const buf = [];
for (let i = 0; i < raw.length; i++) {
buf.push(raw[i]);
if (buf.length > win) buf.shift();
smoothed[i] = applyMethod(buf, method);
}
// Compute y range from BOTH series so neither line clips at the edges.
let yMin = Infinity, yMax = -Infinity;
for (const v of raw) { if (v < yMin) yMin = v; if (v > yMax) yMax = v; }
for (const v of smoothed) { if (v < yMin) yMin = v; if (v > yMax) yMax = v; }
if (!Number.isFinite(yMin) || !Number.isFinite(yMax) || yMin === yMax) {
yMin = yMin - 1; yMax = yMax + 1;
}
const pad = (yMax - yMin) * 0.08;
yMin -= pad; yMax += pad;
const xPx = (i) => VB.left + (i / (N - 1)) * (VB.right - VB.left);
const yPx = (v) => VB.bot - ((v - yMin) / (yMax - yMin)) * (VB.bot - VB.top);
const toPoints = (arr) => arr.map((v, i) => `${xPx(i).toFixed(1)},${yPx(v).toFixed(1)}`).join(' ');
const rawEl = document.getElementById('meas-smooth-raw');
const smEl = document.getElementById('meas-smooth-smoothed');
if (rawEl) rawEl.setAttribute('points', toPoints(raw));
if (smEl) smEl.setAttribute('points', toPoints(smoothed));
const label = document.getElementById('meas-smooth-label');
if (label) {
const m = (method || 'none').toLowerCase();
if (m === '' || m === 'none') label.textContent = 'no smoothing — raw value passed through';
else label.textContent = `method: ${method} · window: ${win} samples`;
}
},
};
})();

View File

@@ -0,0 +1,323 @@
// Unit-handling tests for the measurement data.measurement command.
// Verifies that analog and digital modes accept the same payload shapes
// (bare scalar | rich object | per-channel map) and that supplied units
// are converted into the channel's configured (dropdown) unit.
//
// Run with: node --test test/basic/commands-units.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)),
};
}
// Analog source mock: exposes analogChannel.unit so the handler can resolve
// the channel's configured (dropdown) unit. inputValueSets captures the
// value that was eventually written, after any unit conversion.
function makeAnalogSource({ unit = 'mbar' } = {}) {
const inputValueSets = [];
let _v = 0;
return {
source: {
mode: 'analog',
logger: makeLogger(),
analogChannel: { unit },
get inputValue() { return _v; },
set inputValue(v) { _v = v; inputValueSets.push(v); },
},
inputValueSets,
};
}
// Digital source mock: exposes channels.get(key).unit per channel so each
// digital entry can be converted independently. handleDigitalPayloadCalls
// captures the *flat* {key: convertedNumber} the handler ultimately passes.
function makeDigitalSource(channelUnits) {
const handleDigitalPayloadCalls = [];
const channels = new Map(Object.entries(channelUnits).map(([k, u]) => [k, { unit: u }]));
return {
source: {
mode: 'digital',
logger: makeLogger(),
channels,
handleDigitalPayload: (p) => { handleDigitalPayloadCalls.push(p); return { ok: true }; },
},
handleDigitalPayloadCalls,
};
}
function makeCtx({ logger = makeLogger() } = {}) {
return { logger, RED: { nodes: { getNode: () => undefined } }, node: {}, send: () => {} };
}
function makeRegistry(logger) {
return createRegistry(commands, { logger });
}
// --- analog ----------------------------------------------------------------
test('analog: bare number uses channel default unit (no conversion)', async () => {
const { source, inputValueSets } = makeAnalogSource({ unit: 'mbar' });
const reg = makeRegistry(makeLogger());
await reg.dispatch({ topic: 'data.measurement', payload: 1234 }, source, makeCtx());
assert.deepEqual(inputValueSets, [1234]);
});
test('analog: { value, unit } same as channel passes through unchanged', async () => {
const { source, inputValueSets } = makeAnalogSource({ unit: 'mbar' });
const reg = makeRegistry(makeLogger());
await reg.dispatch(
{ topic: 'data.measurement', payload: { value: 500, unit: 'mbar' } },
source,
makeCtx(),
);
assert.deepEqual(inputValueSets, [500]);
});
test('analog: { value, unit } different but compatible unit is converted', async () => {
const { source, inputValueSets } = makeAnalogSource({ unit: 'mbar' });
const reg = makeRegistry(makeLogger());
// 1 bar = 1000 mbar.
await reg.dispatch(
{ topic: 'data.measurement', payload: { value: 1, unit: 'bar' } },
source,
makeCtx(),
);
assert.equal(inputValueSets.length, 1);
assert.ok(Math.abs(inputValueSets[0] - 1000) < 1e-6,
`expected 1 bar → 1000 mbar, got ${inputValueSets[0]}`);
});
test('analog: msg.unit fallback works for bare-number payloads', async () => {
const { source, inputValueSets } = makeAnalogSource({ unit: 'mbar' });
const reg = makeRegistry(makeLogger());
await reg.dispatch(
{ topic: 'data.measurement', payload: 1, unit: 'bar' },
source,
makeCtx(),
);
assert.equal(inputValueSets.length, 1);
assert.ok(Math.abs(inputValueSets[0] - 1000) < 1e-6,
`expected 1 bar → 1000 mbar via msg.unit, got ${inputValueSets[0]}`);
});
test('analog: unit-measure mismatch warns and falls back to raw value', async () => {
const { source, inputValueSets } = makeAnalogSource({ unit: 'mbar' });
const ctxLogger = makeLogger();
const reg = makeRegistry(ctxLogger);
await reg.dispatch(
{ topic: 'data.measurement', payload: { value: 42, unit: 'kg' } },
source,
makeCtx({ logger: ctxLogger }),
);
assert.deepEqual(inputValueSets, [42]);
assert.ok(
ctxLogger.calls.warn.some((m) => m.includes("'kg'") && m.includes("'mbar'")),
`expected mismatch warning, got: ${JSON.stringify(ctxLogger.calls.warn)}`,
);
});
test('analog: unknown unit warns and falls back to raw value', async () => {
const { source, inputValueSets } = makeAnalogSource({ unit: 'mbar' });
const ctxLogger = makeLogger();
const reg = makeRegistry(ctxLogger);
await reg.dispatch(
{ topic: 'data.measurement', payload: { value: 5, unit: 'gribbles' } },
source,
makeCtx({ logger: ctxLogger }),
);
assert.deepEqual(inputValueSets, [5]);
assert.ok(
ctxLogger.calls.warn.some((m) => m.includes("'gribbles'")),
`expected unknown-unit warning, got: ${JSON.stringify(ctxLogger.calls.warn)}`,
);
});
test('analog: numeric string with msg.unit is converted', async () => {
const { source, inputValueSets } = makeAnalogSource({ unit: 'mbar' });
const reg = makeRegistry(makeLogger());
await reg.dispatch(
{ topic: 'data.measurement', payload: '2', unit: 'bar' },
source,
makeCtx(),
);
assert.equal(inputValueSets.length, 1);
assert.ok(Math.abs(inputValueSets[0] - 2000) < 1e-6,
`expected '2' bar → 2000 mbar, got ${inputValueSets[0]}`);
});
// --- digital ---------------------------------------------------------------
test('digital: per-channel { value, unit } converts each independently', async () => {
const { source, handleDigitalPayloadCalls } = makeDigitalSource({
pIn: 'mbar',
pOut: 'Pa',
});
const reg = makeRegistry(makeLogger());
await reg.dispatch(
{
topic: 'data.measurement',
payload: {
pIn: { value: 1, unit: 'bar' }, // → 1000 mbar
pOut: { value: 1.5, unit: 'bar' }, // → 150000 Pa
},
},
source,
makeCtx(),
);
assert.equal(handleDigitalPayloadCalls.length, 1);
const flat = handleDigitalPayloadCalls[0];
assert.ok(Math.abs(flat.pIn - 1000) < 1e-6, `pIn expected 1000, got ${flat.pIn}`);
assert.ok(Math.abs(flat.pOut - 150000) < 1e-3, `pOut expected 150000, got ${flat.pOut}`);
});
test('digital: bare-number entries use the channel default unit', async () => {
const { source, handleDigitalPayloadCalls } = makeDigitalSource({
a: 'mbar',
b: 'mbar',
});
const reg = makeRegistry(makeLogger());
await reg.dispatch(
{ topic: 'data.measurement', payload: { a: 500, b: 750 } },
source,
makeCtx(),
);
assert.deepEqual(handleDigitalPayloadCalls[0], { a: 500, b: 750 });
});
test('digital: mixed rich + bare entries are converted per-channel', async () => {
const { source, handleDigitalPayloadCalls } = makeDigitalSource({
a: 'mbar',
b: 'mbar',
});
const reg = makeRegistry(makeLogger());
await reg.dispatch(
{
topic: 'data.measurement',
payload: {
a: { value: 1, unit: 'bar' }, // converted → 1000
b: 750, // passthrough
},
},
source,
makeCtx(),
);
const flat = handleDigitalPayloadCalls[0];
assert.ok(Math.abs(flat.a - 1000) < 1e-6, `a expected 1000, got ${flat.a}`);
assert.equal(flat.b, 750);
});
test('digital: msg.unit applies to bare entries when no per-channel unit is given', async () => {
const { source, handleDigitalPayloadCalls } = makeDigitalSource({
a: 'mbar',
b: 'mbar',
});
const reg = makeRegistry(makeLogger());
await reg.dispatch(
{ topic: 'data.measurement', payload: { a: 1, b: 2 }, unit: 'bar' },
source,
makeCtx(),
);
const flat = handleDigitalPayloadCalls[0];
assert.ok(Math.abs(flat.a - 1000) < 1e-6, `a expected 1000, got ${flat.a}`);
assert.ok(Math.abs(flat.b - 2000) < 1e-6, `b expected 2000, got ${flat.b}`);
});
test('digital: unit-measure mismatch on one channel warns + falls back without affecting others', async () => {
const { source, handleDigitalPayloadCalls } = makeDigitalSource({
pressure: 'mbar',
flow: 'm3/h',
});
const ctxLogger = makeLogger();
const reg = makeRegistry(ctxLogger);
await reg.dispatch(
{
topic: 'data.measurement',
payload: {
pressure: { value: 1, unit: 'bar' }, // converted → 1000
flow: { value: 100, unit: 'kg' }, // mismatch → raw 100, warn
},
},
source,
makeCtx({ logger: ctxLogger }),
);
const flat = handleDigitalPayloadCalls[0];
assert.ok(Math.abs(flat.pressure - 1000) < 1e-6, `pressure expected 1000, got ${flat.pressure}`);
assert.equal(flat.flow, 100);
assert.ok(
ctxLogger.calls.warn.some((m) => m.includes("data.measurement[flow]") && m.includes("'kg'")),
`expected per-channel mismatch warning, got: ${JSON.stringify(ctxLogger.calls.warn)}`,
);
});
// --- backwards-compat -----------------------------------------------------
test('analog: { value } without unit uses channel default (rich-payload form)', async () => {
const { source, inputValueSets } = makeAnalogSource({ unit: 'mbar' });
const reg = makeRegistry(makeLogger());
await reg.dispatch(
{ topic: 'data.measurement', payload: { value: 42 } },
source,
makeCtx(),
);
assert.deepEqual(inputValueSets, [42]);
});
test('analog: object payload that is *not* rich still triggers switch-mode warn', async () => {
const { source, inputValueSets } = makeAnalogSource({ unit: 'mbar' });
const ctxLogger = makeLogger();
const reg = makeRegistry(ctxLogger);
await reg.dispatch(
{ topic: 'data.measurement', payload: { tempA: 21.5, tempB: 19.8 } },
source,
makeCtx({ logger: ctxLogger }),
);
assert.equal(inputValueSets.length, 0);
assert.ok(
ctxLogger.calls.warn.some((m) => m.includes('analog mode') && m.includes('digital')),
`expected switch-to-digital warn, got: ${JSON.stringify(ctxLogger.calls.warn)}`,
);
});

View File

@@ -6,7 +6,7 @@ const commands = require('../../src/commands');
const { createRegistry } = require('generalFunctions');
const { makeNodeStub, makeREDStub } = require('../helpers/factories');
test('measurement topic accepts numeric strings and ignores non-numeric objects', async () => {
test('measurement topic accepts numeric strings and rich analog object payloads', async () => {
const inst = Object.create(NodeClass.prototype);
const node = makeNodeStub();
const calls = [];
@@ -29,5 +29,5 @@ test('measurement topic accepts numeric strings and ignores non-numeric objects'
await onInput({ topic: 'measurement', payload: '42' }, () => {}, () => {});
await onInput({ topic: 'measurement', payload: { value: 42 } }, () => {}, () => {});
assert.deepEqual(calls, [42]);
assert.deepEqual(calls, [42, 42]);
});

View File

@@ -1,13 +1,28 @@
# measurement
> **Reflects code as of `afc304b` · regenerated `2026-05-11` via `npm run wiki:all`**
> If this banner is stale, the page may be out of date. Treat as informative, not authoritative.
![code-ref](https://img.shields.io/badge/code--ref-b884c0f-blue) ![s88](https://img.shields.io/badge/S88-Control_Module-a9daee) ![status](https://img.shields.io/badge/status-under--review-orange)
## 1. What this node is
A `measurement` turns a raw sensor signal into a validated, scaled, smoothed reading and re-emits it for any upstream parent. Two modes: **analog** (one channel built from the flat config &mdash; classic 4&ndash;20&nbsp;mA / PLC style) and **digital** (one `Channel` per `config.channels[]` entry &mdash; MQTT / IoT JSON style). It is a leaf in the S88 hierarchy &mdash; no children of its own &mdash; and registers itself as a child of any parent that accepts measurements (`rotatingMachine`, `machineGroupControl`, `pumpingStation`, `reactor`, `monster`, &hellip;).
**measurement** is an S88 Control Module that turns a raw sensor signal into a validated, scaled, smoothed reading and re-emits it for any parent. Two modes: **analog** (one channel built from the flat config) and **digital** (one Channel per `config.channels[]` entry). It is a leaf in the hierarchy — no children of its own.
> [!NOTE]
> Pending full node review (2026-05). Content reflects `CONTRACT.md`, `src/commands/index.js`, and current source only. Some sections are best-effort placeholders pending the next pass.
## 2. Position in the platform
---
## At a glance
| Thing | Value |
|:---|:---|
| What it represents | One sensor signal &mdash; pressure / flow / power / temperature / level / &hellip; |
| S88 level | Control Module |
| Use it when | You need to scale, offset, smooth, outlier-filter, or simulate a sensor reading before handing it to an equipment / unit / process-cell node |
| Don't use it for | Sensor fusion, threshold-trip alarms, or as a control output &mdash; this node is read-only signal conditioning |
| Children it accepts | None &mdash; leaf node |
| Parents it talks to | Any node that subscribes to `<type>.measured.<position>` events (`rotatingMachine`, `MGC`, `pumpingStation`, `reactor`, `monster`, &hellip;) |
---
## How it fits
```mermaid
flowchart LR
@@ -18,12 +33,12 @@ flowchart LR
p3[pumpingStation<br/>Process Cell]:::pc
raw -->|data.measurement| m
m -->|child.register| p1
m -->|child.register<br/>(Port 2 at startup)| p1
m -->|child.register| p2
m -->|child.register| p3
m -.<type>.measured.<position>.-> p1
m -.<type>.measured.<position>.-> p2
m -.<type>.measured.<position>.-> p3
m -.->|"&lt;type&gt;.measured.&lt;position&gt;"| p1
m -.->|"&lt;type&gt;.measured.&lt;position&gt;"| p2
m -.->|"&lt;type&gt;.measured.&lt;position&gt;"| p3
classDef pc fill:#0c99d9,color:#fff
classDef unit fill:#50a8d9,color:#000
classDef equip fill:#86bbdd,color:#000
@@ -32,231 +47,117 @@ flowchart LR
S88 colours: Control Module `#a9daee`, Equipment `#86bbdd`, Unit `#50a8d9`, Process Cell `#0c99d9`. Source of truth: `.claude/rules/node-red-flow-layout.md`.
## 3. Capability matrix
---
| Capability | Status | Notes |
|---|---|---|
| Analog mode — single channel from flat config | ✅ | Default. `data.measurement` payload is numeric. |
| Digital mode — many channels from `config.channels[]` | ✅ | Payload is an object keyed by `channel.key`. |
| Outlier detection | ✅ | Median ± window check. Toggleable via `set.outlier-detection`. |
| Scaling (input range → process range + offset) | ✅ | `config.scaling.{inputMin,inputMax,absMin,absMax,offset}`. |
| Smoothing (moving window) | ✅ | `config.smoothing.{smoothWindow,smoothMethod}`. |
| Min/max tracking | ✅ | `totalMinValue`, `totalMaxValue`, smoothed variants. |
| Calibration (capture current as zero/reference) | ✅ | `cmd.calibrate`. Mutates `config.scaling.offset`. |
| Built-in simulator | ✅ | Sinusoidal/noise driver — `set.simulator` toggles. |
| Repeatability / stability metrics | ✅ | `evaluateRepeatability()`, `isStable()`. |
| Accepts children of its own | ❌ | Leaf node. |
## Try it &mdash; 1-minute demo
## 4. Code map
Import the basic example flow, deploy, and drive a single sensor through scaling + smoothing.
```mermaid
flowchart TB
subgraph nodeRED["nodeClass.js — adapter (BaseNodeAdapter)"]
nc["buildDomainConfig()<br/>static DomainClass, commands<br/>static tickInterval = 1000ms"]
end
subgraph domain["specificClass.js — orchestrator (BaseDomain)"]
sc["Measurement.configure()<br/>mode = analog | digital<br/>builds Channel(s)"]
end
subgraph concerns["src/ concern modules"]
channel["channel.js<br/>outlier → offset → scaling →<br/>smoothing → minMax pipeline"]
simulation["simulation/<br/>built-in Simulator"]
calibration["calibration/<br/>Calibrator + stability"]
commands["commands/<br/>topic registry + handlers"]
end
nc --> sc
sc --> channel
sc --> simulation
sc --> calibration
nc --> commands
```bash
curl -X POST -H 'Content-Type: application/json' \
--data @nodes/measurement/examples/basic.flow.json \
http://localhost:1880/flows
```
| Module | Owns | Read first if you're changing… |
|---|---|---|
| `channel.js` | Per-channel pipeline (outlier → offset → scaling → smoothing → emit) | Per-tick reading flow, unit semantics, emitted event name. |
| `simulation/` | Built-in signal generator for demos and offline tests | Sim behaviour, period / amplitude. |
| `calibration/` | Stability checks, repeatability, offset capture | `cmd.calibrate` behaviour, stable-window heuristic. |
| `commands/` | Input-topic registry and handlers | New input topics, payload validation. |
What to do after deploy:
The analog/digital branch is decided once in `configure()` based on `config.mode.current`. There is no FSM — `tick()` only pumps the simulator when enabled.
1. Click the `measurement 42` inject &mdash; sends `topic: 'measurement'` (legacy alias of `data.measurement`) with payload `42`.
2. Watch Port 0 in the debug pane: `mAbs` updates immediately. After a few injects `totalMinValue` / `totalMaxValue` start tracking the rolling min/max.
3. Toggle the simulator: send `topic: 'set.simulator'`. `tick()` (1000 ms) starts driving `inputValue` through `Simulator.step()`.
4. Trigger calibration: send `topic: 'cmd.calibrate'`. If the rolling window is stable (`stdDev <= config.calibration.stabilityThreshold`) the calibrator captures the current output as the new `config.scaling.offset`.
```mermaid
flowchart LR
cfg[config.mode.current]
cfg -->|"=== 'digital'"| dig[Build N Channels<br/>from config.channels[]]
cfg -->|"=== 'analog' (default)"| ana[Build 1 Channel<br/>from flat config]
dig --> emit_d[handleDigitalPayload<br/>fan-out per channel]
ana --> emit_a[inputValue setter<br/>single channel update]
```
> [!IMPORTANT]
> **GIF needed.** Demo recording of steps 1&ndash;4 with the live status badge. Save as `wiki/_partial-gifs/measurement/01-basic-demo.gif`, target &le; 1&nbsp;MB after `gifsicle -O3 --lossy=80`.
## 5. Topic contract
---
> **Auto-generated** from `src/commands/index.js`. Do NOT hand-edit between the markers. Re-run `npm run wiki:contract`.
## The four things you'll send
<!-- BEGIN AUTOGEN: topic-contract -->
| Topic | Aliases | Payload | What it does |
|:---|:---|:---|:---|
| `data.measurement` | `measurement` | analog: `number` (or numeric string); digital: `{<channelKey>: number, &hellip;}` | Push a raw reading into the pipeline. Wrong shape for the configured mode logs a hint suggesting the other mode. |
| `set.simulator` | `simulator` | (ignored) | Toggle the built-in `Simulator` random-walk on / off. Mutates `config.simulation.enabled`. |
| `set.outlier-detection` | `outlierDetection` | (ignored) | Toggle outlier detection on the analog pipeline. Mutates `config.outlierDetection.enabled`. |
| `cmd.calibrate` | `calibrate` | (ignored) | Run a one-shot calibration. Captures the current output as `config.scaling.offset`; aborts with a warn if the buffer is not stable. |
| Canonical topic | Aliases | Payload | Effect |
|---|---|---|---|
| `set.simulator` | `simulator` | `any` | Replaces the named state value with the supplied payload. |
| `set.outlier-detection` | `outlierDetection` | `any` | Replaces the named state value with the supplied payload. |
| `cmd.calibrate` | `calibrate` | `any` | Triggers an action / sequence — not idempotent. |
| `data.measurement` | `measurement` | `any` | Pushes a value into the node's measurement stream. |
Aliases log a one-time deprecation warning the first time they fire.
<!-- END AUTOGEN: topic-contract -->
---
## 6. Child registration
## What you'll see come out
`measurement` does **not accept children**. It only **registers itself** as a child on its upstream parent.
Sample Port 0 message (analog mode, after a few injects):
```mermaid
flowchart LR
m[measurement]:::ctrl -->|"child.register<br/>(Port 2 at startup)"| parent[rotatingMachine /<br/>MGC / pumpingStation /<br/>reactor / monster]
m -.->|"&lt;type&gt;.measured.&lt;position&gt;<br/>(measurements.emitter)"| parent
classDef ctrl fill:#a9daee,color:#000
```
| What | softwareType payload | Side-effect on parent |
|---|---|---|
| Registration | `measurement` | Parent attaches listener for `<asset.type>.measured.<positionVsParent>`. |
| Subsequent updates | event on `child.measurements.emitter` | Parent mirrors value into its own `MeasurementContainer`. |
Position labels are normalised to **lowercase** in the event name (`upstream`, `downstream`, `atequipment`).
## 7. Lifecycle — what one event (or tick) does
```mermaid
sequenceDiagram
participant ext as external sender
participant m as measurement
participant ch as Channel pipeline
participant emitter as measurements.emitter
participant parent as parent (e.g. rotatingMachine)
ext->>m: data.measurement (12.4)
m->>m: command dispatch (analog branch)
m->>ch: update(12.4)
ch->>ch: outlier check → offset → scale → smooth → minMax
ch->>emitter: <type>.measured.<position> {value, ts, unit}
emitter-->>parent: child event (subscribed at register-time)
m->>m: notifyOutputChanged()
m-->>ext: Port 0 + Port 1 (delta-compressed)
Note over m: every 1000 ms: if simulation.enabled,<br/>simulator.step() → inputValue
```
## 8. Data model — `getOutput()`
Analog mode emits the legacy scalar shape. Digital mode emits a nested `{channels:{...}}` keyed by `channel.key`.
<!-- BEGIN AUTOGEN: data-model -->
| Key | Type | Unit | Sample |
|---|---|---|---|
| `mAbs` | number | — | `0` |
| `mPercent` | number | — | `0` |
| `totalMaxSmooth` | number | — | `0` |
| `totalMaxValue` | number | — | `0` |
| `totalMinSmooth` | number | — | `0` |
| `totalMinValue` | number | — | `0` |
<!-- END AUTOGEN: data-model -->
**Concrete digital sample** (when `mode='digital'`):
~~~json
```json
{
"channels": {
"level-a": { "mAbs": 1.84, "mPercent": 73.6, "totalMinValue": 0.1, "totalMaxValue": 2.4 },
"temp-a": { "mAbs": 18.2, "mPercent": 36.4, "totalMinValue": 14.0, "totalMaxValue": 22.1 }
"topic": "measurement#sensor_a",
"payload": {
"mAbs": 0.42,
"mPercent": 42,
"totalMinValue": 0.12,
"totalMaxValue": 0.78,
"totalMinSmooth": 0.20,
"totalMaxSmooth": 0.65
}
}
~~~
In addition, the legacy `source.emitter` fires `'mAbs'` (analog only) — kept for the editor status badge during the refactor window.
## 9. Configuration — editor form ↔ config keys
```mermaid
flowchart TB
subgraph editor["Node-RED editor form"]
f1[Mode: analog / digital]
f2[Asset type + unit]
f3[Position vs parent]
f4[Scaling: inputMin/Max, absMin/Max, offset]
f5[Smoothing: window + method]
f6[Outlier detection: enabled + window]
f7[Simulation: enabled + amplitude/period]
f8[Digital channels list]
end
subgraph cfg["Domain config slice"]
c1[mode.current]
c2[asset.type / asset.unit]
c3[functionality.positionVsParent]
c4[scaling.*]
c5[smoothing.*]
c6[outlierDetection.*]
c7[simulation.*]
c8[channels []]
end
f1 --> c1
f2 --> c2
f3 --> c3
f4 --> c4
f5 --> c5
f6 --> c6
f7 --> c7
f8 --> c8
```
| Form field | Config key | Default | Range | Where used |
|---|---|---|---|---|
| Mode | `mode.current` | `analog` | enum (`analog`, `digital`) | `Measurement.configure` |
| Asset type | `asset.type` | `pressure` | enum | event name + unit policy |
| Position vs parent | `functionality.positionVsParent` | `atEquipment` | enum | event name suffix |
| Scaling enabled | `scaling.enabled` | `false` | bool | `Channel._applyScaling` |
| Input min/max | `scaling.inputMin/Max` | `0` / `1` | numeric | linear map foot/top |
| Output min/max | `scaling.absMin/absMax` | `50` / `100` | numeric | linear map foot/top |
| Offset | `scaling.offset` | `0` | numeric | calibration target |
| Smoothing window | `smoothing.smoothWindow` | `10` | ≥ 1 (samples) | moving window |
| Outlier detection | `outlierDetection.enabled` | varies | bool | `Channel._isOutlier` |
| Simulation enabled | `simulation.enabled` | `false` | bool | `tick()` step |
Sample Port 0 message (digital mode):
## 10. State chart
```json
{
"topic": "measurement#multi",
"payload": {
"channels": {
"level-a": { "mAbs": 1.84, "mPercent": 73.6, "totalMinValue": 0.1, "totalMaxValue": 2.4 },
"temp-a": { "mAbs": 18.2, "mPercent": 36.4, "totalMinValue": 14.0, "totalMaxValue": 22.1 }
}
}
}
```
**Skipped** — measurement is a pure pipeline. There is no FSM. The only mode switch (analog vs digital) is decided once at `configure()` time and never transitions thereafter; see section 4 for the static branching diagram.
| Field | Meaning |
|:---|:---|
| `mAbs` | Latest output value in scaling-units (after offset + scaling + smoothing). |
| `mPercent` | Same value mapped to `interpolation.percentMin..percentMax` (default 0..100). |
| `totalMinValue` / `totalMaxValue` | Rolling min/max of **raw** (pre-scaling) values. `0` until first sample. |
| `totalMinSmooth` / `totalMaxSmooth` | Rolling min/max of the smoothed output. |
## 11. Examples
Additionally the `source.measurements.emitter` fires `<type>.measured.<position>` on every accepted update &mdash; parents subscribe to that event through the `child.measurements.emitter` handshake established at register time. See [Architecture &mdash; Lifecycle](Reference-Architecture#lifecycle) for the full path.
| Tier | File | What it shows | Status |
|---|---|---|---|
| Basic | `examples/basic.flow.json` | Inject + dashboard, no parent | ⚠️ legacy shape, pre-refactor |
| Integration | `examples/integration.flow.json` | measurement registered as child of a parent | ⚠️ legacy shape, pre-refactor |
| Edge | `examples/edge.flow.json` | Outlier / scaling / simulator edge cases | ⚠️ legacy shape, pre-refactor |
---
Tier 1/2/3 visual-first example flows are still TODO (see `MEMORY.md` "TODO: Example Flows"). Screenshots will land under `wiki/_partial-screenshots/measurement/` when the new flows ship.
## How the pipeline behaves
## 12. Debug recipes
```mermaid
flowchart LR
in[input value] --> out{outlierDetection.enabled?}
out -- yes --> oc[_isOutlier]
oc -- outlier --> drop[drop + warn]
oc -- ok --> off[apply scaling.offset]
out -- no --> off
off --> mm[update raw totalMin/Max]
mm --> sc{scaling.enabled?}
sc -- yes --> lin[linear map<br/>input range → abs range]
sc -- no --> sm[pass-through]
lin --> sm
sm --> sw[push to storedValues<br/>length capped by smoothWindow]
sw --> sf[smoothMethod:<br/>mean / median / kalman / &hellip;]
sf --> sm2[update smooth totalMin/Max]
sm2 --> wo[round + write outputAbs<br/>+ emit measurement event]
```
| Symptom | First thing to check | Where to look |
|---|---|---|
| Parent never receives `<type>.measured.<position>` | `assetType` must match parent's filter exactly (e.g. `flow` — not `flow-electromagnetic`). | `config.asset.type` + `MEMORY.md` integration gotcha. |
| Position labels look uppercase to parent | Event name lowercases — but `functionality.positionVsParent` is sent as-is on `child.register`. | `_buildAnalogChannel` event-name composition. |
| Outliers seem to pass through | `outlierDetection.enabled` may be off (default varies by config). Toggle with `set.outlier-detection`. | `Channel._isOutlier`. |
| `cmd.calibrate` does nothing | Calibrator requires ≥ 2 stable samples — check `isStable()` first. | `calibration/calibrator.js`. |
| Digital payload silently dropped | Unknown channel keys land in the `unknown` log line only at debug level. | enable `logging.logLevel=debug` momentarily. |
| Simulator still running after toggle off | `tick()` reads `config.simulation.enabled` each tick — confirm the toggle actually mutated the config. | `toggleSimulation`. |
The same pipeline runs per `Channel` instance &mdash; once in analog mode, N times in digital mode.
> Never ship `enableLog: 'debug'` in a demo — fills the container log within seconds and obscures real errors.
---
## 13. When you would NOT use this node
## Need more?
- Don't use measurement to **fuse** signals from multiple sensors — it's per-channel only. Aggregate at the parent.
- Don't use measurement for **control output** — it's read-only signal conditioning. Use `rotatingMachine` / `valve` for actuation.
- Don't use measurement for **alarm logic** — there is no threshold-trip output. Build that on top of the emitted reading at the parent or in a dashboard rule.
| Page | What you'll find |
|:---|:---|
| [Reference &mdash; Contracts](Reference-Contracts) | Full topic contract, config schema, child-registration handshake |
| [Reference &mdash; Architecture](Reference-Architecture) | Code map, lifecycle, analog vs digital branching, per-Channel pipeline |
| [Reference &mdash; Examples](Reference-Examples) | Shipped example flows + debug recipes |
| [Reference &mdash; Limitations](Reference-Limitations) | When not to use, known limitations, open questions |
## 14. Known limitations / current issues
| # | Issue | Tracked in |
|---|---|---|
| 1 | Legacy `source.emitter` 'mAbs' event still fired alongside `measurements.emitter` — slated for removal in Phase 7. | `OPEN_QUESTIONS.md` (2026-05-10) |
| 2 | Digital mode's per-channel scaling/smoothing falls back to the analog block's defaults when not specified per channel. | `_buildDigitalChannels`. |
| 3 | Tier 1/2/3 visual-first example flows not yet written; current `examples/` only contains pre-refactor flows. | P9 / P2.14 follow-up. |
| 4 | No automatic recalibration — `cmd.calibrate` is operator-triggered. | `calibration/calibrator.js`. |
[EVOLV master wiki](https://gitea.wbd-rd.nl/RnD/EVOLV/wiki/Home) &middot; [Topology Patterns](https://gitea.wbd-rd.nl/RnD/EVOLV/wiki/Topology-Patterns) &middot; [Topic Conventions](https://gitea.wbd-rd.nl/RnD/EVOLV/wiki/Topic-Conventions)

View File

@@ -0,0 +1,244 @@
# Reference &mdash; Architecture
![code-ref](https://img.shields.io/badge/code--ref-b884c0f-blue)
> [!NOTE]
> Code structure for `measurement`: the three-tier sandwich, the `src/` layout, the per-`Channel` pipeline, the analog vs digital branching, the lifecycle, and the output-port pipeline. For an intuitive overview, return to [Home](Home).
>
> Pending full node review (2026-05). Content reflects current source and `CONTRACT.md`; sections noted as TODO require a second pass.
---
## Three-tier code layout
```
nodes/measurement/
|
+-- measurement.js entry: RED.nodes.registerType('measurement', NodeClass)
| + admin endpoints (menu.js, configData.js, asset-reg)
|
+-- src/
| nodeClass.js extends BaseNodeAdapter (Node-RED bridge)
| specificClass.js extends BaseDomain (orchestrates Channels + helpers)
| channel.js one scalar pipeline (outlier → offset → scale → smooth → emit)
| |
| +-- commands/
| | index.js topic registry (set.simulator / set.outlier-detection /
| | cmd.calibrate / data.measurement)
| | handlers.js pure handler functions (mode-dispatching for data.measurement)
| |
| +-- simulation/
| | simulator.js Simulator — random-walk driver for the analog input
| |
| +-- calibration/
| calibrator.js Calibrator — stability check, offset capture, repeatability proxy
```
### Tier responsibilities
| Tier | File | What it owns | Touches `RED.*` |
|:---|:---|:---|:---:|
| entry | `measurement.js` | Type registration; admin HTTP endpoints (`/menu.js`, `/configData.js`, `/asset-reg`) | Yes |
| nodeClass | `src/nodeClass.js` | Wraps `BaseNodeAdapter`; declares `DomainClass = Measurement`, `commands`, `tickInterval = 1000 ms`, `statusInterval = 1000 ms`; `buildDomainConfig()` reshapes the editor's flat `uiConfig` into the domain config slice | Yes (via base class) |
| specificClass | `src/specificClass.js` | Orchestrator. In `configure()` builds one `Channel` (analog) or N `Channels` (digital), wires up `Simulator` and `Calibrator`, installs legacy mirrors so pre-refactor tests keep passing | No |
| concern | `src/channel.js` | Pure per-channel pipeline: outlier &rarr; offset &rarr; scaling &rarr; smoothing &rarr; min/max &rarr; emit | No |
| concern | `src/simulation/simulator.js` | Random-walk driver used when `config.simulation.enabled` is true | No |
| concern | `src/calibration/calibrator.js` | Stability detection (`isStable`), calibration offset capture (`calibrate`), repeatability proxy (`evaluateRepeatability`) | No |
`specificClass` is stitching. All real work lives in the concern modules.
---
## No FSM &mdash; just modes + a pipeline
Unlike `rotatingMachine` or `pumpingStation`, `measurement` has **no state machine**. The behavioural switch is a one-time decision made in `Measurement.configure()`:
```mermaid
flowchart LR
cfg[config.mode.current]
cfg -->|"=== 'digital'"| dig[_buildDigitalChannels<br/>one Channel per config.channels[i]]
cfg -->|"=== 'analog' (default)"| ana[_buildAnalogChannel<br/>one Channel from flat config]
dig --> emit_d[handleDigitalPayload<br/>fan-out per channel]
ana --> emit_a[inputValue setter<br/>single channel update]
classDef ctrl fill:#a9daee,color:#000
```
After `configure()`:
- **analog mode** &rarr; `this.analogChannel` is set, `this.channels` is an empty `Map`. Setting `m.inputValue = v` runs the whole pipeline and `notifyOutputChanged()` fires Port 0.
- **digital mode** &rarr; `this.channels` is keyed by `channel.key`; `analogChannel` is `undefined`. `handleDigitalPayload(payload)` walks every key in the incoming object, dispatches to the matching `Channel`, and collects a per-channel `{ok, mAbs, mPercent}` summary.
The 1000 ms `tick()` is **only** used to drive the built-in simulator when `config.simulation.enabled` is true; the rest of the node is event-driven (input msg arrives &rarr; pipeline runs &rarr; emit).
---
## The per-`Channel` pipeline
```mermaid
flowchart TB
in[update&#40;value&#41;] --> oe{outlierDetection<br/>.enabled?}
oe -- no --> off[+= scaling.offset]
oe -- yes --> iso[_isOutlier&#40;value&#41;]
iso -- outlier --> drop[return false<br/>warn + drop]
iso -- ok --> off
off --> rmm[update totalMinValue<br/>/ totalMaxValue]
rmm --> sc{scaling.enabled?}
sc -- yes --> as[_applyScaling]
sc -- no --> sm[(unchanged)]
as --> sm
sm --> push[push to storedValues<br/>cap at smoothWindow]
push --> meth[switch&#40;smoothMethod&#41;]
meth --> sms[update totalMinSmooth<br/>/ totalMaxSmooth]
sms --> wo[round to 2dp<br/>compare to outputAbs<br/>(only emit on change)]
wo --> emit[measurements.emitter<br/>fires &lt;type&gt;.measured.&lt;position&gt;]
```
Source: `src/channel.js` `update(value)`.
### Outlier methods
| `method` (config) | Implementation | Threshold default |
|:---|:---|:---:|
| `zScore` (default) | `_zScore`: `\|val - mean\| / stdDev > threshold` | `3` |
| `iqr` | `_iqr`: `val < q1 - 1.5*iqr` or `val > q3 + 1.5*iqr` | `3` |
| `modifiedZScore` | `_modifiedZScore`: `0.6745 * (val - median) / mad > threshold` | `3.5` |
`_isOutlier` returns `false` when fewer than 2 samples are stored (warm-up). The `zScore` branch is intentionally **not** short-circuited at `stdDev === 0`: a perfectly flat baseline marks any deviation as an outlier.
### Smoothing methods
Each tick the smoother pushes the post-scaling value into `storedValues`, trims the buffer to `smoothing.smoothWindow`, then collapses it to a single scalar via `smoothing.smoothMethod`:
| Method | Behaviour |
|:---|:---|
| `none` | Pass through the latest sample |
| `mean` (default) | Arithmetic mean of the window |
| `min` / `max` | Smallest / largest in the window |
| `sd` | Standard deviation |
| `median` | Middle value, robust to outliers |
| `weightedMovingAverage` | Linear weights `1..N` |
| `lowPass` | EWMA, `alpha = 0.2` |
| `highPass` | First-order high-pass, `alpha = 0.8` |
| `bandPass` | LP + HP combination |
| `kalman` | Simple 1-D Kalman with fixed gain |
| `savitzkyGolay` | 5-point cubic SG filter (`[-3, 12, 17, 12, -3] / 35`) |
Unknown method names log an error and pass the raw value through.
### Scaling and percent mapping
`_applyScaling(value)` performs a linear map `[scaling.inputMin..inputMax]` &rarr; `[scaling.absMin..absMax]`, clamping the input to the source range first. An invalid input range (`inputMax <= inputMin`) self-heals to `[0, 1]` and logs a warn.
`_computePercent(value)` then maps the **clamped** result into the percent range `[interpolation.percentMin..percentMax]` (defaults 0..100). When `scaling.enabled` is false and `absMax <= absMin` the percent uses the live `totalMinValue / totalMaxValue` instead.
`_writeOutput` rounds to 2 decimal places and only emits a new measurement when `rounded !== outputAbs` &mdash; so a stable input does **not** retrigger downstream.
---
## Lifecycle &mdash; what one event does
### Analog mode
```mermaid
sequenceDiagram
autonumber
participant ext as external sender
participant nc as nodeClass
participant m as Measurement
participant ch as Channel pipeline
participant emitter as measurements.emitter
participant parent as registered parent
ext->>nc: msg {topic:'data.measurement', payload:42}
nc->>m: dispatch via commands.handlers.dataMeasurement
m->>m: set inputValue = 42
m->>ch: analogChannel.update&#40;42&#41;
ch->>ch: outlier → offset → scale → smooth → minMax
ch->>emitter: pressure.measured.atequipment {value, ts, unit}
emitter-->>parent: child measurement event (subscribed at register-time)
m->>nc: notifyOutputChanged&#40;&#41;
nc-->>ext: Port 0 + Port 1 (delta-compressed)
Note over nc: every 1000 ms: if simulation.enabled,<br/>simulator.step&#40;&#41; → m.inputValue
```
### Digital mode
```mermaid
sequenceDiagram
autonumber
participant ext as external sender
participant nc as nodeClass
participant m as Measurement
participant chs as Channels (per key)
participant emitter as measurements.emitter
participant parent as registered parent
ext->>nc: msg {topic:'data.measurement', payload:{level-a:1.8, temp-a:18}}
nc->>m: handlers.dataMeasurement (digital branch)
m->>m: handleDigitalPayload&#40;payload&#41;
loop for each key in payload
m->>chs: Channel.update&#40;value&#41;
chs->>emitter: &lt;type&gt;.measured.&lt;position&gt; per channel
emitter-->>parent: one event per channel that accepted a value
end
m-->>ext: Port 0 + Port 1 (nested {channels:{...}})
```
> [!NOTE]
> Digital mode currently does **not** call `notifyOutputChanged()` from `handleDigitalPayload`. TODO: confirm whether Port 0 fan-out relies on the tick or on a follow-up notify; pending review of how `BaseNodeAdapter` schedules digital-mode output emission.
---
## Output ports
| Port | Carries | Sample shape |
|:---|:---|:---|
| 0 (process) | Delta-compressed snapshot of `getOutput()` &mdash; analog scalar fields or digital `{channels:{...}}` | `{topic: <name>, payload: {mAbs, mPercent, totalMin/MaxValue, totalMin/MaxSmooth}}` (analog) |
| 1 (telemetry) | InfluxDB line-protocol payload, same fields as Port 0 | `measurement,id=sensor_a mAbs=0.42,mPercent=42,...` |
| 2 (registration) | One `{topic:'registerChild', payload:<node.id>, positionVsParent, distance}` at startup | `{topic:'registerChild', payload:'<id>'}` |
Port-0 / Port-1 use the standard `outputUtils.formatMsg(..., 'process' | 'influxdb')` formatters. Delta compression means consumers see only the keys that changed since the previous tick.
See [EVOLV &mdash; Telemetry](https://gitea.wbd-rd.nl/RnD/EVOLV/wiki/Telemetry) for the platform InfluxDB layout.
---
## Event sources
| Source | Where it fires | What it triggers |
|:---|:---|:---|
| Inbound `msg.topic` | Node-RED input wire | `commands.handlers.<topic>` dispatch via `BaseNodeAdapter` |
| `setInterval(tickInterval = 1000)` | `BaseNodeAdapter` | `Measurement.tick()` &mdash; runs `Simulator.step()` only when `config.simulation.enabled` |
| `setInterval(statusInterval = 1000)` | `BaseNodeAdapter` | `Measurement.getStatusBadge()` re-rendered |
| `Channel._writeOutput` &rarr; `measurements.emitter` | Every accepted update where the rounded output changed | `<type>.measured.<position>` fires once per channel that produced a new value |
| `source.emitter` `'mAbs'` (legacy) | Analog `inputValue` setter | Editor status badge during the refactor window &mdash; deprecated, slated for removal in Phase 7 |
No per-tick FSM. The only background work is the 1000 ms simulator pump.
---
## Where to start reading
| If you're changing... | Read first |
|:---|:---|
| The per-sample pipeline (outlier / scaling / smoothing) | `src/channel.js` `update`, `_isOutlier`, `_applyScaling`, `_applySmoothing` |
| Analog vs digital branching | `src/specificClass.js` `configure`, `_buildAnalogChannel`, `_buildDigitalChannels`, `handleDigitalPayload` |
| Top-level topic dispatch | `src/commands/{index, handlers}.js` |
| Simulator step / bounds | `src/simulation/simulator.js` `step` |
| Calibration stability / offset capture | `src/calibration/calibrator.js` `isStable`, `calibrate`, `evaluateRepeatability` |
| Editor &rarr; domain config reshape | `src/nodeClass.js` `buildDomainConfig` |
| Per-node status badge | `Measurement.getStatusBadge` |
| Output shape | `Measurement.getOutput` (analog) / `getDigitalOutput` (digital) |
---
## Related pages
| Page | Why |
|:---|:---|
| [Home](Home) | Intuitive overview |
| [Reference &mdash; Contracts](Reference-Contracts) | Topic + config + child registration |
| [Reference &mdash; Examples](Reference-Examples) | Shipped flows + debug recipes |
| [Reference &mdash; Limitations](Reference-Limitations) | Known issues and open questions |
| [rotatingMachine wiki](https://gitea.wbd-rd.nl/RnD/rotatingMachine/wiki/Home) | The most common consumer of measurement |
| [EVOLV &mdash; Architecture](https://gitea.wbd-rd.nl/RnD/EVOLV/wiki/Architecture) | Platform-wide three-tier pattern |

279
wiki/Reference-Contracts.md Normal file
View File

@@ -0,0 +1,279 @@
# Reference &mdash; Contracts
![code-ref](https://img.shields.io/badge/code--ref-b884c0f-blue)
> [!NOTE]
> Full topic contract, configuration schema, and child-registration handshake for `measurement`. Source of truth: `src/commands/index.js`, `src/commands/handlers.js`, `src/specificClass.js` `configure()`, and the schema at `generalFunctions/src/configs/measurement.json`.
>
> Pending full node review (2026-05). Hand-written best-effort placeholder where indicated. For an intuitive overview, return to [Home](Home).
---
## Topic contract
The registry lives in `src/commands/index.js`. Each descriptor maps a canonical `msg.topic` to a handler; aliases emit a one-time deprecation warning the first time they fire.
<!-- BEGIN AUTOGEN: topic-contract -->
| Canonical topic | Aliases | Payload | Unit | Effect |
|---|---|---|---|---|
| `set.simulator` | `simulator` | any | — | Toggle the built-in simulator on / off. |
| `set.outlier-detection` | `outlierDetection` | any | — | Toggle / configure outlier detection on the measurement pipeline. |
| `cmd.calibrate` | `calibrate` | any | — | Trigger a one-shot calibration of the measurement. |
| `data.measurement` | `measurement` | any | — | Push a raw measurement (analog: number; digital: per-channel object). |
<!-- END AUTOGEN: topic-contract -->
### Payload-shape rules
| Mode | Accepted | Rejected (logs warn) |
|:---|:---|:---|
| `analog` | `number`; numeric string (trimmed, non-empty, parses with `Number`) | object payload (hint: "Switch Input Mode to 'digital' &hellip;"); non-numeric string |
| `digital` | object `{ key1: number, key2: number, &hellip; }` &mdash; keys must match `config.channels[*].key` | number (hint: "Switch Input Mode to 'analog' &hellip;"); array; any non-object |
Unknown channel keys in a digital payload are collected and reported at `debug` level via `digital payload contained unmapped keys: <list>`.
### Source / mode allow-lists
> [!NOTE]
> TODO: `measurement` does not appear to implement a `flowController`-style action/source allow-list (consult `src/specificClass.js`); it relies on the topic registry's typeof checks. If a future hardening pass adds mode-source gating, fold the table in here.
---
## Data model &mdash; `getOutput()` shape
Source: `src/specificClass.js` `getOutput()` / `getDigitalOutput()` and `src/channel.js` `getOutput()`. Delta-compressed by `outputUtils.formatMsg`: consumers see only the keys that changed.
<!-- BEGIN AUTOGEN: data-model — populate via wiki-gen tool (TODO) -->
### Analog mode (`Measurement.getOutput()`)
| Key | Type | Unit | Notes |
|:---|:---|:---|:---|
| `mAbs` | number | scaling units (`asset.unit` / `general.unit`) | Latest output value after offset + scaling + smoothing. Rounded to 2 dp. |
| `mPercent` | number | % | Output mapped to `interpolation.percentMin..percentMax`. Rounded to 2 dp. |
| `totalMinValue` | number | scaling units | Rolling minimum of the **post-offset, pre-smoothing** values. Reported as `0` until the first sample. |
| `totalMaxValue` | number | scaling units | Rolling maximum of the same. Reported as `0` until the first sample. |
| `totalMinSmooth` | number | scaling units | Rolling minimum of the smoothed output. Starts at `0`. |
| `totalMaxSmooth` | number | scaling units | Rolling maximum of the smoothed output. Starts at `0`. |
### Digital mode (`Measurement.getDigitalOutput()`)
```jsonc
{
"channels": {
"<channel.key>": {
"key": "<channel.key>",
"type": "<channel.type>",
"position": "<channel.position>",
"unit": "<channel.unit>",
"mAbs": <number>,
"mPercent": <number>,
"totalMinValue": <number>,
"totalMaxValue": <number>,
"totalMinSmooth": <number>,
"totalMaxSmooth": <number>
}
// ... one entry per channel that has produced output
}
}
```
<!-- END AUTOGEN: data-model -->
### Status badge
`Measurement.getStatusBadge()`:
| Mode | Badge text | Fill / shape |
|:---|:---|:---|
| `analog` | `<mAbs> <unit>` (e.g. `0.42 bar`) | green / dot |
| `digital` | `digital · <N> channel(s)` | blue / ring |
The legacy `source.emitter` fires `'mAbs'` (analog only) and is kept for the editor status badge during the refactor window &mdash; see [Limitations](Reference-Limitations#legacy-source-emitter).
---
## Events emitted on `source.measurements.emitter`
The shared `MeasurementContainer` fires `<type>.measured.<position>` whenever a `Channel`'s rounded output changes. The type / position come from:
- **analog**: `config.asset.type` and `config.functionality.positionVsParent`.
- **digital**: per-channel `config.channels[i].type` and `config.channels[i].position` (falls back to the node-level `positionVsParent` when missing).
Position labels are normalised to **lowercase** in the event name (`upstream`, `downstream`, `atequipment`). Examples:
- `pressure.measured.upstream`
- `flow.measured.atequipment`
- `level.measured.downstream`
- `temperature.measured.atequipment`
Parents subscribe through the generic `child.measurements.emitter.on(eventName, &hellip;)` handshake established by `childRegistrationUtils` (in `generalFunctions`).
In digital mode one input message can fan out into several events &mdash; one per channel that accepted a value on that tick.
---
## Configuration schema &mdash; editor form to config keys
Source of truth: `generalFunctions/src/configs/measurement.json` plus `nodeClass.buildDomainConfig`. Defaults below come from the schema.
### General (`config.general`)
| Form field | Config key | Default | Notes |
|:---|:---|:---|:---|
| Name | `general.name` | `Sensor` | Human-readable label. |
| (auto-assigned) | `general.id` | `null` | Node-RED node id. |
| Default unit | `general.unit` | `unitless` | Falls back to the asset unit. |
| Enable logging | `general.logging.enabled` | `true` | Master switch. |
| Log level | `general.logging.logLevel` | `info` | `debug` / `info` / `warn` / `error`. |
### Functionality (`config.functionality`)
| Form field | Config key | Default | Notes |
|:---|:---|:---|:---|
| Software type | `functionality.softwareType` | `measurement` | Constant. |
| Role | `functionality.role` | `Sensor` | Constant. |
| Position vs parent | `functionality.positionVsParent` | `atEquipment` | One of `atEquipment` / `upstream` / `downstream`. Used in the `child.register` payload and as the suffix of the measurement event name. |
| Distance offset | `functionality.distance` | `null` | Optional spatial offset; sent with `child.register`. |
### Asset (`config.asset`)
| Form field | Config key | Default | Notes |
|:---|:---|:---|:---|
| Asset UUID | `asset.uuid` | `null` | Globally-unique identifier. |
| Tag code / number | `asset.tagCode` / `asset.tagNumber` | `null` | Asset-registry identifiers. |
| Geolocation | `asset.geoLocation` | `{x:0, y:0, z:0}` | |
| Supplier | `asset.supplier` | `Unknown` | Free text. |
| Category | `asset.category` | `sensor` | `sensor` / `measurement`. |
| Asset type | `asset.type` | `pressure` | **Required.** Matches the type axis on `MeasurementContainer` and the parent's filter (e.g. `flow`, `power`, `temperature`). |
| Model | `asset.model` | `Unknown` | Free text. |
| Asset unit | `asset.unit` | `unitless` | Output unit label for the measurement event payload. |
| Accuracy | `asset.accuracy` | `null` | Optional sensor accuracy. |
| Repeatability | `asset.repeatability` | `null` | Optional repeatability metric. |
> [!IMPORTANT]
> `asset.type` must match the **exact** string the parent listens for. The parent's filter is typically the bare type (`flow`, `pressure`, `power`, &hellip;) &mdash; a measurement configured as `flow-electromagnetic` will not register with a `flow`-only filter on its parent (see [Limitations](Reference-Limitations#asset-type-must-match-the-parents-filter-exactly)).
### Scaling (`config.scaling`)
| Form field | Config key | Default | Notes |
|:---|:---|:---|:---|
| Scaling enabled | `scaling.enabled` | `false` | When false, the input is passed through with only the offset applied. |
| Input min/max | `scaling.inputMin` / `scaling.inputMax` | `0` / `1` | Source range; clamps the input before mapping. |
| Output min/max | `scaling.absMin` / `scaling.absMax` | `50` / `100` | Target range. |
| Offset | `scaling.offset` | `0` | Added before scaling; mutated by `cmd.calibrate`. |
### Smoothing (`config.smoothing`)
| Form field | Config key | Default | Notes |
|:---|:---|:---|:---|
| Window size | `smoothing.smoothWindow` | `10` | `>= 1`. Rolling buffer length. |
| Method | `smoothing.smoothMethod` | `mean` | One of `none` / `mean` / `min` / `max` / `sd` / `median` / `weightedMovingAverage` / `lowPass` / `highPass` / `bandPass` / `kalman` / `savitzkyGolay`. |
### Outlier detection (`config.outlierDetection`)
| Form field | Config key | Default | Notes |
|:---|:---|:---|:---|
| Enabled | `outlierDetection.enabled` | `false` | Toggle with `set.outlier-detection`. |
| Method | `outlierDetection.method` | `zScore` | One of `zScore` / `iqr` / `modifiedZScore`. |
| Threshold | `outlierDetection.threshold` | `3` | Method-specific (e.g. z &gt; 3, mz &gt; 3.5). |
### Simulation (`config.simulation`)
| Form field | Config key | Default | Notes |
|:---|:---|:---|:---|
| Enabled | `simulation.enabled` | `false` | When true, `tick()` (1000 ms) drives `inputValue` via `Simulator.step()`. |
| Safe calibration time | `simulation.safeCalibrationTime` | `100` | ms before calibration is finalised in sim mode. |
### Interpolation (`config.interpolation`)
| Form field | Config key | Default | Notes |
|:---|:---|:---|:---|
| Percent min | `interpolation.percentMin` | `0` | Lower bound of the `mPercent` output. |
| Percent max | `interpolation.percentMax` | `100` | Upper bound. |
### Calibration (`config.calibration`)
| Form field | Config key | Default | Notes |
|:---|:---|:---|:---|
| Stability threshold | `calibration.stabilityThreshold` | `0.01` | Absolute stdDev ceiling (in scaling-units) below which the buffer is considered stable. Fits the default `[50,100]` range; tighten / relax for your sensor. |
### Mode (`config.mode`)
| Form field | Config key | Default | Notes |
|:---|:---|:---|:---|
| Input mode | `mode.current` | `analog` | `analog` (one channel, scalar payload) or `digital` (N channels, object payload). |
### Channels (`config.channels[]` &mdash; digital only)
In digital mode, each entry in `config.channels` defines its own pipeline:
| Field | Required | Falls back to |
|:---|:---:|:---|
| `key` | yes | &mdash; (skipped if missing) |
| `type` | yes | &mdash; (skipped if missing) |
| `position` | no | `config.functionality.positionVsParent` &rarr; `atEquipment` |
| `unit` | no | `config.asset.unit` &rarr; `unitless` |
| `distance` | no | `config.functionality.distance` &rarr; `null` |
| `scaling` | no | `{enabled:false, inputMin:0, inputMax:1, absMin:0, absMax:1, offset:0}` |
| `smoothing` | no | `config.smoothing` |
| `outlierDetection` | no | `config.outlierDetection` |
| `interpolation` | no | `config.interpolation` |
Invalid entries (missing `key` or `type`) are logged and skipped. An empty `config.channels[]` in digital mode logs `digital mode enabled but config.channels is empty; no channels will be emitted.`
### Asset registration (`config.assetRegistration`)
Used by the `/measurement/asset-reg` admin endpoint to register / sync the asset with the upstream asset registry. Not part of the runtime data path.
| Form field | Config key | Default | Notes |
|:---|:---|:---|:---|
| Profile / location / process ids | `assetRegistration.{profileId, locationId, processId}` | `1` | Free integer ids in the asset registry. |
| Status | `assetRegistration.status` | `actief` | Lifecycle status. |
| Child assets | `assetRegistration.childAssets` | `[]` | List of child asset ids. |
### Output (`config.output`)
| Form field | Config key | Default | Notes |
|:---|:---|:---|:---|
| Process output | `output.process` | `process` | `process` / `json` / `csv`. Port-0 formatter. |
| Database output | `output.dbase` | `influxdb` | `influxdb` / `json` / `csv`. Port-1 formatter. |
### Unit policy
> [!NOTE]
> TODO: `measurement` does not currently declare a `unitPolicy` block on its `BaseDomain` configuration (unlike `rotatingMachine`). The per-channel `unit` is carried verbatim into the `MeasurementContainer` write at `_writeOutput`. If a future hardening pass adds a unit-policy enforcement, add the canonical / output / required-unit table here. See `CONTRACT.md` for the current invariants.
---
## Child registration
Source: `src/specificClass.js` `configure` (registers itself via the `BaseDomain` plumbing) and the standard `childRegistrationUtils` handshake in `generalFunctions`.
`measurement` does **not accept children**. It only **registers itself** as a child on its upstream parent.
| Layer | Direction | Topic / event | Payload |
|:---|:---|:---|:---|
| Startup (Port 2) | child &rarr; parent | `registerChild` | `{topic: 'registerChild', payload: <node.id>, positionVsParent, distance}` |
| Runtime | child &rarr; parent | `<asset.type>.measured.<positionVsParent>` on `child.measurements.emitter` | `{value, timestamp, unit, distance?}` (per `MeasurementContainer.value()`) |
| What | softwareType payload | Side-effect on parent |
|:---|:---|:---|
| Registration | `measurement` | Parent attaches a listener for `<asset.type>.measured.<positionVsParent>` on the child's `measurements.emitter`. |
| Subsequent updates | event on `child.measurements.emitter` | Parent mirrors the value into its own `MeasurementContainer` slot. |
Position labels are normalised to **lowercase** in the event name (`upstream`, `downstream`, `atequipment`); the `positionVsParent` field in the register payload is sent as configured (preserves case).
---
## Related pages
| Page | Why |
|:---|:---|
| [Home](Home) | Intuitive overview |
| [Reference &mdash; Architecture](Reference-Architecture) | Code map + per-`Channel` pipeline + lifecycle |
| [Reference &mdash; Examples](Reference-Examples) | Shipped flows + debug recipes |
| [Reference &mdash; Limitations](Reference-Limitations) | Known issues and open questions |
| [EVOLV &mdash; Topic Conventions](https://gitea.wbd-rd.nl/RnD/EVOLV/wiki/Topic-Conventions) | Platform-wide topic rules |
| [EVOLV &mdash; Telemetry](https://gitea.wbd-rd.nl/RnD/EVOLV/wiki/Telemetry) | Port 0 / 1 / 2 InfluxDB layout |

148
wiki/Reference-Examples.md Normal file
View File

@@ -0,0 +1,148 @@
# Reference &mdash; Examples
![code-ref](https://img.shields.io/badge/code--ref-b884c0f-blue)
> [!NOTE]
> Every example flow shipped under `nodes/measurement/examples/`, plus how to load them, what they show, and the debug recipes that go with them. Live source: `nodes/measurement/examples/`.
>
> Pending full node review (2026-05). Tier-1/2/3 visual-first example flows are still TODO (tracked in the superproject `MEMORY.md` "TODO: Example Flows"). The current shipped flows pre-date the refactor; treat them as smoke tests, not as production templates.
---
## Shipped examples
| File | Tier | Dependencies | What it shows | Status |
|:---|:---:|:---|:---|:---|
| `basic.flow.json` | 1 | EVOLV only | Single measurement node driven by inject buttons &mdash; analog scalar input, scaling enabled, three debug taps on Port 0/1/2. | Legacy pre-refactor shape, still imports. |
| `integration.flow.json` | 2 | EVOLV only | Parent-child wiring &mdash; measurement registers as a child of another node and emits its `<type>.measured.<position>` events. | Legacy pre-refactor shape. |
| `edge.flow.json` | 3 | EVOLV only | Invalid / edge payload driving for robustness checks (non-numeric strings, object in analog mode, &hellip;). | Legacy pre-refactor shape. |
The three legacy files predate the AssetResolver refactor and the analog-vs-digital mode flag. They still deploy (the editor will accept the older shape and `nodeClass.buildDomainConfig` reshapes whatever it finds), but the recommended Tier-1/2/3 visual-first replacements are still to be written.
> [!IMPORTANT]
> **TODO &mdash; Tier-1/2/3 visual-first flows.** Replace the three legacy files with:
> - `01 - Basic Analog.json` &mdash; one measurement, inject + scaling + smoothing + outlier-detection toggle + simulator.
> - `02 - Integration with rotatingMachine.json` &mdash; measurement registered as a pressure sensor on a `rotatingMachine`, Port 2 auto-register on deploy, parent's prediction updates as the measurement value moves.
> - `03 - Digital Multi-Channel.json` &mdash; one measurement in `digital` mode with 2&ndash;3 channels (e.g. `level-a`, `temp-a`, `flow-a`) fed by a single object-payload inject.
---
## Loading a flow
### Via the editor
1. Open the Node-RED editor at `http://localhost:1880`.
2. Menu &rarr; Import &rarr; drag the JSON file.
3. Click Deploy.
### Via the Admin API
```bash
curl -X POST -H 'Content-Type: application/json' \
--data @nodes/measurement/examples/basic.flow.json \
http://localhost:1880/flows
```
---
## Example &mdash; `basic.flow.json`
Single-measurement flow with the minimum kit to exercise scaling.
### Nodes on the tab
| Type | Purpose |
|:---|:---|
| `inject` | One-shot `topic: 'measurement', payload: 42` (legacy alias of `data.measurement`) |
| `measurement` | The unit under test &mdash; analog mode, scaling enabled (0..100 &rarr; 0..10), `mean` smoothing, window 5 |
| `debug` &times; 3 | Port 0 (process), Port 1 (InfluxDB), Port 2 (registration) |
### What to do after deploy
1. Click the inject. Port 0 fires with `mAbs ≈ 4.2` (42 scaled into 0..10), `mPercent ≈ 42`.
2. Send another value via the same inject (edit the inject payload to `60`). `totalMinValue` / `totalMaxValue` start tracking, `mAbs` jumps to ~6.0.
3. Send `topic: 'set.simulator'` (use a second inject). `tick()` starts driving `inputValue` through `Simulator.step()` every 1000 ms; Port 0 updates appear automatically.
4. Send `topic: 'cmd.calibrate'`. If `stdDev <= 0.01` (the default `stabilityThreshold`), `config.scaling.offset` jumps to `inputMin - currentOutput`; if not, a warn appears in the log.
5. Send `topic: 'set.outlier-detection'`, then inject a wildly out-of-band value (e.g. `9999`). With outlier detection on the value is dropped with `Outlier detected. Ignoring value=9999`.
> [!IMPORTANT]
> **Screenshot needed.** Editor capture of `basic.flow.json` plus the Port 0 debug output. Save as `wiki/_partial-screenshots/measurement/basic-flow.png`. Replace this callout with the image link.
---
## Example &mdash; `integration.flow.json`
Demonstrates the parent-child handshake: the measurement node's Port 2 auto-fires `child.register` to its parent on deploy, and the parent then receives the `<type>.measured.<position>` event whenever a new reading lands.
> [!IMPORTANT]
> **Screenshot needed.** Editor capture of `integration.flow.json` showing the wiring. Save as `wiki/_partial-screenshots/measurement/integration-flow.png`.
> [!NOTE]
> TODO: confirm the integration flow targets a real EVOLV parent (e.g. `rotatingMachine`) versus a mock function node; if it's a mock, the Tier-2 replacement should use a real parent.
---
## Example &mdash; `edge.flow.json`
Drives the node with malformed inputs to verify the warn paths land cleanly:
- Non-numeric string in analog mode &rarr; `Invalid numeric measurement payload: <value>`.
- Object payload in analog mode &rarr; `analog mode received an object payload (keys: &hellip;). Switch Input Mode to 'digital' &hellip;`.
- Numeric scalar in digital mode &rarr; `digital mode received a number (&hellip;); expected an object &hellip;`.
- Outlier toggle on/off mid-stream &rarr; verifies `analogChannel.outlierDetection.enabled` mirrors `config.outlierDetection.enabled`.
> [!IMPORTANT]
> **Screenshot needed.** Editor capture of `edge.flow.json` plus the log lines each inject triggers. Save as `wiki/_partial-screenshots/measurement/edge-flow.png`.
---
## Debug recipes
| Symptom | First thing to check | Where to look |
|:---|:---|:---|
| Parent never receives `<type>.measured.<position>` | `asset.type` must match the parent's filter exactly (e.g. `flow` &mdash; not `flow-electromagnetic`). Position labels lowercase in the event name. | `config.asset.type` + parent's `childRegistrationUtils` filter. |
| Outliers seem to pass through | `outlierDetection.enabled` may be off (default `false`). Toggle with `set.outlier-detection`. With `<2` samples in the buffer, `_isOutlier` returns `false` regardless. | `Channel._isOutlier`. |
| `cmd.calibrate` does nothing | Calibrator requires `stdDev <= calibration.stabilityThreshold` over `storedValues`. If `storedValues.length < 2`, `isStable()` returns `false` (legacy shape). | `src/calibration/calibrator.js` `isStable`, `calibrate`. |
| Digital payload silently dropped | Unknown channel keys are reported only at `debug` log level (`digital payload contained unmapped keys`). Numeric values that fail `Number.isFinite` warn at `warn`. | `Measurement.handleDigitalPayload`. |
| Simulator still running after toggle off | `tick()` reads `config.simulation.enabled` each tick. Confirm the toggle actually mutated the config (the `set.simulator` handler is idempotent &mdash; it just flips). | `Measurement.tick`, `toggleSimulation`. |
| Port 0 emits nothing after `data.measurement` | Analog: `_writeOutput` only emits when `rounded !== outputAbs`. A repeated identical value is silent by design. | `Channel._writeOutput`. |
| `mPercent` is stuck at `0` or unbounded | `processRange <= 0` (i.e. `absMax <= absMin`); percent falls back to `totalMinValue / totalMaxValue` which start at `0` / `0`. Configure `absMin < absMax`. | `Channel._computePercent`. |
| Scaling output looks clamped | `_applyScaling` clamps the input to `[inputMin, inputMax]` before mapping. Wide-band sensors need `inputMin / inputMax` set to the full physical range. | `Channel._applyScaling`. |
| `mAbs` jumps after `cmd.calibrate` | Expected. Calibration sets `config.scaling.offset = baseline - currentOutputAbs`, which makes the next reading land on the baseline (`inputMin` when scaling enabled, `absMin` otherwise). | `Calibrator.calibrate`. |
| Legacy `setpoint` / `simulator` topics work without warning | First fire emits a one-time deprecation warning via `BaseNodeAdapter`'s alias handling. Subsequent fires are silent &mdash; the topic still works. | `commands/index.js` `aliases`. |
> Never ship `enableLog: 'debug'` in a demo &mdash; fills the container log within seconds and obscures real errors.
---
## Docker compose snippet
To bring up Node-RED + InfluxDB with EVOLV nodes pre-loaded:
```yaml
# docker-compose.yml (extract)
services:
nodered:
build: ./docker/nodered
ports: ['1880:1880']
volumes:
- ./docker/nodered/data:/data/evolv
influxdb:
image: influxdb:2.7
ports: ['8086:8086']
```
Full file: [EVOLV/docker-compose.yml](https://gitea.wbd-rd.nl/RnD/EVOLV/src/branch/development/docker-compose.yml).
---
## Related pages
| Page | Why |
|:---|:---|
| [Home](Home) | Intuitive overview |
| [Reference &mdash; Contracts](Reference-Contracts) | Topic + config + child registration |
| [Reference &mdash; Architecture](Reference-Architecture) | Code map + per-`Channel` pipeline + lifecycle |
| [Reference &mdash; Limitations](Reference-Limitations) | Known issues and open questions |
| [rotatingMachine &mdash; Examples](https://gitea.wbd-rd.nl/RnD/rotatingMachine/wiki/Reference-Examples) | Most common consumer of measurement |
| [EVOLV &mdash; Topology Patterns](https://gitea.wbd-rd.nl/RnD/EVOLV/wiki/Topology-Patterns) | Where measurement fits in a larger plant |

View File

@@ -0,0 +1,117 @@
# Reference &mdash; Limitations
![code-ref](https://img.shields.io/badge/code--ref-b884c0f-blue)
> [!NOTE]
> What `measurement` does not do, current rough edges, and open questions. Open items live in `.agents/improvements/IMPROVEMENTS_BACKLOG.md` in the EVOLV superproject; node-local follow-ups are tracked in the superproject's `MEMORY.md` and `.claude/refactor/OPEN_QUESTIONS.md`.
>
> Pending full node review (2026-05).
---
## When you would not use this node
| Scenario | Use instead |
|:---|:---|
| Fusing signals from multiple sensors into one virtual measurement | This node is per-channel only. Aggregate at the parent (e.g. `rotatingMachine` already combines upstream + downstream into a differential). |
| Producing a control output / actuating something | This is read-only signal conditioning. Use `rotatingMachine`, `valve`, or another equipment-level node. |
| Threshold-trip alarms / latched state | There is no comparator / latch output. Build alarm logic on top of the emitted reading at the parent or in a dashboard rule. |
| A "passive" measurement that should not register with a parent | Registration is automatic at startup &mdash; not currently opt-out. TODO: confirm whether a "no-parent" mode exists; if not, leave the parent input unwired. |
---
## Known limitations
### Asset type must match the parent's filter exactly
Parents subscribe to events by exact string match on `<asset.type>.measured.<position>`. A measurement configured as `flow-electromagnetic` will not be picked up by a parent that filters on `flow`. The fix is mechanical &mdash; set `asset.type` to the bare type the parent expects.
This is documented in the superproject `MEMORY.md` under "Key Integration Gotchas":
> Measurement `assetType: "flow"` required (not "flow-electromagnetic") for pumpingStation/monster.
### Position labels lowercase only in the event name
The event name emits `<type>.measured.<position>` with `position` lowercased (`upstream`, `downstream`, `atequipment`). The `positionVsParent` field in the `child.register` payload, however, is sent **as configured** (preserves case). If a parent indexes children by the register-payload position string, mixed-case there will not match the lowercase position in subsequent events. Document the convention in any new parent that joins measurement.
### Legacy `source.emitter`
`source.emitter` fires `'mAbs'` on the analog `inputValue` setter alongside the canonical `measurements.emitter` path. It is kept for the editor status badge during the refactor window and is **slated for removal in Phase 7**. New consumers must use `measurements.emitter`.
### Digital mode &mdash; `notifyOutputChanged()` not explicitly called
`Measurement.handleDigitalPayload` collects a per-key summary but does not directly call `notifyOutputChanged()`. The analog `inputValue` setter does. TODO: confirm whether digital-mode Port 0 emissions rely on the next `tick()` or a follow-up notify path inside `BaseNodeAdapter`. Until verified, treat digital-mode Port 0 latency as "up to one tick" (1000 ms).
### Digital mode &mdash; per-channel scaling / smoothing fall back to the analog block
When a `config.channels[i]` entry omits a per-channel `scaling`, `smoothing`, `outlierDetection`, or `interpolation`, the missing fields fall back to the node-level config &mdash; **not** to a sensible per-type default. Setting `smoothing.smoothMethod = 'kalman'` at the node level applies that to every digital channel that does not override it. Operators should set every block per channel in production digital flows.
### `data.measurement` accepts numeric strings &mdash; not arrays / NaN
The analog handler parses with `Number(p)` and rejects `NaN`. Empty / whitespace strings are skipped silently. Arrays are not accepted in either mode and log a warn in digital mode.
### Simulator does not respect outlier detection
`Simulator.step()` writes directly into `m.inputValue`. The downstream `Channel.update` does run outlier detection if enabled &mdash; but the simulator's random walk is well-behaved enough that this is effectively a no-op. Don't expect the outlier path to be exercised by the simulator alone.
### `cmd.calibrate` requires &ge; 2 stored values
`Calibrator.isStable()` returns `{isStable:false}` when `storedValues.length < 2`. The legacy `Measurement.isStable()` wrapper returns a bare `false` in that case. A fresh calibration call before any data has arrived is silently rejected.
### Calibration baseline depends on `scaling.enabled`
When `scaling.enabled` is true, the calibration baseline is `scaling.inputMin`. When disabled, it is `scaling.absMin`. Toggling `scaling.enabled` after calibrating shifts the meaning of the captured offset; recalibrate after any scaling-toggle.
### Smoothing buffer not cleared on config change
Changing `smoothing.smoothMethod` or `smoothing.smoothWindow` at runtime does not clear `storedValues`. A previously-mean-smoothed buffer can produce a stale first sample after switching to `lowPass` until the window churns. The conservative workaround is to redeploy.
### `outlierDetection.enabled` mirrored only into `analogChannel`
`toggleOutlierDetection()` propagates the new boolean to `this.analogChannel.outlierDetection.enabled` only. In digital mode the per-channel `Channel.outlierDetection.enabled` is **not** updated by the toggle. TODO: digital-mode parity for `set.outlier-detection`.
### Min/max counters never reset
`totalMinValue` / `totalMaxValue` / `totalMinSmooth` / `totalMaxSmooth` are monotonic over the node's lifetime. There is no explicit reset command. The smooth-min/max additionally have a "first-write" rule that snaps both to the first value &mdash; before that, both read `0`, which can mislead downstream chart axes.
---
## Open questions (tracked)
| Question | Where it lives |
|:---|:---|
| Should digital-mode `notifyOutputChanged()` fire on every accepted update? | Internal &mdash; pending P9 review |
| Drop the legacy `source.emitter 'mAbs'` event | Phase 7 removal |
| Replace legacy `examples/{basic,integration,edge}.flow.json` with Tier-1/2/3 visual-first flows | Superproject `MEMORY.md` "TODO: Example Flows" |
| Add `data.clear-min-max` / `data.reset` topic for the rolling counters | Internal |
| Add per-channel `set.outlier-detection` for digital mode | Internal |
| Auto-recalibration heuristics (currently operator-triggered only) | Internal |
| Per-channel `smoothing` window-clear on config change | Internal |
---
## Migration notes
### From pre-refactor flat config
Older flows used `assetType` / `supplier` / `category` at the top level of the editor config. `nodeClass.buildDomainConfig` reshapes the editor's flat `uiConfig` into the nested domain config slice (`scaling`, `smoothing`, `simulation`, `calibration`, `mode`, `channels`), so legacy flows continue to deploy. The migration is best-effort &mdash; re-saving each measurement node in the editor regenerates the canonical shape.
### From analog-only
Adding `config.mode.current` was additive. Flows that omit it default to `analog` and behave exactly as before. To switch to digital: set the editor's "Input Mode" to `digital` and define `config.channels`.
### From legacy alias topics
`simulator`, `outlierDetection`, `calibrate`, `measurement` continue to work; each emits a one-time deprecation warning on first fire. Prefer the canonical `set.simulator` / `set.outlier-detection` / `cmd.calibrate` / `data.measurement` for new flows.
---
## Related pages
| Page | Why |
|:---|:---|
| [Home](Home) | Intuitive overview |
| [Reference &mdash; Contracts](Reference-Contracts) | Topic + config + child registration (alias map at the end) |
| [Reference &mdash; Architecture](Reference-Architecture) | Code map + per-`Channel` pipeline + lifecycle |
| [Reference &mdash; Examples](Reference-Examples) | Shipped flows + debug recipes |
| [rotatingMachine &mdash; Limitations](https://gitea.wbd-rd.nl/RnD/rotatingMachine/wiki/Reference-Limitations) | Where the most common consumer's caveats overlap |

20
wiki/_Sidebar.md Normal file
View File

@@ -0,0 +1,20 @@
### measurement
- [Home](Home)
**Reference**
- [Contracts](Reference-Contracts)
- [Architecture](Reference-Architecture)
- [Examples](Reference-Examples)
- [Limitations](Reference-Limitations)
**Related**
- [EVOLV master wiki](https://gitea.wbd-rd.nl/RnD/EVOLV/wiki/Home)
- [rotatingMachine wiki](https://gitea.wbd-rd.nl/RnD/rotatingMachine/wiki/Home)
- [machineGroupControl wiki](https://gitea.wbd-rd.nl/RnD/machineGroupControl/wiki/Home)
- [pumpingStation wiki](https://gitea.wbd-rd.nl/RnD/pumpingStation/wiki/Home)
- [Topology Patterns](https://gitea.wbd-rd.nl/RnD/EVOLV/wiki/Topology-Patterns)
- [Topic Conventions](https://gitea.wbd-rd.nl/RnD/EVOLV/wiki/Topic-Conventions)
- [Telemetry](https://gitea.wbd-rd.nl/RnD/EVOLV/wiki/Telemetry)