analoge eingänge
This commit is contained in:
47
services/fetchAnalogInputsService.ts
Normal file
47
services/fetchAnalogInputsService.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
// services/fetchAnalogInputsService.ts
|
||||
|
||||
export const fetchAnalogInputsService = async () => {
|
||||
const mode = process.env.NEXT_PUBLIC_CPL_MODE;
|
||||
|
||||
if (mode === "production") {
|
||||
const scriptUrl = "/CPL?/CPL/SERVICE/analogInputs.js";
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const script = document.createElement("script");
|
||||
script.src = scriptUrl;
|
||||
script.async = true;
|
||||
script.onload = () => resolve();
|
||||
script.onerror = () => reject("❌ Fehler beim Laden der analogInputs.js");
|
||||
document.body.appendChild(script);
|
||||
});
|
||||
|
||||
const win = window as any;
|
||||
|
||||
return Array.from({ length: 8 }, (_, i) => ({
|
||||
id: i + 1,
|
||||
value: parseFloat(win.win_analogInputsValues[i]),
|
||||
label: win.win_analogInputsNames[i],
|
||||
unit: win.win_analogInputsUnits[i],
|
||||
offset: parseFloat(win.win_analogInputsOffset[i]),
|
||||
factor: parseFloat(win.win_analogInputsFactor[i]),
|
||||
interval: parseInt(win.win_analogInputsloggerIntervall[i]),
|
||||
weighting: parseInt(win.win_analogInputsWeighting[i]),
|
||||
}));
|
||||
} else {
|
||||
const res = await fetch("/api/cpl/getAnalogInputsHandler");
|
||||
if (!res.ok) throw new Error("❌ Fehler beim Laden der analogen Eingänge");
|
||||
|
||||
const data = await res.json();
|
||||
|
||||
return data.win_analogInputsValues.map((value: number, i: number) => ({
|
||||
id: i + 1,
|
||||
value,
|
||||
label: data.win_analogInputsNames[i],
|
||||
unit: data.win_analogInputsUnits[i],
|
||||
offset: parseFloat(data.win_analogInputsOffset[i]),
|
||||
factor: parseFloat(data.win_analogInputsFactor[i]),
|
||||
interval: parseInt(data.win_analogInputsloggerIntervall[i]),
|
||||
weighting: parseInt(data.win_analogInputsWeighting[i]),
|
||||
}));
|
||||
}
|
||||
};
|
||||
@@ -1,55 +0,0 @@
|
||||
// services/fetchAnalogeEingaengeService.ts
|
||||
|
||||
export const fetchAnalogeEingaengeService = async (): Promise<Record<
|
||||
string,
|
||||
any
|
||||
> | null> => {
|
||||
try {
|
||||
if (typeof window === "undefined") return null;
|
||||
|
||||
const isDevelopment = process.env.NEXT_PUBLIC_NODE_ENV === "development";
|
||||
|
||||
const scriptSrc = isDevelopment
|
||||
? "/api/cpl/analogeEingaengeAPIHandler"
|
||||
: "/CPL?/CPL/SERVICE/ae.js";
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const script = document.createElement("script");
|
||||
script.src = scriptSrc;
|
||||
script.async = true;
|
||||
script.onload = () => resolve();
|
||||
script.onerror = () => reject(`❌ Error loading ${scriptSrc}`);
|
||||
document.body.appendChild(script);
|
||||
});
|
||||
|
||||
// Now read the real variables
|
||||
const values = (window as any).win_analogInputsValues || [];
|
||||
const names = (window as any).win_analogInputsNames || [];
|
||||
const units = (window as any).win_analogInputsUnits || [];
|
||||
const factor = (window as any).win_analogInputsFactor || [];
|
||||
const offset = (window as any).win_analogInputsOffset || [];
|
||||
const weighting = (window as any).win_analogInputsWeighting || [];
|
||||
const loggerInterval =
|
||||
(window as any).win_analogInputsloggerIntervall || [];
|
||||
|
||||
const result: Record<string, any> = {};
|
||||
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
result[`analogInput${i + 1}`] = {
|
||||
id: i + 1,
|
||||
value: values[i],
|
||||
name: names[i],
|
||||
unit: units[i],
|
||||
factor: factor[i],
|
||||
offset: offset[i],
|
||||
weighting: weighting[i],
|
||||
loggerInterval: loggerInterval[i],
|
||||
};
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error("❌ Error loading analog inputs:", error);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user