eventparser: fix parsing

This commit is contained in:
James Woglom
2024-09-21 17:16:40 -04:00
parent 7066afef33
commit 9752270b6c
5 changed files with 702 additions and 270 deletions
+10 -1
View File
@@ -35,8 +35,9 @@ TYPE_TO_PYOBJ = {
'float32': 'float',
}
HEADER_SIZE = 10
def unpack_command_for(field_def):
return f'struct.unpack_from({field_def["type"].upper()}, raw[:EVENT_LEN], {field_def["offset"]})'
return f'struct.unpack_from({field_def["type"].upper()}, raw[:EVENT_LEN], {HEADER_SIZE + field_def["offset"]})'
TEMPLATE = '''
@dataclass
@@ -58,6 +59,14 @@ class {name}(BaseEvent):
{build_p2}
)
@property
def eventTimestamp(self):
return self.raw.timestamp
@property
def eventId(self):
return self.raw.id
'''
def firstLower(text):
File diff suppressed because it is too large Load Diff
+6 -2
View File
@@ -1,11 +1,15 @@
import struct
import base64
from dataclasses import dataclass
from .raw_event import RawEvent, EVENT_LEN
from .codegen import EVENT_IDS
from .events import EVENT_IDS
from .utils import batched
Event = lambda x: EVENT_IDS[RawEvent.build(x).id].build(x)
Events = lambda x: (Event(bytearray(e)) for e in batched(x, EVENT_LEN))
Events = lambda x: (Event(bytearray(e)) for e in batched(x, EVENT_LEN))
def decode_raw_events(raw):
return base64.b64decode(raw)
+13 -5
View File
@@ -1,27 +1,35 @@
import struct
import arrow
from dataclasses import dataclass
EVENT_LEN = 26
UINT16 = '>H'
UINT32 = '>I'
TANDEM_EPOCH = 1199145600
@dataclass
class RawEvent:
source: int
id: int
timestamp: int
timestampRaw: int
seqNum: int
raw: bytearray
@staticmethod
def build(raw):
source_and_id, = struct.unpack_from(UINT16, raw[:EVENT_LEN], 0)
timestamp, = struct.unpack_from(UINT32, raw[:EVENT_LEN], 2)
timestampRaw, = struct.unpack_from(UINT32, raw[:EVENT_LEN], 2)
seqNum, = struct.unpack_from(UINT32, raw[:EVENT_LEN], 6)
return RawEvent(
source = (source_and_id & 0xF000) >> 12,
id = source_and_id & 0x0FFF,
timestamp = timestamp,
seqNum = seqNum
)
timestampRaw = timestampRaw,
seqNum = seqNum,
raw = raw
)
@property
def timestamp(self):
return arrow.get(TANDEM_EPOCH + self.timestampRaw)
+14 -3
View File
@@ -13,8 +13,12 @@ def enumNameFormat(text):
if t.startswith('yes,'):
return 'Yes'
rem = None
for i in '-,.':
t = t.split(i)[0]
spl = t.split(i)
t = spl[0]
if len(spl) > 1:
rem = rem or spl[1]
for i in '()/"\u201c\u201d':
t = t.replace(i, '')
@@ -25,6 +29,13 @@ def enumNameFormat(text):
return 'TrueVal'
if t.lower() == 'none':
return 'NoneVal'
if t.lower() == 'reserved':
return None
if t.lower() == 'unused':
return None
if t.lower() == 'unavailable' and rem:
suffix = enumNameFormat(rem)
t += f'{suffix[0].lower()}{suffix[1:]}'
return f'{t[0].upper()}{t[1:]}'
@@ -36,7 +47,7 @@ def transform_enum(event_def, name, name_fmt, field, tx):
out += ['']
out += [f'class {enumNameFormat(name_fmt)}Enum(Enum):']
out += [
f' {enumNameFormat(v)} = {k}' for k, v in tx.items()
f' {enumNameFormat(v)} = {k}' for k, v in tx.items() if enumNameFormat(v)
]
out += ['']
out += [
@@ -64,7 +75,7 @@ def transform_bitmask(event_def, name, name_fmt, field, tx):
out += ['']
out += [f'class {enumNameFormat(name_fmt)}Bitmask(IntFlag):',]
out += [
f' {enumNameFormat(v)} = 2**{k}' for k, v in tx.items() if 'unused' not in v.lower()
f' {enumNameFormat(v)} = 2**{k}' for k, v in tx.items() if enumNameFormat(v)
]
out += ['']
out += [