Skip to content

Commit 36d3be7

Browse files
author
graebm
committed
don't need to check QoS after subscribe anymore
1 parent d9fd2fb commit 36d3be7

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

awsiot/__init__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,20 @@ def _subscribe_operation(self, topic, qos, callback, payload_to_class_fn):
125125
message as JSON.
126126
127127
Returns two values. The first is a `Future` which will contain a result
128-
of `None` when the server has acknowledged the subscription, or an
129-
exception if the subscription fails. The second value is a topic which
130-
may be passed to `unsubscribe()` to stop receiving messages.
128+
of `awscrt.mqtt.QoS` when the server has acknowledged the subscription,
129+
or an exception if the subscription fails. The second value is a topic
130+
which may be passed to `unsubscribe()` to stop receiving messages.
131131
Note that messages may arrive before the subscription is acknowledged.
132132
"""
133133

134134
future = Future() # type: Future
135135
try:
136136
def on_suback(suback_future):
137-
if suback_future.exception():
138-
future.set_exception(suback_future.exception())
139-
else:
140-
future.set_result(None)
137+
try:
138+
suback_result = suback_future.result()
139+
future.set_result(suback_result['qos'])
140+
except Exception as e:
141+
future.set_exception(e)
141142

142143
def callback_wrapper(topic, payload_bytes):
143144
try:

samples/basic_discovery.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ def on_publish(topic, message):
121121
print('Publish received on topic {}'.format(topic))
122122
print(message)
123123

124-
subscribe_future = mqtt_connection.subscribe(args.topic, QoS.AT_MOST_ONCE, on_publish)
125-
subscribe_future[0].result()
124+
subscribe_future, _ = mqtt_connection.subscribe(args.topic, QoS.AT_MOST_ONCE, on_publish)
125+
subscribe_result = subscribe_future.result()
126126

127127
loop_count = 0
128128
while loop_count < args.max_pub_ops:

samples/pubsub.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def on_message_received(topic, message):
108108
port = port,
109109
use_websocket=False,
110110
clean_session=True,
111-
keep_alive=5) # DO NOT COMMIT THIS CHANGE
111+
keep_alive=6000)
112112

113113
# Future.result() waits until a result is available
114114
connect_future.result()
@@ -122,9 +122,7 @@ def on_message_received(topic, message):
122122
callback=on_message_received)
123123

124124
subscribe_result = subscribe_future.result()
125-
if subscribe_result['qos'] is None:
126-
raise RuntimeError("Server rejected subscription")
127-
print("Subscribed!")
125+
print("Subscribed with {}".format(str(subscribe_result['qos'])))
128126

129127
# Publish message to server desired number of times.
130128
# This step is skipped if message is blank.

0 commit comments

Comments
 (0)