eive-tmtc/pus_tm/factory_hook.py

59 lines
2.2 KiB
Python

"""
@brief This file transfers control of TM parsing to the user
@details Template configuration file. Copy this folder to the TMTC commander root and adapt
it to your needs.
"""
from tmtccmd.tm.service_8_functional_cmd import Service8TM
from spacepackets.ecss.tm import PusTelemetry
from tmtccmd.utility.logger import get_console_logger
from tmtccmd.pus.service_1_verification import Service1TMExtended
from tmtccmd.pus.service_17_test import Service17TMExtended
from tmtccmd.tm.service_3_housekeeping import Service3TM
from tmtccmd.tm.service_5_event import Service5TM
from tmtccmd.utility.tmtc_printer import TmTcPrinter, PrintFormats
from config.definitions import PUS_APID
LOGGER = get_console_logger()
def ccsds_tm_handler(
apid: int, raw_tm_packet: bytearray, tmtc_printer: TmTcPrinter
) -> None:
if apid == PUS_APID:
pus_factory_hook(raw_tm_packet=raw_tm_packet, tmtc_printer=tmtc_printer)
def pus_factory_hook(raw_tm_packet: bytearray, tmtc_printer: TmTcPrinter):
if len(raw_tm_packet) < 8:
LOGGER.warning("Detected packet shorter than 8 bytes!")
return
service_type = raw_tm_packet[7]
tm_packet = None
try:
if service_type == 1:
tm_packet = Service1TMExtended.unpack(raw_telemetry=raw_tm_packet)
if service_type == 3:
tm_packet = Service3TM.unpack(
raw_telemetry=raw_tm_packet, custom_hk_handling=False
)
if service_type == 5:
tm_packet = Service5TM.unpack(raw_telemetry=raw_tm_packet)
if service_type == 8:
tm_packet = Service8TM.unpack(raw_telemetry=raw_tm_packet)
if service_type == 17:
tm_packet = Service17TMExtended.unpack(raw_telemetry=raw_tm_packet)
if tm_packet is None:
LOGGER.info(
f"The service {service_type} is not implemented in Telemetry Factory"
)
tm_packet = PusTelemetry.unpack(raw_telemetry=raw_tm_packet)
tm_packet.print_source_data(PrintFormats.HEX)
tmtc_printer.print_telemetry(
packet_if=tm_packet, info_if=tm_packet, print_raw_tm=False
)
except ValueError:
# TODO: Log faulty packet
LOGGER.warning("Invalid packet format detected")