diff --git a/.env b/.env index 3bb47b5..8c01c00 100644 --- a/.env +++ b/.env @@ -1,3 +1,3 @@ -VITE_API_HOST=window.location.hostname -VITE_API_PORT=80 -VITE_WS_PATH=/api/v1/app/ws/state \ No newline at end of file +VITE_API_HOST=192.168.1.199 +VITE_API_PORT=8090 +VITE_WS_PATH=/ws \ No newline at end of file diff --git a/src/services/globalCmd/cmdTypes.ts b/src/services/globalCmd/cmdTypes.ts new file mode 100644 index 0000000..b92167d --- /dev/null +++ b/src/services/globalCmd/cmdTypes.ts @@ -0,0 +1,29 @@ +export type OperationCmd = + | "moveMotorToPosition" // 移动电机 + +export type ControlType = { + valveType: string, + isOpen: Boolean +} + +export type SyringeType = { + rotationSpeed: string | number, + direction: string | number, + time: string | number, +} +type PositionType = { + x:string | number + y:string | number +} +type PositionListType = [PositionType] +export type workType = { + direction: string + space: string | number + nitrogenFlowVelocity : string | number + nitrogenAirPressure : string | number + matrixFlowVelocity : string | number + voltage : string | number + height : string | number + movementSpeed : string | number + position: PositionListType +} \ No newline at end of file diff --git a/src/services/globalCmd/globalCmd.ts b/src/services/globalCmd/globalCmd.ts new file mode 100644 index 0000000..6cac6a0 --- /dev/null +++ b/src/services/globalCmd/globalCmd.ts @@ -0,0 +1,48 @@ +import httpRequest, { type BaseResponse } from "../httpRequest"; +import { addTxnRecord } from "../txn"; +import type { OperationCmd, ControlType, SyringeType, workType } from "./cmdTypes"; + +//移动电机 +export function moveMotorToPosition(params: { commandName: string; params: Record }) { + const commandId = addTxnRecord({ ...params, category: "debug" }); + return httpRequest>({ url: "api/cmd/moveMotorToPosition", params: { ...params, commandId }, method: "POST" }); +} + +//切换清洗管路 +export function switchThreeWayValve(params: { type: string }) { + const commandId = addTxnRecord({ ...params, category: "debug" }); + return httpRequest>({ url: "/cmd/switchThreeWayValve", params: { ...params, commandId }, method: "POST" }); +} + +//氮气三路 +export function controlValve(params:ControlType) { + const commandId = addTxnRecord({ ...params, category: "debug" }); + return httpRequest>({ url: "/cmd/controlValve", params: { ...params, commandId }, method: "POST" }); +} + +//电压控制 开启 +export function turnOnHighVoltage(params:{voltage: string | number}) { + const commandId = addTxnRecord({ ...params, category: "debug" }); + return httpRequest>({ url: "/cmd/turnOnHighVoltage", params: { ...params, commandId }, method: "POST" }); +} + +//电压控制 关闭 +export function turnOffHighVoltage() { + return httpRequest>({ url: "/cmd/turnOffHighVoltage", params: {}, method: "POST" }); +} + +//注射泵开启 +export function turnOnSyringePump(params:SyringeType) { + return httpRequest>({ url: "/cmd/turnOnSyringePump", params: {...params}, method: "POST" }); +} + +//注射泵关闭 +export function turnOffSyringePump() { + return httpRequest>({ url: "/cmd/turnOffSyringePump", params: {}, method: "POST" }); +} + +//开始喷涂 +export function startWork(params:workType) { + return httpRequest>({ url: "/cmd/startWork", params, method: "POST" }); +} + diff --git a/src/services/httpRequest.ts b/src/services/httpRequest.ts new file mode 100644 index 0000000..46ed9f7 --- /dev/null +++ b/src/services/httpRequest.ts @@ -0,0 +1,64 @@ +import { Subject } from "rxjs"; + +export interface BaseResponse { + success: boolean; + code: string; + msg: string; + data: T; +} + +type HttpReqParam = { + url: string; + method?: "GET" | "POST" | "PATCH" | "PUT" | "DELETE"; + params?: Record; + encode?: "form" | "json"; // 入参编码类型 + headers?: Record; +}; + +export type ApiException = "invalidToken" | "serverError"; + +const exceptionSub = new Subject(); +export const exceptionOb = exceptionSub.asObservable(); + +function extHandle(res: BaseResponse) { + if (res.code === "A0230") { + // 访问令牌无效或已过期 + exceptionSub.next("invalidToken"); + } + return { + ...res, + success: res.code === "00000", + }; +} + +export default async function httpRequest({ url, method = "GET", params = {}, encode = "json", headers = {} }: HttpReqParam) { + const token = sessionStorage.getItem("token"); + if (token) { + headers = { Authorization: token, ...headers }; + } + if (method === "GET") { + const query = urlEncode(params); + const _url = query ? url + "?" + query : url; + const res = await fetch(_url, { headers }); + return res.json().then(res => extHandle(res) as T); + } else { + const body = encode === "json" ? JSON.stringify(params) : urlEncode(params); + const _headers = + encode === "json" + ? { "Content-Type": "application/json; charset=utf-8", ...headers } + : { "Content-Type": "application/x-www-form-urlencoded; charset=utf-8", ...headers }; + const res = await fetch(url, { method, headers: _headers, body }); + return res.json().then(res => extHandle(res) as T); + } +} +export function urlEncode(params?: Record) { + let query = ""; + if (params && Object.keys(params).length > 0) { + const qs = []; + for (let attr in params) { + qs.push(`${attr}=${encodeURIComponent(params[attr])}`); + } + query = qs.join("&"); + } + return query; +} diff --git a/src/services/txn.ts b/src/services/txn.ts new file mode 100644 index 0000000..1175ea8 --- /dev/null +++ b/src/services/txn.ts @@ -0,0 +1,43 @@ +import type { OperationCmd } from "./globalCmd/cmdTypes"; + +let _lastTimestamp = 0; +export function generateTxnNo() { + const txnNo = Date.now(); + // 确保前后两条指令的txn 不一样 + if (txnNo !== _lastTimestamp) { + if (txnNo < _lastTimestamp) { + _lastTimestamp++; + } else { + _lastTimestamp = txnNo; + } + } else { + _lastTimestamp = txnNo + 1; + } + return _lastTimestamp; +} + +type DebugCmdRecord = { + category: "debug"; + commandName: string; + params: Record; +}; + +type TxnRecord = Partial; + +const txnCmdMap: Record = {}; + +export function addTxnRecord(val: TxnRecord) { + const txn = generateTxnNo().toString(); + txnCmdMap[txn] = val; + return txn; +} + +export function getTxnRecord(txn: string, category: TxnRecord["category"]) { + const record = txnCmdMap[txn]; + // 只有属于指定category时,才返回,且返回后删除记录,节约内存 + if (record && record.category === category) { + delete txnCmdMap[txn]; + return record; + } + return undefined; +} diff --git a/src/views/debug/index.vue b/src/views/debug/index.vue index d11bfb7..80fee96 100644 --- a/src/views/debug/index.vue +++ b/src/views/debug/index.vue @@ -1,141 +1,234 @@