import { defineStore } from "pinia"; import { Session } from "@/utils/storage"; const vitalUrl = "ws://localhost:5173/socket.io/admin/rax/vitalSignsMedicine?token=" + Session.getToken() const medicineUrl = "ws://localhost:5173/socket.io/admin/rax/addMedicine?token=" + Session.getToken() const chatUrl = "ws://localhost:5173/socket.io/admin/rax/chatRoom?token=" + Session.getToken() export const useRemoteWsStore = defineStore("remoteWs", { state: () => { return { patient: {} as any, remoteTasks: [] as any, remoteTasksCap: 10, currentTaskIndex: 0, varMedicine: ["丙泊酚", "舒芬太尼", "瑞芬太尼", "顺阿曲库胺"], fixedMedicine: ["尼卡地平", "艾司洛尔", "麻黄素", "阿托品"], exceptionType: ["BIS_except", "DBP_except", "EtCO2_except", "HR_except", "SBP_except", "ST_except"], exceptionMsg: { "BIS_except": "脑电双频指数异常", "DBP_except": "舒张压异常", "EtCO2_except": "呼气末二氧化碳异常", "HR_except": "心率异常", "SBP_except": "收缩压异常", "ST_except": "ST异常" } as any, } }, actions: { setCurrentTaskIndex(i: number) { this.currentTaskIndex = i Session.set("currentTaskIndex", i) }, getCurrentTaskIndex() { if (Session.get("currentTaskIndex")) { this.currentTaskIndex = Session.get("currentTaskIndex") } return this.currentTaskIndex }, setRemoteTask() { Session.set("remoteTasks", this.remoteTasks) }, getRemoteTask() { if (Session.get("remoteTasks")) { this.remoteTasks = Session.get("remoteTasks") } return this.remoteTasks }, initRemoteTask() { if (Session.get("remoteTasks")) { this.remoteTasks = Session.get("remoteTasks") } if (this.remoteTasks.length <= 0) { for (let i = 0; i < 10; i++) { this.remoteTasks.push({ isRemote: false, isException: false, taskName: "", server: "", serverun: "", serverps: "", patient: "", patientId: "", date: "", log: [], index: i }) } } return this.remoteTasks }, resetRemoteTask(i: number) { this.remoteTasks[i] = Object.assign(this.remoteTasks[i], { isRemote: false, isException: false, taskName: "", server: "", serverun: "", serverps: "", patient: "", patientId: "", date: "", log: [], index: i, message: [] }) Session.set("remoteTasks", this.remoteTasks) }, getActiveRemoteTask() { for (let i = 0; i < this.remoteTasks.length; i++) { if (this.remoteTasks[i].isRemote) return i } }, setRemoteLog(log: any, i: number) { this.remoteTasks[i].log.push(log) }, createConnect(name: string, id: string, date: string) { if (!this.patient[name + id + date]) { const vitalWS = new WebSocket(vitalUrl) const medicineWS = new WebSocket(medicineUrl) const chatWS = new WebSocket(chatUrl) vitalWS.onopen = function () { vitalWS.send(JSON.stringify({ patientName: name, idNum: id, date: date })) } medicineWS.onopen = function () { medicineWS.send(JSON.stringify({ patientName: name, idNum: id, date: date })) } chatWS.onopen = function () { chatWS.send(JSON.stringify({ patientName: name, idNum: id, date: date })) } this.patient[name + id + date] = { vitalWS, medicineWS, chatWS } } }, disconnect(name: string, id: string, date: string) { const patient: any = this.patient[name + id + date] if (patient) { patient.vitalWS.close() patient.medicineWS.close() patient.chatWS.close() delete this.patient[name + id + date] } }, subscribeVital(name: string, id: string, date: string, cb: any) { const patient: any = this.patient[name + id + date] if (patient) { patient.vitalWS.onmessage = cb patient.vitalCB = cb } else { this.createConnect(name, id, date) this.patient[name + id + date].vitalWS.onmessage = cb this.patient[name + id + date].vitalCB = cb } }, unsubscribeVital(name: string, id: string, date: string) { const patient: any = this.patient[name + id + date] if (patient && patient.vitalWS) { patient.vitalWS.onmessage = undefined; patient.vitalCB = undefined; } }, sendMsg(name: string, id: string, date: string, msg: string, cb: any) { const patient: any = this.patient[name + id + date] if (patient) { const params = { patientName: name, idNum: id, date: date, msg } patient.chatWS.send(JSON.stringify(params)) cb({ status: 0 }) } else { cb({ status: 1, msg: "已断开连接" }) } }, subscribeChat(name: string, id: string, date: string, cb: any) { const patient: any = this.patient[name + id + date] if (patient) { patient.chatCB = cb patient.chatWS.onmessage = cb } else { cb({ status: 1, msg: "已断开连接" }) } }, unsubscribeChat(name: string, id: string, date: string) { const patient: any = this.patient[name + id + date] patient.chatCB = undefined; patient.chatWS.onmessage = undefined; }, sendMedicine(args: { name: string, id: string, date: string, flag: string, medicine: string, value: string }, cb: any) { const patient: any = this.patient[args.name + args.id + args.date] if (patient) { const params = { patientName: args.name, idNum: args.id, date: args.date, flag: args.flag, medicine: args.medicine, value: args.value } patient.medicineWS.send(JSON.stringify(params)) cb({ status: 0 }) } else { cb({ status: 1, msg: "已断开连接" }) } }, subscribeMedicine(name: string, id: string, date: string, cb: any) { const patient = this.patient[name + id + date] if (patient) { patient.medicineCB = cb patient.medicineWS.onmessage = cb } else { cb({ status: 1, msg: "已断开连接" }) } }, unsubscribeMedicine(name: string, id: string, date: string) { const patient: any = this.patient[name + id + date] patient.medicineCB = undefined; patient.medicineWS.onmessage = undefined; } } })