Files
monster/src/commands/handlers.js

55 lines
1.9 KiB
JavaScript
Raw Normal View History

'use strict';
// Handlers for monster input topics. Each is a pure function over the
// domain (source). Unit conversion for incoming flow happens in the
// handler (the legacy nodeClass did it inline) — anything else inbound
// is passed straight through to source.handleInput.
const { convert } = require('generalFunctions');
exports.cmdStart = (source, msg) => {
source.handleInput('i_start', Boolean(msg.payload));
};
exports.setSchedule = (source, msg) => {
source.handleInput('monsternametijden', msg.payload);
};
exports.setRain = (source, msg) => {
source.handleInput('rain_data', msg.payload);
};
exports.dataFlow = (source, msg, ctx) => {
const log = ctx?.logger || source.logger;
const value = Number(msg.payload?.value);
const unit = msg.payload?.unit;
if (!Number.isFinite(value) || !unit) {
log?.warn?.('data.flow payload must include numeric value and unit.');
return;
}
let converted = value;
try { converted = convert(value).from(unit).to('m3/h'); }
catch (err) { log?.warn?.(`data.flow unit conversion failed: ${err.message}`); return; }
source.handleInput('input_q', { value: converted, unit: 'm3/h' });
};
exports.setMode = (source, msg) => {
if (typeof source.setMode === 'function') source.setMode(msg.payload);
};
exports.setModelPrediction = (source, msg) => {
if (typeof source.setModelPrediction === 'function') source.setModelPrediction(msg.payload);
};
// Inbound child registration from a measurement (or other) child node.
// Ported from the legacy `case 'registerChild'` branch in nodeClass.
exports.childRegister = (source, msg, ctx) => {
const childId = msg.payload;
const childObj = ctx?.RED?.nodes?.getNode?.(childId);
if (!childObj?.source) {
(ctx?.logger || source.logger)?.warn?.(`child.register skipped: missing child/source for id=${childId}`);
return;
}
source.childRegistrationUtils.registerChild(childObj.source, msg.positionVsParent, msg.distance);
};