28 lines
2.3 KiB
TypeScript
28 lines
2.3 KiB
TypeScript
'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<T>(path = '', method = 'GET', body?: unknown): Promise<T> {
|
|
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<StructuresData>(); }
|
|
export async function api_createStructure(input: Pick<OpcStructure, 'anlageId' | 'name' | 'direction' | 'rootNodeId' | 'connectionId'>) {
|
|
const result = await request<OpcStructure>('', '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<string, boolean | number>) {
|
|
try { return await request<StructureWriteResult>('/' + 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<import('@/types/opcua-structures').ArrayImportPreview>('/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;
|
|
}
|