77import sys
88import json
99import time
10- import os
10+
11+ import LiveObjects
12+
1113INT = "i32"
1214UINT = "u32"
1315BINARY = "bin"
@@ -30,17 +32,18 @@ def __init__(self, value, type_, cb=None):
3032
3133class Connection :
3234 def __init__ (self , board , deviceID , port , apiKey , debug = True ):
33- try :
34- if sys .platform == "linux" or sys .platform == "win32" :
35+ self .mode = board .get_lang_id ()
36+
37+ try :
38+ if self .mode == LiveObjects .BoardsInterface .PYTHON :
3539 import paho .mqtt .client as paho
3640 import os
37- self .mode = 1
38- else :
41+ elif self .mode == LiveObjects .BoardsInterface .MICROPYTHON :
3942 from umqttrobust import MQTTClient
40- self .mode = 0
4143 except ImportError :
42- print ("[ERROR] U have missing libraries Paho-mqtt(for Linux ) or umqttrobust(for uPython)" )
44+ print ("[ERROR] U have missing libraries Paho-mqtt(for Python ) or umqttrobust(for uPython)" )
4345 sys .exit ()
46+
4447 self .__port = port
4548 self .__apiKey = apiKey
4649 self .__parameters = {}
@@ -52,18 +55,19 @@ def __init__(self, board, deviceID, port, apiKey, debug=True):
5255 self .__doLog = debug
5356 self .quit = False
5457
55- if self .mode == 1 :
58+ if self .mode == LiveObjects . BoardsInterface . PYTHON :
5659 self .__mqtt = paho .Client (deviceID )
5760 else :
5861 self .ssl = port == 8883
59- self .__mqtt = MQTTClient (deviceID , self .__server , self .__port , "json+device" , self .__apiKey , 0 , self .ssl , {'server_hostname' :self .__server })
62+ self .__mqtt = MQTTClient (deviceID , self .__server , self .__port , "json+device" ,
63+ self .__apiKey , 0 , self .ssl , {'server_hostname' : self .__server })
6064
6165 def loop (self ):
62- if self .mode == 0 :
66+ if self .mode == LiveObjects . BoardsInterface . MICROPYTHON :
6367 self .__mqtt .check_msg ()
6468
6569 def __onMessage (self , client = "" , userdata = "" , msg = "" ):
66- if self .mode == 1 :
70+ if self .mode == LiveObjects . BoardsInterface . PYTHON :
6771 if msg .topic == "dev/cfg/upd" :
6872 self .__parameterManager (msg )
6973 elif msg .topic == "dev/cmd" :
@@ -75,7 +79,7 @@ def __onMessage(self, client="", userdata="", msg=""):
7579 self .__commandManager (userdata )
7680
7781 def __onConnect (self , client = "" , userdata = "" , flags = "" , rc = "" ):
78- if self .mode == 1 :
82+ if self .mode == LiveObjects . BoardsInterface . PYTHON :
7983 if rc == 0 :
8084 self .outputDebug (INFO , "Connected!" )
8185 if len (self .__commands ) > 0 :
@@ -104,7 +108,7 @@ def __onConnect(self, client="", userdata="", flags="", rc=""):
104108
105109
106110 def connect (self ):
107- if self .mode == 1 :
111+ if self .mode == LiveObjects . BoardsInterface . PYTHON :
108112 self .__mqtt .username_pw_set ("json+device" , self .__apiKey )
109113 self .__mqtt .on_connect = self .__onConnect
110114 self .__mqtt .on_message = self .__onMessage
@@ -123,7 +127,7 @@ def disconnect(self):
123127 self .__mqtt .disconnect ()
124128 self .outputDebug (INFO , "Disconnected" )
125129
126- def outputDebug (self ,info , * args ):
130+ def outputDebug (self , info , * args ):
127131 if self .__doLog :
128132 print ("[" , info , "]" , end = " " , sep = "" )
129133 for arg in args :
@@ -134,7 +138,7 @@ def addCommand(self, name, cmd):
134138 self .__commands [name ] = cmd
135139
136140 def __commandManager (self , msg ):
137- if self .mode == 1 :
141+ if self .mode == LiveObjects . BoardsInterface . PYTHON :
138142 msgDict = json .loads (msg .payload )
139143 self .outputDebug (INFO , "Received message:\n " , json .dumps (msgDict , sort_keys = True , indent = 4 ))
140144 else :
@@ -152,13 +156,13 @@ def __default(self, req=""):
152156 return {"info" : "Command not found" }
153157
154158 def __sendConfig (self ):
155- outMsg = { "cfg" : {} }
159+ outMsg = {"cfg" : {}}
156160 for param in self .__parameters :
157161 outMsg ["cfg" ][param ] = {"t" : self .__parameters [param ].type , "v" : self .__parameters [param ].value }
158162 self .__publishMessage ("dev/cfg" , outMsg )
159163
160164 def __parameterManager (self , msg ):
161- if self .mode == 1 :
165+ if self .mode == LiveObjects . BoardsInterface . PYTHON :
162166 self .outputDebug (INFO , "Received message: " )
163167 self .outputDebug (INFO , json .loads (msg .payload ))
164168 params = json .loads (msg .payload )
@@ -179,8 +183,8 @@ def __parameterManager(self, msg):
179183 self .__parameters [param ].type = FLOAT
180184
181185 self .__parameters [param ].value = params ["cfg" ][param ]["v" ]
182- if self .__parameters [param ].callback != None :
183- self .__parameters [param ].callback (param , params ["cfg" ][param ]["v" ])
186+ if self .__parameters [param ].callback is not None :
187+ self .__parameters [param ].callback (param , params ["cfg" ][param ]["v" ])
184188 self .__publishMessage ("dev/cfg" , params )
185189
186190 def addParameter (self , name , val , type_ , cb = None ):
@@ -232,14 +236,16 @@ def addTags(self, tags):
232236 def sendData (self ):
233237 if self .quit :
234238 sys .exit ()
235- self .__publishMessage ("dev/data" ,self .__payload )
239+ self .__publishMessage ("dev/data" , self .__payload )
236240 self .__payload = {}
237241 self .__payload [self .__value ] = {}
238242
239243 def __publishMessage (self , topic , msg ):
240244 self .outputDebug (INFO , "Publishing message on topic: " , topic )
241- if self .mode == 1 :
245+
246+ if self .mode == LiveObjects .BoardsInterface .PYTHON :
242247 self .outputDebug (INFO , "\n " , json .dumps (msg , sort_keys = True , indent = 4 ))
243248 else :
244249 self .outputDebug (INFO , json .dumps (msg ))
250+
245251 self .__mqtt .publish (topic , json .dumps (msg ))
0 commit comments