node-red-contrib-minelert-universal-io
Version:
Node-RED nodes for Universal IO Gateway and I/O Modules
57 lines (41 loc) • 1.61 kB
JavaScript
const { EventBus, logInfo } = require('@minelert-smartlink/common');
module.exports = function(RED) {
function AnalogOutNode(config) {
RED.nodes.createNode(this, config);
const node = this;
const gatewayNode = RED.nodes.getNode(config.gateway);
const moduleId = config.moduleId || 'MOD-XXXX';
let channel = Number(config.channel) || 0;
node.channelValues = [null, null, null, null]; // Channel 0–3
if (!gatewayNode) {
node.error("Gateway not configured");
return;
}
node.on('input', (msg, send, done) => {
const ch = msg.payload.channel
const value = msg.payload.value || 20
channel = (typeof ch === 'undefined') ? channel : ch;
if (typeof channel !== 'number' || channel < 0 || channel > 3) {
node.error(`Invalid channel: ${channel}`, msg);
if (done) done();
return;
}
node.channelValues[channel] = value;
// Log or send to serial/gateway here
node.status({ fill: "green", shape: "dot", text: `CH${channel}: ${value}` });
const command = {
current: value,
channel: channel,
moduleId: moduleId,
};
const topic = `smartlink/commands/${moduleId}/analogOutput`
logInfo(`Analog Output Command ${JSON.stringify(command)}`);
EventBus.emit('send-command', {topic, command});
if (done) done();
});
node.on('close', () => {
node.status({});
});
}
RED.nodes.registerType("analog-out", AnalogOutNode);
};