eive-obsw/generators/events/event_parser.py

179 lines
6.0 KiB
Python
Raw Normal View History

2021-08-02 11:43:49 +02:00
"""Part of the Mission Operation Database Exporter for the EVIE project.
2021-05-17 19:39:35 +02:00
Event exporter.
"""
import datetime
2023-02-09 15:14:21 +01:00
import logging
2021-08-02 11:43:49 +02:00
import time
2022-03-01 18:03:50 +01:00
import os
2022-06-20 18:05:05 +02:00
from pathlib import Path
2022-03-01 18:03:50 +01:00
from fsfwgen.events.event_parser import (
handle_csv_export,
handle_cpp_export,
SubsystemDefinitionParser,
EventParser,
2022-06-21 00:58:59 +02:00
EventDictT,
2022-03-01 18:03:50 +01:00
)
2021-06-08 17:10:47 +02:00
from fsfwgen.parserbase.file_list_parser import FileListParser
from fsfwgen.utility.printer import PrettyPrinter
2022-03-01 18:03:50 +01:00
from fsfwgen.utility.file_management import copy_file
2021-08-02 11:43:49 +02:00
from definitions import BspType, ROOT_DIR, OBSW_ROOT_DIR
2021-05-17 19:39:35 +02:00
2023-02-09 15:14:21 +01:00
_LOGGER = logging.getLogger(__name__)
2021-05-17 19:39:35 +02:00
DATE_TODAY = datetime.datetime.now()
DATE_STRING_FULL = DATE_TODAY.strftime("%Y-%m-%d %H:%M:%S")
2023-02-09 15:51:36 +01:00
PRINT_EVENTS = False
PRINT_SUBSYSTEM_TABLE = False
2023-03-14 17:12:05 +01:00
EXPORT_SUBSYSTEM_TABLE = True
2023-02-09 15:51:36 +01:00
2021-05-17 19:39:35 +02:00
GENERATE_CPP = True
GENERATE_CPP_H = True
GENERATE_CSV = True
COPY_CPP_FILE = True
COPY_CPP_H_FILE = True
MOVE_CSV_FILE = True
PARSE_HOST_BSP = True
2022-03-01 18:03:50 +01:00
# Store these files relative to the events folder
2022-06-21 00:58:59 +02:00
CPP_FILENAME = Path(
f"{os.path.dirname(os.path.realpath(__file__))}/translateEvents.cpp"
)
CPP_H_FILENAME = Path(
f"{os.path.dirname(os.path.realpath(__file__))}/translateEvents.h"
)
2021-05-17 19:39:35 +02:00
FILE_SEPARATOR = ";"
2021-05-17 19:39:35 +02:00
class BspConfig:
def __init__(self, bsp_select: BspType):
self.bsp_select = bsp_select
self.bsp_dir_name = self.bsp_select.value
2021-05-18 16:16:02 +02:00
# Store this file in the root of the generators folder
self.csv_filename = Path(f"{ROOT_DIR}/{self.bsp_dir_name}_events.csv")
2023-07-21 11:36:39 +02:00
self.subsystems_csv_filename = Path(
f"{ROOT_DIR}/{self.bsp_dir_name}_subsystems.csv"
)
self.csv_copy_dest = Path(f"{OBSW_ROOT_DIR}/tmtc/eive_tmtc/config/events.csv")
2023-07-21 11:36:39 +02:00
self.subsystem_csv_copy_dest = Path(
f"{OBSW_ROOT_DIR}/tmtc/eive_tmtc/config/subsystems.csv"
)
2021-05-17 19:39:35 +02:00
if (
self.bsp_select == BspType.BSP_Q7S
or self.bsp_select == BspType.BSP_LINUX_BOARD
):
self.fsfw_config_root = Path(f"{OBSW_ROOT_DIR}/linux/fsfwconfig")
2021-05-17 19:39:35 +02:00
else:
self.fsfw_config_root = Path(
f"{OBSW_ROOT_DIR}/{self.bsp_dir_name}/fsfwconfig"
)
self.cpp_copy_dest = Path(f"{self.fsfw_config_root}/events/")
self.subystem_defs_destinations = [
f"{self.fsfw_config_root}/events/subsystemIdRanges.h",
f"{OBSW_ROOT_DIR}/fsfw/src/fsfw/events/fwSubsystemIdRanges.h",
f"{OBSW_ROOT_DIR}/common/config/eive/eventSubsystemIds.h",
]
self.header_defs_destinations = [
f"{OBSW_ROOT_DIR}/mission/",
f"{OBSW_ROOT_DIR}/fsfw/",
f"{self.fsfw_config_root}",
f"{OBSW_ROOT_DIR}/test/",
f"{OBSW_ROOT_DIR}/bsp_q7s/",
f"{OBSW_ROOT_DIR}/linux/",
]
def subsystem_defs_as_paths(self):
return [Path(x) for x in self.subystem_defs_destinations]
def header_defs_as_paths(self):
return [Path(x) for x in self.header_defs_destinations]
2021-05-17 19:39:35 +02:00
2021-08-02 11:43:49 +02:00
def parse_events(
2023-02-09 15:58:41 +01:00
bsp_type: BspType,
generate_csv: bool,
generate_cpp: bool,
copy_csv_to_eive_tmtc: bool,
2021-08-02 11:43:49 +02:00
):
bsp_cfg = BspConfig(bsp_type)
2023-02-09 15:51:36 +01:00
_LOGGER.info(f"EventParser: Parsing events for {bsp_type.name}")
2021-08-02 11:43:49 +02:00
# Small delay for clean printout
time.sleep(0.01)
2023-03-14 17:12:05 +01:00
event_list = generate_event_list(bsp_cfg, copy_csv_to_eive_tmtc)
if PRINT_EVENTS:
2021-08-02 11:43:49 +02:00
PrettyPrinter.pprint(event_list)
# Delay for clean printout
time.sleep(0.1)
if generate_csv:
handle_csv_export(
file_name=bsp_cfg.csv_filename,
event_list=event_list,
file_separator=FILE_SEPARATOR,
2021-08-02 11:43:49 +02:00
)
2023-02-09 15:51:36 +01:00
if copy_csv_to_eive_tmtc:
_LOGGER.info(f"Copying CSV file to {bsp_cfg.cpp_copy_dest}")
copy_file(
filename=bsp_cfg.csv_filename,
destination=bsp_cfg.csv_copy_dest,
delete_existing_file=True,
)
2021-08-02 11:43:49 +02:00
if generate_cpp:
2021-05-17 19:39:35 +02:00
handle_cpp_export(
2022-03-01 18:03:50 +01:00
event_list=event_list,
date_string=DATE_STRING_FULL,
file_name=CPP_FILENAME,
generate_header=GENERATE_CPP_H,
header_file_name=CPP_H_FILENAME,
2021-05-17 19:39:35 +02:00
)
if COPY_CPP_FILE:
2023-02-09 15:14:21 +01:00
_LOGGER.info(
f"EventParser: Copying CPP translation file to {bsp_cfg.cpp_copy_dest}"
2022-06-20 18:05:05 +02:00
)
copy_file(CPP_FILENAME, bsp_cfg.cpp_copy_dest)
copy_file(CPP_H_FILENAME, bsp_cfg.cpp_copy_dest)
2023-02-09 15:51:36 +01:00
_LOGGER.info(f"Parsing done for {bsp_type.name}")
2021-05-17 19:39:35 +02:00
2023-03-14 17:12:05 +01:00
def generate_event_list(cfg: BspConfig, copy_csv_to_eive_tmtc: bool) -> EventDictT:
subsystem_parser = SubsystemDefinitionParser(cfg.subsystem_defs_as_paths())
2021-05-17 19:39:35 +02:00
subsystem_table = subsystem_parser.parse_files()
2023-02-09 15:14:21 +01:00
_LOGGER.info(f"Found {len(subsystem_table)} subsystem definitions.")
2023-02-09 15:51:36 +01:00
if PRINT_SUBSYSTEM_TABLE:
PrettyPrinter.pprint(subsystem_table)
2023-03-14 17:12:05 +01:00
if EXPORT_SUBSYSTEM_TABLE:
PrettyPrinter.pprint(subsystem_table)
with open(cfg.subsystems_csv_filename, "w") as file:
for name_str, table_entry in subsystem_table.items():
subsystem_id = int(table_entry[0])
file.write(f"{subsystem_id}{FILE_SEPARATOR}{name_str}\n")
if copy_csv_to_eive_tmtc:
_LOGGER.info(f"Copying CSV file to {cfg.cpp_copy_dest}")
copy_file(
filename=cfg.subsystems_csv_filename,
destination=cfg.subsystem_csv_copy_dest,
delete_existing_file=True,
)
event_header_parser = FileListParser(cfg.header_defs_as_paths())
2021-05-17 19:39:35 +02:00
event_headers = event_header_parser.parse_header_files(
True, "Parsing event header file list:\n", True
)
# PrettyPrinter.pprint(event_headers)
# myEventList = parseHeaderFiles(subsystem_table, event_headers)
event_parser = EventParser(event_headers, subsystem_table)
2022-03-01 18:03:50 +01:00
event_parser.obsw_root_path = OBSW_ROOT_DIR
2021-05-17 19:39:35 +02:00
event_parser.set_moving_window_mode(moving_window_size=7)
event_table = event_parser.parse_files()
2022-06-21 00:58:59 +02:00
events_sorted = dict(sorted(event_table.items()))
2023-02-09 15:14:21 +01:00
_LOGGER.info(f"Found {len(events_sorted)} entries")
2022-06-21 00:58:59 +02:00
return events_sorted