94 lines
3.8 KiB
JavaScript
94 lines
3.8 KiB
JavaScript
async function jsonOrThrow(response, fallbackMessage) {
|
|
if (response.ok) {
|
|
if (response.status === 204) return null;
|
|
return response.json();
|
|
}
|
|
let detail = "";
|
|
try {
|
|
const body = await response.json();
|
|
detail = body?.message || body?.err_msg || JSON.stringify(body);
|
|
} catch {
|
|
detail = await response.text();
|
|
}
|
|
throw new Error(`${fallbackMessage}: ${response.status} ${detail || response.statusText}`);
|
|
}
|
|
|
|
async function get(path, label) {
|
|
const response = await fetch(path);
|
|
return jsonOrThrow(response, label);
|
|
}
|
|
|
|
async function postJson(path, body, label) {
|
|
const response = await fetch(path, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: body === undefined ? undefined : JSON.stringify(body),
|
|
});
|
|
return jsonOrThrow(response, label);
|
|
}
|
|
|
|
async function putJson(path, body, label) {
|
|
const response = await fetch(path, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
});
|
|
return jsonOrThrow(response, label);
|
|
}
|
|
|
|
async function del(path, label) {
|
|
const response = await fetch(path, { method: "DELETE" });
|
|
return jsonOrThrow(response, label);
|
|
}
|
|
|
|
export const runtimeApi = {
|
|
fetchOverview: () => get("/api/runtime/overview", "加载段运行态失败"),
|
|
};
|
|
|
|
export const segmentControl = {
|
|
startAuto: (id) => postJson(`/api/control/segment/${id}/start-auto`, undefined, "启动自动控制失败"),
|
|
stopAuto: (id) => postJson(`/api/control/segment/${id}/stop-auto`, undefined, "停止自动控制失败"),
|
|
ackFault: (id) => postJson(`/api/control/segment/${id}/ack-fault`, undefined, "故障确认失败"),
|
|
reset: (id) => postJson(`/api/control/segment/${id}/reset`, undefined, "复位失败"),
|
|
batchStart: () => postJson(`/api/control/segment/batch-start-auto`, undefined, "批量启动失败"),
|
|
batchStop: () => postJson(`/api/control/segment/batch-stop-auto`, undefined, "批量停止失败"),
|
|
};
|
|
|
|
export const stationApi = {
|
|
list: (lineCode) => {
|
|
const q = lineCode ? `?line_code=${encodeURIComponent(lineCode)}` : "";
|
|
return get(`/api/station${q}`, "加载工位失败");
|
|
},
|
|
detail: (id) => get(`/api/station/${id}`, "加载工位详情失败"),
|
|
create: (payload) => postJson("/api/station", payload, "新增工位失败"),
|
|
update: (id, payload) => putJson(`/api/station/${id}`, payload, "更新工位失败"),
|
|
remove: (id) => del(`/api/station/${id}`, "删除工位失败"),
|
|
upsertSignal: (id, payload) =>
|
|
postJson(`/api/station/${id}/signal`, payload, "绑定工位信号失败"),
|
|
deleteSignal: (id, role) =>
|
|
del(`/api/station/${id}/signal/${encodeURIComponent(role)}`, "解除工位信号绑定失败"),
|
|
};
|
|
|
|
export const segmentApi = {
|
|
list: (lineCode) => {
|
|
const q = lineCode ? `?line_code=${encodeURIComponent(lineCode)}` : "";
|
|
return get(`/api/segment${q}`, "加载段配置失败");
|
|
},
|
|
detail: (id) => get(`/api/segment/${id}/detail`, "加载段详情失败"),
|
|
create: (payload) => postJson("/api/segment", payload, "新增段失败"),
|
|
update: (id, payload) => putJson(`/api/segment/${id}`, payload, "更新段失败"),
|
|
remove: (id) => del(`/api/segment/${id}`, "删除段失败"),
|
|
createStep: (id, payload) =>
|
|
postJson(`/api/segment/${id}/step`, payload, "新增步骤失败"),
|
|
updateStep: (id, stepNo, payload) =>
|
|
putJson(`/api/segment/${id}/step/${stepNo}`, payload, "更新步骤失败"),
|
|
deleteStep: (id, stepNo) =>
|
|
del(`/api/segment/${id}/step/${stepNo}`, "删除步骤失败"),
|
|
createInterlock: (id, payload) =>
|
|
postJson(`/api/segment/${id}/interlock`, payload, "新增联锁失败"),
|
|
deleteInterlock: (id, interlockId) =>
|
|
del(`/api/segment/${id}/interlock/${interlockId}`, "删除联锁失败"),
|
|
replaceResources: (id, keys) =>
|
|
putJson(`/api/segment/${id}/resource`, { resource_keys: keys }, "更新资源声明失败"),
|
|
};
|