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mZ d dl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)Serial)JsonResponsec                 C   s8   t d}| }|d }|dkr&g }t| dd|idS )N5https://thingsaccess.com/rfid/index.php/Api/getBundleresponse   zbundles/bundle.htmldata_bundlecontext)requestsgetjsonr   )requestr	   	data_sendr    r   -/var/www/html/myproject/core/bundles/views.pybundle_page   s    
r   c                 C   s   | j dkrx| jd| jd| jd| jd| jd| jdd}tjd	|d
}|jdkrltdS t| d t	| dS )NPOST
article_IDtag_IDstep_ID
bundleSizestatestatus)r   r   r   r   r   r   z5https://thingsaccess.com/rfid/index.php/Api/addBundler      r   zFailed to add datazbundles/addBundle.html)
methodr   r   r   poststatus_coder   r   errorr   )r   datar	   r   r   r   
Add_bundle   s    







r$   c                  C   s   d } t jddddt jd}ztzZ|  }|rz.| }|dd }t||d  } W qtW q t	k
rp   Y qX qW n t
k
r   Y nX W 5 |  X | S )NZCOM5i        )portZbaudrateZbytesizetimeoutZstopbitsB   )serialr   ZSTOPBITS_ONEclosereadlinestripdecodeindexint
ValueErrorKeyboardInterrupt)numberZserr#   Zdecoded_dataZnumber_start_indexr   r   r   get_bundle_tag_id,   s     

r5   c                 C   s,   | j dkr(t }d|i}t| t|S d S )NGETr4   )r   r5   printr   )r   r4   r	   r   r   r   
get_tag_idC   s
    
r8   c                 C   s   t d}| }|d }d }|D ]}|d |kr"|} q<q"|r| jdkr|| jd| jdd}t jd|d	}|jd
krtdS t	| d t
| dd|idS )Nr   r	   IDr   r   r   )idr   r   z6https://thingsaccess.com/rfid/index.php/Api/editBundler   r   r   zFailed to edit datazbundles/editBundle.htmlitemr   )r   r   r   r   r   r    r!   r   r   r"   r   )r   r:   r	   r   r#   	edit_itemr;   r   r   r   Edit_BundlesL   s&    




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:   z8https://thingsaccess.com/rfid/index.php/Api/deleteBundler   r   r   zFailed to delete datazBundle not deleted)r7   r   r    r!   r   r   r"   r   )r   r:   data_deleter	   r   r   r   delete_Bundlei   s     
r?   )django.shortcutsr   django.contribr   r   r   django.httpr   r+   timer   r   
subprocessr   r$   r5   r8   r=   r?   r   r   r   r   <module>   s   
	