Skip to content

Commit

Permalink
Changed default REST Bluetooth timeout to 5 minutes and added REST
Browse files Browse the repository at this point in the history
endpoint to modify.
  • Loading branch information
erikcw committed May 17, 2016
1 parent 8657780 commit 735871e
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion examples/rest/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class RESTAnovaController(AnovaController):
NOTE: Only a single BlueTooth connection can be open to the Anova at a time.
"""

TIMEOUT = 1 * 60 # Keep the connection open for this many seconds.
TIMEOUT = 5 * 60 # Keep the connection open for this many seconds.
TIMEOUT_HEARTBEAT = 20

def __init__(self, mac_address, connect=True, logger=None):
Expand All @@ -42,7 +42,17 @@ def __init__(self, mac_address, connect=True, logger=None):
self.logger = logging.getLogger()
super(RESTAnovaController, self).__init__(mac_address, connect=connect)

def set_timeout(self, timeout):
"""
Adjust the timeout period (in seconds).
"""
self.TIMEOUT = timeout

def timeout(self, seconds=None):
"""
Determines whether the Bluetooth connection should be timed out
based on the timestamp of the last exectuted command.
"""
if not seconds:
seconds = self.TIMEOUT
timeout_at = self.last_command_at + datetime.timedelta(seconds=seconds)
Expand Down Expand Up @@ -183,6 +193,19 @@ def stop_timer():
output = {"timer_status": app.anova_controller.stop_timer()}
return jsonify(output)

@app.route('/set-timeout', methods=["POST"])
def set_timeout():
"""
Adjust the Bluetooth connection timeout length.
"""
try:
seconds = int(request.get_json()['timeout_seconds'])
except (KeyError, TypeError):
abort(400)
app.anova_controller.set_timeout(seconds)
output = {"timeout_seconds": seconds,}
return jsonify(output)


class AuthMiddleware(object):
"""
Expand Down

0 comments on commit 735871e

Please sign in to comment.