Skip to content

Commit

Permalink
Sleep only if sleep_time is positive
Browse files Browse the repository at this point in the history
  • Loading branch information
niemijoe committed Sep 5, 2022
1 parent cc74c0a commit 514fd09
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions mqtt_data_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,22 @@ def main():

sleep_time = time_end - time.time()
print(sleep_time)
# When listen period has passed, send data to Azure
time.sleep(sleep_time)

# Only sleep if sleep_time is positive. This can happen if sending data to Azure took longer than MONITOR_PERIOD_IN_SECONDS
if sleep_time > 0:
# Sleep while listen period is going, after that we send data to Azure
time.sleep(sleep_time)

# Set time_end as MONITOR_PERIOD_IN_SECONDS in the future
time_end = time.time() + MONITOR_PERIOD_IN_SECONDS
topic_data_map = {}

# Save message counters into topic_data_map and reset them in each topic
for topic in topic_list:
topic_data_map_key = f"{topic.topic_address}:{topic.topic_name}:{topic.topic_port}"
topic_data_map[topic_data_map_key] = topic.msg_count
topic.msg_count = 0

send_mqtt_msg_count_to_azure(topic_data_map)

def send_mqtt_msg_count_to_azure(topic_data_map):
Expand Down

0 comments on commit 514fd09

Please sign in to comment.