Skip to content

Commit 7277872

Browse files
committed
handle ack packet
1 parent 33016f1 commit 7277872

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

AWSIoTPythonSDK/core/protocol/mqtt_core.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from AWSIoTPythonSDK.core.protocol.internal.defaults import ALPN_PROTCOLS
3030
from AWSIoTPythonSDK.core.protocol.internal.events import FixedEventMids
3131
from AWSIoTPythonSDK.core.protocol.paho.client import MQTT_ERR_SUCCESS
32+
from AWSIoTPythonSDK.core.protocol.paho.client import MQTT_ERR_SUBACK_ERROR
3233
from AWSIoTPythonSDK.exception.AWSIoTExceptions import connectError
3334
from AWSIoTPythonSDK.exception.AWSIoTExceptions import connectTimeoutException
3435
from AWSIoTPythonSDK.exception.AWSIoTExceptions import disconnectError
@@ -58,6 +59,12 @@
5859
from queue import Queue
5960

6061

62+
class AckPacket(object):
63+
def __init__(self):
64+
self.event = Event()
65+
self.data = None
66+
67+
6168
class MqttCore(object):
6269

6370
_logger = logging.getLogger(__name__)
@@ -298,12 +305,15 @@ def subscribe(self, topic, qos, message_callback=None):
298305
if ClientStatus.STABLE != self._client_status.get_status():
299306
self._handle_offline_request(RequestTypes.SUBSCRIBE, (topic, qos, message_callback, None))
300307
else:
301-
event = Event()
302-
rc, mid = self._subscribe_async(topic, qos, self._create_blocking_ack_callback(event), message_callback)
303-
if not event.wait(self._operation_timeout_sec):
308+
ack = AckPacket()
309+
rc, mid = self._subscribe_async(topic, qos, self._create_blocking_ack_callback_ret(ack), message_callback)
310+
if not ack.event.wait(self._operation_timeout_sec):
304311
self._internal_async_client.remove_event_callback(mid)
305312
self._logger.error("Subscribe timed out")
306313
raise subscribeTimeoutException()
314+
if ack.data[0] == MQTT_ERR_SUBACK_ERROR:
315+
self._logger.error(f"Subscribe error: {ack.data}")
316+
raise subscribeError(ack.data)
307317
ret = True
308318
return ret
309319

@@ -361,6 +371,12 @@ def ack_callback(mid, data=None):
361371
event.set()
362372
return ack_callback
363373

374+
def _create_blocking_ack_callback_ret(self, ack: AckPacket):
375+
def ack_callback(mid, data=None):
376+
ack.data = data
377+
ack.event.set()
378+
return ack_callback
379+
364380
def _handle_offline_request(self, type, data):
365381
self._logger.info("Offline request detected!")
366382
offline_request = QueueableRequest(type, data)

AWSIoTPythonSDK/core/protocol/paho/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@
137137
MSG_QUEUEING_DROP_OLDEST = 0
138138
MSG_QUEUEING_DROP_NEWEST = 1
139139

140+
# Packet Error Codes
141+
MQTT_ERR_SUBACK_ERROR = 0x80
142+
140143
if sys.version_info[0] < 3:
141144
sockpair_data = "0"
142145
else:

0 commit comments

Comments
 (0)