fix: analoge Eingänge Interval in Modal in json
This commit is contained in:
@@ -1,70 +1,67 @@
|
||||
// /pages/api/cpl/getAnalogInputsHandler.ts
|
||||
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const mode = process.env.NEXT_PUBLIC_CPL_MODE;
|
||||
|
||||
if (mode === "json") {
|
||||
const filePath = path.join(
|
||||
process.cwd(),
|
||||
"mocks/api/SERVICE/analogInputsMockData.json"
|
||||
);
|
||||
const content = fs.readFileSync(filePath, "utf-8");
|
||||
const data = JSON.parse(content);
|
||||
return res.status(200).json(data);
|
||||
}
|
||||
|
||||
if (mode === "jsmock") {
|
||||
const filePath = path.join(
|
||||
process.cwd(),
|
||||
"mocks/device-cgi-simulator/SERVICE/analogInputsMockData.js"
|
||||
);
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
|
||||
function extractArray(name) {
|
||||
const match = fileContent.match(
|
||||
new RegExp(`var\\s+${name}\\s*=\\s*\\[([\\s\\S]*?)\\];`)
|
||||
try {
|
||||
if (mode === "json") {
|
||||
const filePath = path.join(
|
||||
process.cwd(),
|
||||
"mocks/api/SERVICE/analogInputsMockData.json"
|
||||
);
|
||||
return match
|
||||
? match[1].split(",").map((s) => s.trim().replace(/^["']|["']$/g, ""))
|
||||
: [];
|
||||
const content = fs.readFileSync(filePath, "utf-8");
|
||||
const data = JSON.parse(content);
|
||||
return res.status(200).json(data);
|
||||
}
|
||||
|
||||
const result = {
|
||||
win_analogInputsValues: extractArray("win_analogInputsValues").map(
|
||||
Number
|
||||
),
|
||||
win_analogInputsNames: extractArray("win_analogInputsNames"),
|
||||
win_analogInputsOffset: extractArray("win_analogInputsOffset").map(
|
||||
Number
|
||||
),
|
||||
win_analogInputsFactor: extractArray("win_analogInputsFactor").map(
|
||||
Number
|
||||
),
|
||||
win_analogInputsloggerIntervall: extractArray(
|
||||
"win_analogInputsloggerIntervall"
|
||||
).map(Number),
|
||||
win_analogInputsUnits: extractArray("win_analogInputsUnits"),
|
||||
win_analogInputsWeighting: extractArray("win_analogInputsWeighting").map(
|
||||
Number
|
||||
),
|
||||
};
|
||||
if (mode === "jsmock") {
|
||||
const filePath = path.join(
|
||||
process.cwd(),
|
||||
"mocks/device-cgi-simulator/SERVICE/analogInputsMockData.js"
|
||||
);
|
||||
const content = fs.readFileSync(filePath, "utf-8");
|
||||
|
||||
// Begrenzung auf maximal 8 Elemente je Array
|
||||
Object.keys(result).forEach((key) => {
|
||||
if (Array.isArray(result[key])) {
|
||||
result[key] = result[key].slice(0, 8);
|
||||
function extractArray(name: string): any[] {
|
||||
const match = content.match(
|
||||
new RegExp(`var\s+${name}\s*=\s*\[([\s\S]*?)\];`)
|
||||
);
|
||||
if (!match) return [];
|
||||
return match[1]
|
||||
.split(",")
|
||||
.map((s) => s.trim().replace(/^["']|["']$/g, ""))
|
||||
.slice(0, 8);
|
||||
}
|
||||
});
|
||||
|
||||
return res.status(200).json(result);
|
||||
const responseData = {
|
||||
win_analogInputsValues: extractArray("win_analogInputsValues").map(
|
||||
Number
|
||||
),
|
||||
win_analogInputsNames: extractArray("win_analogInputsNames"),
|
||||
win_analogInputsOffset: extractArray("win_analogInputsOffset").map(
|
||||
Number
|
||||
),
|
||||
win_analogInputsFactor: extractArray("win_analogInputsFactor").map(
|
||||
Number
|
||||
),
|
||||
win_analogInputsLoggerIntervall: extractArray(
|
||||
"win_analogInputsLoggerIntervall"
|
||||
).map(Number),
|
||||
win_analogInputsUnits: extractArray("win_analogInputsUnits"),
|
||||
win_analogInputsWeighting: extractArray(
|
||||
"win_analogInputsWeighting"
|
||||
).map(Number),
|
||||
};
|
||||
|
||||
return res.status(200).json(responseData);
|
||||
}
|
||||
|
||||
return res.status(400).json({ error: "Unsupported mode" });
|
||||
} catch (error) {
|
||||
console.error("❌ Fehler beim Lesen der analogen Eingänge:", error);
|
||||
return res.status(500).json({ error: "Fehler beim Lesen der Datei" });
|
||||
}
|
||||
|
||||
return res.status(400).json({ error: "Unsupported mode" });
|
||||
}
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
// /pages/api/cpl/updateAnalogInputsSettingsAPIHandler.ts
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import path from "path";
|
||||
import fs from "fs/promises";
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
if (req.method !== "POST") {
|
||||
return res.status(405).json({ error: "Method not allowed" });
|
||||
}
|
||||
|
||||
const filePath = path.join(
|
||||
process.cwd(),
|
||||
"apiMockData",
|
||||
"SERVICE",
|
||||
"analogInputsMockData.js"
|
||||
);
|
||||
|
||||
try {
|
||||
const { updates } = req.body;
|
||||
|
||||
if (!Array.isArray(updates)) {
|
||||
return res.status(400).json({ error: "Ungültige Datenstruktur" });
|
||||
}
|
||||
|
||||
const raw = await fs.readFile(filePath, "utf-8");
|
||||
|
||||
// Teile den Inhalt in:
|
||||
// 1. Vor dem ersten var
|
||||
// 2. Die var-Zuweisungen (Arrays)
|
||||
// 3. Nach dem letzten var (z. B. Block-Kommentare)
|
||||
|
||||
const varRegex = /var\s+(\w+)\s*=\s*\[([\s\S]*?)\];/g;
|
||||
let match;
|
||||
|
||||
const updateMap: Record<string, string[]> = {};
|
||||
const variableLines: string[] = [];
|
||||
let matchStartIndexes: number[] = [];
|
||||
|
||||
while ((match = varRegex.exec(raw)) !== null) {
|
||||
const [fullMatch, key, valuesBlock] = match;
|
||||
const values = valuesBlock
|
||||
.split(",")
|
||||
.map((v) => v.trim())
|
||||
.filter((v) => v.length > 0);
|
||||
|
||||
updateMap[key] = values;
|
||||
variableLines.push(fullMatch);
|
||||
matchStartIndexes.push(match.index);
|
||||
}
|
||||
|
||||
// Kommentare vor und nach den Variablen extrahieren
|
||||
const firstVarIndex = matchStartIndexes[0] ?? 0;
|
||||
const lastVarMatch = variableLines[variableLines.length - 1] ?? "";
|
||||
const lastVarIndex = raw.lastIndexOf(lastVarMatch);
|
||||
const commentsBefore = raw.slice(0, firstVarIndex).trim();
|
||||
const commentsAfter = raw.slice(lastVarIndex + lastVarMatch.length).trim();
|
||||
|
||||
// Updates anwenden
|
||||
for (const { key, index, value } of updates) {
|
||||
if (!updateMap[key]) {
|
||||
console.warn(`⚠️ Schlüssel '${key}' fehlt – wird übersprungen`);
|
||||
continue;
|
||||
}
|
||||
updateMap[key][index] =
|
||||
typeof value === "string" ? `"${value}"` : value.toString();
|
||||
}
|
||||
|
||||
// Arrays immer einzeilig schreiben
|
||||
const variablesBlock = Object.entries(updateMap)
|
||||
.map(([key, values]) => `var ${key} = [${values.join(", ")}];`)
|
||||
.join("\n");
|
||||
|
||||
const finalContent =
|
||||
[commentsBefore, variablesBlock, commentsAfter]
|
||||
.filter(Boolean)
|
||||
.join("\n\n") + "\n";
|
||||
|
||||
await fs.writeFile(filePath, finalContent, "utf-8");
|
||||
|
||||
res.status(200).json({ message: "Mockdaten gespeichert." });
|
||||
} catch (err) {
|
||||
console.error("❌ Fehler beim Schreiben:", err);
|
||||
res.status(500).json({ error: "Interner Serverfehler" });
|
||||
}
|
||||
}
|
||||
65
pages/api/cpl/updateAnalogInputsSettingsHandler.ts
Normal file
65
pages/api/cpl/updateAnalogInputsSettingsHandler.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
// /pages/api/cpl/updateAnalogInputsSettingsHandler.ts
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method !== "POST") {
|
||||
return res.status(405).json({ error: "Method not allowed" });
|
||||
}
|
||||
|
||||
const mode = process.env.NEXT_PUBLIC_CPL_MODE;
|
||||
const updates = req.body.updates;
|
||||
|
||||
if (!Array.isArray(updates)) {
|
||||
return res.status(400).json({ error: "Missing or invalid updates array" });
|
||||
}
|
||||
|
||||
if (mode === "json") {
|
||||
const filePath = path.join(
|
||||
process.cwd(),
|
||||
"mocks/api/SERVICE/analogInputsMockData.json"
|
||||
);
|
||||
const content = fs.readFileSync(filePath, "utf-8");
|
||||
const data = JSON.parse(content);
|
||||
|
||||
for (const update of updates) {
|
||||
const { key, index, value } = update;
|
||||
if (Array.isArray(data[key]) && index < data[key].length) {
|
||||
data[key][index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), "utf-8");
|
||||
return res.status(200).json({ success: true });
|
||||
}
|
||||
|
||||
if (mode === "jsmock") {
|
||||
const filePath = path.join(
|
||||
process.cwd(),
|
||||
"mocks/device-cgi-simulator/SERVICE/analogInputsMockData.js"
|
||||
);
|
||||
let content = fs.readFileSync(filePath, "utf-8");
|
||||
|
||||
for (const update of updates) {
|
||||
const { key, index, value } = update;
|
||||
const regex = new RegExp(`var\\s+${key}\\s*=\\s*\\[([\\s\\S]*?)\\];`);
|
||||
const match = content.match(regex);
|
||||
if (!match) continue;
|
||||
|
||||
const items = match[1].split(",").map((s) => s.trim());
|
||||
if (index >= items.length) continue;
|
||||
|
||||
const isString = typeof value === "string";
|
||||
items[index] = isString ? `"${value}"` : `${value}`;
|
||||
|
||||
const updatedLine = `var ${key} = [${items.join(", ")}];`;
|
||||
content = content.replace(regex, updatedLine);
|
||||
}
|
||||
|
||||
fs.writeFileSync(filePath, content, "utf-8");
|
||||
return res.status(200).json({ success: true });
|
||||
}
|
||||
|
||||
return res.status(400).json({ error: "Unsupported mode" });
|
||||
}
|
||||
Reference in New Issue
Block a user