eive-tmtc/tmtcc.py

124 lines
4.1 KiB
Python

import argparse
import logging
import sys
import traceback
from typing import Optional
from tmtccmd.config.definitions import CoreModeList
from spacepackets.ecss import PusVerificator
from tmtccmd import get_console_logger, TcHandlerBase
from tmtccmd.config.globals import update_global, CoreGlobalIds
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
from tmtccmd.pus import FileSeqCountProvider
from tmtccmd.tc import ProcedureHelper, FeedWrapper, TcProcedureType
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
from pus_tm.factory_hook import pus_factory_hook
from pus_tc.procedure_packer import pre_tc_send_cb
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)
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())
def tmtcc_post_args(
hook_obj: EiveHookObject, use_gui: bool, args: Optional[argparse.Namespace]
):
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)
if use_gui:
update_global(CoreGlobalIds.MODE, CoreModeList.LISTENER_MODE)
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)