File tree Expand file tree Collapse file tree 2 files changed +33
-4
lines changed Expand file tree Collapse file tree 2 files changed +33
-4
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ def vend():
51
51
52
52
53
53
statuses = []
54
+ merch .acquire ()
54
55
for i , item in enumerate (items ):
55
56
try :
56
57
merch .vend (item [0 ], int (item [1 ]))
@@ -62,11 +63,25 @@ def vend():
62
63
# goes wrong, we still need to let the client know instead of
63
64
# throwing a 500
64
65
statuses .append ({'error' : str (e ), 'location' : item })
66
+ merch .release ()
65
67
66
68
return jsonify (transaction_id = transaction_id , items = statuses )
67
69
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
+ ready = merch .inUse ()
76
+
77
+ if (ready ):
78
+ # 200 to indicate success
79
+ return ('' , 200 )
72
80
81
+ else :
82
+ return ('' , 503 )
83
+
84
+
85
+
86
+ if __name__ == '__main__' :
87
+ app .run (debug = True , host = '0.0.0.0' , threaded = True )
Original file line number Diff line number Diff line change 34
34
# THE SOFTWARE.
35
35
import RPi .GPIO as GPIO
36
36
import time
37
+ from threading import Condition , Lock
37
38
38
39
39
40
class Merch :
@@ -56,9 +57,22 @@ def __init__(self, debug=False):
56
57
self .__low ()
57
58
self .__commit ()
58
59
60
+ self .lock = Lock ()
61
+
59
62
def __del__ (self ):
60
63
self .__cleanup ()
61
64
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 not self .lock .acquire (False )
74
+
75
+
62
76
def __cleanup (self ):
63
77
''' Clean up all of the GPIO pins '''
64
78
GPIO .cleanup ()
You can’t perform that action at this time.
0 commit comments