From 9feaa83c7d437ca9d45e4b31699622338a9fbfe8 Mon Sep 17 00:00:00 2001 From: Privatecoder <45964815+Privatecoder@users.noreply.github.com> Date: Sun, 18 Feb 2024 16:09:45 +0100 Subject: [PATCH] unified logs, added check if serial connection is open to exit-handler --- src/entrypoint.sh | 2 +- src/fetch_bms_data.py | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/entrypoint.sh b/src/entrypoint.sh index 817d0d0..bbee83c 100644 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -2,7 +2,7 @@ # Function to handle SIGTERM cleanup() { - echo "Container stopped, cleaning up..." + echo "INFO:SeplosBMS:Container stopped, cleaning up and exiting..." # Send SIGTERM to the Python script kill -s SIGTERM $! wait $! diff --git a/src/fetch_bms_data.py b/src/fetch_bms_data.py index ecdb35e..1ec423a 100644 --- a/src/fetch_bms_data.py +++ b/src/fetch_bms_data.py @@ -30,13 +30,12 @@ def graceful_exit(signum=None, frame=None): mqtt_client.loop_stop() # close serial connections if open - if SERIAL_MASTER_INSTANCE is not None: + if SERIAL_MASTER_INSTANCE is not None and SERIAL_MASTER_INSTANCE.isOpen(): logger.info("Closing serial connection to master") SERIAL_MASTER_INSTANCE.close() - if SERIAL_SLAVES_INSTANCE is not None: + if SERIAL_SLAVES_INSTANCE is not None and SERIAL_SLAVES_INSTANCE.isOpen(): logger.info("Closing serial connection to slaves") SERIAL_SLAVES_INSTANCE.close() - logger.info("Exiting the program.") if signum is not None: sys.exit(0) @@ -1158,11 +1157,11 @@ def read_serial_data(self): # catch exceptions related to the initial connection to the serial port except serial.SerialException as e: - logger.error("Serial port disconnected, exiting...") + logger.error("Serial port disconnected, cleaning up and exiting...") # handle keyboard-interruption except KeyboardInterrupt: - logger.info("Interrupt received! Cleaning up...") + logger.info("Interrupt received, cleaning up and exiting...") finally: graceful_exit()