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'.
This commit is contained in:
Parnic
2018-04-02 17:09:06 -05:00
parent 973f01ae37
commit 562a9993aa

View File

@ -2,21 +2,23 @@ const ScreenLogic = require('./index');
var finder = new ScreenLogic.FindUnits(); var finder = new ScreenLogic.FindUnits();
finder.on('serverFound', function(server) { finder.on('serverFound', function(server) {
finder.close();
connect(new ScreenLogic.UnitConnection(server)); connect(new ScreenLogic.UnitConnection(server));
}); });
finder.search(); 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')); //connect(new ScreenLogic.UnitConnection(80, '10.0.0.85'));
function connect(client) { function connect(client) {
client.on('loggedIn', function() { client.on('loggedIn', function() {
this.getVersion(); this.getVersion();
}).on('version', function(version) {
this.getPoolStatus(); this.getPoolStatus();
this.getChemicalData(); console.log(" version=" + version.version);
this.getSaltCellConfig();
this.getControllerConfig();
}).on('poolStatus', function(status) { }).on('poolStatus', function(status) {
this.getChemicalData();
console.log(" pool ok=" + status.ok); console.log(" pool ok=" + status.ok);
console.log(" air temp=" + status.airTemp); console.log(" air temp=" + status.airTemp);
console.log(" salt ppm=" + status.saltPPM); console.log(" salt ppm=" + status.saltPPM);
@ -24,18 +26,17 @@ function connect(client) {
console.log(" saturation=" + status.saturation); console.log(" saturation=" + status.saturation);
console.log(" spa active=" + status.isSpaActive()); console.log(" spa active=" + status.isSpaActive());
console.log(" pool active=" + status.isPoolActive()); 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) { }).on('chemicalData', function(chemData) {
this.getSaltCellConfig();
console.log(" calcium=" + chemData.calcium); console.log(" calcium=" + chemData.calcium);
console.log(" cyanuric acid=" + chemData.cyanuricAcid); console.log(" cyanuric acid=" + chemData.cyanuricAcid);
console.log(" alkalinity=" + chemData.alkalinity); console.log(" alkalinity=" + chemData.alkalinity);
}).on('saltCellConfig', function(saltCellConfig) { }).on('saltCellConfig', function(saltCellConfig) {
this.getControllerConfig();
console.log(" salt cell installed=" + saltCellConfig.installed); console.log(" salt cell installed=" + saltCellConfig.installed);
}).on('version', function(version) { }).on('controllerConfig', function(config) {
console.log(" version=" + version.version); console.log(" controller is in celsius=" + config.degC);
client.close();
}); });
client.connect(); client.connect();