Ok, made some changes - this is what I have now. It is taking the IR commands and correctly putting them in a global context called matchedCommands; however, it is not sending them out through the global cache out node, which is configured like my others that are working...Any advice?
Split(array)
// Check if source2 is a number
if (typeof source2 === 'number') {
// Convert source2 to a string to check its length
var source2String = source2.toString();
// Check if source2String has 1, 2, or 3 digits
if (source2String.length <= 3) {
// Proceed with processing
// Convert source2String to an array of digits
var numberArray = source2String.split('').map(Number);
// Validate the number array
if (Array.isArray(numberArray)) {
// Initialize an array to store matched IR commands
var matchedCommands = [];
// Map each digit to its corresponding IR command
numberArray.forEach(function(digit) {
// Ensure the digit is within the range 0-9
if (digit >= 0 && digit <= 9) {
var command = irCommands[digit];
// Push the command to the array of matched commands
matchedCommands.push(command);
} else {
node.warn("Invalid digit: " + digit);
}
});
// Store the array of matched commands in the global context
global.set("matchedCommands", matchedCommands);
} else {
node.error("Invalid number array: " + numberArray);
}
} else {
// Handle the case when source2 has more than 3 digits
node.error("default channel should be up to a three-digit number.");
}
} else {
// Handle the case when source2 is not a number
node.error("Default channel must be a 1, 2 or 3 digit number.");
}
Assign IR Cmds
// Retrieve the number array from the message payload
var numberArray = msg.payload;
// Log the received number array to debug
node.warn("Received number array: " + JSON.stringify(numberArray));
// Define IR commands for 0-9
var irCommands = [
// ir command 0
"1,58000,1,37,23,351,23,164,23,94,23,164,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,94,23,164,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351",
// ir command 1
"1,58000,1,37,23,351,23,164,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351",
//ir command 2
"1,58000,1,37,23,351,23,164,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351",
//ir command 3
"1,58000,1,37,23,351,23,164,23,164,23,164,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,164,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351",
//ir command 4
"1,58000,1,37,23,351,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351",
//ir command 5
"1,58000,1,37,23,351,23,164,23,164,23,94,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351",
//ir command 6
"1,58000,1,37,23,351,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351",
//ir command 7
"1,58000,1,37,23,351,23,164,23,164,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351",
//ir command 8
"1,58000,1,37,23,351,23,164,23,164,23,94,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,94,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351",
//ir command 9-4
"1,58000,1,37,23,351,23,164,23,164,23,94,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,94,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351"
];
// Validate the number array
if (Array.isArray(numberArray)) {
// Initialize an array to store matched IR commands
var matchedCommands = [];
// Map each digit to its corresponding IR command
numberArray.forEach(function(digit) {
// Ensure the digit is within the range 0-9
if (digit >= 0 && digit <= 9) {
var command = irCommands[digit];
// Push the command to the array of matched commands
matchedCommands.push(command);
} else {
node.warn("Invalid digit: " + digit);
}
});
// Store the array of matched commands in the global context
global.set("matchedCommands", matchedCommands);
} else {
node.error("Invalid number array: " + numberArray);
}
// Return the message
return msg;
Send to GC
// Retrieve matchedCommands array from global context
var matchedCommands = global.get('matchedCommands');
// Check if matchedCommands is defined and is an array
if (Array.isArray(matchedCommands)) {
// Iterate over each IR command in the matchedCommands array
matchedCommands.forEach(function(irCommand) {
// Construct a message object with the IR command as the payload
var msgToSend = {
payload: irCommand
};
// Send the message to the Global Cache - IR Command node
node.send(msgToSend);
});
} else {
// Handle the case when matchedCommands is empty or not an array
node.error("No matched IR commands found in the global context.");
}
Iterate ea IR Cmd.
// Iterate over each IR command in the matchedCommands array
for (var i = 0; i < matchedCommands.length; i++) {
// Construct a message object with the IR command as the payload
var msgToSend = {
payload: matchedCommands[i]
};
// Send the message to the Global Cache - IR Command node
node.send(msgToSend);
}
// Return the message
return msg;
Here is my Global Context data for the matchedCommands array, which appears to be right...
["1,58000,1,37,23,351,23,164,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351","1,58000,1,37,23,351,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351","1,58000,1,37,23,351,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351,23,164,23,164,23,94,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,164,23,351"]