Skip to content

Commit

Permalink
feat: Add support for regular epxression matching for current position.
Browse files Browse the repository at this point in the history
  • Loading branch information
QuickSander committed Jun 15, 2020
1 parent c748475 commit 50bf9a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ The configuration can contain the following properties:
* `name` \<string\> **required**: Defines the name which is later displayed in HomeKit
* `getCurrentPosUrl` \<string | [urlObject](#urlobject)\> **required**: Defines the url
(and other properties when using an urlObject) to query the current position from the curtain.
It currently expects the http server to return a integer ranging from 0-100 (step 1) leaving out any html markup.
It expects the http server to return a integer ranging from 0-100 (step 1) leaving out any html markup when no `getCurrentPosRegEx`
is provided.
* `getCurrentPosRegEx` \<string\> **optional**: A regular expression from which the first matched group determines the position.
* `getPositionStateUrl` \<string | [urlObject](#urlobject)\> **optional**: Defines the url
(and other properties when using an urlObject) to query the current state from the curtain.
It expects the http server to return a integer '0' (Closing), '1' (Opening) or '2' (Idle) leaving out any html markup.
Note that Homekit ignores this state as it rather compares _CurrentPosition_ with _TargetPosition_.
* `setTargetPosUrl` \<string | [urlObject](#urlobject)\> **optional**: Defines the url
* `setTargetPosUrl` \<string | [urlObject](#urlobject)\> **required**: Defines the url
(and other properties when using an urlObject) to set the target position at the curtain.
Any `%d` format specifier will be replaced by the requested target position.
* `getTargetPosUrl` \<string | [urlObject](#urlobject)\> **optional**: Defines the url
Expand Down
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ let Service, Characteristic, api;

const configParser = require("homebridge-http-base").configParser;
const http = require("homebridge-http-base").http;
const notifications = require("homebridge-http-base").notifications;
const PullTimer = require("homebridge-http-base").PullTimer;

const PACKAGE_JSON = require('./package.json');
Expand Down Expand Up @@ -71,6 +70,8 @@ function HttpCurtain(log, config) {
this.validateUrl('setTargetPosUrl', true);
this.validateUrl('getTargetPosUrl');
this.validateUrl('identifyUrl');

this.getCurrentPosRegEx = config.getCurrentPosRegEx || '';

this.homebridgeService = new Service.WindowCovering(this.name);

Expand Down Expand Up @@ -185,7 +186,16 @@ HttpCurtain.prototype = {
callback(new Error("Got http error code " + response.statusCode));
}
else {
if(this.getCurrentPosRegEx) {
let matches = body.match(this.getCurrentPosRegEx);
if(matches.length > 1) {
body = matches[1]
if (this.debug)
this.log("Retrieving current position via regular expression. Match: %s", matches[0]);
}
}
const posValue = parseInt(body);

if (this.debug)
this.log("Position value is currently at: %s\%", posValue);

Expand Down

0 comments on commit 50bf9a6

Please sign in to comment.