U
    *"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   //var/www/html/myproject/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(|} qBq(|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rt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   s0    







	
r,   c                 C   sH   d|i}t | tjd|d}|jdkr0tdS t| d tdS 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}zzl|  }|rt| z8| }|dd }t	||d  } td|  W qW q t
k
r   Y qX qW n tk
r   Y nX W 5 |  X | S )	NZCOM5i        )portZbaudrateZbytesizetimeoutZstopbitsO   zNumber:)serialr   ZSTOPBITS_ONEclosereadlinestripr-   decodeindexint
ValueErrorKeyboardInterrupt)numberZserr%   Zdecoded_dataZnumber_start_indexr   r   r   get_operator_tag_idf   s$    


r@   c                 C   s,   | j dkr(t }d|i}t| t|S d S )NGETr?   )r!   r@   r-   r   )r   r?   r
   r   r   r   tag_id_operator   s
    
rB   )django.shortcutsr   django.contribr   r   r   django.httpr   r6   timer   r   r   articles.viewsr   r   r&   r,   r/   r@   rB   r   r   r   r   <module>   s"   	#