node-red-contrib-yamaha-gsp
Version:
Node-RED nodes for controlling Yamaha AV receivers via API
140 lines (121 loc) • 4.54 kB
JavaScript
const axios = require('axios');
module.exports = function(RED) {
// Power control node
function YamahaPowerNode(config) {
RED.nodes.createNode(this, config);
const node = this;
node.ip = config.ip;
node.port = config.port;
node.name = config.name;
node.on('input', async function(msg) {
const baseUrl = `http://${node.ip}:${node.port}/YamahaExtendedControl/v1/`;
const power = (msg.payload === 'on') ? 'on' : 'standby';
try {
const response = await axios.get(`${baseUrl}main/setPower?power=${power}`);
msg.payload = response.data;
node.send(msg);
} catch (error) {
node.error('Error controlling Yamaha power: ' + error.message, msg);
}
});
}
// Volume control node
function YamahaVolumeNode(config) {
RED.nodes.createNode(this, config);
const node = this;
node.ip = config.ip;
node.port = config.port;
node.name = config.name;
node.on('input', async function(msg) {
const baseUrl = `http://${node.ip}:${node.port}/YamahaExtendedControl/v1/`;
const volume = Math.max(-80, Math.min(16.5, parseFloat(msg.payload)));
try {
const response = await axios.get(`${baseUrl}main/setVolume?volume=${volume}`);
msg.payload = response.data;
node.send(msg);
} catch (error) {
node.error('Error controlling Yamaha volume: ' + error.message, msg);
}
});
}
// Input selection node
function YamahaInputNode(config) {
RED.nodes.createNode(this, config);
const node = this;
node.ip = config.ip;
node.port = config.port;
node.name = config.name;
node.on('input', async function(msg) {
const baseUrl = `http://${node.ip}:${node.port}/YamahaExtendedControl/v1/`;
try {
const response = await axios.get(`${baseUrl}main/setInput?input=${msg.payload}`);
msg.payload = response.data;
node.send(msg);
} catch (error) {
node.error('Error changing Yamaha input: ' + error.message, msg);
}
});
}
// Mute control node
function YamahaMuteNode(config) {
RED.nodes.createNode(this, config);
const node = this;
node.ip = config.ip;
node.port = config.port;
node.name = config.name;
node.on('input', async function(msg) {
const baseUrl = `http://${node.ip}:${node.port}/YamahaExtendedControl/v1/`;
const mute = (msg.payload === true || msg.payload === 'true') ? 'true' : 'false';
try {
const response = await axios.get(`${baseUrl}main/setMute?enable=${mute}`);
msg.payload = response.data;
node.send(msg);
} catch (error) {
node.error('Error controlling Yamaha mute: ' + error.message, msg);
}
});
}
// Sound program node
function YamahaProgramNode(config) {
RED.nodes.createNode(this, config);
const node = this;
node.ip = config.ip;
node.port = config.port;
node.name = config.name;
node.on('input', async function(msg) {
const baseUrl = `http://${node.ip}:${node.port}/YamahaExtendedControl/v1/`;
try {
const response = await axios.get(`${baseUrl}main/setSoundProgram?program=${msg.payload}`);
msg.payload = response.data;
node.send(msg);
} catch (error) {
node.error('Error changing Yamaha sound program: ' + error.message, msg);
}
});
}
// Status node
function YamahaStatusNode(config) {
RED.nodes.createNode(this, config);
const node = this;
node.ip = config.ip;
node.port = config.port;
node.name = config.name;
node.on('input', async function(msg) {
const baseUrl = `http://${node.ip}:${node.port}/YamahaExtendedControl/v1/`;
try {
const response = await axios.get(`${baseUrl}main/getStatus`);
msg.payload = response.data;
node.send(msg);
} catch (error) {
node.error('Error getting Yamaha status: ' + error.message, msg);
}
});
}
// Register all nodes
RED.nodes.registerType("yamaha-power", YamahaPowerNode);
RED.nodes.registerType("yamaha-volume", YamahaVolumeNode);
RED.nodes.registerType("yamaha-input", YamahaInputNode);
RED.nodes.registerType("yamaha-mute", YamahaMuteNode);
RED.nodes.registerType("yamaha-program", YamahaProgramNode);
RED.nodes.registerType("yamaha-status", YamahaStatusNode);
};