node-red-contrib-greenhippo
Version:
A collection of node for API calls from Green Hippo Media Servers
136 lines (105 loc) • 3.75 kB
JavaScript
//=============================\\
// infocusav \\
//=============================\\
const http = require('http');
module.exports = function(RED) {
function GreenHippoInfoNode(config) {
RED.nodes.createNode(this, config);
const node = this;
const ip = config.ipAddress;
const port = config.port;
node.on('input', function(msg, send, done) {
const requestIp = ip || msg.ipAddress;
const requestPort = port || msg.port || 40512;
if (!requestIp) {
node.error("IP Address is required", msg);
done();
return;
}
//
// -------- First request: /info --------
//
const optionsInfo = {
hostname: requestIp,
port: parseInt(requestPort, 10),
path: '/info',
method: 'GET',
timeout: 5000,
};
node.log(`Requesting http://${requestIp}:${requestPort}/info`);
const reqInfo = http.request(optionsInfo, (resInfo) => {
let data = '';
resInfo.on('data', (chunk) => {
data += chunk;
});
resInfo.on('end', () => {
try {
const json = JSON.parse(data);
// Output 1: full JSON
send([{ ...msg, payload: json }, null, null]);
// Output 2: engineStatus as boolean
send([null, { payload: json.engineStatus === "Running" }, null]);
} catch (err) {
node.error("Failed to parse JSON response: " + err.message, msg);
}
});
});
reqInfo.on('error', (err) => {
node.error("HTTP request (/info) failed: " + err.message, msg);
// Output 2 = false on connection error
send([null, { payload: false }, null]);
});
reqInfo.on('timeout', () => {
reqInfo.destroy();
node.error("HTTP request (/info) timed out", msg);
});
reqInfo.end();
//
// -------- Second request: /pin/getvalue/... --------
//
const optionsStatus = {
hostname: requestIp,
port: parseInt(requestPort, 10),
path: '/pin/getvalue/status_statuscomponent_system%20status',
method: 'GET',
timeout: 5000,
};
node.log(`Requesting http://${requestIp}:${requestPort}${optionsStatus.path}`);
const reqStatus = http.request(optionsStatus, (resStatus) => {
let statusData = '';
resStatus.on('data', (chunk) => {
statusData += chunk;
});
resStatus.on('end', () => {
try {
const text = statusData.toString().trim();
let status = null;
if (text.includes("MediaManager : Busy")) {
status = "MediaManager:Uploading"; // always send this when busy
} else if (text.includes("System Status=Run")) {
status = "MediaManager:Ready";
} else {
status = text; // fallback
}
// Always send output 3
send([null, null, { payload: status }]);
} catch {
send([null, null, { payload: statusData }]);
}
});
});
reqStatus.on('error', (err) => {
node.error("HTTP request (/status) failed: " + err.message, msg);
send([null, null, { payload: null }]);
});
reqStatus.on('timeout', () => {
reqStatus.destroy();
node.error("HTTP request (/status) timed out", msg);
send([null, null, { payload: null }]);
});
reqStatus.end();
done();
});
}
RED.nodes.registerType("GH-Info", GreenHippoInfoNode);
};