Skip to content

Commit 1d75f02

Browse files
committed
Update Pybytes to version 1.0.0
1 parent 9571c2c commit 1d75f02

15 files changed

+1197
-451
lines changed

esp32/frozen/Base/_OTA.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
'''
2+
Copyright (c) 2019, Pycom Limited.
3+
This software is licensed under the GNU GPL version 3 or any
4+
later version, with permitted additional terms. For more information
5+
see the Pycom Licence v1.0 document supplied with this file, or
6+
available at https://www.pycom.io/opensource/licensing
7+
'''
8+
19
import network
210
import socket
311
import ssl

esp32/frozen/Base/_flash_control_OTA.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
'''
2+
Copyright (c) 2019, Pycom Limited.
3+
This software is licensed under the GNU GPL version 3 or any
4+
later version, with permitted additional terms. For more information
5+
see the Pycom Licence v1.0 document supplied with this file, or
6+
available at https://www.pycom.io/opensource/licensing
7+
'''
8+
19
import os
210
try:
311
from pybytes_debug import print_debug
412
except:
513
from _pybytes_debug import print_debug
614

15+
716
class FCOTA:
817
def __init__(self):
918
pass

esp32/frozen/Base/_mqtt.py

Lines changed: 85 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,142 @@
1-
import time
1+
'''
2+
Copyright (c) 2019, Pycom Limited.
3+
This software is licensed under the GNU GPL version 3 or any
4+
later version, with permitted additional terms. For more information
5+
see the Pycom Licence v1.0 document supplied with this file, or
6+
available at https://www.pycom.io/opensource/licensing
7+
'''
8+
9+
try:
10+
from mqtt_core import MQTTCore as mqtt_core
11+
except:
12+
from _mqtt_core import MQTTCore as mqtt_core
13+
14+
try:
15+
from pybytes_constants import MQTTConstants as mqttConst
16+
except:
17+
from _pybytes_constants import MQTTConstants as mqttConst
18+
219
try:
3-
import mqtt_core
20+
from pybytes_debug import print_debug
421
except:
5-
import _mqtt_core as mqtt_core
22+
from _pybytes_debug import print_debug
23+
24+
import time
625

726

827
class MQTTClient:
928

10-
def __init__(self, client_id, server, mqtt_download_topic, port=0, user=None, password=None, keepalive=0, ssl=False,
11-
ssl_params={}, reconnect=True):
29+
def __init__(
30+
self, client_id, server, mqtt_download_topic,
31+
pybytes_protocol, port=0, user=None, password=None,
32+
keepalive=0, ssl=False,
33+
ssl_params={}, reconnect=True
34+
):
35+
if port == 0:
36+
self.__port = 8883 if ssl else 1883
37+
else:
38+
self.__port = port
1239
self.__reconnect = reconnect
1340
self.__reconnect_count = 0
1441
self.__reconnecting = False
1542
self.__server = server
1643
self.__mqtt_download_topic = mqtt_download_topic
17-
self.__mqtt = mqtt_core.MQTTClient(client_id, server, port, user, password, keepalive,
18-
ssl, ssl_params)
44+
self.__pybytes_protocol = pybytes_protocol
45+
self.__clientId = client_id
46+
self.__user = user
47+
self.__password = password
48+
self.init_mqtt_core()
49+
50+
def init_mqtt_core(self):
51+
self.__mqtt = mqtt_core(
52+
self.__clientId,
53+
True,
54+
mqttConst.MQTTv3_1_1,
55+
receive_timeout=500,
56+
reconnectMethod=self.reconnect
57+
)
58+
self.__mqtt.configEndpoint(self.__server, self.__port)
59+
self.__mqtt._user = self.__user
60+
self.__mqtt._password = self.__password
1961

2062
def getError(self, x):
2163
"""Return a human readable error instead of its code number"""
2264

23-
# This errors are thrown by connect function, I wouldn't be able to find
65+
# This errors are thrown by connect function,
66+
# I wouldn't be able to find
2467
# anywhere a complete list of these error codes
2568
ERRORS = {
26-
'-1': 'MQTTClient: Can\'t connect to MQTT server: "{}"'.format(self.__server),
27-
'-4': 'MQTTClient: Bad credentials when connecting to MQTT server: "{}"'.format(self.__server),
28-
'-9984': 'MQTTClient: Invalid certificate validation when connecting to MQTT server: "{}"'.format(self.__server)
69+
'-1': 'MQTTClient: Can\'t connect to MQTT server: "{}"'.format(self.__server), # noqa
70+
'-4': 'MQTTClient: Bad credentials when connecting to MQTT server: "{}"'.format(self.__server), # noqa
71+
'-9984': 'MQTTClient: Invalid certificate validation when connecting to MQTT server: "{}"'.format(self.__server) # noqa
2972
}
3073
message = str(x)
31-
return ERRORS.get(str(x), 'Unknown error while connecting to MQTT server ' + message)
74+
return ERRORS.get(
75+
str(x),
76+
'Unknown error while connecting to MQTT server {}'.format(message)
77+
)
3278

3379
def connect(self, clean_session=True):
3480
i = 0
3581
while 1:
3682
try:
37-
return self.__mqtt.connect(clean_session)
38-
except OSError as e:
39-
print(self.getError(e))
83+
return self.__mqtt.connect()
84+
except OSError:
4085
if (not self.__reconnect):
4186
raise Exception('Reconnection Disabled.')
4287
i += 1
4388
time.sleep(i)
4489

45-
def set_callback(self, f):
46-
self.__mqtt.set_callback(f)
90+
def set_callback(self, mqtt_client, message):
91+
self.__pybytes_protocol.__process_recv_message(message)
4792

4893
def subscribe(self, topic, qos=0):
49-
self.__mqtt.subscribe(topic, qos)
50-
51-
def check_msg(self):
52-
while 1:
53-
time_before_retry = 10
54-
if self.__reconnecting == False:
55-
try:
56-
return self.__mqtt.check_msg()
57-
except OSError as e:
58-
print("Error check_msg", e)
59-
60-
if (not self.__reconnect):
61-
raise Exception('Reconnection Disabled.')
62-
self.reconnect()
63-
break
64-
else:
65-
time.sleep(time_before_retry)
94+
self.__mqtt.subscribe(topic, qos, self.set_callback)
6695

96+
def unsubscribe(self, topic):
97+
return self.__mqtt.unsubscribe(topic)
6798

6899
def reconnect(self):
69100
if self.__reconnecting:
70101
return
102+
103+
self.init_mqtt_core()
104+
71105
while True:
72-
self.__reconnect_count += 1
106+
self.__reconnect_count += 1
73107
self.__reconnecting = True
74108
try:
75-
self.__mqtt.connect()
109+
if not self.__mqtt.connect():
110+
time.sleep(self.__reconnect_count)
111+
continue
76112
self.subscribe(self.__mqtt_download_topic)
77-
self.__reconnect_count=0
78-
print('Reconnected to MQTT server: "{}"'.format(self.__server))
113+
self.__reconnect_count = 0
79114
self.__reconnecting = False
80115
break
81116
except OSError:
82-
print("Reconnecting failed, will retry in {} seconds".format(self.__reconnect_count))
83117
time.sleep(self.__reconnect_count)
84118

85-
def publish(self, topic, msg, retain=False, qos=0):
119+
def publish(self, topic, msg, retain=False, qos=0, priority=False):
86120
while 1:
87-
if self.__reconnecting == False:
121+
if not self.__reconnecting:
88122
try:
89-
return self.__mqtt.publish(topic, msg, retain, qos)
123+
# Disable retain for publish by now
124+
return self.__mqtt.publish(
125+
topic,
126+
msg,
127+
qos,
128+
False,
129+
priority=priority
130+
)
90131
except OSError as e:
91-
print("Error publish", e)
132+
print_debug(2, "Error publish", e)
92133

93134
if (not self.__reconnect):
94135
raise Exception('Reconnection Disabled.')
95136
self.reconnect()
96-
break
137+
raise Exception('Error publish.')
97138
else:
98139
time.sleep(10)
99140

100-
def wait_msg(self):
101-
while 1:
102-
try:
103-
return self.__mqtt.wait_msg()
104-
except OSError as e:
105-
print("Error wait_msg {}".format(e))
106-
107-
if (not self.__reconnect):
108-
raise Exception('Reconnection Disabled.')
109-
self.reconnect()
110-
111141
def disconnect(self):
112142
self.__mqtt.disconnect()

0 commit comments

Comments
 (0)