57 lines
3.0 KiB
TypeScript
57 lines
3.0 KiB
TypeScript
const BASE = '/api'
|
|
|
|
async function get<T>(path: string): Promise<T> {
|
|
const res = await fetch(`${BASE}${path}`)
|
|
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
|
return res.json()
|
|
}
|
|
|
|
async function post<T>(path: string, body: unknown): Promise<T> {
|
|
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<T>(path: string, body: unknown): Promise<T> {
|
|
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<void> {
|
|
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<PLC[]>('/config/plcs'),
|
|
getMPs: () => get<MP[]>('/config/measurement-points'),
|
|
getStations: (plcId?: number) => get<Station[]>(`/config/stations${plcId ? `?plcId=${plcId}` : ''}`),
|
|
addPLC: (d: Partial<PLC>) => post<{ Id: number }>('/config/plcs', d),
|
|
updatePLC: (id: number, d: Partial<PLC>) => put(`/config/plcs/${id}`, d),
|
|
delPLC: (id: number) => del(`/config/plcs/${id}`),
|
|
addMP: (d: Partial<MP>) => post<{ Id: number }>('/config/measurement-points', d),
|
|
updateMP: (id: number, d: Partial<MP>) => put(`/config/measurement-points/${id}`, d),
|
|
delMP: (id: number) => del(`/config/measurement-points/${id}`),
|
|
addStation: (d: Partial<Station>) => post<{ Id: number }>('/config/stations', d),
|
|
updateStation: (id: number, d: Partial<Station>) => put(`/config/stations/${id}`, d),
|
|
delStation: (id: number) => del(`/config/stations/${id}`),
|
|
queryProduct: (pid: string) => get<QResult>(`/query/${pid}`),
|
|
queryInterlock: (pid: string, sn: string) => get<IResult>(`/query/${pid}/interlock/${encodeURIComponent(sn)}`),
|
|
getStatus: () => get<Status>('/monitor/status'),
|
|
}
|