diff --git a/lib/lock.js b/lib/lock.js index 6fb7050e09267064b760d0cdaff1500d7369ff25..21c53788f26c9b5ce139df246225498100da2c2f 100644 --- a/lib/lock.js +++ b/lib/lock.js @@ -106,18 +106,62 @@ Lock.prototype.connect = function() { }); }; -Lock.prototype.lock = function() { +Lock.prototype.forceLock = function() { debug('locking...'); + console.log('chamou lock'); var cmd = this._session.buildCommand(0x0b); return this._session.execute(cmd); + }; -Lock.prototype.unlock = function() { +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) { + if (status == 'unlocked') + return this.forceLock(); + }.bind(this)); +}; + +Lock.prototype.unlock = function() { + return this.status(true).then(function(status) { + if (status == 'locked') + return this.forceUnlock(); + }.bind(this)); +}; + +Lock.prototype.status = function() { + + debug('status...'); + + var cmd = new Buffer(0x12); + cmd.fill(0x00); + cmd.writeUInt8(0xee, 0x00); // magic + cmd.writeUInt8(0x02, 0x01); + cmd.writeUInt8(0x02, 0x04); + cmd.writeUInt8(0x02, 0x10); + + return this._session.execute(cmd).then(function(response) { + var status = response.readUInt8(0x08); + + var strstatus = 'unknown'; + if (status == 0x03) + strstatus = 'unlocked'; + else if (status == 0x05) + strstatus = 'locked'; + + return strstatus; + + }.bind(this)); + +}; + Lock.prototype.disconnect = function() { debug('disconnecting...'); diff --git a/server.js b/server.js index 4cc0068504c4325124ec0d892e0dbfe35fc1a435..0a7839b6c17d6f9064160d816828455dd31a111f 100755 --- a/server.js +++ b/server.js @@ -13,6 +13,8 @@ var port = config.port || 3000; var app = express(); app.use(morgan(DEBUG ? 'dev' : 'combined')); +var ret = {'status': -1, 'ret': '', 'msg': ''}; + app.get('/api/unlock', function(req, res) { var lock = app.get('lock'); if (!lock) { @@ -20,15 +22,105 @@ app.get('/api/unlock', function(req, res) { return; } - lock.connect().then(function() { - return lock.unlock(); - }).finally(function() { - lock.disconnect(); - // TODO: report errors - res.sendStatus(204); - }); + lock.connect().then(function(){ + + lock.status().then(function(status){ + + ret['ret'] = status; + + if(status == 'locked') + { + lock.unlock().then(function() { + + 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); + } + + + }); + + }); + }); + +app.get('/api/lock', function(req, res) { + var lock = app.get('lock'); + if (!lock) { + res.sendStatus(503); + return; + } + + lock.connect().then(function(){ + + lock.status().then(function(status){ + + ret['ret'] = status; + + if(status == 'unlocked') + { + lock.lock().then(function() { + + 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); + } + + + }); + + }); + +}); + + +app.get('/api/status', function(req, res){ + + var lock = app.get('lock'); + if(!lock) { + res.sendStatus(503); + return; + } + + lock.connect().then(function(){ + + lock.status().then(function(status){ + ret['ret'] = status; + ret['status'] = 0; + lock.disconnect().then(function() { + ret['msg'] = 'Command completed. Disconnected.'; + res.json(ret); + }); + }); + + }); + +}); + + augustctl.scan(config.lockUuid).then(function(peripheral) { var lock = new augustctl.Lock( peripheral,