From 2c318b7c062aaf768902c7ccb2e598a51a8e34cd Mon Sep 17 00:00:00 2001
From: Robin Mueller <robin.mueller.m@gmail.com>
Date: Wed, 11 Mar 2020 18:45:35 +0100
Subject: [PATCH] multiple command serverreceiver bugfixes

---
 comIF/OBSW_ComInterface.py                    |  6 +--
 comIF/OBSW_Serial_ComIF.py                    | 13 +++--
 .../OBSW_MultipleCommandsSenderReceiver.py    | 53 +++++++++----------
 sendreceive/OBSW_TmListener.py                |  2 +-
 4 files changed, 36 insertions(+), 38 deletions(-)

diff --git a/comIF/OBSW_ComInterface.py b/comIF/OBSW_ComInterface.py
index 49cab7f..5121548 100644
--- a/comIF/OBSW_ComInterface.py
+++ b/comIF/OBSW_ComInterface.py
@@ -8,11 +8,11 @@ Description: Generic Communication Interface. Defines the syntax of the communic
 @author: R. Mueller
 """
 from abc import abstractmethod
-from typing import TypeVar, Tuple, Union, List
+from typing import TypeVar, Tuple, Union, List, Deque
 from tm.OBSW_PusTm import pusPacketT, pusPacketInfoT
 
 ComIF_T = TypeVar('ComIF_T', bound='CommunicationInterface')
-pusTupleT = Tuple[pusPacketT, pusPacketInfoT]
+pusTupleQueueT = Deque[Tuple[pusPacketT, pusPacketInfoT]]
 packetListT = List[pusPacketT]
 
 
@@ -68,7 +68,7 @@ class CommunicationInterface:
         pass
 
     # Receive Telemetry and store a tuple consisting of TM information and TM packet into queue
-    def receiveTelemetryAndStoreTuple(self, tmTupleQueue) -> Union[None, pusTupleT]:
+    def receiveTelemetryAndStoreTuple(self, tmTupleQueue) -> Union[None, pusTupleQueueT]:
         pass
 
 
diff --git a/comIF/OBSW_Serial_ComIF.py b/comIF/OBSW_Serial_ComIF.py
index 5241e3c..41cd950 100644
--- a/comIF/OBSW_Serial_ComIF.py
+++ b/comIF/OBSW_Serial_ComIF.py
@@ -8,8 +8,8 @@
 import time
 import serial
 import logging
-from typing import Tuple, Union
-from comIF.OBSW_ComInterface import CommunicationInterface, pusTupleT, packetListT
+from typing import Tuple, Union, Deque
+from comIF.OBSW_ComInterface import CommunicationInterface, pusTupleQueueT, packetListT
 from tm.OBSW_TmPacket import PUSTelemetryFactory
 
 
@@ -113,13 +113,12 @@ class SerialComIF(CommunicationInterface):
             for packet in pusPackets:
                 tmQueue.put(packet)
 
-    def receiveTelemetryAndStoreTuple(self, tmTupleQueue) -> Union[None, pusTupleT]:
+    def receiveTelemetryAndStoreTuple(self, tmTupleQueue: Deque) -> pusTupleQueueT:
         (packetReceived, pusPackets) = self.pollInterface()
         if packetReceived:
             for packet in pusPackets:
                 tmInfo = packet.packTmInformation()
-                tmTupleQueue.put(packet, tmInfo)
-                return tmTupleQueue
-        else:
-            return None
+                tmTupleQueue.append((packet, tmInfo))
+        return tmTupleQueue
+
 
diff --git a/sendreceive/OBSW_MultipleCommandsSenderReceiver.py b/sendreceive/OBSW_MultipleCommandsSenderReceiver.py
index bc3ff58..fc3e866 100644
--- a/sendreceive/OBSW_MultipleCommandsSenderReceiver.py
+++ b/sendreceive/OBSW_MultipleCommandsSenderReceiver.py
@@ -54,9 +54,9 @@ class MultipleCommandSenderReceiver(SequentialCommandSenderReceiver):
         # time.sleep(0.5)
         try:
             self.sendAllQueue()
-            while not self.allRepliesReceived:
-                time.sleep(0.5)
-            self.tmListener.retrieveTmInfoQueue()
+            self.handleLastRepliesListening(self.tmTimeout/2.0)
+            self.tmListener.modeOpFinished.set()
+            self.tmInfoQueue = self.tmListener.retrieveTmInfoQueue()
             # self.handleTcResending() Turned off for now, not needed
             if self.doPrintToFile:
                 self.tmtcPrinter.printToFile()
@@ -93,10 +93,16 @@ class MultipleCommandSenderReceiver(SequentialCommandSenderReceiver):
 
     def retrieveTmInfoQueue(self):
         if self.tmListener.replyEvent.is_set():
-            self.tmInfoQueue = self.tmListener.tmInfoQueue
+            return self.tmListener.tmInfoQueue
         else:
             print("Multiple Command SenderReceiver: Configuration error, reply event not set in TM listener")
 
+    @staticmethod
+    def handleLastRepliesListening(waitTime: float):
+        elapsed_time_seconds = 0
+        start_time = time.time()
+        while elapsed_time_seconds < waitTime:
+            elapsed_time_seconds = time.time() - start_time
     # def checkForMultipleReplies(self):
     #   super().checkForMultipleReplies()
 
@@ -105,29 +111,22 @@ class MultipleCommandSenderReceiver(SequentialCommandSenderReceiver):
         #        # listen for duration timeoutInSeconds for replies
         #        self.handleReplyListening()
 
-    def handleReplyListening(self):
-        tmReady = self.comInterface.dataAvailable(2.0)
-        if tmReady:
-            self.receiveTelemetryAndStoreInformation()
-        if self.tcQueue.empty():
-            print("TC queue empty. Listening for a few more seconds ... ")
-            start_time = time.time()
-            self.handleLastRepliesListening(start_time)
-            self.allRepliesReceived = True
-
-    def receiveTelemetryAndStoreInformation(self):
-        packetList = self.comInterface.receiveTelemetry()
-        for counter in range(0, len(packetList)):
-            tmInfo = packetList[counter].packTmInformation()
-            self.tmInfoQueue.append(tmInfo)
-
-    def handleLastRepliesListening(self, start_time):
-        elapsed_time_seconds = 0
-        while elapsed_time_seconds < self.tmTimeout/2.0:
-            elapsed_time_seconds = time.time() - start_time
-            tmReady = self.comInterface.dataAvailable(self.tmTimeout/4.0)
-            if tmReady:
-                self.receiveTelemetryAndStoreInformation()
+    # def handleReplyListening(self):
+    #     tmReady = self.comInterface.dataAvailable(2.0)
+    #     if tmReady:
+    #         self.receiveTelemetryAndStoreInformation()
+    #     if self.tcQueue.empty():
+    #         print("TC queue empty. Listening for a few more seconds ... ")
+    #         start_time = time.time()
+    #         self.handleLastRepliesListening(start_time)
+    #         self.allRepliesReceived = True
+
+    # def receiveTelemetryAndStoreInformation(self):
+    #     packetList = self.comInterface.receiveTelemetry()
+    #     for counter in range(0, len(packetList)):
+    #         tmInfo = packetList[counter].packTmInformation()
+    #         self.tmInfoQueue.append(tmInfo)
+
 
 
 
diff --git a/sendreceive/OBSW_TmListener.py b/sendreceive/OBSW_TmListener.py
index 8fccb0c..2c83842 100644
--- a/sendreceive/OBSW_TmListener.py
+++ b/sendreceive/OBSW_TmListener.py
@@ -90,7 +90,7 @@ class TmListener:
                 print("TM Listener: Reply sequence received!")
                 self.replyEvent.set()
         elif self.modeId == g.modeList.UnitTest:
-            currentPackets = self.comInterface.receiveTelemetryAndStoreTuple()
+            self.comInterface.receiveTelemetryAndStoreTuple(self.tmInfoQueue)
 
     def checkForOneTelemetrySequence(self) -> bool:
         """
-- 
GitLab