@viun/node-red-contrib-overview
Version:
Collection of nodes to control the Overview's smart AI cameras
69 lines (58 loc) • 2.46 kB
JavaScript
module.exports = function(RED) {
function ChangeRecipeNode(config) {
RED.nodes.createNode(this, config);
const node = this;
node.hostname = config.hostname || 'localhost';
node.port = config.port || '5001';
node.recipeId = config.recipeId;
node.on('input', function(msg, send, done) {
if (!node.recipeId) {
node.error('Recipe ID is required');
done();
return;
}
const http = require('http');
const url = `http://${node.hostname}:${node.port}/recipe/activate`;
const postData = JSON.stringify({
id: node.recipeId
});
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
};
const req = http.request(url, options, (response) => {
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
try {
const responseData = JSON.parse(data);
msg.payload = responseData;
if (responseData.success) {
node.status({ fill: 'green', shape: 'dot', text: 'Recipe changed successfully' });
} else {
node.status({ fill: 'red', shape: 'ring', text: responseData.details || 'Recipe change failed' });
}
send(msg);
done();
} catch (error) {
node.status({ fill: 'red', shape: 'ring', text: error.message });
node.error(error.message, msg);
done(error);
}
});
}).on('error', (error) => {
node.status({ fill: 'red', shape: 'ring', text: 'Error: ' + error.message });
node.error('Failed to change recipe: ' + error.message, msg);
done(error);
});
req.write(postData);
req.end();
});
}
RED.nodes.registerType('change recipe', ChangeRecipeNode);
};