@parallaxcontrol/node-red-control
Version:
A comprehensive package of Node-RED nodes designed to seamlessly integrate with the Parallax Control Engine, offering enhanced control capabilities for AV Automation.
41 lines (34 loc) • 1.5 kB
JavaScript
module.exports = function(RED) {
function AndGateNode(config) {
RED.nodes.createNode(this, config);
var node = this;
var numInputs = config.numInputs || 2;
var inputs = {};
// Initialize inputs
for (let i = 1; i <= numInputs; i++) {
inputs[i] = false;
}
node.on('input', function(msg) {
//and-gate v2
if (typeof msg.payload.Input === 'number' && typeof msg.payload.Value === 'boolean') {
//Key = Input Selection, Value = Boolean
inputs[msg.payload.Input] = msg.payload.Value;
var output = Object.values(inputs).every(val => val === true);
var message = { Value : output };
node.send({payload : message});
//msg.payload = output;
// Set status based on output
if(output) {
node.status({fill: "green", shape: "dot", text: "true"});
} else {
node.status({fill: "red", shape: "dot", text: "false"});
}
} else {
// Set error status and log error if the expected structure is not found
node.status({fill: "red", shape: "ring", text: "Invalid input"});
node.error("Invalid input: Expected JSON Object, i.e.: {\"Input\":1,\"Value\":true}");
}
});
}
RED.nodes.registerType("and-gate", AndGateNode);
}