digitale Eingänge Redux Slice erstellt für mehr Übersicht
This commit is contained in:
50
hooks/einausgaenge/useDigitalOutputsData.ts
Normal file
50
hooks/einausgaenge/useDigitalOutputsData.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
"use client"; // /hooks/einausgaenge/useDigitalOutputsData.ts
|
||||
import { useState, useEffect } from "react";
|
||||
|
||||
// Definition des Typs für digitale Ausgänge
|
||||
interface DigitalOutput {
|
||||
id: number;
|
||||
description: string;
|
||||
toggle: boolean;
|
||||
}
|
||||
|
||||
export function useDigitalOutputs() {
|
||||
const [digitalOutputs, setDigitalOutputs] = useState<DigitalOutput[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const script = document.createElement("script");
|
||||
script.src = "/CPLmockData/SERVICE/da.js";
|
||||
script.async = true;
|
||||
|
||||
script.onload = () => {
|
||||
const da = window.win_da_state;
|
||||
const bezeichnungen = window.win_da_bezeichnung;
|
||||
|
||||
if (da && bezeichnungen) {
|
||||
const outputs = da.map((status: number, index: number) => ({
|
||||
id: index + 1,
|
||||
description: bezeichnungen[index] || `Ausgang${index + 1}`,
|
||||
toggle: status === 1,
|
||||
}));
|
||||
setDigitalOutputs(outputs);
|
||||
} else {
|
||||
console.error("Daten konnten nicht geladen werden.");
|
||||
}
|
||||
setIsLoading(false);
|
||||
};
|
||||
|
||||
script.onerror = () => {
|
||||
console.error("Fehler beim Laden der da.js-Datei.");
|
||||
setIsLoading(false);
|
||||
};
|
||||
|
||||
document.body.appendChild(script);
|
||||
|
||||
return () => {
|
||||
document.body.removeChild(script);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return { digitalOutputs, isLoading, setDigitalOutputs };
|
||||
}
|
||||
Reference in New Issue
Block a user