Abfrage dazu, ob die Paarung Sensor und Distributor gefunden wurde

This commit is contained in:
2025-05-26 15:23:25 +02:00
parent 761bf22636
commit 4100eba906
+32 -25
View File
@@ -357,7 +357,10 @@ class Anlage():
self.add_sensor(sname, pos) self.add_sensor(sname, pos)
def get_sensor_point(self, sname:str) -> Point: def get_sensor_point(self, sname:str) -> Point:
return self._sensors[sname] if sname in self._sensors:
return self._sensors[sname]
raise Exception("Sensor not found")
def connect_sensors_to_racks(self) -> list: def connect_sensors_to_racks(self) -> list:
'''verbindet die Sensoren mit den Racks. '''verbindet die Sensoren mit den Racks.
@@ -373,7 +376,9 @@ class Anlage():
self.add_distributor(dname, pos) self.add_distributor(dname, pos)
def get_distributor_point(self, dname:str) -> Point: def get_distributor_point(self, dname:str) -> Point:
return self._distributors[dname] if dname in self._distributors:
return self._distributors[dname]
raise Exception("Distributor not found")
def connect_distributor_to_racks(self) -> list: def connect_distributor_to_racks(self) -> list:
'''verbindet die Unterverteiler mit den Racks '''verbindet die Unterverteiler mit den Racks
@@ -406,15 +411,13 @@ class Anlage():
candidates = [self._rack_lines[idx] for idx in candidates] candidates = [self._rack_lines[idx] for idx in candidates]
best_dist = max_dist best_dist = max_dist
best_line = candidates[0]
for line in candidates: for line in candidates:
dist = sensor.distance(line) dist = sensor.distance(line)
if dist < best_dist: if dist < best_dist:
best_dist = dist best_dist = dist
best_line = line best_line = line
if best_dist > max_dist:
raise LookupError("no line in correct distance found")
rack_name = self._rack_map[best_line] rack_name = self._rack_map[best_line]
nearest_point = best_line.interpolate(best_line.project(sensor)) nearest_point = best_line.interpolate(best_line.project(sensor))
return nearest_point, rack_name return nearest_point, rack_name
@@ -603,10 +606,15 @@ class Anlage():
def create_cable_paths(self, G) -> dict: def create_cable_paths(self, G) -> dict:
pfade = dict() pfade = dict()
pfade["kabel"] = list() pfade["kabel"] = list()
pfade["errors"] = list()
for sname, dname in self._sensor2dist.items(): for sname, dname in self._sensor2dist.items():
pfad_nodes, pfad_length = self.create_cable_path(G, sname, dname) try:
pfad_nodes, pfad_length = self.create_cable_path(G, sname, dname)
except:
pfade["errors"].append((sname, dname))
continue
pfad_coords = self._nodeids.get_points(pfad_nodes) pfad_coords = self._nodeids.get_points(pfad_nodes)
tuplecoords = [(p.x, p.y) for p in pfad_coords] #tuplecoords = [(p.x, p.y) for p in pfad_coords]
ld = dict() ld = dict()
ld['id'] = f"{dname}-{sname}" ld['id'] = f"{dname}-{sname}"
ld["coords"]= [{"x":round(p.x,1), "y":round(p.y,1)} for p in pfad_coords] ld["coords"]= [{"x":round(p.x,1), "y":round(p.y,1)} for p in pfad_coords]
@@ -815,15 +823,15 @@ class TestPlant(unittest.TestCase):
an.map_distributors_to_sensors(mapping) an.map_distributors_to_sensors(mapping)
G = nx.Graph() G = nx.Graph()
#Fülle eben erstellten Graphen mit Daten # Fülle eben erstellten Graphen mit Daten
pos = an.generate_graph(G) pos = an.generate_graph(G)
#Extrahiere Farb-Informationen der Kanten # Extrahiere Farb-Informationen der Kanten
edge_colors = [G[u][v].get('color', 'black') for u, v in G.edges()] edge_colors = [G[u][v].get('color', 'black') for u, v in G.edges()]
#Zeiche Graphen und zeige in # Zeiche Graphen und zeige in
nx.draw(G, pos, with_labels=False, node_size=10, font_size=8, edge_color=edge_colors) nx.draw(G, pos, with_labels=False, node_size=10, font_size=8, edge_color=edge_colors)
plt.show() plt.show()
#Ermittle kürzeste Wege von Unterverteilern zu zugehörigen Sensoren # Ermittle kürzeste Wege von Unterverteilern zu zugehörigen Sensoren
paths = an.create_cable_paths(G) paths = an.create_cable_paths(G)
paths_by_id = {p['id']: p for p in paths["kabel"]} paths_by_id = {p['id']: p for p in paths["kabel"]}
@@ -893,34 +901,34 @@ class TestPlant(unittest.TestCase):
mapping = {'Dist_1': ['Sens_1', 'Sens_2'], mapping = {'Dist_1': ['Sens_1', 'Sens_2'],
'Dist_2': ['Sens_3']} 'Dist_2': ['Sens_3']}
#Erstelle Anlage # Erstelle Anlage
an = Anlage(tol_snap=1) an = Anlage(tol_snap=1)
#Füge racks aus Daten hinzu # Füge racks aus Daten hinzu
an.set_racks(rack_segs) an.set_racks(rack_segs)
#Verbinde Racks miteinander (ggf. verlängere ungenaue Racks) # Verbinde Racks miteinander (ggf. verlängere ungenaue Racks)
an.join_racks() an.join_racks()
#Füge Sensoren als Knoten hinzu # Füge Sensoren als Knoten hinzu
an.add_sensors(sensors) an.add_sensors(sensors)
#Verbinde Sensoren mit deren naheliegendsten Racks # Verbinde Sensoren mit deren naheliegendsten Racks
an.connect_sensors_to_racks() an.connect_sensors_to_racks()
#Füge UV hinzu # Füge UV hinzu
an.add_distributors(distributors) an.add_distributors(distributors)
#Verbinde UV mit deren naheliegendsten Racks # Verbinde UV mit deren naheliegendsten Racks
an.connect_distributor_to_racks() an.connect_distributor_to_racks()
#Verknüpfe Sensoren mit zugehörigem UV # Verknüpfe Sensoren mit zugehörigem UV
an.map_distributors_to_sensors(mapping) an.map_distributors_to_sensors(mapping)
#Initialisiere Graph # Initialisiere Graph
G3 = nx.Graph() G3 = nx.Graph()
#Fülle eben erstellten Graphen mit Daten # Fülle eben erstellten Graphen mit Daten
pos = an.generate_graph(G3) pos = an.generate_graph(G3)
#Extrahiere Farb-Informationen der Kanten # Extrahiere Farb-Informationen der Kanten
edge_colors = [G3[u][v].get('color', 'black') for u, v in G3.edges()] edge_colors = [G3[u][v].get('color', 'black') for u, v in G3.edges()]
#Zeiche Graphen und zeige in # Zeiche Graphen und zeige in
nx.draw(G3, pos, with_labels=False, node_size=10, font_size=8, edge_color=edge_colors) nx.draw(G3, pos, with_labels=False, node_size=10, font_size=8, edge_color=edge_colors)
plt.show() plt.show()
#Ermittle kürzeste Wege von Unterverteilern zu zugehörigen Sensoren # Ermittle kürzeste Wege von Unterverteilern zu zugehörigen Sensoren
paths = an.create_cable_paths(G3) paths = an.create_cable_paths(G3)
paths_by_id = {p['id']: p for p in paths["kabel"]} paths_by_id = {p['id']: p for p in paths["kabel"]}
@@ -944,7 +952,6 @@ if __name__ == '__main__':
suite.addTest(TestPlant('test_add_point_interim')) suite.addTest(TestPlant('test_add_point_interim'))
suite.addTest(TestPlant('test_add_sensor')) suite.addTest(TestPlant('test_add_sensor'))
suite.addTest(TestPlant('test_add_equipment_w_tree')) suite.addTest(TestPlant('test_add_equipment_w_tree'))
suite.addTest(TestPlant('test_add_equipment_w_tree_batch'))
suite.addTest(TestPlant('test_wegsuche_str_tree')) suite.addTest(TestPlant('test_wegsuche_str_tree'))
suite.addTest(TestPlant('test_wegsuche_w_tree')) suite.addTest(TestPlant('test_wegsuche_w_tree'))
suite.addTest(TestPlant('test_generate_graph')) suite.addTest(TestPlant('test_generate_graph'))