| | |
| | | import { ref } from "vue"; |
| | | import cache from "../utils/cache"; |
| | | import { EventSourcePolyfill } from "event-source-polyfill"; |
| | | import type { BedsideAuxiliaryScreen } from "./type/bedsideAuxiliaryScreen.type"; |
| | | import { |
| | | defaultDeviceData, |
| | | defaultconsumablesCollection, |
| | | formatDeviceData, |
| | | } from "./type/bedsideAuxiliaryScreen.type"; |
| | | import { ElMessage } from "element-plus/es"; |
| | | |
| | | export const useBedsideAuxiliaryScreenStore = defineStore( |
| | | "bedsideAuxiliaryScreen", |
| | |
| | | /** 设备编号 */ |
| | | const deviceCode = ref<string>(cache.get("devcieCode") || ""); |
| | | |
| | | /** 床旁血压计数据 */ |
| | | const bedsideBloodPressureMonitorData = ref({ |
| | | date_time: '', |
| | | sbp: '', |
| | | pulseRate: '', |
| | | dbp: '', |
| | | zuihouTime: '', |
| | | }); |
| | | /** 设备信息数据 */ |
| | | const deviceData = ref<BedsideAuxiliaryScreen>(defaultDeviceData()); |
| | | |
| | | /** |
| | | * 设置设备编号 |
| | |
| | | const message = ref<string | null>(null); |
| | | const isConnected = ref(false); |
| | | |
| | | // 重连控制 |
| | | let retryCount = 0; |
| | | const maxRetryCount = 60; |
| | | const baseRetryDelay = 1000; // 1秒开始重连延迟 |
| | | |
| | | /** |
| | | * 连接 SSE 服务 |
| | | * @param url SSE 地址 |
| | | */ |
| | | const connect = (url: string) => { |
| | | if (source.value) return; // 已连接,避免重复连接 |
| | | ElMessage.success("正在连接设备,请稍候..."); |
| | | |
| | | source.value = new EventSourcePolyfill(url, { |
| | | heartbeatTimeout: 60000, |
| | |
| | | |
| | | source.value.onopen = () => { |
| | | console.log("[SSE] 连接成功"); |
| | | ElMessage.success("链接服务成功"); |
| | | isConnected.value = true; |
| | | retryCount = 0; // 成功连接后重置重试计数 |
| | | }; |
| | | |
| | | source.value.onerror = (e) => { |
| | | console.warn("[SSE] 错误,等待重连中", e); |
| | | isConnected.value = false; |
| | | close(); // 关闭旧连接,避免残留 |
| | | if (retryCount < maxRetryCount) { |
| | | const delay = baseRetryDelay * Math.pow(2, retryCount); // 指数退避 |
| | | retryCount++; |
| | | console.log(`[SSE] 第${retryCount}次重连,延迟${delay}ms`); |
| | | ElMessage.warning( |
| | | `链接服务失败, 第${retryCount}次重连,请耐心等待重连。。` |
| | | ); |
| | | setTimeout(() => { |
| | | connect(url); |
| | | }, delay); |
| | | } else { |
| | | console.error("[SSE] 重连次数达到上限,停止重连"); |
| | | ElMessage.error("重连次数达到上限,请检查网络或设备状态"); |
| | | } |
| | | }; |
| | | |
| | | source.value.onmessage = (e) => { |
| | |
| | | if (beng !== -1 && end !== -1 && dif !== -1) { |
| | | const datax = msg.slice(beng, end + 1); |
| | | const dataBody = JSON.parse(datax); |
| | | |
| | | console.log("dataBody: ", dataBody); |
| | | // 倒计时提示文本 |
| | | if (dataBody.倒计时?.提示文本) {} |
| | | if (dataBody.倒计时?.提示文本) { |
| | | } |
| | | |
| | | // 床旁血压计 |
| | | if (dataBody.推送类型 === '床旁血压计') {} |
| | | // 中央监控大屏信息 |
| | | else if (dataBody.推送类型 === '中央监控大屏信息') {} |
| | | deviceData.value = formatDeviceData(dataBody); |
| | | } |
| | | }; |
| | | }; |
| | |
| | | console.log("[SSE] 连接已关闭"); |
| | | } |
| | | }; |
| | | |
| | | /** 刷新 SSE 连接 */ |
| | | const refresh = (url: string) => { |
| | | retryCount = 0; |
| | | close(); // 先关闭旧连接 |
| | | connect(url); // 再重新连接 |
| | | }; |
| | | |
| | | return { |
| | | deviceCode, |
| | | deviceData, |
| | | setDeviceCode, |
| | | source, |
| | | message, |
| | | isConnected, |
| | | connect, |
| | | close, |
| | | refresh, |
| | | }; |
| | | } |
| | | ); |