Refactor monster to current architecture
This commit is contained in:
156
src/nodeClass.js
Normal file
156
src/nodeClass.js
Normal file
@@ -0,0 +1,156 @@
|
||||
const { outputUtils, configManager, POSITIONS } = require('generalFunctions');
|
||||
const Specific = require('./specificClass');
|
||||
|
||||
class nodeClass {
|
||||
constructor(uiConfig, RED, nodeInstance, nameOfNode) {
|
||||
this.node = nodeInstance;
|
||||
this.RED = RED;
|
||||
this.name = nameOfNode;
|
||||
this.source = null;
|
||||
|
||||
this._loadConfig(uiConfig);
|
||||
this._setupSpecificClass();
|
||||
this._bindEvents();
|
||||
this._registerChild();
|
||||
this._startTickLoop();
|
||||
this._attachInputHandler();
|
||||
this._attachCloseHandler();
|
||||
}
|
||||
|
||||
_loadConfig(uiConfig) {
|
||||
const cfgMgr = new configManager();
|
||||
|
||||
this.config = cfgMgr.buildConfig(this.name, uiConfig, this.node.id, {
|
||||
constraints: {
|
||||
samplingtime: Number(uiConfig.samplingtime) || 0,
|
||||
minVolume: Number(uiConfig.minvolume ?? uiConfig.minVolume) || 5,
|
||||
maxWeight: Number(uiConfig.maxweight ?? uiConfig.maxWeight) || 23,
|
||||
},
|
||||
});
|
||||
|
||||
this.config.functionality = {
|
||||
...this.config.functionality,
|
||||
role: 'samplingCabinet',
|
||||
aquonSampleName: uiConfig.aquon_sample_name || undefined,
|
||||
};
|
||||
|
||||
this.config.asset = {
|
||||
uuid: uiConfig.uuid || null,
|
||||
supplier: uiConfig.supplier || 'Unknown',
|
||||
type: 'sensor',
|
||||
subType: uiConfig.subType || 'pressure',
|
||||
model: uiConfig.model || 'Unknown',
|
||||
emptyWeightBucket: Number(uiConfig.emptyWeightBucket) || 3,
|
||||
};
|
||||
|
||||
this._output = new outputUtils();
|
||||
}
|
||||
|
||||
_setupSpecificClass() {
|
||||
this.source = new Specific(this.config);
|
||||
this.node.source = this.source;
|
||||
}
|
||||
|
||||
_bindEvents() {}
|
||||
|
||||
_updateNodeStatus() {
|
||||
try {
|
||||
const stateText = this.source.running
|
||||
? `${this.source.currentMode}: ON => ${this.source.bucketVol} | ${this.source.maxVolume}`
|
||||
: `${this.source.currentMode}: OFF`;
|
||||
|
||||
return {
|
||||
fill: this.source.running ? 'green' : 'red',
|
||||
shape: 'dot',
|
||||
text: stateText,
|
||||
};
|
||||
} catch (error) {
|
||||
this.node.error(`Error in updateNodeStatus: ${error.message}`);
|
||||
return { fill: 'red', shape: 'ring', text: 'Status Error' };
|
||||
}
|
||||
}
|
||||
|
||||
_registerChild() {
|
||||
setTimeout(() => {
|
||||
this.node.send([
|
||||
null,
|
||||
null,
|
||||
{ topic: 'registerChild', payload: this.node.id, positionVsParent: POSITIONS.UPSTREAM },
|
||||
{ topic: 'registerChild', payload: this.node.id, positionVsParent: POSITIONS.DOWNSTREAM },
|
||||
]);
|
||||
}, 100);
|
||||
}
|
||||
|
||||
_startTickLoop() {
|
||||
setTimeout(() => {
|
||||
this._tickInterval = setInterval(() => this._tick(), 1000);
|
||||
this._statusInterval = setInterval(() => {
|
||||
this.node.status(this._updateNodeStatus());
|
||||
}, 1000);
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
_tick() {
|
||||
this.source.tick();
|
||||
|
||||
const raw = this.source.getOutput();
|
||||
const processMsg = this._output.formatMsg(raw, this.config, 'process');
|
||||
const influxMsg = this._output.formatMsg(raw, this.config, 'influxdb');
|
||||
|
||||
this.node.send([processMsg, influxMsg, null, null]);
|
||||
}
|
||||
|
||||
_attachInputHandler() {
|
||||
this.node.on('input', (msg, _send, done) => {
|
||||
try {
|
||||
switch (msg.topic) {
|
||||
case 'registerChild': {
|
||||
const childId = msg.payload;
|
||||
const childObj = this.RED.nodes.getNode(childId);
|
||||
if (childObj?.source) {
|
||||
this.source.childRegistrationUtils.registerChild(childObj.source, msg.positionVsParent);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'setMode':
|
||||
this.source.setMode(msg.payload);
|
||||
break;
|
||||
case 'start':
|
||||
case 'i_start':
|
||||
this.source.i_start = true;
|
||||
break;
|
||||
case 'i_flow':
|
||||
this.source.q = Number(msg.payload) || 0;
|
||||
break;
|
||||
case 'aquon_monsternametijden':
|
||||
this.source.monsternametijden = msg.payload;
|
||||
break;
|
||||
case 'rain_data':
|
||||
this.source.rain_data = msg.payload;
|
||||
break;
|
||||
case 'model_prediction':
|
||||
this.source.setModelPrediction(msg.payload);
|
||||
break;
|
||||
default:
|
||||
this.source.logger.warn(`Unknown topic: ${msg.topic}`);
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
this.node.error(`Error in input function: ${error.message}`);
|
||||
this.node.status({ fill: 'red', shape: 'ring', text: 'Input Error' });
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
_attachCloseHandler() {
|
||||
this.node.on('close', (done) => {
|
||||
clearInterval(this._tickInterval);
|
||||
clearInterval(this._statusInterval);
|
||||
done();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = nodeClass;
|
||||
393
src/specificClass.js
Normal file
393
src/specificClass.js
Normal file
@@ -0,0 +1,393 @@
|
||||
const EventEmitter = require('events');
|
||||
const { logger, configUtils, convert, childRegistrationUtils } = require('generalFunctions');
|
||||
const defaultConfig = require('../dependencies/monster/monsterConfig.json');
|
||||
|
||||
class Monster {
|
||||
constructor(config = {}) {
|
||||
const loggingEnabled = config.general?.logging?.enabled ?? true;
|
||||
const logLevel = config.general?.logging?.logLevel ?? 'info';
|
||||
|
||||
this.emitter = new EventEmitter();
|
||||
this.configUtils = new configUtils(defaultConfig, loggingEnabled, logLevel);
|
||||
this.config = this.configUtils.initConfig(config);
|
||||
this.logger = new logger(loggingEnabled, logLevel, this.config.general.name);
|
||||
this.childRegistrationUtils = new childRegistrationUtils(this);
|
||||
this.convert = convert;
|
||||
|
||||
this.output = {};
|
||||
this.child = {};
|
||||
this.currentMode = 'AI';
|
||||
this.externalPrediction = null;
|
||||
|
||||
this.aquonSampleName = this.config.functionality?.aquonSampleName || '112100';
|
||||
this._monsternametijden = {};
|
||||
this._rain_data = {};
|
||||
this.aggregatedOutput = {};
|
||||
this.sumRain = 0;
|
||||
this.avgRain = 0;
|
||||
this.daysPerYear = 0;
|
||||
|
||||
this.pulse = false;
|
||||
this.bucketVol = 0;
|
||||
this.sumPuls = 0;
|
||||
this.predFlow = 0;
|
||||
this.bucketWeight = 0;
|
||||
|
||||
this.q = 0;
|
||||
this.i_start = false;
|
||||
this.sampling_time = Number(this.config.constraints?.samplingtime) || 0;
|
||||
this.emptyWeightBucket = Number(this.config.asset?.emptyWeightBucket) || 0;
|
||||
|
||||
this.temp_pulse = 0;
|
||||
this.volume_pulse = 0.05;
|
||||
this.minVolume = Number(this.config.constraints?.minVolume) || 0;
|
||||
this.maxVolume = 0;
|
||||
this.maxWeight = Number(this.config.constraints?.maxWeight) || 0;
|
||||
this.cap_volume = 55;
|
||||
this.targetVolume = 0;
|
||||
this.minPuls = 0;
|
||||
this.maxPuls = 0;
|
||||
this.absMaxPuls = 0;
|
||||
this.targetPuls = 0;
|
||||
this.m3PerPuls = 0;
|
||||
this.predM3PerSec = 0;
|
||||
this.m3PerTick = 0;
|
||||
this.m3Total = 0;
|
||||
this.running = false;
|
||||
|
||||
this.qLineRaw = {};
|
||||
this.minSeen = {};
|
||||
this.maxSeen = {};
|
||||
this.qLineRefined = {};
|
||||
this.calcTimeShiftDry = 0;
|
||||
this.calcTimeShiftWet = 0;
|
||||
this.calcCapacitySewer = 0;
|
||||
this.minDryHours = 0;
|
||||
this.minWetHours = 0;
|
||||
this.resolution = 0;
|
||||
this.tmpTotQ = 0;
|
||||
|
||||
this.predFactor = 0.7;
|
||||
this.start_time = Date.now();
|
||||
this.stop_time = Date.now();
|
||||
this.flowTime = 0;
|
||||
this.timePassed = 0;
|
||||
this.timeLeft = 0;
|
||||
this.currHour = new Date().getHours();
|
||||
this.nextDate = new Date(new Date().setFullYear(new Date().getFullYear() + 1)).getTime();
|
||||
|
||||
this.setMode(this.currentMode);
|
||||
this.set_boundries_and_targets();
|
||||
this.regNextDate(this.monsternametijden);
|
||||
this._syncOutput();
|
||||
}
|
||||
|
||||
set monsternametijden(value) {
|
||||
if (!value || typeof value !== 'object' || Object.keys(value).length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const firstEntry = Array.isArray(value) ? value[0] : Object.values(value)[0];
|
||||
if (
|
||||
firstEntry &&
|
||||
typeof firstEntry.SAMPLE_NAME !== 'undefined' &&
|
||||
typeof firstEntry.DESCRIPTION !== 'undefined' &&
|
||||
typeof firstEntry.SAMPLED_DATE !== 'undefined' &&
|
||||
typeof firstEntry.START_DATE !== 'undefined' &&
|
||||
typeof firstEntry.END_DATE !== 'undefined'
|
||||
) {
|
||||
this._monsternametijden = value;
|
||||
this.regNextDate(value);
|
||||
this._syncOutput();
|
||||
}
|
||||
}
|
||||
|
||||
get monsternametijden() {
|
||||
return this._monsternametijden;
|
||||
}
|
||||
|
||||
set rain_data(value) {
|
||||
this._rain_data = value;
|
||||
|
||||
if (value && this.running === false) {
|
||||
this.updatePredRain(value);
|
||||
this._syncOutput();
|
||||
}
|
||||
}
|
||||
|
||||
get rain_data() {
|
||||
return this._rain_data;
|
||||
}
|
||||
|
||||
set bucketVol(val) {
|
||||
this._bucketVol = Number(val) || 0;
|
||||
this.output.bucketVol = this._bucketVol;
|
||||
this.bucketWeight = this._bucketVol + this.emptyWeightBucket;
|
||||
}
|
||||
|
||||
get bucketVol() {
|
||||
return this._bucketVol;
|
||||
}
|
||||
|
||||
set minVolume(val) {
|
||||
const safeValue = Number(val) === 0 ? 1 : Number(val) || 0;
|
||||
this._minVolume = safeValue;
|
||||
this.output.minVolume = safeValue;
|
||||
}
|
||||
|
||||
get minVolume() {
|
||||
return this._minVolume;
|
||||
}
|
||||
|
||||
set q(val) {
|
||||
const parsed = Number(val) || 0;
|
||||
this._q = parsed;
|
||||
this.output.q = parsed;
|
||||
this.output.qm3sec = this.convert(parsed).from('m3/h').to('m3/s');
|
||||
}
|
||||
|
||||
get q() {
|
||||
return this._q;
|
||||
}
|
||||
|
||||
setMode(mode) {
|
||||
this.currentMode = mode || 'AI';
|
||||
this.output.mode = this.currentMode;
|
||||
}
|
||||
|
||||
setModelPrediction(value) {
|
||||
const parsed = Number(value);
|
||||
if (!Number.isFinite(parsed) || parsed < 0) {
|
||||
this.logger.warn(`Ignoring invalid model prediction: ${value}`);
|
||||
return;
|
||||
}
|
||||
|
||||
this.externalPrediction = parsed;
|
||||
this.predFlow = parsed;
|
||||
this.output.predFlow = parsed;
|
||||
}
|
||||
|
||||
set_boundries_and_targets() {
|
||||
this.maxVolume = Math.max(0, this.maxWeight - this.emptyWeightBucket);
|
||||
this.minPuls = Math.round(this.minVolume / this.volume_pulse);
|
||||
this.maxPuls = Math.round(this.maxVolume / this.volume_pulse);
|
||||
this.absMaxPuls = Math.round(this.cap_volume / this.volume_pulse);
|
||||
|
||||
if (this.minVolume > 0 && this.maxVolume > 0) {
|
||||
this.targetVolume = this.minVolume * Math.sqrt(this.maxVolume / this.minVolume);
|
||||
this.targetPuls = Math.round(this.targetVolume / this.volume_pulse);
|
||||
} else {
|
||||
this.targetVolume = 0;
|
||||
this.targetPuls = 0;
|
||||
}
|
||||
}
|
||||
|
||||
updateArchiveRain(_val) {}
|
||||
|
||||
updatePredRain(value) {
|
||||
if (!value || typeof value !== 'object') {
|
||||
return this.aggregatedOutput;
|
||||
}
|
||||
|
||||
const totalProb = {};
|
||||
let numberOfLocations = 0;
|
||||
|
||||
this.aggregatedOutput = {};
|
||||
|
||||
Object.entries(value).forEach(([locationKey, location]) => {
|
||||
if (!location?.hourly?.time) {
|
||||
return;
|
||||
}
|
||||
|
||||
numberOfLocations++;
|
||||
this.aggregatedOutput[locationKey] = {
|
||||
tag: {
|
||||
latitude: location.latitude,
|
||||
longitude: location.longitude,
|
||||
},
|
||||
precipationRaw: {},
|
||||
precipationProb: {},
|
||||
};
|
||||
|
||||
Object.entries(location.hourly.time).forEach(([key, time]) => {
|
||||
const currTimestamp = new Date(time).getTime();
|
||||
const precipitation = Number(location.hourly.precipitation?.[key]) || 0;
|
||||
let probability = Number(location.hourly.precipitation_probability?.[key]);
|
||||
|
||||
if (!Number.isFinite(probability)) {
|
||||
probability = 100;
|
||||
}
|
||||
if (probability > 0) {
|
||||
probability /= 100;
|
||||
}
|
||||
|
||||
totalProb[currTimestamp] = (totalProb[currTimestamp] || 0) + (precipitation * probability);
|
||||
this.aggregatedOutput[locationKey].precipationRaw[key] = { val: precipitation, time: currTimestamp };
|
||||
this.aggregatedOutput[locationKey].precipationProb[key] = { val: probability, time: currTimestamp };
|
||||
});
|
||||
});
|
||||
|
||||
this.sumRain = Object.values(totalProb).reduce((sum, current) => sum + current, 0);
|
||||
this.avgRain = numberOfLocations > 0 ? this.sumRain / numberOfLocations : 0;
|
||||
|
||||
return this.aggregatedOutput;
|
||||
}
|
||||
|
||||
get_model_prediction() {
|
||||
if (Number.isFinite(this.externalPrediction)) {
|
||||
this.predFlow = this.externalPrediction;
|
||||
} else if (!Number.isFinite(this.predFlow)) {
|
||||
this.predFlow = 0;
|
||||
}
|
||||
|
||||
this.output.predFlow = this.predFlow;
|
||||
return this.predFlow;
|
||||
}
|
||||
|
||||
sampling_program() {
|
||||
if ((this.i_start || Date.now() >= this.nextDate) && !this.running) {
|
||||
this.running = true;
|
||||
this.temp_pulse = 0;
|
||||
this.pulse = false;
|
||||
this.bucketVol = 0;
|
||||
this.sumPuls = 0;
|
||||
this.m3Total = 0;
|
||||
this.timePassed = 0;
|
||||
this.timeLeft = 0;
|
||||
this.predM3PerSec = 0;
|
||||
|
||||
const prediction = this.get_model_prediction();
|
||||
this.m3PerPuls = prediction > 0 && this.targetPuls > 0 ? Math.max(Math.round(prediction / this.targetPuls), 1) : 0;
|
||||
this.predM3PerSec = this.sampling_time > 0 ? prediction / this.sampling_time / 60 / 60 : 0;
|
||||
|
||||
this.start_time = Date.now();
|
||||
this.stop_time = Date.now() + (this.sampling_time * 60 * 60 * 1000);
|
||||
this.regNextDate(this.monsternametijden);
|
||||
this.i_start = false;
|
||||
}
|
||||
|
||||
if (this.running && this.stop_time > Date.now()) {
|
||||
this.timePassed = Math.round((Date.now() - this.start_time) / 1000);
|
||||
this.timeLeft = Math.round((this.stop_time - Date.now()) / 1000);
|
||||
|
||||
if (this.m3PerPuls > 0) {
|
||||
const update = this.m3PerTick / this.m3PerPuls;
|
||||
this.temp_pulse += update;
|
||||
}
|
||||
|
||||
this.m3Total += this.m3PerTick;
|
||||
|
||||
if (this.temp_pulse >= 1 && this.sumPuls < this.absMaxPuls) {
|
||||
this.temp_pulse -= 1;
|
||||
this.pulse = true;
|
||||
this.sumPuls++;
|
||||
this.bucketVol = Math.round(this.sumPuls * this.volume_pulse * 100) / 100;
|
||||
} else if (this.pulse) {
|
||||
this.pulse = false;
|
||||
}
|
||||
} else if (this.running) {
|
||||
this.m3PerPuls = 0;
|
||||
this.temp_pulse = 0;
|
||||
this.pulse = false;
|
||||
this.bucketVol = 0;
|
||||
this.sumPuls = 0;
|
||||
this.timePassed = 0;
|
||||
this.timeLeft = 0;
|
||||
this.predFlow = Number.isFinite(this.externalPrediction) ? this.externalPrediction : 0;
|
||||
this.predM3PerSec = 0;
|
||||
this.m3Total = 0;
|
||||
this.running = false;
|
||||
}
|
||||
}
|
||||
|
||||
flowCalc() {
|
||||
const timePassed = this.flowTime > 0 ? (Date.now() - this.flowTime) / 1000 : 0;
|
||||
this.m3PerTick = this.q / 60 / 60 * timePassed;
|
||||
this.flowTime = Date.now();
|
||||
}
|
||||
|
||||
tick() {
|
||||
this.flowCalc();
|
||||
this.sampling_program();
|
||||
this.logQoverTime();
|
||||
this._syncOutput();
|
||||
}
|
||||
|
||||
regNextDate(monsternametijden) {
|
||||
let nextDate = new Date(new Date().setFullYear(new Date().getFullYear() + 1)).getTime();
|
||||
let daysRemaining = 0;
|
||||
|
||||
if (monsternametijden && typeof monsternametijden === 'object') {
|
||||
Object.entries(monsternametijden).forEach(([_key, line]) => {
|
||||
if (!line || line.START_DATE === 'NULL') {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentDate = new Date(line.START_DATE);
|
||||
const currentTimestamp = currentDate.getTime();
|
||||
|
||||
if (line.SAMPLE_NAME === this.aquonSampleName && currentTimestamp > Date.now()) {
|
||||
if (currentTimestamp < nextDate) {
|
||||
nextDate = currentTimestamp;
|
||||
}
|
||||
|
||||
if (new Date().getFullYear() === currentDate.getFullYear()) {
|
||||
daysRemaining++;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
this.daysPerYear = daysRemaining;
|
||||
this.nextDate = nextDate;
|
||||
}
|
||||
|
||||
logQoverTime() {
|
||||
const currentHour = new Date().getHours();
|
||||
if (this.currHour !== currentHour) {
|
||||
this.qLineRaw[this.currHour] = this.tmpTotQ;
|
||||
this.tmpTotQ = 0;
|
||||
this.currHour = currentHour;
|
||||
} else {
|
||||
this.tmpTotQ += this.q;
|
||||
}
|
||||
}
|
||||
|
||||
createMinMaxSeen() {
|
||||
this.minSeen = {};
|
||||
this.maxSeen = {};
|
||||
for (let hour = 1; hour < this.sampling_time; hour++) {
|
||||
this.minSeen[hour] = null;
|
||||
this.maxSeen[hour] = null;
|
||||
}
|
||||
}
|
||||
|
||||
_syncOutput() {
|
||||
this.output = {
|
||||
...this.output,
|
||||
pulse: this.pulse,
|
||||
bucketVol: this.bucketVol,
|
||||
sumPuls: this.sumPuls,
|
||||
predFlow: this.predFlow,
|
||||
predM3PerSec: this.predM3PerSec,
|
||||
bucketWeight: this.bucketWeight,
|
||||
m3PerTick: this.m3PerTick,
|
||||
m3Total: this.m3Total,
|
||||
running: this.running,
|
||||
timePassed: this.timePassed,
|
||||
timeLeft: this.timeLeft,
|
||||
daysPerYear: this.daysPerYear,
|
||||
nextDate: this.nextDate,
|
||||
avgRain: this.avgRain,
|
||||
sumRain: this.sumRain,
|
||||
mode: this.currentMode,
|
||||
};
|
||||
}
|
||||
|
||||
getOutput() {
|
||||
this._syncOutput();
|
||||
return { ...this.output };
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Monster;
|
||||
Reference in New Issue
Block a user