Skip to content
Snippets Groups Projects
Commit 53d8e524 authored by Robson's avatar Robson
Browse files

Fixing callback

parent 5e49ac5b
Branches
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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",
......
......@@ -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');
});
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment