Skip to content

Commit

Permalink
unified logs, added check if serial connection is open to exit-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Privatecoder committed Feb 18, 2024
1 parent 03ed44c commit 9feaa83
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 $!
Expand Down
9 changes: 4 additions & 5 deletions src/fetch_bms_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

0 comments on commit 9feaa83

Please sign in to comment.