Fire HTTP requests under certain circumstances, self-healing the API

parent 3b28a516
{
"name": "augustctl",
"version": "0.1.1",
"version": "0.2.0",
"description": "Controller for August Smart Lock",
"author": "Dan Walters <dan@walters.io>",
"author": "Erick Hitter <contact@ethitter.com>",
"license": "MIT",
"main": "index.js",
"bin": {
......@@ -15,17 +15,18 @@
"bluetooth"
],
"dependencies": {
"apicache": "*",
"asyncawait": "^0.7.4",
"bluebird": "^2.9.34",
"connect-timeout": "*",
"debug": "^2.2.0",
"express": "^4.13.3",
"morgan": "^1.6.1",
"noble": "^1.1.0",
"asyncawait": "^0.7.4",
"apicache": "*",
"connect-timeout": "*"
"request": "*"
},
"repository": {
"type": "git",
"url": "http://github.com/sretlawd/augustctl/augustctl.git"
"url": "https://github.com/ethitter/augustctl/augustctl.git"
}
}
......@@ -12,6 +12,7 @@ var async = require( 'asyncawait/async' );
var apicache = require( 'apicache' ).options( { defaultDuration: 15000 } );
var cache = apicache.middleware;
var timeout = require( 'connect-timeout' );
var request = require( 'request' );
var config = require( process.env.AUGUSTCTL_CONFIG || './config.json' );
var serverConfig = require( process.env.AUGUSTCTL_SERVER_CONFIG || './server-config.json' );
......@@ -74,7 +75,7 @@ function clearCaches( lockName ) {
// Middleware to handle timeout status checks
function haltOnTimedout( req, res, next ) {
if ( req.timedout ) {
lock.disconnect();
request( 'http://' + address + ':' + port + '/api/disconnect/' + req.params.lock_name );
} else {
next();
}
......@@ -86,8 +87,10 @@ function haltOnTimedout( req, res, next ) {
// Endpoint to check lock status
app.get( '/api/status/:lock_name', timeout( '5 seconds' ), cache( '5 seconds' ), haltOnTimedout, function( req, res ) {
var lockName = req.params.lock_name;
// Parse allowed request arguments
var lock = getLockInstance( req.params.lock_name, res );
var lock = getLockInstance( lockName, res );
if ( ! lock ) {
return;
}
......@@ -107,6 +110,15 @@ app.get( '/api/status/:lock_name', timeout( '5 seconds' ), cache( '5 seconds' ),
// Perform requested action
lock.connect().then( actionFunction ).catch( function( err ) {
console.error( err );
try {
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 );
} );
} );
......@@ -157,6 +169,15 @@ app.get( '/api/:lock_action(lock|unlock)/:lock_name', timeout( '5 seconds' ), ca
// Perform requested action
lock.connect().then( actionFunction ).catch( function( err ) {
console.error( err );
try {
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 );
} );
} );
......
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