6
6
# Modified Work Copyright (c) 2019 Bradley Beach, esp32spi_mqtt
7
7
# Modified Work Copyright (c) 2012-2019 Roger Light and others, Paho MQTT Python
8
8
9
+ # pylint: disable=too-many-lines
10
+
9
11
"""
10
12
`adafruit_minimqtt`
11
13
================================================================================
@@ -126,6 +128,7 @@ class MQTT:
126
128
:param str client_id: Optional client identifier, defaults to a unique, generated string.
127
129
:param bool is_ssl: Sets a secure or insecure connection with the broker.
128
130
:param int keep_alive: KeepAlive interval between the broker and the MiniMQTT client.
131
+ :param int recv_timeout: receive timeout, in seconds.
129
132
:param socket socket_pool: A pool of socket resources available for the given radio.
130
133
:param ssl_context: SSL context for long-lived SSL connections.
131
134
:param bool use_binary_mode: Messages are passed as bytearray instead of string to callbacks.
@@ -142,6 +145,7 @@ def __init__(
142
145
client_id=None,
143
146
is_ssl=True,
144
147
keep_alive=60,
148
+ recv_timeout=10,
145
149
socket_pool=None,
146
150
ssl_context=None,
147
151
use_binary_mode=False,
@@ -154,6 +158,7 @@ def __init__(
154
158
self._use_binary_mode = use_binary_mode
155
159
156
160
self.keep_alive = keep_alive
161
+ self._recv_timeout = recv_timeout
157
162
self._user_data = None
158
163
self._is_connected = False
159
164
self._msg_size_lim = MQTT_MSG_SZ_LIM
@@ -514,6 +519,7 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None):
514
519
self._send_str(self._password)
515
520
if self.logger is not None:
516
521
self.logger.debug("Receiving CONNACK packet from broker")
522
+ stamp = time.monotonic()
517
523
while True:
518
524
op = self._wait_for_msg()
519
525
if op == 32:
@@ -527,6 +533,12 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None):
527
533
self.on_connect(self, self._user_data, result, rc[2])
528
534
return result
529
535
536
+ if op is None:
537
+ if time.monotonic() - stamp > self._recv_timeout:
538
+ raise MMQTTException(
539
+ f"No data received from broker for {self._recv_timeout} seconds."
540
+ )
541
+
530
542
def disconnect(self):
531
543
"""Disconnects the MiniMQTT client from the MQTT broker."""
532
544
self.is_connected()
@@ -637,6 +649,7 @@ def publish(self, topic, msg, retain=False, qos=0):
637
649
if qos == 0 and self.on_publish is not None:
638
650
self.on_publish(self, self._user_data, topic, self._pid)
639
651
if qos == 1:
652
+ stamp = time.monotonic()
640
653
while True:
641
654
op = self._wait_for_msg()
642
655
if op == 0x40:
@@ -649,6 +662,12 @@ def publish(self, topic, msg, retain=False, qos=0):
649
662
self.on_publish(self, self._user_data, topic, rcv_pid)
650
663
return
651
664
665
+ if op is None:
666
+ if time.monotonic() - stamp > self._recv_timeout:
667
+ raise MMQTTException(
668
+ f"No data received from broker for {self._recv_timeout} seconds."
669
+ )
670
+
652
671
def subscribe(self, topic, qos=0):
653
672
"""Subscribes to a topic on the MQTT Broker.
654
673
This method can subscribe to one topics or multiple topics.
@@ -697,6 +716,7 @@ def subscribe(self, topic, qos=0):
697
716
for t, q in topics:
698
717
self.logger.debug("SUBSCRIBING to topic %s with QoS %d", t, q)
699
718
self._sock.send(packet)
719
+ stamp = time.monotonic()
700
720
while True:
701
721
op = self._wait_for_msg()
702
722
if op == 0x90:
@@ -710,6 +730,12 @@ def subscribe(self, topic, qos=0):
710
730
self._subscribed_topics.append(t)
711
731
return
712
732
733
+ if op is None:
734
+ if time.monotonic() - stamp > self._recv_timeout:
735
+ raise MMQTTException(
736
+ f"No data received from broker for {self._recv_timeout} seconds."
737
+ )
738
+
713
739
def unsubscribe(self, topic):
714
740
"""Unsubscribes from a MQTT topic.
715
741
@@ -747,6 +773,7 @@ def unsubscribe(self, topic):
747
773
if self.logger is not None:
748
774
self.logger.debug("Waiting for UNSUBACK...")
749
775
while True:
776
+ stamp = time.monotonic()
750
777
op = self._wait_for_msg()
751
778
if op == 176:
752
779
rc = self._sock_exact_recv(3)
@@ -759,6 +786,12 @@ def unsubscribe(self, topic):
759
786
self._subscribed_topics.remove(t)
760
787
return
761
788
789
+ if op is None:
790
+ if time.monotonic() - stamp > self._recv_timeout:
791
+ raise MMQTTException(
792
+ f"No data received from broker for {self._recv_timeout} seconds."
793
+ )
794
+
762
795
def reconnect(self, resub_topics=True):
763
796
"""Attempts to reconnect to the MQTT broker.
764
797
0 commit comments