refactored
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
from msg_container import MsgContainer
|
||||
from msg_listener import MsgListener
|
||||
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):
|
||||
MsgListener.__init__(self)
|
||||
self.parser = parser
|
||||
self.mclass = mclass
|
||||
self.mid = mid
|
||||
|
||||
def on_recv(self, msg: MsgContainer):
|
||||
data, hdr = frame_parse(msg.data)
|
||||
msg_parsed = self.parser.from_bytes(data)
|
||||
print(f"{msg.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
|
||||
else:
|
||||
return True
|
||||
return False
|
||||
@@ -0,0 +1,3 @@
|
||||
from ubx.messages.msg import UbxMsg
|
||||
from ubx.messages.nav import Sig, Sat
|
||||
from ubx.messages.rxm import RawX
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
import struct
|
||||
import ubx.msg
|
||||
from ubx.messages.msg import UbxMsg
|
||||
|
||||
class Sig(ubx.msg.UbxMsg):
|
||||
class Sig(UbxMsg):
|
||||
class Group1:
|
||||
@classmethod
|
||||
def from_bytes(cls, data: bytes):
|
||||
@@ -38,7 +38,7 @@ class Sig(ubx.msg.UbxMsg):
|
||||
obj.signals.append(signal)
|
||||
return obj.__dict__
|
||||
|
||||
class Sat(ubx.msg.UbxMsg):
|
||||
class Sat(UbxMsg):
|
||||
class Group1:
|
||||
@classmethod
|
||||
def from_bytes(cls, data: bytes):
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
import struct
|
||||
import ubx.msg
|
||||
from ubx.messages.msg import UbxMsg
|
||||
|
||||
class RawX(ubx.msg.UbxMsg):
|
||||
class RawX(UbxMsg):
|
||||
class Group1:
|
||||
@classmethod
|
||||
def from_bytes(cls, data: bytes):
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
import struct
|
||||
from msg_container import MsgContainer
|
||||
from msg_listener import MsgListener
|
||||
from msg_talker import MsgTalker
|
||||
from ubx.ubx import UBX_SYNC_WORD, frame_parse, frame_create
|
||||
from ubx.listener import UbxListener
|
||||
|
||||
UBX_PKT_DEBUG = False
|
||||
def ubx_pkt_debug(s: str):
|
||||
if UBX_PKT_DEBUG:
|
||||
print(s)
|
||||
|
||||
class UbxPacketizer(MsgListener, MsgTalker):
|
||||
def __init__(self, sync_bytes: bytes = UBX_SYNC_WORD):
|
||||
MsgListener.__init__(self)
|
||||
MsgTalker.__init__(self)
|
||||
self.sync_bytes = sync_bytes
|
||||
self.packet = b''
|
||||
self.wait_sync = True
|
||||
self.sync_count = 0
|
||||
self.timestamp = 0
|
||||
self.d_save = b''
|
||||
|
||||
def reset(self):
|
||||
self.packet = b''
|
||||
self.wait_sync = True
|
||||
self.sync_count = 0
|
||||
|
||||
def on_recv(self, msg: MsgContainer):
|
||||
for n in range (0, len(msg.data)):
|
||||
d = msg.data[n]
|
||||
if d == self.sync_bytes[self.sync_count]:
|
||||
self.d_save += struct.pack('B', d)
|
||||
self.sync_count += 1
|
||||
if self.sync_count == len(self.sync_bytes):
|
||||
ubx_pkt_debug(f"{msg.name}:UBX-sync")
|
||||
if len(self.packet) > 0:
|
||||
ubx_pkt_debug(f"{msg.name}:Remaining packet: {self.packet}")
|
||||
self.sync_count = 0
|
||||
self.d_save = b''
|
||||
self.packet = b''
|
||||
self.wait_sync = False
|
||||
self.timestamp = msg.timestamp
|
||||
else:
|
||||
if self.sync_count > 0:
|
||||
self.packet += self.d_save
|
||||
self.d_save = b''
|
||||
self.sync_count = 0
|
||||
if not self.wait_sync:
|
||||
self.packet += struct.pack('B', d)
|
||||
data, hdr = frame_parse(self.packet)
|
||||
if data is not None:
|
||||
if hdr.length == len(data):
|
||||
self.call_listener(MsgContainer(msg.name, self.timestamp, self.packet))
|
||||
self.packet = b''
|
||||
self.wait_sync = True
|
||||
|
||||
def is_msg(self, msg: MsgContainer):
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
UBX_PKT_DEBUG = True
|
||||
class MySink(MsgListener):
|
||||
def __init__(self):
|
||||
MsgListener.__init__(self)
|
||||
|
||||
def on_recv(self, msg: MsgContainer):
|
||||
print(msg)
|
||||
|
||||
def is_msg(self, msg: MsgContainer):
|
||||
return True
|
||||
|
||||
ubx_rx = UbxListener('test', 1, 2)
|
||||
|
||||
pkt = UbxPacketizer()
|
||||
pkt.register_listener(ubx_rx)
|
||||
|
||||
pkt.reset()
|
||||
frame = frame_create(_class=1, _id=2, _data=b'Hallo' + b'\xb5' + b',\x62Welt') + frame_create(_class=1, _id=2, _data=b',') + frame_create(_class=1, _id=2, _data=b'UBX') + frame_create(_class=1, _id=2, _data=b'World')
|
||||
pkt.on_recv(MsgContainer('test', 31101970.0208, frame))
|
||||
+3
-3
@@ -1,8 +1,8 @@
|
||||
from msg_container import MsgContainer
|
||||
from ubx.ubx import frame_parse, frame_parse_hdr, frame_create
|
||||
from ubx.msg import UbxMsg
|
||||
from ubx.ubx import frame_parse, frame_create
|
||||
from ubx.messages.msg import UbxMsg
|
||||
from transceiver import Transceiver
|
||||
from receiver.ubx_listener import UbxListener
|
||||
from ubx.listener import UbxListener
|
||||
from threading import BoundedSemaphore
|
||||
|
||||
class UbxQuery(UbxListener):
|
||||
|
||||
Reference in New Issue
Block a user