3030 PacketReceived ,
3131)
3232from infuse_iot .socket_comms import (
33+ ClientNotification ,
3334 ClientNotificationConnectionCreated ,
3435 ClientNotificationConnectionFailed ,
3536 ClientNotificationEpacketReceived ,
@@ -64,6 +65,12 @@ def __init__(self, database: DeviceDatabase, server: LocalServer, bleak_mapping:
6465 self ._queues : dict [int , asyncio .Queue ] = {}
6566 self ._tasks : dict [int , asyncio .Task ] = {}
6667
68+ def wrapped_broadcast (self , notifcation : ClientNotification ):
69+ try :
70+ self ._server .broadcast (notifcation )
71+ except OSError as e :
72+ Console .log_error (f"Failed to broadcast notification: { str (e )} " )
73+
6774 def notification_handler (self , _characteristic : BleakGATTCharacteristic , data : bytearray ):
6875 try :
6976 hdr , decr = CtypeBtGattFrame .decrypt (self ._db , None , bytes (data ))
@@ -89,7 +96,7 @@ def notification_handler(self, _characteristic: BleakGATTCharacteristic, data: b
8996 bytes (decr ),
9097 )
9198 Console .log_rx (pkt .ptype , len (data ))
92- self ._server . broadcast (ClientNotificationEpacketReceived (pkt ))
99+ self .wrapped_broadcast (ClientNotificationEpacketReceived (pkt ))
93100
94101 async def create_connection_internal (
95102 self , request : GatewayRequestConnectionRequest , dev : BLEDevice , queue : asyncio .Queue
@@ -118,7 +125,7 @@ async def create_connection_internal(
118125
119126 Console .log_info (f"{ dev } : Connected (MTU { client .mtu_size } )" )
120127
121- self ._server . broadcast (
128+ self .wrapped_broadcast (
122129 ClientNotificationConnectionCreated (
123130 request .infuse_id ,
124131 # ATT header uses 3 bytes of the MTU
@@ -174,7 +181,7 @@ def datagram_received(self, data: bytes, addr: tuple[str | Any, int]):
174181
175182 ble_dev = self ._mapping .get (request .infuse_id , None )
176183 if ble_dev is None :
177- self ._server . broadcast (ClientNotificationConnectionFailed (request .infuse_id ))
184+ self .wrapped_broadcast (ClientNotificationConnectionFailed (request .infuse_id ))
178185 return
179186
180187 # Create queue for further data transfer
@@ -251,7 +258,10 @@ def simple_callback(self, device: BLEDevice, data: AdvertisementData):
251258 Console .log_rx (hdr .type , len (payload ))
252259 pkt = PacketReceived ([hop ], hdr .type , decr )
253260 notification = ClientNotificationEpacketReceived (pkt )
254- self .server .broadcast (notification )
261+ try :
262+ self .server .broadcast (notification )
263+ except OSError as e :
264+ Console .log_error (f"Failed to broadcast notification: { str (e )} " )
255265
256266 async def async_bt_receiver (self ):
257267 loop = asyncio .get_event_loop ()
0 commit comments