eive-tmtc/tmtcc.py

154 lines
5.4 KiB
Python
Raw Normal View History

2022-05-17 11:13:32 +02:00
import argparse
2022-07-04 15:22:53 +02:00
import logging
2022-05-17 11:13:32 +02:00
import sys
import traceback
from typing import Optional
2022-05-31 16:42:53 +02:00
from tmtccmd.config.definitions import CoreModeList
2022-07-04 15:22:53 +02:00
from spacepackets.ecss import PusVerificator
from tmtccmd import get_console_logger, TcHandlerBase
2022-07-04 17:59:09 +02:00
from tmtccmd.com_if import ComInterface
2022-05-31 16:42:53 +02:00
from tmtccmd.config.globals import update_global, CoreGlobalIds
2022-05-17 11:13:32 +02:00
2022-07-04 15:22:53 +02:00
from deps.tmtccmd.tmtccmd.logging.pus import RawTmtcTimedLogWrapper
from deps.tmtccmd.tmtccmd.pus import VerificationWrapper
from deps.tmtccmd.tmtccmd.tm import SpecificApidHandlerBase, GenericApidHandlerBase
from deps.tmtccmd.tmtccmd.utility.tmtc_printer import FsfwTmTcPrinter
2022-07-04 17:59:09 +02:00
from tmtccmd.logging import get_current_time_string
2022-07-04 15:22:53 +02:00
from tmtccmd.pus import FileSeqCountProvider
2022-07-04 17:59:09 +02:00
from tmtccmd.tc import (
ProcedureHelper,
FeedWrapper,
TcProcedureType,
QueueEntryHelper,
TcQueueEntryType,
)
2022-07-04 15:22:53 +02:00
2022-05-17 11:13:32 +02:00
try:
import spacepackets
except ImportError as error:
print(error)
print("Python spacepackets module could not be imported")
print(
'Install with "cd spacepackets && python3 -m pip intall -e ." for interative installation'
)
sys.exit(1)
try:
import tmtccmd.runner as tmtccmd
from tmtccmd.logging.pus import create_tmtc_logger
from tmtccmd.ccsds.handler import ApidHandler, CcsdsTmHandler
from tmtccmd.config import SetupArgs, default_json_path
from tmtccmd.config.args import (
create_default_args_parser,
add_default_tmtccmd_args,
parse_default_input_arguments,
)
except ImportError as error:
run_tmtc_commander = None
initialize_tmtc_commander = None
tb = traceback.format_exc()
print(tb)
print("Python tmtccmd submodule could not be imported")
sys.exit(1)
from config import __version__
from config.definitions import PUS_APID
from config.hook_implementations import EiveHookObject
2022-07-04 15:22:53 +02:00
from pus_tm.factory_hook import pus_factory_hook
2022-07-04 17:59:09 +02:00
from pus_tc.procedure_packer import pre_tc_send_cb, handle_default_procedure
2022-07-04 15:22:53 +02:00
LOGGER = get_console_logger()
class PusHandler(SpecificApidHandlerBase):
def __init__(
self,
wrapper: VerificationWrapper,
printer: FsfwTmTcPrinter,
raw_logger: RawTmtcTimedLogWrapper,
):
super().__init__(PUS_APID, None)
self.printer = printer
self.verif_wrapper = wrapper
self.raw_logger = raw_logger
def handle_tm(self, packet: bytes, _user_args: any):
pus_factory_hook(packet, self.verif_wrapper, self.printer, self.raw_logger)
class UnknownApidHandler(GenericApidHandlerBase):
def handle_tm(self, apid: int, _packet: bytes, _user_args: any):
LOGGER.warning(f"Packet with unknwon APID {apid} detected")
class TcHandler(TcHandlerBase):
def __init__(
self,
seq_count_provider: FileSeqCountProvider,
pus_verificator: PusVerificator,
file_logger: logging.Logger,
raw_logger: RawTmtcTimedLogWrapper,
):
super().__init__()
self.seq_count_provider = seq_count_provider
self.pus_verificator = pus_verificator
self.file_logger = file_logger
self.raw_logger = raw_logger
def feed_cb(self, info: ProcedureHelper, wrapper: FeedWrapper):
if info.proc_type == TcProcedureType.DEFAULT:
handle_default_procedure(info.to_def_procedure(), wrapper)
2022-05-17 11:13:32 +02:00
2022-07-04 17:59:09 +02:00
def send_cb(self, entry_helper: QueueEntryHelper, com_if: ComInterface):
if entry_helper.is_tc:
if entry_helper.entry_type == TcQueueEntryType.PUS_TC:
pus_tc_wrapper = entry_helper.to_pus_tc_entry()
pus_tc_wrapper.pus_tc.seq_count = (
self.seq_count_provider.next_seq_count()
)
pus_tc_wrapper.pus_tc.apid = PUS_APID
# Add TC after Sequence Count stamping
self.pus_verificator.add_tc(pus_tc_wrapper.pus_tc)
raw_tc = pus_tc_wrapper.pus_tc.pack()
self.raw_logger.log_tc(pus_tc_wrapper.pus_tc)
tc_info_string = f"Sent {pus_tc_wrapper.pus_tc}"
LOGGER.info(tc_info_string)
self.file_logger.info(
f"{get_current_time_string(True)}: {tc_info_string}"
)
com_if.send(raw_tc)
elif entry_helper.entry_type == TcQueueEntryType.LOG:
log_entry = entry_helper.to_log_entry()
LOGGER.info(log_entry.log_str)
self.file_logger.info(log_entry.log_str)
2022-05-17 11:13:32 +02:00
def tmtcc_pre_args() -> EiveHookObject:
print(f"-- eive tmtc v{__version__} --")
print(f"-- spacepackets v{spacepackets.__version__} --")
tmtccmd.init_printout(False)
return EiveHookObject(json_cfg_path=default_json_path())
2022-05-17 17:21:03 +02:00
def tmtcc_post_args(
hook_obj: EiveHookObject, use_gui: bool, args: Optional[argparse.Namespace]
):
2022-05-17 11:13:32 +02:00
setup_args = SetupArgs(
hook_obj=hook_obj, use_gui=use_gui, apid=PUS_APID, cli_args=args
)
tmtc_file_logger = create_tmtc_logger()
apid_handler = ApidHandler(cb=ccsds_tm_handler, queue_len=50, user_args=None)
ccsds_handler = CcsdsTmHandler()
ccsds_handler.add_tm_handler(apid=PUS_APID, handler=apid_handler)
tmtccmd.setup(setup_args=setup_args)
tmtccmd.add_ccsds_handler(ccsds_handler)
2022-05-31 16:42:53 +02:00
if use_gui:
update_global(CoreGlobalIds.MODE, CoreModeList.LISTENER_MODE)
2022-05-17 11:13:32 +02:00
tmtc_backend = tmtccmd.create_default_tmtc_backend(
setup_args=setup_args,
tm_handler=ccsds_handler,
)
tmtc_backend.usr_send_wrapper = (pre_tc_send_cb, tmtc_file_logger)
tmtccmd.run(tmtc_backend=tmtc_backend)