369 lines
12 KiB
TypeScript
369 lines
12 KiB
TypeScript
"use client";
|
||
// /components/main/kabelueberwachung/kue705FO/Charts/LoopMeasurementChart/LoopChartActionBar.tsx
|
||
import React from "react";
|
||
import DateRangePicker from "@/components/common/DateRangePicker";
|
||
import { useDispatch, useSelector } from "react-redux";
|
||
import { RootState } from "@/redux/store";
|
||
import {
|
||
setLoopMeasurementCurveChartData,
|
||
setSelectedMode,
|
||
setChartOpen,
|
||
setLoading,
|
||
} from "@/redux/slices/kabelueberwachungChartSlice";
|
||
import { setBrushRange } from "@/redux/slices/brushSlice";
|
||
import { Listbox } from "@headlessui/react";
|
||
import { setChartTitle as setLoopChartTitle } from "@/redux/slices/loopChartTypeSlice";
|
||
|
||
//-----------------------------------------------------------------------------------useLoopChartLoader
|
||
export const useLoopChartLoader = () => {
|
||
const dispatch = useDispatch();
|
||
const { vonDatum, bisDatum, selectedMode, selectedSlotType, slotNumber } =
|
||
useSelector((state: RootState) => state.kabelueberwachungChartSlice);
|
||
const hasShownNoDataAlert = React.useRef(false);
|
||
|
||
const formatDate = (dateString: string) => {
|
||
const [year, month, day] = dateString.split("-");
|
||
return `${year};${month};${day}`;
|
||
};
|
||
|
||
const getApiUrl = (
|
||
mode: "DIA0" | "DIA1" | "DIA2",
|
||
type: number,
|
||
slotNumber: number
|
||
) => {
|
||
const typeFolder = "schleifenwiderstand";
|
||
|
||
let url: string;
|
||
|
||
if (process.env.NODE_ENV === "development") {
|
||
url = `/api/cpl/slotDataAPIHandler?slot=${slotNumber}&messart=${typeFolder}&dia=${mode}&vonDatum=${vonDatum}&bisDatum=${bisDatum}`;
|
||
} else {
|
||
url = `${window.location.origin}/CPL?seite.ACP&${mode}=${formatDate(
|
||
vonDatum
|
||
)};${formatDate(bisDatum)};${slotNumber};${type};`;
|
||
}
|
||
|
||
console.log("API URL:", url); // Hier wird die URL in der Konsole ausgegeben
|
||
return url;
|
||
};
|
||
|
||
const loadLoopChartData = async () => {
|
||
const type = selectedSlotType === "schleifenwiderstand" ? 4 : 3;
|
||
if (slotNumber === null) return;
|
||
|
||
dispatch(setLoading(true));
|
||
dispatch(setChartOpen(false));
|
||
dispatch(setLoopMeasurementCurveChartData([]));
|
||
|
||
const startTime = Date.now();
|
||
const MIN_LOADING_TIME_MS = 1000;
|
||
|
||
try {
|
||
const apiUrl = getApiUrl(selectedMode, type, slotNumber);
|
||
const response = await fetch(apiUrl);
|
||
const data = await response.json();
|
||
|
||
const waitTime = Math.max(
|
||
0,
|
||
MIN_LOADING_TIME_MS - (Date.now() - startTime)
|
||
);
|
||
await new Promise((res) => setTimeout(res, waitTime));
|
||
|
||
if (Array.isArray(data) && data.length > 0) {
|
||
dispatch(setLoopMeasurementCurveChartData(data));
|
||
dispatch(setChartOpen(true));
|
||
} else {
|
||
dispatch(setLoopMeasurementCurveChartData([]));
|
||
dispatch(setChartOpen(false));
|
||
if (!hasShownNoDataAlert.current) {
|
||
alert("⚠️ Keine Messdaten im gewählten Zeitraum gefunden");
|
||
hasShownNoDataAlert.current = true; // ⬅️ Nur einmal zeigen
|
||
}
|
||
}
|
||
} catch (err) {
|
||
console.error("❌ Fehler beim Laden:", err);
|
||
alert("❌ Fehler beim Laden.");
|
||
} finally {
|
||
dispatch(setLoading(false));
|
||
}
|
||
};
|
||
|
||
return { loadLoopChartData };
|
||
};
|
||
|
||
//-----------------------------------------------------------------------------------LoopChartActionBar
|
||
const LoopChartActionBar: React.FC = () => {
|
||
const dispatch = useDispatch();
|
||
|
||
const {
|
||
vonDatum,
|
||
bisDatum,
|
||
selectedMode,
|
||
selectedSlotType,
|
||
slotNumber,
|
||
isLoading,
|
||
} = useSelector((state: RootState) => state.kabelueberwachungChartSlice);
|
||
|
||
const { chartTitle } = useSelector((state: RootState) => state.loopChartType);
|
||
|
||
const getApiUrl = (
|
||
mode: "DIA0" | "DIA1" | "DIA2",
|
||
type: number,
|
||
slotNumber: number
|
||
) => {
|
||
const typeFolder = "schleifenwiderstand";
|
||
|
||
const baseUrl =
|
||
process.env.NODE_ENV === "development"
|
||
? `/api/cpl/slotDataAPIHandler?slot=${slotNumber}&messart=${typeFolder}&dia=${mode}&vonDatum=${vonDatum}&bisDatum=${bisDatum}`
|
||
: `${window.location.origin}/CPL?seite.ACP&${mode}=${formatDate(
|
||
vonDatum
|
||
)};${formatDate(bisDatum)};${slotNumber};${type};`;
|
||
console.log("baseUrl", baseUrl);
|
||
|
||
return baseUrl;
|
||
};
|
||
|
||
const formatDate = (dateString: string) => {
|
||
const [year, month, day] = dateString.split("-");
|
||
return `${year};${month};${day}`;
|
||
};
|
||
|
||
const handleStartRSL = async () => {
|
||
if (slotNumber === null) {
|
||
alert("⚠️ Bitte zuerst einen KÜ auswählen!");
|
||
return;
|
||
}
|
||
|
||
const cgiUrl = `${window.location.origin}/CPL?kabelueberwachung.html&KS_${slotNumber}=1`;
|
||
|
||
try {
|
||
console.log("🚀 Starte RSL Messung für Slot:", slotNumber);
|
||
console.log("📡 CGI URL:", cgiUrl);
|
||
|
||
const response = await fetch(cgiUrl);
|
||
|
||
if (!response.ok) {
|
||
throw new Error(`CGI-Fehler: ${response.status}`);
|
||
}
|
||
|
||
console.log("✅ RSL Messung gestartet für Slot", slotNumber);
|
||
alert(`✅ RSL Messung für Slot ${slotNumber + 1} gestartet`);
|
||
} catch (err) {
|
||
console.error("❌ Fehler beim Starten der RSL Messung:", err);
|
||
alert("❌ Fehler beim Starten der RSL Messung.");
|
||
}
|
||
};
|
||
|
||
const handleFetchData = async () => {
|
||
const type = selectedSlotType === "schleifenwiderstand" ? 4 : 3;
|
||
|
||
if (slotNumber === null) {
|
||
alert("⚠️ Bitte zuerst einen KÜ auswählen!");
|
||
return;
|
||
}
|
||
|
||
const apiUrl = getApiUrl(selectedMode, type, slotNumber);
|
||
if (!apiUrl) return;
|
||
|
||
dispatch(setLoading(true));
|
||
dispatch(setChartOpen(false));
|
||
dispatch(setLoopMeasurementCurveChartData([]));
|
||
|
||
const MIN_LOADING_TIME_MS = 1000;
|
||
const startTime = Date.now();
|
||
|
||
try {
|
||
const response = await fetch(apiUrl, {
|
||
method: "GET",
|
||
headers: { "Content-Type": "application/json" },
|
||
});
|
||
|
||
if (!response.ok) throw new Error(`Fehler: ${response.status}`);
|
||
|
||
const jsonData = await response.json();
|
||
const elapsedTime = Date.now() - startTime;
|
||
const waitTime = Math.max(0, MIN_LOADING_TIME_MS - elapsedTime);
|
||
await new Promise((resolve) => setTimeout(resolve, waitTime));
|
||
//------------------------
|
||
console.log("▶️ Lade Daten für:");
|
||
console.log(" Slot:", slotNumber);
|
||
console.log(" Typ:", selectedSlotType, "→", type);
|
||
console.log(" Modus:", selectedMode);
|
||
console.log(" Von:", vonDatum);
|
||
console.log(" Bis:", bisDatum);
|
||
console.log(" URL:", apiUrl);
|
||
console.log(" Daten:", jsonData);
|
||
|
||
//-----------------------
|
||
|
||
if (Array.isArray(jsonData) && jsonData.length > 0) {
|
||
dispatch(setLoopMeasurementCurveChartData(jsonData));
|
||
dispatch(setChartOpen(true));
|
||
} else {
|
||
alert("⚠️ Keine Messdaten im gewählten Zeitraum gefunden.");
|
||
dispatch(setLoopMeasurementCurveChartData([]));
|
||
dispatch(setChartOpen(false));
|
||
}
|
||
} catch (err) {
|
||
console.error("❌ Fehler beim Laden der Daten:", err);
|
||
alert("❌ Fehler beim Laden der Daten.");
|
||
} finally {
|
||
dispatch(setLoading(false));
|
||
}
|
||
};
|
||
|
||
return (
|
||
<div className="flex justify-between p-1 bg-gray-100 rounded-lg ">
|
||
<div className="flex items-center">
|
||
<label className="text-sm font-semibold">
|
||
KÜ {slotNumber !== null ? slotNumber + 1 : "-"}
|
||
</label>
|
||
</div>
|
||
|
||
<div className="flex items-center space-x-2">
|
||
{/* DateRangePicker – wie bei ISO nur sichtbar bei Messkurve */}
|
||
<div
|
||
style={{
|
||
visibility: chartTitle === "Messkurve" ? "visible" : "hidden",
|
||
}}
|
||
>
|
||
<DateRangePicker compact />
|
||
</div>
|
||
|
||
{/* DIA0/DIA1/DIA2 Dropdown – nur sichtbar bei Messkurve */}
|
||
<div
|
||
style={{
|
||
visibility: chartTitle === "Messkurve" ? "visible" : "hidden",
|
||
}}
|
||
>
|
||
<Listbox
|
||
value={selectedMode}
|
||
onChange={(value) => {
|
||
dispatch(setSelectedMode(value));
|
||
dispatch(setBrushRange({ startIndex: 0, endIndex: 0 }));
|
||
}}
|
||
>
|
||
<div className="relative w-48">
|
||
<Listbox.Button className="w-full border px-3 py-1 rounded text-left bg-white flex justify-between items-center text-sm">
|
||
<span>
|
||
{
|
||
{
|
||
DIA0: "Alle Messwerte",
|
||
DIA1: "Stündliche Werte",
|
||
DIA2: "Tägliche Werte",
|
||
}[selectedMode]
|
||
}
|
||
</span>
|
||
<svg
|
||
className="w-5 h-5 text-gray-400"
|
||
viewBox="0 0 20 20"
|
||
fill="currentColor"
|
||
>
|
||
<path
|
||
fillRule="evenodd"
|
||
d="M5.23 7.21a.75.75 0 011.06.02L10 10.585l3.71-3.355a.75.75 0 111.02 1.1l-4.25 3.85a.75.75 0 01-1.02 0l-4.25-3.85a.75.75 0 01.02-1.06z"
|
||
clipRule="evenodd"
|
||
/>
|
||
</svg>
|
||
</Listbox.Button>
|
||
<Listbox.Options className="absolute z-50 mt-1 w-full border rounded bg-white shadow max-h-60 overflow-auto text-sm">
|
||
{["DIA0", "DIA1", "DIA2"].map((mode) => (
|
||
<Listbox.Option
|
||
key={mode}
|
||
value={mode}
|
||
className={({ selected, active }) =>
|
||
`px-4 py-1 cursor-pointer ${
|
||
selected
|
||
? "bg-littwin-blue text-white"
|
||
: active
|
||
? "bg-gray-200"
|
||
: ""
|
||
}`
|
||
}
|
||
>
|
||
{
|
||
{
|
||
DIA0: "Alle Messwerte",
|
||
DIA1: "Stündliche Werte",
|
||
DIA2: "Tägliche Werte",
|
||
}[mode]
|
||
}
|
||
</Listbox.Option>
|
||
))}
|
||
</Listbox.Options>
|
||
</div>
|
||
</Listbox>
|
||
</div>
|
||
{/* Dropdown für Messkurve / Meldungen (wie ISO) */}
|
||
<Listbox
|
||
value={chartTitle}
|
||
onChange={(value: "Messkurve" | "Meldungen") =>
|
||
dispatch(setLoopChartTitle(value))
|
||
}
|
||
>
|
||
<div className="relative w-40">
|
||
<Listbox.Button className="w-full border px-3 py-1 rounded text-left bg-white flex justify-between items-center text-sm">
|
||
<span>{chartTitle}</span>
|
||
<svg
|
||
className="w-5 h-5 text-gray-400"
|
||
viewBox="0 0 20 20"
|
||
fill="currentColor"
|
||
>
|
||
<path
|
||
fillRule="evenodd"
|
||
d="M5.23 7.21a.75.75 0 011.06.02L10 10.585l3.71-3.355a.75.75 0 111.02 1.1l-4.25 3.85a.75.75 0 01-1.02 0l-4.25-3.85a.75.75 0 01.02-1.06z"
|
||
clipRule="evenodd"
|
||
/>
|
||
</svg>
|
||
</Listbox.Button>
|
||
<Listbox.Options className="absolute z-50 mt-1 w-full border rounded bg-white shadow max-h-60 overflow-auto text-sm">
|
||
{(["Messkurve", "Meldungen"] as const).map((option) => (
|
||
<Listbox.Option
|
||
key={option}
|
||
value={option}
|
||
className={({ selected, active }) =>
|
||
`px-4 py-1 cursor-pointer ${
|
||
selected
|
||
? "bg-littwin-blue text-white"
|
||
: active
|
||
? "bg-gray-200"
|
||
: ""
|
||
}`
|
||
}
|
||
>
|
||
{option}
|
||
</Listbox.Option>
|
||
))}
|
||
</Listbox.Options>
|
||
</div>
|
||
</Listbox>
|
||
|
||
{/* Buttons – nur sichtbar bei Messkurve, Platz bleibt erhalten */}
|
||
<div
|
||
style={{
|
||
visibility: chartTitle === "Messkurve" ? "visible" : "hidden",
|
||
}}
|
||
className="flex items-center space-x-2"
|
||
>
|
||
<button
|
||
onClick={handleStartRSL}
|
||
className="px-4 py-1 bg-littwin-blue text-white rounded text-sm whitespace-nowrap"
|
||
disabled={isLoading}
|
||
>
|
||
RSL starten
|
||
</button>
|
||
<button
|
||
onClick={handleFetchData}
|
||
className="px-4 py-1 bg-littwin-blue text-white rounded text-sm whitespace-nowrap"
|
||
>
|
||
Daten laden
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default LoopChartActionBar;
|