3939# Constants
4040
4141LOG_LINE_LIMIT = 50000
42+ CONNECT_STATUS_PARAM_THROTTLE_SECS = 0.2
4243
4344
4445DATASTREAM_RATES = {
@@ -105,6 +106,8 @@ def __init__(
105106
106107 self .connectionError : Optional [str ] = None
107108 self ._last_connect_progress : float = 0.0
109+ self ._last_param_progress_emit_time : float = 0.0
110+ self ._last_connect_status_payload : Optional [dict ] = None
108111
109112 self .connection_phases = [
110113 "Waiting for heartbeat" ,
@@ -299,10 +302,33 @@ def setupControllers(self) -> None:
299302 self .navController = NavController (self )
300303 self .ftpController = FtpController (self )
301304
302- def sendParamFetchConnectionStatusUpdate (self , data : dict ) -> None :
305+ def _emitConnectionStatus (
306+ self , message : str , progress : float , sub_message : str = ""
307+ ) -> None :
303308 if not self .droneConnectStatusCb :
304309 return
305310
311+ progress = round (min (max (float (progress ), 0.0 ), 100.0 ), 2 )
312+ progress = max (progress , self ._last_connect_progress )
313+ payload = {
314+ "message" : message ,
315+ "sub_message" : sub_message ,
316+ "progress" : progress ,
317+ }
318+
319+ if payload == self ._last_connect_status_payload :
320+ return
321+
322+ try :
323+ self .droneConnectStatusCb (payload )
324+ except Exception :
325+ self .logger .exception ("Connection status callback failed" )
326+ return
327+
328+ self ._last_connect_status_payload = payload .copy ()
329+ self ._last_connect_progress = progress
330+
331+ def sendParamFetchConnectionStatusUpdate (self , data : dict ) -> None :
306332 total_params = max (int (data .get ("total_number_of_params" , 0 )), 1 )
307333 current_index = max (int (data .get ("current_param_index" , 0 )) + 1 , 1 )
308334 current_index = min (current_index , total_params )
@@ -314,16 +340,23 @@ def sendParamFetchConnectionStatusUpdate(self, data: dict) -> None:
314340 if current_param_id :
315341 sub_message = f"{ sub_message } : { current_param_id } "
316342
317- self .droneConnectStatusCb (
318- {
319- "message" : "Fetching Params" ,
320- "sub_message" : sub_message ,
321- "progress" : progress ,
322- }
343+ now = time .monotonic ()
344+ is_final_update = current_index >= total_params
345+ if (
346+ not is_final_update
347+ and now - self ._last_param_progress_emit_time
348+ < CONNECT_STATUS_PARAM_THROTTLE_SECS
349+ ):
350+ return
351+
352+ self ._emitConnectionStatus (
353+ message = "Fetching Params" ,
354+ sub_message = sub_message ,
355+ progress = progress ,
323356 )
324- self ._last_connect_progress = progress
357+ self ._last_param_progress_emit_time = now
325358
326- def sendConnectionStatusUpdate (self , msg_index ) :
359+ def sendConnectionStatusUpdate (self , msg_index : int ) -> None :
327360 total_msgs = len (self .connection_phases )
328361 if msg_index < 0 or msg_index >= total_msgs :
329362 self .logger .error (f"Invalid connection status index { msg_index } " )
@@ -332,21 +365,8 @@ def sendConnectionStatusUpdate(self, msg_index):
332365 msg = self .connection_phases [msg_index ]
333366
334367 # Do not regress progress during non-fetch stages.
335- progress = (
336- 100.0
337- if msg_index == total_msgs - 1
338- else float (getattr (self , "_last_connect_progress" , 0.0 ))
339- )
340- self ._last_connect_progress = progress
341-
342- if self .droneConnectStatusCb :
343- self .droneConnectStatusCb (
344- {
345- "message" : msg ,
346- "sub_message" : "" ,
347- "progress" : progress ,
348- }
349- )
368+ progress = 100.0 if msg_index == total_msgs - 1 else self ._last_connect_progress
369+ self ._emitConnectionStatus (message = msg , sub_message = "" , progress = progress )
350370
351371 @staticmethod
352372 def checkBaudrateValid (baud : int ) -> bool :
0 commit comments