File tree Expand file tree Collapse file tree 2 files changed +32
-4
lines changed
Expand file tree Collapse file tree 2 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ def vend():
5151
5252
5353 statuses = []
54+ merch .acquire ()
5455 for i , item in enumerate (items ):
5556 try :
5657 merch .vend (item [0 ], int (item [1 ]))
@@ -62,11 +63,24 @@ def vend():
6263 # goes wrong, we still need to let the client know instead of
6364 # throwing a 500
6465 statuses .append ({'error' : str (e ), 'location' : item })
66+ merch .release ()
6567
6668 return jsonify (transaction_id = transaction_id , items = statuses )
6769
68- if __name__ == '__main__' :
69- # Make sure flask runs in a single thread. Otherwise concurrent requests
70- # may cause problems with vending
71- app .run (debug = True , host = '0.0.0.0' , threaded = False )
70+ @app .route ('/status' , methods = ['GET' ])
71+ def status ():
72+ if request .headers .get ('Authorization' , '' ) != token_value :
73+ abort (401 )
74+
75+ notready = merch .inUse ()
7276
77+ if (notready ):
78+ return ('' , 503 )
79+ else :
80+ # 200 to indicate success
81+ return ('' , 200 )
82+
83+
84+
85+ if __name__ == '__main__' :
86+ app .run (debug = True , host = '0.0.0.0' , threaded = True )
Original file line number Diff line number Diff line change 3434# THE SOFTWARE.
3535import RPi .GPIO as GPIO
3636import time
37+ from threading import Condition , Lock
3738
3839
3940class Merch :
@@ -56,9 +57,22 @@ def __init__(self, debug=False):
5657 self .__low ()
5758 self .__commit ()
5859
60+ self .lock = Lock ()
61+
5962 def __del__ (self ):
6063 self .__cleanup ()
6164
65+ def acquire (self ):
66+ self .lock .acquire ()
67+
68+ def release (self ):
69+ self .lock .release ()
70+
71+ def inUse (self ):
72+ # Trylock
73+ return self .lock .locked ()
74+
75+
6276 def __cleanup (self ):
6377 ''' Clean up all of the GPIO pins '''
6478 GPIO .cleanup ()
You can’t perform that action at this time.
0 commit comments