o
    *"d                     @   s   d dl mZ d dlmZ d dl mZ d dlZd dlmZ d dlZd dl	Z	d dlmZ d dl
Z
d dlmZ d dlmZ d dlmZ d	d
 Zdd Zdd Zdd Zdd Zdd ZdS )    )render)messages)redirectN)HttpResponse)JsonResponse)Serial)	get_skillc                 C   s8   t d}| }|d }|dkrg }t| dd|idS )N7https://thingsaccess.com/rfid/index.php/Api/getOperatorresponse   zoperators/operator.htmldata_operatorcontext)requestsgetjsonr   )requestr
   	data_sendr    r   &D:\rfid_django\core\operators\views.pyoperator_page   s   
r   c                 C   s   t  }| jdkr>t| j}|d|d|d|d|d|dd}tjd	|d
}| }|d }td|iS t	| dd|idS )NPOSTNamecniccelladdressskilltag_id)r   r   r   r   r   r   z7https://thingsaccess.com/rfid/index.php/Api/addOperatorr   r
   data_articlezoperators/addOperator.htmlskillsr   )
r   methodr   loadsbodyr   r   postr   r   )r   r    Z	data_jsondatar
   r   r   r   r   r   Add_operator   s   
	r&   c              	   C   s   t  }td}| }|d }d }|D ]}|d |kr |} nq|r^| jdkr^|| jd| jd| jd| jd| jd	| jd
d}tjd|d}|jdkrZtdS t	dS t
| d||ddS )Nr	   r
   IDr   r   r   r   r   r   r   )idr   r   r   r   r   r   z8https://thingsaccess.com/rfid/index.php/Api/editOperatorr      r   zOperator not updatedzoperators/editOperator.html)itemr    r   )r   r   r   r   r!   r   r$   status_coder   r   r   )r   r(   r    r
   r   r%   Z	edit_itemr*   r   r   r   Edit_Operator4   s2   







	
r,   c                 C   sD   d|i}t | tjd|d}|jdkrtdS t| d tdS )Nr(   z:https://thingsaccess.com/rfid/index.php/Api/deleteOperatorr   r)   r   zFailed to delete datazOperator not deleted)printr   r$   r+   r   r   errorr   )r   r(   Zdata_deleter
   r   r   r   delete_OperatorW   s   
r/   c                  C   s   d } t jddddt jd}zLz4	 |  }|r@t| z| }|dd }t||d  } td	|  W n
 t	y?   Y nw qW n	 t
yK   Y nw W |  | S W |  | S |  w )
NZCOM5i        )portZbaudrateZbytesizetimeoutZstopbitsTO   zNumber:)serialr   ZSTOPBITS_ONEreadlinestripr-   decodeindexint
ValueErrorKeyboardInterruptclose)numberZserr%   decoded_dataZnumber_start_indexr   r   r   get_operator_tag_idf   s8   


rA   c                 C   s,   | j dkrt }d|i}t| t|S d S )NGETr?   )r!   rA   r-   r   )r   r?   r
   r   r   r   tag_id_operator   s   
rC   )django.shortcutsr   django.contribr   r   r   django.httpr   r6   timer   r   r   articles.viewsr   r   r&   r,   r/   rA   rC   r   r   r   r   <module>   s$   	#