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

addClient disconnecting #35

Closed coricidin12 closed 4 years ago

coricidin12 commented 4 years ago

Thanks for this library, it's great! I'm using the addClient() function to subscribe to updates from the controller which works fine, but it seems to disconnect after about 20 minutes or so and stop receiving updates. Also if I try to send commands to the controller after that, it gives me an error that says "this socket has been ended by the other party". Can anyone else confirm this? Is there some autoreconnect procedure? I want to have a persistent 24/7 connection to the controller.

parnic commented 4 years ago

@bshep found and added this feature, maybe he'll have some more insight.

I've always used polling for myself rather than subscribing and holding a connection.

bshep commented 4 years ago

Thanks for this library, it's great! I'm using the addClient() function to subscribe to updates from the controller which works fine, but it seems to disconnect after about 20 minutes or so and stop receiving updates. Also if I try to send commands to the controller after that, it gives me an error that says "this socket has been ended by the other party". Can anyone else confirm this? Is there some autoreconnect procedure? I want to have a persistent 24/7 connection to the controller.

Can you provide some test code to reproduce the issue? I didnt test for prolonged periods, these calls were RE'd from what I can see on the network stream. It's possible you need to send periodic messages to keep the connection open since the regular app sends stuff back/forth several times a second, once i get some test code i can try some different options.

coricidin12 commented 4 years ago

Here is my code. I tried keeping it alive by sending a getVersion() once a minute, but that didn't seem to work.

var hottubClient = new ScreenLogic.UnitConnection(80, '192.168.X.XX');
hottubClient.connect()
hottubClient.on('loggedIn', function() {
    this.getControllerConfig();
    this.addClient(1234);
}).on('version', function(version) {
    this.getPoolStatus();
    console.log(' version=' + version.version);
}).on('poolStatus', function(status) {
    hottub.setpoint = status.setPoint[1];
    hottub.currentTemp = status.currentTemp[1];
    hottub.heatStatus = status.heatStatus[1];

  for (var i = 0; i < status.circuitArray.length; i++) {
      hottub[circuitIndex[status.circuitArray[i].id]] = status.circuitArray[i].state;
  }
  console.log(hottub);
  socketClient.emit('update',hottub);
}).on('controllerConfig', function(config) {
  for (var i = 0; i < config.bodyArray.length; i++) {
      circuitIndex[config.bodyArray[i].circuitId] = config.bodyArray[i].name;
  }
}).on('loginFailed', function() {
  console.log(' unable to login (wrong password?)');
});
bshep commented 4 years ago

Here is my code. I tried keeping it alive by sending a getVersion() once a minute, but that didn't seem to work.

Please try this code, i have had it running for over 30mins, i ran out of time to test it longer, but will leave it running overnight:

https://gist.github.com/bshep/a1d0c16819a0722f22454eb1a49662d0

If it doesn't work then go to 'index.js' and change the following and try again, as I said I ran out of time to do testing so I'm not sure if the following is necessary or not:

    this.client = new net.Socket();
    this.client.setKeepAlive(true,10 * 1000); // <<--- add this line after line 135
    var _this = this;
    var buffer = Buffer.alloc(1024);

Update: Been running now close to 24hrs

bshep commented 4 years ago

Here is my code. I tried keeping it alive by sending a getVersion() once a minute, but that didn't seem to work.

Please try this code, i have had it running for over 30mins, i ran out of time to test it longer, but will leave it running overnight:

https://gist.github.com/bshep/a1d0c16819a0722f22454eb1a49662d0

@coricidin12 I tested above code + patch for 36+ Hrs, eventually it disconnected due to a power outage, will submit a PR to master to include the keepAlive setting as I don't see how it can cause a bad side-effect, of note if I don't poll every so often I usually get disconnected after 10mins, i haven't tested how often polling is needed, but the above test is set for 30s.

parnic commented 4 years ago

This was merged with commit 2e5203fb85

coricidin12 commented 4 years ago

I've been testing your code (without the index.js patch) for 5 hours without disconnect. Thanks!

parnic commented 2 years ago

I've just added a pingServer() function that you can call periodically to keep this connection alive if you want to avoid something like requesting the pool status every so often.

https://github.com/parnic/node-screenlogic/commit/56361de2dc40cbee3395d343d84978dc0fcc8674