forked from rechner/py-aamva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
299 lines (257 loc) · 12.7 KB
/
gui.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env python
#*-* coding: utf-8 *-*
import wx
import threading
import serial
import aamva
def xstr(s):
return '' if s is None else str(s)
t_DATA_WAITING = wx.NewEventType()
DATA_WAITING = wx.PyEventBinder(t_DATA_WAITING, 1)
t_THREAD_ERROR = wx.NewEventType()
THREAD_ERROR = wx.PyEventBinder(t_THREAD_ERROR, 1)
class AAMVATestFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
vSizer = wx.BoxSizer(wx.VERTICAL)
hSizer1 = wx.BoxSizer(wx.HORIZONTAL)
#Row 1
nameSt = wx.StaticText(self.panel, wx.ID_ANY, "Name")
hSizer1.Add(nameSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.NameText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer1.Add(self.NameText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 5)
middleSt = wx.StaticText(self.panel, wx.ID_ANY, "Middle")
hSizer1.Add(middleSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.MiddleText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer1.Add(self.MiddleText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
vSizer.Add(hSizer1, 0, wx.EXPAND, 5)
#Row 2
hSizer2 = wx.BoxSizer(wx.HORIZONTAL)
surnameSt = wx.StaticText(self.panel, wx.ID_ANY, "Surname")
hSizer2.Add(surnameSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.SurnameText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer2.Add(self.SurnameText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
dobSt = wx.StaticText(self.panel, wx.ID_ANY, "DOB")
hSizer2.Add(dobSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.DOBText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer2.Add(self.DOBText, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
vSizer.Add(hSizer2, 0, wx.EXPAND, 5)
#Row 3
hSizer3 = wx.BoxSizer(wx.HORIZONTAL)
addressSt1 = wx.StaticText(self.panel, wx.ID_ANY, "Address 1")
hSizer3.Add(addressSt1, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.AddressText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer3.Add(self.AddressText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
vSizer.Add(hSizer3, 0, wx.EXPAND, 5)
#Row 4
hSizer4 = wx.BoxSizer(wx.HORIZONTAL)
addressSt2 = wx.StaticText(self.panel, wx.ID_ANY, "Address 2")
hSizer4.Add(addressSt2, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.Address2Text = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer4.Add(self.Address2Text, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
vSizer.Add(hSizer4, 0, wx.EXPAND, 5)
#Row 5
hSizer5 = wx.BoxSizer(wx.HORIZONTAL)
citySt = wx.StaticText(self.panel, wx.ID_ANY, "City")
hSizer5.Add(citySt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.CityText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer5.Add(self.CityText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
stateSt = wx.StaticText(self.panel, wx.ID_ANY, "State")
hSizer5.Add(stateSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.StateText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer5.Add(self.StateText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
ZIPSt = wx.StaticText(self.panel, wx.ID_ANY, "ZIP")
hSizer5.Add(ZIPSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.ZIPText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer5.Add(self.ZIPText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
vSizer.Add(hSizer5, 0, wx.EXPAND, 5)
#Row 6
hSizer6 = wx.BoxSizer(wx.HORIZONTAL)
IINSt = wx.StaticText(self.panel, wx.ID_ANY, "IIN")
hSizer6.Add(IINSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.IINText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer6.Add(self.IINText, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
LicenseNoSt = wx.StaticText(self.panel, wx.ID_ANY, "License #")
hSizer6.Add(LicenseNoSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.LicenseNoText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer6.Add(self.LicenseNoText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
vSizer.Add(hSizer6, 0, wx.EXPAND, 5)
#Row 7
hSizer7 = wx.BoxSizer(wx.HORIZONTAL)
issuedSt = wx.StaticText(self.panel, wx.ID_ANY, "Issued")
hSizer7.Add(issuedSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.IssuedText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer7.Add(self.IssuedText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
expiresSt = wx.StaticText(self.panel, wx.ID_ANY, "Expires")
hSizer7.Add(expiresSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.ExpiresText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer7.Add(self.ExpiresText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
countrySt = wx.StaticText(self.panel, wx.ID_ANY, "Country")
hSizer7.Add(countrySt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.CountryText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer7.Add(self.CountryText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
vSizer.Add(hSizer7, 0, wx.EXPAND, 5)
#Row 8
hSizer8 = wx.BoxSizer(wx.HORIZONTAL)
self.MaleRadio = wx.RadioButton(self.panel, wx.ID_ANY, "M")
hSizer8.Add(self.MaleRadio, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.FemaleRadio = wx.RadioButton(self.panel, wx.ID_ANY, "F")
hSizer8.Add(self.FemaleRadio, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
heightSt = wx.StaticText(self.panel, wx.ID_ANY, "Height")
hSizer8.Add(heightSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.HeightText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer8.Add(self.HeightText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
weightSt = wx.StaticText(self.panel, wx.ID_ANY, "Weight")
hSizer8.Add(weightSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.WeightText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer8.Add(self.WeightText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
hairSt = wx.StaticText(self.panel, wx.ID_ANY, "Hair")
hSizer8.Add(hairSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.HairText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer8.Add(self.HairText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
vSizer.Add(hSizer8, 0, wx.EXPAND, 5)
#Row 9
hSizer9 = wx.BoxSizer(wx.HORIZONTAL)
eyesSt = wx.StaticText(self.panel, wx.ID_ANY, "Eyes")
hSizer9.Add(eyesSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.EyesText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer9.Add(self.EyesText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
endorsementsSt = wx.StaticText(self.panel, wx.ID_ANY, "Endorsements")
hSizer9.Add(endorsementsSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.EndorsementsText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer9.Add(self.EndorsementsText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
restrictionsSt = wx.StaticText(self.panel, wx.ID_ANY, "Restrictions")
hSizer9.Add(restrictionsSt, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
self.RestrictionsText = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_READONLY)
hSizer9.Add(self.RestrictionsText, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
vSizer.Add(hSizer9, 0, wx.EXPAND, 5)
self.clearForm()
#Bind events
self.Bind(DATA_WAITING, self.ProcessScan)
self.Bind(THREAD_ERROR, self.ThreadError)
self.Bind(wx.EVT_CLOSE, self.Close)
#Setup parser
self.parser = aamva.AAMVA()
#Start serial thread
self.THREAD_EXIT_SIGNAL = threading.Event()
self.thread = threading.Thread(target=self._serialWorkerThread,
args=('/dev/ttyUSB0',))
self.thread.start()
self.panel.SetSizer(vSizer)
self.panel.Layout()
def clearForm(self):
self.NameText.Clear()
self.MiddleText.Clear()
self.SurnameText.Clear()
self.DOBText.Clear()
self.AddressText.Clear()
self.Address2Text.Clear()
self.CityText.Clear()
self.StateText.Clear()
self.ZIPText.Clear()
self.IINText.Clear()
self.LicenseNoText.Clear()
self.IssuedText.Clear()
self.ExpiresText.Clear()
self.CountryText.Clear()
self.MaleRadio.SetValue(False)
self.FemaleRadio.SetValue(False)
self.HeightText.Clear()
self.WeightText.Clear()
self.HairText.Clear()
self.EyesText.Clear()
self.EndorsementsText.Clear()
self.RestrictionsText.Clear()
return
def _serialWorkerThread(self, device):
try:
ser = None
#ser = serial.Serial(device, timeout=0.2, baudrate=115200
ser = serial.Serial(device, baudrate=115200, dsrdtr=True, rtscts=True)
print("Opened serial thread...")
while True:
charbuffer = ""
#Read data from scanner until we see a \r\n
while charbuffer[-2:] != '\r\n':
char = ser.read(1) #blocks if no data to read
print(char, end='')
charbuffer += char
#check if we need to close the port:
if self.THREAD_EXIT_SIGNAL.isSet():
ser.close()
return None
#Throw event to have the data processed
evt = wx.PyCommandEvent(t_DATA_WAITING, self.GetId())
evt.data = charbuffer
self.GetEventHandler().ProcessEvent(evt)
except serial.SerialException as e:
#Post event to display error message
evt = wx.PyCommandEvent(t_THREAD_ERROR, self.GetId())
evt.message = "Error opening scanner ({0}):\n\n{1}".format(device, e)
self.GetEventHandler().ProcessEvent(evt)
finally:
#Close the serial device if something goes wrong
if ser: ser.close()
return
def ThreadError(self, evt):
wx.CallAfter(self.ErrorMessage, evt.message)
def ErrorMessage(self, message):
dlg = wx.MessageDialog(self.panel, message,
'Read Error', wx.OK | wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
def ProcessScan(self, evt):
print("Got a scan!")
try:
license = self.parser.decode(evt.data)
except aamva.ReadError as e:
#GUI interaction must be done in a thread-safe way
wx.CallAfter(self.ErrorMessage, 'Invalid data.\n{0}'.format(e))
return
#clear form
self.clearForm()
#set the fields
self.NameText.SetValue(license['first'])
if license['middle'] is not None:
self.MiddleText.SetValue(license['middle'])
self.SurnameText.SetValue(license['last'])
self.DOBText.SetValue(xstr(license['dob']))
self.AddressText.SetValue(license['address'])
self.Address2Text.SetValue(xstr(license['address2']))
self.CityText.SetValue(license['city'])
self.StateText.SetValue(license['state'])
self.ZIPText.SetValue(xstr(license['ZIP'])[0:5]+"-"+xstr(license['ZIP'])[5:])
self.IINText.SetValue(license['IIN'])
self.LicenseNoText.SetValue(license['license_number'])
self.IssuedText.SetValue(xstr(license['issued']))
self.ExpiresText.SetValue(xstr(license['expiry']))
try:
self.CountryText.SetValue(license['country'])
except KeyError:
self.CountryText.SetValue("???")
if license['sex'] == aamva.MALE:
self.MaleRadio.SetValue(True)
elif license['sex'] == aamva.FEMALE:
self.FemaleRadio.SetValue(True)
self.HeightText.SetValue(xstr(license['height']))
self.WeightText.SetValue(xstr(license['weight']))
if license['hair'] is None:
self.HairText.SetValue("???")
else:
self.HairText.SetValue(license['hair'])
if license['eyes'] is None:
self.EyesText.SetValue("???")
else:
self.EyesText.SetValue(license['eyes'])
self.EndorsementsText.SetValue(license['endorsements'])
self.RestrictionsText.SetValue(license['restrictions'])
def Close(self, event):
self.THREAD_EXIT_SIGNAL.set()
self.thread.join() #blocks until thread exits
self.Destroy()
if __name__ == '__main__':
app = wx.App() # Error messages go to popup window
pFrame = AAMVATestFrame(None, title="Scan a License", size=(500, 370))
pFrame.Show()
app.MainLoop()