node-red-contrib-musiccast
Version:
A Node-RED collection for monitoring and controlling a Yamaha Musiccast network.
91 lines (72 loc) • 3.42 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 MusiccastNodeInput(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.input = node.mc.extIncoming('input', config, msg);
newCall.topic = newCall.input;
newCall.device = node.mc.extIncoming('device', config, msg);
newCall.command = node.mc.extIncoming('command', config, msg);
newCall.attributes = node.mc.extIncoming('attributes', config, msg);
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);
});
} 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-input", MusiccastNodeInput);
};