a
    YíÌfó  ã                   @   sF   d Z ddlZddlmZ ddlmZ ddlmZmZ G dd„ dƒZ	dS )uÎ  
Backend principal para el Sistema de Lectura AutomÃ¡tica de Medidores.

Este mÃ³dulo gestiona mÃºltiples conexiones de medidores y coordina el sistema general.
Proporciona mÃ©todos para aÃ±adir, eliminar y listar conexiones, asÃ­ como para verificar su estado.

Desarrolladores:
- Para modificar la gestiÃ³n general de conexiones, ajuste los mÃ©todos de la clase MeterReadingBackend
- Si se necesitan nuevas funcionalidades a nivel de sistema, aÃ±Ã¡dalas aquÃ­
é    N)ÚMeterConnection)ÚAlertSystem)ÚForeÚStylec                   @   s>   e Zd Zdd„ Zddd„Zdd„ Zd	d
„ Zdd„ Zdd„ ZdS )ÚMeterReadingBackendc                 C   s   g | _ tƒ | _d S )N)Úconnectionsr   Úalert_system)Úself© r
   ú5/var/www/blue/EV-AMI-Back_02/meter_reading_backend.pyÚ__init__   s    zMeterReadingBackend.__init__Fé<   c                 C   s^   t ||||| j||ƒ}| j |¡ tj| j|fdd� ¡  tt	j
› d|› d|› tj› �ƒ dS )uÜ  
        AÃ±ade una nueva conexiÃ³n de medidor al backend.

        Args:
            host (str): DirecciÃ³n IP del medidor.
            port (int): Puerto del medidor.
            mode (str): Modo de conexiÃ³n ("client" o "server").
            meter_type (str): Tipo de protocolo del medidor.
            interrogation_enabled (bool): Si se debe interrogar activamente al medidor.
            interrogation_frequency (int): Frecuencia de interrogaciÃ³n en segundos.
        T)ÚtargetÚargsZdaemonu   ConexiÃ³n aÃ±adida: ú:N)r   r   r   ÚappendÚ	threadingZThreadÚrun_connectionÚstartÚprintr   ÚGREENr   Ú	RESET_ALL)r	   ÚhostÚportÚmodeÚ
meter_typeÚinterrogation_enabledÚinterrogation_frequencyÚ
connectionr
   r
   r   Úadd_connection   s    z"MeterReadingBackend.add_connectionc                 C   sl   d|  krt | jƒk rRn n4| j |¡}ttj› d|j› d|j› dtj	› �ƒ nttj
› dtj	› �ƒ dS )u‹   
        Elimina una conexiÃ³n por su Ã­ndice.
        
        Args:
            index (int): Ã�ndice de la conexiÃ³n a eliminar.
        r   u
   ConexiÃ³n r   z eliminada.u   Ã�ndice de conexiÃ³n invÃ¡lido.N)Úlenr   Úpopr   r   ZYELLOWr   r   r   r   ÚRED)r	   Úindexr   r
   r
   r   Úremove_connection'   s    (z%MeterReadingBackend.remove_connectionc                 C   sP   t | jƒD ]@\}}ttj› |› d|j› d|j› d|j› d|j› t	j
› �ƒ q
dS )z#Lista todas las conexiones activas.ú: r   ú - N)Ú	enumerater   r   r   ÚCYANr   r   r   r   r   r   )r	   ÚiÚconnr
   r
   r   Úlist_connections5   s    z$MeterReadingBackend.list_connectionsc                 C   sp   t | jƒD ]`\}}|jr*tj› dtj› �ntj› dtj› �}ttj	› |› d|j
› d|j› d|› tj› �	ƒ q
dS )z*Muestra el estado de todas las conexiones.Z	ConectadoZDesconectador%   r   r&   N)r'   r   Z	connectedr   r   r   r   r"   r   r(   r   r   )r	   r)   r*   Ústatusr
   r
   r   r,   :   s    *zMeterReadingBackend.statusc                 C   s   |  ¡  | ¡  dS )u,   Ejecuta una conexiÃ³n de medidor individual.N)ZconnectZhandle_connection)r	   r   r
   r
   r   r   @   s    z"MeterReadingBackend.run_connectionN)Fr   )	Ú__name__Ú
__module__Ú__qualname__r   r   r$   r+   r,   r   r
   r
   r
   r   r      s   
r   )
Ú__doc__r   Zmeter_connectionr   r   r   Zcoloramar   r   r   r
   r
   r
   r   Ú<module>   s
   