node-red-contrib-musiccast
Version:
A Node-RED collection for monitoring and controlling a Yamaha Musiccast network.
103 lines (82 loc) • 3.99 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.
*/
module.exports = function (RED) {
function MusiccastNodeDevice(config) {
RED.nodes.createNode(this, config);
this.mc = RED.nodes.getNode(config.mc);
let node = this;
if (node.mc) {
node.status({});
node.on('input', function (msg, send, done) {
node.status({});
try{
let newCall = msg;
newCall.device = node.mc.extIncoming('device', config, msg);
newCall.command = node.mc.extIncoming('command', config, msg);
newCall.attributes = node.mc.extIncoming('attributes', config, msg);
newCall.topic = node.mc.getSystemBase(newCall.command,"system");
if (newCall.command !== 'getInternalConfig') {
node.mc.sendCommand(newCall).then(function (response) {
if (response) {
let newMsg = newCall;
newMsg.payload = response.data;
delete newMsg.attributes;
delete newMsg.topic;
if (config.f_show_status && node.mc.genStatusLabel(newMsg.command, response)) {
node.mc.showPalStatus(node,{
fill: "green",
shape: "dot",
text: node.mc.genStatusLabel(newMsg.command, response)
});
}
send(newMsg);
done();
} else {
if (config.f_show_status) {
node.mc.showPalStatus(node,{
fill: "yellow",
shape: "dot",
text: "invalid response"
});
}
node.error("No valid response received.", newCall);
done();
}
}).catch(function (error) {
node.status({fill: "red", shape: "circle", text: "unexpected error"});
node.error(error, newCall);
done(error);
});
} else {
/*
Special function to send the internal device config as a msg.
*/
send({
device: newCall.device,
command: newCall.command,
payload: node.mc.findDevice(newCall.device)
});
}
} catch (err){
node.error(err);
done(err);
}
});
} else {
node.status({fill: "red", shape: "dot", text: "configuration error"});
node.error(RED._("musiccast.errors.missing-config"));
}
}
RED.nodes.registerType("musiccast-device", MusiccastNodeDevice);
};