-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathJChipSetup.py
403 lines (339 loc) · 13.3 KB
/
JChipSetup.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import re
import socket
import wx
import wx.lib.intctrl
import Model
import Utils
import JChip
import ChipReader
from JChip import EVT_CHIP_READER
import RaceResult
import Ultra
import MyLapsServer
import HelpSearch
from ReadSignOnSheet import GetTagNums
HOST, PORT = JChip.DEFAULT_HOST, JChip.DEFAULT_PORT
def CheckExcelLink():
race = Model.race
if not race:
return (False, 'No active race.')
try:
externalFields = race.excelLink.getFields()
except (ValueError, AttributeError):
return (False, 'Unconfigured.')
if 'Tag' not in externalFields:
return (False, '"Tag" column not specified.')
return (True, 'Excel Link OK')
#------------------------------------------------------------------------------------------------
reIP = re.compile( '^[0-9.]+$' )
def GetAllIps():
addrInfo = socket.getaddrinfo( socket.gethostname(), None )
ips = set()
for a in addrInfo:
try:
ip = a[4][0]
except Exception:
continue
if reIP.search(ip):
ips.add( ip )
return sorted( ips )
class JChipSetupDialog( wx.Dialog ):
def __init__( self, parent, id = wx.ID_ANY ):
super().__init__( parent, id, _("Chip Reader Setup"),
style=wx.DEFAULT_DIALOG_STYLE|wx.TAB_TRAVERSAL|wx.RESIZE_BORDER )
self.timer = None
self.receivedCount = 0
self.refTime = None
self.enableJChipCheckBox = wx.CheckBox( self, label = _('Use RFID Reader During Race') )
if Model.race:
self.enableJChipCheckBox.SetValue( getattr(Model.race, 'enableJChipIntegration', False) )
else:
self.enableJChipCheckBox.Enable( False )
self.testJChip = wx.ToggleButton( self, label = _('Start RFID Test') )
self.testJChip.SetFont( wx.Font( (0,24), wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL ) )
self.testJChip.Bind( wx.EVT_TOGGLEBUTTON, self.testJChipToggle )
self.testList = wx.TextCtrl( self, style=wx.TE_READONLY|wx.TE_MULTILINE, size=(-1,200) )
self.testList.Bind( wx.EVT_RIGHT_DOWN, self.skip )
self.Bind(EVT_CHIP_READER, self.handleChipReaderEvent)
bs = wx.BoxSizer( wx.VERTICAL )
todoList = '\n'.join( '{}) {}'.format(i + 1, s) for i, s in enumerate( [
_('Make sure the RFID receiver is plugged into the network.'),
_('If you are using Impinj/Alien, make sure the CrossMgrImpinj or CrossMgrAlien bridge programs are running.'),
_('You must have the Sign-On Excel sheet ready and linked before your race.'),
_('You must configure a "Tag" field in your Sign-On Excel Sheet.'),
_('Run this test before each race.'),
]) )
intro = ('\n'.join( [
_('CrossMgr supports the JChip, RaceResult, Ultra, MyLaps, Impinj and Alien RFID readers.'),
_('For more details, consult the documentation for your reader.'),
] ) + '\n' + _('Checklist:') + '\n\n{}\n').format( todoList )
border = 4
bs.Add( wx.StaticText(self, label = intro), 0, wx.EXPAND|wx.ALL, border )
bs.Add( self.enableJChipCheckBox, 0, wx.ALL|wx.ALIGN_LEFT, border )
#-------------------------------------------------------------------
bs.AddSpacer( border )
bs.Add( wx.StaticText( self, label = _('Reader Configuration:') ), 0, wx.EXPAND|wx.ALL, border )
#-------------------------------------------------------------------
gridBagSizer = wx.GridBagSizer()
bs.Add( gridBagSizer, 0, wx.EXPAND|wx.ALL, border )
row = 0
gridBagSizer.Add( wx.StaticText( self, label='{}:'.format(_('Reader Type')) ), pos=(row,0), border=border,
flag=wx.TOP|wx.LEFT|wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL )
self.chipReaderType = wx.Choice( self, choices=ChipReader.ChipReader.Choices )
self.chipReaderType.SetSelection( 0 )
self.chipReaderType.Bind( wx.EVT_CHOICE, self.changechipReaderType )
gridBagSizer.Add( self.chipReaderType,
pos=(row, 1), border=border, flag=wx.EXPAND|wx.TOP|wx.RIGHT|wx.ALIGN_LEFT )
row += 1
sep = ' -' + _('or') + '- '
ips = sep.join( GetAllIps() )
self.ipaddr = wx.TextCtrl( self, value = ips, style = wx.TE_READONLY, size=(240,-1) )
self.autoDetect = wx.Button( self, label=_('AutoDetect') )
self.autoDetect.Show( False )
self.autoDetect.Bind( wx.EVT_BUTTON, self.doAutoDetect )
iphs = wx.BoxSizer( wx.HORIZONTAL )
iphs.Add( self.ipaddr, 1, flag=wx.EXPAND )
iphs.Add( self.autoDetect, 0, flag=wx.LEFT, border=4 )
gridBagSizer.Add( wx.StaticText( self, label=_('Remote IP Address:') ),
pos=(row,0), flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL )
gridBagSizer.Add( iphs, pos=(row, 1), border=border, flag=wx.EXPAND|wx.RIGHT|wx.ALIGN_LEFT )
row += 1
self.port = wx.lib.intctrl.IntCtrl( self, -1, min=1, max=65535, value=PORT,
limited=True, style = wx.TE_READONLY )
gridBagSizer.Add( wx.StaticText(self, label = _('Remote Port:')), pos=(row,0),
flag=wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL )
gridBagSizer.Add( self.port, pos=(row,1), border=border, flag=wx.EXPAND|wx.RIGHT|wx.ALIGN_LEFT )
bs.Add( wx.StaticText( self, label = _('If using JChip, see "7 Setting of Connections" in JChip "Control Panel Soft Manual" for more details.') ),
border=border, flag = wx.GROW|wx.ALL )
#-------------------------------------------------------------------
bs.Add( self.testJChip, 0, wx.ALIGN_CENTER|wx.ALL, border )
bs.Add( wx.StaticText(self, label = _('Messages:')), 0, wx.EXPAND|wx.ALL, border=border )
bs.Add( self.testList, 1, wx.EXPAND|wx.ALL, border )
btnSizer = self.CreateStdDialogButtonSizer( wx.OK|wx.CANCEL|wx.HELP )
self.Bind( wx.EVT_BUTTON, self.onOK, id=wx.ID_OK )
self.Bind( wx.EVT_BUTTON, self.onCancel, id=wx.ID_CANCEL )
self.Bind( wx.EVT_BUTTON, lambda evt: HelpSearch.showHelp('Menu-ChipReader.html#chip-reader-setup'), id=wx.ID_HELP )
if btnSizer:
bs.Add( btnSizer, 0, wx.EXPAND | wx.ALL, border )
self.stopTest()
self.SetSizerAndFit(bs)
bs.Fit( self )
self.update()
self.CentreOnParent(wx.BOTH)
wx.CallAfter( self.SetFocus )
def skip(self, evt):
return
def commit( self ):
race = Model.race
if not race:
return
race.chipReaderType = max( 0, self.chipReaderType.GetSelection() )
race.chipReaderIpAddr = self.ipaddr.GetValue()
if race.chipReaderType == 1:
Utils.writeConfig( 'RaceResultHost', race.chipReaderIpAddr )
elif race.chipReaderType == 2:
Utils.writeConfig( 'UltraHost', race.chipReaderIpAddr )
race.chipReaderPort = self.port.GetValue()
race.enableJChipIntegration = bool(self.enableJChipCheckBox.GetValue())
ChipReader.chipReaderCur.reset( race.chipReaderType )
def update( self ):
race = Model.race
if not race:
return
self.enableJChipCheckBox.SetValue( race.enableJChipIntegration )
self.chipReaderType.SetSelection( max(0, race.chipReaderType) )
self.ipaddr.SetValue( race.chipReaderIpAddr )
self.port.SetValue( race.chipReaderPort )
self.changechipReaderType()
def changechipReaderType( self, event=None ):
selection = self.chipReaderType.GetSelection()
if selection == 0: # JChip/CrossMgrImpinj/CrossMgrAlien
self.port.SetValue( JChip.DEFAULT_PORT )
self.port.SetEditable( False )
self.ipaddr.SetValue( Utils.GetDefaultHost() )
self.ipaddr.SetEditable( False )
self.autoDetect.Show( False )
elif selection == 1: # RaceResult
self.port.SetValue( RaceResult.DEFAULT_PORT )
self.port.SetEditable( True )
self.ipaddr.SetEditable( True )
rfidReaderHost = Utils.readConfig( 'RfidReaderHost', None )
if rfidReaderHost:
try:
self.ipaddr.SetValue( rfidReaderHost )
except Exception as e:
self.ipaddr.SetValue( Utils.GetDefaultHost() )
self.autoDetect.Show( True )
elif selection == 2: # Ultra
self.port.SetValue( Ultra.DEFAULT_PORT )
self.port.SetEditable( True )
self.ipaddr.SetEditable( True )
rfidReaderHost = Utils.readConfig( 'RfidReaderHost', None )
if rfidReaderHost:
try:
self.ipaddr.SetValue( rfidReaderHost )
except Exception as e:
self.ipaddr.SetValue( Utils.GetDefaultHost() )
self.autoDetect.Show( True )
elif selection == 3: # WebReader
self.port.SetValue( 8765 )
self.port.SetEditable( False )
self.ipaddr.SetEditable( False )
rfidReaderHost = ''
try:
self.ipaddr.SetValue( rfidReaderHost )
except Exception as e:
self.ipaddr.SetValue( Utils.GetDefaultHost() )
self.autoDetect.Show( False )
elif selection == 4: # MyLaps
self.port.SetValue( MyLapsServer.DEFAULT_PORT )
self.port.SetEditable( False )
self.ipaddr.SetEditable( False )
rfidReaderHost = ''
try:
self.ipaddr.SetValue( rfidReaderHost )
except Exception as e:
self.ipaddr.SetValue( Utils.GetDefaultHost() )
self.autoDetect.Show( False )
self.Layout()
self.Refresh()
def doAutoDetect( self, event ):
selection = self.chipReaderType.GetSelection()
autoDetect = [RaceResult.AutoDetect, Ultra.AutoDetect][selection-1]
def getHost():
with wx.BusyCursor():
try:
return None, autoDetect(self.port.GetValue())
except Exception as e:
return e, None
error, readerHost = getHost()
if error:
Utils.MessageOK(
self,
'{}:\n\n{}'.format(_("AutoDetect Error"), error),
_("AutoDetect Error"),
wx.ICON_ERROR
)
return
if not readerHost:
Utils.MessageOK(
self, '{}:\n\n{}'.format(_("AutoDetect Failure"), _('Reader not found.')),
_("AutoDetect Failure"),
wx.ICON_ERROR
)
return
self.ipaddr.SetValue( readerHost )
def handleChipReaderEvent( self, event ):
if not event.tagTimes:
return
race = Model.race
if race:
if not race.enableUSBCamera:
return
GetTagNums()
tag, dt = event.tagTimes[-1]
def testJChipToggle( self, event ):
self.commit()
if not Model.race:
self.stopTest()
Utils.MessageOK( self, _('No active race. Cannot perform RFID test. "New" or "Open" a race first.'), _('Cannot Perform RFID Test') )
return
if Model.race.isRunning():
self.stopTest()
Utils.MessageOK( self, _('Cannot perform RFID test while race is running.'), _('Cannot Perform RFID Test') )
return
if self.testJChip.GetValue():
correct, reason = CheckExcelLink()
explain = _('CrossMgr will not be able to associate chip Tags with Bib numbers.') + '\n' + \
_('You may proceed with the test, but you need to fix the Excel sheet.') + '\n\n' + \
_('See documentation for details.')
if not correct:
if not Utils.MessageOKCancel( self, (_('Problems with Excel sheet.') + '\n\n ' + _('Reason:') + ' {}\n\n{}').format(reason, explain),
title = _('Excel Link Problem'), iconMask = wx.ICON_WARNING ):
self.testJChip.SetValue( False )
return
tagNums = GetTagNums( True )
if correct and not tagNums:
if not Utils.MessageOKCancel( self, (_('All Tag entries in the Excel sheet are blank.') + '\n\n{}').format(explain),
title = _('Excel Link Problem'), iconMask = wx.ICON_WARNING ):
self.testJChip.SetValue( False )
return
ChipReader.chipReaderCur.readerEventWindow = self
self.testList.Clear()
self.testJChip.SetLabel( 'Stop RFID Test' )
self.testJChip.SetBackgroundColour( wx.Colour(255,128,128) )
self.testJChip.SetValue( True )
ChipReader.chipReaderCur.StartListener( test=True )
self.appendMsg( 'listening for RFID connection...' )
# Start a timer to monitor the receiver.
self.receivedCount = 0
self.timer = wx.CallLater( 1000, self.onTimerCallback, 'started' )
else:
self.stopTest()
def appendMsg( self, s ):
self.testList.AppendText( s + '\n' )
def onTimerCallback( self, stat ):
data = ChipReader.chipReaderCur.GetData()
lastTag = None
for d in data:
if d[0] == 'data':
self.receivedCount += 1
ts = d[2].isoformat(' ')
if len(ts) == 8:
ts += '.00'
else:
ts = ts[:-2]
try:
num = '{}'.format(Model.race.tagNums[d[1]])
except (AttributeError, ValueError, KeyError):
num = 'not found'
lastTag = d[1]
self.appendMsg( '{}: tag={}, time={}, Bib={}'.format(self.receivedCount, d[1], ts, num) )
elif d[0] == 'connected':
self.appendMsg( '*******************************************' )
self.appendMsg( '{}: {}'.format(d[0], ', '.join('{}'.format(s) for s in d[1:]) ) )
elif d[0] == 'disconnected':
self.appendMsg( d[0] )
self.appendMsg( '' )
self.appendMsg( _('listening for RFID connection...') )
elif d[0] == 'name':
self.appendMsg( '{}: {}'.format(_('receiver name'), d[1]) )
else:
self.appendMsg( '{}: {}'.format(d[0], ', '.join('<<{}>>'.format(s) for s in d[1:]) ) )
if data:
self.testList.SetInsertionPointEnd()
self.timer.Restart( 1000, 'restarted' )
if lastTag and Utils.mainWin and getattr(Utils.mainWin, 'findDialog', None):
if Utils.mainWin.findDialog.IsShown():
Utils.mainWin.findDialog.refresh( lastTag )
def stopTest( self ):
ChipReader.chipReaderCur.StopListener()
if self.timer:
self.timer.Stop()
self.timer = None
self.testList.Clear()
self.appendMsg( _('No test running.') )
ChipReader.chipReaderCur.readerEventWindow = None
self.testJChip.SetLabel( _('Start RFID Test') )
self.testJChip.SetBackgroundColour( wx.NullColour )
self.testJChip.SetValue( False )
def onOK( self, event ):
self.stopTest()
self.commit()
wx.CallAfter( Utils.refresh )
self.EndModal( wx.ID_OK )
def onCancel( self, event ):
self.stopTest()
self.EndModal( wx.ID_CANCEL )
if __name__ == '__main__':
print( GetAllIps() )
#sys.exit()
app = wx.App(False)
mainWin = wx.Frame(None,title="CrossMan", size=(600,400))
Model.setRace( Model.Race() )
Model.race._populate()
Model.race.finishRaceNow()
Model.race.enableUSBCamera = True
mainWin.Show()
with JChipSetupDialog( mainWin ) as dlg:
dlg.ShowModal()