in src/ipc_pubsub/mqtt_core_pubsub.py [0:0]
def publish_to_mqtt(self, topic, message_object):
'''
Publish a Python object serlized as a JSON message to the IoT Core MQTT topic.
'''
try:
log.info('MQTT PUBLISH: topic: {} - Message: {}'.format(topic, message_object))
self.mqtt_request.topic_name = topic
json_message = json.dumps(message_object)
self.mqtt_request.payload = bytes(json_message, "utf-8")
operation = self.mqtt_publish_client.new_publish_to_iot_core()
operation.activate(self.mqtt_request)
future = operation.get_response()
future.result(self.mqtt_timeout)
except KeyError as key_error: # pragma: no cover # includes requests for fields that don't exist in the received object
log.error('KEY_ERROR: KeyError occurred while publishing to IoT Core on MQTT Topic. ERROR MESSAGE: {} - TOPIC: {} - MESSAGE: {}'.format(key_error, topic, message_object))
except concurrent.futures.TimeoutError as timeout_error: # pragma: no cover
log.error('TIMEOUT_ERROR: Timeout occurred while publishing to IoT Core on MQTT Topic. ERROR MESSAGE: {} - TOPIC: {} - MESSAGE: {}'.format(timeout_error, topic, message_object))
except UnauthorizedError as unauth_error: # pragma: no cover
log.error('UNAUTHORIZED_ERROR: Unauthorized error while publishing to IoT Core on MQTT Topic. ERROR MESSAGE: {} - TOPIC: {} - MESSAGE: {}'.format(unauth_error, topic, message_object))
except Exception as err: # pragma: no cover
log.error('EXCEPTION: Exception while publishing to IoT Core on MQTT Topic. ERROR MESSAGE: {} - TOPIC: {} - MESSAGE: {}'.format(err, topic, message_object))