@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.
55 lines (47 loc) • 2.09 kB
JavaScript
const { exec } = require('child_process');
module.exports = function(RED) {
function Cm4ProSerial(config) {
RED.nodes.createNode(this, config);
var node = this;
// Initialize configuration properties
this.Mode = config.mode;
this.Baudrate = config.baudrate;
this.Databits = config.databits;
this.Parity = config.parity;
this.Stopbits = config.stopbits;
this.Hardware = config.hardware;
/*
# Input arguments
MODE="$1" # RS485-1, RS485-2, or RS232-1
BAUDRATE="$2" # e.g., 9600
DATABITS="$3" # e.g., 8
PARITY="$4" # e.g., N (None), E (Even), O (Odd)
STOPBITS="$5" # e.g., 1 or 2
DATA="$6" # The data to send
*/
node.on('input', function(msg) {
if (typeof msg.payload.Value !== 'string') {
// Handle non-string payload
node.warn(`Invalid input type: ${typeof msg.payload.Value}`);
node.status({ fill: "red", shape: "ring", text: "Error: Payload must be a string." });
return; // Stop further processing
}
// Construct the shell command using JSON.stringify only for msg.payload.Value
let command = `. /var/www/html/commands/cm4ProSerial.sh ${node.Mode} ${node.Baudrate} ${node.Databits} ${node.Parity} ${node.Stopbits} ${JSON.stringify(msg.payload.Value)}`;
node.warn(`Command: ${command}`); // Log the constructed command
// Execute the command
exec(command, { shell: '/bin/bash' }, (error, stdout, stderr) => {
if (error) {
node.error(`Error: ${stderr}`, msg);
msg.payload = `Error: ${stderr}`;
node.send(msg);
return;
}
// Send the command output as payload
msg.payload = stdout;
node.send(msg);
});
});
}
RED.nodes.registerType("cm4-pro-serial", Cm4ProSerial);
};