-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinisher.py
190 lines (167 loc) · 5.01 KB
/
finisher.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
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()
STATE = 0
WAIT = 1
STARTED = 3
DONE = 4
RESET = 5
STOP = 6
startTime = 0.0
endTime = 0.0
@app.route('/health', methods=['GET'])
def health():
print "HEALTHCHECK received"
return Response(status=200)
@app.route('/start', methods=['POST'])
def start():
global STATE, stateLock
print "START signal received"
try:
data = json.loads(request.data)
with stateLock:
startTime = data['start_time']
STATE = STARTED
except:
startTime = time.time()
STATE = STARTED
print "error getting start time"
return Response(status=200)
@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 = STOP
except:
endTime = time.time()
STATE = STOP
print "error getting start time"
return Response(status=200)
@app.route('/reset', methods=['POST'])
def reset():
global STATE, stateLock
print "RESET signal received"
with stateLock:
STATE = RESET
return Response(status=200)
def notifyStart():
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://starterbuttonpi/start", data=json.dumps(payload), headers=headers)
if response.status_code != requests.codes.ok:
print "Error notifying start"
except:
print "Error exception occurred notifying start"
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 playBuzzer(lock):
with lock:
GPIO.output(BUZZER, True)
time.sleep(3)
GPIO.output(BUZZER, False)
def waitForStart():
global STATE
currState = -1
while True:
with stateLock:
currState = STATE
if currState == WAIT :
time.sleep(0.1)
elif currState == STARTED:
time.sleep(0.1)
elif currState == RESET:
with stateLock:
STATE = WAIT
elif currState == STOP:
# POST time?
with stateLock:
STATE = WAIT
elif currState == DONE :
t = threading.Thread(target=playBuzzer, args=(buzzerLock,))
t.daemon = True
t.start()
d = threading.Thread(target=notifyDisplay)
d.daemon = True
f = threading.Thread(target=notifyStart)
f.daemon = True
f.start()
d.start()
# PSOT time?
with stateLock:
STATE = WAIT
print "Finished one iteration"
else:
print "WARNING unknown state: " + str(currState)
def waitForButtonPress():
global stateLock, STATE, endTime
prevState = True
button_state = True
while True:
prevState = button_state
button_state = GPIO.input(DOME)
if button_state == False and prevState == True:
# button pressed
print "STOP button pressed"
with stateLock:
STATE = DONE
endTime = time.time()
elif button_state == True and prevState == False:
# button released
print "STOP button released"
time.sleep(.2)
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()