@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.
74 lines (64 loc) • 3.02 kB
JavaScript
const fs = require('fs');
const { exec } = require('child_process');
const net = require('net');
//connect to device
module.exports = function (RED) {
function BiampTesiraConnect(config) {
RED.nodes.createNode(this, config);
var node = this;
this.ipAddress = config.ip;
this.username = config.user;
this.password = config.pass;
// Path to the named pipe for receiving responses
const pipePath = '/var/www/html/commands/ssh_command_output_pipe';
node.on('input', function(msg) {
//OPEN PIPE FOR READING
fs.open(pipePath, fs.constants.O_RDONLY, (err, fd) => {
if(err){
node.error(`Pipe open error: ${err.message}`, msg);
node.status({ fill: "red", shape: "dot", text: "Pipe open error" });
return;
}
const pipe = new net.Socket({ fd });
// Now `pipe` is a stream that can be used for reading from the FIFO.
pipe.on('data', (data) => {
const dataString = data.toString('utf-8');
msg.payload = `${dataString}`;
node.send(msg);// send to next node
if(dataString.includes('Command timeout or no response from device.')){
node.status({ fill: "red", shape: "dot", text:"Device not responding"});
}
else if(dataString.includes('Welcome to the Tesira Text Protocol Server...')){
node.status({ fill: "green", shape: "dot", text:"Connected"});
}
});
pipe.on('error', (error) => {
msg.payload = `${error}`;
node.send(msg);
});
pipe.on('end', (end) => {
msg.payload = `${end}`;
node.send(msg);
});
});
//CONNECT TO SSH DEVICE
// Construct the command string
const command = `/usr/bin/expect /var/www/html/commands/ssh_persistent_control.exp "${node.username}" "${node.password}" "${node.ipAddress}"`;
// Run the expect script
exec(command, (error, stdout, stderr) => {
if (error) {
msg.payload = `biamp-tesira-connect.js, exec command error: ${error}, ${stdout}, ${stderr}`;
node.send(msg);// send to next node
node.warn(`${stdout}`);
node.warn(`Error: ${stderr}`);
node.error(`Error: ${stderr}`, msg);
msg.payload = `Error: ${stderr}`;
// Update the status to reflect the error
node.status({ fill: "red", shape: "dot", text: "Error" });
return;
}
});
});
}
RED.nodes.registerType("biamp-tesira-connect", BiampTesiraConnect);
};