in awsiot/__init__.py [0:0]
def _publish_operation(self, topic: str, qos: int, payload: Optional[PayloadObj]) -> Future:
"""
Performs a 'Publish' style operation for an MQTT service.
Parameters:
topic - The topic to publish this message to.
qos - The Quality of Service guarantee of this message
payload - (Optional) If set, the message will be a string of JSON, built from this object.
If unset, an empty message is sent.
Returns a `Future` which will contain a result of `None` when the
server has acknowledged the message, or an exception if the
publish fails.
"""
future = Future() # type: Future
try:
def on_puback(puback_future):
if puback_future.exception():
future.set_exception(puback_future.exception())
else:
future.set_result(None)
if payload is None:
payload_str = ""
else:
payload_str = json.dumps(payload)
pub_future, _ = self.mqtt_connection.publish(
topic=topic,
payload=payload_str,
qos=qos,
)
pub_future.add_done_callback(on_puback)
except Exception as e:
future.set_exception(e)
return future