@@ -20,8 +20,9 @@ class EdgeLinkUdpClient:
2020
2121 def __init__ (self , local_port : int ) -> None :
2222 self .local_port = local_port
23- self ._on_message : list [Callable [[str ], None ]] = []
24- self ._on_error : list [Callable [[Exception ], None ]] = []
23+ self ._on_message : list [Callable [[str ], None ]] = []
24+ self ._on_error : list [Callable [[Exception ], None ]] = []
25+ self ._on_device_status : list [Callable [[bool , str , str ], None ]] = []
2526 self ._queue : deque [str ] = deque ()
2627 self ._transport : asyncio .BaseTransport | None = None
2728 self .is_running = False
@@ -32,6 +33,10 @@ def on_message(self, cb: Callable[[str], None]) -> None:
3233 def on_error (self , cb : Callable [[Exception ], None ]) -> None :
3334 self ._on_error .append (cb )
3435
36+ def on_device_status (self , cb : Callable [[bool , str , str ], None ]) -> None :
37+ """cb(is_connected: bool, endpoint: str, device_id: str) — fired when an upstream device starts/stops sending packets (timeout-based)."""
38+ self ._on_device_status .append (cb )
39+
3540 async def start (self ) -> None :
3641 loop = asyncio .get_running_loop ()
3742 self ._transport , _ = await loop .create_datagram_endpoint (
@@ -52,6 +57,23 @@ def _receive(self, data: bytes, _addr: tuple) -> None:
5257 msg = data .decode (errors = "replace" ).strip ()
5358 if not msg :
5459 return
60+
61+ if msg .startswith ("EDGELINK_STATUS:" ):
62+ # body: "STATUS:protocol@ip" or "STATUS:protocol@ip:deviceId"
63+ body = msg [16 :]
64+ sep = body .find (":" )
65+ status = body [:sep ] if sep >= 0 else body
66+ rest = body [sep + 1 :] if sep >= 0 else ""
67+ connected = status .upper () == "CONNECTED"
68+ dev_sep = rest .rfind (":" )
69+ endpoint = rest [:dev_sep ] if dev_sep >= 0 else rest
70+ device_id = rest [dev_sep + 1 :] if dev_sep >= 0 else ""
71+ for cb in self ._on_device_status :
72+ cb (connected , endpoint , device_id )
73+ return
74+ if msg .startswith ("EDGELINK_" ):
75+ return
76+
5577 self ._queue .append (msg )
5678 for cb in self ._on_message :
5779 cb (msg )
0 commit comments