@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.
67 lines (52 loc) • 2.54 kB
JavaScript
const { exec } = require('child_process');
module.exports = function(RED) {
function Cm4ProDigitalInput(config) {
RED.nodes.createNode(this, config);
var node = this;
// Object to store the last known state of the inputs
let lastState = { DIN1: null, DIN2: null };
// Output objects for each pin
var outputs = {
DIN1: { Value: false },
DIN2: { Value: false }
};
// Polling function to execute the command
const pollGpioStatus = () => {
let command = `/var/www/html/commands/cm4ProDigitalInput.sh`;
exec(command, (error, stdout, stderr) => {
if (error) {
node.error(`Execution Error: ${stderr}`);
node.status({ fill: "red", shape: "ring", text: "Error executing command" });
return;
}
stdout = stdout.trim();
var lines = stdout.split('\n');
lines.forEach(line => {
let [pinStatus, status] = line.split(': ');
let pin = pinStatus.trim().split(' ')[0]; // Assumes format "DIN1 Status"
let state = status.trim() === '1';
//flip the state so that 0 is true and 1 is false
state = !state;
if (lastState[pin] !== state) {
lastState[pin] = state; // Update the last known state
outputs[pin].Value = state; // Update the output value
// Prepare the message object for each pin
if (pin === "DIN1") {
node.send([{ payload: outputs[pin] }, null]); // Send only on first output
} else if (pin === "DIN2") {
node.send([null, { payload: outputs[pin] }]); // Send only on second output
}
}
});
//node.status({ fill: "green", shape: "dot", text: "Output processed successfully" });
});
};
// Start polling at the specified interval (250ms)
const intervalId = setInterval(pollGpioStatus, 250);
// Clean up on node close
node.on('close', function() {
clearInterval(intervalId); // Stop polling when the node is redeployed or Node-RED is stopped
});
}
RED.nodes.registerType("cm4-pro-digital-input", Cm4ProDigitalInput);
};