Files
meshcore-bot/tests/unit/test_packet_capture_stats.py
Treehouse-00 f45124b6a6 feat(packet_capture): include the radio's packet counters in status stats
refresh_stats() asks the radio for core and radio stats only, so status
messages carry no packet counters, and dashboards that show an
observer's received and sent counts have none for the bot. The service
was ported from meshcore-packet-capture in December 2025;
packet-capture added packet stats in June 2026 (3699420), after the
port.

Fetch them with get_stats_packets() the same way as the other two, and
publish them as meshcore-packet-capture does: the library's own keys
(recv, sent, flood_tx, direct_tx, flood_rx, direct_rx, recv_errors)
plus packets_received and packets_sent, the names
meshcore-packet-capture and openhop_repeater publish. The status
example in docs/packet-capture.md showed rx_packets and tx_packets,
names the bot never published; it now shows the real ones.

get_stats_packets() is in every meshcore release the bot allows: it
arrived in 2.2.8 with the core and radio commands. Companion firmware
answers all three from 1.11.0. Older firmware rejects the request with
an immediate error, as it already rejects the other two.
2026-11-01 16:18:10 -08:00

51 lines
1.8 KiB
Python

"""Status stats carry the radio's packet counters, as meshcore-packet-capture's do."""
from __future__ import annotations
import asyncio
import logging
import types
from meshcore import EventType
from meshcore.events import Event
from modules.service_plugins.packet_capture_service import PacketCaptureService
def build_service(commands):
"""Build a minimal PacketCaptureService without running __init__."""
service = object.__new__(PacketCaptureService)
service.bot = types.SimpleNamespace(meshcore=types.SimpleNamespace(is_connected=True, commands=commands))
service.logger = logging.getLogger("test-packet-capture-stats")
service.debug = False
service.stats_status_enabled = True
service.stats_refresh_interval = 300
service.stats_capability_state = None
service.stats_fetch_lock = asyncio.Lock()
service.latest_stats = None
service.last_stats_fetch = 0
return service
async def test_status_stats_include_packet_counters():
core = {"battery_mv": 4100, "uptime_secs": 600, "errors": 0, "queue_len": 0}
radio = {"noise_floor": -110, "tx_air_secs": 12, "rx_air_secs": 340}
# The keys meshcore_py parses from a STATS_TYPE_PACKETS frame
packets = {"recv": 15, "sent": 9, "flood_tx": 6, "direct_tx": 3, "flood_rx": 14, "direct_rx": 1, "recv_errors": 7}
async def get_stats_core():
return Event(EventType.STATS_CORE, core)
async def get_stats_radio():
return Event(EventType.STATS_RADIO, radio)
async def get_stats_packets():
return Event(EventType.STATS_PACKETS, packets)
commands = types.SimpleNamespace(
get_stats_core=get_stats_core, get_stats_radio=get_stats_radio, get_stats_packets=get_stats_packets
)
stats = await build_service(commands).refresh_stats(force=True)
assert stats == {**core, **radio, **packets, "packets_sent": 9, "packets_received": 15}