// /utils/createAndSetDevices.js
import L from "leaflet";
import "leaflet.smooth_marker_bouncing";
import { SERVER_URL } from "../config/urls.js";
import * as config from "../config/config.js";
// Funktion zur Bestimmung der Farbklasse basierend auf der Priorität
const getColorClass = (level) => {
if (level === 1) return "red";
if (level === 2) return "orange";
if (level === 3) return "yellow";
return "green"; // Standardfarbe
};
// Funktion zum Erstellen und Setzen von Markern
export const createAndSetDevices = async (systemId, setMarkersFunction, GisSystemStatic, priorityConfig) => {
try {
// Lade die statischen Daten
const response1 = await fetch(config.mapGisStationsStaticDistrictUrl);
const jsonResponse = await response1.json();
const response2 = await fetch(config.mapGisStationsStatusDistrictUrl);
const statusResponse = await response2.json();
const getIdSystemAndAllowValueMap = new Map(GisSystemStatic.map((system) => [system.IdSystem, system.Allow]));
if (jsonResponse.Points && statusResponse.Statis) {
const statisMap = new Map(statusResponse.Statis.map((s) => [s.IdLD, s]));
let markersData = jsonResponse.Points
.filter((station) => station.System === systemId && getIdSystemAndAllowValueMap.get(station.System) === 1)
.map((station) => {
const statis = statisMap.get(station.IdLD);
// Priorität und Farbklasse bestimmen
const priorityConfigForStation = priorityConfig.find((p) => p.location_id === station.IdLD);
const level = priorityConfigForStation ? priorityConfigForStation.prio_level : 5; // Standardlevel
const colorClass = getColorClass(level); // Farbklasse ermitteln
// Dynamische CSS-Filter verwenden
const marker = L.marker([station.X, station.Y], {
icon: L.divIcon({
className: `leaflet-marker-icon ${colorClass}`, // Dynamische CSS-Klasse für Filter
html: `
`, // Dynamischer Filter
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
}),
zIndexOffset: 100 * (6 - level), // Z-Index nach Priorität
});
// Popup-Info dynamisch erstellen
const statusInfo = statusResponse.Statis.filter((status) => status.IdLD === station.IdLD)
.reverse()
.map(
(status) => `