69 lines
1.8 KiB
TypeScript
69 lines
1.8 KiB
TypeScript
// /pages/api/cpl/slotDataAPIHandler.ts
|
|
import { NextApiRequest, NextApiResponse } from "next";
|
|
import path from "path";
|
|
import fs from "fs/promises";
|
|
|
|
// Typ für einzelne Einträge im JSON-Array
|
|
type ChartDataEntry = {
|
|
timestamp?: string;
|
|
zeit?: string;
|
|
time?: string;
|
|
[key: string]: unknown; // zusätzliche Werte erlaubt
|
|
};
|
|
|
|
// Hilfsfunktion: JSON-Datei laden
|
|
async function loadJsonData(filePath: string): Promise<ChartDataEntry[]> {
|
|
const data = await fs.readFile(filePath, "utf8");
|
|
const parsed = JSON.parse(data);
|
|
|
|
if (!Array.isArray(parsed)) {
|
|
throw new Error("Ungültiges Format: Erwartet ein Array");
|
|
}
|
|
|
|
return parsed;
|
|
}
|
|
|
|
export default async function handler(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse
|
|
) {
|
|
const { slot, messart, dia, vonDatum, bisDatum } = req.query;
|
|
|
|
if (!slot || !messart || !dia) {
|
|
return res.status(400).json({ error: "Missing required parameters" });
|
|
}
|
|
|
|
const jsonFilePath = path.join(
|
|
process.cwd(),
|
|
"mocks",
|
|
"device-cgi-simulator",
|
|
"kuesChartData",
|
|
`slot${slot}`,
|
|
`${messart}`,
|
|
`${dia}.json`
|
|
);
|
|
|
|
try {
|
|
const jsonData = await loadJsonData(jsonFilePath);
|
|
|
|
let filteredData = jsonData;
|
|
|
|
if (vonDatum && bisDatum) {
|
|
const von = new Date(`${vonDatum}T00:00:00`);
|
|
const bis = new Date(`${bisDatum}T23:59:59`);
|
|
|
|
filteredData = jsonData.filter((item) => {
|
|
const dateString = item.timestamp ?? item.zeit ?? item.time;
|
|
const itemDate = dateString ? new Date(dateString) : null;
|
|
|
|
return itemDate !== null && itemDate >= von && itemDate <= bis;
|
|
});
|
|
}
|
|
|
|
res.status(200).json(filteredData);
|
|
} catch (error) {
|
|
console.error("Fehler beim Lesen der Slot-Daten:", error);
|
|
res.status(500).json({ error: "Fehler beim Lesen der Slot-Daten" });
|
|
}
|
|
}
|