只有hello包实现多跳,还没加入业务数据
具体的还要看opencode和gpt记录接着优化
This commit is contained in:
@@ -2,11 +2,12 @@
|
||||
Packet model for LoRa route simulation.
|
||||
|
||||
Defines packet types and structure for HELLO, DATA, and ACK packets.
|
||||
Includes path tracing for multi-hop verification.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from enum import IntEnum
|
||||
from typing import Optional
|
||||
from typing import Optional, List
|
||||
|
||||
|
||||
class PacketType(IntEnum):
|
||||
@@ -20,7 +21,7 @@ class PacketType(IntEnum):
|
||||
@dataclass
|
||||
class Packet:
|
||||
"""
|
||||
LoRa packet structure.
|
||||
LoRa packet structure with path tracing.
|
||||
|
||||
Attributes:
|
||||
type: Packet type (HELLO, DATA, or ACK)
|
||||
@@ -28,6 +29,7 @@ class Packet:
|
||||
dst: Destination node ID (-1 for broadcast)
|
||||
seq: Sequence number
|
||||
hop: Current hop count
|
||||
path: List of node IDs traversed (for multi-hop verification)
|
||||
payload: Optional payload data
|
||||
rssi: Received signal strength indicator (set on receive)
|
||||
"""
|
||||
@@ -37,15 +39,26 @@ class Packet:
|
||||
dst: int
|
||||
seq: int
|
||||
hop: int = 0
|
||||
path: List[int] = None # Path trace for observability
|
||||
payload: Optional[str] = None
|
||||
rssi: Optional[float] = None # Set by receiver
|
||||
rssi: Optional[float] = None
|
||||
|
||||
def __post_init__(self):
|
||||
"""Initialize path if not provided."""
|
||||
if self.path is None:
|
||||
self.path = [self.src]
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (
|
||||
f"Packet({self.type.name}, src={self.src}, dst={self.dst}, "
|
||||
f"seq={self.seq}, hop={self.hop})"
|
||||
f"seq={self.seq}, hop={self.hop}, path={self.path})"
|
||||
)
|
||||
|
||||
def add_hop(self, node_id: int):
|
||||
"""Add a node to the path and increment hop count."""
|
||||
self.hop += 1
|
||||
self.path.append(node_id)
|
||||
|
||||
@property
|
||||
def is_broadcast(self) -> bool:
|
||||
"""Check if packet is broadcast (dst = -1)."""
|
||||
@@ -66,6 +79,11 @@ class Packet:
|
||||
"""Check if packet is an ACK packet."""
|
||||
return self.type == PacketType.ACK
|
||||
|
||||
@property
|
||||
def path_length(self) -> int:
|
||||
"""Get the path length (number of hops)."""
|
||||
return len(self.path) - 1 if self.path else 0
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Convert packet to dictionary for serialization."""
|
||||
return {
|
||||
@@ -74,6 +92,7 @@ class Packet:
|
||||
"dst": self.dst,
|
||||
"seq": self.seq,
|
||||
"hop": self.hop,
|
||||
"path": self.path,
|
||||
"payload": self.payload,
|
||||
"rssi": self.rssi,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user