eive-tmtc/pus_tm/event_handler.py

62 lines
2.1 KiB
Python
Raw Normal View History

2022-04-05 17:05:11 +02:00
import logging
2022-03-04 11:02:10 +01:00
import os.path
2022-04-05 17:05:11 +02:00
from datetime import datetime
2022-03-04 14:27:19 +01:00
from config.object_ids import get_object_ids
2022-04-05 17:05:11 +02:00
from tmtccmd.tm import Service5Tm
2022-04-05 00:51:52 +02:00
from tmtccmd.logging import get_console_logger
2022-04-05 19:27:55 +02:00
from tmtccmd.utility.tmtc_printer import FsfwTmTcPrinter
from tmtccmd.fsfw import parse_fsfw_events_csv, EventDictT, EventInfo
2022-03-04 11:56:42 +01:00
LOGGER = get_console_logger()
2022-03-04 11:02:10 +01:00
DEFAULT_EVENTS_CSV_PATH = "config/events.csv"
2022-03-04 14:27:19 +01:00
__EVENT_DICT = None
2022-03-04 11:56:42 +01:00
2022-03-04 14:27:19 +01:00
def get_event_dict() -> EventDictT:
global __EVENT_DICT
if __EVENT_DICT is None:
if os.path.exists(DEFAULT_EVENTS_CSV_PATH):
__EVENT_DICT = parse_fsfw_events_csv(DEFAULT_EVENTS_CSV_PATH)
else:
LOGGER.warning(f"No Event CSV file found at {DEFAULT_EVENTS_CSV_PATH}")
__EVENT_DICT = dict()
return __EVENT_DICT
2022-03-04 11:02:10 +01:00
2022-04-05 19:27:55 +02:00
def handle_event_packet(
2022-04-05 19:49:42 +02:00
raw_tm: bytes, printer: FsfwTmTcPrinter, file_logger: logging.Logger
2022-04-05 19:27:55 +02:00
) -> str:
tm = Service5Tm.unpack(raw_telemetry=raw_tm)
printer.handle_long_tm_print(packet_if=tm, info_if=tm)
2022-03-04 11:56:42 +01:00
additional_event_info = ""
2022-03-04 14:27:19 +01:00
event_dict = get_event_dict()
2022-04-05 17:05:11 +02:00
info = event_dict.get(tm.event_id)
2022-03-07 11:26:44 +01:00
if info is None:
2022-04-05 17:05:11 +02:00
LOGGER.warning(f"Event ID {tm.event_id} has no information")
2022-03-07 11:26:44 +01:00
info = EventInfo()
info.name = "Unknown event"
2022-03-04 14:27:19 +01:00
obj_ids = get_object_ids()
2022-04-05 17:05:11 +02:00
obj_id_obj = obj_ids.get(tm.reporter_id.as_bytes)
2022-03-04 14:27:19 +01:00
if obj_id_obj is None:
2022-04-05 17:05:11 +02:00
LOGGER.warning(f"Object ID 0x{tm.reporter_id.as_string} has no name")
obj_name = tm.reporter_id.as_string
2022-03-04 14:27:19 +01:00
else:
obj_name = obj_id_obj.name
2022-04-05 17:05:11 +02:00
generic_event_string = (
f"Object {obj_name} generated Event {tm.event_id} | {info.name}"
)
2022-03-04 14:27:19 +01:00
if info.info != "":
additional_event_info = (
2022-04-05 17:05:11 +02:00
f"Additional info: {info.info} | P1: {tm.param_1} | P2: {tm.param_2}"
2022-03-04 14:27:19 +01:00
)
2022-04-05 17:05:11 +02:00
file_logger.info(
f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}: {generic_event_string}"
)
LOGGER.info(generic_event_string)
if additional_event_info != "":
file_logger.info(additional_event_info)
print(additional_event_info)
return generic_event_string + " | " + additional_event_info