/
rfid.py
112 lines (94 loc) · 2.58 KB
/
rfid.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
#!/usr/bin/env/python
import serial
import sys
from struct import unpack
import time
import pgdb
from automaton.lib import exceptions, logger, settings_loader
from automaton.lib.client_wrapper_thrift import ClientWrapper
def do_valid_tag(db, code):
cursor = db.cursor()
query = ('SELECT "User Data"."First Name", "User Data"."Last Name" FROM '
'"Users"."User Data" WHERE "User Data"."Username" '
'= \'{0}\';'.format(code))
try:
cursor.execute(query)
except Exception as e:
print e
cursor.close()
db.rollback()
return None
name = cursor.fetchone()
cursor.close()
if name == None:
return None
name = " ".join(name)
global client
client.execute("say", "Welcome back " + name)
def checkid(db, idnum):
cursor = db.cursor()
query = ('SELECT "Registered Keys"."Username" FROM "RFID"."Registered Keys" '
'WHERE "Registered Keys"."ID" = \'{0}\''.format(idnum))
try:
cursor.execute(query)
except Exception as e:
print e
cursor.close()
db.rollback()
return None
if cursor.rowcount > 0:
data = cursor.fetchone()[0]
else:
data = None
cursor.close()
return data
try:
s = serial.Serial("/dev/ttyUSB0", 2400)
except serial.serialutil.SerialException:
s = serial.Serial("/dev/ttyUSB1", 2400)
op = {"DBHOST": "localhost", "DBUSER": "RFID", "DBPASS": ""}
op.update(settings_loader.load_app_settings(sys.argv[0]))
if op["DBPASS"] == "":
logger.log("Error: no db password provided")
sys.exit()
try:
db = pgdb.connect(user=op["DBUSER"], password=op["DBPASS"],
host=op["DBHOST"], database="Automaton")
except Exception as e:
print "Error connecting to database:", e
sys.exit()
client = ClientWrapper.ClientWrapper(op["THRIFT_SERVER"])
client.open()
try:
client.registerPlugin("say")
except exceptions.PluginNotLoadedException:
print "PluginNotLoaded Exception"
except exceptions.ServiceNotRegisteredException:
print "Service not registered"
# Enable the reader
s.setRTS(1)
while True:
try:
if unpack("B", s.read()) == (10, ):
idnum = ''
while len(idnum) < 10:
char = s.read()
val = unpack("B", char)
if val == (10, ) or val == (13, ):
break
idnum += char
if len(idnum) == 10:
username = checkid(db, idnum)
if username != None:
logger.log("Valid tag: " + idnum)
do_valid_tag(db, username)
else:
logger.log("Invalid tag: " + idnum)
s.setRTS(0)
time.sleep(2)
s.setRTS(1)
except Exception as e:
print e
break
#client.close()
db.close()