o
    Ah	                     @   s   d dl mZ d dlmZmZ d dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ dZdefd	d
ZdefddZdedefddZdefddZdee fddZdS )    )HTTPException)IotDeviceCreateIotDeviceUpdate)str_to_objectid)datetime)	ASCENDING
DESCENDING)Optionaliot_devicesdevicec                    s6   |   }t |d< |t |}t|j|d< |S )Ncreated_dateid)dictr   utcnowCOLLECTION_NAME
insert_onestrinserted_id)r   dbdataresult r   ?/var/www/html/moveengine/app/v1/services/platform/iotdevices.pycreate_iot_device_service   s   r   
account_idc                    s<   |t  dt| i}|stdddt|d |d< |S )N_idi  zIoT Device not found)status_codedetailr   )r   find_oner   r   r   r   r   r   r   r   r   get_iot_device_service   s   r    updatec                    sZ   dd |j dd D }t |d< |t dt| id|iI d H  t| |I d H S )Nc                 S   s   i | ]\}}||qS r   r   ).0kvr   r   r   
<dictcomp>   s    z-update_iot_device_service.<locals>.<dictcomp>T)exclude_unsetlast_updatedr   z$set)r   itemsr   r   r   
update_oner   r    )r   r!   r   update_datar   r   r   update_iot_device_service   s
   "r+   c                    s2   t | |I d H }|t dt| iI d H  |S )Nr   )r    r   
delete_oner   r   r   r   r   delete_iot_device_service"   s   r-   qc                    s   i }|r|dd}	d|	id|	ig|d< |r||d< |r ||d< |s$|r>i }
|r0t |d|
d	< |r:t |d|
d
< |
|d< |t || |}g }|D ]}t|d |d< || qO|t |}||dS )Ni)z$regexz$options
model_nameinstallation_detailsz$ordevice_typestatusz%Y-%m-%dz$gtez$lter   r   r   )total_countusers)	r   strptimer   findskiplimitr   appendcount_documents)r8   r9   r.   r2   r3   	date_fromdate_tor   filter_queryregex_querydate_filtercursordevicesdtotalr   r   r   list_iot_devices_service'   s2   

rE   N)fastapir   !app.v1.models.platform.iotdevicesr   r   app.v1.libraries.objectr   r   pymongor   r   typingr	   r   r   r   r    r+   r-   rE   r   r   r   r   <module>   s    