
    h                     ,    d dl m Z   G d d          ZdS )    )datetimec                   F    e Zd ZdZddZd ZddZd Zd Zd Z	d	 Z
d
 ZdS )SyncControllerz-Handles GPS to RTC synchronization operationsNc                 ~    || _         || _        |pdd| _        d| _        d | _        d| _        d | _        d | _        d S )Nc                 &    t          d|            S )Nz[SYNC] )print)msgcolors     0/home/morepork/Documents/CSEM/sync_controller.py<lambda>z)SyncController.__init__.<locals>.<lambda>	   s    5Q`[^Q`Q`KaKa     Fr   N)rtc
gps_readerstatus_callbacksync_in_progressgps_time_objgps_satssync_success_callbacksync_error_callback)selfr   r   r   s       r   __init__zSyncController.__init__   sS    $.b3a3a3a %  &*"#'   r   c                 "    || _         || _        dS )zUpdate GPS data from GPS readerN)r   r   )r   gps_timer   s      r   update_gps_datazSyncController.update_gps_data   s    $ r   c                    | j         s%|                     dd           |r |d           dS | j        dk     r%|                     dd           |r |d           dS | j        r|                     dd	           dS || _        || _        d
| _        |                     dd	           | j                                         d
S )z
        Initiate GPS to RTC synchronization
        
        Args:
            success_callback: Function to call on successful sync (sync_time)
            error_callback: Function to call on sync error (error)
        zNo GPS time available.redzNo GPS time availableF   zNot enough satellites (need 4+)zNot enough satelliteszSync already in progress...orangeTz"Waiting for GPS second boundary...)r   r   r   r   r   r   r   request_sync)r   success_callbackerror_callbacks      r   sync_rtc_timezSyncController.sync_rtc_time   s       	  !95AAA 867775=1  !BEJJJ 867775  	  !>III5 &6"#1  !%A8LLL 	$$&&&tr   c                 z   	 | j                             |           t          d| d           | j        r|                     |           |                     |           dS # t
          $ rN}t          d|            | j        r|                     |           |                     |           Y d}~dS d}~ww xY w)z<Handle the actual RTC synchronization (called by GPS reader)z[SYNC] Successfully wrote z to RTC at second boundaryz[SYNC ERROR] N)r   
write_timer   r   _sync_success	Exceptionr   _sync_error)r   	sync_timees      r   handle_synczSyncController.handle_sync<   s    	 H	***TyTTTUUU) 6**9555y))))) 	  	  	 %!%%&&&' ,((+++Q	 s   AA" "
B:,AB55B:c                 j    |                     d          }|                     dd           d| _        dS )zHandle successful syncz%Y-%m-%d %H:%M:%SzRTC synced successfully!greenFN)strftimer   r   )r   r)   sync_strs      r   r&   zSyncController._sync_successO   s;    %%&9::7AAA %r   c                 @    |                      dd           d| _        dS )zHandle sync errorzRTC sync failed!r   FN)r   r   )r   errors     r   r(   zSyncController._sync_errorU   s'    /777 %r   c                     | j         S )z&Check if sync is currently in progress)r   r   s    r   is_sync_in_progressz"SyncController.is_sync_in_progressZ   s    $$r   c                 D    | j         | j        du| j        dk    | j        dS )z#Get current sync status informationNr   )in_progressgps_time_availablesufficient_satellitessatellite_count)r   r   r   r3   s    r   get_sync_statuszSyncController.get_sync_status^   s3      0"&"34"?%)]a%7#}	
 
 	
r   r   )NN)__name__
__module____qualname____doc__r   r   r#   r+   r&   r(   r4   r:    r   r   r   r      s        77( ( ( (! ! !
" " " "H     && & && & &
% % %
 
 
 
 
r   r   N)r   r   r?   r   r   <module>r@      sS         b
 b
 b
 b
 b
 b
 b
 b
 b
 b
r   