I am sorry folks, but this is/has been going on for a while and now and as I am looking at it, I am confused.
Few things happening:
I have 3 outputs and I can not for the life of me see how/where the third one is made.
I am wanting to add extra stuff to the messages so I can Maybe track down the bigger problem.
(And I've just worked that out.)
So on to the BIGGER question.
Quick backstory.
I have 3 machines.
Each test the main router and the public IP address every 20 seconds.
They construct a message and send that to one another and in the second bit the messages are combined.
The values are tallied and if I get a case where all are set to 0
then I am told the uplink is off line.
A few months back I went through and did a lot of tidying up OLD code and as good as it was: it was also bad. (Sod's law)
2 of the machine's nodes were not parsing the messages and so it was 1 in 3 of the machines that was actually giving me a true message.
I fixed the first one - and stupidly didn't see the second one.
The rogue reports of uplink down, (etc) seemed to stop.
They're ba-ack!
But about daily at the same-ish times.
What I'm wanting to do is set a new/extra part of the messages sent that tells me who sent the message.
Then I can (maybe) get closer to understanding / finding out what is going on.
The code - slightly different in each machine for weightings of messages.
// 2023 09 30 -- new code.
let msg1 = {};
let msg2 = {};
let machine1 = "TimePi";
let machine2 = "BedPi";
let machine3 = "TelePi";
let newval;
// set up to wipe all stuff.
if (msg.payload == "WIPE") {
context.set('m1', 0);
context.set('m2', 0);
context.set('m3', 0);
context.set('mstate', 0);
context.set('ustate', 0);
node.status({ fill: "green", text: "WIPED" });
// node.warn("WIPED");
return;
}
if (msg.payload == "DEBUG") {
context.set("debug", msg.onoff);
if (msg.onoff == 0) {
node.status({});
// return;
} else {
node.status({ fill: "blue", text: "DEBUG ON" });
// return;
}
return;
}
//Set Bit
function setBit(number, bitPosition) {
return number | (1 << bitPosition);
}
//Clear Bit
function clearBit(number, bitPosition) {
const mask = ~(1 << bitPosition);
return number & mask;
}
/////////////////////////////////
//node.warn("Look for this " + msg.payload.Who);
let machine = msg.payload.Who;
//let link = msg.link;
let mstate = msg.payload.Modem;
let ustate = msg.payload.UpLink;
let modem;
let uplink;
let value;
let m1 = context.get("m1") || 0;
let m2 = context.get("m2") || 0;
let m3 = context.get("m3") || 0;
let debug = context.get("debug") || 0;
node.status({}); // Wipe status
if (debug == 1) {
node.warn("************************");
node.warn("Machine is " + machine);
//node.warn("link is " + link);
node.warn("Modem state is " + mstate);
node.warn("Uplink state is " + ustate);
}
//node.warn("mstate " + mstate);
if (machine == machine1) {
// Machine 1
if (debug == 1) {
//
node.warn("Setting value for MACHINE 1");
}
// node.warn("Machine 1");
context.set("m1", 1);
if (mstate == "Offline") {
value = context.get("mstate") || 0;
newval = clearBit(value, 0);
context.set("mstate", newval);
} else
if (mstate == "Online") {
value = context.get("mstate") || 0;
newval = setBit(value, 0);
context.set("mstate", newval);
}
if (ustate == "Offline") {
value = context.get("ustate") || 0;
newval = clearBit(value, 0);
context.set("ustate", newval);
} else
if (ustate == "Online") {
value = context.get("ustate") || 0;
newval = setBit(value, 0);
context.set("ustate", newval);
}
}
if (machine == machine2) {
// Machine 2
if (debug == 1) {
//
node.warn("Setting value for MACHINE 2");
}
// node.warn("Machine 2");
context.set("m2", 1);
if (mstate == "Offline") {
value = context.get("mstate") || 0;
newval = clearBit(value, 1);
context.set("mstate", newval);
} else
if (mstate == "Online") {
value = context.get("mstate") || 0;
newval = setBit(value, 1);
context.set("mstate", newval);
}
if (ustate == "Offline") {
value = context.get("ustate") || 0;
newval = clearBit(value, 1);
context.set("ustate", newval);
} else
if (ustate == "Online") {
value = context.get("ustate") || 0;
newval = setBit(value, 1);
context.set("ustate", newval);
}
}
if (machine == machine3) {
// Machine 3
if (debug == 1) {
//
node.warn("Setting value for MACHINE 3");
}
// node.warn("Machine 3");
context.set("m3", 1);
if (mstate == "Offline") {
value = context.get("mstate") || 0;
newval = clearBit(value, 2);
context.set("mstate", newval);
} else
if (mstate == "Online") {
value = context.get("mstate") || 0;
newval = setBit(value, 2);
context.set("mstate", newval);
}
if (ustate == "Offline") {
value = context.get("ustate") || 0;
newval = clearBit(value, 2);
context.set("ustate", newval);
} else
if (ustate == "Online") {
value = context.get("ustate") || 0;
newval = setBit(value, 2);
context.set("ustate", newval);
}
}
m1 = context.get("m1") || 0;
m2 = context.get("m2") || 0;
m3 = context.get("m3") || 0;
modem = context.get("mstate") || 0;
uplink = context.get("ustate") || 0;
msg2.payload = "M " + modem + ", U " + uplink + ", m1 " + m1 + ", m2 " + m2 + ", m3 " + m3;
if (debug == 1) {
node.warn("--------------");
node.warn("Modem value " + modem);
node.warn("Uplink value " + uplink);
node.warn("Counters " + m1 + " " + m2 + " " + m3);
node.warn("--------------");
}
node.status({ text: "Uplink " + uplink + " Modem " + modem + " Counters " + m3 + " " + m2 + " " + m1});
if (m1 == 1 && m2 == 1 && m3 == 1) {
// This is used to wipe values once 3 messages are received.
// node.warn("Wiping stored values.");
context.set("m1", 0);
context.set("m2", 0);
context.set("m3", 0);
context.set("mstate", 0);
context.set("ustate", 0);
// modem = context.get("modem") || 0;
// uplink = context.get("uplink") || 0;
// Now send the message
//node.status({text:""});
//}
//if (m1 == 1 || m2 == 1 || m3 == 1) {
//---------
if (debug == 1) {
//
node.warn("Values being sent");
node.warn("modem is " + modem);
}
//---------
if (modem === 0) {
uplink = "UNKNOWN";
msg = { "payload": '<font color = "red"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "red" };
//msg.colour = "red";
} else
// if (modem > 3) {
if (modem == 7) {
//node.status({fill: "green",text:machine3});
msg = { "payload": '<font color = "lime"> <i class="fa fa-bullseye fa-2x"></i></font>', "colour": "lime" };
//msg.msg.colour = "lime";
} else
// if (modem > 2) {
if (modem == 3 || modem == 5 || modem == 6) {
//node.status({fill: "yellow",text:machine2});
msg = { "payload": '<font color = "green"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "green" };
//msg.colour = "green";
} else
// if (modem == 1) {
if (modem == 1 || modem == 2 || modem == 4) {
//node.status({fill: "red",text:machine1});
msg = { "payload": '<font color = "springgreen"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "springgreen" };
//msg.colour = "springgreen";
}
if (uplink === 0) {
msg1 = { "payload": '<font color = "red"> <i class="fa fa fa-bullseye fa-2x" ></i></font>', "colour": "red" };
//msg1.colour = "red";
} else
// if (uplink > 3) {
if (uplink == 7) {
msg1 = { "payload": '<font color = "lime"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "lime" };
//msg1.colour = "lime";
} else
// if (uplink > 2) {
if (uplink == 3 || modem == 5 || modem == 6) {
msg1 = { "payload": '<font color = "green"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "green" };
//msg1.colour = "green";
} else
// if (uplink == 1)
if (uplink == 1 || modem == 2 || modem == 4) {
msg1 = { "payload": '<font color = "springgreen"> <i class="fa fa-bullseye fa-2x" ></i></font>', "colour": "springgreen" };
//msg1.colour = "springgreen";
} else
if (uplink == "UNKNOWN") {
msg1 = { "payload": '<font color = "black"> <i class="fa fa-question-circle fa-2x" ></i></font>', "colour": "black" };
//msg.colour = "black";
}
// msg2.payload = {"Modem":modem,"Uplink":uplink};
if (debug == 1) {
//
node.warn("Payload set to " + msg.payload);
}
return [msg, msg1, msg2];
}
if (debug == 1) {
//
node.warn("NO MESSAGE");
}
return [null, null, msg2];
The important part at the bottom.
return [msg, msg1, msg2]
msg and msg1 are nearly identical, but are for different devices. Modem (to call it) and the uplink.
I'm wanting to add - with as little extra typing as possible - a msg.sender
to the messages.
But simply doing this:
msg.sender = "devicename"
msg1.sender = "devicename"
just before the return [msg,msg1,msg2]
line.
Gives me errors.
I'll try again - again banking on Sod's law to make a lire of me - but I'm confused why it gives me an error.
Thanks in advance.
Slight update:
It only throws the error when I do the msg1.sender = "BedPi";
But seems to accept msg.sender = "BedPi";
Screen shots.
And yes, I've cheated and commented out the line for the sake of getting the screenshots.
All good.
Again: Sorry. I've got the flu and my brain is not working the best.