@parallaxcontrol/node-red-control
Version:
A comprehensive package of Node-RED nodes designed to seamlessly integrate with the Parallax Control Engine, offering enhanced control capabilities for AV Automation.
94 lines (81 loc) • 3.86 kB
JavaScript
module.exports = function(RED) {
function SaveSignalStateNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.on('input', function(msg) {
var dataType = msg.payload.Type;
var joinID = msg.payload.JoinID;
var inputValue = msg.payload.Value;
const DATA_TYPE_DIGITAL = "DIGITAL";
const DATA_TYPE_ANALOG = "ANALOG";
const DATA_TYPE_SERIAL = "SERIAL";
const DATA_TYPE_DIGITAL_DICTIONARY = "DIGITAL_DICTIONARY";
const DATA_TYPE_ANALOG_DICTIONARY = "ANALOG_DICTIONARY";
const DATA_TYPE_SERIAL_DICTIONARY = "SERIAL_DICTIONARY";
function saveData(dataType) {
/*
node.context().global.set("key", "value");
var val = node.context().global.get("key");
node.warn("val is: "+val);
*/
try {
// Retrieve the existing dictionary or create a new one if it doesn't exist
var _dictionary = node.context().global.get(dataType) || {};
if (joinID != "_msgid") {
if (_dictionary) {
_dictionary[joinID] = inputValue;
node.context().global.set(dataType, _dictionary);
var message = { Value : true };
node.send({ payload: message });
node.status({ fill: "blue", shape: "dot", text: "Saving " + dataType + " Data" });
} else {
// If the dictionary doesn't exist, create a new one
_dictionary = {};
_dictionary[joinID] = inputValue;
node.context().global.set(dataType, _dictionary);
var message = { Value : true };
node.send({ payload: message });
node.status({ fill: "blue", shape: "dot", text: "Saving " + dataType + " Data" });
}
} else {
// Handle the case where joinID is "_msgid"
node.send({ payload: false });
}
} catch (error) {
// Handle and log the error
node.error("Error in saveData: " + error.message);
var message = { Value : false };
node.send({ payload: message });
node.status({ fill: "red", shape: "ring", text: "Saving " + dataType + " Data" + " failed" });
}
}
// Save values based on data type
switch (dataType) {
case DATA_TYPE_DIGITAL:
saveData(dataType);
break;
case DATA_TYPE_ANALOG:
saveData(dataType);
break;
case DATA_TYPE_SERIAL:
saveData(dataType);
break;
case DATA_TYPE_DIGITAL_DICTIONARY:
saveData(dataType);
break;
case DATA_TYPE_ANALOG_DICTIONARY:
saveData(dataType);
break;
case DATA_TYPE_SERIAL_DICTIONARY:
saveData(dataType);
break;
default:
// Handle unsupported data types here
node.warn("Unsupported data type: " + dataType);
break;
}
return msg;
});
}
RED.nodes.registerType("save-signal-state", SaveSignalStateNode);
}