added basic basin class
This commit is contained in:
244
basin.html
Normal file
244
basin.html
Normal file
@@ -0,0 +1,244 @@
|
|||||||
|
|
||||||
|
<script src="/measurement/menu.js"></script> <!-- Load the menu script for dynamic dropdowns -->
|
||||||
|
<script src="/measurement/configData.js"></script> <!-- Load the config script for node information -->
|
||||||
|
|
||||||
|
<script>
|
||||||
|
RED.nodes.registerType("measurement", {
|
||||||
|
category: "EVOLV",
|
||||||
|
color: "#e4a363", // color for the node based on the S88 schema
|
||||||
|
defaults: {
|
||||||
|
|
||||||
|
// Define default properties
|
||||||
|
name: { value: "sensor" }, // use asset category as name
|
||||||
|
|
||||||
|
// Define specific properties
|
||||||
|
scaling: { value: false },
|
||||||
|
i_min: { value: 0, required: true },
|
||||||
|
i_max: { value: 0, required: true },
|
||||||
|
i_offset: { value: 0 },
|
||||||
|
o_min: { value: 0, required: true },
|
||||||
|
o_max: { value: 1, required: true },
|
||||||
|
simulator: { value: false },
|
||||||
|
smooth_method: { value: "" },
|
||||||
|
count: { value: "10", required: true },
|
||||||
|
|
||||||
|
//define asset properties
|
||||||
|
uuid: { value: "" },
|
||||||
|
supplier: { value: "" },
|
||||||
|
category: { value: "" },
|
||||||
|
assetType: { value: "" },
|
||||||
|
model: { value: "" },
|
||||||
|
unit: { value: "" },
|
||||||
|
|
||||||
|
//logger properties
|
||||||
|
enableLog: { value: false },
|
||||||
|
logLevel: { value: "error" },
|
||||||
|
|
||||||
|
//physicalAspect
|
||||||
|
positionVsParent: { value: "" },
|
||||||
|
positionIcon: { value: "" },
|
||||||
|
hasDistance: { value: false },
|
||||||
|
distance: { value: 0 },
|
||||||
|
distanceUnit: { value: "m" },
|
||||||
|
distanceDescription: { value: "" }
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
inputs: 1,
|
||||||
|
outputs: 3,
|
||||||
|
inputLabels: ["Input"],
|
||||||
|
outputLabels: ["process", "dbase", "parent"],
|
||||||
|
icon: "font-awesome/fa-tachometer",
|
||||||
|
|
||||||
|
label: function () {
|
||||||
|
return this.positionIcon + " " + this.assetType || "Measurement";
|
||||||
|
},
|
||||||
|
|
||||||
|
oneditprepare: function() {
|
||||||
|
const waitForMenuData = () => {
|
||||||
|
if (window.EVOLV?.nodes?.measurement?.initEditor) {
|
||||||
|
window.EVOLV.nodes.measurement.initEditor(this);
|
||||||
|
} else {
|
||||||
|
setTimeout(waitForMenuData, 50);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// Wait for the menu data to be ready before initializing the editor
|
||||||
|
waitForMenuData();
|
||||||
|
|
||||||
|
// THIS IS NODE SPECIFIC --------------- Initialize the dropdowns and other specific UI elements -------------- this should be derived from the config in the future (make config based menu)
|
||||||
|
// Populate smoothing methods dropdown
|
||||||
|
const smoothMethodSelect = document.getElementById('node-input-smooth_method');
|
||||||
|
const options = window.EVOLV?.nodes?.measurement?.config?.smoothing?.smoothMethod?.rules?.values || [];
|
||||||
|
|
||||||
|
// Clear existing options
|
||||||
|
smoothMethodSelect.innerHTML = '';
|
||||||
|
|
||||||
|
// Add empty option
|
||||||
|
const emptyOption = document.createElement('option');
|
||||||
|
emptyOption.value = '';
|
||||||
|
emptyOption.textContent = 'Select method...';
|
||||||
|
smoothMethodSelect.appendChild(emptyOption);
|
||||||
|
|
||||||
|
// Add smoothing method options
|
||||||
|
options.forEach(option => {
|
||||||
|
const optionElement = document.createElement('option');
|
||||||
|
optionElement.value = option.value;
|
||||||
|
optionElement.textContent = option.value;
|
||||||
|
optionElement.title = option.description; // Add tooltip with full description
|
||||||
|
smoothMethodSelect.appendChild(optionElement);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Set current value if it exists
|
||||||
|
if (this.smooth_method) {
|
||||||
|
smoothMethodSelect.value = this.smooth_method;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Scale rows toggle ---
|
||||||
|
const chk = document.getElementById('node-input-scaling');
|
||||||
|
const rowMin = document.getElementById('row-input-i_min');
|
||||||
|
const rowMax = document.getElementById('row-input-i_max');
|
||||||
|
|
||||||
|
function toggleScalingRows() {
|
||||||
|
const show = chk.checked;
|
||||||
|
rowMin.style.display = show ? 'block' : 'none';
|
||||||
|
rowMax.style.display = show ? 'block' : 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
// wire and initialize
|
||||||
|
chk.addEventListener('change', toggleScalingRows);
|
||||||
|
toggleScalingRows();
|
||||||
|
|
||||||
|
//------------------- END OF CUSTOM config UI ELEMENTS ------------------- //
|
||||||
|
},
|
||||||
|
oneditsave: function () {
|
||||||
|
const node = this;
|
||||||
|
|
||||||
|
// Validate asset properties using the asset menu
|
||||||
|
if (window.EVOLV?.nodes?.measurement?.assetMenu?.saveEditor) {
|
||||||
|
success = window.EVOLV.nodes.measurement.assetMenu.saveEditor(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate logger properties using the logger menu
|
||||||
|
if (window.EVOLV?.nodes?.measurement?.loggerMenu?.saveEditor) {
|
||||||
|
success = window.EVOLV.nodes.measurement.loggerMenu.saveEditor(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
// save position field
|
||||||
|
if (window.EVOLV?.nodes?.measurement?.positionMenu?.saveEditor) {
|
||||||
|
window.EVOLV.nodes.measurement.positionMenu.saveEditor(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save basic properties
|
||||||
|
["smooth_method"].forEach(
|
||||||
|
(field) => (node[field] = document.getElementById(`node-input-${field}`).value || "")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Save numeric and boolean properties
|
||||||
|
["scaling", "simulator"].forEach(
|
||||||
|
(field) => (node[field] = document.getElementById(`node-input-${field}`).checked)
|
||||||
|
);
|
||||||
|
|
||||||
|
["i_min", "i_max", "i_offset", "o_min", "o_max", "count"].forEach(
|
||||||
|
(field) => (node[field] = parseFloat(document.getElementById(`node-input-${field}`).value) || 0)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Validation checks
|
||||||
|
if (node.scaling && (isNaN(node.i_min) || isNaN(node.i_max))) {
|
||||||
|
RED.notify("Scaling enabled, but input range is incomplete!", "error");
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<!-- Main UI -->
|
||||||
|
|
||||||
|
<script type="text/html" data-template-name="measurement">
|
||||||
|
|
||||||
|
<!-- Scaling Checkbox -->
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-scaling"
|
||||||
|
><i class="fa fa-compress"></i> Scaling</label>
|
||||||
|
<input type="checkbox" id="node-input-scaling" style="width:20px; vertical-align:baseline;"/>
|
||||||
|
<span>Enable input scaling?</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Source Min/Max (only if scaling is true) -->
|
||||||
|
<div class="form-row" id="row-input-i_min">
|
||||||
|
<label for="node-input-i_min"><i class="fa fa-arrow-down"></i> Source Min</label>
|
||||||
|
<input type="number" id="node-input-i_min" placeholder="0" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-row" id="row-input-i_max">
|
||||||
|
<label for="node-input-i_max"><i class="fa fa-arrow-up"></i> Source Max</label>
|
||||||
|
<input type="number" id="node-input-i_max" placeholder="3000" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Offset -->
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-i_offset"><i class="fa fa-adjust"></i> Input Offset</label>
|
||||||
|
<input type="number" id="node-input-i_offset" placeholder="0" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Output / Process Min/Max -->
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-o_min"><i class="fa fa-tag"></i> Process Min</label>
|
||||||
|
<input type="number" id="node-input-o_min" placeholder="0" />
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-o_max"><i class="fa fa-tag"></i> Process Max</label>
|
||||||
|
<input type="number" id="node-input-o_max" placeholder="1" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Simulator Checkbox -->
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-simulator"><i class="fa fa-cog"></i> Simulator</label>
|
||||||
|
<input type="checkbox" id="node-input-simulator" style="width:20px; vertical-align:baseline;"/>
|
||||||
|
<span>Activate internal simulation?</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Smoothing Method -->
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-smooth_method"><i class="fa fa-line-chart"></i> Smoothing</label>
|
||||||
|
<select id="node-input-smooth_method" style="width:60%;">
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Smoothing Window -->
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-count">Window</label>
|
||||||
|
<input type="number" id="node-input-count" placeholder="10" style="width:60px;"/>
|
||||||
|
<div class="form-tips">Number of samples for smoothing</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Optional Extended Fields: supplier, cat, type, model, unit -->
|
||||||
|
<!-- Asset fields will be injected here -->
|
||||||
|
<div id="asset-fields-placeholder"></div>
|
||||||
|
|
||||||
|
<!-- loglevel checkbox -->
|
||||||
|
<div id="logger-fields-placeholder"></div>
|
||||||
|
|
||||||
|
<!-- Position fields will be injected here -->
|
||||||
|
<div id="position-fields-placeholder"></div>
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
<script type="text/html" data-help-name="measurement">
|
||||||
|
<p><b>Measurement Node</b>: Scales, smooths, and simulates measurement data.</p>
|
||||||
|
<p>Use this node to scale, smooth, and simulate measurement data. The node can be configured to scale input data to a specified range, smooth the data using a variety of methods, and simulate data for testing purposes.</p>
|
||||||
|
<li><b>Supplier:</b> Select a supplier to populate machine options.</li>
|
||||||
|
<li><b>SubType:</b> Select a subtype if applicable to further categorize the asset.</li>
|
||||||
|
<li><b>Model:</b> Define the specific model for more granular asset configuration.</li>
|
||||||
|
<li><b>Unit:</b> Assign a unit to standardize measurements or operations.</li>
|
||||||
|
<li><b>Scaling:</b> Enable or disable input scaling. When enabled, you must provide the source min and max values.</li>
|
||||||
|
<li><b>Source Min/Max:</b> Define the minimum and maximum values for the input range when scaling is enabled.</li>
|
||||||
|
<li><b>Input Offset:</b> Specify an offset value to be added to the input measurement.</li>
|
||||||
|
<li><b>Process Min/Max:</b> Define the minimum and maximum values for the output range after processing.</li>
|
||||||
|
<li><b>Simulator:</b> Activate internal simulation for testing purposes.</li>
|
||||||
|
<li><b>Smoothing:</b> Select a smoothing method to apply to the measurement data.</li>
|
||||||
|
<li><b>Window:</b> Define the number of samples to use for smoothing.</li>
|
||||||
|
<li><b>Enable Log:</b> Enable or disable logging for this node.</li>
|
||||||
|
<li><b>Log Level:</b> Select the log level (Info, Debug, Warn, Error) for logging messages.</li>
|
||||||
|
|
||||||
|
</script>
|
||||||
40
basin.js
Normal file
40
basin.js
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
const nameOfNode = 'basin'; // this is the name of the node, it should match the file name and the node type in Node-RED
|
||||||
|
const nodeClass = require('./src/nodeClass.js'); // this is the specific node class
|
||||||
|
const { MenuManager, configManager } = require('generalFunctions');
|
||||||
|
|
||||||
|
// This is the main entry point for the Node-RED node, it will register the node and setup the endpoints
|
||||||
|
module.exports = function(RED) {
|
||||||
|
// Register the node type
|
||||||
|
RED.nodes.registerType(nameOfNode, function(config) {
|
||||||
|
// Initialize the Node-RED node first
|
||||||
|
RED.nodes.createNode(this, config);
|
||||||
|
// Then create your custom class and attach it
|
||||||
|
this.nodeClass = new nodeClass(config, RED, this, nameOfNode);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Setup admin UIs
|
||||||
|
const menuMgr = new MenuManager(); //this will handle the menu endpoints so we can load them dynamically
|
||||||
|
const cfgMgr = new configManager(); // this will handle the config endpoints so we can load them dynamically
|
||||||
|
|
||||||
|
// Register the different menu's for the measurement node (in the future we could automate this further by refering to the config)
|
||||||
|
RED.httpAdmin.get(`/${nameOfNode}/menu.js`, (req, res) => {
|
||||||
|
try {
|
||||||
|
const script = menuMgr.createEndpoint(nameOfNode, ['logger']);
|
||||||
|
res.type('application/javascript').send(script);
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).send(`// Error generating menu: ${err.message}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Endpoint to get the configuration data for the specific node
|
||||||
|
RED.httpAdmin.get(`/${nameOfNode}/configData.js`, (req, res) => {
|
||||||
|
try {
|
||||||
|
const script = cfgMgr.createEndpoint(nameOfNode);
|
||||||
|
// Send the configuration data as JSON response
|
||||||
|
res.type('application/javascript').send(script);
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).send(`// Error generating configData: ${err.message}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
||||||
29
package.json
Normal file
29
package.json
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"name": "basin",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Control module",
|
||||||
|
"main": "basin.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "node basin.js"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://gitea.centraal.wbd-rd.nl/RnD/basin.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"basin",
|
||||||
|
"node-red",
|
||||||
|
"recipient",
|
||||||
|
"water"
|
||||||
|
],
|
||||||
|
"author": "Rene De Ren",
|
||||||
|
"license": "SEE LICENSE",
|
||||||
|
"dependencies": {
|
||||||
|
"generalFunctions": "git+https://gitea.centraal.wbd-rd.nl/RnD/generalFunctions.git"
|
||||||
|
},
|
||||||
|
"node-red": {
|
||||||
|
"nodes": {
|
||||||
|
"basin": "basin.js"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
153
src/nodeClass.js
Normal file
153
src/nodeClass.js
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
/**
|
||||||
|
* basin.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");
|
||||||
|
|
||||||
|
class nodeClass {
|
||||||
|
/**
|
||||||
|
* Create a node.
|
||||||
|
* @param {object} uiConfig - Node-RED node configuration.
|
||||||
|
* @param {object} RED - Node-RED runtime API.
|
||||||
|
* @param {object} nodeInstance - The Node-RED node instance.
|
||||||
|
* @param {string} nameOfNode - The name of the node, used for
|
||||||
|
*/
|
||||||
|
constructor(uiConfig, RED, nodeInstance, nameOfNode) {
|
||||||
|
|
||||||
|
// Preserve RED reference for HTTP endpoints if needed
|
||||||
|
this.node = nodeInstance;
|
||||||
|
this.RED = RED;
|
||||||
|
this.name = nameOfNode;
|
||||||
|
|
||||||
|
// Load default & UI config
|
||||||
|
this._loadConfig(uiConfig,this.node);
|
||||||
|
|
||||||
|
// Instantiate core 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.
|
||||||
|
* @param {object} uiConfig - Raw config from Node-RED UI.
|
||||||
|
*/
|
||||||
|
_loadConfig(uiConfig,node) {
|
||||||
|
const cfgMgr = new configManager();
|
||||||
|
this.defaultConfig = cfgMgr.getConfig(this.name);
|
||||||
|
|
||||||
|
// Merge UI config over defaults
|
||||||
|
this.config = {
|
||||||
|
general: {
|
||||||
|
name: this.name,
|
||||||
|
id: node.id, // node.id is for the child registration process
|
||||||
|
unit: uiConfig.unit, // add converter options later to convert to default units (need like a model that defines this which units we are going to use and then conver to those standards)
|
||||||
|
logging: {
|
||||||
|
enabled: uiConfig.enableLog,
|
||||||
|
logLevel: uiConfig.logLevel
|
||||||
|
}
|
||||||
|
},
|
||||||
|
functionality: {
|
||||||
|
positionVsParent: uiConfig.positionVsParent,// Default to 'atEquipment' if not specified
|
||||||
|
distance: uiConfig.hasDistance ? uiConfig.distance : undefined
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 Node-RED status updates.
|
||||||
|
*/
|
||||||
|
_bindEvents() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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();
|
||||||
|
|
||||||
|
const raw = this.source.getOutput();
|
||||||
|
const processMsg = this._output.formatMsg(raw, this.config, 'process');
|
||||||
|
const influxMsg = this._output.formatMsg(raw, this.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) => {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean up timers and intervals when Node-RED stops the node.
|
||||||
|
*/
|
||||||
|
_attachCloseHandler() {
|
||||||
|
this.node.on('close', (done) => {
|
||||||
|
clearInterval(this._tickInterval);
|
||||||
|
//clearInterval(this._statusInterval);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = nodeClass;
|
||||||
181
src/specificClass.js
Normal file
181
src/specificClass.js
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
const EventEmitter = require('events');
|
||||||
|
const {logger,configUtils,configManager,MeasurementContainer,coolprop} = require('generalFunctions');
|
||||||
|
|
||||||
|
class Basin {
|
||||||
|
constructor(config={}) {
|
||||||
|
|
||||||
|
this.emitter = new EventEmitter(); // Own EventEmitter
|
||||||
|
this.configManager = new configManager();
|
||||||
|
this.defaultConfig = this.configManager.getConfig('basin');
|
||||||
|
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
|
||||||
|
});
|
||||||
|
|
||||||
|
// Basin-specific properties
|
||||||
|
this.flowrate = null; // Function to calculate flow rate based on water level rise or fall
|
||||||
|
this.timeBeforeOverflow = null; // Time before the basin overflows at current inflow rate
|
||||||
|
this.timeBeforeEmpty = null; // Time before the basin empties at current outflow rate
|
||||||
|
this.heightInlet = null; // Height of the inlet pipe from the bottom of the basin
|
||||||
|
this.heightOutlet = null; // Height of the outlet pipe from the bottom of the basin
|
||||||
|
this.heightOverflow = null; // Height of the overflow point from the bottom of the basin
|
||||||
|
this.volume = null; // Total volume of water in the basin, calculated from water level and basin dimensions
|
||||||
|
this.emptyVolume = null; // Volume in the basin when empty (at level of outlet pipe)
|
||||||
|
this.fullVolume = null; // Volume in the basin when at level of overflow point
|
||||||
|
this.crossSectionalArea = null; // Cross-sectional area of the basin, used to calculate volume from water level
|
||||||
|
|
||||||
|
// Initialize basin-specific properties from config
|
||||||
|
this.initBasinProperties();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*------------------- Register child events -------------------*/
|
||||||
|
registerChild(child, softwareType) {
|
||||||
|
this.logger.debug('Setting up child event for softwaretype ' + softwareType);
|
||||||
|
|
||||||
|
if(softwareType === "measurement"){
|
||||||
|
const position = child.config.functionality.positionVsParent;
|
||||||
|
const distance = child.config.functionality.distanceVsParent || 0;
|
||||||
|
const measurementType = child.config.asset.type;
|
||||||
|
const key = `${measurementType}_${position}`;
|
||||||
|
//rebuild to measurementype.variant no position and then switch based on values not strings or names.
|
||||||
|
const eventName = `${measurementType}.measured.${position}`;
|
||||||
|
|
||||||
|
this.logger.debug(`Setting up listener for ${eventName} from child ${child.config.general.name}`);
|
||||||
|
// Register event listener for measurement updates
|
||||||
|
child.measurements.emitter.on(eventName, (eventData) => {
|
||||||
|
this.logger.debug(`🔄 ${position} ${measurementType} from ${eventData.childName}: ${eventData.value} ${eventData.unit}`);
|
||||||
|
|
||||||
|
console.log(` Emitting... ${eventName} with data:`);
|
||||||
|
// Store directly in parent's measurement container
|
||||||
|
this.measurements
|
||||||
|
.type(measurementType)
|
||||||
|
.variant("measured")
|
||||||
|
.position(position)
|
||||||
|
.value(eventData.value, eventData.timestamp, eventData.unit);
|
||||||
|
|
||||||
|
// Call the appropriate handler
|
||||||
|
this._callMeasurementHandler(measurementType, eventData.value, position, eventData);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_callMeasurementHandler(measurementType, value, position, context) {
|
||||||
|
switch (measurementType) {
|
||||||
|
case 'pressure':
|
||||||
|
this.updateMeasuredPressure(value, position, context);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'flow':
|
||||||
|
this.updateMeasuredFlow(value, position, context);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'temperature':
|
||||||
|
this.updateMeasuredTemperature(value, position, context);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'level':
|
||||||
|
this.updateMeasuredLevel(value, position, context);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
this.logger.warn(`No handler for measurement type: ${measurementType}`);
|
||||||
|
// Generic handler - just update position
|
||||||
|
this.updatePosition();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// context handler for pressure updates
|
||||||
|
updateMeasuredPressure(value, position, context = {}) {
|
||||||
|
|
||||||
|
let kelvinTemp = null;
|
||||||
|
|
||||||
|
//pressure updates come from pressure boxes inside the basin they get converted to a level and stored as level measured at position inlet or outlet
|
||||||
|
this.logger.debug(`Pressure update: ${value} at ${position} from ${context.childName || 'child'} (${context.childId || 'unknown-id'})`);
|
||||||
|
|
||||||
|
// Store in parent's measurement container for the first time
|
||||||
|
this.measurements.type("pressure").variant("measured").position(position).value(value, context.timestamp, context.unit);
|
||||||
|
|
||||||
|
//convert pressure to level based on density of water and height of pressure sensor
|
||||||
|
const mTemp = this.measurements.type("temperature").variant("measured").position("atEquipment").getCurrentValue('K'); //default to 20C if no temperature measurement
|
||||||
|
|
||||||
|
//prefer measured temp but otherwise assume nominal temp for wastewater
|
||||||
|
if(mTemp === null){
|
||||||
|
this.logger.warn(`No temperature measurement available, defaulting to 15C for pressure to level conversion.`);
|
||||||
|
this.measurements.type("temperature").variant("assumed").position("atEquipment").value(15, Date.now(), "C");
|
||||||
|
kelvinTemp = this.measurements.type('temperature').variant('assumed').position('atEquipment').getCurrentValue('K');
|
||||||
|
} else {
|
||||||
|
kelvinTemp = mTemp;
|
||||||
|
}
|
||||||
|
this.logger.debug(`Using temperature: ${kelvinTemp} K for calculations`);
|
||||||
|
const density = coolprop.PropsSI('D','T',kelvinTemp,'P',101325,'Water'); //density in kg/m3 at temp and surface pressure
|
||||||
|
const g =
|
||||||
|
|
||||||
|
//calculate how muc flow went in or out based on pressure difference
|
||||||
|
this.logger.debug(`Using pressure: ${pressure} for calculations`);
|
||||||
|
}
|
||||||
|
|
||||||
|
initBasinProperties() {
|
||||||
|
// Initialize basin-specific properties from config
|
||||||
|
this.heightInlet = this.config.basin.heightInlet || 0; // Default to 0 if not specified
|
||||||
|
this.heightOutlet = this.config.basin.heightOutlet || 0; // Default to 0 if not specified
|
||||||
|
this.heightOverflow = this.config.basin.heightOverflow || 0; // Default to 0 if not specified
|
||||||
|
this.crossSectionalArea = this.config.basin.crossSectionalArea || 1; // Default to 1 m² if not specified
|
||||||
|
}
|
||||||
|
|
||||||
|
measurement
|
||||||
|
|
||||||
|
getOutput() {
|
||||||
|
return {
|
||||||
|
volume: this.volume,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Basin;
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
// */
|
||||||
|
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
const PropsSI = await coolprop.getPropsSI();
|
||||||
|
|
||||||
|
// 👇 replace these with your real inputs
|
||||||
|
const tC_input = 25; // °C
|
||||||
|
const pPa_input = 101325; // Pa
|
||||||
|
|
||||||
|
// Sanitize & convert
|
||||||
|
const T = Number(tC_input) + 273.15; // K
|
||||||
|
const P = Number(pPa_input); // Pa
|
||||||
|
const fluid = 'Water';
|
||||||
|
|
||||||
|
// Preconditions
|
||||||
|
if (!Number.isFinite(T) || !Number.isFinite(P)) {
|
||||||
|
throw new Error(`Bad inputs: T=${T} K, P=${P} Pa`);
|
||||||
|
}
|
||||||
|
if (T <= 0) throw new Error(`Temperature must be in Kelvin (>0). Got ${T}.`);
|
||||||
|
if (P <= 0) throw new Error(`Pressure must be >0 Pa. Got ${P}.`);
|
||||||
|
|
||||||
|
// Try T,P order
|
||||||
|
let rho = PropsSI('D', 'T', T, 'P', P, fluid);
|
||||||
|
// Fallback: P,T order (should be equivalent)
|
||||||
|
if (!Number.isFinite(rho)) rho = PropsSI('D', 'P', P, 'T', T, fluid);
|
||||||
|
|
||||||
|
console.log({ T, P, rho });
|
||||||
|
|
||||||
|
if (!Number.isFinite(rho)) {
|
||||||
|
console.error('Still Infinity. Extra checks:');
|
||||||
|
console.error('typeof T:', typeof T, 'typeof P:', typeof P);
|
||||||
|
console.error('Example known-good call:', PropsSI('D', 'T', 298.15, 'P', 101325, 'Water'));
|
||||||
|
}
|
||||||
|
})();
|
||||||
Reference in New Issue
Block a user