node-red-contrib-minelert-universal-io
Version:
Node-RED nodes for Universal IO Gateway and I/O Modules
49 lines (38 loc) • 1.38 kB
JavaScript
const { EventBus, logInfo } = require('@minelert-smartlink/common');
module.exports = function(RED) {
function DigitalInNode(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;
}
function onDigitalReading(data) {
if (data.moduleId === moduleId) {
node.send({
topic: `digital-in/${moduleId}/channel/${data.channel}`,
payload: {
value: data.value,
channel: data.channel,
}
// raw: data
});
node.channelValues[data.channel] = data.value;
// Log or send to serial/gateway here
node.status({ fill: "green", shape: "dot", text: `CH${data.channel}: ${data.value}` });
}
}
// Subscribe to event bus
EventBus.on("io-state-change", onDigitalReading);
node.on("close", () => {
EventBus.off("io-state-change", onDigitalReading);
});
node.on('close', () => {
node.status({});
});
}
RED.nodes.registerType("digital-in", DigitalInNode);
};