This is not working and I don't understand why. (Complicated kinda)

Yeah, I write dog's breakfast for code.

That's who I am.

I am TRYING to optimise this code from the already big dog's breakfast.

The existing one is convoluted and I've found problems with it.
(calling the setBit and clearBit routines.)
Moving on

I receive messages from my 3 devices. (Format below)
and they tell me the state of the nation WRT my uplink.

It kind of did work a long time ago, but I fear that was more luck than coding.
And when I noticed the first stage of bad coding, the whole thing just fell apart.

The biggest problem - IMO - is that I didn't do a good job of making routines.

This opened/s a whole new can of worms with indenting.
But I am wanting to look past that as .... decorative more than useful.

To declare:
When I do { } I do it differently and so there is a bit of confusion

My way:

if (j == "something)
{
      // do stuff here
}
else
if (j == "something else
{
    //  other stuff
}

The way it is done is like this:

if (j == "something){
      // do stuff here
}

Not a big deal. I can usually read around that.
But the else.... I digress

(off topic) why are the lines above different colours?
White, pink and green.

So I stepped way back and started anew.

And I made it handle only ONE sender's messages.

That looked to work so I expanded it to handle all 3.

At this stage I thought I could put the block of code in the switch() block.

So I copied it three times within the constrains of the case{ } part.

Should work

It doesn't.

I can set all the bits for all 3 devices but can't clear them.

The message:
(example)
{"Who":"TimePi","router":"online","pip":"online","modem":"online","internet":"offlline"}

That one is supposed to clear the internet bit (0) of the node's context.
It can set it quite easily - tested.

But it all falls apart when I try to clear the bit.

Sorry this is a big ask.
But I fear I've missed the elephant in my travels.

Someone - please?

/////////////////////////////////
//
//  Routines.
//
//Set Bit
function setBit(number, bitPosition) {
    return number | (1 << bitPosition);
}
//
//Clear Bit
function clearBit(number, bitPosition) {
    const mask = ~(1 << bitPosition);
    return number & mask;
}
//
/////////////////////////////////


let sender = msg.payload.Who

let router = msg.payload.router
let pip = msg.payload.pip
let modem = msg.payload.modem
let internet = msg.payload.internet

let bit
let rstate = context.get("rstate") || 0
let pstate = context.get("pstate") || 0
let mstate = context.get("mstate") || 0
let istate = context.get("istate") || 0


//node.warn("Sender " + sender)
//node.warn("bit " + bit)     //  This doesn't work now I've moved all the code into blocks.
//node.warn("Router " + router)
//node.warn("PIP " + pip)
//node.warn("modem " + modem)
//node.warn("Internet " + internet)

//node.warn("rstate " + rstate)
//node.warn("pip " + pstate)
//node.warn("mstate " + mstate)
//node.warn("istate " + istate)


switch (sender) {
    case "TimePi":
        bit = 0
        //      Router
        if (router == "offline") {
            //
            //    node.warn("router off line")
            rstate = clearBit(rstate, bit)
            context.set("rstate", rstate)
        } else
        //    if (router == "online")
        {
            //
            //    node.warn("router on line")
            rstate = setBit(rstate, bit)
            context.set("rstate", rstate)
        }
        //      Publib IP
        if (pip == "offline") {
            //
            //    node.warn("pip off line")
            pstate = clearBit(pstate, bit)
            context.set("pstate", rstate)
        } else
        //    if (pip == "online") 
        {
            //
            //    node.warn("pip on line")
            pstate = setBit(pstate, bit)
            context.set("pstate", rstate)
        }
        //      modem
        if (modem == "offline") {
            //
            //    node.warn("modem off line")
            mstate = clearBit(mstate, bit)
            context.set("mstate", rstate)
        } else
        //    if (modem == "online") 
        {
            //
            //    node.warn("modem on line")
            mstate = setBit(mstate, bit)
            context.set("mstate", rstate)
        }
        //      Internet IP
        if (internet == "offline") {
            //
            //    node.warn("internet off line")
            istate = clearBit(istate, bit)
            context.set("istate", rstate)
        } else
        //    if (internet == "online") 
        {
            //
            //    node.warn("internet on line")
            istate = setBit(istate, bit)
            context.set("istate", rstate)
        }
        break
    case "TelePi":
        bit = 1
        //      Router
        if (router == "offline") {
            //
            //    node.warn("router off line")
            rstate = clearBit(rstate, bit)
            context.set("rstate", rstate)
        } else
        //    if (router == "online")
        {
            //
            //    node.warn("router on line")
            rstate = setBit(rstate, bit)
            context.set("rstate", rstate)
        }
        //      Publib IP
        if (pip == "offline") {
            //
            //    node.warn("pip off line")
            pstate = clearBit(pstate, bit)
            context.set("pstate", rstate)
        } else
        //    if (pip == "online") 
        {
            //
            //    node.warn("pip on line")
            pstate = setBit(pstate, bit)
            context.set("pstate", rstate)
        }
        //      modem
        if (modem == "offline") {
            //
            //    node.warn("modem off line")
            mstate = clearBit(mstate, bit)
            context.set("mstate", rstate)
        } else
        //    if (modem == "online") 
        {
            //
            //    node.warn("modem on line")
            mstate = setBit(mstate, bit)
            context.set("mstate", rstate)
        }
        //      Internet IP
        if (internet == "offline") {
            //
            //    node.warn("internet off line")
            istate = clearBit(istate, bit)
            context.set("istate", rstate)
        } else
        //    if (internet == "online") 
        {
            //
            //    node.warn("internet on line")
            istate = setBit(istate, bit)
            context.set("istate", rstate)
        }
        break
    case "BedPi":
        bit = 2
        //      Router
        if (router == "offline") {
            //
            //    node.warn("router off line")
            rstate = clearBit(rstate, bit)
            context.set("rstate", rstate)
        } else
        //    if (router == "online")
        {
            //
            //    node.warn("router on line")
            rstate = setBit(rstate, bit)
            context.set("rstate", rstate)
        }
        //      Publib IP
        if (pip == "offline") {
            //
            //    node.warn("pip off line")
            pstate = clearBit(pstate, bit)
            context.set("pstate", rstate)
        } else
        //    if (pip == "online") 
        {
            //
            //    node.warn("pip on line")
            pstate = setBit(pstate, bit)
            context.set("pstate", rstate)
        }
        //      modem
        if (modem == "offline") {
            //
            //    node.warn("modem off line")
            mstate = clearBit(mstate, bit)
            context.set("mstate", rstate)
        } else
        //    if (modem == "online") 
        {
            //
            //    node.warn("modem on line")
            mstate = setBit(mstate, bit)
            context.set("mstate", rstate)
        }
        //      Internet IP
        if (internet == "offline") {
            //
            //    node.warn("internet off line")
            istate = clearBit(istate, bit)
            context.set("istate", rstate)
        } else
        //    if (internet == "online") 
        {
            //
            //    node.warn("internet on line")
            istate = setBit(istate, bit)
            context.set("istate", rstate)
        }
        break
}
return msg;

Andrew, here is a link to an explanation of the JavaScript if, else, and else if statements. Take a read and see if that clears it up for you.

Um, the link?

:person_shrugging:

oops, haven't had coffee yet

2 Likes

No worries.

Thanks.

Because you are only putting on a quote on the beginning of 'something' & 'something else' and not also at the end of the string...

"something else
"something else"

1 Like

Thanks.

I'm kinda overwhelmed with all the stuff I am trying.

FOUND IT!

Didn't change all the names.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.