-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreaderd.py
More file actions
executable file
·237 lines (209 loc) · 6.94 KB
/
readerd.py
File metadata and controls
executable file
·237 lines (209 loc) · 6.94 KB
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
#!/usr/bin/env python3
# readerd: Reads RFID cards at machine terminal as a login method for users.
# Flask enpoint is provided for remote login, troubleshooting and simulating jobs
# ProcBridge client talks to the Java UI
# - This uses the 24v air filter signal to determine jobtime.
# - We're adding 2 min of extra air filter time after the job is over.
# - Use another relay in series with 24v air filter control to toggle
# air filter/assist.
import signal
import time
import MFRC522
import RPi.GPIO as GPIO
import sys
import os
import datetime
from flask import Flask, request, url_for, abort
import _thread
from procbridge import procbridge
import spidev
spi = spidev.SpiDev()
spi.open(1, 2)
spi.max_speed_hz = 100000
spi.mode = 0b00
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
air_filter_sense = 16
air_filter_power = 15
proc_host = '127.0.0.1'
client_port = 8877
client = procbridge.ProcBridge(proc_host, client_port)
# makes Flask stfu
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
# quiets GPIO warnings
GPIO.setwarnings(False)
card_serial = ""
card_serialold = ""
count_error = 0
alarm_state = 0
previous_sense_state = 0
def setup():
print('GPIO setup')
GPIO.setmode(GPIO.BOARD)
GPIO.setup(air_filter_sense, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(air_filter_power, GPIO.OUT)
def teardown():
global run
run = False
rdr.cleanup()
print('GPIO teardown')
GPIO.cleanup()
def two_min_air_filter():
GPIO.output(air_filter_power,GPIO.HIGH)
time.sleep(120)
GPIO.output(air_filter_power,GPIO.LOW)
def flip_filter_pin(state):
GPIO.output(air_filter_sense,GPIO.state)
def turbine_state():
global alarm_state
x = 0
total_amps = 0
while x <= 100:
resp = spi.xfer2([0x00, 0x00])
w = resp[0]
w <<= 8
w |= resp[1]
refV = 3.3
lsb = refV/4096
mV= (w-2048)*lsb*1000
amps = abs( mV/66 )
#print('amps is: %s' % amps)
total_amps = amps + total_amps
#print('total_amps is: %s' % total_amps)
#print(x)
x = x + 1
# time.sleep(1)
avg = ( total_amps / 100)
#print('average is %s' % avg)
if avg > .1:
#print("we're on.")
return 1
else:
#print("we're off.")
alarm_state = 1
return 0
def read_rfid_gpio():
global alarm_state
global previous_sense_state
while 1:
(status,TagType,CardTypeRec) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
if status == MIFAREReader.MI_OK:
cardTypeNo = CardTypeRec[1]*256+CardTypeRec[0]
(status,uid,uidData) = MIFAREReader.MFRC522_Anticoll()
if status == MIFAREReader.MI_OK:
count_error = 0
print(uidData)
card_serial = MIFAREReader.list2HexStr(uidData)
card_serial = str(int(card_serial,16))
print(card_serial)
# wait one sec. befor next read will be started
time.sleep(1)
if len(card_serial) == 10 or 9 or 8:
print('{"uuid":"%s"}' % card_serial)
print(client.request('echo', {"uuid":card_serial}))
else:
print("not 10 or 8 length")
#read filter sense pin (inverted because of pullup) and current
filter_pin_state = int(not GPIO.input(air_filter_sense))
# print(filter_pin_state)
# if we're not in an alarm_state during a job, take a reading and evaluate.
if filter_pin_state == 1 and alarm_state != 1:
tstate = turbine_state()
print (' turbine state is %s ' % tstate)
if tstate == 0:
#print('first try')
# let's give the turbine a second to spin up.
time.sleep(1)
# check again just to be sure
tstate = turbine_state()
if tstate == 0:
#print('second try')
response = client.request('echo', {"filter_alarm":"1"})
# if we're sure the UI got the message, reset alarm_state and filter sense pin
if response['filter_alarm'] == '1':
GPIO.setup(air_filter_sense, GPIO.IN, GPIO.PUD_UP)
alarm_state = 0
if previous_sense_state != filter_pin_state and tstate is 1:
print(client.request('echo', {"job":str(filter_pin_state)}))
previous_sense_state = filter_pin_state
# add extra filter time
if previous_sense_state != filter_pin_state and filter_pin_state == 0:
_thread.start_new_thread(two_min_air_filter,())
# wait a bit before next read will be started
time.sleep(.1)
# Initialize the Flask application
app = Flask(__name__)
#app.config['DEBUG'] = True
app.config.update(
JSONIFY_PRETTYPRINT_REGULAR=False
)
mylogfile = ('/var/log/%s.access.log' % os.path.basename(__file__))
log_level = 5
def debug_message(current_log_level, message_level, message):
timestamp = time.strftime('%Y%m%d_%H:%M:%S')
if message_level <= current_log_level:
print('%s - %s' % (timestamp, message))
logfile = open(mylogfile, "a");
logfile.write("%s - %s" % (timestamp, message))
logfile.write("\n")
logfile.close()
@app.route('/client', methods = ['POST'])
def accept_card_uid():
global alarm_state
if request.method == 'POST':
#print("this is a POST")
try:
card_serial = (request.form['uuid'])
if len(card_serial) == 10 or 9 or 8:
print(client.request('echo', {"uuid":card_serial}))
return str('ok')
else:
print("not 10 or 8 length")
except:
pass
try:
job_state = (request.form['job'])
if job_state == "1" or "0":
print(client.request('echo', {"job":job_state}))
if job_state == "1":
print('job state is 1')
GPIO.setup(air_filter_sense, GPIO.IN, GPIO.PUD_DOWN)
else:
print('job state is 0')
GPIO.setup(air_filter_sense, GPIO.IN, GPIO.PUD_UP)
# for quick and dirty testing of interlockd, uncomment and change port
#print(client.request('echo', {"pin":job_state}))
return str('ok')
except:
pass
try:
in_service = (request.form['in_service'])
if in_service == "1" or "0":
print(client.request('echo', {"in_service":in_service}))
return str('ok')
except:
pass
try:
filter_alarm = (request.form['filter_alarm'])
if filter_alarm == "1" or "0":
print(client.request('echo', {"filter_alarm":filter_alarm}))
return str('ok')
except:
pass
_thread.start_new_thread(read_rfid_gpio,())
if __name__ == '__main__':
setup()
app.run(
host="0.0.0.0",
port=int("7000"),
)
try:
for line in sys.stdin:
if line.strip() == 'exit':
break
except KeyboardInterrupt:
pass
teardown()
print('goodbye')