120 lines
3.5 KiB
Python
120 lines
3.5 KiB
Python
import os
|
|
import json
|
|
import argparse
|
|
import heapq
|
|
import math
|
|
import matplotlib.pyplot as plt
|
|
import networkx as nx
|
|
from shapely.geometry import LineString, Point
|
|
from shapely.ops import nearest_points
|
|
from plant import Anlage
|
|
|
|
# Funktionen
|
|
def load_json(jsonfilename):
|
|
with open(jsonfilename, encoding='utf-8') as fh:
|
|
return json.load(fh)
|
|
|
|
|
|
def create_plant(racks:dict, sensors:dict, distributors:dict, mapping:dict):
|
|
|
|
# racks = {'Rack_1-0': [Point(0, 0), Point(0, 10)],
|
|
# 'Rack_2-0': [Point(10, -2), Point(10, 5)],
|
|
# 'Rack_2-1': [Point(0, 3), Point(10, 3)]}
|
|
|
|
# sensors = {'Sens_1': Point(1, 1),
|
|
# 'Sens_2': Point(2, 4),
|
|
# 'Sens_3': Point(9, 2)}
|
|
|
|
# distributors = {'Dist_1': Point(-1, 9),
|
|
# 'Dist_2': Point(11, 0)}
|
|
|
|
# mapping = {'Dist_1': ['Sens_1', 'Sens_2'],
|
|
# 'Dist_2': ['Sens_3']}
|
|
|
|
# "mapping": {
|
|
# "UC0101": [
|
|
# "BG3241",
|
|
# "BG3240",
|
|
# "MA0062",
|
|
# "FC0062"
|
|
# ]
|
|
# }
|
|
|
|
an = Anlage()
|
|
# Füge racks aus Daten hinzu
|
|
an.set_racks(racks)
|
|
# Verbinde Racks miteinander (ggf. verlängere ungenaue Racks)
|
|
an.join_racks()
|
|
# Füge Sensoren als Knoten hinzu
|
|
an.add_sensors(sensors)
|
|
# Verbinde Sensoren mit deren naheliegendsten Racks
|
|
an.connect_sensors_to_racks()
|
|
# Füge UV hinzu
|
|
an.add_distributors(distributors)
|
|
# Verbinde UV mit deren naheliegendsten Racks
|
|
an.connect_distributor_to_racks()
|
|
# Verknüpfe Sensoren mit zugehörigem UV
|
|
an.map_distributors_to_sensors(mapping)
|
|
|
|
# Initialisiere Graph
|
|
G = nx.Graph()
|
|
# Fülle eben erstellten Graphen mit Daten
|
|
an.generate_graph(G)
|
|
|
|
# Ermittle kürzeste Wege von Unterverteilern zu zugehörigen Sensoren
|
|
paths = an.create_cable_paths(G)
|
|
if args.graph:
|
|
draw_graph(G,an)
|
|
|
|
def draw_graph(G:nx.Graph, an:Anlage):
|
|
pos = an.get_node_positions()
|
|
|
|
edge_colors = [G[u][v].get('color', 'black') for u, v in G.edges()]
|
|
|
|
nx.draw(G, pos, with_labels=False, node_size=10, font_size=8, edge_color=edge_colors)
|
|
plt.show()
|
|
|
|
def prepare_data(rawdata:dict):
|
|
sensors = data["sensors"]
|
|
subdists = data["distributors"]
|
|
racks = data["racks"]
|
|
mapping = data["mapping"]
|
|
dracks = dict()
|
|
for rname,lp in racks:
|
|
if rname not in dracks:
|
|
dracks[rname] = list()
|
|
dracks[rname].append(Point(lp))
|
|
|
|
return (sensors, subdists, dracks, mapping)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description='Berechne Wege von Sensoren zu Verteilern über Kabeltrassen')
|
|
parser.add_argument('-f', '--filename', action='store', required=True, default="easy_position.json", help='file with all informations about positions gathered from getpositions', metavar='my_positions.json')
|
|
parser.add_argument('-c', '--console', action='store_true', help='Ausgabe auf Konsole')
|
|
parser.add_argument('-g', '--graph', action='store_true', help='Zeichnet den Graphen der Anlage')
|
|
|
|
args = parser.parse_args()
|
|
|
|
# Umgebungsvariablen
|
|
work_dir = os.environ.get("PROJECT_WORK")
|
|
config_dir = os.environ.get("PROJECT_CFG")
|
|
|
|
# Pfade zu JSON-Dateien
|
|
jsonfilename = args.filename
|
|
sensors_path = os.path.join(work_dir, jsonfilename)
|
|
|
|
# Einlesen
|
|
rawdata = load_json(sensors_path)
|
|
|
|
(racks, sensors, subdists, mapping) = prepare_data(rawdata)
|
|
|
|
|
|
plant = create_plant(racks, sensors, subdists, mapping)
|
|
|
|
# Erstelle Anlage
|
|
|
|
|
|
|
|
|
|
|