diff --git a/.env.dev b/.env.dev index 4d4fdee..ccd00d6 100644 --- a/.env.dev +++ b/.env.dev @@ -2,5 +2,5 @@ FT_NODE_ENV=dev -FT_WS_URL=ws://192.168.1.199:8080/ws +FT_WS_URL=ws://192.168.1.199:9527 FT_PROXY=http://192.168.1.199:8080 \ No newline at end of file diff --git a/package.json b/package.json index 8da533d..663cb18 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "keywords": [], "main": "index.js", "scripts": { + "dev:server": "node server/index.js", "dev": "vite --mode dev", "dev:test": "vite --mode test", "dev:prod": "vite --mode prod", @@ -30,6 +31,7 @@ "axios": "1.8.2", "cssnano": "^7.0.6", "element-plus": "^2.9.5", + "express": "^5.1.0", "konva": "^9.3.18", "lodash": "^4.17.21", "pinia": "^3.0.1", @@ -43,7 +45,8 @@ "postcss-write-svg": "^3.0.1", "tailwindcss": "^4.0.12", "vue": "^3.5.13", - "vue-router": "^4.5.0" + "vue-router": "^4.5.0", + "ws": "^8.18.1" }, "devDependencies": { "@antfu/eslint-config": "^4.3.0", diff --git a/server/index.js b/server/index.js new file mode 100644 index 0000000..4768925 --- /dev/null +++ b/server/index.js @@ -0,0 +1,73 @@ +// const express = require('express') +import express from 'express' +import { WebSocketServer } from 'ws' + +const app = express() + +const PORT = 8080 // 可根据需要更改端口号 +app.listen(PORT, () => { + console.log(`服务器已启动,正在监听端口 ${PORT}`) +}) +app.use(express.json()) + +const levels = ['info', 'warn', 'success', 'error', 'finish'] + +app.post('/api/debug/cmd', (req, res) => { + const { commandId, command, params } = req.body + console.log('收到命令:', command, '参数:', params) + + // 模拟返回数据 + const mockResponse = { + code: '0', + msg: '成功', + data: null, + } + + let messageNum = 0 + + // 异步广播消息 + setTimeout(() => { + res.json(mockResponse) + const poll = setInterval(() => { + if (messageNum === 10) { + clearInterval(poll) + } + broadcast({ + type: 'notification', + data: { + commandId, + command, + level: levels[Math.floor(Math.random() * levels.length)], + title: `步骤${messageNum}执行完成`, + content: `具体信息${messageNum}`, + dateTime: new Date().toLocaleString(), + }, + }) + messageNum++ + }, Math.floor(Math.random() * (1000 - 500 + 1)) + 500) + }, 1000) +}) + +const wss = new WebSocketServer({ port: 9527 }) + +const clients = new Set() +wss.on('connection', (ws) => { + clients.add(ws) + ws.on('close', () => { + console.log('客户端断开') + }) +}) + +function broadcast(message) { + try { + const jsonMessage = JSON.stringify(message) + clients.forEach((client) => { + if (client.readyState === WebSocket.OPEN) { + client.send(jsonMessage) + } + }) + } + catch (e) { + console.log('广播发送失败:', e, message) + } +} diff --git a/src/apis/system.ts b/src/apis/system.ts new file mode 100644 index 0000000..f651a64 --- /dev/null +++ b/src/apis/system.ts @@ -0,0 +1,9 @@ +import http from 'libs/http' + +export interface Params { + commandId: string + command: string + params: any +} +export const debugControl = (params: Params): Promise => http.post('/debug/cmd', params) +export const control = (params: Params): Promise => http.post('/cmd', params) diff --git a/src/assets/styles/element.scss b/src/assets/styles/element.scss index 884d370..6fe14c6 100644 --- a/src/assets/styles/element.scss +++ b/src/assets/styles/element.scss @@ -2,8 +2,6 @@ --el-font-size-base: 14px; --el-button-size: 30px; - - --el-color-primary: #1989FA; //--el-button-active-bg-color: linear-gradient(90deg, #0657C0 24%, #096AE0 101%); //--text-color-primary: #17213c; diff --git a/src/components/common/FTButton/index.vue b/src/components/common/FTButton/index.vue index 3578a76..bba3182 100644 --- a/src/components/common/FTButton/index.vue +++ b/src/components/common/FTButton/index.vue @@ -78,7 +78,7 @@ defineExpose({ z-index: 100; } .my-button { - height: var(--el-button-size); + height: 30px; padding: 5px 20px; border-radius: 5px; display: flex; diff --git a/src/components/common/FTStream/index.vue b/src/components/common/FTStream/index.vue index 40ffb65..dff1d0a 100644 --- a/src/components/common/FTStream/index.vue +++ b/src/components/common/FTStream/index.vue @@ -1,6 +1,6 @@ @@ -20,6 +23,7 @@ const router = useRouter() + diff --git a/src/libs/http.ts b/src/libs/http.ts index 15b740c..33a8e62 100644 --- a/src/libs/http.ts +++ b/src/libs/http.ts @@ -26,7 +26,7 @@ http.interceptors.response.use( (response) => { if ( response.status === 200 - && response.data.code !== '00000' + && response.data.code !== '0' ) { // 返回错误拦截 FtMessage.error(response.data.msg) diff --git a/src/libs/utils.ts b/src/libs/utils.ts index fdac27c..9cf535b 100644 --- a/src/libs/utils.ts +++ b/src/libs/utils.ts @@ -1,33 +1,55 @@ +import { control, debugControl } from 'apis/system' import { FtMessage } from 'libs/message' -import { socket } from 'libs/socket' -import { useSystemStore } from 'stores/useSystemStore' +import { useSystemStore } from 'stores/systemStore' -export const sendControl = async (params: any) => { - if (!params.cmdId) { - params.cmdId = Date.now().toString() +export const sendControl = async (params: any, type: 'debug' | 'control' = 'control') => { + if (!params.commandId) { + params.commandId = Date.now().toString() } const systemStore = useSystemStore() systemStore.systemList = [] - const cmdName = cmdNameMap[params.cmdCode as keyof typeof cmdNameMap] || params.cmdCode + const cmdName = cmdNameMap[params.command as keyof typeof cmdNameMap] || params.command - socket.init((data: any) => { - if (data.cmdId === params.cmdId) { - systemStore.pushSystemList(data) - } - }, 'cmd_debug') - socket.init((data: any) => { - if (data.cmdId === params.cmdId) { - systemStore.pushSystemList(data) - } - }, 'cmd_response') - // TODO 接口调用 - // await (type === 'debug' ? debugControl(params) : control(params)) + await (type === 'debug' ? debugControl(params) : control(params)) systemStore.updateStreamVisible(true) FtMessage.success(`[${cmdName}]已发送`) } export const cmdNameMap = { + door_open: '开门', + door_close: '关门', + door_stop: '停止开关门', + liquid_arm_rotation: '加液机械臂启动', + liquid_arm_reset: '加液机械臂复位', + liquid_arm_stop: '加液机械臂停止', + liquid_pump_start: '启动加液泵', + liquid_pump_stop: '停止加液泵', + liquid_pump_pre_filling: '预充加液头', + liquid_pump_pre_evacuation: '排空加液头', + shaker_start: '摇匀', + shaker_stop: '停止摇匀', + pallet_elevator_lift_up: '托盘上升', + pallet_elevator_lift_down: '托盘下降', + pallet_elevator_stop: '托盘停止', + heater_start: '开始加热', + heater_stop: '停止加热', + debug_heater_start_heat_maintaining: '启动恒温', + debug_heater_stop_heat_maintaining: '停止恒温', + debug_cold_trap_start_refrigeration: '启动制冷', + debug_cold_trap_stop_refrigeration: '停止制冷', + debug_fan_start: '打开风扇', + debug_fan_stop: '关闭风扇', + debug_transportation_arm_move: '龙门架机械臂移动', + debug_transportation_arm_stop: '龙门架机械臂停止移动', + debug_transportation_arm_reset: '龙门架机械臂复位', + debug_holding_jaw_open: '打开夹爪', + debug_holding_jaw_pause: '暂停夹爪', + debug_holding_jaw_close: '闭合夹爪', + debug_cover_elevator_lift_up: '拍子抬升', + debug_cover_elevator_stop: '拍子停止', + debug_cover_elevator_reset: '拍子复位', + debug_cover_elevator_lift_down: '拍子下降', } diff --git a/src/stores/debugStore.ts b/src/stores/debugStore.ts new file mode 100644 index 0000000..cf27124 --- /dev/null +++ b/src/stores/debugStore.ts @@ -0,0 +1,64 @@ +import { defineStore } from 'pinia' + +export const useDebugStore = defineStore('debug', { + state: () => ({ + formData: { + // 加液机械臂 + liquidArmData: { + largeArmAngle: 10, + smallArmAngle: 20, + largeArmRotationRate: 10, + smallArmRotationRate: 10, + }, + // 加液泵 + liquidPumpData: { + index: 1, + rate: 3, + }, + // 摇匀速度 + shakeSpeed: { + rate: 10, + }, + // 加热区 + heatArea: { + index: 1, + heatMotorData: { + distance: 1, + rate: 10, + }, + heatTemperature: { + temperature: 20, + }, + }, + // 转运模组 + transferModule: { + // X轴 + xMotorData: { + xDimDistance: 1, + xDimRate: 10, + }, + // y轴 + yMotorData: { + yDimDistance: 1, + yDimRate: 10, + }, + // z轴 + zMotorData: { + zDimDistance: 1, + zDimRate: 10, + }, + // 夹爪 + JawData: { + rate: 10, + }, + }, + // 拍子模组 + lidData: { + rate: 10, + distance: 19, + }, + }, + }), + actions: { + }, +}) diff --git a/src/stores/useSystemStore.ts b/src/stores/systemStore.ts similarity index 92% rename from src/stores/useSystemStore.ts rename to src/stores/systemStore.ts index e87ff03..00460d9 100644 --- a/src/stores/useSystemStore.ts +++ b/src/stores/systemStore.ts @@ -9,7 +9,7 @@ export const useSystemStore = defineStore('system', { }, isDebug: import.meta.env.FT_NODE_ENV === 'dev', streamVisible: false, - systemList: [{ cmdCode: '' }], + systemList: [], }), actions: { updateStreamVisible(bool: boolean) { diff --git a/src/views/debug/index.vue b/src/views/debug/index.vue index b05790e..e477a39 100644 --- a/src/views/debug/index.vue +++ b/src/views/debug/index.vue @@ -1,3 +1,788 @@ + + + +