@@ -1604,11 +1604,14 @@ def __init__(self, host, port, provision_request):
16041604 super ().__init__ ()
16051605 self ._host = host
16061606 self ._port = port
1607- self ._username = "provision"
1607+ self ._username = b"provision"
1608+ self .tls_set (tls_version = ssl .PROTOCOL_TLSv1_2 ,
1609+ cert_reqs = ssl .CERT_NONE )
16081610 self .__credentials = None
16091611 self .on_connect = self .__on_connect
16101612 self .on_message = self .__on_message
16111613 self .__provision_request = provision_request
1614+ self ._provisioned = False
16121615
16131616 def __on_connect (self , client , _ , __ , rc ): # Callback for connect
16141617 if rc == 0 :
@@ -1630,13 +1633,22 @@ def __on_message(self, _, __, msg):
16301633 else :
16311634 log .error ("[Provisioning client] Provisioning was unsuccessful with status %s and message: %s" % (
16321635 provision_device_status , decoded_message ["errorMsg" ]))
1633- self .disconnect ()
16341636
1635- def provision (self ):
1637+ self ._provisioned = True
1638+
1639+ def provision (self , timeout = 60 ):
16361640 log .info ("[Provisioning client] Connecting to ThingsBoard" )
16371641 self .__credentials = None
1642+ self ._provisioned = False
16381643 self .connect (self ._host , self ._port , 60 )
1639- self .loop_forever ()
1644+ start_connection_time = monotonic ()
1645+ self .loop_start ()
1646+
1647+ while not self ._provisioned and monotonic () - start_connection_time < timeout :
1648+ sleep (1 )
1649+
1650+ self .loop_stop ()
1651+ self .disconnect ()
16401652
16411653 def get_credentials (self ):
16421654 return self .__credentials
0 commit comments