3
-hh)                 @   s  d dl mZ d dlZd dlmZ ddlmZmZmZm	Z	m
Z
 d dlZd dljjZd dlmZ d dlZd dlZd dlZd dlZd dlmZ ejejjdd	e_ej Zd
ZdZdZdZeej Z d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGgZ!d$Z"d Z#d%d& Z$d'd( Z%d)d* Z&d+d, Z'd-d. Z(d/Z)ej*j+e)rBe,d0 ej-  e.e)d1Z/e/j0eej1  W dQ R X z
e(  W dej2e) X dS )H    )clientN)timezone   )TtnstateReportProductReportcurrentloopTtn)
connection)Qzutf-8)encodingz191.101.2.61i[  Zszmsdda8jKHks3grand_beton/report/1grand_beton/report/2grand_beton/report/4grand_beton/report/8grand_beton/report/16grand_beton/report/32grand_beton/report/64grand_beton/report/128grand_beton/report/256grand_beton/report/512grand_beton/report/1024grand_beton/production/1grand_beton/production/2grand_beton/production/4grand_beton/production/8grand_beton/production/16grand_beton/production/32grand_beton/production/64grand_beton/production/128grand_beton/production/256grand_beton/production/512grand_beton/production/1024zgrand_beton/ttn/backc               C   s   t j rt j  rt j  d S )N)r
   	is_usableclose r&   r&   9/home/mqttuser/mqtt_project/mqtt_processor/mqtt_client.pyensure_connection3   s    r(   c       (      C   s^  t   tj|}d|ks"d|kr|jdd }|jdd }t|tr^tjtjj	|dtj
 }|jd d}|jdg }t|tjr|}ntjj	|d}tj|tj
 }xN|D ]D}	|	jdd }
|	jdd }y|d k	rt|}t   tjjt|d	t|
d
@ j }|s:tjj|
|||d td|
 d| d|  ntd|
 d| d| d t   tjj|
dj rd|i}|d k	r||d< tjj|
djf | td|  ntd|
  W q tk
r } ztd|  W Y d d }~X qX qW |dkr~|jdd }tj|}d|d< d| }| j|tj|jdtdd}|j}|dkrjtd| d  ntd!| d"|  |d#krZ|jdd }|rZtj|}d#|d< d| }| j|tj|jdtdd}|j}|dkrtd$|  ntd%| d&|  nNd'|krZ|jd(g }|jd)g }|jd}x|D ]}|jdpTd*}|jd+pdd*}|jd,ptd}|jd-pd}|jd.pd}|jd/pd0}|jd1pd0}|jd2pd0}|jd3pd0} |jd4pd0}!|jdpd}
|jd5pd*}"|jd-pd}#|jdp$d}$t   |rPtjj	|d}%tj|%tj
 }tjj||$d6j sNt||||||||| |!|
|"|#||$d7}&|&j  x|D ]}'|'jdd |jdd krt   tjj|'jd.d |'jd-d |'jd8d |'jd9d |'jd:d |'jd;d |'jd<d |'jd=d |'jd>d |'jd?d |'jd@d |'jdAd |&j|$dB qW t   q@W d S )CNttn
productionstdatetimez%Y-%m-%d %H:%M:%S)tzinfoZind_ttnZind_product)state)idTtn)r/   r.   datejsonzSaved new Ttnstate: idTtn=z, state=z, date=zRecord with ttnState idTtn=z already exists)idr.   	idProductzUpdated ttn: z!No record found in ttn for idTtn=zError:    bsu   zgrand_beton/ttn/zutf-8F)ZqosZretainr   z Message with st=15 published to z ------z%Failed to send message with st=15 to z, BSU result code:    z Message with st=13 published to z%Failed to send message with st=13 to z, result code: reportproductcurrent_loopz2025-01-01 00:00:00Z	time_doneZ	v_productnum_loopZv_loopdriver carZclass_recipeZname_reciperecipeZ
time_start)idPlant
indProduct)	dateStarttimeEndvProduct
loopNumbervLoopr<   r>   classRecipe
nameReciper?   r/   	timeStartr;   r@   rA   code	dispenserZ
err_dosingZerr_dosing_persentZkorr_dosingZkorr_humidityZweight_fact_loopZweight_fact_m3Zweight_recipe_loopZweight_recipe_m3)rF   rE   rJ   	dispencerdoisingErrordoisingErrorPersentdoisingKorrhumidityKorrweightFactLoopweightFactM3weightRecipeLoopweightRecipeM3r3   rA   ) r(   r1   loadsget
isinstancestrr   
make_awarer,   strptimeget_current_timezonereplaceintr   objectsfilterr   existscreateprintr	   update	ExceptionZpublishdumpsencodeMQTT_QOSrcr   saver   r2   )(r   topicpayloaddatarJ   r0   Zttn_listZnaive_datetimeZaware_datetimer)   r/   r3   Zexisting_recordZupdate_dataer5   Znew_payloadZ	new_topicresultstatusZproduct_listZcurrent_loop_listr@   r9   rB   rC   rD   rE   rF   r<   r>   rG   rH   r?   rI   r;   rA   Z
naive_dateZproduct_objloopr&   r&   r'   save_message8   s    


$
























rq   c             C   s    t d t| |j|jj  d S )NzReceived message:-----otladka)rb   rq   rj   rk   decode)r   userdatamsgr&   r&   r'   
on_message   s    ru   c             C   s   t d|  | jt d S )Nz*Connected to MQTT broker with result code )rb   	subscribeMQTT_TOPICS_SUB)r   rs   flagsrh   r&   r&   r'   
on_connect   s    ry   c              C   s>   t jtd} t| _t| _| jtt | jt	t
d | j  | S )N)Z	client_id<   )mqttClientMQTT_CLIENT_IDry   ru   Zusername_pw_setMQTT_USERNAMEMQTT_PASSWORDconnectMQTT_BROKER	MQTT_PORTZ
loop_start)r   r&   r&   r'   start_mqtt_client   s    r   z/tmp/mqtt_client.lockzMQTT client is already running.w)r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r    r   )r!   r   )r"   r   )r#   r   )3httpr   r1   django.utilsr   modelsr   r   r   r   r	   uuidZpaho.mqtt.clientr{   	django.dbr
   r,   sysosiodjango.db.modelsr   TextIOWrapperstdoutbufferr|   r   r   r~   r   rX   uuid4r}   rw   ZMQTT_TOPIC_PUBrg   r(   rq   ru   ry   r   Z	lock_filepathr`   rb   exitopenfwritegetpidremover&   r&   r&   r'   <module>   sr    	
