Skip to content

Commit 0cc7f5b

Browse files
committed
Simplifying example code '1_send_data.py' by moving initialization part to module 'Connection.py'.
1 parent 1c5d07b commit 0cc7f5b

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

1_send_data.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,11 @@
77
import time
88
import LiveObjects
99

10-
board = LiveObjects.BoardsFactory(net_type=LiveObjects.BoardsInterface.DEFAULT_CARRIER)
11-
12-
apikey = board.get_apikey()
13-
client_id = board.get_client_id()
14-
security_level = board.get_security_level()
15-
16-
# Create LiveObjects with parameters: Board - ClientID - Security - APIKEY
17-
lo = LiveObjects.Connection(board, client_id, security_level, apikey)
10+
lo = LiveObjects.Connection()
1811

1912
MESSAGE_RATE = 5
2013

2114
# Main program
22-
board.network_connect()
2315
lo.connect() # Connect to LiveObjects
2416
last = uptime = time.time()
2517

LiveObjects/Connection.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ def __init__(self, value, type_, cb=None):
3131

3232

3333
class Connection:
34-
def __init__(self, board, deviceID, port, apiKey, debug=True):
35-
self.mode = board.get_lang_id()
34+
def __init__(self, debug=True):
35+
self.__board = LiveObjects.BoardsFactory(net_type=LiveObjects.BoardsInterface.DEFAULT_CARRIER)
36+
self.mode = self.__board.get_lang_id()
3637

3738
try:
3839
if self.mode == LiveObjects.BoardsInterface.PYTHON:
@@ -44,8 +45,9 @@ def __init__(self, board, deviceID, port, apiKey, debug=True):
4445
print("[ERROR] U have missing libraries Paho-mqtt(for Python) or umqttrobust(for uPython)")
4546
sys.exit()
4647

47-
self.__port = port
48-
self.__apiKey = apiKey
48+
self.__port = self.__board.get_security_level()
49+
self.__apiKey = self.__board.get_apikey()
50+
self.__device_id = self.__board.get_client_id()
4951
self.__parameters = {}
5052
self.__server = "mqtt.liveobjects.orange-business.com"
5153
self.__topic = "dev/data"
@@ -56,7 +58,7 @@ def __init__(self, board, deviceID, port, apiKey, debug=True):
5658
self.quit = False
5759

5860
if self.mode == LiveObjects.BoardsInterface.PYTHON:
59-
self.__mqtt = paho.Client(deviceID)
61+
self.__mqtt = paho.Client(self.__device_id)
6062
elif self.mode == LiveObjects.BoardsInterface.MICROPYTHON:
6163

6264
class MQTTClient2(MQTTClient):
@@ -99,8 +101,8 @@ def wait_msg(self): # overriding original method wait_msg due to avoid infin
99101
elif op & 6 == 4:
100102
assert 0
101103

102-
self.ssl = port == SSL
103-
self.__mqtt = MQTTClient2(deviceID, self.__server, self.__port, "json+device",
104+
self.ssl = self.__port == SSL
105+
self.__mqtt = MQTTClient2(self.__device_id, self.__server, self.__port, "json+device",
104106
self.__apiKey, 0, self.ssl, {'server_hostname': self.__server})
105107

106108
def loop(self):
@@ -148,6 +150,7 @@ def __onConnect(self, client="", userdata="", flags="", rc=""):
148150
self.__sendConfig()
149151

150152
def connect(self):
153+
self.__board.connect()
151154
if self.mode == LiveObjects.BoardsInterface.PYTHON:
152155
self.__mqtt.username_pw_set("json+device", self.__apiKey)
153156
self.__mqtt.on_connect = self.__onConnect

LiveObjects/hal.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def check_network_capabilities(self, net_type):
4747
print('Carrier not supported.')
4848
sys.exit()
4949

50-
def network_connect(self):
50+
def connect(self):
5151
pass
5252

5353
def network_disconnect(self):
@@ -67,7 +67,7 @@ def __init__(self, net_type):
6767
self._lte_tls_capability = True
6868
self._credentials = super().create_credentials(self._net_type)
6969

70-
def network_connect(self):
70+
def connect(self):
7171
super().check_network_capabilities(self._net_type)
7272
if self._net_type == BoardsInterface.WIFI:
7373
pycom_wifi_connect(self._credentials.get_creds()['ssid'], self._credentials.get_creds()['password'])
@@ -89,7 +89,7 @@ def __init__(self, net_type):
8989
self._wifi_tls_capability = False
9090
self._credentials = super().create_credentials(self._net_type)
9191

92-
def network_connect(self):
92+
def connect(self):
9393
super().check_network_capabilities(self._net_type)
9494
wifi_connect(self._credentials.get_creds()['ssid'], self._credentials.get_creds()['password'])
9595

@@ -109,7 +109,7 @@ def __init__(self, net_type):
109109
self._wifi_tls_capability = True
110110
self._credentials = super().create_credentials(self._net_type)
111111

112-
def network_connect(self):
112+
def connect(self):
113113
super().check_network_capabilities(self._net_type)
114114
wifi_connect(self._credentials.get_creds()['ssid'], self._credentials.get_creds()['password'])
115115

@@ -125,7 +125,7 @@ def __init__(self, net_type):
125125
self._existing_network_tls_capability = True
126126
self._credentials = super().create_credentials(self._net_type)
127127

128-
def network_connect(self):
128+
def connect(self):
129129
super().check_network_capabilities(self._net_type)
130130
use_existing_network_connection()
131131

0 commit comments

Comments
 (0)