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