@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.
71 lines (68 loc) • 4.16 kB
JavaScript
// Import the exec module
const { exec } = require('child_process');
module.exports = function(RED) {
function Cm4ProRelay(config) {
RED.nodes.createNode(this, config);
var node = this;
node.on('input', function(msg) {
// relay v2
if (typeof msg.payload.Input === 'number' && typeof msg.payload.Value === 'boolean') {
if (msg.payload.Value === true) {
switch (msg.payload.Input) {
case 1:
{
// Execute the command to turn the relay on
exec("sudo /var/www/html/commands/cm4ProRelay.sh on", { shell: '/bin/bash' }, (error, stdout, stderr) => {
// Prepare the output message
var outputMsg = {
payload: {
command: 'on',
response: stdout.trim(), // Trim any trailing whitespace
returnCode: error ? error.code : 0, // Return code or 0 if no error
error: error ? error.message : null, // Error message if any
stderr: stderr // Standard error output
}
};
node.status({ fill: "green", shape: "dot", text: "Relay On" });
// Create messages for each state
var messageNO1 = { Value: true };
var messageNC1 = { Value: false };
var messageNO2 = { Value: true };
var messageNC2 = { Value: false };
// Send messages to four outputs
node.send([ [{payload: messageNO1}], [{payload: messageNC1}], [{payload: messageNO2}], [{payload: messageNC2}], outputMsg ]);
});
}
break;
case 2:
{
// Execute the command to turn the relay off
exec("sudo /var/www/html/commands/cm4ProRelay.sh off", { shell: '/bin/bash' }, (error, stdout, stderr) => {
// Prepare the output message
var outputMsg = {
payload: {
command: 'off',
response: stdout.trim(), // Trim any trailing whitespace
returnCode: error ? error.code : 0, // Return code or 0 if no error
error: error ? error.message : null, // Error message if any
stderr: stderr // Standard error output
}
};
node.status({ fill: "grey", shape: "dot", text: "Relay Off" });
// Create messages for each state
var messageNO1 = { Value: false };
var messageNC1 = { Value: true };
var messageNO2 = { Value: false };
var messageNC2 = { Value: true };
// Send messages to four outputs
node.send([ [{payload: messageNO1}], [{payload: messageNC1}], [{payload: messageNO2}], [{payload: messageNC2}], outputMsg ]);
});
}
break;
}
}
}
});
}
RED.nodes.registerType("cm4-pro-relay", Cm4ProRelay);
};