From 85ae4f17f42a6c069c96cb08251768572e7d975f Mon Sep 17 00:00:00 2001 From: lertlmaier Date: Thu, 12 Jun 2025 12:41:27 +0200 Subject: [PATCH] Fehler-Handling erweitert: Warnings die bei erstellung des Mappings entstehen werden durch routing hindurch bis nach drawdxf geleitet und in Excel ausgabe geschrieben. --- lib/drawdxf.py | 34 ++++++++++++++++++++++++---------- lib/getpositions.py | 7 ++++--- lib/routing.py | 11 +++++++---- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/lib/drawdxf.py b/lib/drawdxf.py index c4151e1..983c0ac 100644 --- a/lib/drawdxf.py +++ b/lib/drawdxf.py @@ -4,7 +4,7 @@ import json import os.path from dataclasses import dataclass, asdict, fields from dacite import from_dict -from typing import List +from typing import List, Dict from datetime import datetime from openpyxl import Workbook import math @@ -30,12 +30,14 @@ class Polyline: ret.append( (p.x, p.y) ) return ret -@dataclass +@dataclass +# Fehlgeschlagene Anbindung von einem Sensor / Dist zu einem Rack class Error_Connection: name: str coords: Point @dataclass +# Felgeschlagene Verbindung von einem Dist zu Sensor(en) aus beliebigem Grund class Error_Routing: unterverteiler: str sensoren: List[str] @@ -46,8 +48,9 @@ class Polylines: errors_routing: List[Error_Routing] errors_sensors: List[Error_Connection] errors_dists: List[Error_Connection] - errors_existing_dists: List[str] - errors_existing_sensors: List[str] + errors_dists_not_in_layout: List[str] + errors_sensors_not_in_layout: List[str] + errors_missing_attributes: Dict[str, str] def add_polyline(msp, points:Polyline, dxf_attribs): @@ -305,22 +308,24 @@ def write_excel_from_json(plines:Polylines, outpath:str): cumm_length = length_summary.get(artnr, "") ws2.append([artnr, count, cumm_length]) + # Abfage ob Fehler Worsheets ausgegeben werden - if len(plines.errors_existing_sensors) > 0 or len(plines.errors_dists) > 0: + if len(plines.errors_sensors) > 0 or len(plines.errors_dists) > 0: # Worksheet 3 - Nicht an Racks gekoppeltes Equipment - ws3 = wb.create_sheet("Not connected Equipment") + ws3 = wb.create_sheet("ERR-Equipment-Connection") ws3.append(["Type", "ID", "x", "y"]) + # nicht angebundene Sensoren for error in plines.errors_sensors: ws3.append(["Sensor / Actuator", error.name, error.coords.x, error.coords.y]) - + # nicht angebundene Distributoren for error in plines.errors_dists: ws3.append(["Subistributor", error.name, error.coords.x, error.coords.y]) if len(plines.errors_routing) > 0: # Worksheet 4 - Fehlgeschlagenes Routing - ws4 = wb.create_sheet("Routing Errors") + ws4 = wb.create_sheet("ERR-Routing") ws4.append(["Subdistributor", "Sensor / Actuator", "Details"]) nicht_angebunden = set(e.name for e in plines.errors_sensors + plines.errors_dists) @@ -329,8 +334,9 @@ def write_excel_from_json(plines:Polylines, outpath:str): uv = routing_error.unterverteiler uv_nicht_angebunden = uv in nicht_angebunden - if uv in plines.errors_existing_dists: - ws4.append([uv,"-", "Distributor not found in given layout"]) + # Wenn Distributor nicht in Layout gefunden, dann nur diesen Fehler ausgeben. Alle Verbindungen hier fehlgeschlagen + if uv in plines.errors_dists_not_in_layout: + ws4.append([uv,"-", "Distributor not found in given layout."]) continue for sensor in routing_error.sensoren: @@ -346,6 +352,14 @@ def write_excel_from_json(plines:Polylines, outpath:str): grund = "Failed routing (not caused by missing connection)" ws4.append([uv, sensor, grund]) + + if len(plines.errors_missing_attributes) > 0: + # Worksheet 5 - Fehlende Attribute -> kein Routing + ws5 = wb.create_sheet("ERR-Attributes") + ws5.append(["ID", "Error Detail"]) + + for sname, err_msg in plines.errors_missing_attributes.items(): + ws5.append([sname, err_msg]) wb.save(outpath) diff --git a/lib/getpositions.py b/lib/getpositions.py index 127eac7..1616e0a 100644 --- a/lib/getpositions.py +++ b/lib/getpositions.py @@ -160,10 +160,10 @@ def create_mappings(positions:dict) -> dict: dnamen = dict() # sammle die Sensoren mit ihren zugehörigen Unterverteilern sensor2unterverteiler = dict() - warnings = list() + warnings = dict() for sensorname,v in positions.items(): if "KENNZEICHNUNG" not in v: - warnings.append(f"{sensorname}: keine KENNZEICHNUNG vorhanden") + warnings[sensorname] = "keine KENNZEICHNUNG vorhanden" continue unterverteiler_pfad = v["KENNZEICHNUNG"] @@ -182,7 +182,7 @@ def create_mappings(positions:dict) -> dict: # match.group(4) # - # match.group(5) # KF1FDI7 else: - warnings.append(unterverteiler_pfad) + warnings[sensorname] = f"Ungültiger Pfad in Kennzeichnung: {unterverteiler_pfad}" continue if verteiler not in dnamen: @@ -519,6 +519,7 @@ if __name__ == '__main__': if args.write: basename = os.path.splitext(args.write)[0] res_not_found = check_existance(res_mappings, res_dist, res_sens) + res_not_found["missing_attributes"] = warnings output_results["not_found"] = res_not_found write_results(to_json(output_results), work_dir, f"{basename}.json") diff --git a/lib/routing.py b/lib/routing.py index 4d61ba8..25ec199 100644 --- a/lib/routing.py +++ b/lib/routing.py @@ -144,11 +144,13 @@ def prepare_data(rawdata:dict): dtunlength = rawdata["tunnels"]["length"] + # Fehler, welche im getpositions auftreten weiterführen: im Layout fehlende Dists / Sensoren / fehlende Attribute errors_dists = rawdata["not_found"]["missing_distributors"] errors_sensors = rawdata["not_found"]["missing_sensors"] + errors_attributes = rawdata["not_found"]["missing_attributes"] - return (dracks, dsensors, dsubdists, mapping, dtunnels, dtunlength, errors_dists, errors_sensors) + return (dracks, dsensors, dsubdists, mapping, dtunnels, dtunlength, errors_dists, errors_sensors, errors_attributes) if __name__ == "__main__": @@ -170,7 +172,7 @@ if __name__ == "__main__": # Einlesen und Vorbereiten der Daten rawdata = load_json(sensors_path) - (racks, sensors, subdists, mapping, tunnels, tunlength, errors_dists, errors_sensors) = prepare_data(rawdata) + (racks, sensors, subdists, mapping, tunnels, tunlength, errors_dists, errors_sensors, errors_attributes) = prepare_data(rawdata) config = configparser.ConfigParser(allow_no_value=True, delimiters=("=")) config.optionxform = lambda option: option # preserve case for letters @@ -191,8 +193,9 @@ if __name__ == "__main__": # Ausgabe schreiben if args.write: basename = os.path.splitext(args.write)[0] - cable_paths["errors_existing_dists"] = errors_dists - cable_paths["errors_existing_sensors"] = errors_sensors + cable_paths["errors_dists_not_in_layout"] = errors_dists + cable_paths["errors_sensors_not_in_layout"] = errors_sensors + cable_paths["errors_missing_attributes"] = errors_attributes write_results(to_json(cable_paths), work_dir, f"{basename}.json")