Compare commits

...

6 Commits

Author SHA1 Message Date
znetsixe
15b7414d41 P11.5 + B2.1/B2.2: per-command units + description (where applicable)
Adds  to scalar setters whose payloads are
plain numbers OR {value, unit}. Skipped where payload is compound or
mode-dependent (control-%, {F, C: [...]}, etc.) — documented inline.
Every command gains a description field for wikiGen consumption.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 17:41:19 +02:00
znetsixe
497f05d92c B1.3: isStable real threshold (config-driven, replaces tautology)
The legacy stdDev < stdDev*2 was always true. New behaviour: stdDev <=
config.calibration.stabilityThreshold OR stdDev === 0. Default
threshold 0.01 in scaling-units. Schema field + editor UI added. 4
BUG-PRESERVED tests rewritten + 4 new edge tests. 101/101 pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 17:29:15 +02:00
znetsixe
e6e212a504 B2.4: remove legacy 'mAbs' event re-emission
No production consumer; deprecated since the MeasurementContainer-based
event surface landed. Drops the on-emit subscription that bridged the
analog channel's <type>.measured.<position> event to source.emitter
as 'mAbs'. 96/96 tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 17:13:17 +02:00
znetsixe
2aa80212e4 P9.3: wiki/Home.md following 14-section visual-first template + wiki:* scripts
Auto-generated topic-contract + data-model sections via shared wikiGen
script. Hand-written Mermaid diagrams for position-in-platform, code
map, child registration, lifecycle, configuration, state chart (where
applicable).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 15:17:33 +02:00
znetsixe
42a0333b7c P3 wave 2: convert measurement to BaseDomain + Channel-based analog
specificClass.js: 716 → 244 lines.
  Measurement extends BaseDomain. Analog mode now routes through one
  Channel (key=null) — eliminates ~400 lines of inline pipeline that
  duplicated what Channel.update() already did.

  Public surface preserved for tests:
    - tick() runs the simulator (when enabled) — Simulator owns the
      random walk, orchestrator just writes the output back.
    - inputValue setter routes through analogChannel.update.
    - calibrate() / evaluateRepeatability() delegate to Calibrator.
    - toggleSimulation / toggleOutlierDetection unchanged.
    - 'mAbs' emitter event re-emitted from the analog channel's
      MeasurementContainer event — backwards compat (deprecated;
      tracked in OPEN_QUESTIONS.md for removal in Phase 7/8.5).

nodeClass.js: 230 → 42 lines.
  Extends BaseNodeAdapter. tickInterval=1000 (only meaningful when
  simulator enabled; tick is a no-op otherwise — toggling simulation
  shouldn't require a redeploy). buildDomainConfig parses channels
  JSON + mode and shapes scaling/smoothing/simulation slices.

96 / 96 tests pass (basic 77 + integration 17 + edge 2).
Two routing tests adjusted to seed the new commandRegistry path
(legacy private wiring removed); domain-tier tests unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 20:39:54 +02:00
znetsixe
b990f67df1 P3 wave 1: extract measurement simulator/calibration/commands + CONTRACT
src/simulation/simulator.js  random-walk generator (was simulateInput inline)
  src/calibration/calibrator.js  calibrate + isStable + evaluateRepeatability,
                                using generalFunctions/stats. NB: isStable
                                tautology preserved verbatim — see
                                OPEN_QUESTIONS.md 2026-05-10 for the bug.
  src/commands/                  registry + handlers (canonical names from start)
  CONTRACT.md                    inputs/outputs/events surface

77 basic tests pass (62 pre-refactor + 15 new across the three new files).
specificClass.js / nodeClass.js untouched — integration is P3 wave 2.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 20:32:26 +02:00
16 changed files with 1270 additions and 857 deletions

59
CONTRACT.md Normal file
View File

@@ -0,0 +1,59 @@
# measurement — Contract
Hand-maintained for Phase 3; the `## Inputs` table is generated from
`src/commands/index.js` (see Phase 9 generator). Keep ≤ 80 lines.
## Inputs (msg.topic on Port 0)
| Canonical | Aliases (deprecated) | Payload | Effect |
|---|---|---|---|
| `set.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. |
Aliases log a one-time deprecation warning the first time they fire.
## Outputs (msg.topic on Port 0/1/2)
- **Port 0 (process):** `msg.topic = config.general.name`. Payload built by
`outputUtils.formatMsg(..., 'process')` from `getOutput()` (analog) or
`getDigitalOutput()` (digital). Delta-compressed — only changed fields are
emitted.
- **Port 1 (InfluxDB telemetry):** same shape as Port 0, formatted with the
`'influxdb'` formatter.
- **Port 2 (registration):** at startup the node sends one
`{ topic: 'registerChild', payload: <node.id>, positionVsParent, distance }`
to its parent.
## Events emitted by `source.measurements.emitter`
The `MeasurementContainer` fires `<type>.measured.<position>` whenever a
matching series receives a new value. The type / position labels are set
from `config.asset.type` and `config.functionality.positionVsParent`
(analog), or per-channel from `config.channels[*]` (digital). Examples:
- `pressure.measured.upstream`
- `flow.measured.atequipment`
- `level.measured.downstream`
- `temperature.measured.atequipment`
Position labels are always lowercase in the event name. Parents subscribe
through the generic `child.measurements.emitter.on(eventName, ...)` handshake
established by `childRegistrationUtils`.
In digital mode one input message can fan out into several events — one
per channel that accepted a value on that tick.
The legacy internal `source.emitter` also fires `'mAbs'` with the current
scaled absolute value (analog mode only). This is deprecated in favour of
`measurements.emitter` and kept only for the editor status badge during the
refactor window.
## Children registered by this node
None — `measurement` is a leaf in the S88 hierarchy (Control Module). It
registers itself as a child of an upstream parent (rotatingMachine,
pumpingStation, reactor, monster, …) but does not accept its own children.
Registration goes via Port 2 at startup and is keyed off
`positionVsParent` / `distance` in the node's UI config.

View File

@@ -34,6 +34,7 @@
simulator: { value: false }, simulator: { value: false },
smooth_method: { value: "" }, smooth_method: { value: "" },
count: { value: "10", required: true }, count: { value: "10", required: true },
stabilityThreshold: { value: 0.01 },
processOutputFormat: { value: "process" }, processOutputFormat: { value: "process" },
dbaseOutputFormat: { value: "influxdb" }, dbaseOutputFormat: { value: "influxdb" },
@@ -227,6 +228,12 @@
(field) => (node[field] = parseFloat(document.getElementById(`node-input-${field}`).value) || 0) (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 // Mode-dependent validation. In digital mode we don't care about
// scaling completeness (the channels have their own per-channel // scaling completeness (the channels have their own per-channel
// scaling); in analog mode we still warn about half-filled ranges. // scaling); in analog mode we still warn about half-filled ranges.
@@ -329,6 +336,14 @@
<input type="number" id="node-input-count" placeholder="10" style="width:60px;"/> <input type="number" id="node-input-count" placeholder="10" style="width:60px;"/>
<div class="form-tips">Number of samples for smoothing</div> <div class="form-tips">Number of samples for smoothing</div>
</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>
</div>
</div> </div>
<hr> <hr>

View File

@@ -4,7 +4,10 @@
"description": "Control module measurement", "description": "Control module measurement",
"main": "measurement.js", "main": "measurement.js",
"scripts": { "scripts": {
"test": "node --test test/basic/*.test.js test/integration/*.test.js test/edge/*.test.js" "test": "node --test test/basic/*.test.js test/integration/*.test.js test/edge/*.test.js",
"wiki:contract": "node ../generalFunctions/scripts/wikiGen.js contract ./src/commands/index.js --write ./wiki/Home.md",
"wiki:datamodel": "node ../generalFunctions/scripts/wikiGen.js datamodel ./src/specificClass.js --write ./wiki/Home.md",
"wiki:all": "npm run wiki:contract && npm run wiki:datamodel"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@@ -0,0 +1,96 @@
'use strict';
const { stats } = require('generalFunctions');
const DEFAULT_STABILITY_THRESHOLD = 0.01;
/**
* Calibration helper extracted from measurement/specificClass.js.
*
* The orchestrator owns the rolling buffer and the live config; this class
* reads them through accessor callbacks (`storedValuesRef` / `configRef`)
* so it never holds stale references when the orchestrator mutates either.
*/
class Calibrator {
constructor({ storedValuesRef, configRef, logger } = {}) {
if (typeof storedValuesRef !== 'function' || typeof configRef !== 'function') {
throw new Error('Calibrator requires storedValuesRef and configRef functions');
}
this._storedValues = storedValuesRef;
this._config = configRef;
this.logger = logger || { info() {}, warn() {}, debug() {}, error() {} };
}
/**
* Decide whether the rolling window is stable enough to trust.
* Compares the window's stdDev against config.calibration.stabilityThreshold
* (absolute, in scaling-units). A constant buffer (stdDev=0) is always
* stable regardless of threshold.
*/
isStable() {
const values = this._storedValues();
if (!Array.isArray(values) || values.length < 2) {
return { isStable: false, stdDev: 0 };
}
const stdDev = stats.stdDev(values);
const cfg = this._config();
const raw = cfg && cfg.calibration && cfg.calibration.stabilityThreshold;
const threshold = Number.isFinite(Number(raw)) && Number(raw) >= 0
? Number(raw)
: DEFAULT_STABILITY_THRESHOLD;
return { isStable: stdDev === 0 || stdDev <= threshold, stdDev };
}
/**
* Compute the offset that drives `currentOutputAbs` to the configured
* baseline (scaling input-min when scaling is enabled, abs-min otherwise).
* Returns null when the input is not stable — caller leaves the offset
* untouched and logs the abort.
*/
calibrate(currentOutputAbs) {
const { isStable } = this.isStable();
if (!isStable) {
this.logger.warn('Large fluctuations detected between stored values. Calibration aborted.');
return null;
}
const cfg = this._config();
const scaling = (cfg && cfg.scaling) || {};
const baseline = scaling.enabled ? scaling.inputMin : scaling.absMin;
if (typeof baseline !== 'number' || !Number.isFinite(baseline)) {
this.logger.warn('Calibration baseline missing from config.scaling. Aborted.');
return null;
}
const offset = baseline - currentOutputAbs;
this.logger.info(`Stable input value detected. Calibration completed. Offset=${offset}`);
return { offset };
}
/**
* Repeatability proxy: the std-dev of the smoothed rolling buffer once
* stability is confirmed. Smoothing must be active, otherwise the buffer
* is just raw input and the metric is meaningless.
*/
evaluateRepeatability() {
const cfg = this._config();
const method = cfg && cfg.smoothing && cfg.smoothing.smoothMethod;
const normalized = typeof method === 'string' ? method.toLowerCase() : method;
if (normalized === 'none' || normalized == null) {
this.logger.warn('Repeatability evaluation is not possible without smoothing.');
return { repeatability: null, reason: 'smoothing-disabled' };
}
const values = this._storedValues();
if (!Array.isArray(values) || values.length < 2) {
this.logger.warn('Not enough data to evaluate repeatability.');
return { repeatability: null, reason: 'insufficient-data' };
}
const { isStable, stdDev } = this.isStable();
if (!isStable) {
this.logger.warn('Data not stable enough to evaluate repeatability.');
return { repeatability: null, reason: 'unstable' };
}
this.logger.info(`Repeatability evaluated. Standard Deviation: ${stdDev}`);
return { repeatability: stdDev };
}
}
module.exports = Calibrator;

74
src/commands/handlers.js Normal file
View File

@@ -0,0 +1,74 @@
'use strict';
// Handler functions for measurement commands. Each handler receives:
// source: the domain (specificClass) instance — exposes toggleSimulation,
// toggleOutlierDetection, calibrate, handleDigitalPayload, mode,
// inputValue (settable), 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.
function _logger(source, ctx) {
return ctx?.logger || source?.logger || null;
}
exports.setSimulator = (source) => {
// Idempotent flip — payload is ignored; the source owns the boolean.
source.toggleSimulation();
};
exports.setOutlierDetection = (source) => {
source.toggleOutlierDetection();
};
exports.calibrate = (source) => {
source.calibrate();
};
exports.dataMeasurement = (source, msg, ctx) => {
const log = _logger(source, ctx);
if (source.mode === 'digital') {
return _handleDigital(source, msg, log);
}
return _handleAnalog(source, msg, log);
};
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}`);
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.`
);
}
}

45
src/commands/index.js Normal file
View File

@@ -0,0 +1,45 @@
'use strict';
// measurement command registry. Consumed by BaseNodeAdapter via
// `static commands = require('./commands')`. Each descriptor maps a
// canonical msg.topic to its handler; legacy names are listed under
// `aliases` and emit a one-time deprecation warning at runtime.
const handlers = require('./handlers');
module.exports = [
{
topic: 'set.simulator',
aliases: ['simulator'],
// Toggle — payload is ignored. `any` keeps the registry validator happy
// for legacy callers that ship trigger payloads of various shapes.
payloadSchema: { type: 'any' },
description: 'Toggle the built-in simulator on / off.',
handler: handlers.setSimulator,
},
{
topic: 'set.outlier-detection',
aliases: ['outlierDetection'],
payloadSchema: { type: 'any' },
description: 'Toggle / configure outlier detection on the measurement pipeline.',
handler: handlers.setOutlierDetection,
},
{
topic: 'cmd.calibrate',
aliases: ['calibrate'],
payloadSchema: { type: 'any' },
description: 'Trigger a one-shot calibration of the measurement.',
handler: handlers.calibrate,
},
{
topic: 'data.measurement',
aliases: ['measurement'],
// Mode-dispatched: digital expects object (per-channel), analog expects
// number/numeric string in the configured Channel scaling units. Units
// are mode-dependent and resolved inside the handler — no registry-level
// `units` field.
payloadSchema: { type: 'any' },
description: 'Push a raw measurement (analog: number; digital: per-channel object).',
handler: handlers.dataMeasurement,
},
];

View File

@@ -1,229 +1,42 @@
/** 'use strict';
* measurement.class.js
*
* Encapsulates all node logic in a reusable class. In future updates we can split this into multiple generic classes and use the config to specifiy which ones to use.
* This allows us to keep the Node-RED node clean and focused on wiring up the UI and event handlers.
*/
const { outputUtils, configManager } = require('generalFunctions');
const Specific = require("./specificClass");
const { BaseNodeAdapter } = require('generalFunctions');
const Measurement = require('./specificClass');
const commands = require('./commands');
class nodeClass { class nodeClass extends BaseNodeAdapter {
/** static DomainClass = Measurement;
* Create a MeasurementNode. static commands = commands;
* @param {object} uiConfig - Node-RED node configuration. // Tick drives the simulator's random walk when enabled. Disabled mode is
* @param {object} RED - Node-RED runtime API. // event-driven via the `output-changed` emit from the analog Channel.
* @param {object} nodeInstance - The Node-RED node instance. static tickInterval = 1000;
* @param {string} nameOfNode - The name of the node, used for static statusInterval = 1000;
*/
constructor(uiConfig, RED, nodeInstance, nameOfNode) {
// Preserve RED reference for HTTP endpoints if needed buildDomainConfig(uiConfig, _nodeId) {
this.node = nodeInstance;
this.RED = RED;
this.name = nameOfNode;
// Load default & UI config
this._loadConfig(uiConfig,this.node);
// Instantiate core Measurement class
this._setupSpecificClass();
// Wire up event and lifecycle handlers
this._bindEvents();
this._registerChild();
this._startTickLoop();
this._attachInputHandler();
this._attachCloseHandler();
}
/**
* Load and merge default config with user-defined settings.
* Uses ConfigManager.buildConfig() for base sections (general, asset, functionality),
* then adds measurement-specific domain config.
* @param {object} uiConfig - Raw config from Node-RED UI.
*/
_loadConfig(uiConfig,node) {
const cfgMgr = new configManager();
this.defaultConfig = cfgMgr.getConfig(this.name);
// Build config: base sections + measurement-specific domain config
// `channels` (digital mode) is stored on the UI as a JSON string to
// avoid requiring a custom editor table widget at first. We parse here;
// invalid JSON is logged and the node falls back to an empty array.
let channels = []; let channels = [];
if (typeof uiConfig.channels === 'string' && uiConfig.channels.trim()) { if (typeof uiConfig.channels === 'string' && uiConfig.channels.trim()) {
try { channels = JSON.parse(uiConfig.channels); } try { channels = JSON.parse(uiConfig.channels); }
catch (e) { node.warn(`Invalid channels JSON: ${e.message}`); channels = []; } catch (e) { this.node.warn(`Invalid channels JSON: ${e.message}`); channels = []; }
} else if (Array.isArray(uiConfig.channels)) { } else if (Array.isArray(uiConfig.channels)) {
channels = uiConfig.channels; channels = uiConfig.channels;
} }
const mode = (typeof uiConfig.mode === 'string' && uiConfig.mode.toLowerCase() === 'digital') ? 'digital' : 'analog'; const mode = (typeof uiConfig.mode === 'string' && uiConfig.mode.toLowerCase() === 'digital') ? 'digital' : 'analog';
this.config = cfgMgr.buildConfig(this.name, uiConfig, node.id, { return {
scaling: { scaling: {
enabled: uiConfig.scaling, enabled: uiConfig.scaling,
inputMin: uiConfig.i_min, inputMin: uiConfig.i_min,
inputMax: uiConfig.i_max, inputMax: uiConfig.i_max,
absMin: uiConfig.o_min, absMin: uiConfig.o_min,
absMax: uiConfig.o_max, absMax: uiConfig.o_max,
offset: uiConfig.i_offset offset: uiConfig.i_offset,
},
smoothing: {
smoothWindow: uiConfig.count,
smoothMethod: uiConfig.smooth_method
},
simulation: {
enabled: uiConfig.simulator
}, },
smoothing: { smoothWindow: uiConfig.count, smoothMethod: uiConfig.smooth_method },
simulation: { enabled: uiConfig.simulator },
calibration: { stabilityThreshold: uiConfig.stabilityThreshold },
mode: { current: mode }, mode: { current: mode },
channels, channels,
}); };
// Utility for formatting outputs
this._output = new outputUtils();
}
/**
* Instantiate the core logic and store as source.
*/
_setupSpecificClass() {
this.source = new Specific(this.config);
this.node.source = this.source; // Store the source in the node instance for easy access
}
/**
* Bind Measurement events to Node-RED status updates. Using internal emitter. --> REMOVE LATER WE NEED ONLY COMPLETE CHILDS AND THEN CHECK FOR UPDATES
*/
_bindEvents() {
// Analog mode: the classic 'mAbs' event pushes a green dot with the
// current value + unit to the editor.
this.source.emitter.on('mAbs', (val) => {
this.node.status({ fill: 'green', shape: 'dot', text: `${val} ${this.config.general.unit}` });
});
// Digital mode: summarise how many channels have ticked a value.
// This runs on every accepted channel update so the editor shows live
// activity instead of staying blank when no single scalar exists.
if (this.source.mode === 'digital') {
this.node.status({ fill: 'blue', shape: 'ring', text: `digital · ${this.source.channels.size} channel(s)` });
}
}
/**
* Register this node as a child upstream and downstream.
* Delayed to avoid Node-RED startup race conditions.
*/
_registerChild() {
setTimeout(() => {
this.node.send([
null,
null,
{ topic: 'registerChild', payload: this.node.id , positionVsParent: this.config?.functionality?.positionVsParent || 'atEquipment' , distance: this.config?.functionality?.distance || null},
]);
}, 100);
}
/**
* Start the periodic tick loop to drive the Measurement class.
*/
_startTickLoop() {
setTimeout(() => {
this._tickInterval = setInterval(() => this._tick(), 1000);
}, 1000);
}
/**
* Execute a single tick: update measurement, format and send outputs.
*/
_tick() {
this.source.tick();
// In digital mode we don't funnel through calculateInput with a single
// scalar; instead each Channel has already emitted into the
// MeasurementContainer on message arrival. The tick payload carries a
// per-channel snapshot so downstream flows still see a heartbeat.
const raw = (this.source.mode === 'digital')
? this.source.getDigitalOutput()
: this.source.getOutput();
const processMsg = this._output.formatMsg(raw, this.source.config, 'process');
const influxMsg = this._output.formatMsg(raw, this.source.config, 'influxdb');
// Send only updated outputs on ports 0 & 1
this.node.send([processMsg, influxMsg]);
}
/**
* Attach the node's input handler, routing control messages to the class.
*/
_attachInputHandler() {
this.node.on('input', (msg, send, done) => {
try {
switch (msg.topic) {
case 'simulator':
this.source.toggleSimulation();
break;
case 'outlierDetection':
this.source.toggleOutlierDetection();
break;
case 'calibrate':
this.source.calibrate();
break;
case 'measurement':
// Dispatch based on mode:
// analog -> scalar payload (number or numeric string)
// digital -> object payload keyed by channel name
if (this.source.mode === 'digital') {
if (msg.payload && typeof msg.payload === 'object' && !Array.isArray(msg.payload)) {
const summary = this.source.handleDigitalPayload(msg.payload);
// Summarise what actually got accepted on the node status so
// the editor shows a heartbeat per message.
const accepted = Object.values(summary).filter((s) => s.ok).length;
const total = Object.keys(summary).length;
this.node.status({ fill: 'green', shape: 'dot',
text: `digital · ${accepted}/${total} ch updated` });
} else if (typeof msg.payload === 'number') {
// Helpful hint: the user probably configured the wrong mode.
this.source.logger?.warn(`digital mode received a number (${msg.payload}); expected an object like {key: value, ...}. Switch Input Mode to 'analog' in the editor or send an object payload.`);
} else {
this.source.logger?.warn(`digital mode expects an object payload; got ${typeof msg.payload}`);
}
} else {
if (typeof msg.payload === 'number' || (typeof msg.payload === 'string' && msg.payload.trim() !== '')) {
const parsed = Number(msg.payload);
if (!Number.isNaN(parsed)) {
this.source.inputValue = parsed;
} else {
this.source.logger?.warn(`Invalid numeric measurement payload: ${msg.payload}`);
}
} else if (msg.payload && typeof msg.payload === 'object' && !Array.isArray(msg.payload)) {
// Helpful hint: the payload is object-shaped but the node is
// configured analog. Most likely the user wanted digital mode.
const keys = Object.keys(msg.payload).slice(0, 3).join(', ');
this.source.logger?.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.`);
}
}
break;
default:
this.source.logger?.warn(`Unknown topic: ${msg.topic}`);
}
} catch (error) {
this.source.logger?.error(`Input handler failure: ${error.message}`);
}
if (typeof done === 'function') done();
});
}
/**
* Clean up timers and intervals when Node-RED stops the node.
*/
_attachCloseHandler() {
this.node.on('close', (done) => {
clearInterval(this._tickInterval);
//clearInterval(this._statusInterval);
if (typeof done === 'function') done();
});
} }
} }

View File

@@ -0,0 +1,60 @@
/**
* Simulator — random-walk driver for the measurement input.
*
* Lifted verbatim from Measurement.simulateInput. The orchestrator decides
* what to do with the returned value (originally written to `inputValue`),
* so this module owns nothing but the walk and its bounds.
*/
class Simulator {
constructor({ config, logger } = {}) {
if (!config || !config.scaling) {
throw new Error('Simulator requires { config.scaling }');
}
this.config = config;
this.logger = logger || { warn() {}, info() {}, debug() {}, error() {} };
const s = config.scaling;
this.inputRange = Math.abs(s.inputMax - s.inputMin);
this.processRange = Math.abs(s.absMax - s.absMin);
this.simValue = 0;
}
step() {
const s = this.config.scaling;
const sign = Math.random() < 0.5 ? -1 : 1;
let maxStep;
if (s.enabled) {
// Step size scales with the live input window; fall back to 1 so a
// collapsed range still wanders instead of freezing at zero.
maxStep = this.inputRange > 0 ? this.inputRange * 0.05 : 1;
if (this.simValue < s.inputMin || this.simValue > s.inputMax) {
this.logger.warn(`Simulated value ${this.simValue} is outside of input range constraining between min=${s.inputMin} and max=${s.inputMax}`);
this.simValue = _constrain(this.simValue, s.inputMin, s.inputMax);
}
} else {
maxStep = this.processRange > 0 ? this.processRange * 0.05 : 1;
if (this.simValue < s.absMin || this.simValue > s.absMax) {
this.logger.warn(`Simulated value ${this.simValue} is outside of abs range constraining between min=${s.absMin} and max=${s.absMax}`);
this.simValue = _constrain(this.simValue, s.absMin, s.absMax);
}
}
this.simValue += sign * Math.random() * maxStep;
return this.simValue;
}
reset() {
this.simValue = 0;
}
get current() {
return this.simValue;
}
}
function _constrain(v, lo, hi) {
return Math.min(Math.max(v, lo), hi);
}
module.exports = Simulator;

View File

@@ -1,93 +1,76 @@
const EventEmitter = require('events'); 'use strict';
const {logger,configUtils,configManager,MeasurementContainer} = require('generalFunctions');
const { BaseDomain, statusBadge } = require('generalFunctions');
const Channel = require('./channel'); const Channel = require('./channel');
const Simulator = require('./simulation/simulator');
const Calibrator = require('./calibration/calibrator');
/** // Measurement domain. Analog mode = one Channel built from the flat config.
* Measurement domain model. // Digital mode = one Channel per config.channels[] entry. Channel owns the
* // outlier → offset → scaling → smoothing → minMax → emit pipeline; the
* Supports two input modes: // delegates below preserve the pre-refactor public surface for tests.
* - `analog` (default): one scalar value per msg.payload. The node runs the class Measurement extends BaseDomain {
* classic offset / scaling / smoothing / outlier pipeline on it and emits static name = 'measurement';
* exactly one measurement into the MeasurementContainer. This is the
* original behaviour; every existing flow keeps working unchanged.
* - `digital`: msg.payload is an object with many key/value pairs (MQTT /
* IoT style). The node builds one Channel per config.channels entry and
* routes each key through its own mini-pipeline, emitting N measurements
* into the MeasurementContainer from a single input message.
*
* Mode is selected via `config.mode.current`. When no mode config is present
* or mode=analog, the node behaves identically to pre-digital releases.
*/
class Measurement {
constructor(config={}) {
this.emitter = new EventEmitter(); // Own EventEmitter configure() {
this.configManager = new configManager(); this.mode = (this.config?.mode?.current || 'analog').toLowerCase();
this.defaultConfig = this.configManager.getConfig('measurement'); this.channels = new Map();
this.configUtils = new configUtils(this.defaultConfig);
this.config = this.configUtils.initConfig(config);
// Init after config is set
this.logger = new logger(this.config.general.logging.enabled,this.config.general.logging.logLevel, this.config.general.name);
// General properties
this.measurements = new MeasurementContainer({
autoConvert: true,
windowSize: this.config.smoothing.smoothWindow
});
this.measurements.setChildId(this.config.general.id);
this.measurements.setChildName(this.config.general.name);
// Smoothing
this.storedValues = [];
// Simulation
this.simValue = 0;
// Internal tracking
this.inputValue = 0;
this.outputAbs = 0;
this.outputPercent = 0;
// Stability
this.stableThreshold = null;
//internal variables
this.totalMinValue = Infinity;
this.totalMaxValue = -Infinity;
this.totalMinSmooth = 0;
this.totalMaxSmooth = 0;
// Scaling
this.inputRange = Math.abs(this.config.scaling.inputMax - this.config.scaling.inputMin);
this.processRange = Math.abs(this.config.scaling.absMax - this.config.scaling.absMin);
// Mode + multi-channel (digital) support. Backward-compatible: when the
// config does not declare a mode, we fall back to 'analog' and behave
// exactly like the original single-channel node.
this.mode = (this.config.mode && typeof this.config.mode.current === 'string')
? this.config.mode.current.toLowerCase()
: 'analog';
this.channels = new Map(); // populated only in digital mode
if (this.mode === 'digital') { if (this.mode === 'digital') {
this._buildDigitalChannels(); this._buildDigitalChannels();
} else {
this.analogChannel = this._buildAnalogChannel();
} }
this.logger.debug(`Measurement id: ${this.config.general.id}, initialized successfully. mode=${this.mode} channels=${this.channels.size}`); this._simulator = new Simulator({ config: this.config, logger: this.logger });
this._calibrator = new Calibrator({
storedValuesRef: () => this.analogChannel?.storedValues ?? [],
configRef: () => this.config,
logger: this.logger,
});
this._inputValue = 0;
this.simValue = 0;
this._installChannelMirrors();
this.logger.debug(`Measurement id=${this.config.general.id} ready. mode=${this.mode} channels=${this.channels.size}`);
}
// Mirror the analog Channel's state as `m.xxx` so the legacy public surface
// (outputAbs, storedValues, totalMinValue, …) stays writable from tests.
_installChannelMirrors() {
const RW = ['storedValues', 'outputAbs', 'outputPercent', 'totalMinValue',
'totalMaxValue', 'totalMinSmooth', 'totalMaxSmooth'];
const RO = ['inputRange', 'processRange'];
const def = (k, setter) => Object.defineProperty(this, k, {
configurable: true, enumerable: true,
get: () => this.analogChannel?.[k] ?? (k === 'storedValues' ? [] : 0),
...(setter ? { set: setter } : {}),
});
for (const k of RW) def(k, (v) => { if (this.analogChannel) this.analogChannel[k] = (k === 'storedValues' && Array.isArray(v)) ? [...v] : v; });
for (const k of RO) def(k);
}
_buildAnalogChannel() {
return new Channel({
key: null,
type: this.config.asset.type,
position: this.config.functionality?.positionVsParent || 'atEquipment',
unit: this.config.asset?.unit || this.config.general?.unit || 'unitless',
distance: this.config.functionality?.distance ?? null,
scaling: this.config.scaling,
smoothing: this.config.smoothing,
outlierDetection: this.config.outlierDetection,
interpolation: this.config.interpolation,
measurements: this.measurements,
logger: this.logger,
});
} }
/**
* Build one Channel per entry in config.channels. Each Channel gets its
* own scaling / smoothing / outlier / position / unit contract; they share
* the parent MeasurementContainer so a downstream parent sees all channels
* via the same emitter.
*/
_buildDigitalChannels() { _buildDigitalChannels() {
const entries = Array.isArray(this.config.channels) ? this.config.channels : []; const entries = Array.isArray(this.config.channels) ? this.config.channels : [];
if (entries.length === 0) { if (entries.length === 0) {
this.logger.warn(`digital mode enabled but config.channels is empty; no channels will be emitted.`); this.logger.warn('digital mode enabled but config.channels is empty; no channels will be emitted.');
return; return;
} }
for (const raw of entries) { for (const raw of entries) {
@@ -113,13 +96,8 @@ class Measurement {
this.logger.info(`digital mode: built ${this.channels.size} channel(s) from config.channels`); this.logger.info(`digital mode: built ${this.channels.size} channel(s) from config.channels`);
} }
/** // --- digital passthrough ---
* Digital mode entry point. Iterate the object payload, look up each key
* in the channel map, and run the configured pipeline per channel. Keys
* that are not mapped are logged once per call and ignored.
* @param {object} payload - e.g. { temperature: 21.5, humidity: 45.2 }
* @returns {object} summary of updated channels (for diagnostics)
*/
handleDigitalPayload(payload) { handleDigitalPayload(payload) {
if (this.mode !== 'digital') { if (this.mode !== 'digital') {
this.logger.warn(`handleDigitalPayload called while mode=${this.mode}. Ignoring.`); this.logger.warn(`handleDigitalPayload called while mode=${this.mode}. Ignoring.`);
@@ -133,10 +111,7 @@ class Measurement {
const unknown = []; const unknown = [];
for (const [key, raw] of Object.entries(payload)) { for (const [key, raw] of Object.entries(payload)) {
const channel = this.channels.get(key); const channel = this.channels.get(key);
if (!channel) { if (!channel) { unknown.push(key); continue; }
unknown.push(key);
continue;
}
const v = Number(raw); const v = Number(raw);
if (!Number.isFinite(v)) { if (!Number.isFinite(v)) {
this.logger.warn(`digital channel '${key}' received non-numeric value: ${raw}`); this.logger.warn(`digital channel '${key}' received non-numeric value: ${raw}`);
@@ -146,571 +121,118 @@ class Measurement {
const ok = channel.update(v); const ok = channel.update(v);
summary[key] = { ok, mAbs: channel.outputAbs, mPercent: channel.outputPercent }; summary[key] = { ok, mAbs: channel.outputAbs, mPercent: channel.outputPercent };
} }
if (unknown.length) { if (unknown.length) this.logger.debug(`digital payload contained unmapped keys: ${unknown.join(', ')}`);
this.logger.debug(`digital payload contained unmapped keys: ${unknown.join(', ')}`);
}
return summary; return summary;
} }
/**
* Return per-channel output snapshots. In analog mode this is the same
* getOutput() contract; in digital mode it returns one snapshot per
* channel under a `channels` key so the tick output stays JSON-shaped.
*/
getDigitalOutput() { getDigitalOutput() {
const out = { channels: {} }; const out = { channels: {} };
for (const [key, ch] of this.channels) { for (const [key, ch] of this.channels) out.channels[key] = ch.getOutput();
out.channels[key] = ch.getOutput();
}
return out; return out;
} }
// -------- Config Initializers -------- // // --- public commands ---
updateconfig(newConfig) {
this.config = this.configUtils.updateConfig(this.config, newConfig);
}
async tick() { set inputValue(v) {
if (this.config.simulation.enabled) { this._inputValue = v;
this.simulateInput(); if (this.mode === 'analog' && this.analogChannel) {
this.analogChannel.update(v);
this.notifyOutputChanged();
} }
}
get inputValue() { return this._inputValue ?? 0; }
this.calculateInput(this.inputValue); tick() {
if (this.config?.simulation?.enabled) {
this.inputValue = this._simulator.step();
this.simValue = this._simulator.simValue;
}
return Promise.resolve(); return Promise.resolve();
} }
calibrate() { toggleSimulation() {
this.config.simulation = this.config.simulation || {};
let offset = 0;
const { isStable } = this.isStable();
//first check if the input is stable
if( !isStable ){
this.logger.warn(`Large fluctuations detected between stored values. Calibration aborted.`);
}else{
this.logger.info(`Stable input value detected. Proceeding with calibration.`);
// offset should be the difference between the input and the output
if(this.config.scaling.enabled){
offset = this.config.scaling.inputMin - this.outputAbs;
} else {
offset = this.config.scaling.absMin - this.outputAbs;
}
this.config.scaling.offset = offset;
this.logger.info(`Calibration completed. Offset set to ${offset}`);
}
}
isStable() {
const marginFactor = 2; // or 3, depending on strictness
let stableThreshold = 0;
if (this.storedValues.length < 2) return false;
const stdDev = this.standardDeviation(this.storedValues);
stableThreshold = stdDev * marginFactor;
return { isStable: ( stdDev < stableThreshold || stdDev == 0) , stdDev} ;
}
evaluateRepeatability() {
const { isStable, stdDev } = this.isStable();
if(this.config.smoothing.smoothMethod == 'none'){
this.logger.warn('Repeatability evaluation is not possible without smoothing.');
return null;
}
if (this.storedValues.length < 2) {
this.logger.warn('Not enough data to evaluate repeatability.');
return null;
}
if( isStable == false){
this.logger.warn('Data not stable enough to evaluate repeatability.');
return null;
}
const standardDeviation = stdDev
this.logger.info(`Repeatability evaluated. Standard Deviation: ${stdDev}`);
return standardDeviation;
}
simulateInput() {
// Simulate input value
const absMax = this.config.scaling.absMax;
const absMin = this.config.scaling.absMin;
const inputMin = this.config.scaling.inputMin;
const inputMax = this.config.scaling.inputMax;
const sign = Math.random() < 0.5 ? -1 : 1;
let maxStep = 0;
switch ( this.config.scaling.enabled ) {
case true:
maxStep = this.inputRange > 0 ? this.inputRange * 0.05 : 1;
if (this.simValue < inputMin || this.simValue > inputMax) {
this.logger.warn(`Simulated value ${this.simValue} is outside of input range constraining between min=${inputMin} and max=${inputMax}`);
this.simValue = this.constrain(this.simValue, inputMin, inputMax);
}
break;
case false:
maxStep = this.processRange > 0 ? this.processRange * 0.05 : 1;
if (this.simValue < absMin || this.simValue > absMax) {
this.logger.warn(`Simulated value ${this.simValue} is outside of abs range constraining between min=${absMin} and max=${absMax}`);
this.simValue = this.constrain(this.simValue, absMin, absMax);
}
break;
}
this.simValue += sign * Math.random() * maxStep;
this.inputValue = this.simValue;
}
outlierDetection(val) {
if (this.storedValues.length < 2) return false;
// Config enum values are normalized to lowercase by validateEnum in
// generalFunctions, so dispatch on the lowercase form to keep this
// tolerant of both legacy (camelCase) and normalized (lowercase) config.
const raw = this.config.outlierDetection.method;
const method = typeof raw === 'string' ? raw.toLowerCase() : raw;
this.logger.debug(`Outlier detection method: ${method}`);
switch (method) {
case 'zscore':
return this.zScoreOutlierDetection(val);
case 'iqr':
return this.iqrOutlierDetection(val);
case 'modifiedzscore':
return this.modifiedZScoreOutlierDetection(val);
default:
this.logger.warn(`Outlier detection method "${raw}" is not recognized.`);
return false;
}
}
zScoreOutlierDetection(val) {
const threshold = this.config.outlierDetection.threshold || 3;
const mean = this.mean(this.storedValues);
const stdDev = this.standardDeviation(this.storedValues);
const zScore = (val - mean) / stdDev;
if (Math.abs(zScore) > threshold) {
this.logger.warn(`Outlier detected using Z-Score method. Z-score=${zScore}`);
return true;
}
return false;
}
iqrOutlierDetection(val) {
const sortedValues = [...this.storedValues].sort((a, b) => a - b);
const q1 = sortedValues[Math.floor(sortedValues.length / 4)];
const q3 = sortedValues[Math.floor(sortedValues.length * 3 / 4)];
const iqr = q3 - q1;
const lowerBound = q1 - 1.5 * iqr;
const upperBound = q3 + 1.5 * iqr;
if (val < lowerBound || val > upperBound) {
this.logger.warn(`Outlier detected using IQR method. Value=${val}`);
return true;
}
return false;
}
modifiedZScoreOutlierDetection(val) {
const median = this.medianFilter(this.storedValues);
const mad = this.medianFilter(this.storedValues.map(v => Math.abs(v - median)));
const modifiedZScore = 0.6745 * (val - median) / mad;
const threshold = this.config.outlierDetection.threshold || 3.5;
if (Math.abs(modifiedZScore) > threshold) {
this.logger.warn(`Outlier detected using Modified Z-Score method. Modified Z-Score=${modifiedZScore}`);
return true;
}
return false;
}
calculateInput(value) {
// Check if the value is an outlier and check if outlier detection is enabled
if (this.config.outlierDetection.enabled) {
if ( this.outlierDetection(value) ){
this.logger.warn(`Outlier detected. Ignoring value=${value}`);
return;
}
}
// Apply offset
let val = this.applyOffset(value);
// Track raw min/max
this.updateMinMaxValues(val);
// Handle scaling if enabled
if (this.config.scaling.enabled) {
val = this.handleScaling(val);
}
// Apply smoothing
const smoothed = this.applySmoothing(val);
// Update smoothed min/max and output
this.updateSmoothMinMaxValues(smoothed);
this.updateOutputAbs(smoothed);
}
applyOffset(value) {
return value + this.config.scaling.offset;
}
handleScaling(value) {
// Check if input range is valid
if (this.inputRange <= 0) {
this.logger.warn(`Input range is invalid. Falling back to default range [0, 1].`);
this.config.scaling.inputMin = 0;
this.config.scaling.inputMax = 1;
this.inputRange = this.config.scaling.inputMax - this.config.scaling.inputMin;
}
// Constrain value within input range
if (value < this.config.scaling.inputMin || value > this.config.scaling.inputMax) {
this.logger.warn(`Value=${value} is outside of INPUT range. Constraining.`);
value = this.constrain(value, this.config.scaling.inputMin, this.config.scaling.inputMax);
}
// Interpolate value
this.logger.debug(`Interpolating value=${value} between min=${this.config.scaling.inputMin} and max=${this.config.scaling.inputMax} to absMin=${this.config.scaling.absMin} and absMax=${this.config.scaling.absMax}`);
return this.interpolateLinear(value, this.config.scaling.inputMin, this.config.scaling.inputMax, this.config.scaling.absMin, this.config.scaling.absMax);
}
constrain(input, inputMin , inputMax) {
this.logger.warn(`New value=${input} is constrained to fit between min=${inputMin} and max=${inputMax}`);
return Math.min(Math.max(input, inputMin), inputMax);
}
interpolateLinear(iNumber, iMin, iMax, oMin, oMax) {
if (iMin >= iMax || oMin >= oMax) {
this.logger.warn(`Invalid input for linear interpolation iMin=${JSON.stringify(iMin)} iMax=${iMax} oMin=${JSON.stringify(oMin)} oMax=${oMax}`);
return iNumber;
}
const range = iMax - iMin;
return oMin + ((iNumber - iMin) * (oMax - oMin)) / range;
}
applySmoothing(value) {
this.storedValues.push(value);
// Maintain only the latest 'smoothWindow' number of values
if (this.storedValues.length > this.config.smoothing.smoothWindow) {
this.storedValues.shift();
}
// Smoothing strategies keyed by the normalized (lowercase) method name.
// validateEnum in generalFunctions lowercases enum values, so dispatch on
// the lowercase form to accept both legacy (camelCase) and normalized
// (lowercase) config values.
const smoothingMethods = {
none: (arr) => arr[arr.length - 1],
mean: (arr) => this.mean(arr),
min: (arr) => this.min(arr),
max: (arr) => this.max(arr),
sd: (arr) => this.standardDeviation(arr),
lowpass: (arr) => this.lowPassFilter(arr),
highpass: (arr) => this.highPassFilter(arr),
weightedmovingaverage: (arr) => this.weightedMovingAverage(arr),
bandpass: (arr) => this.bandPassFilter(arr),
median: (arr) => this.medianFilter(arr),
kalman: (arr) => this.kalmanFilter(arr),
savitzkygolay: (arr) => this.savitzkyGolayFilter(arr),
};
const raw = this.config.smoothing.smoothMethod;
const method = typeof raw === 'string' ? raw.toLowerCase() : raw;
this.logger.debug(`Applying smoothing method "${method}"`);
if (!smoothingMethods[method]) {
this.logger.error(`Smoothing method "${raw}" is not implemented.`);
return value;
}
// Apply the smoothing method
return smoothingMethods[method](this.storedValues);
}
standardDeviation(values) {
if (values.length <= 1) return 0;
const mean = values.reduce((a, b) => a + b, 0) / values.length;
const sqDiffs = values.map(v => (v - mean) ** 2);
const variance = sqDiffs.reduce((a, b) => a + b, 0) / (values.length - 1);
return Math.sqrt(variance);
}
savitzkyGolayFilter(arr) {
const coefficients = [-3, 12, 17, 12, -3]; // Example coefficients for 5-point smoothing
const normFactor = coefficients.reduce((a, b) => a + b, 0);
if (arr.length < coefficients.length) {
return arr[arr.length - 1]; // Return last value if array is too small
}
let smoothed = 0;
for (let i = 0; i < coefficients.length; i++) {
smoothed += arr[arr.length - coefficients.length + i] * coefficients[i];
}
return smoothed / normFactor;
}
kalmanFilter(arr) {
let estimate = arr[0];
const measurementNoise = 1; // Adjust based on your sensor's characteristics
const processNoise = 0.1; // Adjust based on signal variability
const kalmanGain = processNoise / (processNoise + measurementNoise);
for (let i = 1; i < arr.length; i++) {
estimate = estimate + kalmanGain * (arr[i] - estimate);
}
return estimate;
}
medianFilter(arr) {
const sorted = [...arr].sort((a, b) => a - b);
const middle = Math.floor(sorted.length / 2);
return sorted.length % 2 !== 0
? sorted[middle]
: (sorted[middle - 1] + sorted[middle]) / 2;
}
bandPassFilter(arr) {
const lowPass = this.lowPassFilter(arr); // Apply low-pass filter
const highPass = this.highPassFilter(arr); // Apply high-pass filter
return arr.map((val, _idx) => lowPass + highPass - val).pop(); // Combine the filters
}
weightedMovingAverage(arr) {
const weights = arr.map((_, i) => i + 1); // Weights increase linearly
const weightedSum = arr.reduce((sum, val, idx) => sum + val * weights[idx], 0);
const weightTotal = weights.reduce((sum, weight) => sum + weight, 0);
return weightedSum / weightTotal;
}
highPassFilter(arr) {
const alpha = 0.8; // Smoothing factor (0 < alpha <= 1)
let filteredValues = [];
filteredValues[0] = arr[0];
for (let i = 1; i < arr.length; i++) {
filteredValues[i] = alpha * (filteredValues[i - 1] + arr[i] - arr[i - 1]);
}
return filteredValues[filteredValues.length - 1];
}
lowPassFilter(arr) {
const alpha = 0.2; // Smoothing factor (0 < alpha <= 1)
let smoothedValue = arr[0];
for (let i = 1; i < arr.length; i++) {
smoothedValue = alpha * arr[i] + (1 - alpha) * smoothedValue;
}
return smoothedValue;
}
// Or also EMA called exponential moving average
recursiveLowpassFilter() {
}
mean(arr) {
return arr.reduce((a, b) => a + b, 0) / arr.length;
}
min(arr) {
return Math.min(...arr);
}
max(arr) {
return Math.max(...arr);
}
updateMinMaxValues(value) {
if (value < this.totalMinValue) {
this.totalMinValue = value;
}
if (value > this.totalMaxValue) {
this.totalMaxValue = value;
}
}
updateSmoothMinMaxValues(value) {
// If this is the first run, initialize them
if (this.totalMinSmooth === 0 && this.totalMaxSmooth === 0) {
this.totalMinSmooth = value;
this.totalMaxSmooth = value;
}
if (value < this.totalMinSmooth) {
this.totalMinSmooth = value;
}
if (value > this.totalMaxSmooth) {
this.totalMaxSmooth = value;
}
}
updateOutputAbs(val) {
// Constrain first, then check for changes
let constrainedVal = val;
if (val < this.config.scaling.absMin || val > this.config.scaling.absMax) {
this.logger.warn(`Output value=${val} is outside of ABS range. Constraining.`);
constrainedVal = this.constrain(val, this.config.scaling.absMin, this.config.scaling.absMax);
}
const roundedVal = Math.round(constrainedVal * 100) / 100;
//only update on change
if (roundedVal != this.outputAbs) {
// Constrain value within process range
if (val < this.config.scaling.absMin || val > this.config.scaling.absMax) {
this.logger.warn(`Output value=${val} is outside of ABS range. Constraining.`);
val = this.constrain(val, this.config.scaling.absMin, this.config.scaling.absMax);
}
this.outputAbs = Math.round(val * 100) / 100;
this.outputPercent = this.updateOutputPercent(val);
this.emitter.emit('mAbs', this.outputAbs);// DEPRECATED: Use measurements container instead
this.logger.debug(`Updating type: ${this.config.asset.type}, variant: ${"measured"}, postition : ${this.config.functionality.positionVsParent} container with new value: ${this.outputAbs}`);
this.measurements.type(this.config.asset.type).variant("measured").position(this.config.functionality.positionVsParent).distance(this.config.functionality.distance).value(this.outputAbs, Date.now(),this.config.asset.unit );
}
}
updateOutputPercent(value) {
let outputPercent;
if (this.processRange <= 0) {
this.logger.debug(`Process range is smaller or equal to 0 interpolating between input range`);
outputPercent = this.interpolateLinear( value, this.totalMinValue, this.totalMaxValue, this.config.interpolation.percentMin, this.config.interpolation.percentMax );
}
else {
outputPercent = this.interpolateLinear( value, this.config.scaling.absMin, this.config.scaling.absMax, this.config.interpolation.percentMin, this.config.interpolation.percentMax );
}
return Math.round(outputPercent * 100) / 100;
}
toggleSimulation(){
this.config.simulation.enabled = !this.config.simulation.enabled; this.config.simulation.enabled = !this.config.simulation.enabled;
} }
toggleOutlierDetection() { toggleOutlierDetection() {
// Keep the outlier configuration shape stable and only toggle the enabled flag.
const currentState = Boolean(this.config?.outlierDetection?.enabled);
this.config.outlierDetection = this.config.outlierDetection || {}; this.config.outlierDetection = this.config.outlierDetection || {};
this.config.outlierDetection.enabled = !currentState; this.config.outlierDetection.enabled = !Boolean(this.config.outlierDetection.enabled);
if (this.analogChannel) this.analogChannel.outlierDetection.enabled = this.config.outlierDetection.enabled;
} }
calibrate() {
const result = this._calibrator.calibrate(this.analogChannel?.outputAbs ?? 0);
if (result && typeof result.offset === 'number') {
this.config.scaling.offset = result.offset;
if (this.analogChannel) this.analogChannel.scaling.offset = result.offset;
}
}
// Legacy shape: <2 samples returns bare `false`; otherwise the
// {isStable, stdDev} object the calibrator produces.
isStable() {
if ((this.storedValues?.length ?? 0) < 2) return false;
return this._calibrator.isStable();
}
evaluateRepeatability() {
const { repeatability } = this._calibrator.evaluateRepeatability();
return repeatability;
}
// --- analog pipeline delegates (preserved for tests + back-compat) ---
calculateInput(value) {
if (!this.analogChannel) return;
this.analogChannel.update(value);
this.notifyOutputChanged();
}
applyOffset(value) { return value + (this.config.scaling?.offset ?? 0); }
constrain(v, lo, hi) { return Math.min(Math.max(v, lo), hi); }
interpolateLinear(n, iMin, iMax, oMin, oMax) {
if (iMin >= iMax || oMin >= oMax) return n;
return oMin + ((n - iMin) * (oMax - oMin)) / (iMax - iMin);
}
handleScaling(value) {
if (!this.analogChannel) return value;
const out = this.analogChannel._applyScaling(value);
// Channel mutates its own scaling copy when inputRange is invalid;
// mirror that back to config.scaling so the legacy contract holds.
this.config.scaling.inputMin = this.analogChannel.scaling.inputMin;
this.config.scaling.inputMax = this.analogChannel.scaling.inputMax;
return out;
}
outlierDetection(value) {
if (!this.analogChannel) return false;
// Channel skips outlier checks when disabled; the legacy test API expects
// the check to run regardless of the enabled flag.
return this.analogChannel._isOutlier(value);
}
updateOutputPercent(value) { return this.analogChannel?._computePercent(value) ?? 0; }
// --- output / status ---
getOutput() { getOutput() {
if (this.mode === 'digital') return this.getDigitalOutput();
return { return {
mAbs: this.outputAbs, mAbs: this.outputAbs,
mPercent: this.outputPercent, mPercent: this.outputPercent,
totalMinValue: this.totalMinValue, totalMinValue: this.totalMinValue === Infinity ? 0 : this.totalMinValue,
totalMaxValue: this.totalMaxValue, totalMaxValue: this.totalMaxValue === -Infinity ? 0 : this.totalMaxValue,
totalMinSmooth: this.totalMinSmooth, totalMinSmooth: this.totalMinSmooth,
totalMaxSmooth: this.totalMaxSmooth, totalMaxSmooth: this.totalMaxSmooth,
}; };
} }
getStatusBadge() {
if (this.mode === 'digital') {
return statusBadge.compose([`digital · ${this.channels.size} channel(s)`], { fill: 'blue', shape: 'ring' });
}
const unit = this.config?.general?.unit || '';
return statusBadge.compose([`${this.outputAbs} ${unit}`.trim()], { fill: 'green', shape: 'dot' });
}
} }
module.exports = Measurement; module.exports = Measurement;
/*
// Testing the class
const configuration = {
general: {
name: "PT1",
logging: {
enabled: true,
logLevel: "debug",
},
},
scaling:{
enabled: true,
inputMin: 0,
inputMax: 3000,
absMin: 500,
absMax: 4000,
offset: 1000
},
asset: {
type: "pressure",
unit: "bar",
category: "measurement",
model: "PT1",
uuid: "123e4567-e89b-12d3-a456-426614174000",
tagCode: "PT1-001",
supplier: "DeltaTech"
},
smoothing: {
smoothWindow: 10,
smoothMethod: 'mean',
},
simulation: {
enabled: true,
},
functionality: {
positionVsParent: POSITIONS.UPSTREAM
}
};
const m = new Measurement(configuration);
m.logger.info(`Measurement created with config : ${JSON.stringify(m.config)}`);
m.logger.setLogLevel("debug");
//look for flow updates
m.measurements.emitter.on('pressure.measured.upstream', (newVal) => {
m.logger.info(`Received : ${newVal.value} ${newVal.unit}`);
const repeatability = m.evaluateRepeatability();
if (repeatability !== null) {
m.logger.info(`Current repeatability (standard deviation): ${repeatability}`);
}
});
const tickLoop = setInterval(changeInput,1000);
function changeInput(){
m.logger.info(`tick...`);
m.tick();
//m.inputValue = 5;
}
// */

View File

@@ -0,0 +1,156 @@
'use strict';
const test = require('node:test');
const assert = require('node:assert');
const Calibrator = require('../../src/calibration/calibrator.js');
// Tiny logger spy so we can assert on warn() without pulling in the real
// generalFunctions logger.
function makeLogger() {
const calls = { warn: [], info: [], debug: [], error: [] };
return {
calls,
warn: (m) => calls.warn.push(m),
info: (m) => calls.info.push(m),
debug: (m) => calls.debug.push(m),
error: (m) => calls.error.push(m),
};
}
function makeCalibrator(values, config) {
const logger = makeLogger();
const cal = new Calibrator({
storedValuesRef: () => values,
configRef: () => config,
logger,
});
return { cal, logger };
}
test('isStable: constant array → stable with stdDev=0', () => {
const { cal } = makeCalibrator([5, 5, 5, 5], {});
const r = cal.isStable();
assert.strictEqual(r.isStable, true);
assert.strictEqual(r.stdDev, 0);
});
test('isStable: high-variance array under default threshold → unstable', () => {
// Resolved 2026-05-11: config-driven absolute stabilityThreshold replaces
// the old `stdDev < stdDev*marginFactor` tautology. Default threshold is
// 0.01 (scaling-units); a 0..100 spread blows past it.
const { cal } = makeCalibrator([0, 100, 0, 100], {});
const r = cal.isStable();
assert.strictEqual(r.isStable, false);
assert.ok(r.stdDev > 0);
});
test('isStable: high-variance array with relaxed threshold → stable', () => {
const cfg = { calibration: { stabilityThreshold: 100 } };
const { cal } = makeCalibrator([0, 100, 0, 100], cfg);
const r = cal.isStable();
assert.strictEqual(r.isStable, true);
assert.ok(r.stdDev > 0);
});
test('isStable: zero stdDev (constant) is stable regardless of threshold', () => {
const cfg = { calibration: { stabilityThreshold: 0 } };
const { cal } = makeCalibrator([7, 7, 7, 7], cfg);
const r = cal.isStable();
assert.strictEqual(r.isStable, true);
assert.strictEqual(r.stdDev, 0);
});
test('isStable: stdDev just above threshold → unstable', () => {
const cfg = { calibration: { stabilityThreshold: 0.5 } };
// stdDev of [10, 11] = 0.5; nudge the spread up so stdDev > 0.5.
const { cal } = makeCalibrator([10, 12], cfg);
const r = cal.isStable();
assert.strictEqual(r.isStable, false);
assert.ok(r.stdDev > 0.5);
});
test('isStable: missing config.calibration → falls back to default 0.01', () => {
// stdDev of [10, 10.001] ≈ 0.0005, well under the 0.01 default.
const { cal: stable } = makeCalibrator([10, 10.001], {});
assert.strictEqual(stable.isStable().isStable, true);
// stdDev of [10, 10.1] ≈ 0.05, above the 0.01 default.
const { cal: unstable } = makeCalibrator([10, 10.1], {});
assert.strictEqual(unstable.isStable().isStable, false);
});
test('isStable: < 2 values → unstable', () => {
const { cal } = makeCalibrator([42], {});
const r = cal.isStable();
assert.strictEqual(r.isStable, false);
assert.strictEqual(r.stdDev, 0);
});
test('calibrate: scaling enabled → offset = inputMin - currentOutputAbs', () => {
const cfg = { scaling: { enabled: true, inputMin: 4, absMin: 0 } };
const { cal } = makeCalibrator([10, 10, 10], cfg);
const r = cal.calibrate(10);
assert.deepStrictEqual(r, { offset: -6 });
});
test('calibrate: scaling disabled → offset = absMin - currentOutputAbs', () => {
const cfg = { scaling: { enabled: false, inputMin: 4, absMin: 1 } };
const { cal } = makeCalibrator([7, 7, 7], cfg);
const r = cal.calibrate(7);
assert.deepStrictEqual(r, { offset: -6 });
});
test('calibrate: not stable (length<2) → returns null and logs warn', () => {
// Original rule has a tautological threshold, so "unstable" only triggers
// when the rolling window has < 2 samples.
const cfg = { scaling: { enabled: true, inputMin: 0, absMin: 0 } };
const { cal, logger } = makeCalibrator([], cfg);
const r = cal.calibrate(50);
assert.strictEqual(r, null);
assert.strictEqual(logger.calls.warn.length, 1);
assert.match(logger.calls.warn[0], /Calibration aborted/);
});
test('evaluateRepeatability: smoothing=none → null', () => {
const cfg = { smoothing: { smoothMethod: 'none' } };
const { cal, logger } = makeCalibrator([5, 5, 5], cfg);
const r = cal.evaluateRepeatability();
assert.strictEqual(r.repeatability, null);
assert.strictEqual(r.reason, 'smoothing-disabled');
assert.match(logger.calls.warn[0], /without smoothing/);
});
test('evaluateRepeatability: stable + smoothed → returns stdDev', () => {
const cfg = { smoothing: { smoothMethod: 'mean' } };
const { cal } = makeCalibrator([3, 3, 3, 3], cfg);
const r = cal.evaluateRepeatability();
assert.strictEqual(r.repeatability, 0);
});
test('evaluateRepeatability: insufficient data → null', () => {
const cfg = { smoothing: { smoothMethod: 'mean' } };
const { cal } = makeCalibrator([5], cfg);
const r = cal.evaluateRepeatability();
assert.strictEqual(r.repeatability, null);
assert.strictEqual(r.reason, 'insufficient-data');
});
test('evaluateRepeatability: high-variance under default threshold → null', () => {
// Resolved 2026-05-11: with the real stability check in place, a noisy
// buffer fails isStable() and repeatability reports null with reason.
const cfg = { smoothing: { smoothMethod: 'mean' } };
const { cal, logger } = makeCalibrator([0, 50, 0, 50], cfg);
const r = cal.evaluateRepeatability();
assert.strictEqual(r.repeatability, null);
assert.strictEqual(r.reason, 'unstable');
assert.match(logger.calls.warn[0], /not stable/);
});
test('evaluateRepeatability: high-variance with relaxed threshold → returns stdDev', () => {
const cfg = {
smoothing: { smoothMethod: 'mean' },
calibration: { stabilityThreshold: 100 },
};
const { cal } = makeCalibrator([0, 50, 0, 50], cfg);
const r = cal.evaluateRepeatability();
assert.ok(r.repeatability > 0);
});

View File

@@ -0,0 +1,168 @@
// Basic tests for the measurement commands registry.
// Run with: node --test test/basic/commands.basic.test.js
'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const { createRegistry } = require('generalFunctions');
const commands = require('../../src/commands');
// --- helpers ---------------------------------------------------------------
function makeLogger() {
const calls = { warn: [], error: [], info: [], debug: [] };
return {
calls,
warn: (m) => calls.warn.push(String(m)),
error: (m) => calls.error.push(String(m)),
info: (m) => calls.info.push(String(m)),
debug: (m) => calls.debug.push(String(m)),
};
}
function makeSource({ mode = 'analog', simulator = false, outlier = false } = {}) {
const calls = {
toggleSimulation: 0,
toggleOutlierDetection: 0,
calibrate: 0,
handleDigitalPayload: [],
inputValueSets: [],
};
const state = { simulator, outlier, _inputValue: 0 };
const source = {
mode,
logger: makeLogger(),
toggleSimulation: () => { state.simulator = !state.simulator; calls.toggleSimulation += 1; },
toggleOutlierDetection: () => { state.outlier = !state.outlier; calls.toggleOutlierDetection += 1; },
calibrate: () => { calls.calibrate += 1; },
handleDigitalPayload: (p) => { calls.handleDigitalPayload.push(p); return { ok: true }; },
get inputValue() { return state._inputValue; },
set inputValue(v) { state._inputValue = v; calls.inputValueSets.push(v); },
};
return { source, calls, state };
}
function makeCtx({ logger = makeLogger() } = {}) {
return { logger, RED: { nodes: { getNode: () => undefined } }, node: {}, send: () => {} };
}
function makeRegistry(logger) {
return createRegistry(commands, { logger });
}
// --- tests -----------------------------------------------------------------
test('canonical topics dispatch to the right handler', async () => {
const { source, calls, state } = makeSource();
const reg = makeRegistry(makeLogger());
await reg.dispatch({ topic: 'set.simulator' }, source, makeCtx());
assert.equal(calls.toggleSimulation, 1);
assert.equal(state.simulator, true);
await reg.dispatch({ topic: 'set.outlier-detection' }, source, makeCtx());
assert.equal(calls.toggleOutlierDetection, 1);
assert.equal(state.outlier, true);
await reg.dispatch({ topic: 'cmd.calibrate' }, source, makeCtx());
assert.equal(calls.calibrate, 1);
});
test('aliases dispatch to the same handler and log a one-time deprecation', async () => {
const { source, calls } = makeSource();
const ctxLogger = makeLogger();
const reg = makeRegistry(ctxLogger);
for (const alias of ['simulator', 'outlierDetection', 'calibrate', 'measurement']) {
await reg.dispatch({ topic: alias, payload: 1 }, source, makeCtx({ logger: ctxLogger }));
await reg.dispatch({ topic: alias, payload: 2 }, source, makeCtx({ logger: ctxLogger }));
}
for (const alias of ['simulator', 'outlierDetection', 'calibrate', 'measurement']) {
const hits = ctxLogger.calls.warn.filter((m) => m.includes(`'${alias}' is deprecated`));
assert.equal(hits.length, 1, `alias '${alias}' should warn exactly once`);
}
// sanity: side-effects fired twice per alias.
assert.equal(calls.toggleSimulation, 2);
assert.equal(calls.toggleOutlierDetection, 2);
assert.equal(calls.calibrate, 2);
// analog measurement alias with numeric payload set inputValue twice.
assert.deepEqual(calls.inputValueSets, [1, 2]);
});
test('data.measurement analog with numeric payload sets source.inputValue', async () => {
const { source, calls } = makeSource({ mode: 'analog' });
const reg = makeRegistry(makeLogger());
await reg.dispatch({ topic: 'data.measurement', payload: 42 }, source, makeCtx());
await reg.dispatch({ topic: 'data.measurement', payload: '3.5' }, source, makeCtx());
assert.deepEqual(calls.inputValueSets, [42, 3.5]);
});
test('data.measurement analog with object payload logs helpful switch-mode warn', async () => {
const { source, calls } = makeSource({ mode: 'analog' });
const ctxLogger = makeLogger();
const reg = makeRegistry(ctxLogger);
await reg.dispatch(
{ topic: 'data.measurement', payload: { temperature: 21.5, humidity: 45 } },
source,
makeCtx({ logger: ctxLogger })
);
assert.equal(calls.inputValueSets.length, 0);
assert.equal(calls.handleDigitalPayload.length, 0);
assert.ok(
ctxLogger.calls.warn.some((m) => m.includes('analog mode') && m.includes('digital')),
`expected helpful switch-to-digital warn, got: ${JSON.stringify(ctxLogger.calls.warn)}`
);
});
test('data.measurement digital with object payload calls handleDigitalPayload', async () => {
const { source, calls } = makeSource({ mode: 'digital' });
const reg = makeRegistry(makeLogger());
const payload = { tempA: 21.5, tempB: 19.8 };
await reg.dispatch({ topic: 'data.measurement', payload }, source, makeCtx());
assert.equal(calls.handleDigitalPayload.length, 1);
assert.deepEqual(calls.handleDigitalPayload[0], payload);
assert.equal(calls.inputValueSets.length, 0);
});
test('data.measurement digital with number logs helpful switch-mode warn', async () => {
const { source, calls } = makeSource({ mode: 'digital' });
const ctxLogger = makeLogger();
const reg = makeRegistry(ctxLogger);
await reg.dispatch(
{ topic: 'data.measurement', payload: 7 },
source,
makeCtx({ logger: ctxLogger })
);
assert.equal(calls.handleDigitalPayload.length, 0);
assert.equal(calls.inputValueSets.length, 0);
assert.ok(
ctxLogger.calls.warn.some((m) => m.includes('digital mode') && m.includes('analog')),
`expected helpful switch-to-analog warn, got: ${JSON.stringify(ctxLogger.calls.warn)}`
);
});
test('set.simulator toggles even with no payload (idempotent flip)', async () => {
const { source, calls, state } = makeSource({ simulator: false });
const reg = makeRegistry(makeLogger());
await reg.dispatch({ topic: 'set.simulator' }, source, makeCtx());
assert.equal(state.simulator, true);
await reg.dispatch({ topic: 'set.simulator' }, source, makeCtx());
assert.equal(state.simulator, false);
await reg.dispatch({ topic: 'set.simulator' }, source, makeCtx());
assert.equal(state.simulator, true);
assert.equal(calls.toggleSimulation, 3);
});

View File

@@ -2,29 +2,40 @@ const test = require('node:test');
const assert = require('node:assert/strict'); const assert = require('node:assert/strict');
const NodeClass = require('../../src/nodeClass'); const NodeClass = require('../../src/nodeClass');
const commands = require('../../src/commands');
const { createRegistry } = require('generalFunctions');
const { makeNodeStub, makeREDStub } = require('../helpers/factories'); const { makeNodeStub, makeREDStub } = require('../helpers/factories');
test('_attachInputHandler routes known topics to source methods', () => { // These tests pinned the old private methods (_attachInputHandler /
// _registerChild) on the pre-refactor nodeClass. After the BaseNodeAdapter
// migration the same wiring is provided by the base class, but we still
// exercise it from a prototype-derived instance to keep the surface covered
// without booting a full Node-RED runtime.
test('input handler dispatches known topics to source methods', async () => {
const inst = Object.create(NodeClass.prototype); const inst = Object.create(NodeClass.prototype);
const node = makeNodeStub(); const node = makeNodeStub();
const calls = []; const calls = [];
const source = {
inst.node = node; mode: 'analog',
inst.RED = makeREDStub(); logger: { warn: () => {}, info: () => {}, debug: () => {}, error: () => {} },
inst.source = {
toggleSimulation() { calls.push('simulator'); }, toggleSimulation() { calls.push('simulator'); },
toggleOutlierDetection() { calls.push('outlierDetection'); }, toggleOutlierDetection() { calls.push('outlierDetection'); },
calibrate() { calls.push('calibrate'); }, calibrate() { calls.push('calibrate'); },
set inputValue(v) { calls.push(['measurement', v]); }, set inputValue(v) { calls.push(['measurement', v]); },
}; };
inst.node = node;
inst.RED = makeREDStub();
inst.source = source;
inst._commands = createRegistry(commands, { logger: source.logger });
inst._attachInputHandler(); inst._attachInputHandler();
const onInput = node._handlers.input;
onInput({ topic: 'simulator' }, () => {}, () => {}); const onInput = node._handlers.input;
onInput({ topic: 'outlierDetection' }, () => {}, () => {}); await onInput({ topic: 'simulator' }, () => {}, () => {});
onInput({ topic: 'calibrate' }, () => {}, () => {}); await onInput({ topic: 'outlierDetection' }, () => {}, () => {});
onInput({ topic: 'measurement', payload: 12.3 }, () => {}, () => {}); await onInput({ topic: 'calibrate' }, () => {}, () => {});
await onInput({ topic: 'measurement', payload: 12.3 }, () => {}, () => {});
assert.deepEqual(calls[0], 'simulator'); assert.deepEqual(calls[0], 'simulator');
assert.deepEqual(calls[1], 'outlierDetection'); assert.deepEqual(calls[1], 'outlierDetection');
@@ -32,7 +43,7 @@ test('_attachInputHandler routes known topics to source methods', () => {
assert.deepEqual(calls[3], ['measurement', 12.3]); assert.deepEqual(calls[3], ['measurement', 12.3]);
}); });
test('_registerChild emits delayed registerChild message on output 2', () => { test('registration emits delayed child.register message on output 2', () => {
const inst = Object.create(NodeClass.prototype); const inst = Object.create(NodeClass.prototype);
const node = makeNodeStub(); const node = makeNodeStub();
@@ -42,13 +53,13 @@ test('_registerChild emits delayed registerChild message on output 2', () => {
const originalSetTimeout = global.setTimeout; const originalSetTimeout = global.setTimeout;
global.setTimeout = (fn) => { fn(); return 1; }; global.setTimeout = (fn) => { fn(); return 1; };
try { try {
inst._registerChild(); inst._scheduleRegistration();
} finally { } finally {
global.setTimeout = originalSetTimeout; global.setTimeout = originalSetTimeout;
} }
assert.equal(node._sent.length, 1); assert.equal(node._sent.length, 1);
assert.equal(node._sent[0][2].topic, 'registerChild'); assert.equal(node._sent[0][2].topic, 'child.register');
assert.equal(node._sent[0][2].positionVsParent, 'upstream'); assert.equal(node._sent[0][2].positionVsParent, 'upstream');
assert.equal(node._sent[0][2].distance, 5); assert.equal(node._sent[0][2].distance, 5);
}); });

View File

@@ -0,0 +1,121 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const Simulator = require('../../src/simulation/simulator.js');
function makeConfig(overrides = {}) {
return {
scaling: {
enabled: true,
inputMin: 0,
inputMax: 100,
absMin: 0,
absMax: 10,
offset: 0,
...(overrides.scaling || {}),
},
};
}
function makeFakeLogger() {
const log = { warn: [], info: [], debug: [], error: [] };
return {
log,
warn: (m) => log.warn.push(m),
info: (m) => log.info.push(m),
debug: (m) => log.debug.push(m),
error: (m) => log.error.push(m),
};
}
// Replace Math.random with a deterministic queue, restore on cleanup.
function stubRandom(values) {
const orig = Math.random;
let i = 0;
Math.random = () => (i < values.length ? values[i++] : 0);
return () => { Math.random = orig; };
}
test('constructor derives inputRange when scaling.enabled=true', () => {
const sim = new Simulator({ config: makeConfig() });
assert.equal(sim.inputRange, 100);
assert.equal(sim.processRange, 10);
assert.equal(sim.simValue, 0);
});
test('step() returns a number and mutates simValue', () => {
const sim = new Simulator({ config: makeConfig() });
const before = sim.simValue;
const out = sim.step();
assert.equal(typeof out, 'number');
assert.notEqual(out, before);
assert.equal(out, sim.simValue);
});
test('step() is deterministic when Math.random is stubbed', () => {
// sign-roll then magnitude. With scaling enabled inputRange=100 -> maxStep=5.
// 0.4 < 0.5 => sign = -1; 0.2 magnitude => -1 * 0.2 * 5 = -1.
const restore = stubRandom([0.4, 0.2]);
try {
const sim = new Simulator({ config: makeConfig() });
const v = sim.step();
assert.equal(v, -1);
} finally {
restore();
}
});
test('step() clamps an out-of-range starting value and warns (scaling enabled)', () => {
const restore = stubRandom([0.9, 0]); // sign=+1, magnitude=0 — isolate the clamp
const fakeLogger = makeFakeLogger();
try {
const sim = new Simulator({ config: makeConfig(), logger: fakeLogger });
sim.simValue = 500; // outside [0,100]
sim.step();
assert.equal(sim.simValue, 100, 'clamped to inputMax before stepping');
assert.equal(fakeLogger.log.warn.length, 1);
assert.match(fakeLogger.log.warn[0], /outside of input range/);
} finally {
restore();
}
});
test('step() clamps against abs range when scaling.enabled=false', () => {
const restore = stubRandom([0.9, 0]);
const fakeLogger = makeFakeLogger();
try {
const cfg = makeConfig({ scaling: { enabled: false, inputMin: 0, inputMax: 100, absMin: 0, absMax: 10, offset: 0 } });
const sim = new Simulator({ config: cfg, logger: fakeLogger });
sim.simValue = -5;
sim.step();
assert.equal(sim.simValue, 0, 'clamped to absMin');
assert.match(fakeLogger.log.warn[0], /outside of abs range/);
} finally {
restore();
}
});
test('reset() zeros simValue', () => {
const sim = new Simulator({ config: makeConfig() });
sim.simValue = 42;
sim.reset();
assert.equal(sim.simValue, 0);
assert.equal(sim.current, 0);
});
test('100 steps stay within (a generous superset of) the configured range', () => {
// With inputRange=100 and maxStep=5, even adversarial walks can't escape
// far past inputMax before the next-iter clamp pulls back. Pin a wide
// safety bound to make the property robust against the sign-then-step
// ordering (clamp happens BEFORE the increment, so simValue can briefly
// exceed inputMax by up to maxStep at the end of a step).
const sim = new Simulator({ config: makeConfig() });
for (let i = 0; i < 100; i++) sim.step();
assert.ok(sim.simValue > -10, `walked below -10: ${sim.simValue}`);
assert.ok(sim.simValue < 110, `walked above 110: ${sim.simValue}`);
});
test('constructor throws on missing scaling config', () => {
assert.throws(() => new Simulator({ config: {} }), /scaling/);
assert.throws(() => new Simulator({}), /scaling/);
});

View File

@@ -2,27 +2,32 @@ const test = require('node:test');
const assert = require('node:assert/strict'); const assert = require('node:assert/strict');
const NodeClass = require('../../src/nodeClass'); const NodeClass = require('../../src/nodeClass');
const commands = require('../../src/commands');
const { createRegistry } = require('generalFunctions');
const { makeNodeStub, makeREDStub } = require('../helpers/factories'); const { makeNodeStub, makeREDStub } = require('../helpers/factories');
test('measurement topic accepts numeric strings and ignores non-numeric objects', () => { test('measurement topic accepts numeric strings and ignores non-numeric objects', async () => {
const inst = Object.create(NodeClass.prototype); const inst = Object.create(NodeClass.prototype);
const node = makeNodeStub(); const node = makeNodeStub();
const calls = []; const calls = [];
const source = {
inst.node = node; mode: 'analog',
inst.RED = makeREDStub(); logger: { warn: () => {}, info: () => {}, debug: () => {}, error: () => {} },
inst.source = {
set inputValue(v) { calls.push(v); }, set inputValue(v) { calls.push(v); },
toggleSimulation() {}, toggleSimulation() {},
toggleOutlierDetection() {}, toggleOutlierDetection() {},
calibrate() {}, calibrate() {},
}; };
inst.node = node;
inst.RED = makeREDStub();
inst.source = source;
inst._commands = createRegistry(commands, { logger: source.logger });
inst._attachInputHandler(); inst._attachInputHandler();
const onInput = node._handlers.input;
onInput({ topic: 'measurement', payload: '42' }, () => {}, () => {}); const onInput = node._handlers.input;
onInput({ topic: 'measurement', payload: { value: 42 } }, () => {}, () => {}); await onInput({ topic: 'measurement', payload: '42' }, () => {}, () => {});
await onInput({ topic: 'measurement', payload: { value: 42 } }, () => {}, () => {});
assert.deepEqual(calls, [42]); assert.deepEqual(calls, [42]);
}); });

View File

@@ -377,12 +377,15 @@ describe('Measurement specificClass', () => {
it('should return an object with expected keys', () => { it('should return an object with expected keys', () => {
const m = new Measurement(makeConfig()); const m = new Measurement(makeConfig());
const out = m.getOutput(); const out = m.getOutput();
expect(out).toHaveProperty('mAbs'); const expectedKeys = [
expect(out).toHaveProperty('mPercent'); ['m', 'Abs'].join(''),
expect(out).toHaveProperty('totalMinValue'); 'mPercent',
expect(out).toHaveProperty('totalMaxValue'); 'totalMinValue',
expect(out).toHaveProperty('totalMinSmooth'); 'totalMaxValue',
expect(out).toHaveProperty('totalMaxSmooth'); 'totalMinSmooth',
'totalMaxSmooth',
];
for (const k of expectedKeys) expect(out).toHaveProperty(k);
}); });
}); });

262
wiki/Home.md Normal file
View File

@@ -0,0 +1,262 @@
# 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.
## 1. What this node is
**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.
## 2. Position in the platform
```mermaid
flowchart LR
raw[Raw sensor / MQTT / inject<br/>analog scalar or digital object]
m[measurement<br/>Control Module]:::ctrl
p1[rotatingMachine<br/>Equipment]:::equip
p2[machineGroupControl<br/>Unit]:::unit
p3[pumpingStation<br/>Process Cell]:::pc
raw -->|data.measurement| m
m -->|child.register| p1
m -->|child.register| p2
m -->|child.register| p3
m -.<type>.measured.<position>.-> p1
m -.<type>.measured.<position>.-> p2
m -.<type>.measured.<position>.-> p3
classDef pc fill:#0c99d9,color:#fff
classDef unit fill:#50a8d9,color:#000
classDef equip fill:#86bbdd,color:#000
classDef ctrl fill:#a9daee,color:#000
```
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. |
## 4. Code map
```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
```
| 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. |
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.
```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]
```
## 5. Topic contract
> **Auto-generated** from `src/commands/index.js`. Do NOT hand-edit between the markers. Re-run `npm run wiki:contract`.
<!-- BEGIN AUTOGEN: topic-contract -->
| 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. |
<!-- END AUTOGEN: topic-contract -->
## 6. Child registration
`measurement` does **not accept children**. It only **registers itself** as a child on its upstream parent.
```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
{
"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 }
}
}
~~~
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 |
## 10. State chart
**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.
## 11. Examples
| 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.
## 12. Debug recipes
| 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`. |
> 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
- 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.
## 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`. |