Files
measurement/measurement.js
lzm d7f6613892 refactor(measurement): modularize editor JS
Move inline <script> from measurement.html into 8 modules under
src/editor/. measurement.js adds the static-file routes that serve them
to Node-RED.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 09:10:05 +02:00

76 lines
3.4 KiB
JavaScript

const nameOfNode = 'measurement'; // this is the name of the node, it should match the file name and the node type in Node-RED
const path = require('path');
const nodeClass = require('./src/nodeClass.js'); // this is the specific node class
const { MenuManager, configManager, assetApiConfig } = require('generalFunctions');
const assetUtils = require('generalFunctions/assetUtils');
// 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, ['asset','logger','position']);
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}`);
}
});
// Editor JS modules — loaded by measurement.html via <script src=...> tags.
// Files live in src/editor/. Filename is restricted to a safe charset to
// prevent path-traversal. Mirror of the pumpingStation pattern.
RED.httpAdmin.get(`/${nameOfNode}/editor/:file`, (req, res) => {
const safe = String(req.params.file || '').replace(/[^a-zA-Z0-9._-]/g, '');
if (!safe.endsWith('.js')) return res.status(400).send('// invalid');
res.type('application/javascript');
res.sendFile(path.join(__dirname, 'src', 'editor', safe), (err) => {
if (err && !res.headersSent) res.status(404).send('// editor module not found');
});
});
RED.httpAdmin.post(`/${nameOfNode}/asset-reg`, async (req, res) => {
const body = req.body || {};
const assetPayload = body.asset;
if (!assetPayload) {
return res.status(400).json({ success: false, message: 'Missing asset payload' });
}
try {
const nodeConfig = cfgMgr.getConfig(nameOfNode);
const registrationDefaults = (nodeConfig && nodeConfig.assetRegistration && nodeConfig.assetRegistration.default) || {};
const result = await assetUtils.syncAsset({
assetSelection: assetPayload,
registrationDefaults,
apiConfig: assetApiConfig,
nodeContext: { id: body.nodeId, name: body.nodeName }
});
res.json({ success: result.success, data: result.data, message: result.message });
} catch (error) {
console.error(`[${nameOfNode}] asset-reg error`, error);
res.status(500).json({ success: false, message: error.message });
}
});
};