Javascript "switch" throwing out unexpected result

I have my projector (Epson 3020) up-and-running with the Runco plugin. However, I also want to be able to poll the status of the projector. With the Runco, the command “op status ?” is sent to receive the current projector status. With the Epson the command is “pwr?” to do the same thing. I have used iTest from Global Cache to verify the response I get to “pwr?”, and it works fine. However, the response is formatted a bit differently that the Runco’s. The author of the plugin used the trim and split functions to pull out the code, and that wasn’t working. Instead, I decided to use the “includes” parameter to simply check for the value to see if the projector was on (01 in my case). I tested the code in a javascript editor and it worked as expected. However, when I incorporate it into the plugin and tie the value to a label the label displays “Unknown”. The code should constrain the values to simply “On” or “Off”, so I’m a bit confused. Here’s my code:

    flushReceiveBuffer();
    socket.send("pwr?\r");
    sleep(250);     // sleep briefly just to make sure we get the whole response
    response = socket.receive({ timeout: 500 });
    responseVal=response.includes('01');
    switch(responseVal) {
        case false:   // cooling down, off
            powerStatus = "Off";
            break;
            
        case true:   // displaying
            powerStatus = "On";
            break;

        default:
            // unknown/unhandled status (or an error), default to off
            powerStatus = "Off";
            break;
    }

Seems like any error or unhandled status should set the value of powerStatus to “Off”, but it is showing “Unknown” instead. Any help would be appreciated!

You really need to be using console.log statements to debug your code. The error isn’t always where you think it is. How do you know it’s even reaching your “switch” statement? I don’t think there’s anything wrong with your “switch” statement. Although I do think it’s a bit odd that you chose to use a “switch” for a boolean which only has 2 possible values. But again, I don’t think that is your issue.

I think the issue is the line above:

responseVal=response.includes('01');

The “includes” method is only supported in ES6 (JavaScript 2015). The Home Remote’s JavaScript engine is only ECMAScript 5.1 compliant.

You need to choose a method that is compatible with ES 5.1. For this case, you should be using “indexOf”. If the return value is -1 then you will know that 01 was not found in the return string.

var responseVal;
var powerStatus;
responseVal=response.indexOf('01');
if(responseVal == -1)
{
    powerStatus = "Off";
}
else
{
    powerStatus = "On";
}

Thank you for your reply!! It was very helpful. It’s good to know the compatibility version… in the sites I visited the command I was using seemed widely compatible, but not entirely so. I’m glad to know this is one of those cases.

Regarding the choice of boolean, you can probably tell that I’m a hack programmer at best :slight_smile: I would prefer to not use a boolean, but in this case I really only care if the projector is in one of two states–on or off (which is what it basically is in the Runco code, which breaks it down to “On” or “Off” even though there are 4 cases). When I sent the “pwr?” command via iTest it was responding with only three variables. When the projector was off it returned “P???0C?”, when it was warming up it returned “PWR=02”, and when on returned “PWR=01”. The last two were expected, but when off I don’t know why it didn’t return “PWR=00”, which is supposedly what it should do via the 3020 ESC codes I was sent by Epson. Also, in iTest, it seemed to have a carriage return and a colon on the next line. The “includes” allowed me to bypass all of that and simply look for “01”.

I was trying to pass the value received (response) to the powerStatus variable so I could view it in the label (to make sure the value was what I expected), but that didn’t work for some reason. I didn’t mess with it for too long, but it would have been nice to see if the value returned matched what I was seeing in iTest.

As for the console.log, I was using that in the javascript coding that I was attempting. I didn’t use it here because I’m not sure exactly where this information is displayed at runtime (where is the log located?). It would be MUCH easier to use the console.log if I knew where the output was shown. If you could direct me to that information it would be very helpful! Now that I know it resides somewhere I’ll try to locate it.

EDIT:

LOL! As soon as I went back to the editor I found it hiding in the bottom-left corner.