@ -4,9 +4,9 @@ import sys 
		
	
		
			
				from  datetime  import  datetime ,  timedelta from  datetime  import  datetime ,  timedelta  
		
	
		
			
				from  time  import  sleep from  time  import  sleep  
		
	
		
			
				from  typing  import  List from  typing  import  List  
		
	
		
			
				
 
		
	
		
			
				import  sys  
		
	
		
			
				import  os import  os  
		
	
		
			
				import  time  
		
	
		
			
				import  threading   # Import threading module  
		
	
		
			
				
 
		
	
		
			
				# Add the proto directory to the Python path # Add the proto directory to the Python path  
		
	
		
			
				proto_dir  =  os . path . join ( os . path . dirname ( __file__ ) ,  ' message_queue ' ,  ' proto ' ) proto_dir  =  os . path . join ( os . path . dirname ( __file__ ) ,  ' message_queue ' ,  ' proto ' )  
		
	
	
		
			
				
					
						
							 
					
					
						
							 
					
					
				 
				@ -37,9 +37,6 @@ from message_queue.proto.ConnectStatus_pb2 import ConnectStatus 
		
	
		
			
				from  video_streamer.gst_video_streamer  import  GstVideoStreamer from  video_streamer.gst_video_streamer  import  GstVideoStreamer  
		
	
		
			
				import  cv2 import  cv2  
		
	
		
			
				
 
		
	
		
			
				import  os  
		
	
		
			
				import  time   # Ensure time module is imported  
		
	
		
			
				
 
		
	
		
			
				config_manager  =  ConfigManager ( ' config.yaml ' ) config_manager  =  ConfigManager ( ' config.yaml ' )  
		
	
		
			
				rtsp_links  =  config_manager . configs [ ' rtsp_links ' ] . get ( ) rtsp_links  =  config_manager . configs [ ' rtsp_links ' ] . get ( )  
		
	
		
			
				debug  =  config_manager . configs [ ' debug ' ] . get ( ) debug  =  config_manager . configs [ ' debug ' ] . get ( )  
		
	
	
		
			
				
					
					
					
						
							 
					
				 
				@ -65,41 +62,41 @@ class ConnectionTracker: 
		
	
		
			
				# Create an instance of ConnectionTracker # Create an instance of ConnectionTracker  
		
	
		
			
				connection_tracker  =  ConnectionTracker ( ) connection_tracker  =  ConnectionTracker ( )  
		
	
		
			
				
 
		
	
		
			
				def  check_client_connection ( ) :  
		
	
		
			
				    if  not  connection_tracker . is_client_connected ( ) :  
		
	
		
			
				        print ( " Client disconnected. Searching for another client... " )  
		
	
		
			
				        cl_ip ,  _  =  start_discovery_service ( 12345 )  
		
	
		
			
				        ic ( cl_ip )  
		
	
		
			
				
 
		
	
		
			
				        # Reinitialize the Manager with the new client addressimport time  # Ensure time module is imported  
		
	
		
			
				
 
		
	
		
			
				# Helper class to track client connection status  
		
	
		
			
				class  ConnectionTracker :  
		
	
		
			
				class  ConnectionThread ( threading . Thread ) :  
		
	
		
			
				    def  __init__ ( self ) :     def  __init__ ( self ) :  
		
	
		
			
				        self . last_message_time  =  time . time ( )   # Use time.time() to get the current time   
		
	
		
			
				        self . timeout  =  15   # Timeout in seconds (adjust as needed)  
		
	
		
			
				        super ( ) . __init__ ( )  
		
	
		
			
				        self . running  =  True  
		
	
		
			
				
 
		
	
		
			
				    def  update_last_message_time ( self ) :  
		
	
		
			
				        self . last_message_time  =  time . time ( )   # Update with the current time  
		
	
		
			
				    def  run ( self ) :  
		
	
		
			
				        while  self . running :  
		
	
		
			
				            if  not  connection_tracker . is_client_connected ( ) :  
		
	
		
			
				                print ( " Client disconnected. Searching for another client... " )  
		
	
		
			
				                cl_ip ,  _  =  self . start_discovery_service ( 12345 )  
		
	
		
			
				                print ( cl_ip )  
		
	
		
			
				
 
		
	
		
			
				    def  is_client_connected ( self ) :  
		
	
		
			
				        return  ( time . time ( )  -  self . last_message_time )  <  self . timeout   # Use time.time()  
		
	
		
			
				                # Reinitialize the Manager with the new client address  
		
	
		
			
				                global  manager  
		
	
		
			
				                manager  =  Manager ( f " tcp://{cl_ip}:5558 " ,  f " tcp://{cl_ip}:5557 " )  
		
	
		
			
				                manager . start ( manager_callback )   # Restart the Manager and re-register the callback  
		
	
		
			
				
 
		
	
		
			
				# Create an instance of ConnectionTracker  
		
	
		
			
				connection_tracker  =  ConnectionTracker ( )  
		
	
		
			
				                connection_tracker . update_last_message_time ( )   # Reset the timer after reconnecting   
		
	
		
			
				            time . sleep ( 10 )   # Check every 10 seconds   
		
	
		
			
				
 
		
	
		
			
				def  check_client_connection ( ) :  
		
	
		
			
				    if  not  connection_tracker . is_client_connected ( ) :  
		
	
		
			
				        print ( " Client disconnected. Searching for another client... " )  
		
	
		
			
				        cl_ip ,  _  =  start_discovery_service ( 12345 )  
		
	
		
			
				        print ( cl_ip )  
		
	
		
			
				    def  start_discovery_service ( self ,  port ) :  
		
	
		
			
				        sock  =  socket . socket ( socket . AF_INET ,  socket . SOCK_DGRAM )  
		
	
		
			
				        sock . bind ( ( ' 0.0.0.0 ' ,  port ) )  
		
	
		
			
				        print ( f " Discovery service listening on port {port}... " )  
		
	
		
			
				
 
		
	
		
			
				        # Reinitialize the Manager with the new client address  
		
	
		
			
				        global  manager  
		
	
		
			
				        manager  =  Manager ( f " tcp://{cl_ip}:5558 " ,  f " tcp://{cl_ip}:5557 " )  
		
	
		
			
				        manager . start ( manager_callback )   # Restart the Manager and re-register the callback  
		
	
		
			
				        while  True :  
		
	
		
			
				            data ,  addr  =  sock . recvfrom ( 1024 )  
		
	
		
			
				            if  data . decode ( )  ==  " DISCOVER_SERVER " :  
		
	
		
			
				                print ( f " Received discovery request from {addr} " )  
		
	
		
			
				                sock . sendto ( b " SERVER_RESPONSE " ,  addr )  
		
	
		
			
				                break  
		
	
		
			
				        return  addr  
		
	
		
			
				
 
		
	
		
			
				        connection_tracker . update_last_message_time ( )   # Reset the timer after reconnecting  
		
	
		
			
				    def  stop ( self ) :  
		
	
		
			
				        self . running  =  False  
		
	
		
			
				
 
		
	
		
			
				if  __name__  ==  ' __main__ ' : if  __name__  ==  ' __main__ ' :  
		
	
		
			
				    QCoreApplication . setAttribute ( Qt . AA_EnableHighDpiScaling )     QCoreApplication . setAttribute ( Qt . AA_EnableHighDpiScaling )  
		
	
	
		
			
				
					
						
							 
					
					
						
							 
					
					
				 
				@ -193,12 +190,13 @@ if __name__ == '__main__': 
		
	
		
			
				    core . newFrame . connect ( gotNewFrame )     core . newFrame . connect ( gotNewFrame )  
		
	
		
			
				    core . coordsUpdated . connect ( gotCoords )     core . coordsUpdated . connect ( gotCoords )  
		
	
		
			
				
 
		
	
		
			
				    # Set up the QTimer to check client connection every 10 seconds  
		
	
		
			
				    timer  =  QTimer ( )  
		
	
		
			
				    timer . timeout . connect ( check_client_connection )  
		
	
		
			
				    timer . start ( 10000 )   # 10 seconds  
		
	
		
			
				    # Start the connection thread  
		
	
		
			
				    connection_thread  =  ConnectionThread ( )  
		
	
		
			
				    connection_thread . start ( )  
		
	
		
			
				
 
		
	
		
			
				    try :     try :  
		
	
		
			
				        app . exec_ ( )         app . exec_ ( )  
		
	
		
			
				    except  KeyboardInterrupt :     except  KeyboardInterrupt :  
		
	
		
			
				        sys . exit ( 0 )  
		
	
		
			
				        connection_thread . stop ( )  
		
	
		
			
				        connection_thread . join ( )  
		
	
		
			
				        sys . exit ( 0 )