node-red-contrib-minelert-universal-io
Version:
Node-RED nodes for Universal IO Gateway and I/O Modules
53 lines (38 loc) • 1.46 kB
JavaScript
const { EventBus, logInfo } = require('@minelert-smartlink/common');
module.exports = function(RED) {
function DigitalOutNode(config) {
RED.nodes.createNode(this, config);
const node = this;
const gatewayNode = RED.nodes.getNode(config.gateway);
const moduleId = config.moduleId || 'MOD-XXXX';
node.channelValues = [null, null, null, null]; // Channel 0–3
if (!gatewayNode) {
node.error("Gateway not configured");
return;
}
node.on('input', (msg, send, done) => {
const { channel, value} = msg.payload;
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 = {
value: value,
channel: channel,
moduleId: moduleId,
};
const topic = `smartlink/commands/${moduleId}/digitalOutput`
logInfo(`Digital Output Command ${JSON.stringify(command)}`);
EventBus.emit('send-command', {topic, command});
if (done) done();
});
node.on('close', () => {
node.status({});
});
}
RED.nodes.registerType("digital-out", DigitalOutNode);
};