Compare commits
2 Commits
cf10e20404
...
ae46e0021c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae46e0021c | ||
|
|
7e683792d4 |
@@ -1,4 +1,4 @@
|
||||
const inputExample =
|
||||
const inputExample = // eslint-disable-line no-unused-vars
|
||||
//tensor 1
|
||||
[
|
||||
// 1 prediction per hour
|
||||
|
||||
747
dependencies/monster/monster_class.js
vendored
747
dependencies/monster/monster_class.js
vendored
File diff suppressed because one or more lines are too long
22
monster.html
22
monster.html
@@ -10,6 +10,8 @@ RED.nodes.registerType('monster', {
|
||||
|
||||
// Define default properties
|
||||
name: { value: "", required: true },
|
||||
processOutputFormat: { value: "process" },
|
||||
dbaseOutputFormat: { value: "influxdb" },
|
||||
enableLog: { value: false },
|
||||
logLevel: { value: "error" },
|
||||
|
||||
@@ -228,6 +230,26 @@ RED.nodes.registerType('monster', {
|
||||
|
||||
<hr />
|
||||
|
||||
<h3>Output Formats</h3>
|
||||
<div class="form-row">
|
||||
<label for="node-input-processOutputFormat"><i class="fa fa-random"></i> Process Output</label>
|
||||
<select id="node-input-processOutputFormat" style="width:60%;">
|
||||
<option value="process">process</option>
|
||||
<option value="json">json</option>
|
||||
<option value="csv">csv</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-dbaseOutputFormat"><i class="fa fa-database"></i> Database Output</label>
|
||||
<select id="node-input-dbaseOutputFormat" style="width:60%;">
|
||||
<option value="influxdb">influxdb</option>
|
||||
<option value="json">json</option>
|
||||
<option value="csv">csv</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<hr />
|
||||
|
||||
<!-- loglevel checkbox -->
|
||||
<div class="form-row">
|
||||
<label for="node-input-enableLog"
|
||||
|
||||
185
monster.js
185
monster.js
@@ -1,182 +1,9 @@
|
||||
module.exports = function (RED) {
|
||||
function monster(config) {
|
||||
const nameOfNode = 'monster';
|
||||
const nodeClass = require('./src/nodeClass.js');
|
||||
|
||||
// create node
|
||||
module.exports = function(RED) {
|
||||
RED.nodes.registerType(nameOfNode, function(config) {
|
||||
RED.nodes.createNode(this, config);
|
||||
|
||||
// call this => node so whenver you want to call a node function type node and the function behind it
|
||||
var node = this;
|
||||
|
||||
try{
|
||||
|
||||
|
||||
// fetch monster object from monster.js
|
||||
const Monster = require("./dependencies/monster/monster_class");
|
||||
const OutputUtils = require("../generalFunctions/helper/outputUtils");
|
||||
|
||||
const mConfig={
|
||||
general: {
|
||||
name: config.name,
|
||||
id: node.id,
|
||||
unit: config.unit,
|
||||
logging:{
|
||||
logLevel: config.logLevel,
|
||||
enabled: config.enableLog,
|
||||
},
|
||||
},
|
||||
asset: {
|
||||
supplier: config.supplier,
|
||||
subType: config.subType,
|
||||
model: config.model,
|
||||
emptyWeightBucket: config.emptyWeightBucket,
|
||||
},
|
||||
constraints: {
|
||||
minVolume: config.minVolume,
|
||||
maxWeight: config.maxWeight,
|
||||
samplingtime: config.samplingtime,
|
||||
},
|
||||
}
|
||||
|
||||
// make new monster on creation to work with.
|
||||
const m = new Monster(mConfig);
|
||||
|
||||
// put m on node memory as source
|
||||
node.source = m;
|
||||
|
||||
//load output utils
|
||||
const output = new OutputUtils();
|
||||
|
||||
//internal vars
|
||||
this.interval_id = null;
|
||||
|
||||
//updating node state
|
||||
function updateNodeStatus() {
|
||||
try{
|
||||
|
||||
const bucketVol = m.bucketVol;
|
||||
const maxVolume = m.maxVolume;
|
||||
const state = m.running;
|
||||
const mode = "AI" ; //m.mode;
|
||||
|
||||
let status;
|
||||
|
||||
switch (state) {
|
||||
case false:
|
||||
status = { fill: "red", shape: "dot", text: `${mode}: OFF` };
|
||||
break;
|
||||
case true:
|
||||
status = { fill: "green", shape: "dot", text: `${mode}: ON => ${bucketVol} | ${maxVolume}` };
|
||||
break;
|
||||
}
|
||||
|
||||
return status;
|
||||
} catch (error) {
|
||||
node.error("Error in updateNodeStatus: " + error);
|
||||
return { fill: "red", shape: "ring", text: "Status Error" };
|
||||
}
|
||||
}
|
||||
|
||||
function tick(){
|
||||
try{
|
||||
// load status node
|
||||
const status = updateNodeStatus();
|
||||
// kick time based function in node
|
||||
m.tick();
|
||||
//show node status
|
||||
node.status(status);
|
||||
} catch (error) {
|
||||
node.error("Error in tick function: " + error);
|
||||
node.status({ fill: "red", shape: "ring", text: "Tick Error" });
|
||||
}
|
||||
}
|
||||
|
||||
// register child on first output this timeout is needed because of node - red stuff
|
||||
setTimeout(
|
||||
() => {
|
||||
|
||||
/*---execute code on first start----*/
|
||||
let msgs = [];
|
||||
|
||||
msgs[2] = { topic : "registerChild" , payload: node.id, positionVsParent: "upstream" };
|
||||
msgs[3] = { topic : "registerChild" , payload: node.id, positionVsParent: "downstream" };
|
||||
|
||||
//send msg
|
||||
this.send(msgs);
|
||||
},
|
||||
100
|
||||
);
|
||||
|
||||
//declare refresh interval internal node
|
||||
setTimeout(
|
||||
() => {
|
||||
/*---execute code on first start----*/
|
||||
this.interval_id = setInterval(function(){ tick() },1000)
|
||||
},
|
||||
1000
|
||||
);
|
||||
|
||||
node.on('input', function (msg,send,done) {
|
||||
try{
|
||||
switch(msg.topic) {
|
||||
case 'registerChild':
|
||||
const childId = msg.payload;
|
||||
const childObj = RED.nodes.getNode(childId);
|
||||
m.childRegistrationUtils.registerChild(childObj.source ,msg.positionVsParent);
|
||||
break;
|
||||
case 'setMode':
|
||||
m.setMode(msg.payload);
|
||||
break;
|
||||
case 'start':
|
||||
m.i_start = true;
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
node.error("Error in input function: " + error);
|
||||
node.status({ fill: "red", shape: "ring", text: "Input Error" });
|
||||
}
|
||||
|
||||
if(msg.topic == "i_flow"){
|
||||
monster.q = parseFloat(msg.payload);
|
||||
}
|
||||
|
||||
if(msg.topic == "i_start"){
|
||||
monster.i_start = true;
|
||||
}
|
||||
|
||||
if(msg.topic == "model_prediction"){
|
||||
let var1 = msg.payload.dagvoorheen;
|
||||
let var2 = msg.payload.dagnadien;
|
||||
monster.get_model_prediction(var1, var2);
|
||||
}
|
||||
|
||||
if(msg.topic == "aquon_monsternametijden"){
|
||||
monster.monsternametijden = msg.payload;
|
||||
}
|
||||
|
||||
if(msg.topic == "rain_data"){
|
||||
monster.rain_data = msg.payload;
|
||||
}
|
||||
|
||||
//register child classes
|
||||
if(msg.topic == "registerChild"){
|
||||
let child = msg.payload;
|
||||
monster.registerChild(child);
|
||||
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
// tidy up any async code here - shutdown connections and so on.
|
||||
node.on('close', function() {
|
||||
clearTimeout(this.interval_id);
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
node.error("Error in monster function: " + error);
|
||||
node.status({ fill: "red", shape: "ring", text: "Monster Error" });
|
||||
}
|
||||
}
|
||||
RED.nodes.registerType("monster", monster);
|
||||
|
||||
this.nodeClass = new nodeClass(config, RED, this, nameOfNode);
|
||||
});
|
||||
};
|
||||
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;
|
||||
95
test/specificClass.test.js
Normal file
95
test/specificClass.test.js
Normal file
@@ -0,0 +1,95 @@
|
||||
const Monster = require('../src/specificClass');
|
||||
|
||||
describe('monster specificClass', () => {
|
||||
function createMonster(overrides = {}) {
|
||||
return new Monster({
|
||||
general: {
|
||||
name: 'Monster Test',
|
||||
unit: 'm3/h',
|
||||
logging: {
|
||||
enabled: false,
|
||||
logLevel: 'error',
|
||||
},
|
||||
},
|
||||
asset: {
|
||||
emptyWeightBucket: 3,
|
||||
},
|
||||
constraints: {
|
||||
samplingtime: 1,
|
||||
minVolume: 5,
|
||||
maxWeight: 23,
|
||||
},
|
||||
functionality: {
|
||||
aquonSampleName: '112100',
|
||||
},
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
test('aggregates rain data and exposes output state', () => {
|
||||
const monster = createMonster();
|
||||
|
||||
monster.rain_data = [
|
||||
{
|
||||
latitude: 51.7,
|
||||
longitude: 4.81,
|
||||
hourly: {
|
||||
time: ['2026-03-12T00:00', '2026-03-12T01:00'],
|
||||
precipitation: [1, 3],
|
||||
precipitation_probability: [100, 50],
|
||||
},
|
||||
},
|
||||
{
|
||||
latitude: 51.8,
|
||||
longitude: 4.91,
|
||||
hourly: {
|
||||
time: ['2026-03-12T00:00', '2026-03-12T01:00'],
|
||||
precipitation: [2, 2],
|
||||
precipitation_probability: [100, 100],
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const output = monster.getOutput();
|
||||
|
||||
expect(monster.sumRain).toBe(6.5);
|
||||
expect(monster.avgRain).toBe(3.25);
|
||||
expect(output.sumRain).toBe(6.5);
|
||||
expect(output.avgRain).toBe(3.25);
|
||||
});
|
||||
|
||||
test('supports external prediction input and starts sampling safely', () => {
|
||||
const monster = createMonster();
|
||||
|
||||
monster.setModelPrediction(120);
|
||||
monster.q = 3600;
|
||||
monster.i_start = true;
|
||||
monster.flowTime = Date.now() - 1000;
|
||||
|
||||
monster.tick();
|
||||
|
||||
const output = monster.getOutput();
|
||||
expect(output.running).toBe(true);
|
||||
expect(output.predFlow).toBe(120);
|
||||
expect(output.predM3PerSec).toBeCloseTo(120 / 3600, 6);
|
||||
});
|
||||
|
||||
test('calculates the next AQUON date from monsternametijden input', () => {
|
||||
const monster = createMonster();
|
||||
const nextMonth = new Date();
|
||||
nextMonth.setMonth(nextMonth.getMonth() + 1);
|
||||
|
||||
monster.monsternametijden = [
|
||||
{
|
||||
SAMPLE_NAME: '112100',
|
||||
DESCRIPTION: 'future sample',
|
||||
SAMPLED_DATE: null,
|
||||
START_DATE: nextMonth.toISOString(),
|
||||
END_DATE: nextMonth.toISOString(),
|
||||
},
|
||||
];
|
||||
|
||||
expect(monster.daysPerYear).toBeGreaterThanOrEqual(0);
|
||||
expect(monster.nextDate).toBeGreaterThan(Date.now());
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user