Commit 5e49ac5b authored by Robson's avatar Robson

First working version

parent 171bdc50
......@@ -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...');
......
......@@ -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,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment