From 5f813787548238c3b7d434aff51595c7191e5c04 Mon Sep 17 00:00:00 2001 From: flap1 Date: Wed, 22 May 2024 05:01:11 +0900 Subject: [PATCH] feat: Add functionality to modify event_handler_rules based on eh.csv --- c2a_generator/__init__.py | 2 ++ c2a_generator/eh_rules_c.py | 54 +++++++++++++++++++++++++++++++++++ c2a_generator/eh_rules_h.py | 56 +++++++++++++++++++++++++++++++++++++ c2a_generator/wings_json.py | 18 +++++++++++- test.py | 41 +++++++++++++++++++++++++++ 5 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 c2a_generator/eh_rules_c.py create mode 100644 c2a_generator/eh_rules_h.py diff --git a/c2a_generator/__init__.py b/c2a_generator/__init__.py index 1776e96..81fa5fc 100644 --- a/c2a_generator/__init__.py +++ b/c2a_generator/__init__.py @@ -4,6 +4,8 @@ from .cmd_csv import generate # noqa from .cmd_def_c import generate # noqa from .cmd_def_h import generate # noqa +from .eh_rules_c import generate # noqa +from .eh_rules_h import generate # noqa from .subobc_cmd_def_h import generate # noqa from .subobc_tlm_buf_c import generate # noqa from .subobc_tlm_buf_h import generate # noqa diff --git a/c2a_generator/eh_rules_c.py b/c2a_generator/eh_rules_c.py new file mode 100644 index 0000000..20c35d2 --- /dev/null +++ b/c2a_generator/eh_rules_c.py @@ -0,0 +1,54 @@ +import csv +from pathlib import Path + + +def generate(src_path: str, dest_path: Path, eh_header: str) -> None: + assert dest_path.parent.exists(), f"{dest_path} does not exist" + with open(src_path, "r", encoding="utf-8") as csv_file, open(dest_path, "w", encoding="utf-8") as src_file: + src_file.write( + f""" +#pragma section REPRO +/** + * @file + * @brief EH の Rule 共通コード + */ +{eh_header} + +void EH_load_default_rules(void) +{{ + EH_RuleSettings settings; + +"""[ + 1: + ] + ) + reader = csv.reader(csv_file) + headers = next(reader) + dict_reader = csv.DictReader(csv_file, fieldnames=headers) + for row in dict_reader: + if not any(row): + continue + code = "" + if row["description"]: + row_description = row["description"].replace("\n", "\n// ") + code += f" // {row_description}\n" + code += f" settings.event.group = {row['group']};\n" + code += f" settings.event.local = {row['local']};\n" + code += f" settings.event.err_level = EL_ERROR_LEVEL_{row['err_level']};\n" + code += f" settings.should_match_err_level = {1 if row['should_match_err_level'] == 'TRUE' else 0};\n" + code += f" settings.condition.type = EH_RESPONSE_CONDITION_{row['type'].upper()};\n" + code += f" settings.condition.count_threshold = {row['count_threshold']};\n" + code += f" settings.condition.time_threshold_ms = {int(float(row['time_threshold[s]']) * 1000)};\n" + code += f" settings.deploy_bct_id = {row['bc']};\n" + code += f" settings.is_active = {1 if row['should_match_err_level'] == 'TRUE' else 0};\n" + code += f" EH_register_rule({row['name']}, &settings);\n\n" + src_file.write(code) + src_file.write( + """} +} + +#pragma section +"""[ + 1: + ] + ) diff --git a/c2a_generator/eh_rules_h.py b/c2a_generator/eh_rules_h.py new file mode 100644 index 0000000..f6470ca --- /dev/null +++ b/c2a_generator/eh_rules_h.py @@ -0,0 +1,56 @@ +import csv +from pathlib import Path + + +def generate(src_path: str, dest_path: Path, base_id: int) -> None: + assert dest_path.parent.exists(), f"{dest_path} does not exist" + with open(src_path, "r", encoding="utf-8") as csv_file, open(dest_path, "w", encoding="utf-8") as header_file: + header_file.write( + """ +/** + * @file + * @brief EH の Rule 共通ヘッダ + */ +#ifndef EVENT_HANDLER_RULES_H_ +#define EVENT_HANDLER_RULES_H_ + +/** + * @enum EH_RULE_ID + * @brief EH_Rule の ID + * @note 最大数は EH_RULE_MAX で規定 + * @note uint16_t を想定 + */ +typedef enum +{ +"""[ + 1: + ] + ) + reader = csv.reader(csv_file) + headers = next(reader) + dict_reader = csv.DictReader(csv_file, fieldnames=headers) + for row in dict_reader: + if not any(row): + continue + try: + header_file.write(f' {row["name"]} = {base_id},\n') + base_id += 1 + except ValueError: + continue + header_file.write( + """ +} EH_RULE_ID; + + +/** + * @brief event_handler のデフォルトルールを読み込む + * @param void + * @return void + */ +void EH_load_default_rules(void); + +#endif +"""[ + 1: + ] + ) diff --git a/c2a_generator/wings_json.py b/c2a_generator/wings_json.py index 833894a..ad1abf0 100644 --- a/c2a_generator/wings_json.py +++ b/c2a_generator/wings_json.py @@ -26,6 +26,9 @@ def generate( aobc_csv_path: Optional[Path] = None, tobc_csv_path: Optional[Path] = None, mif_csv_path: Optional[Path] = None, + eh_src: Optional[Path] = None, + eh_base_id: int = 0, + eh_list: list = [], ) -> None: data = [] if aobc_csv_path: @@ -37,7 +40,20 @@ def generate( if mif_csv_path: mif_bc_dict = csv_to_json(mif_csv_path) data.append({"obc_name": "MIF", "bc": mif_bc_dict, "el": [], "eh": []}) - data.append({"obc_name": "MOBC", "bc": [], "el": [], "eh": []}) + if eh_src: + with open(eh_src, "r", encoding="utf-8") as src_file: + reader = csv.reader(src_file) + headers = next(reader) + dict_reader = csv.DictReader(src_file, fieldnames=headers) + + for row in dict_reader: + if not any(row): + continue + if not row["name"].strip(): + continue + eh_list.append({"name": row["name"], "id": eh_base_id}) + eh_base_id += 1 + data.append({"obc_name": "MOBC", "bc": [], "el": [], "eh": eh_list}) bcid = 0 for src_path, bcid_base in bct_src: if bcid_base is not None: diff --git a/test.py b/test.py index 4c2fe66..d573a8b 100644 --- a/test.py +++ b/test.py @@ -6,6 +6,36 @@ Path(__file__).parent.parent / "sils-docker/sils/FlightSW/c2a-mobc-onglaisat" ) +# eh +c2a_generator.eh_rules_h.generate( + root_path / "design/eh.csv", + root_path / "src/src_user/Settings/System/EventHandlerRules/event_handler_rules.h", + base_id=9 +) +c2a_generator.eh_rules_c.generate( + root_path / "design/eh.csv", + root_path / "src/src_user/Settings/System/EventHandlerRules/event_handler_rules.c", + eh_header=""" +#include "event_handler_rules.h" +#include +#include "../../../TlmCmd/block_command_definitions.h" +#include "../../../TlmCmd/block_command_definitions.h" +#include "../../../IfWrapper/uart_user.h" + +#include "../../../Applications/UserDefined/Cdh/uart_fault_recovery.h" +#include "../../../Applications/UserDefined/Cdh/mode_monitor.h" +#include "../../../Applications/UserDefined/Power/under_voltage_control_utility.h" +#include "../../../Applications/UserDefined/Power/under_voltage_control.h" +#include "../../../Applications/UserDefined/Power/over_current.h" +#include "../../../Applications/UserDefined/Thermal/mobc_temperature_monitor.h" +#include "../../../Applications/UserDefined/Thermal/high_temperature_deviation_control.h" +#include "../../../Applications/UserDefined/Thermal/bat_heater_control.h" +#include "../../../Applications/UserDefined/Mission/mission_sequence.h" +#include "../../../Applications/UserDefined/Mission/rsi_sun_angle.h" +#include "../../../Applications/UserDefined/Com/comm_fdir.h" +""" +) + # cmd c2a_generator.cmd_def_c.generate( root_path / "design/cmd.csv", @@ -101,4 +131,15 @@ bct_src, root_path / "../../../wings/aspnetapp/WINGS/ClientApp/src/assets/alias/c2a_onglai.json", + eh_src=root_path / "design/eh.csv", + eh_base_id=9, + eh_list=[], +) + +c2a_generator.wings_json.generate( + bct_src, + root_path / "database/c2a_onglai.json", + eh_src=root_path / "design/eh.csv", + eh_base_id=9, + eh_list=[], )