mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-06-11 11:56:18 +00:00
Improve log tracking event
This commit is contained in:
51
examples/rf_packet_monitor.py
Normal file
51
examples/rf_packet_monitor.py
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import asyncio
|
||||||
|
import argparse
|
||||||
|
from meshcore import MeshCore
|
||||||
|
from meshcore.events import EventType
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
parser = argparse.ArgumentParser(description='MeshCore RF Packet Monitor')
|
||||||
|
parser.add_argument('-p', '--port', required=True, help='Serial port path')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Connect to device
|
||||||
|
print(f"Connecting to {args.port}...")
|
||||||
|
mc = await MeshCore.create_serial(args.port, 115200)
|
||||||
|
|
||||||
|
def handle_rf_packet(event):
|
||||||
|
packet = event.payload
|
||||||
|
if isinstance(packet, dict):
|
||||||
|
print(f"Raw RF packet received:")
|
||||||
|
if 'snr' in packet:
|
||||||
|
print(f" SNR: {packet['snr']:.1f} dB")
|
||||||
|
if 'rssi' in packet:
|
||||||
|
print(f" RSSI: {packet['rssi']} dBm")
|
||||||
|
if 'payload_length' in packet:
|
||||||
|
print(f" Payload length: {packet['payload_length']} bytes")
|
||||||
|
if 'payload' in packet:
|
||||||
|
print(f" Payload (hex): {packet['payload']}")
|
||||||
|
else:
|
||||||
|
print(f"RF packet received: {packet}")
|
||||||
|
|
||||||
|
# Subscribe to RF log data
|
||||||
|
subscription = mc.subscribe(EventType.RX_LOG_DATA, handle_rf_packet)
|
||||||
|
|
||||||
|
print("Waiting for log data (press Ctrl+C to exit)...")
|
||||||
|
try:
|
||||||
|
# Keep the script running to receive logs
|
||||||
|
while True:
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\nExiting...")
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
mc.unsubscribe(subscription)
|
||||||
|
await mc.disconnect()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: {e}")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
asyncio.run(main())
|
||||||
@@ -31,6 +31,7 @@ class EventType(Enum):
|
|||||||
STATUS_RESPONSE = "status_response"
|
STATUS_RESPONSE = "status_response"
|
||||||
LOG_DATA = "log_data"
|
LOG_DATA = "log_data"
|
||||||
TRACE_DATA = "trace_data"
|
TRACE_DATA = "trace_data"
|
||||||
|
RX_LOG_DATA = "rx_log_data"
|
||||||
|
|
||||||
# Command response types
|
# Command response types
|
||||||
OK = "command_ok"
|
OK = "command_ok"
|
||||||
|
|||||||
@@ -227,8 +227,34 @@ class MessageReader:
|
|||||||
await self.dispatcher.dispatch(Event(EventType.STATUS_RESPONSE, res))
|
await self.dispatcher.dispatch(Event(EventType.STATUS_RESPONSE, res))
|
||||||
|
|
||||||
elif packet_type_value == PacketType.LOG_DATA.value:
|
elif packet_type_value == PacketType.LOG_DATA.value:
|
||||||
logger.debug("Received log data")
|
logger.debug(f"Received RF log data: {data.hex()}")
|
||||||
await self.dispatcher.dispatch(Event(EventType.LOG_DATA, data[1:].decode('utf-8', errors='replace')))
|
|
||||||
|
# Parse as raw RX data
|
||||||
|
log_data = {
|
||||||
|
"raw_hex": data[1:].hex()
|
||||||
|
}
|
||||||
|
|
||||||
|
# First byte is SNR (signed byte, multiplied by 4)
|
||||||
|
if len(data) > 1:
|
||||||
|
snr_byte = data[1]
|
||||||
|
# Convert to signed value
|
||||||
|
snr = (snr_byte if snr_byte < 128 else snr_byte - 256) / 4.0
|
||||||
|
log_data["snr"] = snr
|
||||||
|
|
||||||
|
# Second byte is RSSI (signed byte)
|
||||||
|
if len(data) > 2:
|
||||||
|
rssi_byte = data[2]
|
||||||
|
# Convert to signed value
|
||||||
|
rssi = rssi_byte if rssi_byte < 128 else rssi_byte - 256
|
||||||
|
log_data["rssi"] = rssi
|
||||||
|
|
||||||
|
# Remaining bytes are the raw data payload
|
||||||
|
if len(data) > 3:
|
||||||
|
log_data["payload"] = data[3:].hex()
|
||||||
|
log_data["payload_length"] = len(data) - 3
|
||||||
|
|
||||||
|
# Dispatch as RF log data
|
||||||
|
await self.dispatcher.dispatch(Event(EventType.RX_LOG_DATA, log_data))
|
||||||
|
|
||||||
elif packet_type_value == PacketType.TRACE_DATA.value:
|
elif packet_type_value == PacketType.TRACE_DATA.value:
|
||||||
logger.debug(f"Received trace data: {data.hex()}")
|
logger.debug(f"Received trace data: {data.hex()}")
|
||||||
|
|||||||
Reference in New Issue
Block a user