Skip to content
Snippets Groups Projects
Verified Commit 1b6d0f99 authored by Erick Hitter's avatar Erick Hitter
Browse files

Remove timeout handling and replace with check that lock is connected, which resolves most errors.

Also, lock/unlock requests now attempt to override any existing connections.
parent 32fda0f4
Branches
No related tags found
No related merge requests found
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
"apicache": "*", "apicache": "*",
"asyncawait": "^0.7.4", "asyncawait": "^0.7.4",
"bluebird": "^2.9.34", "bluebird": "^2.9.34",
"connect-timeout": "*",
"debug": "^2.2.0", "debug": "^2.2.0",
"express": "^4.13.3", "express": "^4.13.3",
"morgan": "^1.6.1", "morgan": "^1.6.1",
......
...@@ -11,7 +11,6 @@ var await = require( 'asyncawait/await' ); ...@@ -11,7 +11,6 @@ var await = require( 'asyncawait/await' );
var async = require( 'asyncawait/async' ); var async = require( 'asyncawait/async' );
var apicache = require( 'apicache' ).options( { defaultDuration: 15000 } ); var apicache = require( 'apicache' ).options( { defaultDuration: 15000 } );
var cache = apicache.middleware; var cache = apicache.middleware;
var timeout = require( 'connect-timeout' );
var request = require( 'request' ); var request = require( 'request' );
var config = require( process.env.AUGUSTCTL_CONFIG || './config.json' ); var config = require( process.env.AUGUSTCTL_CONFIG || './config.json' );
...@@ -63,35 +62,24 @@ function statusStringtoInt( status ) { ...@@ -63,35 +62,24 @@ function statusStringtoInt( status ) {
return statusInt; return statusInt;
} }
// Clear cached routes
function clearCaches( lockName ) {
apicache.clear( '/api/status/' + lockName );
apicache.clear( '/api/lock/' + lockName );
apicache.clear( '/api/unlock/' + lockName );
return true;
}
// Middleware to handle timeout status checks
function haltOnTimedout( req, res, next ) {
if ( req.timedout ) {
request( 'http://' + address + ':' + port + '/api/disconnect/' + req.params.lock_name );
} else {
next();
}
}
/** /**
* ROUTES * ROUTES
*/ */
// Endpoint to check lock status // Endpoint to check lock status
app.get( '/api/status/:lock_name', timeout( '5 seconds' ), cache( '5 seconds' ), haltOnTimedout, function( req, res ) { app.get( '/api/status/:lock_name', cache( '5 seconds' ), function( req, res, next ) {
var lockName = req.params.lock_name; var lockName = req.params.lock_name;
// Parse allowed request arguments // Parse allowed request arguments
var lock = getLockInstance( lockName, res ); var lock = getLockInstance( lockName, res );
if ( ! lock ) { if ( ! lock ) {
res.sendStatus( 400 );
return;
}
// Check if lock is already connected, and bail if it is since two devices can't connect at once
if ( lock.isConnected() ) {
res.sendStatus( 503 );
return; return;
} }
...@@ -110,21 +98,13 @@ app.get( '/api/status/:lock_name', timeout( '5 seconds' ), cache( '5 seconds' ), ...@@ -110,21 +98,13 @@ app.get( '/api/status/:lock_name', timeout( '5 seconds' ), cache( '5 seconds' ),
// Perform requested action // Perform requested action
lock.connect().then( actionFunction ).catch( function( err ) { lock.connect().then( actionFunction ).catch( function( err ) {
console.error( err ); console.error( err );
lock.disconnect();
try { res.sendStatus( 500 );
request( 'http://' + address + ':' + port + '/api/disconnect/' + lockName, function() {
request( 'http://' + address + ':' + port + '/api/status/' + lockName );
} );
} catch ( e ) {
// We don't care about request results
}
res.sendStatus( 503 );
} ); } );
} ); } );
// Endpoint to change lock state // Endpoint to change lock state
app.get( '/api/:lock_action(lock|unlock)/:lock_name', timeout( '5 seconds' ), cache( '3 seconds' ), haltOnTimedout, function( req, res ) { app.get( '/api/:lock_action(lock|unlock)/:lock_name', function( req, res, next ) {
// Parse allowed request arguments // Parse allowed request arguments
var action = req.params.lock_action, var action = req.params.lock_action,
allowedActions = [ 'unlock', 'lock' ]; allowedActions = [ 'unlock', 'lock' ];
...@@ -136,9 +116,15 @@ app.get( '/api/:lock_action(lock|unlock)/:lock_name', timeout( '5 seconds' ), ca ...@@ -136,9 +116,15 @@ app.get( '/api/:lock_action(lock|unlock)/:lock_name', timeout( '5 seconds' ), ca
var lockName = req.params.lock_name, var lockName = req.params.lock_name,
lock = getLockInstance( lockName, res ); lock = getLockInstance( lockName, res );
if ( ! lock ) { if ( ! lock ) {
res.sendStatus( 400 );
return; return;
} }
// Check if lock is already connected, and disconnect so we can force the action
if ( lock.isConnected() ) {
lock.disconnect();
}
// Suspendable functions to interact with lock based on requested action // Suspendable functions to interact with lock based on requested action
var actionFunction = async( function() { var actionFunction = async( function() {
var status = await( lock.status() ); var status = await( lock.status() );
...@@ -162,36 +148,40 @@ app.get( '/api/:lock_action(lock|unlock)/:lock_name', timeout( '5 seconds' ), ca ...@@ -162,36 +148,40 @@ app.get( '/api/:lock_action(lock|unlock)/:lock_name', timeout( '5 seconds' ), ca
} }
lock.disconnect(); lock.disconnect();
clearCaches( lockName ); apicache.clear( '/api/status/' + lockName );
res.json( ret ); res.json( ret );
} ); } );
// Perform requested action // Perform requested action
lock.connect().then( actionFunction ).catch( function( err ) { lock.connect().then( actionFunction ).catch( function( err ) {
console.error( err ); console.error( err );
lock.disconnect();
try { try {
request( 'http://' + address + ':' + port + '/api/disconnect/' + lockName, function() { request( 'http://' + address + ':' + port + '/api/' + action + '/' + lockName );
request( 'http://' + address + ':' + port + '/api/status/' + lockName ); } catch ( e ) {}
} );
} catch ( e ) {
// We don't care about request results
}
res.sendStatus( 503 ); res.sendStatus( 500 );
} ); } );
} ); } );
// Endpoint to disconnect a lock's BLE connections // Endpoint to disconnect a lock's BLE connections
app.get( '/api/disconnect/:lock_name', function( req, res ) { app.get( '/api/disconnect/:lock_name', function( req, res, next ) {
// Parse allowed request arguments // Parse allowed request arguments
var lock = getLockInstance( req.params.lock_name, res ); var lock = getLockInstance( req.params.lock_name, res );
if ( ! lock ) { if ( ! lock ) {
res.sendStatus( 400 );
return;
}
// Check if lock is already connected, and bail if it's already disconnected
if ( ! lock.isConnected() ) {
res.sendStatus( 503 );
return; return;
} }
lock.disconnect(); lock.disconnect();
clearCaches( req.params.lock_name ); apicache.clear( '/api/status/' + req.params.lock_name );
res.sendStatus( 204 ); res.sendStatus( 204 );
} ); } );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment