From 8338b8a04417e0df374762c6fb4afb2d58f4f7d4 Mon Sep 17 00:00:00 2001
From: "Robin.Mueller" <robin.mueller.m@gmail.com>
Date: Sun, 26 Apr 2020 21:15:19 +0200
Subject: [PATCH] exit handler wiht logger

---
 obsw_tmtc_client.py                              | 16 ++++++++++------
 .../obsw_multiple_commands_sender_receiver.py    |  4 +++-
 sendreceive/obsw_sequential_sender_receiver.py   |  3 +--
 utility/obsw_exit_handler.py                     | 10 ++++++----
 4 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/obsw_tmtc_client.py b/obsw_tmtc_client.py
index d4c97f1..23ff7ac 100644
--- a/obsw_tmtc_client.py
+++ b/obsw_tmtc_client.py
@@ -118,11 +118,15 @@ class TmTcHandler:
 
     def perform_operation(self):
         while True:
-            if self.command_received:
-                self.handle_action()
-                self.command_received = False
-            logger.info("TMTC Client in idle mode")
-            time.sleep(5)
+            try:
+                if self.command_received:
+                    self.handle_action()
+                    self.command_received = False
+                logger.info("TMTC Client in idle mode")
+                time.sleep(5)
+            except (IOError, KeyboardInterrupt):
+                logger.info("Closing TMTC client.")
+
 
     def handle_action(self):
         tmtc_printer = TmTcPrinter(g.G_DISPLAY_MODE, g.G_PRINT_TO_FILE, True)
@@ -137,7 +141,7 @@ class TmTcHandler:
             sys.exit()
         else:
             communication_interface = set_communication_interface(tmtc_printer)
-            atexit.register(keyboard_interrupt_handler, comInterface=communication_interface)
+            atexit.register(keyboard_interrupt_handler, com_interface=communication_interface)
             tm_listener = TmListener(
                 com_interface=communication_interface, tm_timeout=g.G_TM_TIMEOUT,
                 tc_timeout_factor=g.G_TC_SEND_TIMEOUT_FACTOR)
diff --git a/sendreceive/obsw_multiple_commands_sender_receiver.py b/sendreceive/obsw_multiple_commands_sender_receiver.py
index aee4dab..8e7f67d 100644
--- a/sendreceive/obsw_multiple_commands_sender_receiver.py
+++ b/sendreceive/obsw_multiple_commands_sender_receiver.py
@@ -15,7 +15,9 @@ from comIF.obsw_com_interface import CommunicationInterface
 from utility.obsw_tmtc_printer import TmTcPrinter
 from sendreceive.obsw_tm_listener import TmListenerT
 import config.obsw_config as g
+from utility.obsw_tmtc_printer import get_logger
 
+logger = get_logger()
 
 class MultipleCommandSenderReceiver(SequentialCommandSenderReceiver):
     """
@@ -61,7 +63,7 @@ class MultipleCommandSenderReceiver(SequentialCommandSenderReceiver):
             if g.G_PRINT_TO_FILE:
                 self._tmtc_printer.print_to_file()
         except (KeyboardInterrupt, SystemExit):
-            print("Keyboard Interrupt or System Exit detected")
+            logger.info("Closing TMTC Client")
             exit()
 
         return self.tcInfoQueue, self.tmTupleQueue
diff --git a/sendreceive/obsw_sequential_sender_receiver.py b/sendreceive/obsw_sequential_sender_receiver.py
index 0bce546..83ba8d4 100644
--- a/sendreceive/obsw_sequential_sender_receiver.py
+++ b/sendreceive/obsw_sequential_sender_receiver.py
@@ -50,9 +50,8 @@ class SequentialCommandSenderReceiver(CommandSenderReceiver):
         try:
             self.__handle_tc_sending()
         except (KeyboardInterrupt, SystemExit):
-            print("Keyboard Interrupt or System Exit detected")
+            logger.info("Closing TMTC Client")
             sys.exit()
-            # self.handleInterrupt(receiverThread)
 
     def __handle_tc_sending(self):
         while not self.__all_replies_received:
diff --git a/utility/obsw_exit_handler.py b/utility/obsw_exit_handler.py
index 38232e9..ff9990b 100644
--- a/utility/obsw_exit_handler.py
+++ b/utility/obsw_exit_handler.py
@@ -1,6 +1,8 @@
 import signal
 from comIF.obsw_com_interface import CommunicationInterface
+from utility.obsw_logger import get_logger
 
+logger = get_logger()
 
 class GracefulKiller:
     kill_now = False
@@ -14,9 +16,9 @@ class GracefulKiller:
         print("I was killed")
 
 
-def keyboard_interrupt_handler(comInterface: CommunicationInterface):
-    print("Disconnect registered")
+def keyboard_interrupt_handler(com_interface: CommunicationInterface):
+    logger.info("Disconnect registered")
     # Unit Test closes Serial Port at the end
     # We could do some optional stuff here
-    if comInterface is not None:
-        comInterface.send_telecommand(bytearray([0, 0, 0, 0, 0]))
+    if com_interface is not None:
+        com_interface.send_telecommand(bytearray([0, 0, 0, 0, 0]))
-- 
GitLab