diff --git a/lib/lock.js b/lib/lock.js index 21c53788f26c9b5ce139df246225498100da2c2f..de26855bea5f3bc0a6368e801ecfe7274a41e144 100644 --- a/lib/lock.js +++ b/lib/lock.js @@ -108,19 +108,15 @@ Lock.prototype.connect = function() { Lock.prototype.forceLock = function() { debug('locking...'); - console.log('chamou lock'); var cmd = this._session.buildCommand(0x0b); return this._session.execute(cmd); - -}; +} Lock.prototype.forceUnlock = function() { debug('unlocking...'); - console.log('calling unlock'); - var cmd = this._session.buildCommand(0x0a); return this._session.execute(cmd); -}; +} Lock.prototype.lock = function() { return this.status().then(function(status) { @@ -139,7 +135,6 @@ Lock.prototype.unlock = function() { Lock.prototype.status = function() { debug('status...'); - var cmd = new Buffer(0x12); cmd.fill(0x00); cmd.writeUInt8(0xee, 0x00); // magic diff --git a/package.json b/package.json index 80acdc5f34561ba1442f3b984ef9514fa376fdbb..b31d70d6a5587a15dd1d9debd527dac37e7a6fb7 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "debug": "^2.2.0", "express": "^4.13.3", "morgan": "^1.6.1", - "noble": "^1.1.0" + "noble": "^1.1.0", + "asyncawait": "^0.7.4" }, "repository": { "type": "git", diff --git a/server.js b/server.js index 0a7839b6c17d6f9064160d816828455dd31a111f..9f54bd4c9279d91643e27430a0e1cceeadd12eb7 100755 --- a/server.js +++ b/server.js @@ -3,7 +3,8 @@ var augustctl = require('./index'); var express = require('express'); var morgan = require('morgan'); - +var await = require('asyncawait/await'); +var async = require('asyncawait/async'); var config = require(process.env.AUGUSTCTL_CONFIG || './config.json'); var DEBUG = process.env.NODE_ENV !== 'production'; @@ -22,35 +23,39 @@ app.get('/api/unlock', function(req, res) { return; } - lock.connect().then(function(){ - lock.status().then(function(status){ +var execStatus = async(function() { + + var status = await(lock.status()); + + if(status == 'locked') + { - ret['ret'] = status; + var cmd = await(lock.forceUnlock()); + ret['msg'] = 'Command completed. Disconnected.'; + ret['status'] = 0; + ret['ret'] = 'unlocked'; + console.log('Released unlock request'); - if(status == 'locked') - { - lock.unlock().then(function() { + } + else + { + ret['status'] = 1; + ret['msg'] = 'Lock is already unlocked'; + res.json(ret); - lock.disconnect().then(function() { - ret['msg'] = 'Command completed. Disconnected.'; - ret['status'] = 0; - ret['ret'] = 'unlocked'; - res.json(ret); - }); + } - }); - } - else - { - ret['status'] = 1; - ret['msg'] = 'Lock is already unlocked'; - res.json(ret); - } + lock.disconnect(); + res.json(ret); +}); + lock.connect().then(function(){ - }); + var exec = execStatus(); + }).catch(function(e) { + console.error(e.toString()); }); }); @@ -63,36 +68,38 @@ app.get('/api/lock', function(req, res) { return; } - lock.connect().then(function(){ - lock.status().then(function(status){ + var execLock = async(function() { + var status = await(lock.status()); + + if(status == 'unlocked') + { - ret['ret'] = status; + var cmd = await(lock.forceLock()); + ret['msg'] = 'Command completed. Disconnected.'; + ret['status'] = 0; + ret['ret'] = 'locked'; + console.log('Released lock request'); - if(status == 'unlocked') - { - lock.lock().then(function() { + } + else + { + ret['status'] = 1; + ret['msg'] = 'Lock is already locked'; - lock.disconnect().then(function() { - ret['msg'] = 'Command completed. Disconnected.'; - ret['status'] = 0; - ret['ret'] = 'locked'; - res.json(ret); - }); + } - }); - } - else - { - ret['status'] = 1; - ret['msg'] = 'Lock is already locked'; - res.json(ret); - } + res.json(ret); + lock.disconnect(); +}); + lock.connect().then(function(){ - }); + var status = execLock(); - }); + }).finally(function(){ + console.log('Finally'); + }); }); @@ -105,19 +112,28 @@ app.get('/api/status', function(req, res){ return; } - lock.connect().then(function(){ + var execStatus = async(function() { + var status = await(lock.status()); + ret['ret'] = status; + ret['status'] = 0; + ret['msg'] = 'Command completed.'; - lock.status().then(function(status){ - ret['ret'] = status; - ret['status'] = 0; - lock.disconnect().then(function() { - ret['msg'] = 'Command completed. Disconnected.'; - res.json(ret); - }); - }); + console.log('Disconnecting'); + lock.disconnect(); + console.log('Returning'); + res.json(ret); }); + + lock.connect().then(function() { + var status = execStatus(); + + }).finally(function() { + console.log('Finally'); + }); + + });