2
2
import paho .mqtt .client as mqtt
3
3
4
4
class Hub ():
5
- def __init__ (self , account_id , api_key , device_id = None , message_handler = None ):
5
+ def __init__ (self , account_id , api_key , device_id , message_handler = None ):
6
6
self .account_id = account_id
7
- self .client = mqtt .Client (client_id = device_id , clean_session = True )
7
+ self .client = mqtt .Client (client_id = self . _normalize_device_id ( device_id ) , clean_session = True )
8
8
self .client .username_pw_set (account_id , api_key )
9
9
# self.client.tls_set(ca_certs='labstack.com/cert.pem')
10
10
self .handlers = {}
@@ -17,11 +17,14 @@ def handler(client, userdata, msg):
17
17
h (topic , msg .payload )
18
18
self .client .on_message = handler
19
19
20
- def _normalize_topic (self , topic ):
21
- return '{}/{}' .format (self .account_id , topic )
20
+ def _normalize_device_id (self , id ):
21
+ return '{}:{}' .format (self .account_id , id )
22
+
23
+ def _normalize_topic (self , name ):
24
+ return '{}/{}' .format (self .account_id , name )
22
25
23
- def _denormalize_topic (self , topic ):
24
- return topic .lstrip (self .account_id + '/' )
26
+ def _denormalize_topic (self , name ):
27
+ return name .lstrip (self .account_id + '/' )
25
28
26
29
def connect (self , handler = None ):
27
30
self .client .connect ("hub.labstack.com" , 1883 )
0 commit comments