This will need a lot of reading and a lot of patients trying to understand what it is doing.
This is a subflow
I have which is hooked up to a lot of stuff.
There are nodes to configure the needed variables so they work.
If left alone, the flows work and I don't get any errors.
But if I modify the flow (no where near the subflow node) I get errors from the node.
I tried to document what is happening in the code/node and as you may see it is about the 14'th ittaration of the node.
I can post the stuff around it to (maybe) help with understanding.
Alas it still isn't working to my complete expectation. But for now it suffices for most things.
Just when I am editing the flow (else where) it (the node is used about 10 times) throws an error saying variables aren't set.
This is a late addition to kind of help me.
Alas the problem can't be made easier because it is reporting a variable (or more) isn't set.
So I can't get any more specific detail on which one.
But does that really matter?
Before the deploy
button is pressed it is working.
The deploy
is (mostly) modified nodes only.
Saves time on the redeploy - I believe.
// 2019 10 1 16:40
// Button_Press_scanner14b
//
// Added a line when configuring node with
// `SET_Device_ID`
//
// CHECK AROUND line 59 is commented out.
// Should NOT be setting 'online'
//
// Removed check if error is active if receiving ICSS == 5.
// Makes it easier to clear false alarms.
//
//
// Written now for SUBFLOW use.
// MUST have name injected to node with message.topic = "SET_Device_ID" and payload of devicename.
//
// Sends ERROR and ERROR_ACK messages to set icon if error happens.
// Holds colour of buttons to RED until ERROR_ACK button pressed.
//
// Node variables.
// Name of machine this button set applies.
// Tags needed:
// msg.device_ID (name of device)
// msg.state (state)
// msg.background (if machine state active)
var device = context.get('device')||0;
// new
// This locks the button colours if an alarm is received.
var block_change = context.get("BLOCK_CHANGE") ||0;
if (msg.topic == "SET_Device_ID")
{
node.warn("Setting node name");
node.warn(msg.payload);
context.set('device',msg.payload);
}
var msg2;
var msg3;
// Check if debug is enabled.
var debug = flow.get(device+'_Button_Debug') || false;
//node.warn("Debug set to " + debug);
// Background colour for button. Colour for OFF-LINE device status.
var backgroundcolour = context.get('background') || "brown";
// State This sets what is happening. See below.
var previous_state = context.get('state') || 0;
// State.
var ICSS = msg.state || 0;
//node.warn("Message state is " + ICSS);
// Used to signal machine shutting down.
var shutdown = context.get('shutdown') || 0;
// If machine is online or offline.
var online = context.get('online') || "Off-line"; // status of machine.
// Code for testing node.
//online = "On-line"; // Temporary line of code.
//node.warn("online state is " + online);
// I/C message variables.
var ICDID = msg.device_ID || 0;
//var ICDID = payload.DEVICE;
//node.warn("Device ID is " + ICDID);
// Set up stuff here.
// This may no longer be needed. Left in as precuation.
if (ICDID == "ALL")
{
ICDID = device;
if (debug === true)
{
node.warn("** Reset button colours **");
node.warn(msg.background);
}
previous_state = 4;
backgroundcolour = msg.background;
context.set("BLOCK_CHANGE",0);
block_change = 0;
}
if (ICDID != device)
{
node.warn("Node Configuration not set");
node.error("Node Configuration not set", msg)
// fa-eye-slash
return;
}
// Main body here.
// Check device name.
if (ICDID == device)
{
if (debug === true)
{
node.warn("*******************");
node.warn("** DEVICES MATCH **");
node.warn("Existing state is " + previous_state);
node.warn("Incoming state is " + ICSS);
node.warn("Line state is " + online);
node.warn("Shutdown is " + shutdown);
node.warn("Back ground colour is " + backgroundcolour);
node.warn("*******************");
}
// Check function name.
// Check things have changed.
if (previous_state !== ICSS)
{
// And they have.
// List of states:
// 0 - normal or command received and ok.
// 1 - command sent.
// 2 - command received.
// 3 - command error.
// 4 - device status changed.
// 5 - clear existing alarm. (Work needed)
// 6 - Shutdown command received.
//node.warn("Miss match detected");
// Check if ERROR is active.
if (ICSS == 5)
{
//
// if (previous_state == 3)
// {
// CLEAR ALARM button pressed.
context.set("BLOCK_CHANGE",0);
block_change = 0;
previous_state = 0;
//
//node.warn("Alarm clear signal received");
node.status({fill:"green",shape:"dot",text:"0"});
// This line was background:msg.background but failed.
// Changed to what it is now and all ok.
msg2 = {payload:"", background:"lime"};
msg3 = {payload:"ERROR_ACK",background:"lime"};
msg = null;
return [msg,msg2,msg3];
// }
}
if (block_change == 1)
{
//
// Don't allow any changes unless it is the "clear" signal.
//
return;
}
context.set('previous_state', ICSS); // save new state.
previous_state = ICSS; // NEW PART
// Check if state is 0.
if (ICSS === 0)
{
if(previous_state != 3)
{
// Button ready to be pressed.
node.warn("Button ready for pressing");
node.status({fill:"green",shape:"dot",text:"0"});
msg2 = { payload:"", background:msg.background};
msg3 = {payload:"ERROR_ACK",background:"lime"};
msg = null;
return [msg,msg2,msg3];
}
} else
if (ICSS == 1)
{
// Button is pressed. Command SENT.
// Need to check here if context 'online' is correct to allow button press.
node.warn("Button press phase 1");
if (online == "On-line")
{
node.status({fill:"grey",shape:"dot",text:"1"});
msg2 = { payload:"", background:"grey"};
if (debug === true)
{
node.warn("Button press detected");
node.warn("original message payload is " + msg);
}
}
return [msg,msg2];
} else
if (ICSS == 2)
{
// Command RECEIVED.
node.status({fill:"black",shape:"dot",text:"2"});
msg2 = { payload:"", background:"black"};
msg = null;
return [msg,msg2];
} else
if (ICSS == 3)
{
// Command ERROR.
node.status({fill:"red",shape:"dot",text:"3"});
msg2 = { payload:"", background:"red"};
msg = null;
context.set("BLOCK_CHANGE",1);
context.set('state',3);
//
// Third message to send to set icon
// md-error
msg3 = {payload:"ERROR"};
return [msg,msg2,msg3];
} else
if (ICSS == 4) ////////// MACHINE STATE CHANGED //////////////////
{
if(previous_state != 3)
{
if (debug === true)
{
// Update button colour.
node.warn("@@@@@@@@@@@@@@@@");
node.warn(device);
node.warn("Payload = " + msg.payload);
node.warn("Background colour -- " + msg.background);
node.warn("State changed");
}
context.set('background',msg.background);
// Indicate node changed.
node.status({fill:"yellow",shape:"dot",text:"4"});
msg2 = { payload:"", background:msg.background};
context.set('state', 0);
// Set online status.
if (msg.background == "lime")
{
//
if (shutdown == 0)
{
//node.warn("Online routine");
context.set('online',"On-line");
//backgroundcolour = "lime";
backgroundcolour = msg.background;
}
} else
//if (msg.background == "brown")
{
//
//node.warn("Offline routine");
context.set('online',"Off-line");
context.set('shutdown',0);
//backgroundcolour = "brown";
backgroundcolour = msg.background;
}
}
msg = null;
return [msg,msg2];
} else
if (ICSS == 6)
{
//
context.set('shutdown',1);
node.warn("********* Shutdown set to 1 *************");
context.set('online',"Off-line");
msg2 = { payload:"", background:"brown"};
// backgroundcolour = msg.background; // Dunno what this line is supposed to do.
msg = null; // 2018-10-06 Added these two lines to fix problem of buttons not changing from BLACK.
return [msg,msg2];
}
}
}