Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle different Nuki states #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions nukibridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ global.NUKI_LOCK_STATE_UNLATCHING = 7;
global.NUKI_LOCK_STATE_MOTOR_BLOCKED = 254;
global.NUKI_LOCK_STATE_UNDEFINED = 255;

global.HOMEKIT_LOCK_STATE_UNSECURED = 0;
global.HOMEKIT_LOCK_STATE_SECURED = 1;
global.HOMEKIT_LOCK_STATE_JAMMED = 2;
global.HOMEKIT_LOCK_STATE_UNKNOWN = 3;

var DEFAULT_REQUEST_TIMEOUT_LOCK_STATE = 15000;
var DEFAULT_REQUEST_TIMEOUT_LOCK_ACTION = 45000;
var DEFAULT_WEBHOOK_SERVER_PORT = 51827;
Expand Down Expand Up @@ -510,9 +515,8 @@ NukiLock.prototype.isLocked = function isLocked(callback /*(err, isLocked)*/, fo
if(forceRequest || this.nukiBridge.lockStateMode === LOCK_STATE_MODE_REQUEST_LOCKSTATE || this.nukiBridge.lockStateMode === LOCK_STATE_MODE_REQUEST_LASTKNOWNLOCKSTATE) {
var callbackWrapper = (function(err, json) {
if(err) {
var cachedIsLocked = this.getIsLockedCached();
this.log("Request for lock state aborted. This is no problem and might happen due to canceled request or due to long response time of the Nuki bridge. Using cached value isLocked = '%s'.", cachedIsLocked);
callback(null, cachedIsLocked);
this.log("Request for lock state aborted. This might happen due to canceled request or due to long response time of the Nuki bridge.");
callback(null, NUKI_LOCK_STATE_UNDEFINED);
}
else {
var state = NUKI_LOCK_STATE_UNDEFINED;
Expand Down Expand Up @@ -547,12 +551,20 @@ NukiLock.prototype.getLowBatt = function getLowBatt(callback /*(err, lowBattt)*/
};

NukiLock.prototype._isLocked = function _isLocked(state) {
var isLocked =
state == NUKI_LOCK_STATE_LOCKED ||
state == NUKI_LOCK_STATE_LOCKING ||
state == NUKI_LOCK_STATE_UNCALIBRATED ||
state == NUKI_LOCK_STATE_MOTOR_BLOCKED ||
state == NUKI_LOCK_STATE_UNDEFINED;
var isLocked = HOMEKIT_LOCK_STATE_UNKNOWN;
switch(state) {
case NUKI_LOCK_STATE_LOCKED:
isLocked = HOMEKIT_LOCK_STATE_SECURED;
break;
case NUKI_LOCK_STATE_MOTOR_BLOCKED:
isLocked = HOMEKIT_LOCK_STATE_JAMMED;
break;
case NUKI_LOCK_STATE_UNCALIBRATED, NUKI_LOCK_STATE_UNDEFINED:
isLocked = HOMEKIT_LOCK_STATE_UNKNOWN;
break;
default:
isLocked = HOMEKIT_LOCK_STATE_UNSECURED;
}
return isLocked;
};

Expand Down Expand Up @@ -620,4 +632,4 @@ NukiLock.prototype._getLockStorageKey = function _getLockStorageKey() {
return 'bridge-'+this.nukiBridge.bridgeUrl+'-lock-'+this.instanceId+'-'+this.id+'-cache';
};

module.exports = {NukiBridge: NukiBridge, NukiLock: NukiLock};
module.exports = {NukiBridge: NukiBridge, NukiLock: NukiLock};