I don't want the answer given to me. Though I accept I am doing it the wrong way (maybe long would be a better word) I want to go through the motions and hope this helps me understand better what it is I am wanting to do.
3 machines sending in reports of what they see - all looking at the same thing.
Machine 1, machine 2 and machine 3
They are looking at: uplink and modem.
(I know modem is an old word and not really correct, but it shall suffice for what I am doing)
So each send a message out into the network for the other two to receive and process, as well as their own version on the state of the nation.
Machine 1: uplink: good modem: good
Machine 2: uplink: good modem: good
Machine 3: uplink: good modem: good
(and any combination there of good/bad)
This is kind of getting into IP routing kind of stuff, but I am still only learning about that and so I won't say this again.
Machine 1 can see the modem (and up link). All is fine. It also sees that both 2 and 3 see the modem and uplink.
Because (say) 3 sees it, the colour
is lime. (irrespective of what it and 2 say.)
Say 3 doesn't see then, but 2 does: the colour would be green
(irrespective of what it sees.)
If neither 2 or 3 see the modem (I'll forget about the uplink, as the logic there is the same anyway) but it does: the colour would be light blue
.
That is an example.
Now, machine 2:
Similar.
But say 1 is higher weight this time and 3 is the next.
Then machine 3:
It's external weight is 2 then 1 - then itself.
The kink:
If none can see the modem
the uplink
become **unknown**
for obvious reasons.
To make it .... a function I want to do this in a function
node and edit the weight of each machine's message to derive the outgoing message's colour.
As each machine sends its message and all 3 machine's messages are stored in context - I'm guessing - then I can't just save the "un/seen" message for the device. I have to assign it a value at that stage - yes?
Then compare it to thresholds when it is read and if it is above a value (4, 2, 1) then that determines the colour.
That's how I am seeing it at this stage.
Update:
This is some code I wrote and it works but I have found a quirk with it.
(more after the code)
Sorry about all the // lines.
msg1 = {};
machine1 = "BedPi";
machine2 = "TimePi";
machine3 = "TelePi";
//node.warn("Look for this " + msg.payload.Who);
var machine = msg.payload.Who;
//var link = msg.link;
var mstate = msg.payload.Modem;
var ustate = msg.payload.Uplink
var modem;
var uplink;
//node.warn("********");
//node.warn("Machine is " + machine);
//node.warn("link is " + link);
//node.warn("Modem state is " + mstate);
//node.warn("Uplink state is " + ustate);
if (mstate == "Offline")
{
mstate = 0;
} else
{
mstate = 1;
}
if (ustate == "Offline")
{
ustate = 0;
} else
{
ustate = 1;
}
//node.warn("Modem state is " + mstate);
//node.warn("Uplink state is " + ustate);
//node.warn("State is value " + state);
if (machine == machine1)
{
// Machine 1
// node.warn("Machine 1 routine active");
ustate = ustate + 1;
mstate = mstate + 1;
// if (link == "modem")
// {
// Modem
context.set("modem",mstate);
// } else
// if (link == "uplink")
// {
//
context.set("uplink",ustate);
// }
} else
if (machine == machine2)
{
// Machine 2
// node.warn("Machine 2 routine active");
ustate = ustate + 2;
mstate = mstate + 2;
// node.warn("New state value is " + state);
// if (link == "modem")
// {
// Modem
context.set("modem",mstate);
// } else
// if (link == "uplink")
// {
//
context.set("uplink",ustate);
// }
} else
if (machine == machine3)
{
// Machine 3
// node.warn("Machine 3 routine active");
ustate = ustate + 4;
mstate = mstate + 4;
// if (link == "modem")
// {
// Modem
context.set("modem",mstate);
// } else
// if (link == "uplink")
// {
//
context.set("uplink",ustate);
// }
}
// Contexts set. Now determine the output.
if (msg.payload != "HEARTBEAT")
{
return;
}
modem = context.get("modem") || 0;
uplink = context.get("uplink") || 0;
if (modem === 0)
{
uplink = "UNKNOWN";
msg = {payload:'<font color = "red"> <i class="fa fa-bullseye fa-2x" ></i></font>'};
} else
if (modem > 4)
{
node.status({fill: "green",text:machine1});
msg = {payload:'<font color = "lime"> <i class="fa fa-bullseye fa-2x"></i></font>'};
} else
if (modem > 2)
{
node.status({fill: "yellow",text:machine2});
msg = {payload:'<font color = "green"> <i class="fa fa-bullseye fa-2x" ></i></font>'};
} else
if (modem > 1)
{
node.status({fill: "red",text:machine3});
msg = {payload:'<font color = "springgreen"> <i class="fa fa-bullseye fa-2x" ></i></font>'};
}
if (uplink === 0)
{
msg1 = {payload:'<font color = "red"> <i class="fa fa fa-bullseye fa-2x" ></i></font>'};
} else
if (uplink > 4)
{
msg1 = {payload:'<font color = "lime"> <i class="fa fa-bullseye fa-2x" ></i></font>'};
} else
if (uplink > 2)
{
msg1 = {payload:'<font color = "green"> <i class="fa fa-bullseye fa-2x" ></i></font>'};
} else
if (uplink > 1)
{
msg1 = {payload:'<font color = "springgreen"> <i class="fa fa-bullseye fa-2x" ></i></font>'};
} else
if (uplink == "UNKNOWN")
{
msg1 = {payload:'<font color = "black"> <i class="fa fa-question-circle fa-2x" ></i></font>'};
}
return [msg,msg1];
This works as far as I can see.
But the quirk is that the input from all machine is async'.
So the output toggles between the higher two states.
Thoughts?
Update:
I've updated the code posted and this seems a bit better.
But I am still suspicious it is still hiding a problem.
Third update:
Yes, there are still problems.
It works on one machine and not on another and I think it is to do with the messages coming in.
(If you have got this far: Thanks)
To make it so it can work anywhere/how here are the problems as I see them:
1 - the messages are coming in asynchronously and that is the problem. (the/a)
2 - to get around that, I included another input to the node (message) which is the heartbeat and clocks the message through if all three messages are received.
3 - The incoming message is true
or false
to indicate the state.
I change that to 1
and 0
and take than number through the code, adding to it depending on who sent the message - or: from whom the message came.
This works nice but because of timing, the stored value is wiped at the wrong time.
I see the value go from 0 (fresh deploy of the node) to 2
, 3
, 5
and back to 0
.
5
means all three machines are seeing things as good.
When all three machines have sent their message, and that is received the value is sent.
I see two machines have sent their messages and the value is 3
. (I see the flags saying the two message are received.)
When the third one is received, the flags are wiped and I have a value of 5
stored, but only one machine's flag is set.
ARGH!
I can see how I want it to work, but with contexts and setting the value depending on the incoming message - I think - is where it is being wiped.
It is a trick to getting 3 valid messages before sending the resulting message.
Is that any clearer?