-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfc_logbook.py
executable file
·172 lines (154 loc) · 5.36 KB
/
nfc_logbook.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
import nxppy
import time
import sys
import signal
import pygame
from pygame.locals import *
import sqlite3
import datetime
import serial
import os
mifare = nxppy.Mifare()
device = '/dev/ttyUSB0'
ser = serial.Serial(device, 9600)
f = open("/home/pi/user_list.csv",'r')
user_hash = {}
for line in f:
user_hash[line.split(',')[0].upper()] = line.split(',')[1].strip()
f.close()
def signal_term_handler(signum = None, frame = None):
sys.stderr.write("Terminated.\n")
f.close()
sys.exit(0)
for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]:
signal.signal(sig, signal_term_handler)
screen = None;
testdb = None
try:
testdb = sqlite3.connect("/home/pi/test.db")
cur = testdb.cursor()
#cur.execute("CREATE TABLE Logbook(Id INTEGER PRIMARY KEY, Cardid TEXT, Name TEXT, Time TEXT, date TEXT, duration TEXT)")
except:
if testdb:
testdb.rollback()
sys.stderr.write("Unable to connect to database")
sys.exit(1)
#"Ininitializes a new pygame screen using the framebuffer"
# Based on "Python GUI in Linux frame buffer"
# http://www.karoltomala.com/blog/?p=679
disp_no = os.getenv("DISPLAY")
if disp_no:
print "I'm running under X display = {0}".format(disp_no)
# Check which frame buffer drivers are available
# Start with fbcon since directfb hangs with composite output
drivers = ['fbcon']
found = False
for driver in drivers:
# Make sure that SDL_VIDEODRIVER is set
if not os.getenv('SDL_VIDEODRIVER'):
os.putenv('SDL_VIDEODRIVER', driver)
try:
pygame.display.init()
except pygame.error:
print 'Driver: {0} failed.'.format(driver)
continue
found = True
break
if not found:
raise Exception('No suitable video driver found!')
size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
print "Framebuffer size: %d x %d" % (size[0], size[1])
screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
# Clear the screen to start
screen.fill((0, 0, 255))
# Initialise font support
pygame.font.init()
# Render the screen
pygame.display.update()
uid = None
# Get a font and use it render some text on a Surface.
font = pygame.font.Font(None, 80)
logged = False #is anyone logged in
logouttime = datetime.datetime.now()
while 1:
for event in pygame.event.get():
if event.key == K_ESCAPE:
testdb.close()
sys.exit()
#clear the screen
pygame.mouse.set_visible(False) #hide the mouse cursor
screen.fill((255, 255, 255))
# Render the hackspace logo at 400,160
logo = pygame.image.load('/home/pi/logo1.1_300_trans.png').convert()
screen.blit(logo, (400, 160))
# Render some text with a background color
datestr = time.strftime("%A, %d %b %Y", time.localtime())
text_surface = font.render(datestr,
True, (0, 0, 0))
# Blit the text
screen.blit(text_surface, (10, 30))
timestr = time.strftime("%H:%M:%S", time.localtime())
text_surface = font.render(timestr,
True, (0, 0, 0))
# Blit the text
screen.blit(text_surface, (20, 90))
if logged:
tdur = datetime.datetime.now() - tlogin
logstr = str(tdur).split('.',2)[0]
text_surface = font.render(username,
True, (0, 0, 0))
screen.blit(text_surface, (20, 210))
text_surface = font.render(logstr,
True, (0, 0, 0))
screen.blit(text_surface, (20, 270))
# Update the display
pygame.display.update()
try:
uid = mifare.select()
except nxppy.SelectError:
uid = None
pass
if uid is not None:
tslo = datetime.datetime.now() - logouttime
if (tslo.total_seconds() < 30):
continue
#print "Card detected:", uid
if uid in user_hash.keys():
username = user_hash[uid]
if (logged and (tdur.total_seconds() > 10)): #logout
logged = False
ser.write('O')
#erase logged text
text_surface = font.render(username,
True, (255, 255, 255))
screen.blit(text_surface, (20, 210))
text_surface = font.render(logstr,
True, (255, 255, 255))
screen.blit(text_surface, (20, 270))
cur.execute("""INSERT INTO Logbook(Cardid,Name,Time,date,duration) VALUES(?,?,?,?,?);""",(loggedid,loggeduser,timestr,datestr,logstr))
testdb.commit()
logouttime = datetime.datetime.now()
elif (logged and (tdur.total_seconds() < 10)):
continue
else:
logged = True
ser.write('H')
tlogin = datetime.datetime.now()
loggeduser = username
loggedid = uid
#text_surface = font.render("Hello",
# True, (0, 0, 0))
#screen.blit(text_surface, (20, 210))
text_surface = font.render(username,
True, (0, 0, 0))
screen.blit(text_surface, (20, 270))
else:
text_surface = font.render("unknown card",
True, (0, 0, 0))
screen.blit(text_surface, (20, 210))
uid = None #reset uid to None
pygame.display.update()
time.sleep(1)
#get out of full screen
#size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
pygame.quit()