'use server'; import { revalidatePath } from 'next/cache'; import type { OpcStructure, StructuresData, StructureWriteResult } from '@/types/opcua-structures'; const url = 'http://localhost:3000/rk2/opcua-structures'; async function request(path = '', method = 'GET', body?: unknown): Promise { const response = await fetch(url + path, { method, cache: 'no-store', ...(body ? { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) } : {}) }); const result = await response.json(); if (!response.ok) throw new Error(Array.isArray(result.message) ? result.message.join(', ') : result.message ?? 'Strukturanfrage fehlgeschlagen'); return result; } export async function api_structures() { return request(); } export async function api_createStructure(input: Pick) { const result = await request('', 'POST', input); revalidatePath('/sps/opcua-strukturen'); return result; } export async function api_removeStructure(row: OpcStructure) { await request('/' + encodeURIComponent(row.name), 'DELETE', { anlageId: row.anlageId, id: row.id, version: row.version }); revalidatePath('/sps/opcua-strukturen'); } export async function api_writeStructure(row: OpcStructure, values: Record) { try { return await request('/' + encodeURIComponent(row.name) + '/write', 'POST', { anlageId: row.anlageId, id: row.id, version: row.version, values }); } catch (error) { throw new Error((error instanceof Error ? error.message : 'Schreiben fehlgeschlagen') + ' Keine automatische Wiederholung. Bei Verbindungsfehlern ist das Schreibergebnis eventuell unbekannt.'); } } export async function api_previewStructureArray(input: import('@/types/opcua-structures').ArrayImportInput) { return request('/array/preview', 'POST', input); } export async function api_importStructureArray(input: import('@/types/opcua-structures').ArrayImportInput & { token: string }) { const result = await request<{ created: number; skipped: number; invalid: number; failures: { name: string; message: string }[]; notProcessed: number }>('/array/import', 'POST', input); revalidatePath('/sps/opcua-strukturen'); return result; }