const BASE = '/api' async function get(path: string): Promise { const res = await fetch(`${BASE}${path}`) if (!res.ok) throw new Error(`HTTP ${res.status}`) return res.json() } async function post(path: string, body: unknown): Promise { const res = await fetch(`${BASE}${path}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }) if (!res.ok) throw new Error(`HTTP ${res.status}`) return res.json() } async function put(path: string, body: unknown): Promise { const res = await fetch(`${BASE}${path}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }) if (!res.ok) throw new Error(`HTTP ${res.status}`) return res.json() } async function del(path: string): Promise { const res = await fetch(`${BASE}${path}`, { method: 'DELETE' }) if (!res.ok) throw new Error(`HTTP ${res.status}`) } export interface PLC { Id: number; PLCName: string; IPAddress: string; DBNumber: number; StationCount: number; SortOrder: number } export interface MP { Id: number; PointName: string; FieldUnit: string; SortOrder: number } export interface Station { Id: number; PLCId: number; StationIndex: number; StationName: string; PointId: number; SortOrder: number; pointName?: string } export interface QPoint { pointId: number; pointName: string; fieldUnit: string; recordTime: string; isQualified: number; inspectionCount: number; upperLimit: number | null; lowerLimit: number | null; measuredValue: number | null } export interface QResult { productId: string; data: QPoint[]; message?: string } export interface IResult { currentStation: string; currentPoint: string; prevStation: string | null; prevResult: number; message?: string } export interface Status { plcCount: number; stationCount: number; pointCount: number; dataTableCount: number; totalRecords: number } export const api = { getPLCs: () => get('/config/plcs'), getMPs: () => get('/config/measurement-points'), getStations: (plcId?: number) => get(`/config/stations${plcId ? `?plcId=${plcId}` : ''}`), addPLC: (d: Partial) => post<{ Id: number }>('/config/plcs', d), updatePLC: (id: number, d: Partial) => put(`/config/plcs/${id}`, d), delPLC: (id: number) => del(`/config/plcs/${id}`), addMP: (d: Partial) => post<{ Id: number }>('/config/measurement-points', d), updateMP: (id: number, d: Partial) => put(`/config/measurement-points/${id}`, d), delMP: (id: number) => del(`/config/measurement-points/${id}`), addStation: (d: Partial) => post<{ Id: number }>('/config/stations', d), updateStation: (id: number, d: Partial) => put(`/config/stations/${id}`, d), delStation: (id: number) => del(`/config/stations/${id}`), queryProduct: (pid: string) => get(`/query/${pid}`), queryInterlock: (pid: string, sn: string) => get(`/query/${pid}/interlock/${encodeURIComponent(sn)}`), getStatus: () => get('/monitor/status'), }