Skip to content

Commit

Permalink
Polling of position_url #55
Browse files Browse the repository at this point in the history
  • Loading branch information
dxdc committed Jun 28, 2021
1 parent e64584e commit 2fb37c5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Add the accessory in `config.json` in your home directory inside `.homebridge`.
"up_url": "http://1.2.3.4/window/up",
"down_url": "http://1.2.3.4/window/down",
"position_url": "http://1.2.3.4/window/position",
"position_interval": 15000,
"position_jsonata": "ShutterPosition1",
"stop_url": "http://1.2.3.4/window/stop",
"http_options": {
Expand Down Expand Up @@ -182,6 +183,8 @@ This implementation does take into account whether or not the blinds are moving,

If more robust handling of `position_url` responses in JSON format is needed, `position_jsonata` can be defined. This allows a [JSONata](https://jsonata.org/) expression to be set to parse the result. For example, considering the following JSON response:

`position_interval` is specified in ms, and defaults to 15000 ms (15 s). It can be used to set the polling frequency, particularly useful in cases where the blinds can also be controlled externally.

```json
{
"example": { "value": 4 }
Expand Down
7 changes: 7 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@
"placeholder": "http://1.2.3.4/window/position",
"format": "uri"
},
"position_interval": {
"title": "Polling interval for Position URL",
"type": "integer",
"description": "Milliseconds between polling requests to position URL",
"minimum": 5000,
"placeholder": 15000
},
"position_jsonata": {
"title": "Jsonata for Position URL",
"type": "string",
Expand Down
36 changes: 31 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function BlindsHTTPAccessory(log, config) {
this.log.error(`Error parsing jsonata: ${err.message}`);
}
}
this.positionPollInterval = Math.max(parseInt(config.position_interval, 10) || 15000, 5000);
this.stopURL = config.stop_url || false;
this.httpOptions = config.http_options || config.http_method || { method: 'POST' };
this.successCodes = config.success_codes || [200];
Expand Down Expand Up @@ -93,13 +94,13 @@ function BlindsHTTPAccessory(log, config) {
this.lastCommandMoveUp = this.currentTargetPosition % 100 > 0 ? null : this.currentTargetPosition === 100;

if (this.positionURL) {
this.getCurrentPosition(
this.updatePositionByUrl();

this.pollInterval = setInterval(
function () {
this.currentTargetPosition = this.lastPosition;
if (this.currentTargetPosition % 100 === 0) {
this.lastCommandMoveUp = this.currentTargetPosition === 100;
}
this.updatePositionByUrl();
}.bind(this),
this.positionPollInterval,
);
}

Expand Down Expand Up @@ -326,6 +327,31 @@ BlindsHTTPAccessory.prototype.setCurrentPositionByUrl = function (callback) {
);
};

BlindsHTTPAccessory.prototype.updatePositionByUrl = function () {
if (this.stopTimeout !== null || this.stepInterval !== null || this.lagTimeout !== null) {
if (this.verbose) {
this.log.info(
`Polling skipped (updatePositionByUrl); stopTimeout: ${this.stopTimeout}, stepInterval: ${this.stepInterval}, lagTimeout: ${this.lagTimeout}`,
);
}
return; // blinds in motion by plugin
}

if (this.verbose) {
this.log.info('Polling started (updatePositionByUrl)');
}

this.getCurrentPosition(function () {
this.currentTargetPosition = this.lastPosition;
if (this.currentTargetPosition % 100 === 0) {
this.lastCommandMoveUp = this.currentTargetPosition === 100;
}
if (this.verbose) {
this.log.info('Polling finished (updatePositionByUrl)');
}
}).bind(this);
};

BlindsHTTPAccessory.prototype.getTargetPosition = function (callback) {
if (this.verbose) {
this.log.info(`Requested TargetPosition: ${this.currentTargetPosition}%`);
Expand Down

0 comments on commit 2fb37c5

Please sign in to comment.