-
Notifications
You must be signed in to change notification settings - Fork 0
/
starter.py
305 lines (276 loc) · 8.76 KB
/
starter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import threading, time, json
import RPi.GPIO as GPIO
from flask import Flask, request, Response
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
app = Flask(__name__)
DOME = 23
BUZZER = 24
GPIO.setmode(GPIO.BCM)
GPIO.setup(DOME, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #dome button to GPIO23
GPIO.setup(BUZZER, GPIO.OUT) # to GPIO24
buzzerLock = threading.Lock()
stateLock = threading.Lock()
startTime = 0.0
endTime = 0.0
STATE = 0
WAIT = 1
START = 2
STARTED = 3
DONE = 4
RESET = 5
STOP = 6
@app.route('/health', methods=['GET'])
def health():
print "checking health status of system"
dStatus = 404
fStatus = 404
try:
d = requests.Session()
retries = Retry(
total=5,
read=5,
connect=5,
backoff_factor=0.3,
status_forcelist=[ 502, 503, 504 ])
d.mount('http://', HTTPAdapter(max_retries=retries))
dResp = d.get("http://noisemakerpi/health")
dStatus = dResp.status_code
except:
print "Error connecting to noisemakerpi"
try:
f = requests.Session()
retries = Retry(
total=5,
read=5,
connect=5,
backoff_factor=0.3,
status_forcelist=[ 502, 503, 504 ])
f.mount('http://', HTTPAdapter(max_retries=retries))
fResp = f.get("http://finisherbuttonpi/health")
fStatus = fResp.status_code
except:
print "Error connecting to finisherbuttonpi"
if fResp.status_code == requests.codes.ok and dResp.status_code == requests.codes.ok:
return Response(status=200)
data = {
"finish_status": fStatus,
"display_status": dStatus,
}
js = json.dumps(data)
return Response(js, status=200, mimetype='application/json')
@app.route('/stop', methods=['POST'])
def stop():
global STATE, stateLock
print "STOP signal received"
try:
data = json.loads(request.data)
with stateLock:
endTime = data['stop_time']
STATE = DONE
except:
endTime = time.time()
STATE = DONE
print "error getting stop time"
return Response(status=200)
def resetSystem():
dStatus = 404
fStatus = 404
try:
d = requests.Session()
retries = Retry(
total=5,
read=5,
connect=5,
backoff_factor=0.3,
status_forcelist=[ 502, 503, 504 ])
d.mount('http://', HTTPAdapter(max_retries=retries))
dResp = d.post("http://noisemakerpi/reset")
dStatus = dResp.status_code
except:
print "Error connecting to noisemakerpi"
try:
f = requests.Session()
retries = Retry(
total=5,
read=5,
connect=5,
backoff_factor=0.3,
status_forcelist=[ 502, 503, 504 ])
f.mount('http://', HTTPAdapter(max_retries=retries))
fResp = f.post("http://finisherbuttonpi/reset")
fStatus = fResp.status_code
except:
print "Error connecting to finisherbuttonpi"
if fResp.status_code == requests.codes.ok and dResp.status_code == requests.codes.ok:
print "WARNING system in a bad state"
def playBuzzer():
global buzzerLock
with buzzerLock:
i = 0
while i < 3:
#turn buzzer on
GPIO.output(BUZZER, True)
time.sleep(.5)
#turn off buzzer
GPIO.output(BUZZER, False)
time.sleep(.5)
i+=1
GPIO.output(BUZZER, True)
time.sleep(3)
GPIO.output(BUZZER, False)
def forceStop():
global endTime
payload = {"stop_time": endTime}
headers = {'content-type': 'application/json'}
dStatus = 404
fStatus = 404
try:
d = requests.Session()
retries = Retry(
total=5,
read=5,
connect=5,
backoff_factor=0.3,
status_forcelist=[ 502, 503, 504 ])
d.mount('http://', HTTPAdapter(max_retries=retries))
dResp = d.post("http://noisemakerpi/stop", data=json.dumps(payload), headers=headers)
dStatus = dResp.status_code
except:
print "Error connecting to noisemakerpi"
try:
f = requests.Session()
retries = Retry(
total=5,
read=5,
connect=5,
backoff_factor=0.3,
status_forcelist=[ 502, 503, 504 ])
f.mount('http://', HTTPAdapter(max_retries=retries))
fResp = f.post("http://finisherbuttonpi/stop", data=json.dumps(payload), headers=headers)
fStatus = fResp.status_code
except:
print "Error connecting to finisherbuttonpi"
if fResp.status_code == requests.codes.ok and dResp.status_code == requests.codes.ok:
print "WARNING system in a bad state"
def notifyStop():
global startTime
payload = {"start_time": startTime}
headers = {'content-type': 'application/json'}
try:
s = requests.Session()
retries = Retry(
total=5,
read=5,
connect=5,
backoff_factor=0.3,
status_forcelist=[ 502, 503, 504 ])
s.mount('http://', HTTPAdapter(max_retries=retries))
resp = s.post("http://stopbuttonpi/start", data=json.dumps(payload), headers=headers)
if response.status_code != requests.codes.ok:
print "Error notifying stop"
except:
print "Error exception occurred notifying display"
def notifyDisplay():
global startTime
payload = {"start_time": startTime}
headers = {'content-type': 'application/json'}
try:
s = requests.Session()
retries = Retry(
total=5,
read=5,
connect=5,
backoff_factor=0.3,
status_forcelist=[ 502, 503, 504 ])
s.mount('http://', HTTPAdapter(max_retries=retries))
resp = s.post("http://noisemakerpi/start", data=json.dumps(payload), headers=headers)
if response.status_code != requests.codes.ok:
print "Error notifying display"
except:
print "Error exception occurred notifying display"
def waitForStart():
global STATE, startTime, stateLock
currState = -1
while True:
with stateLock:
currState = STATE
if currState == WAIT :
time.sleep(0.1)
elif currState == START :
d = threading.Thread(target=notifyDisplay)
d.daemon = True
f = threading.Thread(target=notifyStop)
f.daemon = True
b = threading.Thread(target=playBuzzer)
b.daemon = True
b.start()
# wait for first 3 tones of buzzer to play
time.sleep(3)
startTime = time.time()
f.start()
d.start()
with stateLock:
STATE = STARTED
elif currState == STARTED:
time.sleep(0.1)
elif currState == RESET:
#call reset on finisher and noise
resetSystem()
with stateLock:
STATE = DONE
elif currState == STOP:
forceStop()
with stateLock:
STATE = DONE
elif currState == DONE :
print "Finished one iteration"
with stateLock:
STATE = WAIT
else:
print "WARNING unknown state: " + currState
def waitForButtonPress():
global stateLock, STATE, endTime
prevState = True
button_state = True
buttonPushTime = 0.0
while True:
prevState = button_state
button_state = GPIO.input(DOME)
if button_state == False and prevState == True:
# button pressed
buttonPushTime = time.time()
print "START button pressed"
elif button_state == True and prevState == False:
# button released
print "START button released"
if STATE == WAIT:
with stateLock:
STATE = START
elif STATE == STARTED:
if (time.time() - buttonPushTime) >= 4:
# reset state
print "RESET button press detected"
with stateLock:
STATE = RESET
else :
with stateLock:
STATE = STOP
endTime = time.time()
time.sleep(.5)
if __name__ == '__main__':
STATE = WAIT
try:
s = threading.Thread(target=waitForStart)
s.daemon = True
s.start()
b = threading.Thread(target=waitForButtonPress)
b.daemon = True
b.start()
#start flask server
app.run(port=9080, debug=False)
except (KeyboardInterrupt, SystemExit):
print "caught keyboard interrupt"
finally:
GPIO.cleanup()