def send_telemetry(self) -> None: messages_to_send = 20 # keep track of all our messages outstanding_messages = [] for i in range(0, messages_to_send): logger.info("Sending telemetry {}".format(i)) # Make a message payload = {"index": i, "text": "This is message # {}".format(i)} msg = Message(payload) # Get our telemetry topic telemetry_topic = topic_builder.build_telemetry_publish_topic( self.auth.device_id, self.auth.module_id, msg) # publish. Don't wait for the PUBACK mi = self.mqtt_client.publish(telemetry_topic, msg.get_binary_payload(), qos=1) if mi.rc: raise Exception(mqtt.error_string(mi.rc)) # Remember the message info for later outstanding_messages.append(mi) # Now go through and wait for everything to publish. for mi in outstanding_messages: mi.wait_for_publish()
def send_telemetry(self) -> None: messages_to_send = 20 outstanding_messages = [] start = time.time() for i in range(0, messages_to_send): logger.info("Sending telemetry {}".format(i)) payload = { "index": i, "text": "Hello from sample_4. This is message # {}".format(i), } msg = Message(payload) telemetry_topic = topic_builder.build_telemetry_publish_topic( self.auth.device_id, self.auth.module_id, msg ) mi = self.mqtt_client.publish( telemetry_topic, msg.get_binary_payload(), qos=1 ) outstanding_messages.append(mi) outstanding_messages = self.remove_completed_messages( outstanding_messages ) print( "{} sent, {} awaiting PUBACK".format( i + 1, len(outstanding_messages) ) ) while len(outstanding_messages): print("Waiting for {} messages".format(len(outstanding_messages))) outstanding_messages[0].wait_for_publish() outstanding_messages = self.remove_completed_messages( outstanding_messages ) end = time.time() print("Done sending telemetry.") print( "{} messages sent in {} seconds. {} messages per second".format( messages_to_send, end - start, messages_to_send / (end - start) ) )