Files
nodeMap/utils/createAndSetDevices.js
2025-02-03 08:46:18 +01:00

91 lines
3.6 KiB
JavaScript

// /utils/createAndSetDevices.js
import L from "leaflet";
import "leaflet.smooth_marker_bouncing";
import { toast } from "react-toastify";
import * as config from "../config/config.js";
import { disablePolylineEvents, enablePolylineEvents } from "./setupPolylines";
import { store } from "../redux/store"; // Redux-Store importieren
import { updateLineStatus } from "../redux/slices/lineVisibilitySlice"; // Redux-Slice importieren
// Funktion zum Bestimmen der Priorität basierend auf dem Icon-Pfad
const determinePriority = (iconPath, priorityConfig) => {
for (let priority of priorityConfig) {
if (iconPath.includes(priority.name.toLowerCase())) {
return priority.level;
}
}
return 5; // Standardpriorität (niedrigste)
};
// **Funktion zum Erstellen und Setzen von Markern**
export const createAndSetDevices = async (systemId, setMarkersFunction, GisSystemStatic, priorityConfig) => {
try {
// API-Daten abrufen
const response1 = await fetch(config.mapGisStationsStaticDistrictUrl);
const jsonResponse = await response1.json();
const response2 = await fetch(config.mapGisStationsStatusDistrictUrl);
const statusResponse = await response2.json();
if (!jsonResponse.Points || !statusResponse.Statis) {
console.error("❌ Fehlende Daten in API-Response!");
return;
}
console.log("✅ API-Daten geladen:", jsonResponse.Points.length, "Punkte gefunden.");
// **Filter: Nur Stationen mit `Active === 1`**
const activeStations = jsonResponse.Points.filter((station) => station.System === systemId && station.Active === 1);
console.log("🔍 Gefilterte aktive Stationen:", activeStations.length, activeStations);
const statisMap = new Map(statusResponse.Statis.map((s) => [s.IdLD, s]));
let markersData = activeStations.map((station) => {
const statis = statisMap.get(station.IdLD);
const iconPath = statis ? `img/icons/${statis.Na}-marker-icon-${station.Icon}.png` : `img/icons/marker-icon-${station.Icon}.png`;
const priority = determinePriority(iconPath, priorityConfig);
const zIndexOffset = 100 * (5 - priority);
const marker = L.marker([station.X, station.Y], {
icon: L.icon({
iconUrl: iconPath,
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
}),
title: station.LD_Name,
contextmenu: true,
data: { Active: station.Active, idLD: station.IdLD }, // Speichere Active & idLD
zIndexOffset: zIndexOffset,
});
marker.bindPopup(`
<div class="bg-white rounded-lg">
<span class="text-lg font-semibold text-gray-900">${station.LD_Name}</span>
<span class="text-md font-bold text-gray-800"> ${station.Device}</span><br>
<span class="text-gray-800"><strong> ${station.Area_Short} </strong>(${station.Area_Name})</span><br>
<span class="text-gray-800"><strong>${station.Location_Short} </strong> (${station.Location_Name})</span>
</div>
`);
// **Speichere `idLD` und `Active` in Redux**
store.dispatch(updateLineStatus({ idLD: station.IdLD, active: station.Active }));
return marker;
});
console.log("📌 Marker erstellt:", markersData.length, markersData);
// **Sicherstellen, dass vorhandene Marker nicht überschrieben werden**
setMarkersFunction((prevMarkers) => [...prevMarkers, ...markersData]);
// **Debugging: Alle Marker in der Konsole anzeigen**
console.log(
"📌 Alle Marker (Debugging):",
markersData.map((m) => m.options.title)
);
} catch (error) {
console.error("❌ Fehler beim Abrufen der Daten: ", error);
}
};