/
iserial.py
114 lines (93 loc) · 3.12 KB
/
iserial.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
# Author:
# Description: iSerial.py is a serial listener that reads information
# from the LinkUSB iButton reader from iButtonLink.
#
import serial
import time, sys, re
from optparse import OptionParser
import util.logger as Log
logger = Log.Logging('doorberrypi.db')
def main():
#configure serial interfaces and handle prompts
parser = OptionParser(usage="%prog -p SERIALPORT", version="%prog 0.1")
parser.set_defaults(
port = "/dev/ttyUSB0",
baud = 9600,
)
parser.add_option("-p", dest="port", action="store",
help='Serial interface (Default is /dev/ttyUSB0)')
parser.add_option("-b", dest="baud", action="store",
help='Set the baud rate (Default is 9600)')
(options, args) = parser.parse_args()
#Validate input here
#if options.port:
#check that port exists, is unlocked, and accessible to the user
#if not options.baud == 9600 or 19200 or 38400 or 56600:
# print("Invalid baud rate")
# exit()
#TODO turn this into it's own process
print("Connecting to %s at %s" % (options.port, options.baud))
try:
ser = serial.Serial(options.port, options.baud, timeout=.5)
if ser.isOpen(): print("Connection successful...")
if test(ser): print("Communication test successful...")
else: sys.exit()
ser.readlines() # clear the buffer
ser.write('$') # scanning mode
except:
print("Could not connect to %s" % options.port)
sys.exit()
buffer = []
while True:
logger.log("iButton scanning started", "N/A")
try: #print(ser.inWaiting())
if ser.inWaiting() > 39:
buffer = ser.readlines()
serialread = buffer[:2] #take only the last 2 items
ibutton = ivalidate(serialread)
if ibutton: #check to make sure it is an ibutton
conn = sqlite3.connect("dorrberrypi.db")
name, status = conn.cursor().execute('SELECT fullname,status from users WHERE ibutton=?',ibutton)
if status == "ACTIVE":
##R
# CODE TO RELEASE THE DOOR HERE
###
print("Member active. Door opened")
logger.Log("Door opened", ibutton)
else:
logger.Log("Inactive member auth attempt", ibutton)
else:
print("Invalid iButton read. Someone is fucking with the reader")
else:
time.sleep(.5) ##TODO make more elegant
##TODO search for properly formatted ibuttons or bail
## if invalid ibuttons come in that wait
## TODO add security here :)
except:
print("Serial IO error. Exiting")
sys.exit()
ser.close()
def ivalidate(ibuttons):
#validate the input from a user
ipattern = re.compile('^!,([A-F0-9]{16})')
for i in ibuttons:
imatch = ipattern.match(i)
if imatch: return imatch.group(0)[2:] #strip the first two chars
else: return None
def test(ser):
#make sure connection is good
try:
ser.write('r') #send a reset signal
time.sleep(1) #give it a second to respond
response = ser.readlines()
if 'N\r\n' in response: #valid response
return True
else:
return False
except:
return False
def alert():
#send alerts when something goes wrong
print("Something terrible has happened.")
if __name__ == "__main__":
main()