import os import time import json # Pfad zur Eingabedatei und Ausgabedatei input_file = "src/dtc_defs.txt" output_file = "include/dtc_defs.h" json_output_file = "data_src/static/dtc_table.json" # Überprüfen, ob das Verzeichnis existiert, andernfalls erstellen json_output_dir = os.path.dirname(json_output_file) if not os.path.exists(json_output_dir): os.makedirs(json_output_dir) # Mehrdimensionales Array zum Speichern der Zeilen aus der Eingabedatei dtc_lines = [] # Lesen und analysieren der Eingabedatei with open(input_file, "r", encoding="utf-8") as f: for line in f: line = line.strip() if not line or line.startswith("#"): continue parts = line.split(";") if len(parts) == 5: num, dtc_name, dtc_severity, title, description = [part.strip() for part in parts] dtc_lines.append([int(num), dtc_name, dtc_severity, title, description]) # Überprüfen auf Duplikate in den DTC-Nummern und DTC-Namen num_set = set() dtc_name_set = set() duplicates = [] for line in dtc_lines: num, dtc_name, _, _, _ = line if num in num_set: duplicates.append(f"DTC-Nummer {num} ist ein Duplikat.") else: num_set.add(num) if dtc_name in dtc_name_set: duplicates.append(f"DTC-Name '{dtc_name}' ist ein Duplikat.") else: dtc_name_set.add(dtc_name) if duplicates: for duplicate in duplicates: print(f"Fehler: {duplicate}") raise ValueError("Duplicate DTC Data detected") # Suchen nach DTC_NO_DTC und DTC_LAST_DTC dtc_no_dtc_added = False dtc_last_dtc_line = None for line in dtc_lines: _, dtc_name, _, _, _ = line if dtc_name == "DTC_NO_DTC": dtc_no_dtc_added = True elif dtc_name == "DTC_LAST_DTC": dtc_last_dtc_line = line # Einen DTC für DTC_NO_DTC hinzufügen (wenn nicht vorhanden) if not dtc_no_dtc_added: dtc_lines.insert(0, [0, "DTC_NO_DTC", "DTC_NONE", "No Error", "No Error"]) # Falls DTC_LAST_DTC existiert, lösche es if dtc_last_dtc_line: dtc_lines.remove(dtc_last_dtc_line) # Einen DTC für DTC_LAST_DTC hinzufügen (mit der höchsten Nummer) if dtc_lines: highest_num = max([line[0] for line in dtc_lines]) else: highest_num = 0 dtc_lines.append([highest_num + 1, "DTC_LAST_DTC", "DTC_NONE", "Last Error", "Last Error"]) # Sortieren der Zeilen nach der Nummer aufsteigend dtc_lines.sort(key=lambda x: x[0]) # DTC_NAME_CONSTANT-Makros initialisieren dtc_macros = [] dtc_structs = [] dtc_table_data = [] # Verarbeiten der sortierten Zeilen for i, line in enumerate(dtc_lines): num, dtc_name, dtc_severity, title, description = line dtc_macros.append(f"#define {dtc_name:<30} {num}") comma = "," if i < len(dtc_lines) - 1 else " " dtc_structs.append(f" {{ {dtc_name:<30}, {dtc_severity:<12} }}{comma} // {description}") dtc_table_data.append({"num": num, "title": title, "description": description}) # Unix-Zeitstempel hinzufügen timestamp = int(time.time()) # Headerdatei schreiben with open(output_file, "w") as f: f.write(f"// Auto-generated by script on {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))}\n") f.write(f"#ifndef DTC_DEFS_H\n") f.write(f"#define DTC_DEFS_H\n\n") f.write("#include \n\n") f.write("typedef uint32_t DTCNum_t;\n\n") f.write("typedef enum\n") f.write("{\n") f.write(" DTC_INACTIVE,\n") f.write(" DTC_ACTIVE,\n") f.write(" DTC_PREVIOUS\n") f.write("} DTCActive_t;\n\n") f.write("typedef enum\n") f.write("{\n") f.write(" DTC_NONE,\n") f.write(" DTC_INFO,\n") f.write(" DTC_WARN,\n") f.write(" DTC_CRITICAL\n") f.write("} DTCSeverity_t;\n\n") f.write("typedef struct {\n") f.write(" DTCNum_t code;\n") f.write(" DTCSeverity_t severity;\n") f.write("} DTC_t;\n\n") # Füge die DTC_NAME_CONSTANT #define hinzu f.write("\n".join(dtc_macros) + "\n\n") f.write(f"const DTC_t dtc_definitions[] = {{\n") f.write(",\n".join(dtc_structs) + "\n") f.write("};\n\n") f.write(f"const uint32_t dtc_generation_timestamp = {timestamp};\n\n") f.write("#endif // DTC_DEFS_H\n") print(f"Header-Datei wurde erstellt: {output_file}") # JSON-Datei mit UTF-8-Zeichencodierung erstellen with open(json_output_file, 'w', encoding='utf-8') as json_f: json.dump(dtc_table_data, json_f, ensure_ascii=False, indent=4, separators=(',', ': ')) print(f"JSON-Datei wurde erstellt: {json_output_file}")