refactored

This commit is contained in:
2026-03-29 15:37:18 +02:00
parent f7b1b18b2f
commit 670c4db7a4
9 changed files with 46 additions and 28 deletions
+5 -7
View File
@@ -4,22 +4,20 @@ from ubx.ubx import frame_parse, frame_parse_hdr
from ubx.messages.msg import UbxMsg
class UbxListener(MsgListener):
def __init__(self, parser: UbxMsg, mclass: int|None = None, mid: int|None = None):
def __init__(self, msg: UbxMsg):
MsgListener.__init__(self)
self.parser = parser
self.mclass = mclass
self.mid = mid
self.msg = msg
def on_recv(self, msg: MsgContainer):
data, hdr = frame_parse(msg.data)
msg_parsed = self.parser.from_bytes(data)
msg_parsed = self.msg.from_bytes(data)
print(f"{msg.source.name}:{hdr.mclass}:{hdr.mid}:data({len(data)}):{msg_parsed}")
def is_msg(self, msg: MsgContainer):
hdr = frame_parse_hdr(msg.data)
if hdr is not None:
if self.mclass is not None and self.mid is not None:
return self.mclass == hdr.mclass and self.mid == hdr.mid
if self.msg.mclass is not None and self.msg.mid is not None:
return self.msg.mclass == hdr.mclass and self.msg.mid == hdr.mid
else:
return True
return False
+3
View File
@@ -1,6 +1,9 @@
class UbxMsg:
def __init__(self, mclass: int, mid: int):
self.mclass = mclass
self.mid = mid
@classmethod
def from_bytes(cls, data: bytes):
+6
View File
@@ -2,6 +2,9 @@ import struct
from ubx.messages.msg import UbxMsg
class Sig(UbxMsg):
def __init__(self):
UbxMsg.__init__(self, 0x01, 0x43)
class Group1:
@classmethod
def from_bytes(cls, data: bytes):
@@ -39,6 +42,9 @@ class Sig(UbxMsg):
return obj.__dict__
class Sat(UbxMsg):
def __init__(self):
UbxMsg.__init__(self, 0x01, 0x35)
class Group1:
@classmethod
def from_bytes(cls, data: bytes):
+3
View File
@@ -2,6 +2,9 @@ import struct
from ubx.messages.msg import UbxMsg
class RawX(UbxMsg):
def __init__(self):
UbxMsg.__init__(self, 0x02, 0x15)
class Group1:
@classmethod
def from_bytes(cls, data: bytes):
+10 -4
View File
@@ -6,21 +6,27 @@ from ubx.listener import UbxListener
from threading import BoundedSemaphore
class UbxQuery(UbxListener):
def __init__(self, transceiver: Transceiver, parser: UbxMsg, mclass: int|None = None, mid: int|None = None):
UbxListener.__init__(self, parser, mclass, mid)
def __init__(self, transceiver: Transceiver, msg: UbxMsg):
UbxListener.__init__(self, msg)
self.transceiver = transceiver
self.sema = BoundedSemaphore()
self.msg = UbxMsg()
def on_recv(self, msg: MsgContainer):
if self.is_msg(msg):
data, hdr = frame_parse(msg.data)
msg_parsed = self.parser.from_bytes(data)
msg_parsed = self.msg.from_bytes(data)
self.msg = msg_parsed
self.sema.release()
def poll(self):
frame = frame_create(self.msg.mclass, self.msg.mid, b'')
self.transceiver.send(frame)
self.sema.acquire()
print(self.msg)
def query(self, data: bytes):
frame = frame_create(0x01, 0x35, b'')
self.transceiver.send(frame)
self.sema.acquire()
print(self.msg)