o
    ìcj                     @   s   d dl Z d dlmZmZ d dlmZ d dlmZ d dlm	Z	 e 
eZe ZG dd deZG dd	 d	eZG d
d deZG dd deZG dd deZde	defddZdS )    N)	APIRouterHTTPException)	BaseModel)	CallState)CallServicec                   @      e Zd ZU eed< dS )CallRequestnumberN__name__
__module____qualname__str__annotations__ r   r   4/var/www/html/bot-asterisk-ari/backend/api/routes.pyr         
 r   c                   @   r   )HangupRequestcall_idNr
   r   r   r   r   r      r   r   c                   @   s&   e Zd ZU eed< eed< eed< dS )AgentConnectRequestusername	extensionpasswordNr
   r   r   r   r   r      s   
 r   c                   @   s   e Zd ZU eed< eed< dS )WebRtcOfferRequestsdptypeNr
   r   r   r   r   r      s   
 r   c                   @   s>   e Zd ZU dZedB ed< dZedB ed< dZedB ed< dS )WebRtcIceRequestN	candidatesdp_midsdp_mline_index)	r   r   r   r   r   r   r   r   intr   r   r   r   r   !   s   
 r   call_servicereturnc                    s\  t ddtdtf fdd}t ddtdtf fdd}t d	dtf fd
d}t ddtdtf fdd}t ddtdtf fdd}t ddtdtf fdd}t ddtf fdd}t ddtf fdd}t ddtf fdd}	t ddtdt	dtf fd d!}
t d"dtdt
dtf fd#d$}t S )%Nz/callbodyr"   c                    s"     | jI d H }|j|jdS )Nr   status)start_outboundr	   r   r%   )r#   callr!   r   r   
start_call(   s   z%create_api_router.<locals>.start_callz/hangupc                    s,     | jI d H }|stdddddiS )N  Call not foundstatus_codedetailokT)hangupr   r   )r#   r/   r(   r   r   hangup_call-   s
   z&create_api_router.<locals>.hangup_callz/callsc                     s      } ddd | D iS )Ncallsc                 S   s   g | ]}|  qS r   )	to_public).0cr   r   r   
<listcomp>7   s    z9create_api_router.<locals>.list_calls.<locals>.<listcomp>)
list_calls)r2   r(   r   r   r7   4   s   z%create_api_router.<locals>.list_callsz/calls/{call_id}/answerr   c              
      sj   z
  | I d H }W n" ty   tdddd  ty- } z	tdt|d|d }~ww |j|jdS )Nr*   r+   r,     r$   )answer_inboundKeyErrorr   
ValueErrorr   r   r%   )r   r'   excr(   r   r   answer_call9   s   z&create_api_router.<locals>.answer_callz/calls/{call_id}c                    s     | }|stddd|jdkr)|js)|jdvr) | I d H }|r'|}|S |jdkrP|jdv rP|jsP jjrPz | I d H }W |S  t	yO   Y |S w |S )Nr*   r+   r,   outbound)endedfailedinbound)answeredtalking)
get_callr   	directionexternal_media_attachedr%   sync_outbound_mediasettingswebrtc_enabledensure_webrtc_readyr:   )r   r'   syncedr(   r   r   rD   C   s4   




z#create_api_router.<locals>.get_callz/agent/connectc              
      sr   z  | j| j| jI d H W S  ty$ } z	tdt|d|d }~w ty8 } z	tdt|d|d }~ww )Nr8   r,     )register_agentr   r   r   r;   r   r   RuntimeError)r#   r<   r(   r   r   agent_connect\   s   z(create_api_router.<locals>.agent_connectz/agent/statusc                      s
      S )N)get_agent_statusr   r(   r   r   agent_statusi   s   z'create_api_router.<locals>.agent_statusz/healthc                     sn      I d H }  j}| r|js|   j}|rdn| rdnd}d| | | j|j j| |j jj	 
 dS )N	websockethttpoffliner/   )r%   ari_operationalari_connectedari_modeari_ws_connectedari_ws_task_aliveari_events_recentari_reachableari_last_errorrI   agent)ari_healthyari_listener
task_alivestartari_ws_healthyrV   rZ   
last_errorrH   rI   rP   )ari_oklistenerws_okmoder(   r   r   healthm   s&   
z!create_api_router.<locals>.healthz/webrtc/configc                      s    j j j dS )N)enabledice_servers)rH   rI   media_managerrj   r   r(   r   r   webrtc_config   s   z(create_api_router.<locals>.webrtc_configz/calls/{call_id}/webrtc/offerc              
      s   j jstddd | }|stdddz | I d H } j| |j|jI d H }W n6 t	y=   tdddd  t
yQ } z	tdt|d|d }~w tye } z	tdt|d|d }~ww d|_ j| }td	| ||j  |I d H  |S )
Nr8   WebRTC deshabilitador,   r*   r+   rL   i  Tz5WebRTC offer OK call_id=%s (RTP puerto %s, bridge=%s))rH   rI   r   rD   rJ   rk   apply_offerr   r   r:   rN   r   	Exceptionwebrtc_connectedrtp_portloggerinfo	bridge_id_notify)r   r#   r'   answerr<   portr(   r   r   webrtc_offer   s<   

z'create_api_router.<locals>.webrtc_offerz/calls/{call_id}/webrtc/icec                    s\    j jstdddz j| |j|j|jI d H  W ddiS  ty-   tdddd w )Nr8   rm   r,   r*   zCall session not foundr/   T)	rH   rI   r   rk   add_ice_candidater   r   r   r:   )r   r#   r(   r   r   
webrtc_ice   s   z%create_api_router.<locals>.webrtc_ice)routerpostr   dictr   getr   r   r   r   r   )r!   r)   r1   r7   r=   rD   rO   rQ   rh   rl   rx   rz   r   r(   r   create_api_router'   s.   	r   )loggingfastapir   r   pydanticr   calls.modelsr   services.call_servicer   	getLoggerr   rr   r{   r   r   r   r   r   r   r   r   r   r   <module>   s    
