Skip to content

Commit 1c5d07b

Browse files
committed
From BoardsInterface class (hal.py) method 'mqtt_lib_import_str' has been removed as unnecessary. Improving class MQTTClient2 containing overridden method 'wait_msg' which implements timeout for waiting for broker answer. Example file 1_send_data.py has been changed to avoid adding timeout concerning waiting for broker answer to MESSAGE_RATE.
1 parent 9f3316f commit 1c5d07b

File tree

3 files changed

+10
-19
lines changed

3 files changed

+10
-19
lines changed

1_send_data.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121
# Main program
2222
board.network_connect()
2323
lo.connect() # Connect to LiveObjects
24-
last = time.time()
25-
uptime = time.time()
24+
last = uptime = time.time()
2625

2726
while True:
28-
if time.time() >= last + MESSAGE_RATE:
27+
if (time.time()) >= last + MESSAGE_RATE:
2928
lo.addToPayload("uptime", int(time.time() - uptime)) # Add value to payload: name - value
3029
lo.sendData() # Sending data to cloud
31-
lo.loop() # Check for incoming messages and if connection is still active
3230
last = time.time()
31+
lo.loop() # Check for incoming messages and if connection is still active

LiveObjects/Connection.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ def __init__(self, board, deviceID, port, apiKey, debug=True):
5858
if self.mode == LiveObjects.BoardsInterface.PYTHON:
5959
self.__mqtt = paho.Client(deviceID)
6060
elif self.mode == LiveObjects.BoardsInterface.MICROPYTHON:
61-
self.ssl = port == 8883
6261

63-
class MQTTClient2(MQTTClient): # overriding original method wait_msg due to infinite waiting
64-
def wait_msg(self):
62+
class MQTTClient2(MQTTClient):
63+
TIMEOUT = 1000 # [ms]
64+
65+
def wait_msg(self): # overriding original method wait_msg due to avoid infinite waiting
6566
import select
6667
poller = select.poll()
6768
poller.register(self.sock, select.POLLIN)
68-
res = poller.poll(1000)
69+
res = poller.poll(MQTTClient2.TIMEOUT)
6970
if not res:
7071
res = None
7172
self.sock.setblocking(True)
@@ -98,6 +99,7 @@ def wait_msg(self):
9899
elif op & 6 == 4:
99100
assert 0
100101

102+
self.ssl = port == SSL
101103
self.__mqtt = MQTTClient2(deviceID, self.__server, self.__port, "json+device",
102104
self.__apiKey, 0, self.ssl, {'server_hostname': self.__server})
103105

@@ -145,13 +147,12 @@ def __onConnect(self, client="", userdata="", flags="", rc=""):
145147
self.__mqtt.subscribe(b"dev/cfg/upd")
146148
self.__sendConfig()
147149

148-
149150
def connect(self):
150151
if self.mode == LiveObjects.BoardsInterface.PYTHON:
151152
self.__mqtt.username_pw_set("json+device", self.__apiKey)
152153
self.__mqtt.on_connect = self.__onConnect
153154
self.__mqtt.on_message = self.__onMessage
154-
if self.__port == 8883:
155+
if self.__port == SSL:
155156
filename = "/etc/ssl/certs/ca-certificates.crt"
156157
self.__mqtt.tls_set(filename)
157158
self.__mqtt.connect(self.__server, self.__port, 60)

LiveObjects/hal.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ def get_lang_str(self):
3939
def get_lang_id(self):
4040
return self._lang_id
4141

42-
def mqtt_lib_import_str(self):
43-
import_strings = {BoardsInterface.PYTHON: 'import paho.mqtt.client as paho',
44-
BoardsInterface.MICROPYTHON: 'from umqttrobust import MQTTClient'}
45-
return import_strings[self._lang_id]
46-
4742
def get_security_level(self):
4843
pass
4944

@@ -70,7 +65,6 @@ def __init__(self, net_type):
7065
self._carrier_capability = (BoardsInterface.WIFI, BoardsInterface.LTE)
7166
self._wifi_tls_capability = True
7267
self._lte_tls_capability = True
73-
self._mqtt_lib = super().mqtt_lib_import_str()
7468
self._credentials = super().create_credentials(self._net_type)
7569

7670
def network_connect(self):
@@ -93,7 +87,6 @@ def __init__(self, net_type):
9387
self._net_type = BoardsInterface.WIFI if net_type == BoardsInterface.DEFAULT_CARRIER else net_type
9488
self._carrier_capability = (BoardsInterface.WIFI,)
9589
self._wifi_tls_capability = False
96-
self._mqtt_lib = super().mqtt_lib_import_str()
9790
self._credentials = super().create_credentials(self._net_type)
9891

9992
def network_connect(self):
@@ -114,7 +107,6 @@ def __init__(self, net_type):
114107
self._net_type = BoardsInterface.WIFI if net_type == BoardsInterface.DEFAULT_CARRIER else net_type
115108
self._carrier_capability = (BoardsInterface.WIFI,)
116109
self._wifi_tls_capability = True
117-
self._mqtt_lib = super().mqtt_lib_import_str()
118110
self._credentials = super().create_credentials(self._net_type)
119111

120112
def network_connect(self):
@@ -131,7 +123,6 @@ def __init__(self, net_type):
131123
self._net_type = BoardsInterface.EXISTING_NETWORK if net_type == BoardsInterface.DEFAULT_CARRIER else net_type
132124
self._carrier_capability = (BoardsInterface.EXISTING_NETWORK,)
133125
self._existing_network_tls_capability = True
134-
self._mqtt_lib = super().mqtt_lib_import_str()
135126
self._credentials = super().create_credentials(self._net_type)
136127

137128
def network_connect(self):

0 commit comments

Comments
 (0)