From 7ee0653ed8f119b315e3e4b1b97109ef9ae2e346 Mon Sep 17 00:00:00 2001 From: Josh Anderson Date: Fri, 30 Aug 2019 17:31:57 +0100 Subject: [PATCH 1/4] Keep a target position attribute --- src/SomaShade.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/SomaShade.js b/src/SomaShade.js index fd8af55..06ec6e8 100644 --- a/src/SomaShade.js +++ b/src/SomaShade.js @@ -31,6 +31,7 @@ class SomaShade extends EventEmitter { this.group = null; this.positionLastChanged = null; this.batteryLevelLastChanged = null; + this.targetPosition = -1; this.state = 'unknown'; this.connectTime = null; @@ -51,6 +52,7 @@ class SomaShade extends EventEmitter { Object.defineProperty(this, '_position', {set: function(position) { this.position = position; + this.targetPosition = position; this.positionLastChanged = new Date(); this.state = this.position === -1 ? 'unknown' : this.position === 0 ? 'closed' : 'open'; this.emit('positionChanged', this.getState()); @@ -73,6 +75,7 @@ class SomaShade extends EventEmitter { var closePercent = position; closePercent = 100 - closePercent; closePercent = closePercent.toString(); + this.targetPosition = position; if (this.movePercentCharacteristic == null) { this.desiredPositionOnReconnect = position; return; @@ -88,6 +91,8 @@ class SomaShade extends EventEmitter { this.motorCharacteristic.write(Buffer.from([0x69]), false, (error) => { if (error) { this.log(error); + } else { + this.targetPosition = 100; } }); } @@ -96,6 +101,8 @@ class SomaShade extends EventEmitter { this.motorCharacteristic.write(Buffer.from([0x96]), true, (error) => { if (error) { this.log(error); + } else { + this.targetPosition = 0; } }); } @@ -219,6 +226,7 @@ class SomaShade extends EventEmitter { batteryLevelLastChanged: this.batteryLevelLastChanged, position: this.position, positionLastChanged: this.positionLastChanged, + targetPosition: this.targetPosition, connectionState: this.connectionState, state: this.state, group: this.group, From bf870d3a17600a6f4ddb4fba49f94da0d4e16238 Mon Sep 17 00:00:00 2001 From: Josh Anderson Date: Fri, 30 Aug 2019 17:32:25 +0100 Subject: [PATCH 2/4] Set opening/closing in addition to open/closed --- src/SomaShade.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/SomaShade.js b/src/SomaShade.js index 06ec6e8..b5f6ba5 100644 --- a/src/SomaShade.js +++ b/src/SomaShade.js @@ -80,9 +80,12 @@ class SomaShade extends EventEmitter { this.desiredPositionOnReconnect = position; return; } + var that = this; this.movePercentCharacteristic.write(Buffer.from([closePercent.toString(16)]), false, function(error) { if (error) { - this.log('ERROR writing to position - %o', error); + that.log('ERROR writing to position - %o', error); + } else if (position != that.position) { + that.state = position > that.position ? 'opening' : 'closing'; } }); } @@ -92,6 +95,7 @@ class SomaShade extends EventEmitter { if (error) { this.log(error); } else { + this.state = 'opening'; this.targetPosition = 100; } }); @@ -102,6 +106,7 @@ class SomaShade extends EventEmitter { if (error) { this.log(error); } else { + this.state = 'closing'; this.targetPosition = 0; } }); From 41d3523163a1bfa536df1988acd66d900ef07905 Mon Sep 17 00:00:00 2001 From: Josh Anderson Date: Fri, 30 Aug 2019 18:21:54 +0100 Subject: [PATCH 3/4] Use arrow functions universally --- src/SomaShade.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/SomaShade.js b/src/SomaShade.js index b5f6ba5..d01dd5f 100644 --- a/src/SomaShade.js +++ b/src/SomaShade.js @@ -80,12 +80,11 @@ class SomaShade extends EventEmitter { this.desiredPositionOnReconnect = position; return; } - var that = this; - this.movePercentCharacteristic.write(Buffer.from([closePercent.toString(16)]), false, function(error) { + this.movePercentCharacteristic.write(Buffer.from([closePercent.toString(16)]), false, (error) => { if (error) { - that.log('ERROR writing to position - %o', error); - } else if (position != that.position) { - that.state = position > that.position ? 'opening' : 'closing'; + this.log('ERROR writing to position - %o', error); + } else if (position != this.position) { + this.state = position > this.position ? 'opening' : 'closing'; } }); } From 127e8e3886110961c63b0107f9cb01adaf7fe01d Mon Sep 17 00:00:00 2001 From: Josh Anderson Date: Sat, 7 Sep 2019 19:54:03 +0100 Subject: [PATCH 4/4] Move state/target position earlier, assuming command will go through --- src/SomaShade.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/SomaShade.js b/src/SomaShade.js index d01dd5f..47b7aa6 100644 --- a/src/SomaShade.js +++ b/src/SomaShade.js @@ -76,6 +76,9 @@ class SomaShade extends EventEmitter { closePercent = 100 - closePercent; closePercent = closePercent.toString(); this.targetPosition = position; + if (position != this.position) { + this.state = position > this.position ? 'opening' : 'closing'; + } if (this.movePercentCharacteristic == null) { this.desiredPositionOnReconnect = position; return; @@ -83,30 +86,30 @@ class SomaShade extends EventEmitter { this.movePercentCharacteristic.write(Buffer.from([closePercent.toString(16)]), false, (error) => { if (error) { this.log('ERROR writing to position - %o', error); - } else if (position != this.position) { - this.state = position > this.position ? 'opening' : 'closing'; } }); } moveUp() { + if (this.state != 100) { + this.state = 'opening'; + } + this.targetPosition = 100; this.motorCharacteristic.write(Buffer.from([0x69]), false, (error) => { if (error) { this.log(error); - } else { - this.state = 'opening'; - this.targetPosition = 100; } }); } moveDown() { + if (this.state != 0) { + this.state = 'closing'; + } + this.targetPosition = 0; this.motorCharacteristic.write(Buffer.from([0x96]), true, (error) => { if (error) { this.log(error); - } else { - this.state = 'closing'; - this.targetPosition = 0; } }); }