From 562a9993aa45c736c9f1b81d34caae9345fcba55 Mon Sep 17 00:00:00 2001 From: Parnic Date: Mon, 2 Apr 2018 17:09:06 -0500 Subject: [PATCH] Updated example Close the finder when we've found a unit. Request and handle each piece of data in sequence instead of requesting it all at once and then closing when the last expected message comes through. This is potentially slightly slower, but much clearer and 'safer'. --- example.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/example.js b/example.js index 1cfb057..4c6d44b 100755 --- a/example.js +++ b/example.js @@ -2,21 +2,23 @@ const ScreenLogic = require('./index'); var finder = new ScreenLogic.FindUnits(); finder.on('serverFound', function(server) { + finder.close(); connect(new ScreenLogic.UnitConnection(server)); }); finder.search(); +// use this instead of the above `finder` logic if you want to use a direct connection //connect(new ScreenLogic.UnitConnection(80, '10.0.0.85')); function connect(client) { client.on('loggedIn', function() { this.getVersion(); + }).on('version', function(version) { this.getPoolStatus(); - this.getChemicalData(); - this.getSaltCellConfig(); - this.getControllerConfig(); + console.log(" version=" + version.version); }).on('poolStatus', function(status) { + this.getChemicalData(); console.log(" pool ok=" + status.ok); console.log(" air temp=" + status.airTemp); console.log(" salt ppm=" + status.saltPPM); @@ -24,18 +26,17 @@ function connect(client) { console.log(" saturation=" + status.saturation); console.log(" spa active=" + status.isSpaActive()); console.log(" pool active=" + status.isPoolActive()); - }).on('controllerConfig', function(config) { - console.log(" controller is in celsius=" + config.degC); - client.close(); - finder.close(); }).on('chemicalData', function(chemData) { + this.getSaltCellConfig(); console.log(" calcium=" + chemData.calcium); console.log(" cyanuric acid=" + chemData.cyanuricAcid); console.log(" alkalinity=" + chemData.alkalinity); }).on('saltCellConfig', function(saltCellConfig) { + this.getControllerConfig(); console.log(" salt cell installed=" + saltCellConfig.installed); - }).on('version', function(version) { - console.log(" version=" + version.version); + }).on('controllerConfig', function(config) { + console.log(" controller is in celsius=" + config.degC); + client.close(); }); client.connect();