From fea6bad8bbfda61dccc7685fcec77f7ff3f1e4a9 Mon Sep 17 00:00:00 2001 From: violetaperezandrade Date: Fri, 19 Jan 2024 19:58:06 -0300 Subject: [PATCH] add and read from config file --- src/config.json | 10 ++++++++++ src/main.py | 33 ++++++++++++++------------------- 2 files changed, 24 insertions(+), 19 deletions(-) create mode 100644 src/config.json diff --git a/src/config.json b/src/config.json new file mode 100644 index 0000000..50648f3 --- /dev/null +++ b/src/config.json @@ -0,0 +1,10 @@ +{ + "packet_period": 1, + "device_id": "97bebd21-a9c8-41a3-9a88-969cd3dd7bba", + "deviations": { + "temperature": 3, + "humidity": 5, + "light": 25, + "watering": 5 + } +} \ No newline at end of file diff --git a/src/main.py b/src/main.py index 47b3ec1..e08abe8 100644 --- a/src/main.py +++ b/src/main.py @@ -9,7 +9,8 @@ - Enviar paquete a la queue. ''' -import time, random +import time +import json import logging from data_packet import generate_data, create_packet, data_has_changed @@ -34,28 +35,22 @@ def simulate_packets(config): finally: time.sleep(config["packet_period"]) - -''' -Reads the config file. At this moment, it is mocked. -''' - - def read_config_file(path): - # TODO - return { - "packet_period": 1, - "device_id": str(random.getrandbits(128)), - "deviations": { - "temperature": 3, - "humidity": 5, - "light": 25, - "watering": 5 - } - } + try: + with open(path, 'r') as file: + config_data = json.load(file) + return config_data + except FileNotFoundError: + logging.error(f"Config file not found at: {path}") + raise + except json.JSONDecodeError as json_err: + logging.error(f"Error decoding config file: {json_err}") + raise if __name__ == '__main__': logging.basicConfig(level=logging.INFO) - config = read_config_file("") + config_path = "config.json" + config = read_config_file(config_path) simulate_packets(config)