-
Notifications
You must be signed in to change notification settings - Fork 20
/
ChipImport.py
312 lines (261 loc) · 10.8 KB
/
ChipImport.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
import wx
import os
import math
import datetime
import wx.lib.intctrl
import Model
import Utils
import JChip
from ReadSignOnSheet import GetTagNums
from Undo import undo
from HighPrecisionTimeEdit import HighPrecisionTimeEdit
def DoChipImport( fname, parseTagTime, startTime = None,
clearExistingData = True, timeAdjustment = None ):
errors = []
race = Model.race
if race and race.isRunning():
Utils.MessageOK( Utils.getMainWin(), '\n\n'.join( [_('Cannot Import into a Running Race.'), _('Wait until you have a complete data set, then import the full data into a New race.')] ),
title = _('Cannot Import into Running Race'), iconMask = wx.ICON_ERROR )
return
# If startTime is None, the first time will be taken as the start time.
# All first time's for each rider will then be ignored.
if timeAdjustment is None:
timeAdjustment = datetime.timedelta(seconds=0.0)
raceStart = None
with open(fname, encoding='utf8') as f, Model.LockRace() as race:
year, month, day = [int(n) for n in race.date.split('-')]
raceDate = datetime.date( year=year, month=month, day=day )
JChip.reset( raceDate )
if startTime:
raceStart = datetime.datetime.combine( raceDate, startTime )
race.resetStartClockOnFirstTag = False
else:
race.resetStartClockOnFirstTag = True
tagNums = GetTagNums( True )
race.missingTags = set()
tFirst, tLast = None, None
lineNo = 0
riderRaceTimes = {}
for line in f:
lineNo += 1
line = line.strip()
if not line or line[0] in '#;':
continue
tag, t = parseTagTime( line, lineNo, errors )
if tag is None:
continue
if raceStart and t < raceStart:
errors.append( '{} {}: {} ({})'.format(_('line'), lineNo, _('time is before race start'), t.strftime('%H:%M:%S.%f')) )
continue
tag = tag.lstrip('0').upper()
t += timeAdjustment
if not tFirst:
tFirst = t
tLast = t
try:
num = tagNums[tag]
riderRaceTimes.setdefault( num, [] ).append( t )
except KeyError:
if tag not in race.missingTags:
errors.append( '{} {}: {}: {}'.format(_('line'), lineNo, _('tag missing from Excel sheet'), tag) )
race.missingTags.add( tag )
continue
#------------------------------------------------------------------------------
# Populate the race with the times.
if not riderRaceTimes:
errors.insert( 0, _('No matching tags found in Excel link. Import aborted.') )
return errors
# Put all the rider times into the race.
if clearExistingData:
race.clearAllRiderTimes()
if not raceStart:
raceStart = tFirst
race.startTime = raceStart
for num, lapTimes in riderRaceTimes.items():
for t in lapTimes:
raceTime = (t - raceStart).total_seconds()
if not race.hasTime(num, raceTime):
race.addTime( num, raceTime )
if tLast:
race.finishTime = tLast + datetime.timedelta( seconds = 0.0001 )
# Figure out the race minutes from the recorded laps.
if riderRaceTimes:
lapNumMax = max( len(ts) for ts in riderRaceTimes.values() )
if lapNumMax > 0:
tElapsed = min( ts[-1] for ts in riderRaceTimes.values() if len(ts) == lapNumMax )
raceMinutes = int((tElapsed - raceStart).total_seconds() / 60.0) + 1
race.minutes = raceMinutes
return errors
#------------------------------------------------------------------------------------------------
class ChipImportDialog( wx.Dialog ):
def __init__( self, chipName, parseTagTime, parent, id = wx.ID_ANY, fileSuffix = 'txt' ):
super().__init__( parent, id, '{} {}'.format(chipName, _('Import')),
style=wx.DEFAULT_DIALOG_STYLE|wx.TAB_TRAVERSAL )
self.chipName = chipName
self.parseTagTime = parseTagTime
self.fileSuffix = fileSuffix
todoList = [
'{} {}'.format(chipName, _('Import Data File')),
'',
_('You must first "New" a race and fill in the details.'),
_('You must also configure a "Tag" field in your Sign-On Excel Sheet and link the sheet to the race.'),
_('This is required so CrossMgr can link the tags in the import file back to rider numbers and info.'),
'',
_('Race Data:'),
_('If the first chip read is NOT the start of the race, you will need to enter the start time manually.'),
_('Otherwise the import will use the first chip read as the race start.'),
'',
_('TimeTrial Data:'),
_("The first chip read for each rider will be interpreted as the rider's start time."),
'',
_('Warning: Importing from chip data could replace all the data in this race.'),
_('Proceed with caution.'),
]
intro = '\n'.join(todoList)
gs = wx.FlexGridSizer( rows=0, cols=3, vgap=10, hgap=5 )
gs.Add( wx.StaticText(self, label = '{} {}:'.format(chipName, _('Data File'))), 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT )
self.chipDataFile = wx.TextCtrl( self, -1, '', size=(450,-1) )
defaultPath = Utils.getFileName()
if not defaultPath:
defaultPath = Utils.getDocumentsDir()
else:
defaultPath = os.path.join( os.path.split(defaultPath)[0], '' )
self.chipDataFile.SetValue( defaultPath )
gs.Add( self.chipDataFile, 1, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT|wx.GROW)
btn = wx.Button( self, label=_('Browse') + '...' )
btn.Bind( wx.EVT_BUTTON, self.onBrowseChipReaderDataFile )
gs.Add( btn, 0, wx.ALIGN_CENTER_VERTICAL )
gs.AddSpacer(1)
self.dataType = wx.StaticText( self, label = _("Data Is:") )
gs.Add( self.dataType, 1, wx.ALIGN_LEFT )
gs.AddSpacer(1)
gs.Add( wx.StaticText(self, label = _('Data Policy:') ), 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT )
self.importPolicy = wx.Choice( self, choices = [
_('Clear All Existing Data Before Import'),
_('Merge New Data with Existing')
] )
self.importPolicy.SetSelection( 0 )
gs.Add( self.importPolicy, 1, wx.ALIGN_LEFT )
gs.AddSpacer(1)
gs.Add( wx.StaticText(self, label = _('Import Data Time Adjustment:') ), 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT )
self.timeAdjustment = HighPrecisionTimeEdit( self, size=(120,-1) )
self.behindAhead = wx.Choice( self, choices=[_('Behind'), _('Ahead')] )
self.behindAhead.SetSelection( 0 )
self.timeAdjustment.SetSeconds( 0.0 )
hb = wx.BoxSizer()
hb.Add( self.behindAhead, flag=wx.ALIGN_BOTTOM|wx.BOTTOM, border=4 )
hb.Add( self.timeAdjustment, flag=wx.ALL, border=4 )
gs.Add( hb, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT )
gs.AddSpacer(1)
self.manualStartTime = wx.CheckBox(self, label = _('Race Start Time (if NOT first recorded time):') )
self.Bind( wx.EVT_CHECKBOX, self.onChangeManualStartTime, self.manualStartTime )
gs.Add( self.manualStartTime, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT )
self.raceStartTime = HighPrecisionTimeEdit( self, seconds=10*60*60 )
self.raceStartTime.Enable( False )
gs.Add( self.raceStartTime, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT)
gs.AddSpacer(1)
with Model.LockRace() as race:
isTimeTrial = getattr(race, 'isTimeTrial', False) if race else False
if isTimeTrial:
self.manualStartTime.Enable( False )
self.manualStartTime.Show( False )
self.raceStartTime.Enable( False )
self.raceStartTime.Show( False )
self.dataType.SetLabel( _('Data will be imported for a Time Trial') )
else:
self.dataType.SetLabel( _('Data will be imported for a Race') )
bs = wx.BoxSizer( wx.VERTICAL )
border = 4
hs = wx.BoxSizer( wx.HORIZONTAL )
try:
image = wx.Image( os.path.join(Utils.getImageFolder(), '{}Logo.png'.format(chipName)), wx.BITMAP_TYPE_PNG )
except Exception as e:
image = wx.EmptyImage( 32, 32, True )
hs.Add( wx.StaticBitmap(self, wx.ID_ANY, image.ConvertToBitmap()), 0 )
hs.Add( wx.StaticText(self, label = intro), 1, wx.EXPAND|wx.LEFT, border*2 )
bs.Add( hs, 1, wx.EXPAND|wx.ALL, border )
#-------------------------------------------------------------------
bs.AddSpacer( border )
bs.Add( gs, 0, wx.EXPAND | wx.ALL, border )
btnSizer = self.CreateStdDialogButtonSizer( wx.OK|wx.CANCEL )
self.Bind( wx.EVT_BUTTON, self.onOK, id=wx.ID_OK )
if btnSizer:
bs.Add( btnSizer, 0, wx.EXPAND | wx.ALL, border )
self.SetSizerAndFit(bs)
bs.Fit( self )
self.CentreOnParent(wx.BOTH)
wx.CallAfter( self.SetFocus )
def onChangeManualStartTime( self, event ):
self.raceStartTime.Enable( event.IsChecked() )
def onBrowseChipReaderDataFile( self, event ):
defaultPath = self.chipDataFile.GetValue()
if not defaultPath:
defaultPath = Utils.getFileName()
if defaultPath:
defaultPath = os.path.split(defaultPath)[0]
else:
defaultPath = Utils.getDocumentsDir()
defaultFile = ''
else:
defaultPath, defaultFile = os.path.split(defaultPath)
with wx.FileDialog( self, '{} {}'.format( self.chipName, _('Import file') ),
style=wx.FD_OPEN | wx.FD_CHANGE_DIR,
wildcard='RFID (*.{})|*.{}'.format(self.fileSuffix, self.fileSuffix),
defaultDir=defaultPath if defaultPath else '',
defaultFile=defaultFile if defaultFile else '',
) as dlg:
if dlg.ShowModal() == wx.ID_OK:
self.chipDataFile.SetValue( dlg.GetPath() )
def onOK( self, event ):
fname = self.chipDataFile.GetValue()
try:
with open( fname ):
pass
except IOError:
Utils.MessageOK( self, '{}:\n\n"{}"'.format(_('Could not open data file for import'), fname),
title = _('Cannot Open File'), iconMask = wx.ICON_ERROR)
return
clearExistingData = (self.importPolicy.GetSelection() == 0)
timeAdjustment = self.timeAdjustment.GetSeconds()
if self.behindAhead.GetSelection() == 1:
timeAdjustment *= -1
# Get the start time.
if not clearExistingData:
if not Model.race or not Model.race.startTime:
Utils.MessageOK( self,
'\n\n'.join( [_('Cannot Merge into Unstarted Race.'), _('Clear All Existing Data is allowed.')] ),
title = _('Import Merge Failed'), iconMask = wx.ICON_ERROR
)
return
startTime = Model.race.startTime.time()
else:
if self.manualStartTime.IsChecked():
startSeconds = self.raceStartTime.GetSeconds()
fraction, seconds = math.modf( startSeconds )
seconds = int(seconds)
startTime = datetime.time( (seconds//(60*60))%24, (seconds//60)%60, seconds%60, int(fraction*1000000) )
else:
startTime = None
undo.pushState()
errors = DoChipImport( fname, self.parseTagTime, startTime,
clearExistingData,
datetime.timedelta(seconds = timeAdjustment) )
if errors:
# Copy the tags to the clipboard.
clipboard = wx.Clipboard.Get()
if not clipboard.IsOpened():
clipboard.Open()
clipboard.SetData( wx.TextDataObject('\n'.join(errors)) )
clipboard.Close()
if len(errors) > 10:
errors = errors[:10]
errors.append( '...' )
tagStr = '\n'.join(errors)
Utils.MessageOK( self,
'{}:\n\n{}\n\n{}.'.format(_('Import File contains errors'), tagStr, _('All errors have been copied to the clipboard')),
_('Import Warning'),
iconMask = wx.ICON_WARNING )
else:
Utils.MessageOK( self, _('Import Successful'), _('Import Successful') )
wx.CallAfter( Utils.refresh )
self.EndModal( wx.ID_OK )