diff --git a/eive_tmtc/pus_tm/hk_handler.py b/eive_tmtc/pus_tm/hk_handler.py index e474ccd..2a479a0 100644 --- a/eive_tmtc/pus_tm/hk_handler.py +++ b/eive_tmtc/pus_tm/hk_handler.py @@ -2,7 +2,8 @@ import dataclasses import logging import sqlite3 -from typing import List, Optional, cast +from typing import List, cast +from uuid import UUID from eive_tmtc.tmtc.acs.acs_ctrl import handle_acs_ctrl_hk_data from eive_tmtc.tmtc.internal_err_reporter import handle_ier_hk_data @@ -17,7 +18,6 @@ from eive_tmtc.tmtc.acs.reaction_wheels import handle_rw_hk_data from eive_tmtc.tmtc.com.syrlinks_handler import handle_syrlinks_hk_data from eive_tmtc.tmtc.tcs import handle_thermal_controller_hk_data from eive_tmtc.tmtc.tcs.tmp1075 import handle_tmp_1075_hk_data -from spacepackets.ecss import PusTelemetry from tmtccmd.pus.tm.s3_fsfw_hk import ( HkContentType, Service3FsfwTm, @@ -56,11 +56,12 @@ class HkFilter: def handle_hk_packet( raw_tm: bytes, + packet_uuid: UUID, obj_id_dict: ObjectIdDictT, printer: FsfwTmTcPrinter, hk_filter: HkFilter, hk_level: int, - db: Optional[sqlite3.Connection], + db_con: sqlite3.Connection, ): tm_packet = Service3FsfwTm.unpack(raw_telemetry=raw_tm, custom_hk_handling=False) named_obj_id = cast(ObjectIdU32, obj_id_dict.get(tm_packet.object_id.as_bytes)) @@ -74,9 +75,9 @@ def handle_hk_packet( printer=printer, object_id=named_obj_id, hk_packet=tm_packet, - tm=tm_packet.pus_tm, + tm_packet=tm_packet.pus_tm, hk_data=hk_data, - db=db, + db=db_con, ) return try: @@ -87,11 +88,11 @@ def handle_hk_packet( if hk_level >= 1: handle_regular_hk_print( printer=printer, + packet_uuid=packet_uuid, object_id=named_obj_id, hk_packet=tm_packet, - tm=tm_packet.pus_tm, hk_data=hk_data, - db=db, + db=db_con, ) except ValueError as e: _LOGGER.exception( @@ -102,17 +103,17 @@ def handle_hk_packet( def handle_regular_hk_print( # noqa C901: Complexity okay here - printer: FsfwTmTcPrinter, - object_id: ObjectIdU32, hk_packet: Service3FsfwTm, - tm: PusTelemetry, + packet_uuid: UUID, hk_data: bytes, - db: Optional[sqlite3.Connection], + db: sqlite3.Connection, + object_id: ObjectIdU32, + printer: FsfwTmTcPrinter, ): objb = object_id.as_bytes set_id = hk_packet.set_id - assert tm.time_provider is not None - packet_dt = tm.time_provider.as_date_time() + assert hk_packet.pus_tm.time_provider is not None + packet_dt = hk_packet.pus_tm.time_provider.as_date_time() pw = PrintWrapper(printer.file_logger) """This function is called when a Service 3 Housekeeping packet is received.""" if objb in [obj_ids.RW1_ID, obj_ids.RW2_ID, obj_ids.RW3_ID, obj_ids.RW4_ID]: @@ -133,6 +134,7 @@ def handle_regular_hk_print( # noqa C901: Complexity okay here return handle_core_hk_data(pw=pw, hk_data=hk_data, set_id=set_id) elif objb == obj_ids.PDU_1_HANDLER_ID: return handle_pdu_data( + packet_uuid=packet_uuid, hk_packet=hk_packet, con=db, pw=pw, @@ -142,6 +144,7 @@ def handle_regular_hk_print( # noqa C901: Complexity okay here ) elif objb == obj_ids.PDU_2_HANDLER_ID: return handle_pdu_data( + packet_uuid=packet_uuid, hk_packet=hk_packet, con=db, pw=pw, diff --git a/eive_tmtc/pus_tm/pus_demux.py b/eive_tmtc/pus_tm/pus_demux.py deleted file mode 100644 index bc5b274..0000000 --- a/eive_tmtc/pus_tm/pus_demux.py +++ /dev/null @@ -1,130 +0,0 @@ -"""Core EIVE TM handler module -""" -import logging -import sqlite3 -from typing import Optional - -from spacepackets.ccsds.time import CdsShortTimestamp -from spacepackets.ecss import PusTelemetry -from spacepackets.ecss.pus_17_test import Service17Tm -from spacepackets.util import PrintFormats -from tmtccmd.fsfw.tmtc_printer import FsfwTmTcPrinter -from tmtccmd.logging.pus import RawTmtcTimedLogWrapper -from tmtccmd.pus import VerificationWrapper -from tmtccmd.pus.s20_fsfw_param import Service20FsfwTm, Service20ParamDumpWrapper -from tmtccmd.pus.s20_fsfw_param_defs import CustomSubservice as ParamSubservice -from tmtccmd.pus.s200_fsfw_mode import Service200FsfwTm -from tmtccmd.pus.s200_fsfw_mode import Subservice as ModeSubservice - -from eive_tmtc.config.object_ids import get_object_ids - -from .action_reply_handler import handle_action_reply -from .defs import PrintWrapper -from .event_handler import handle_event_packet -from .hk_handler import HkFilter, handle_hk_packet -from .verification_handler import generic_retval_printout, handle_service_1_fsfw_packet - -_LOGGER = logging.getLogger(__name__) - - -def pus_factory_hook( # noqa C901 : Complexity okay here - packet: bytes, - verif_wrapper: VerificationWrapper, - printer: FsfwTmTcPrinter, - raw_logger: RawTmtcTimedLogWrapper, - hk_level: int, - hk_filter: HkFilter, - db: Optional[sqlite3.Connection], -): - if len(packet) < 8: - _LOGGER.warning("Detected packet shorter than 8 bytes!") - return - try: - tm_packet = PusTelemetry.unpack(packet, CdsShortTimestamp.empty()) - # _LOGGER.info(f"Sequence count: {tm_packet.seq_count}") - except ValueError as value_error: - _LOGGER.warning(f"{value_error}") - _LOGGER.warning("Could not generate PUS TM object from raw data") - _LOGGER.warning(f"Raw Packet: [{packet.hex(sep=',')}], REPR: {packet!r}") - return - service = tm_packet.service - obj_id_dict = get_object_ids() - pw = PrintWrapper(printer.file_logger) - dedicated_handler = True - if service == 1: - handle_service_1_fsfw_packet(wrapper=verif_wrapper, raw_tm=packet) - elif service == 3: - handle_hk_packet( - db=db, - printer=printer, - raw_tm=packet, - obj_id_dict=obj_id_dict, - hk_level=hk_level, - hk_filter=hk_filter, - ) - elif service == 5: - handle_event_packet(raw_tm=packet, pw=pw) - elif service == 8: - handle_action_reply(raw_tm=packet, printer=printer, obj_id_dict=obj_id_dict) - elif service == 17: - tm_packet = Service17Tm.unpack( - data=packet, time_reader=CdsShortTimestamp.empty() - ) - if tm_packet.subservice == 2: - verif_wrapper.dlog("Received Ping Reply TM[17,2]") - dedicated_handler = True - elif service == 20: - param_packet = Service20FsfwTm.unpack( - raw_telemetry=packet, time_reader=CdsShortTimestamp.empty() - ) - if tm_packet.subservice == ParamSubservice.TM_DUMP_REPLY: - param_wrapper = Service20ParamDumpWrapper(param_tm=param_packet) - try: - param = param_wrapper.get_param() - obj = obj_id_dict.get(param_wrapper.param_tm.object_id) - pw.dlog(f"Received parameter dump TM from {obj}") - pw.dlog(f"Parameter: {param}") - if param.rows == 1 and param.columns == 1: - try: - scalar_param = param.parse_scalar_param() - if isinstance(scalar_param, int): - pw.dlog(f"Scalar integer parameter: {scalar_param}") - elif isinstance(scalar_param, float): - pw.dlog(f"Scalar floating point parameter: {scalar_param}") - except ValueError as e: - pw.dlog(f"received {e} trying to parse scalar parameter") - else: - # TODO: Could improve display further by actually displaying a matrix as a - # matrix using row and column information - pw.dlog( - "Received vector or matrix data:" - f" {param.param_raw.hex(sep=',')}" - ) - except ValueError as e: - pw.dlog(f"received {e} when trying to parse parameters") - except NotImplementedError as e: - pw.dlog(f"received {e} when trying to parse parameters") - else: - pw.dlog(f"unknown subservice {tm_packet.subservice} for parameter service") - elif service == 200: - tm_packet = Service200FsfwTm.unpack( - raw_telemetry=packet, time_reader=CdsShortTimestamp.empty() - ) - if tm_packet.subservice == ModeSubservice.TM_CANT_REACH_MODE: - obj_id = tm_packet.object_id - obj_id_obj = obj_id_dict.get(obj_id) - retval = tm_packet.return_value - string_list = generic_retval_printout(retval) - pw.dlog(f"Received Mode Reply from {obj_id_obj}: Can't reach mode.") - for string in string_list: - pw.dlog(f"Reason: {string}") - dedicated_handler = True - else: - dedicated_handler = False - else: - _LOGGER.info(f"The service {service} is not implemented in Telemetry Factory") - tm_packet.print_source_data(PrintFormats.HEX) - dedicated_handler = True - if not dedicated_handler and tm_packet is not None: - printer.handle_long_tm_print(packet_if=tm_packet, info_if=tm_packet) - raw_logger.log_tm(tm_packet) diff --git a/eive_tmtc/pus_tm/pus_handler.py b/eive_tmtc/pus_tm/pus_handler.py new file mode 100644 index 0000000..d02dda0 --- /dev/null +++ b/eive_tmtc/pus_tm/pus_handler.py @@ -0,0 +1,171 @@ +"""Core EIVE TM handler module +""" +import logging +import sqlite3 +import uuid +from typing import Any + +from spacepackets.ccsds.time import CdsShortTimestamp +from spacepackets.ecss import PusTelemetry +from spacepackets.ecss.pus_17_test import Service17Tm +from spacepackets.util import PrintFormats +from tmtccmd.fsfw.tmtc_printer import FsfwTmTcPrinter +from tmtccmd.logging.pus import RawTmtcTimedLogWrapper +from tmtccmd.pus import VerificationWrapper +from tmtccmd.pus.s20_fsfw_param import Service20FsfwTm, Service20ParamDumpWrapper +from tmtccmd.pus.s20_fsfw_param_defs import CustomSubservice as ParamSubservice +from tmtccmd.pus.s200_fsfw_mode import Service200FsfwTm +from tmtccmd.pus.s200_fsfw_mode import Subservice as ModeSubservice +from tmtccmd.tmtc import GenericApidHandlerBase, SpecificApidHandlerBase +from eive_tmtc.config.definitions import DB_PATH, PUS_APID + +from eive_tmtc.config.object_ids import get_object_ids + +from .action_reply_handler import handle_action_reply +from .defs import PrintWrapper +from .event_handler import handle_event_packet +from .hk_handler import HkFilter, handle_hk_packet +from .verification_handler import generic_retval_printout, handle_service_1_fsfw_packet + +_LOGGER = logging.getLogger(__name__) + + +class PusHandler(SpecificApidHandlerBase): + def __init__( + self, + wrapper: VerificationWrapper, + printer: FsfwTmTcPrinter, + raw_logger: RawTmtcTimedLogWrapper, + hk_level: int, + ): + super().__init__(PUS_APID, None) + self.printer = printer + self.verif_wrapper = wrapper + self.raw_logger = raw_logger + self.hk_level = hk_level + self.these_objs_hk_only = [] + self.hk_filter = HkFilter(object_ids=self.these_objs_hk_only, set_ids=[]) + self.obj_id_dict = get_object_ids() + self.pw = PrintWrapper(printer.file_logger) + + def handle_tm(self, packet: bytes, _user_args: Any): + self.pus_handler( + packet, + ) + + def pus_handler( # noqa C901 : Complexity okay here + self, + packet: bytes, + ): + packet_uuid = uuid.uuid4() + if len(packet) < 8: + _LOGGER.warning("Detected packet shorter than 8 bytes!") + return + try: + tm_packet = PusTelemetry.unpack(packet, CdsShortTimestamp.empty()) + # _LOGGER.info(f"Sequence count: {tm_packet.seq_count}") + except ValueError as value_error: + _LOGGER.warning(f"{value_error}") + _LOGGER.warning("Could not generate PUS TM object from raw data") + _LOGGER.warning(f"Raw Packet: [{packet.hex(sep=',')}], REPR: {packet!r}") + return + service = tm_packet.service + dedicated_handler = True + if service == 1: + handle_service_1_fsfw_packet(wrapper=self.verif_wrapper, raw_tm=packet) + elif service == 3: + handle_hk_packet( + db_con=sqlite3.connect(DB_PATH), + packet_uuid=packet_uuid, + printer=self.printer, + raw_tm=packet, + obj_id_dict=self.obj_id_dict, + hk_level=self.hk_level, + hk_filter=self.hk_filter, + ) + elif service == 5: + handle_event_packet(raw_tm=packet, pw=self.pw) + elif service == 8: + handle_action_reply( + raw_tm=packet, printer=self.printer, obj_id_dict=self.obj_id_dict + ) + elif service == 17: + pus17_tm = Service17Tm.unpack( + data=packet, time_reader=CdsShortTimestamp.empty() + ) + if pus17_tm.subservice == 2: + self.verif_wrapper.dlog("Received Ping Reply TM[17,2]") + dedicated_handler = True + elif service == 20: + self._handle_param_packet(packet, tm_packet) + elif service == 200: + dedicated_handler = self._handle_mode_packet(packet, tm_packet) + else: + _LOGGER.info( + f"The service {service} is not implemented in Telemetry Factory" + ) + tm_packet.print_source_data(PrintFormats.HEX) + dedicated_handler = True + if not dedicated_handler and tm_packet is not None: + self.printer.handle_long_tm_print(packet_if=tm_packet, info_if=tm_packet) + self.raw_logger.log_tm(tm_packet) + + def _handle_param_packet(self, raw_data: bytes, tm_packet: PusTelemetry): + param_packet = Service20FsfwTm.unpack( + raw_telemetry=raw_data, time_reader=CdsShortTimestamp.empty() + ) + if tm_packet.subservice == ParamSubservice.TM_DUMP_REPLY: + param_wrapper = Service20ParamDumpWrapper(param_tm=param_packet) + try: + param = param_wrapper.get_param() + obj = self.obj_id_dict.get(param_wrapper.param_tm.object_id) + self.pw.dlog(f"Received parameter dump TM from {obj}") + self.pw.dlog(f"Parameter: {param}") + if param.rows == 1 and param.columns == 1: + try: + scalar_param = param.parse_scalar_param() + if isinstance(scalar_param, int): + self.pw.dlog(f"Scalar integer parameter: {scalar_param}") + elif isinstance(scalar_param, float): + self.pw.dlog( + f"Scalar floating point parameter: {scalar_param}" + ) + except ValueError as e: + self.pw.dlog(f"received {e} trying to parse scalar parameter") + else: + # TODO: Could improve display further by actually displaying a matrix as a + # matrix using row and column information + self.pw.dlog( + "Received vector or matrix data:" + f" {param.param_raw.hex(sep=',')}" + ) + except ValueError as e: + self.pw.dlog(f"received {e} when trying to parse parameters") + except NotImplementedError as e: + self.pw.dlog(f"received {e} when trying to parse parameters") + else: + self.pw.dlog( + f"unknown subservice {tm_packet.subservice} for parameter service" + ) + + def _handle_mode_packet(self, raw_data: bytes, _: PusTelemetry) -> bool: + tm_packet = Service200FsfwTm.unpack( + raw_telemetry=raw_data, time_reader=CdsShortTimestamp.empty() + ) + if tm_packet.subservice == ModeSubservice.TM_CANT_REACH_MODE: + obj_id = tm_packet.object_id + obj_id_obj = self.obj_id_dict.get(obj_id) + retval = tm_packet.return_value + string_list = generic_retval_printout(retval) + self.pw.dlog(f"Received Mode Reply from {obj_id_obj}: Can't reach mode.") + for string in string_list: + self.pw.dlog(f"Reason: {string}") + return True + return False + + +class UnknownApidHandler(GenericApidHandlerBase): + def handle_tm(self, apid: int, packet: bytes, _user_args: Any): + _LOGGER.warning( + f"Packet with unknown APID {apid} detected: {packet.hex(sep=',')}" + ) diff --git a/eive_tmtc/tmtc/power/tm.py b/eive_tmtc/tmtc/power/tm.py index 2fd6fce..650bea1 100644 --- a/eive_tmtc/tmtc/power/tm.py +++ b/eive_tmtc/tmtc/power/tm.py @@ -1,6 +1,7 @@ import struct import sqlite3 from typing import List, Optional, Tuple +from uuid import UUID from eive_tmtc.tmtc.power.acu import acu_config_table_handler from eive_tmtc.tmtc.power.common_power import ( @@ -149,12 +150,13 @@ class DevicesInfoParser: def handle_pdu_data( + hk_data: bytes, hk_packet: Service3FsfwTm, + packet_uuid: UUID, con: Optional[sqlite3.Connection], pw: PrintWrapper, pdu_idx: int, set_id: int, - hk_data: bytes, ): current_idx = 0 priv_idx = pdu_idx - 1 @@ -229,27 +231,54 @@ def handle_pdu_data( f"Temperature {temperature} | VCC {vcc} | VBAT {vbat}" ) if con is not None: + packet_dt = hk_packet.pus_tm.time_provider.as_datetime() # type: ignore cursor = con.cursor() if pdu_idx == 1: - tbl_name = "Pdu1" + tbl_base_name = "Pdu1_" channel_list = PDU1_CHANNELS_NAMES else: - tbl_name = "Pdu2" + tbl_base_name = "Pdu2_" channel_list = PDU2_CHANNELS_NAMES + cursor.execute( + f""" + CREATE TABLE IF NOT EXISTS {tbl_base_name}BootCount( + packet_uuid TEXT PRIMARY KEY, + generation_time TEXT, + Count NUM + )""" + ) + cursor.execute( + f"INSERT INTO {tbl_base_name}BootCount VALUES(?, ?, ?)", + (str(packet_uuid), packet_dt, boot_count), + ) + for idx, name in enumerate(channel_list): + words = name.split() + camel_case_name = "".join(word.capitalize() for word in words) + tbl_name = f"{tbl_base_name}{camel_case_name}" + print(f"creating table {tbl_name}") cursor.execute( - f"CREATE TABLE {tbl_name}{name} IF NOT EXISTS " - f"(GenerationTime, OutEnable, Voltage, Current)" + f""" + CREATE TABLE IF NOT EXISTS {tbl_name}( + packet_uuid TEXT PRIMARY KEY, + generation_time TEXT, + out_enable NUM, + voltage NUM, + current NUM + )""" ) - value_list = [ - hk_packet.pus_tm.time_provider.as_datetime(), # type: ignore + value_tuple = ( + str(packet_uuid), + packet_dt, output_enb_list[idx], voltage_list[idx], current_list[idx], - ] - cursor.execute( - f"INSERT INTO {tbl_name}{name} VALUES(?, ?, ?, ?)", value_list ) + print(value_tuple) + cursor.execute( + f"INSERT INTO {tbl_name} VALUES(?, ?, ?, ?, ?)", value_tuple + ) + con.commit() pw.dlog(info) diff --git a/tmtcc.py b/tmtcc.py index e6e4188..a4fc4b0 100755 --- a/tmtcc.py +++ b/tmtcc.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 -import sqlite3 import logging import sys import time @@ -16,7 +15,6 @@ from spacepackets.ecss import PusVerificator from spacepackets.version import get_version as get_sp_version from tmtccmd import BackendBase from tmtccmd.cfdp.handler import RemoteEntityCfgTable -from eive_tmtc.cfdp.handler import CfdpInCcsdsHandler from tmtccmd.cfdp.mib import ( IndicationCfg, LocalEntityCfg, @@ -41,11 +39,13 @@ from tmtccmd.logging.pus import ( TimedLogWhen, ) from tmtccmd.pus import VerificationWrapper -from tmtccmd.tmtc import CcsdsTmHandler, GenericApidHandlerBase, SpecificApidHandlerBase +from tmtccmd.tmtc import CcsdsTmHandler from tmtccmd.util import FileSeqCountProvider, PusFileSeqCountProvider from eive_tmtc import APP_LOGGER +from eive_tmtc.pus_tm.pus_handler import PusHandler, UnknownApidHandler from eive_tmtc.cfdp.fault_handler import EiveCfdpFaultHandler +from eive_tmtc.cfdp.handler import CfdpInCcsdsHandler from eive_tmtc.cfdp.tm import CfdpInCcsdsWrapper from eive_tmtc.cfdp.user import EiveCfdpUser, EiveCheckTimerProvider from eive_tmtc.config.definitions import ( @@ -53,12 +53,9 @@ from eive_tmtc.config.definitions import ( CFDP_LOCAL_ENTITY_ID, CFDP_REMOTE_ENTITY_ID, PUS_APID, - DB_PATH, ) from eive_tmtc.config.hook import EiveHookObject from eive_tmtc.pus_tc.tc_handler import TcHandler -from eive_tmtc.pus_tm.hk_handler import HkFilter -from eive_tmtc.pus_tm.pus_demux import pus_factory_hook _LOGGER = APP_LOGGER _LOG_LEVEL = logging.INFO @@ -68,43 +65,6 @@ ROTATING_TIMED_LOGGER_INTERVAL_WHEN = TimedLogWhen.PER_MINUTE ROTATING_TIMED_LOGGER_INTERVAL = 30 -class PusHandler(SpecificApidHandlerBase): - def __init__( - self, - wrapper: VerificationWrapper, - printer: FsfwTmTcPrinter, - raw_logger: RawTmtcTimedLogWrapper, - hk_level: int, - ): - super().__init__(PUS_APID, None) - self.printer = printer - self.verif_wrapper = wrapper - self.raw_logger = raw_logger - self.hk_level = hk_level - self.these_objs_hk_only = [] - self.hk_filter = HkFilter(object_ids=self.these_objs_hk_only, set_ids=[]) - self.tm_db = sqlite3.connect(DB_PATH) - - def handle_tm(self, packet: bytes, _user_args: any): - # with open("tc.bin", "wb") as of: - # of.write(packet) - pus_factory_hook( - packet, - self.verif_wrapper, - self.printer, - self.raw_logger, - self.hk_level, - self.hk_filter, - ) - - -class UnknownApidHandler(GenericApidHandlerBase): - def handle_tm(self, apid: int, packet: bytes, _user_args: any): - _LOGGER.warning( - f"Packet with unknown APID {apid} detected: {packet.hex(sep=',')}" - ) - - class CustomCcsdsTmHandler(CcsdsTmHandler): def user_hook(self, apid: int, packet: bytes): _LOGGER.debug(f"Received packet {packet.hex(sep=',')} with APID {apid}")