node-red-contrib-minelert-universal-io
Version:
Node-RED nodes for Universal IO Gateway and I/O Modules
50 lines (39 loc) • 1.42 kB
JavaScript
const { EventBus, logInfo } = require('@minelert-smartlink/common');
module.exports = function(RED) {
function AnalogInNode(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 onAnalogReading(data) {
if (data.moduleId === moduleId) {
node.send({
topic: `analog-in/${moduleId}/channel/${data.channel}`,
payload: {
value: data.value,
channel: data.channel,
unit: data.unit || 'mA'
}
// 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} ${data.unit}` });
}
}
// Subscribe to event bus
EventBus.on("io-state-change", onAnalogReading);
node.on("close", () => {
EventBus.off("io-state-change", onAnalogReading);
});
node.on('close', () => {
node.status({});
});
}
RED.nodes.registerType("analog-in", AnalogInNode);
};