Skip to content

Commit

Permalink
Make status page return useful information
Browse files Browse the repository at this point in the history
The flask app is now multithreaded. Only one request will handle merch
at a time through /vend, and the /status endpoint will return 200 if the
machine is available to vend, and 503 if it is currently vending
  • Loading branch information
abrandemuehl committed Apr 8, 2017
1 parent c624e4d commit 944ed3f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
23 changes: 19 additions & 4 deletions machine_controller/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def vend():


statuses = []
merch.acquire()
for i, item in enumerate(items):
try:
merch.vend(item[0], int(item[1]))
Expand All @@ -62,11 +63,25 @@ def vend():
# goes wrong, we still need to let the client know instead of
# throwing a 500
statuses.append({'error': str(e), 'location': item})
merch.release()

return jsonify(transaction_id=transaction_id, items=statuses)

if __name__ == '__main__':
# Make sure flask runs in a single thread. Otherwise concurrent requests
# may cause problems with vending
app.run(debug=True, host='0.0.0.0', threaded=False)
@app.route('/status', methods=['GET'])
def status():
if request.headers.get('Authorization', '') != token_value:
abort(401)

ready = merch.inUse()

if(ready):
# 200 to indicate success
return ('', 200)

else:
return ('', 503)



if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', threaded=True)
14 changes: 14 additions & 0 deletions machine_controller/vend.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
# THE SOFTWARE.
import RPi.GPIO as GPIO
import time
from threading import Condition, Lock


class Merch:
Expand All @@ -56,9 +57,22 @@ def __init__(self, debug=False):
self.__low()
self.__commit()

self.lock = Lock()

def __del__(self):
self.__cleanup()

def acquire(self):
self.lock.acquire()

def release(self):
self.lock.release()

def inUse(self):
# Trylock
return not self.lock.acquire(False)


def __cleanup(self):
''' Clean up all of the GPIO pins '''
GPIO.cleanup()
Expand Down

0 comments on commit 944ed3f

Please sign in to comment.