From b15fdf60c6454d9382684a726c0bd66ba2d1b108 Mon Sep 17 00:00:00 2001 From: zhangjiming Date: Mon, 3 Mar 2025 14:51:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=8Aws=E4=B8=8A=E6=8A=A5=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=88=86=E7=A6=BB=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/globalCmd/cmdTypes.ts | 78 --------------------------- src/services/socket.ts | 33 +++++++----- src/services/wsTypes.ts | 106 +++++++++++++++++++++++++++++++++++++ src/stores/equipmentStatus.ts | 2 +- 4 files changed, 128 insertions(+), 91 deletions(-) create mode 100644 src/services/wsTypes.ts diff --git a/src/services/globalCmd/cmdTypes.ts b/src/services/globalCmd/cmdTypes.ts index 0a96f1b..a8fcd13 100644 --- a/src/services/globalCmd/cmdTypes.ts +++ b/src/services/globalCmd/cmdTypes.ts @@ -78,81 +78,3 @@ export type ControlValueType = { isOpen: boolean; }; }; - -export type EquipmentStatusType = { - emergencyStop: boolean; //急停状态 - pause: boolean; - - //X轴电机状态 - xAxisPosition: number; //电机位置 - xAxisSpeed: number; //电机速度 - xAxisMovementEnded: boolean; - xAxisAtOrigin: boolean; - xAxisLimited: boolean; - - //Y轴电机状态 - yAxisPosition: number; - yAxisSpeed: number; - yAxisMovementEnded: boolean; - yAxisAtOrigin: boolean; - yAxisLimited: boolean; - - //Z轴电机状态 - zAxisPosition: number; - zAxisSpeed: number; - zAxisMovementEnded: boolean; - zAxisAtOrigin: boolean; - zAxisLimited: boolean; - - //三通阀状态 - threeWayValvePosition: "Dehumidification" | "Cleaning" | "Nozzle"; - - // 流量计状态 - flowRate: number; - - // 温湿度传感器状态 - temperature: number; - humidity: number; - - //注射泵状态 - syringePumpNormal: boolean; -}; - -export const defaultStatus: EquipmentStatusType = { - emergencyStop: false, //急停状态 - pause: false, - - //X轴电机状态 - xAxisPosition: 0, //电机位置 - xAxisSpeed: 0, //电机速度 - xAxisMovementEnded: false, - xAxisAtOrigin: false, - xAxisLimited: false, - - //Y轴电机状态 - yAxisPosition: 0, - yAxisSpeed: 0, - yAxisMovementEnded: false, - yAxisAtOrigin: false, - yAxisLimited: false, - - //Z轴电机状态 - zAxisPosition: 0, - zAxisSpeed: 0, - zAxisMovementEnded: false, - zAxisAtOrigin: false, - zAxisLimited: false, - - //三通阀状态 - threeWayValvePosition: "Nozzle", - - // 流量计状态 - flowRate: 0, - - // 温湿度传感器状态 - temperature: 0, - humidity: 20, - - //注射泵状态 - syringePumpNormal: false, -}; diff --git a/src/services/socket.ts b/src/services/socket.ts index 1826ffc..3812a2d 100644 --- a/src/services/socket.ts +++ b/src/services/socket.ts @@ -1,6 +1,7 @@ import { Subject } from "rxjs"; +import type { Datagram } from "./wsTypes"; -export type SocketState = 'open' | 'close' | 'error' +export type SocketState = "open" | "close" | "error"; class WebSocketClient { private ws: WebSocket | null = null; @@ -9,9 +10,14 @@ class WebSocketClient { private maxReconnectAttempts: number = 5; private reconnectInterval: number = 3000; - readonly dataOb = new Subject() - readonly stateOb = new Subject() - + private dataSub = new Subject(); + get dataOb() { + return this.dataSub.asObservable(); + } + private stateSub = new Subject(); + get stateOb() { + return this.stateSub.asObservable(); + } constructor(url: string) { this.url = url; } @@ -41,28 +47,32 @@ class WebSocketClient { this.ws.onopen = () => { console.log("WebSocket 连接已建立"); this.reconnectAttempts = -1; // 重置重连次数 - this.stateOb.next('open') + this.stateSub.next("open"); }; // 接收消息的处理 this.ws.onmessage = (event: MessageEvent) => { try { - const data = JSON.parse(event.data); - // console.log('🚀 ~ WebSocketClient ~ bindEvents ~ data:', data) - this.dataOb.next(data) + const data = JSON.parse(event.data) as Datagram; + // console.log("🚀 ~ WebSocketClient ~ bindEvents ~ data:", data); + if (data.type === "cmd") { + this.dataSub.next({ type: data.type, data: { ...data.data, success: data.data.status === "D0000" } }); + } else { + this.dataSub.next(data); + } } catch (error) { console.error("消息解析错误:", error); } }; this.ws.onclose = () => { - this.stateOb.next('close') + this.stateSub.next("close"); console.log("WebSocket 连接已关闭"); this.reconnect(); }; this.ws.onerror = error => { - this.stateOb.next('error') + this.stateSub.next("error"); console.error("WebSocket 错误:", error); }; } @@ -107,7 +117,6 @@ export const createWebSocket = (url: string): WebSocketClient => { } }; - export const sharedWsUrl = `ws://${import.meta.env.VITE_API_HOST}:${import.meta.env.VITE_API_PORT}${ import.meta.env.VITE_WS_PATH -}`; \ No newline at end of file +}`; diff --git a/src/services/wsTypes.ts b/src/services/wsTypes.ts new file mode 100644 index 0000000..3d9e422 --- /dev/null +++ b/src/services/wsTypes.ts @@ -0,0 +1,106 @@ + +export type CmdDatagram = { + type: "cmd"; // 指令 + data: { + commandId: string; + commandName: string; + status: "D0000" | "D1111"; + message: string; + success: boolean; + }; +}; + +type WarnType = "wash_complete" | "prefill_complete" | "dehumidify_complete" | "spray_complete" | "error"; + +export type WarnDatagram = { + type: "warn"; // 报警 + data: { + code: WarnType; + msg: string; + }; +}; + +export type StatusDatagram = { + type: "status"; // 状态 + data: EquipmentStatusType; +}; + +export type EquipmentStatusType = { + emergencyStop: boolean; //急停状态 + pause: boolean; + + //X轴电机状态 + xAxisPosition: number; //电机位置 + xAxisSpeed: number; //电机速度 + xAxisMovementEnded: boolean; + xAxisAtOrigin: boolean; + xAxisLimited: boolean; + + //Y轴电机状态 + yAxisPosition: number; + yAxisSpeed: number; + yAxisMovementEnded: boolean; + yAxisAtOrigin: boolean; + yAxisLimited: boolean; + + //Z轴电机状态 + zAxisPosition: number; + zAxisSpeed: number; + zAxisMovementEnded: boolean; + zAxisAtOrigin: boolean; + zAxisLimited: boolean; + + //三通阀状态 + threeWayValvePosition: "Dehumidification" | "Cleaning" | "Nozzle"; + + // 流量计状态 + flowRate: number; + + // 温湿度传感器状态 + temperature: number; + humidity: number; + + //注射泵状态 + syringePumpNormal: boolean; +}; + +export const defaultStatus: EquipmentStatusType = { + emergencyStop: false, //急停状态 + pause: false, + + //X轴电机状态 + xAxisPosition: 0, //电机位置 + xAxisSpeed: 0, //电机速度 + xAxisMovementEnded: false, + xAxisAtOrigin: false, + xAxisLimited: false, + + //Y轴电机状态 + yAxisPosition: 0, + yAxisSpeed: 0, + yAxisMovementEnded: false, + yAxisAtOrigin: false, + yAxisLimited: false, + + //Z轴电机状态 + zAxisPosition: 0, + zAxisSpeed: 0, + zAxisMovementEnded: false, + zAxisAtOrigin: false, + zAxisLimited: false, + + //三通阀状态 + threeWayValvePosition: "Nozzle", + + // 流量计状态 + flowRate: 0, + + // 温湿度传感器状态 + temperature: 0, + humidity: 20, + + //注射泵状态 + syringePumpNormal: false, +}; + +export type Datagram = CmdDatagram | WarnDatagram | StatusDatagram ; \ No newline at end of file diff --git a/src/stores/equipmentStatus.ts b/src/stores/equipmentStatus.ts index 1d5cfa0..776db29 100644 --- a/src/stores/equipmentStatus.ts +++ b/src/stores/equipmentStatus.ts @@ -1,7 +1,7 @@ import { defineStore } from "pinia"; import { ref } from "vue"; import * as R from "ramda"; -import { defaultStatus, type EquipmentStatusType } from "../services/globalCmd/cmdTypes"; +import { defaultStatus, type EquipmentStatusType } from "@/services/wsTypes"; export const useEquipmentStatusStore = defineStore("equipmentStatus", () => { const equipmentStatus = ref(defaultStatus);