rax-medical/src/stores/remote-ws-store.ts

407 lines
16 KiB
TypeScript
Raw Normal View History

2024-05-30 14:57:25 +08:00
import {defineStore} from "pinia";
import {Session} from "@/utils/storage";
import {ElMessage} from "element-plus";
2024-04-23 09:42:25 +08:00
// 本地调试版本:
2024-05-30 14:57:25 +08:00
const vitalUrl = "ws://" + window.location.host + "/socket.io/admin/rax/vitalSignsMedicine?token=" + Session.getToken()
2024-11-13 13:17:04 +08:00
// const medicineUrl = "ws://" + window.location.host + "/socket.io/admin/rax/addMedicine?token=" + Session.getToken()
2024-05-30 14:57:25 +08:00
const chatUrl = "ws://" + window.location.host + "/socket.io/admin/rax/chatRoom?token=" + Session.getToken()
2024-04-23 09:42:25 +08:00
// 服务器部署版本
// const vitalUrl = "wss://" + window.location.host + "/socket.io/admin/rax/vitalSignsMedicine?token=" + Session.getToken()
// const medicineUrl = "wss://" + window.location.host + "/socket.io/admin/rax/addMedicine?token=" + Session.getToken()
// const chatUrl = "wss://" + window.location.host + "/socket.io/admin/rax/chatRoom?token=" + Session.getToken()
// 测试echars
// const vitalUrl = "wss://" + "110.41.142.124" + "/socket.io/admin/rax/vitalSignsMedicine?token=" + Session.getToken()
// const medicineUrl = "wss://" + "110.41.142.124" + "/socket.io/admin/rax/addMedicine?token=" + Session.getToken()
// const chatUrl = "wss://" + "110.41.142.124" + "/socket.io/admin/rax/chatRoom?token=" + Session.getToken()
2024-04-23 09:42:25 +08:00
export const useRemoteWsStore = defineStore("remoteWs", {
2024-06-05 14:09:09 +08:00
state: () => {
return {
patient: {} as any,
remoteTasks: [] as any,
remoteTasksCap: 10,
currentTaskIndex: -1,
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,
closeStatus: {} as any
2024-05-30 14:57:25 +08:00
}
},
2024-06-05 14:09:09 +08:00
actions: {
setCurrentTaskIndex(i: number) {
this.currentTaskIndex = i
Session.set("currentTaskIndex", i)
},
getCurrentTaskIndex() {
if (Session.get("currentTaskIndex") > -1) {
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() {
let index = 0
for (let i = 0; i < this.remoteTasks.length; i++) {
if (this.remoteTasks[i].isRemote) index = i
}
return index
},
setRemoteLog(log: any, i: number) {
this.remoteTasks[i].log.push(log)
},
getCloseStatus() {
return this.closeStatus
},
/*createConnect(name: string, id: string, date: string, index: number) {
if (!this.patient[name + id + date + index]) {
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,
msgType: "msg"
}))
}
2024-05-29 17:09:56 +08:00
2024-06-05 14:09:09 +08:00
vitalWS.onerror = () => {
ElMessage.error("远程管理" + (index + 1) + "出错")
}
2024-05-29 17:09:56 +08:00
2024-06-05 14:09:09 +08:00
vitalWS.onmessage = (e: any) => {
if (e && e.data) {
const data = JSON.parse(e.data);
if (data.msgType == "heartbeat") {
vitalWS.send(JSON.stringify({msgType: "heartbeat"}))
}
}
}
2024-05-30 20:50:03 +08:00
2024-06-05 14:09:09 +08:00
/!* medicineWS.onopen = function () {
medicineWS.send(JSON.stringify({
patientName: name,
idNum: id,
date: date
}))
}
2024-05-29 17:09:56 +08:00
2024-06-05 14:09:09 +08:00
medicineWS.onclose = () => {
ElMessage.info("远程管理" + (index + 1) + "给药已断开")
}
2024-05-29 17:09:56 +08:00
2024-06-05 14:09:09 +08:00
medicineWS.onerror = () => {
ElMessage.error("远程管理" + (index + 1) + "给药出错")
}*!/
2024-05-29 17:09:56 +08:00
2024-06-05 14:09:09 +08:00
chatWS.onopen = function () {
chatWS.send(JSON.stringify({
patientName: name,
idNum: id,
date: date,
msgType: "msg"
}))
}
2024-05-29 17:09:56 +08:00
2024-06-05 14:09:09 +08:00
chatWS.onerror = () => {
ElMessage.error("远程管理" + (index + 1) + "通讯出错")
}
chatWS.onmessage = (e: any) => {
if (e && e.data) {
const data = JSON.parse(e.data);
if (data.msgType == "heartbeat") {
chatWS.send(JSON.stringify({msgType: "heartbeat"}))
}
}
}
2024-05-29 17:09:56 +08:00
2024-06-05 14:09:09 +08:00
this.patient[name + id + date + index] = {
vitalWS,
// medicineWS,
chatWS
}
2024-05-30 20:50:03 +08:00
}
2024-06-05 14:09:09 +08:00
},*/
disconnect(name: string, id: string, date: string, index: number) {
this.disconnectChat(name, id, date, index)
this.disconnectVital(name, id, date, index)
delete this.patient[name + id + date + index]
},
createVital(name: string, id: string, date: string, index: number) {
const patient = this.patient[name + id + date + index] ? this.patient[name + id + date + index] : this.patient[name + id + date + index] = {}
if (patient && !patient['vitalWS']) {
patient['vitalWS'] = new WebSocket(vitalUrl)
patient['vitalWS'].onopen = function () {
2024-06-05 15:38:41 +08:00
ElMessage.info("远程控制" + (index + 1) + ",生命体征数据连接成功")
2024-06-05 14:09:09 +08:00
patient['vitalWS'].send(JSON.stringify({
patientName: name,
idNum: id,
date: date,
msgType: "msg"
}))
}
2024-05-30 20:50:03 +08:00
2024-06-05 14:09:09 +08:00
patient['vitalWS'].onerror = () => {
2024-06-05 15:38:41 +08:00
ElMessage.error("远程管理" + (index + 1) + "生命体征数据出错")
2024-06-05 14:09:09 +08:00
}
delete this.closeStatus[name + id + date + index + 'vitalWS']
2024-05-30 20:50:03 +08:00
}
2024-06-05 14:09:09 +08:00
},
subscribeVital(name: string, id: string, date: string, index: number, cb: any) {
const patient: any = this.patient[name + id + date + index]
patient.vitalWS.onmessage = (e: any) => {
if (e && e.data) {
const data = JSON.parse(e.data);
if (data.msgType == "msg") {
cb(e)
} else {
patient.vitalWS.send(JSON.stringify({msgType: "heartbeat"}))
}
}
}
},
vitalOnclose(name: string, id: string, date: string, index: number, cb: any) {
const patient: any = this.patient[name + id + date + index]
2024-06-05 15:38:41 +08:00
const status = this.getCloseStatus()
patient.vitalWS.onclose = () => {
if (!status[name + id + date + index + 'vitalWS']) {
delete patient.vitalWS
cb()
}
}
2024-06-05 14:09:09 +08:00
},
unsubscribeVital(name: string, id: string, date: string, index: number) {
const patient: any = this.patient[name + id + date + index]
if (patient && patient.vitalWS) {
patient.vitalWS.onmessage = undefined;
}
},
sendMsg(name: string, id: string, date: string, msg: string, index: number, cb: any) {
const patient: any = this.patient[name + id + date + index]
if (patient) {
const params = {
patientName: name,
idNum: id,
date: date,
2024-11-13 13:17:04 +08:00
msg: msg,
2024-06-05 14:09:09 +08:00
msgType: "msg"
}
patient.chatWS.send(JSON.stringify(params))
cb({
status: 0
})
2024-05-30 20:50:03 +08:00
} else {
2024-06-05 14:09:09 +08:00
cb({
status: 1,
msg: "已断开连接"
})
2024-05-30 20:50:03 +08:00
}
2024-06-05 14:09:09 +08:00
},
2024-11-13 13:17:04 +08:00
sendAudio(name: string, id: string, date: string, audio: any, index: number, cb: any) {
const patient: any = this.patient[name + id + date + index]
if (patient) {
const params = {
patientName: name,
idNum: id,
date: date,
content: audio,
msgType: "audio"
}
patient.chatWS.send(JSON.stringify(params))
cb({
status: 0
})
} else {
cb({
status: 1,
msg: "已断开连接"
})
}
},
2024-06-05 14:09:09 +08:00
disconnectVital(name: string, id: string, date: string, index: number) {
const patient: any = this.patient[name + id + date + index]
if (patient && patient.vitalWS) {
this.closeStatus[name + id + date + index + 'vitalWS'] = true
patient.vitalWS.close()
delete patient['vitalWS']
}
},
createChat(name: string, id: string, date: string, index: number) {
const patient = this.patient[name + id + date + index] ? this.patient[name + id + date + index] : this.patient[name + id + date + index] = {}
if (patient && !patient['chatWS']) {
patient['chatWS'] = new WebSocket(chatUrl)
patient['chatWS'].onopen = function () {
2024-06-05 15:38:41 +08:00
ElMessage.info("远程控制" + (index + 1) + ",通讯连接成功")
2024-06-05 14:09:09 +08:00
patient['chatWS'].send(JSON.stringify({
patientName: name,
idNum: id,
date: date,
msgType: "init"
2024-06-05 14:09:09 +08:00
}))
}
patient['chatWS'].onerror = () => {
2024-06-05 15:38:41 +08:00
ElMessage.error("远程控制" + (index + 1) + "通讯出错")
2024-06-05 14:09:09 +08:00
}
delete this.closeStatus[name + id + date + index + 'chatWS']
}
},
subscribeChat(name: string, id: string, date: string, index: number, cb: any) {
const patient: any = this.patient[name + id + date + index]
patient.chatWS.onmessage = (e: any) => {
if (e && e.data) {
const data = JSON.parse(e.data);
2024-11-13 13:17:04 +08:00
if (data.msgType == "msg" || data.msgType == "audio") {
2024-06-05 14:09:09 +08:00
cb(e)
} else {
patient.chatWS.send(JSON.stringify({msgType: "heartbeat"}))
}
}
}
},
chatOnclose(name: string, id: string, date: string, index: number, cb: any) {
const patient: any = this.patient[name + id + date + index]
2024-06-05 15:38:41 +08:00
patient.chatWS.onclose = () => {
const status = this.getCloseStatus()
if (!status[name + id + date + index + 'chatWS']) {
delete patient.chatWS
cb()
}
}
2024-06-05 14:09:09 +08:00
},
unsubscribeChat(name: string, id: string, date: string, index: number) {
const patient: any = this.patient[name + id + date + index]
patient.chatWS.onmessage = undefined;
},
disconnectChat(name: string, id: string, date: string, index: number) {
const patient: any = this.patient[name + id + date + index]
if (patient && patient.chatWS) {
this.closeStatus[name + id + date + index + 'chatWS'] = true
patient.chatWS.close()
delete patient['chatWS']
}
},
sendMedicine(args: {
name: string,
id: string,
date: string,
flag: string,
medicine: string,
value: string
}, index: number, cb: any) {
const patient: any = this.patient[args.name + args.id + args.date + index]
if (patient) {
const params = {
patientName: args.name,
idNum: args.id,
date: args.date,
flag: args.flag,
medicine: args.medicine,
value: args.value,
msgType: "msg"
}
patient.medicineWS.send(JSON.stringify(params))
cb({
status: 0
})
2024-05-30 20:50:03 +08:00
} else {
2024-06-05 14:09:09 +08:00
cb({
status: 1,
msg: "已断开连接"
})
2024-05-30 20:50:03 +08:00
}
2024-06-05 14:09:09 +08:00
},
subscribeMedicine(name: string, id: string, date: string, index: number, cb: any) {
const patient = this.patient[name + id + date + index]
if (patient) {
patient.medicineCB = cb
patient.medicineWS.onmessage = (e: any) => {
if (e && e.data) {
const data = JSON.parse(e.data);
if (data.msgType == "msg") {
cb(e)
} else {
patient.medicineWS.send(JSON.stringify({msgType: "heartbeat"}))
}
}
}
2024-05-30 20:50:03 +08:00
} else {
2024-06-05 14:09:09 +08:00
cb({
status: 1,
msg: "已断开连接"
})
2024-05-30 20:50:03 +08:00
}
2024-06-05 14:09:09 +08:00
},
medicineOnclose(name: string, id: string, date: string, index: number, cb: any) {
const patient: any = this.patient[name + id + date + index]
patient.medicineWS.onclose = cb
},
unsubscribeMedicine(name: string, id: string, date: string, index: number) {
const patient: any = this.patient[name + id + date + index]
patient.medicineCB = undefined;
patient.medicineWS.onmessage = undefined;
2024-05-30 20:50:03 +08:00
}
2024-05-30 14:57:25 +08:00
}
2024-04-23 09:42:25 +08:00
})