UNPKG

node-red-contrib-minelert-universal-io

Version:

Node-RED nodes for Universal IO Gateway and I/O Modules

47 lines (37 loc) 1.38 kB
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 && data.ioType === 'ai') { node.send({ // topic: `analog-in/${moduleId}/channel/${data.channel}`, payload: { value: data.value, channel: data.channel, 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}` }); } } // Subscribe to event bus EventBus.on("io-channel-event", onAnalogReading); node.on("close", () => { EventBus.off("io-channel-event", onAnalogReading); node.status({}); }); } RED.nodes.registerType("analog-in", AnalogInNode); };