From 71a429ebca6e2ab09b4806b7a4b5ec78fc86a5e2 Mon Sep 17 00:00:00 2001 From: Lloyd Date: Mon, 27 Oct 2025 12:45:30 +0000 Subject: [PATCH] Refactor packet handling: replace custom hash function with built-in packet hash method and clean up unused code --- repeater/engine.py | 49 +++++----------------------------------------- 1 file changed, 5 insertions(+), 44 deletions(-) diff --git a/repeater/engine.py b/repeater/engine.py index dd7b4d6..bdbeeb0 100644 --- a/repeater/engine.py +++ b/repeater/engine.py @@ -2,7 +2,7 @@ import asyncio import logging import time from collections import OrderedDict -from typing import Any, Dict, Optional, Tuple +from typing import Optional, Tuple from pymc_core.node.handlers.base import BaseHandler from pymc_core.protocol import Packet @@ -13,48 +13,13 @@ from pymc_core.protocol.constants import ( ROUTE_TYPE_DIRECT, ROUTE_TYPE_FLOOD, ) -from pymc_core.protocol.packet_utils import PacketHeaderUtils +from pymc_core.protocol.packet_utils import PacketHeaderUtils, PacketTimingUtils from repeater.airtime import AirtimeManager logger = logging.getLogger("RepeaterHandler") -class PacketTimingUtils: - - @staticmethod - def estimate_airtime_ms( - packet_length_bytes: int, radio_config: Optional[Dict[str, Any]] = None - ) -> float: - - if radio_config is None: - radio_config = { - "spreading_factor": 10, - "bandwidth": 250000, - "coding_rate": 5, - "preamble_length": 8, - } - - sf = radio_config.get("spreading_factor", 10) - bw = radio_config.get("bandwidth", 250000) # Hz - cr = radio_config.get("coding_rate", 5) - preamble = radio_config.get("preamble_length", 8) - - # LoRa symbol duration: Ts = 2^SF / BW - symbol_duration_ms = (2**sf) / (bw / 1000) - - # Number of payload symbols - payload_symbols = max( - 8, int((8 * packet_length_bytes - 4 * sf + 28 + 16) / (4 * (sf - 2))) * (cr + 4) - ) - - # Total time = preamble + payload - preamble_ms = (preamble + 4.25) * symbol_duration_ms - payload_ms = payload_symbols * symbol_duration_ms - - return preamble_ms + payload_ms - - class RepeaterHandler(BaseHandler): @staticmethod @@ -191,7 +156,7 @@ class RepeaterHandler(BaseHandler): ) # Check if this is a duplicate - pkt_hash = self._packet_hash(packet) + pkt_hash = packet.calculate_packet_hash().hex() is_dupe = pkt_hash in self.seen_packets and not transmitted # Set drop reason for duplicates @@ -281,11 +246,7 @@ class RepeaterHandler(BaseHandler): for k in expired: del self.seen_packets[k] - def _packet_hash(self, packet: Packet) -> str: - if len(packet.payload or b"") >= 8: - return packet.payload[:8].hex() - return (packet.payload or b"").hex() def _get_drop_reason(self, packet: Packet) -> str: @@ -389,7 +350,7 @@ class RepeaterHandler(BaseHandler): def is_duplicate(self, packet: Packet) -> bool: - pkt_hash = self._packet_hash(packet) + pkt_hash = packet.calculate_packet_hash().hex() if pkt_hash in self.seen_packets: logger.debug(f"Duplicate suppressed: {pkt_hash[:16]}") return True @@ -397,7 +358,7 @@ class RepeaterHandler(BaseHandler): def mark_seen(self, packet: Packet): - pkt_hash = self._packet_hash(packet) + pkt_hash = packet.calculate_packet_hash().hex() self.seen_packets[pkt_hash] = time.time() if len(self.seen_packets) > self.max_cache_size: