From c587ed9c7b674d9c304e20e53eebcdc06dad1ac1 Mon Sep 17 00:00:00 2001 From: znetsixe <73483679+znetsixe@users.noreply.github.com> Date: Mon, 23 Feb 2026 13:17:03 +0100 Subject: [PATCH] working --- src/nodeClass.js | 41 ++++++++++++++++++-------- src/specificClass.js | 11 +++++-- test/edge/invalid-payload.edge.test.js | 4 +-- test/edge/outlier-toggle.edge.test.js | 6 ++-- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/nodeClass.js b/src/nodeClass.js index b3ac9e6..fc46108 100644 --- a/src/nodeClass.js +++ b/src/nodeClass.js @@ -87,8 +87,6 @@ class nodeClass { } }; - console.log(`position vs child for ${this.name} is ${this.config.functionality.positionVsParent} the distance is ${this.config.functionality.distance}`); - // Utility for formatting outputs this._output = new outputUtils(); } @@ -154,17 +152,34 @@ class nodeClass { */ _attachInputHandler() { this.node.on('input', (msg, send, done) => { - switch (msg.topic) { - case 'simulator': this.source.toggleSimulation(); break; - case 'outlierDetection': this.source.toggleOutlierDetection(); break; - case 'calibrate': this.source.calibrate(); break; - case 'measurement': - if (typeof msg.payload === 'number') { - this.source.inputValue = parseFloat(msg.payload); - } - break; + try { + switch (msg.topic) { + case 'simulator': + this.source.toggleSimulation(); + break; + case 'outlierDetection': + this.source.toggleOutlierDetection(); + break; + case 'calibrate': + this.source.calibrate(); + break; + case 'measurement': + 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}`); + } + } + break; + default: + this.source.logger?.warn(`Unknown topic: ${msg.topic}`); + } + } catch (error) { + this.source.logger?.error(`Input handler failure: ${error.message}`); } - done(); + if (typeof done === 'function') done(); }); } @@ -175,7 +190,7 @@ class nodeClass { this.node.on('close', (done) => { clearInterval(this._tickInterval); //clearInterval(this._statusInterval); - done(); + if (typeof done === 'function') done(); }); } } diff --git a/src/specificClass.js b/src/specificClass.js index 83991c8..8bcc537 100644 --- a/src/specificClass.js +++ b/src/specificClass.js @@ -1,6 +1,10 @@ const EventEmitter = require('events'); const {logger,configUtils,configManager,MeasurementContainer} = require('generalFunctions'); +/** + * Measurement domain model. + * Handles scaling, smoothing, outlier filtering and emits normalized measurement output. + */ class Measurement { constructor(config={}) { @@ -506,7 +510,10 @@ class Measurement { } toggleOutlierDetection() { - this.config.outlierDetection = !this.config.outlierDetection; + // 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.enabled = !currentState; } getOutput() { @@ -586,4 +593,4 @@ function changeInput(){ //m.inputValue = 5; } -// */ \ No newline at end of file +// */ diff --git a/test/edge/invalid-payload.edge.test.js b/test/edge/invalid-payload.edge.test.js index 4938f11..09a26f8 100644 --- a/test/edge/invalid-payload.edge.test.js +++ b/test/edge/invalid-payload.edge.test.js @@ -4,7 +4,7 @@ const assert = require('node:assert/strict'); const NodeClass = require('../../src/nodeClass'); const { makeNodeStub, makeREDStub } = require('../helpers/factories'); -test('measurement topic ignores non-number payloads (current behavior)', () => { +test('measurement topic accepts numeric strings and ignores non-numeric objects', () => { const inst = Object.create(NodeClass.prototype); const node = makeNodeStub(); const calls = []; @@ -24,5 +24,5 @@ test('measurement topic ignores non-number payloads (current behavior)', () => { onInput({ topic: 'measurement', payload: '42' }, () => {}, () => {}); onInput({ topic: 'measurement', payload: { value: 42 } }, () => {}, () => {}); - assert.equal(calls.length, 0); + assert.deepEqual(calls, [42]); }); diff --git a/test/edge/outlier-toggle.edge.test.js b/test/edge/outlier-toggle.edge.test.js index 47a3f57..b9c4f37 100644 --- a/test/edge/outlier-toggle.edge.test.js +++ b/test/edge/outlier-toggle.edge.test.js @@ -3,10 +3,12 @@ const assert = require('node:assert/strict'); const { makeMeasurementInstance } = require('../helpers/factories'); -test('toggleOutlierDetection currently converts config object to boolean (known gap)', () => { +test('toggleOutlierDetection toggles enabled flag while preserving config object', () => { const m = makeMeasurementInstance(); assert.equal(typeof m.config.outlierDetection, 'object'); + const before = Boolean(m.config.outlierDetection.enabled); m.toggleOutlierDetection(); - assert.equal(typeof m.config.outlierDetection, 'boolean'); + assert.equal(typeof m.config.outlierDetection, 'object'); + assert.equal(Boolean(m.config.outlierDetection.enabled), !before); });