Skip to content

Commit

Permalink
working on a usable continuous mode
Browse files Browse the repository at this point in the history
  • Loading branch information
umohr-irs committed May 22, 2022
1 parent 208f577 commit b031eef
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 7 deletions.
25 changes: 25 additions & 0 deletions src/tmtccmd/core/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,31 @@ def close_listener(self, join: bool = False, join_timeout_seconds: float = 1.0):
if join:
close_thread.join(timeout=join_timeout_seconds)


def performPacking(self):
service_queue = deque()
service_queue_packer = ServiceQueuePacker()
service_queue_packer.pack_service_queue_core(
service=self.__service,
service_queue=service_queue,
op_code=self.__op_code,
)
if not self.__com_if.valid:
return None
return service_queue

def performSending(self, service_queue):
try:
LOGGER.info("Performing service command operation")
self.daemon_receiver.set_tc_queue(service_queue)
self.daemon_receiver.send_queue_tc_and_return()
except KeyboardInterrupt:
LOGGER.info("Keyboard Interrupt.")
sys.exit()
except IOError:
LOGGER.exception("IO Error occured")
sys.exit()

def perform_operation(self):
"""Periodic operation"""
try:
Expand Down
37 changes: 35 additions & 2 deletions src/tmtccmd/core/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ def run_worker(self):
LOGGER.warning("Unknown worker operation code!")
self.finished.emit()

class ContWorkerThread(QObject):
finished = pyqtSignal()

def __init__(self, queue, tmtc_handler: TmTcHandler):
super(QObject, self).__init__()
self.queue = queue
self.tmtc_handler = tmtc_handler


def run_worker(self):
self.tmtc_handler.performSending(self.queue)
self.finished.emit()


class RunnableThread(QRunnable):
"""
Expand Down Expand Up @@ -213,8 +226,15 @@ def __start_seq_cmd_op(self):
self._tmtc_handler.set_opcode(self._current_op_code)

if self._tmtc_handler.mode == CoreModeList.CONTINUOUS_MODE:
self._tmtc_handler.perform_operation()
self.__set_send_button(True)
# uses a widget to ask for parameters, so needs to be in this thread
queue = self._tmtc_handler.performPacking()

# now enter new thread for long lasting task
self.__start_qthread_task_continuous(
queue,
finish_callback=self.__finish_seq_cmd_op,
)
# TODO add abort window...
else :
self.__start_qthread_task(
op_code=WorkerOperationsCodes.SEQUENTIAL_COMMANDING,
Expand Down Expand Up @@ -418,6 +438,19 @@ def __set_up_pixmap(self, grid: QGridLayout, row: int) -> int:
grid.addWidget(label, row, 0, 1, 2)
row += 1
return row

def __start_qthread_task_continuous(self, queue, finish_callback):
self.__thread = QThread()
self.__worker = ContWorkerThread(queue, tmtc_handler=self._tmtc_handler)
self.__worker.moveToThread(self.__thread)

self.__thread.started.connect(self.__worker.run_worker)
self.__worker.finished.connect(self.__thread.quit)
self.__worker.finished.connect(self.__worker.deleteLater)
self.__thread.finished.connect(self.__thread.deleteLater)

self.__thread.finished.connect(finish_callback)
self.__thread.start()

def __start_qthread_task(self, op_code: WorkerOperationsCodes, finish_callback):
self.__thread = QThread()
Expand Down
48 changes: 43 additions & 5 deletions src/tmtccmd/sendreceive/sequential_sender_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def send_queue_tc_and_return(self):
# Set to true for first packet, otherwise nothing will be sent.
self._next_send_condition = True
if not self._tc_queue.__len__() == 0:
self.__check_next_tc_send()
self.__handle_tc_sending_without_tm_reception()
except (KeyboardInterrupt, SystemExit):
LOGGER.info("Keyboard Interrupt.")
sys.exit()
Expand All @@ -109,6 +109,28 @@ def __print_rem_timeout(self, op_divider: int, divisor: int = 15):
f"{rem_time:.01f} seconds wait time remaining"
)

def __handle_tc_sending_without_tm_reception(self):
"""Internal function which handles the given TC queue
TODO: Make it testable by not delaying here and removing the loop, make
this function runnable in discrete steps
"""
next_sleep = 0.2
tc_queue_is_empty_and_processed = False
while not tc_queue_is_empty_and_processed:
if self._tc_queue.__len__() == 0:
if self._wait_period == 0:
# cache this for last wait time
self._start_time = time.time()
tc_queue_is_empty_and_processed = True
if not tc_queue_is_empty_and_processed:
if not self.wait_period_ongoing():
self._wait_period = 0
# force sending of TC, we do not wait for TM
self._just_send_next_telecommand()
time.sleep(next_sleep)

LOGGER.info("SequentialSenderReceiver: All commands sent!")

def __handle_tc_sending_and_tm_reception(self):
"""Internal function which handles the given TC queue while also simultaneously
polling all TM.
Expand Down Expand Up @@ -184,8 +206,6 @@ def _send_next_telecommand(self) -> bool:
if not self._tc_queue:
return False
tc_queue_tuple = self._tc_queue.pop()
print("popped")
print(tc_queue_tuple)
if self.check_queue_entry(tc_queue_tuple):
self._start_time = time.time()
packet, cmd_info = tc_queue_tuple
Expand Down Expand Up @@ -213,13 +233,31 @@ def _send_next_telecommand(self) -> bool:
if self._usr_send_cb is not None:
queue_cmd, queue_cmd_arg = tc_queue_tuple
try:
print("callback")
self._usr_send_cb(
queue_cmd, self._com_if, queue_cmd_arg, self._usr_send_args
)
except TypeError:
LOGGER.exception("User TC send callback invalid")
print("try again")
# If the queue entry was not a telecommand, send next telecommand
self.__check_next_tc_send()
return True

def _just_send_next_telecommand(self) -> bool:
"""Sends the next telecommand and returns whether an actual telecommand was sent"""
# Queue empty. Can happen because a wait period might still be ongoing
if not self._tc_queue:
return False
tc_queue_tuple = self._tc_queue.pop()
if self.check_queue_entry(tc_queue_tuple):
self._start_time = time.time()
packet, cmd_info = tc_queue_tuple
if self._usr_send_cb is not None:
try:
self._usr_send_cb(
packet, self._com_if, cmd_info, self._usr_send_args
)
except TypeError:
LOGGER.exception("User TC send callback invalid")
else:
self._com_if.send(packet)
return True

0 comments on commit b031eef

Please sign in to comment.