parnic / node-screenlogic

Pentair ScreenLogic Javascript library using Node.JS
https://www.npmjs.com/package/node-screenlogic
MIT License
52 stars 14 forks source link

SLSetHeatSetPoint not parsing API response #65

Closed alonweinstein closed 1 year ago

alonweinstein commented 1 year ago

Hi!

I'm using setSetPoint to set the temperature, and it's working fine -- but the emitted event doesn't parse the response from ScreenLogic so there's no way to programmatically verify the temperature was set correctly.

It looks like the messageId is different -- looking at the code SLSetHeatSetPoint expects message ID 12528, but the response I'm getting has 12529:

{"length":8,"_encoding":"utf8","_writeOffset":8,"_readOffset":8,"_buff":{"type":"Buffer","data":[0,0,241,48,0,0,0,0]},"_wroteSize":true,"senderId":0,"messageId":12529,"dataLength":0}

Is this just a matter of changing the messageId to get this parsed properly?

parnic commented 1 year ago

Each message's response is +1 from the message ID. If you'll notice SLSetHeatSetPoint has a getResponseId() that returns MSG_ID (12528) + 1, which is 12529. index.js catches that and fires the setPointChanged event with the parsed response from the equipment:

https://github.com/parnic/node-screenlogic/blob/0834bf44d114fff6966daf24ac16a4a1ddb47b92/index.js#L414

Can you share the code you're using that isn't working properly? My expectation is that setPointChanged's argument has the values you're looking for, but there may be a bug.

parnic commented 1 year ago

Note that if you're still using code similar to https://github.com/parnic/node-screenlogic/issues/64, that won't work as the library functions off named events rather than promises currently. https://github.com/parnic/node-screenlogic/blob/main/example.js shows how to interact with the library.

alonweinstein commented 1 year ago

This is running in an AWS lambda, so there's some additional code around the relevant code itself but I'm pasting it as-is here to give context:

    const body = JSON.parse(event.body)
    const { userName, password, bodyType, temperature } = body;

    const remote = new ScreenLogic.RemoteLogin(userName);

    const p = new Promise(( resolve, reject)  => {
        remote.on('gatewayFound', function(unit) {
            remote.close();

            if (unit && unit.gatewayFound) {
              console.log('unit ' + remote.systemName + ' found at ' + unit.ipAddr + ':' + unit.port);
              const client = new ScreenLogic.UnitConnection(unit.port, unit.ipAddr, password);
              client.on('loggedIn', function() {
                client.setSetPoint(0, bodyType, temperature);
              }).on('setPointChanged', function(ev) {
                // console.log(JSON.stringify(ev));
                client.close();

                resolve({
                    statusCode: 200,
                    headers: { "Content-Type": "text/plain" },
                    body: JSON.stringify(ev)
                });
              })
              client.connect();
            } else {
              console.log('no unit found by that name');
            }
          });

          remote.connect();
    });

    return p;

What I'm doing:

parnic commented 1 year ago

Ah, ok, I see what's going on. This event doesn't provide the new temperature value in its response. From the Pentair equipment, we get the message in your initial post:

[0,0,241,48,0,0,0,0]

The first 2 being the sender id, 0, the second 2 being the message id little-endian (48 << 8 + 241 = 12529), and the final 4 being the size of the payload for the response (0). To get the new temperature, you would probably want to issue a getPoolStatus() and extract the values from there.