node-red-contrib-musiccast
Version:
A Node-RED collection for monitoring and controlling a Yamaha Musiccast network.
204 lines (156 loc) • 7.56 kB
JavaScript
/*
Apache-2.0
Copyright (c) 2023 Vahdettin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
const axios = require('axios');
const xml2js = require('xml2js');
function be(RED) {
const TIMEOUT = 2000;
let debug = true;
let debugResponses = false;
const beZones = require('../lib/constants').zones,
beInputs = require('../lib/constants').inputs;
function d(text) {
if (debug === true) {
try {
console.log(text);
} catch (err) {
console.warn(err)
}
}
}
function dResponse(obj) {
if (debugResponses === true) {
try {
console.log("Resp: " + JSON.stringify(obj));
} catch (err) {
console.warn(err);
}
}
}
function handle(err, status = 500, res, message = "Unknown") {
if (err && status && res) {
console.warn(message);
res.status(status);
res.end(JSON.stringify({error: message}));
} else {
res.end(JSON.stringify({error: "unknown_error"}));
}
}
/*
UI: Get a device descriptor and location info given an address.
Used for discovery in configuration device editor
*/
RED.httpAdmin.get('/musiccast/discover', async function (req, res, next) {
let address = req.query.address || "invalid";
d("GET /musiccast/discover address: " + address);
const discover = async () => {
try {
d("GET 'http://" + address + ":49154/MediaRenderer/desc.xml");
const getDesc = await axios.get('http://' + address + ':49154/MediaRenderer/desc.xml',
{
timeout: TIMEOUT,
responseType: 'xml',
transformResponse: function (data) {
dResponse(data);
return data;
}
});
let device_description = getDesc.data;
/*
Get available features of a device. Used to filter options in UI.
*/
d('GET http://' + address + ':80/YamahaExtendedControl/v1/system/getFeatures');
const getFeatures = await axios.get('http://' + address + ':80/YamahaExtendedControl/v1/system/getFeatures',
{
timeout: TIMEOUT,
transformResponse: function (data) {
let newData = {
input_list: beInputs,
zone_list: beZones
};
try {
if (data && JSON.parse(data) && JSON.parse(data).system) {
if (JSON.parse(data).system.input_list) {
newData.input_list = {};
JSON.parse(data).system.input_list.forEach(function (input, index, array) {
newData.input_list[input.id] = {
id: input.id
}
})
}
if (JSON.parse(data).zone && Array.isArray(JSON.parse(data).zone)) {
newData.zone_list = {};
JSON.parse(data).zone.forEach(function (zone, index, array) {
newData.zone_list[zone.id] = {
id: zone.id,
input_list: zone.input_list || [],
link_control_list: zone.link_control_list || [],
link_audio_delay_list: zone.link_audio_delay_list || [],
distribution: JSON.parse(data).distribution || {},
sound_program_list: zone.sound_program_list || []
}
})
}
return newData;
} else {
console.warn("Invalid device.")
handle("invalid_device", 500, res);
}
} catch (err) {
console.warn(err);
handle("invalid_device", 500, res);
}
}
}).catch(function (err) {
console.warn('Failed to decode YamahaYXC Device Description. Error: ' + err);
handle("invalid_device", 500, res);
});
let feature_data = getFeatures.data;
const parseDescData = await xml2js.parseStringPromise(device_description).then(function (result) {
if(address === "192.168.10.92"){
console.log(result.root.device[0])
}
return {
modelName: result.root.device[0].modelName[0] || null,
modelNumber: result.root.device[0].modelNumber[0] || null,
serialNumber: result.root.device[0].serialNumber[0] || null,
modelDescription: result.root.device[0].modelDescription[0] || null,
manufacturer: result.root.device[0].manufacturer[0] || null,
friendlyName: result.root.device[0].friendlyName[0] || null,
presentationURL: result.root.device[0].presentationURL[0] || null,
udn: result.root.device[0].UDN[0] || null
};
})
.catch(function (err) {
console.warn('Failed to decode YamahaYXC Device Description. Error: ' + err);
});
dResponse(JSON.stringify({
device_description: parseDescData || {},
feature_data: feature_data || {}
}));
res.end(JSON.stringify({
device_description: parseDescData || {},
feature_data: feature_data || {}
}));
} catch (err) {
handle(err, 500, res);
}
};
if (address && address !== "invalid") {
await discover();
} else {
handle("invalid_parameters", 500, res)
}
})
console.log("node-red-contrib-musiccast backend started.");
}
module.exports.be = be;