
    *i%                         d Z ddlZddlmZ ddlmZmZmZmZm	Z	m
Z
 ddlmZ h dZerddlmZ ded	eeef         fd
Z G d d          ZdS )a  Camera configuration management.

Maps semantic camera names to hardware serial numbers, camera types, and roles.

**Format**::

    {
        "scene_1":       {"serial": 37038161,       "type": "zed",        "role": "scene"},
        "scene_2":       {"serial": 55667788,       "type": "zed",        "role": "scene"},
        "left_wrist":    {"serial": "123456789012", "type": "realsense",  "role": "left_wrist"},
        "right_wrist":   {"serial": 14932342,       "type": "zed",        "role": "right_wrist"}
    }

Roles
-----
- ``"scene"``       : fixed overhead / scene camera (multiple allowed)
- ``"left_wrist"``  : wrist camera on the left arm (at most one)
- ``"right_wrist"`` : wrist camera on the right arm (at most one)

**Legacy format** (ZED only, still fully supported)::

    {
        "scene_camera":        37038161,
        "left_wrist_camera":   16522755
    }

Integer values are assumed to be ZED cameras with no role assigned.
    N)Path)TYPE_CHECKINGAnyDictListOptionalUnion)CAMERA_CONFIG>   scene
left_wristright_wrist)Cameraentryreturnc                     t          | t                    rd| fS t          | t                    r2|                     dd                                          }|| d         fS t          d|           )zEReturn (camera_type, serial) from a config entry (old or new format).zedtypeserialzInvalid camera config entry: )
isinstanceintdictgetlower
ValueError)r   cam_types     2/home/robot-lab/raiden_cmu/raiden/camera_config.py_parse_entryr   *   su    % e|% )99VU++1133x((
>U>>
?
??    c                      e Zd ZdZefdefdZddZddZdede	e
eef                  fd	Zdede	e         fd
Zde
eef         de	e         fdZdede	e         fdZdede	e         fdZdedee         fdZdeeef         fdZdee         fdZ	 	 ddede
eef         dede	e         ddf
dZdedefdZdeddfdZdeeef         fdZedeeef         fd            Zdeeef         fdZdS )CameraConfigzEManages camera configuration mapping semantic names to hardware info.config_filec                    t          |          | _        | j        j                            dd           | j                                        r[t          | j                  5 }t          j        |          | _        d d d            n# 1 swxY w Y   | 	                                 d S i | _        | 
                                 d S )NT)parentsexist_ok)r   r!   parentmkdirexistsopenjsonloadcameras_warn_invalid_roles_save)selfr!   fs      r   __init__zCameraConfig.__init__7   s    ,,%%dT%BBB""$$ 	d&'' <1/3y||< < < < < < < < < < < < < < <$$&&&&&DLJJLLLLLs   #B		BBr   Nc                     t          | j        d          5 }t          j        | j        |d           d d d            d S # 1 swxY w Y   d S )Nw   )indent)r(   r!   r)   dumpr+   )r.   r/   s     r   r-   zCameraConfig._saveG   s    $"C(( 	1AIdlAa0000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s   A  AAc                     | j                                         D ]b\  }}t          |t                    rHd|v rD|d         }|t          vr3t          d| d| j         d| dt          t                     d	          cd S )NroleCamera 'z' in z has invalid role ''. Valid roles: .)r+   itemsr   r   _VALID_ROLESr   r!   sorted)r.   namer   r7   s       r   r,   z CameraConfig._warn_invalid_rolesK   s    <--// 	 	KD%%&& 6U??V}|++$J4 J Jd.> J J J J282F2FJ J J  		 	r   r>   c                 f    | j                             |          }|dS t          |          \  }}|S )zLReturn the serial number for a camera name (int for ZED, str for RealSense).Nr+   r   r   )r.   r>   r   _r   s        r   get_serial_by_namezCameraConfig.get_serial_by_nameY   s7      &&=4 ''	6r   c                 f    | j                             |          }|dS t          |          \  }}|S )zGReturn 'zed' or 'realsense' for the named camera, or None if not found.Nr@   )r.   r>   r   r   rA   s        r   get_camera_typezCameraConfig.get_camera_typea   s7      &&=4"5))!r   r   c                     | j                                         D ];\  }}t          |          \  }}t          |          t          |          k    r|c S <d S N)r+   r;   r   str)r.   r   r>   r   rA   entry_serials         r   get_name_by_serialzCameraConfig.get_name_by_seriali   s`    <--// 	 	KD%*511OA|<  CKK// 0tr   c                     | j                             |          }t          |t                    r|                    d          S dS )zMReturn the role for a camera ('scene', 'left_wrist', 'right_wrist'), or None.r7   N)r+   r   r   r   )r.   r>   r   s      r   get_rolezCameraConfig.get_rolep   s@      &&eT"" 	%99V$$$tr   r7   c                     | j                                         D ]7\  }}t          |t                    r|                    d          |k    r|c S 8dS )zReturn the name of the unique camera with the given role, or None.

        Use for 'left_wrist' and 'right_wrist' (at most one each).
        For 'scene' cameras use get_cameras_by_role().
        r7   N)r+   r;   r   r   r   )r.   r7   r>   r   s       r   get_camera_by_rolezCameraConfig.get_camera_by_rolew   s\      <--// 	 	KD%%&& 599V+<+<+D+Dtr   c                 N    fd| j                                         D             S )zPReturn names of all cameras with the given role (e.g. multiple 'scene' cameras).c                 v    g | ]5\  }}t          |t                    r|                    d           k    3|6S )r7   )r   r   r   ).0r>   r   r7   s      r   
<listcomp>z4CameraConfig.get_cameras_by_role.<locals>.<listcomp>   sR     
 
 
e%&&
 ,199V+<+<+D+D +D+D+Dr   )r+   r;   )r.   r7   s    `r   get_cameras_by_rolez CameraConfig.get_cameras_by_role   s<    
 
 
 
#|1133
 
 
 	
r   c                 4    | j                                         S )z%Return a copy of the raw config dict.)r+   copyr.   s    r   list_cameraszCameraConfig.list_cameras   s    |  """r   c                 N    t          | j                                                  S rF   )listr+   keysrU   s    r   list_camera_nameszCameraConfig.list_camera_names   s    DL%%''(((r   r   camera_typec           
          |4|t           vr+t          d| d| dt          t                      d          ||d}|||d<   || j        |<   |                                  dS )a@  Add or update a camera entry.

        Args:
            name: Unique camera name (key in the config).
            serial: Hardware serial number (int for ZED, str for RealSense).
            camera_type: 'zed' or 'realsense'.
            role: 'scene', 'left_wrist', or 'right_wrist'. Optional but recommended.
        NzInvalid role '' for camera 'r9   r:   )r   r   r7   )r<   r   r=   r+   r-   )r.   r>   r   r[   r7   r   s         r   
add_camerazCameraConfig.add_camera   s     L 8 88 8 8T 8 8 &| 4 48 8 8   ,2; G G E&M"T

r   c                 T    || j         v r| j         |= |                                  dS dS )NTF)r+   r-   )r.   r>   s     r   remove_camerazCameraConfig.remove_camera   s0    4<T"JJLLL4ur   r   c                    | j                             |          }|t          d| d| j         d          t	          |          \  }}t          |t                    r|                    dd          nd}|dk    r!dd	lm}  ||t          |          |
          S |dk    r!ddl
m}  ||t          |          |
          S t          d| d| d          )u   Create and return a Camera instance for the named camera.

        Does NOT call open() – the caller is responsible for that.

        Raises:
            ValueError: if the name is not in the config or the type is unknown.
        Nr8   z' not found in config ()fps   r   r   )	ZedCamera)rc   	realsense)RealSenseCamerazUnknown camera type 'r]   z&'. Supported types: 'zed', 'realsense')r+   r   r   r!   r   r   r   raiden.cameras.zedre   r   raiden.cameras.realsenserg   rG   )r.   r>   r   r   r   rc   re   rg   s           r   create_camerazCameraConfig.create_camera   s,      &&=K4KK8HKKK   (..&+5eT+B+BJ599UB'''u4444449T3v;;C8888{""@@@@@@"?4V#>>>>2H 2 2D 2 2 2
 
 	
r   c                     ddl m} d |j                                        D             }i }| j                                        D ]7\  }}t          |          \  }}|dk    rt          |          |v ||<   2d||<   8|S )z;Check which configured ZED cameras are currently connected.r   Nc                     h | ]	}|j         
S  )serial_number)rP   cams     r   	<setcomp>z9CameraConfig.validate_against_hardware.<locals>.<setcomp>   s    VVV3S.VVVr   r   T)pyzed.slslr   get_device_listr+   r;   r   r   )r.   rr   connected_serialsresultr>   r   r   r   s           r   validate_against_hardwarez&CameraConfig.validate_against_hardware   s    VV"):S:S:U:UVVV<--// 	$ 	$KD%+E22Hf5  "6{{.??t  $tr   c                      ddl m}  i }| j                                        D ]:}t	          |j                  |j        |j        | j        j	        k    d||j
        <   ;|S )z7Detect all connected ZED cameras and return their info.r   N)modelid	available)rq   rr   r   rs   rG   camera_modelry   camera_stateCAMERA_STATE	AVAILABLErn   )rr   detectedcam_infos      r   detect_cameraszCameraConfig.detect_cameras   sy     		1133 	 	HX233k%2bo6OO0 0HX+,,
 r   c                 
   |                                  }|si S t          |                                          }i }g d}t          |          D ]3\  }}|t	          |          k     r||         nd| df\  }}|d|d||<   4|S )z7Suggest a default config based on detected ZED cameras.))scene_camerar   )right_wrist_camerar   )left_wrist_camerar   camera_r   r   )r   r   r7   )r   rX   rY   	enumeratelen)	r.   r   serials	suggestedrolesir   keyr7   s	            r   create_default_configz"CameraConfig.create_default_config   s    &&(( 	Ix}}''$&	
 
 

 #7++ 	M 	MIAv$%E

NNa18PIC(.tLLIcNNr   )r   N)r   N) __name__
__module____qualname____doc__r
   rG   r0   r-   r,   r   r	   r   rB   rD   rI   rK   rM   r   rR   r   r   rV   rX   rZ   r^   boolr`   rj   rv   staticmethodr   r   rm   r   r   r    r    4   s       OO*7 
 
C 
 
 
 
 1 1 1 1   s xc3h/H    C HSM    sCx Xc]    S Xc]    	s 	x} 	 	 	 	
 
S	 
 
 
 
#d38n # # # #)49 ) ) ) ) !"  c3h 	
 sm 
   4# $    
# 
( 
 
 
 
H4T	?     DdO    \tCH~      r   r    )r   r)   pathlibr   typingr   r   r   r   r   r	   raiden._configr
   r<   raiden.cameras.baser   tuplerG   r   r    rm   r   r   <module>r      s    :        B B B B B B B B B B B B B B B B ( ( ( ( ( (555 +******@ @c3h @ @ @ @V V V V V V V V V Vr   