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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   $D:\rfid_django\core\bundles\views.pybundle_page   s   
r   c                 C   s   | j dkr<| jd| jd| jd| jd| jd| jdd}tjd	|d
}|jdkr6t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}zCz+	 |  }|r7z| }|dd }t||d  } W n
 ty6   Y nw qW n	 t	yB   Y nw W |
  | S W |
  | S |
  w )	NCOM5i        )portbaudratebytesizetimeoutstopbitsTB   )serialr   STOPBITS_ONEreadlinestripdecodeindexint
ValueErrorKeyboardInterruptclose)numberserr#   decoded_datanumber_start_indexr   r   r   get_bundle_tag_id,   s4   
r=   c                 C   s,   | j dkrt }d|i}t| t|S d S )NGETr9   )r   r=   printr   )r   r9   r	   r   r   r   
get_tag_idC   s   
r@   c                 C   s   t d}| }|d }d }|D ]}|d |kr|} nq|rI| jdkrI|| jd| jdd}t jd|d	}|jd
krCt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   rB   r	   r   r#   	edit_itemrC   r   r   r   Edit_BundlesL   s(   




rE   c                 C   sD   d|i}t | tjd|d}|jdkrtdS t| d tdS )NrB   z8https://thingsaccess.com/rfid/index.php/Api/deleteBundler   r   r   zFailed to delete datazBundle not deleted)r?   r   r    r!   r   r   r"   r   )r   rB   data_deleter	   r   r   r   delete_Bundlei   s   
rG   )django.shortcutsr   django.contribr   r   r   django.httpr   r/   timer   r   
subprocessr   r$   r=   r@   rE   rG   r   r   r   r   <module>   s     
	