U
    ÉZjÙC  ã                   @   s”   d dl Z ddlmZ G dd„ deƒZG dd„ deƒZdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZG dd„ dƒZG dd„ dƒZdS )é    Né   )ÚPacketTypesc                   @   s   e Zd ZdS )ÚMQTTExceptionN©Ú__name__Ú
__module__Ú__qualname__© r	   r	   úW/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/paho/mqtt/properties.pyr      s   r   c                   @   s   e Zd ZdS )ÚMalformedPacketNr   r	   r	   r	   r
   r      s   r   c                 C   s   t t d| ¡ƒS )Nú!H©Ú	bytearrayÚstructÚpack©Úlengthr	   r	   r
   Ú
writeInt16   s    r   c                 C   s   t  d| d d… ¡d S )Nr   é   r   ©r   Úunpack©Úbufr	   r	   r
   Ú	readInt16#   s    r   c                 C   s   t t d| ¡ƒS )Nú!Lr   r   r	   r	   r
   Ú
writeInt32(   s    r   c                 C   s   t  d| d d… ¡d S )Nr   é   r   r   r   r	   r	   r
   Ú	readInt32-   s    r   c                 C   s$   t | tƒst| dƒ} tt| ƒƒ|  S )Núutf-8)Ú
isinstanceÚbytesr   Úlen)Údatar	   r	   r
   ÚwriteUTF2   s    

r#   c                 C   sž   |dkrt | ƒ}ntdƒ‚|d8 }||kr2tdƒ‚| dd| …  d¡}|D ]D}t|ƒ}|dkrp|dkrptdƒ‚|dkr€td	ƒ‚|d
krLtdƒ‚qL||d fS )Nr   z%Not enough data to read string lengthz Length delimited string too longr   i Ø  iÿß  z,[MQTT-1.5.4-1] D800-DFFF found in UTF-8 datar   z'[MQTT-1.5.4-2] Null found in UTF-8 dataiÿþ  z#[MQTT-1.5.4-3] U+FEFF in UTF-8 data)r   r   ÚdecodeÚord)ÚbufferÚmaxlenr   r   ÚcZord_cr	   r	   r
   ÚreadUTF9   s     

r)   c                 C   s   t t| ƒƒ|  S ©N)r   r!   )r&   r	   r	   r
   Ú
writeBytesN   s    r+   c                 C   s    t | ƒ}| dd| … |d fS )Nr   )r   )r&   r   r	   r	   r
   Ú	readBytesR   s    r,   c                   @   s(   e Zd ZdZedd„ ƒZedd„ ƒZdS )ÚVariableByteIntegerszh
    MQTT variable byte integer helper class.  Used
    in several places in MQTT v5.0 properties.

    c                 C   sh   d|   krdks&n t d| ›d�ƒ‚d}| d }| d } | dkrJ|dO }|t|gƒ7 }| dkr*qdq*|S )z‹
          Convert an integer 0 <= x <= 268435455 into multi-byte format.
          Returns the buffer converted from the integer.
        r   éÿÿÿzValue z must be in range 0-268435455ó    é€   )Ú
ValueErrorr    )Úxr&   Údigitr	   r	   r
   Úencode^   s    zVariableByteIntegers.encodec                 C   sX   d}d}d}|d7 }| d }| dd… } ||d@ | 7 }|d@ dkrFqP|d9 }q||fS )zó
          Get the value of a multi-byte integer from a buffer
          Return the value, and the number of bytes used.

          [MQTT-1.5.5-1] the encoded value MUST use the minimum number of bytes necessary to represent the value
        r   r   Né   r0   r	   )r&   Z
multiplierÚvaluer    r3   r	   r	   r
   r$   q   s    
zVariableByteIntegers.decodeN)r   r   r   Ú__doc__Ústaticmethodr4   r$   r	   r	   r	   r
   r-   W   s
   
r-   c                   @   sx   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )Ú
Propertiesab  MQTT v5.0 properties class.

    See Properties.names for a list of accepted property names along with their numeric values.

    See Properties.properties for the data type of each property.

    Example of use::

        publish_properties = Properties(PacketTypes.PUBLISH)
        publish_properties.UserProperty = ("a", "2")
        publish_properties.UserProperty = ("c", "3")

    First the object is created with packet type as argument, no properties will be present at
    this point. Then properties are added as attributes, the name of which is the string property
    name without the spaces.

    c              %   C   sÞ  || _ dddddddg| _dd	d
dddddddddddddddddddddd d!d"d#œ| _| j d¡tjtjgf| j d¡tjtjgf| j d¡tjtjgf| j d¡tjtjgf| j d¡tjtjgf| j d¡tjtjgf| j d¡tjtj	tj
gf| j d¡tj	gf| j d¡tj	gf| j d¡tjtj	tjgf| j d¡tjtj	tjgf| j d¡tjgf| j d¡tjgf| j d¡tjgf| j d¡tj	gf| j d¡tj	tj
gf| j d¡tj	tjtjtjtjtjtjtj
tjg	f| j d¡tjtj	gf| j d¡tjtj	gf| j d¡tjgf| j d¡tj	gf| j d¡tj	gf| j d¡tjtj	tjtjtjtjtjtjtjtjtjtj
tjtjgf| j d¡tjtj	gf| j d¡tj	gf| j d¡tj	gf| j d¡tj	gfd$œ| _d S )%NÚByteúTwo Byte IntegerúFour Byte IntegerúVariable Byte IntegerúBinary DataúUTF-8 Encoded StringúUTF-8 String Pairr   r   é   é   é	   é   é   é   é   é   é   é   é   é   é   é   é   é!   é"   é#   é$   é%   é&   é'   é(   é)   é*   )zPayload Format IndicatorzMessage Expiry IntervalzContent TypezResponse TopiczCorrelation DatazSubscription IdentifierzSession Expiry IntervalzAssigned Client IdentifierzServer Keep AlivezAuthentication MethodzAuthentication DatazRequest Problem InformationzWill Delay IntervalzRequest Response InformationzResponse InformationzServer ReferencezReason StringzReceive MaximumzTopic Alias MaximumzTopic AliaszMaximum QoSzRetain AvailablezUser PropertyzMaximum Packet SizezWildcard Subscription Availablez!Subscription Identifier AvailablezShared Subscription Available)r   r   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   )Ú
packetTypeÚtypesÚnamesÚindexr   ZPUBLISHZWILLMESSAGEZ	SUBSCRIBEZCONNECTZCONNACKZ
DISCONNECTZAUTHZPUBACKZPUBRECZPUBRELZPUBCOMPZSUBACKZUNSUBACKZUNSUBSCRIBEÚ
properties)ÚselfrZ   r	   r	   r
   Ú__init__š   sÐ      ÿå!

ÿ
ÿ
ÿ
ÿ
ÿ

ÿ
    þÿ

ÿ

ÿ
       ûÿ

ÿÑzProperties.__init__c                 C   s   |   |¡dkS )N)rD   rU   )ÚgetIdentFromName)r_   ÚcompressedNamer	   r	   r
   ÚallowsMultipleï   s    zProperties.allowsMultiplec                 C   s6   d}| j  ¡ D ]"}|| dd¡kr| j | } q2q|S )Néÿÿÿÿú Ú )r\   ÚkeysÚreplace)r_   rb   ÚresultÚnamer	   r	   r
   ra   ò   s    
zProperties.getIdentFromNamec                 C   s–  |  dd¡}ddddg}||kr2t | ||¡ �n`|dd„ | j ¡ D ƒkr^td	| j ¡ › �ƒ‚| j| j|  |¡ d
 kr”td|› dt	j
| j › �ƒ‚t|tƒ�sJ|dkrÈ|d
k s¸|dkrÈt|› d�ƒ‚n‚|dkrð|dk sà|dkrðt|› d�ƒ‚nZ|dk�r|d
k �s|dk�rt|› d�ƒ‚n,|dk�rJ|dk�rJ|d
k�rJt|› d�ƒ‚|  |¡�r„t|tƒ�sh|g}t| |ƒ�r„t | |¡| }t | ||¡ d S )Nre   rf   rZ   r[   r\   r^   c                 S   s   g | ]}|  d d¡‘qS )re   rf   )rh   )Ú.0Zanamer	   r	   r
   Ú
<listcomp>  s     z*Properties.__setattr__.<locals>.<listcomp>zProperty name must be one of r   z	Property z does not apply to packet type )ZReceiveMaximumZ
TopicAliasiÿÿ  z, property value must be in the range 1-65535)ZTopicAliasMaximumr   z, property value must be in the range 0-65535)ZMaximumPacketSizeZSubscriptionIdentifierr.   z0 property value must be in the range 1-268435455)ZRequestResponseInformationZRequestProblemInformationZPayloadFormatIndicatorz property value must be 0 or 1)rh   ÚobjectÚ__setattr__r\   rg   r   rZ   r^   ra   r   ZNamesr   Úlistrc   ÚhasattrÚ__getattribute__)r_   rj   r6   ZprivateVarsr	   r	   r
   rn   û   sV    ÿÿÿÿÿ
ÿÿ
ÿÿÿzProperties.__setattr__c                 C   sb   d}d}| j  ¡ D ]B}| dd¡}t| |ƒr|s8|d7 }||› dt| |ƒ› �7 }d}q|d7 }|S )	Nú[Tre   rf   z, z : Fú])r\   rg   rh   rp   Úgetattr)r_   r&   Úfirstrj   rb   r	   r	   r
   Ú__str__$  s    
zProperties.__str__c                 C   s`   i }| j  ¡ D ]L}| dd¡}t| |ƒrt| |ƒ}|dkrRt|tƒrR| ¡ ||< q|||< q|S )Nre   rf   ZCorrelationData)r\   rg   rh   rp   rt   r   r    Úhex)r_   r"   rj   rb   Úvalr	   r	   r
   Újson1  s    


zProperties.jsonc                 C   s6   d}| j  ¡ D ]"}| dd¡}t| |ƒrd} q2q|S )NTre   rf   F)r\   rg   rh   rp   )r_   Úrcrj   rb   r	   r	   r
   ÚisEmpty=  s    
zProperties.isEmptyc                 C   s4   | j  ¡ D ]$}| dd¡}t| |ƒr
t| |ƒ q
d S )Nre   rf   )r\   rg   rh   rp   Údelattr)r_   rj   rb   r	   r	   r
   ÚclearF  s    
zProperties.clearc                 C   sú   d}|t  |¡7 }|| j d¡kr2|t|gƒ7 }nÄ|| j d¡krP|t|ƒ7 }n¦|| j d¡krn|t|ƒ7 }nˆ|| j d¡krŽ|t  |¡7 }nh|| j d¡kr¬|t|ƒ7 }nJ|| j d¡krÊ|t|ƒ7 }n,|| j d¡krö|t|d	 ƒt|d
 ƒ 7 }|S )Nr/   r:   r;   r<   r=   r>   r?   r@   r   r   )	r-   r4   r[   r]   r    r   r   r+   r#   )r_   Ú
identifierÚtyper6   r&   r	   r	   r
   ÚwritePropertyL  s"    zProperties.writePropertyc              	   C   sš   d}| j  ¡ D ]x}| dd¡}t| |ƒr|  |¡}| j| d }|  |¡rnt| |ƒD ]}||  |||¡7 }qTq||  ||t| |ƒ¡7 }qt	 
t|ƒ¡| S )Nr/   re   rf   r   )r\   rg   rh   rp   ra   r^   rc   rt   r€   r-   r4   r!   )r_   r&   rj   rb   r~   Ú	attr_typeÚpropr	   r	   r
   r   _  s     


 ÿ

ÿzProperties.packc                 C   s  || j  d¡kr|d }d}nè|| j  d¡kr<t|ƒ}d}nÊ|| j  d¡krZt|ƒ}d}n¬|| j  d¡krzt |¡\}}nŒ|| j  d	¡kr˜t|ƒ\}}nn|| j  d
¡kr¸t||ƒ\}}nN|| j  d¡k�rt||ƒ\}}||d … }t||| ƒ\}}||f}||7 }||fS )Nr:   r   r   r;   r   r<   r   r=   r>   r?   r@   )r[   r]   r   r   r-   r$   r,   r)   )r_   r&   r   Úpropslenr6   ÚvaluelenZvalue1Z	valuelen1r	   r	   r
   ÚreadPropertyp  s,    zProperties.readPropertyc                 C   s&   d }| j D ]}| j | |kr
|}q
|S r*   )r\   )r_   r~   rz   rj   r	   r	   r
   ÚgetNameFromIdentˆ  s
    
zProperties.getNameFromIdentc                 C   sØ   |   ¡  t |¡\}}||d … }|}|dkrÌt |¡\}}||d … }||8 }| j| d }|  |||¡\}}	||	d … }||	8 }|  |¡}
|
 dd¡}|  |¡s¾t| |ƒr¾t	dt
› d�ƒ‚t| |
|ƒ q&| || fS )Nr   re   rf   z
Property 'z' must not exist more than once)r}   r-   r$   r^   r…   r†   rh   rc   rp   r   ÚpropertyÚsetattr)r_   r&   rƒ   ZVBIlenZpropslenleftr~   ZVBIlen2r�   r6   r„   Zpropnamerb   r	   r	   r
   r   �  s4    ÿ  ÿ

ÿzProperties.unpackN)r   r   r   r7   r`   rc   ra   rn   rv   ry   r{   r}   r€   r   r…   r†   r   r	   r	   r	   r
   r9   ‡   s   U	)	r9   )r   Zpackettypesr   Ú	Exceptionr   r   r   r   r   r   r#   r)   r+   r,   r-   r9   r	   r	   r	   r
   Ú<module>   s   0