Could someone have a look at this code?
This is what I got as the error:
{"event":{"clientX":1017,"clientY":197,"bbox":[969,224,1123,176]},"socketid":"26L447WP_qWBf2bLAACh","_msgid":"6d14dec2dbcfa5d1","error":{"message":"Error: Invalid context key","source":{"id":"796ab18f4ae96bdf","type":"function","name":"My blocker node. V3","count":1}},"_error":"Error: Invalid context key","settings":{"input":"2022-02-25T19:33:41.269Z","input_format":"","input_tz":"Australia/Sydney","output_format":"YYYY-MM-DD HH:mm:ss","output_locale":"en_AU","output_tz":"Australia/Sydney"},"time":"2022-02-26 06:33:41","payload":{"message":"Error: Invalid context key","source":{"id":"796ab18f4ae96bdf","type":"function","name":"My blocker node. V3","count":1}},"topic":"ERROR_REPORT/BedPi/Machine_Health"}
invalid context key
Sorry, I don't understand.
// The shape property can be: ring or dot.
// The fill property can be: red, green, yellow, blue or grey
// These are the three commands accepted.
const all = "ALL"; // Allow all message to pass
const block = "BLOCK"; // Block repeat message (of same topic)
const one = "ONE"; // Allow ONE message to pass. (may not be perfect)
const stop = "STOP"; // Block ALL message... ALL!
// Get the topic used to determine which messages are to control the mode.
let CTLTopic = env.get("TOPIC");
if (CTLTopic.length < 1)
throw"You must define a TOPIC for controlling the node";
if (msg.topic == CTLTopic)
{
//
// Set how node works.
//
if (msg.payload == stop)
{
node.status({ text: "Set to stop" });
context.set("MODE",msg.payload);
} else if (msg.payload == block)
{
node.status({ text: "Set to block" });
context.set("MODE",msg.payload);
} else if (msg.payload == all)
{
node.status({ text: "Set to allow" });
context.set("MODE",msg.payload);
} else if (msg.payload == one)
{
context.set("previousMode",context.get("MODE"));
node.warn(context.get("previousMode"));
node.status({ text: "Set to once" });
context.set("MODE",msg.payload);
} else // Show valid commands.
{
node.warn("For control of the node's mode msg.payload must be `" + all + "` `" + block + "` or `" + stop + "` or `" + one +"`");
};
return;
}
// -- Main code starts here.
// Get topic
// 26 Feb 3 lines commented out
//var topic = msg.topic;
//if (topic == "")
// topic = "BLANK";
// Get last stored value from topic
var lastval = context.get(topic) || null;
// Get new value from message
var newval = msg.payload;
// Get status from context.
var mode = context.get("MODE");
// =============== STOP
//else if (mode == "STOP")
if (mode == stop)
{
return;
}
// =============== ALL
//else if (mode == "ALL")
else if (mode == all)
{
node.status({fill:"blue",shape:"dot",text:"All"});
context.set(topic, newval);
return msg;
}
// =============== BLOCK
//if (mode == "BLOCK")
else if (mode == block)
{
//
// See if the new value is different to the old value.
//
if (newval != lastval)
{
context.set(topic, newval);
node.status({fill:"green",shape:"dot",text:"Passed"});
return msg;
}
node.status({fill:"red",shape:"dot",text:"Blocked"});
return;
}
// =============== ONE
//else if (mode == "ONE")
else if (mode == one)
{
//
// Allow only 1 message to pass
//
// mode = context.get("MODE");
context.set(topic, newval);
context.set("MODE",context.get("previousMode"));
// context.set("MODE","BLOCK");
node.status({ text: "Set to " + context.get("previousMode") });
return msg;
}