forked from esitarski/CrossMgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImpinjImport.py
95 lines (80 loc) · 2.58 KB
/
ImpinjImport.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
import wx
import sys
import six
import math
import Utils
import Model
import JChip
from ChipImport import ChipImportDialog
import datetime
combine = datetime.datetime.combine
import string
if six.PY2:
sepTrans = string.maketrans( '/-:', ' ' )
else:
sepTrans = str.maketrans( '/-:', ' ' )
def timeFromStr( tStr ):
try:
tStr = tStr.translate( sepTrans )
hour, minute, second = tStr.split()[-3:]
fract, second = math.modf( float(second) )
microsecond = fract * 1000000.0
t = combine( JChip.dateToday, datetime.time(hour=int(hour), minute=int(minute), second=int(second), microsecond=int(microsecond)) )
return t
except (IndexError, ValueError):
return None
def parseTagTime( line, lineNo, errors ):
try:
tag, tStr = line.split(',')[:2]
except (IndexError, ValueError):
errors.append( u'{}: {} {}'.format(_('line'), lineNo, _('unrecognised input')) )
return None, None
if tag.startswith( 'Tag' ):
return None, None # Skip header row.
tStrSave = tStr
tag = tag.replace( ' ', '' )
tStr = tStr.translate( sepTrans ).strip()
tFields = tStr.split()
try:
year, month, day, hour, minute = [int(f) for f in tFields[:-1]]
fract, second = math.modf( float(tFields[-1]) )
microsecond = int(fract * 1000000.0)
second = int(second)
return tag, datetime.datetime( year, month, day, hour, minute, second, microsecond )
except:
# Get the date (if present).
tDate = JChip.dateToday
try:
yyyy, mm, dd = [int(v) for v in tFields[-3:]]
if 1900 <= yyyy <= 3000 and 1 <= mm <= 12 and 1 <= dd <= 31:
tDate = datetime.date( yyyy, mm, dd )
except (IndexError, ValueError):
pass
# Get the time.
try:
hour, minute, second = tFields[3:6]
fract, second = math.modf( float(second) )
microsecond = fract * 1000000.0
t = combine( tDate, datetime.time(hour=int(hour), minute=int(minute), second=int(second), microsecond=int(microsecond)) )
except (IndexError, ValueError):
errors.append( u'{}: {} {}: "{}"'.format( _('line'), lineNo, _('invalid time'), tStrSave) )
return None, None
return tag, t
def ImpinjImportDialog( parent, id = wx.ID_ANY ):
return ChipImportDialog( 'Impinj', parseTagTime, parent, id )
if __name__ == '__main__':
data = '''Tag,Timestamp
AAABBB, 2016-09-11 09:15:34.305
AAADDD, 2016-10-11 23:15:34.305
'''
errors = []
for lineNo, line in enumerate(data.split('\n'), 1):
six.print_( parseTagTime(line, lineNo, errors) )
sys.exit()
app = wx.App(False)
mainWin = wx.Frame(None,title="CrossMan", size=(600,400))
Model.setRace( Model.Race() )
mainWin.Show()
dlg = ImpinjImportDialog( mainWin )
dlg.ShowModal()
dlg.Destroy()