Skip to content

Commit f0e2eea

Browse files
committed
format
1 parent 3d87bf6 commit f0e2eea

3 files changed

Lines changed: 46 additions & 32 deletions

File tree

node/node/app.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
from node.read import read_gpio_sensors
44
from node.transmit import send_to_rabbitmq
55

6-
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
6+
logging.basicConfig(
7+
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
8+
)
9+
710

811
async def run():
912
while True:
@@ -13,12 +16,14 @@ async def run():
1316
logging.info("Data sent to RabbitMQ")
1417
await asyncio.sleep(1)
1518

19+
1620
def main():
1721
try:
1822
asyncio.run(run())
1923
except KeyboardInterrupt:
2024
logging.info("Program terminated by user")
2125
return 0
2226

27+
2328
if __name__ == "__main__":
2429
main()

node/node/read/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .read import read_gpio_sensors
22

3-
__all__ = ['read_gpio_sensors']
3+
__all__ = ["read_gpio_sensors"]

node/node/transmit/transmit.py

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,47 @@
33
from node.logger import ops_logger
44
from node.settings import CONFIG
55

6+
67
def send_to_rabbitmq(data):
7-
connection = None
8-
try:
9-
ops_logger.info("Attempting to connect to RabbitMQ")
10-
if CONFIG["rabbitmq_user"] and CONFIG["rabbitmq_password"]:
11-
credentials = pika.PlainCredentials(CONFIG["rabbitmq_user"], CONFIG["rabbitmq_password"])
12-
else:
13-
credentials = pika.PlainCredentials("guest", "guest")
14-
connection = pika.BlockingConnection(pika.ConnectionParameters(host=CONFIG["rabbitmq_host"], port=CONFIG["rabbitmq_port"], credentials=credentials))
15-
channel = connection.channel()
16-
ops_logger.info("Connected to RabbitMQ")
8+
connection = None
9+
try:
10+
ops_logger.info("Attempting to connect to RabbitMQ")
11+
if CONFIG["rabbitmq_user"] and CONFIG["rabbitmq_password"]:
12+
credentials = pika.PlainCredentials(
13+
CONFIG["rabbitmq_user"], CONFIG["rabbitmq_password"]
14+
)
15+
else:
16+
credentials = pika.PlainCredentials("guest", "guest")
17+
connection = pika.BlockingConnection(
18+
pika.ConnectionParameters(
19+
host=CONFIG["rabbitmq_host"],
20+
port=CONFIG["rabbitmq_port"],
21+
credentials=credentials,
22+
)
23+
)
24+
channel = connection.channel()
25+
ops_logger.info("Connected to RabbitMQ")
1726

18-
ops_logger.info(f"Declaring queue: {CONFIG['rabbitmq_queue']}")
19-
channel.queue_declare(queue=CONFIG["rabbitmq_queue"], durable=True)
27+
ops_logger.info(f"Declaring queue: {CONFIG['rabbitmq_queue']}")
28+
channel.queue_declare(queue=CONFIG["rabbitmq_queue"], durable=True)
2029

21-
ops_logger.info(f"Publishing data to queue: {CONFIG['rabbitmq_queue']}")
22-
channel.basic_publish(
23-
exchange='',
24-
routing_key=CONFIG["rabbitmq_queue"],
25-
body=json.dumps(data)
26-
)
30+
ops_logger.info(f"Publishing data to queue: {CONFIG['rabbitmq_queue']}")
31+
channel.basic_publish(
32+
exchange="",
33+
routing_key=CONFIG["rabbitmq_queue"],
34+
body=json.dumps(data),
35+
)
2736

28-
ops_logger.info(f"Sent data to RabbitMQ: {data}")
29-
except pika.exceptions.AMQPConnectionError as e:
30-
ops_logger.error(f"RabbitMQ connection error")
31-
except pika.exceptions.AMQPChannelError as e:
32-
ops_logger.error(f"RabbitMQ channel error")
33-
except Exception as e:
34-
ops_logger.error(f"RabbitMQ error")
35-
finally:
36-
try:
37-
connection.close()
38-
ops_logger.info("Closed RabbitMQ connection")
37+
ops_logger.info(f"Sent data to RabbitMQ: {data}")
38+
except pika.exceptions.AMQPConnectionError as e:
39+
ops_logger.error("RabbitMQ connection error")
40+
except pika.exceptions.AMQPChannelError as e:
41+
ops_logger.error("RabbitMQ channel error")
3942
except Exception as e:
40-
ops_logger.error(f"Error closing RabbitMQ connection: {e}")
43+
ops_logger.error("RabbitMQ error")
44+
finally:
45+
try:
46+
connection.close()
47+
ops_logger.info("Closed RabbitMQ connection")
48+
except Exception as e:
49+
ops_logger.error(f"Error closing RabbitMQ connection: {e}")

0 commit comments

Comments
 (0)