From 32fda0f4b2bc53d41ba56149444af60c6e579089 Mon Sep 17 00:00:00 2001
From: Erick Hitter <services@ethitter.com>
Date: Tue, 30 Aug 2016 17:25:48 -0700
Subject: [PATCH] Introduce method to check if lock is in some state of
 connection

---
 lib/lock.js | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/lib/lock.js b/lib/lock.js
index de26855..9f41107 100644
--- a/lib/lock.js
+++ b/lib/lock.js
@@ -53,24 +53,24 @@ Lock.prototype.connect = function() {
 
     // initialize the secure session
     this._secureSession = new SecureLockSession(
-      this._peripheral,
-      characteristicByUuid("bd4ac6130b4511e38ffd0800200c9a66"),
-      characteristicByUuid("bd4ac6140b4511e38ffd0800200c9a66"),
-      this._offlineKeyOffset
+        this._peripheral,
+        characteristicByUuid("bd4ac6130b4511e38ffd0800200c9a66"),
+        characteristicByUuid("bd4ac6140b4511e38ffd0800200c9a66"),
+        this._offlineKeyOffset
     );
     this._secureSession.setKey(this._offlineKey);
 
     // intialize the session
     this._session = new LockSession(
-      this._peripheral,
-      characteristicByUuid("bd4ac6110b4511e38ffd0800200c9a66"),
-      characteristicByUuid("bd4ac6120b4511e38ffd0800200c9a66")
+        this._peripheral,
+        characteristicByUuid("bd4ac6110b4511e38ffd0800200c9a66"),
+        characteristicByUuid("bd4ac6120b4511e38ffd0800200c9a66")
     );
 
     // start the sessions
     return Promise.join(
-      this._secureSession.start(),
-      this._session.start()
+        this._secureSession.start(),
+        this._session.start()
     );
   }.bind(this)).then(function() {
     // send SEC_LOCK_TO_MOBILE_KEY_EXCHANGE
@@ -127,9 +127,9 @@ Lock.prototype.lock = function() {
 
 Lock.prototype.unlock = function() {
   return this.status(true).then(function(status) {
-   if (status == 'locked')
+    if (status == 'locked')
       return this.forceUnlock();
-   }.bind(this));
+  }.bind(this));
 };
 
 Lock.prototype.status = function() {
@@ -178,4 +178,18 @@ Lock.prototype.disconnect = function() {
   }
 };
 
+Lock.prototype.isConnected = function() {
+  debug( 'checking connection...' );
+
+  if (
+      this._session instanceof LockSession &&
+      'undefined' !== typeof this._session._peripheral.state &&
+      'disconnected' !== this._session._peripheral.state
+  ) {
+    return true;
+  }
+
+  return false;
+};
+
 module.exports = Lock;
-- 
GitLab