gx
chenyc
2025-04-19 b30f6233ce835966040d3527b64f12806ce1943a
src/samples/sockteStomp.ts
@@ -1,55 +1,71 @@
import Stomp from 'stompjs'
var stompClient: Stomp.Client | null=null
import os from 'os'
let stompClient: Stomp.Client | null=null
import { userInfoStore } from '@/stores/userInfo'
import { confingInfoStore } from '@/stores/StoresConfing'
import { patientInfoStore } from '@/stores/patient'
import {sockteStore} from '@/stores/sockteInfo'
let sockteNum=0
interface device{
    deviceName:string
    deviceType:string
}
let config={}
let devices:Array<device>=[]
let clientCode:string=''
/**
 * 保存结果到vuex
 * @param resultInfo 结果写入
 */
const writeResult=(resultInfo:any)=>{
    if(devices.length>0){
        const deviceInfo=devices.find(de=>{de.deviceName===resultInfo.deviceName})
        const deviceInfo=devices.find(de=>{
            return de.deviceName===resultInfo.deviceNumber
        })
        console.log(deviceInfo,'更具设备number查找到的设备')
        console.log(resultInfo)
        if(deviceInfo!==undefined){
            switch(deviceInfo.deviceType){
                case '读卡器':
                    sockteStore().setdkqSockte(
                        {
                            deviceName:resultInfo.deviceName,
                            type:"读卡器",
                            result:resultInfo.result,
                            resultTime:resultInfo.resultTime,
                            state:true
                        }
                    )
                    console.log('读卡器收到消息')
                    const res=JSON.parse(resultInfo.result)
                    if(res.clientCode!==null){
                        sockteStore().setdkqSockte(
                            {
                                deviceName:resultInfo.deviceNumber,
                                type:"读卡器",
                                result:resultInfo.result,
                                resultTime:resultInfo.resultTime,
                                state:2
                            }
                        )
                    }
                    break
                case "体重秤":
                    console.log(resultInfo,'体重秤')
                    sockteStore().setweightSockte(
                        {
                            deviceName:resultInfo.deviceName,
                            deviceName:resultInfo.deviceNumber,
                            type:"体重秤",
                            result:resultInfo.result,
                            result:Number(resultInfo.result).toString(),
                            resultTime:resultInfo.resultTime,
                            state:true
                            state:2
                        }
                    )
                    break
                case "血压计":
                    console.log(resultInfo,'血压计')
                    sockteStore().setxyjSockte(
                        {
                            deviceName:resultInfo.deviceName,
                            deviceName:resultInfo.deviceNumber,
                            type:"血压计",
                            result:resultInfo.result,
                            resultTime:resultInfo.resultTime,
                            state:true
                            state:2
                        }
                    )
                    break
@@ -60,62 +76,129 @@
    }
}
const writePatient=(resultInfo:any)=>{
    // 给一个时间变化
    const da=new Date()
    console.log(da)
    const info={
        id:resultInfo.patientInfo===null?0:resultInfo.patientInfo.id,
        code:resultInfo.patientInfo===null?'':resultInfo.patientInfo.code,
        name:resultInfo.patientInfo===null?'':resultInfo.patientInfo.patientName,
        patientAvatarIcon:resultInfo.patientInfo===null?'':resultInfo.patientInfo.patientAvatarIcon,
        deviceCode:resultInfo.deviceCode===null?"":resultInfo.deviceCode,
        patientGender:resultInfo.patientInfo.patientGender,
        hemoCode:resultInfo.hemoCode===null?"":resultInfo.hemoCode,
        pureWeight:resultInfo.pureWeight===null?"":resultInfo.pureWeight,
        datetime:da,
        clothesWeight:resultInfo.clothesWeight===null?0:resultInfo.clothesWeight,
        isScheduled:resultInfo.isScheduled,
        isAfterMed:resultInfo.isAfterMed,
        deviceNo:resultInfo.deviceNo,
        preWeight:resultInfo.preWeight,
        // s上次体重
        lastTimeAfterWeight:resultInfo.lastTimeAfterWeight,
        // 上次血压
        lastTimeAfterBP:resultInfo.lastTimeAfterBP,
        txq:resultInfo.txq,
        medMethod:resultInfo.medMethod,
        glq:resultInfo.glq,
        preBP:resultInfo.preBP,
        nextRecordDate:resultInfo.nextRecordDate,
        actuallyClliang:resultInfo.actuallyClliang
    }
    // 写入vuex里
    patientInfoStore().setpatientInfo(info)
}
// 订阅结果事件返回函数
const  callback = function(message:any) {
    if (message.body) {
    if (message.body!==undefined) {
        const data=JSON.parse(message.body)
    if(data.deviceName)
        console.log(data,'接收到的数据')
        writeResult(data)
    } else {
    }
    else {
        alert("接收数据异常");
    }
};
const callbackState=function(message:any){
const PatientCallback=function(message:any){
    if (message.body) {
        const data=JSON.parse(message.body)
    if(data.deviceName)
        console.log(data,'设备心跳包数据')
        writeResult(data)
    } else {
        alert("接收数据异常");
        console.log(data,'患者信息读取')
        writePatient(data)
    }
}
var isErrConnectBackCalled = false;
const connectCallback=function(){
    console.log("链接成功",stompClient)
    isErrConnectBackCalled = true
    const pcName= sockteStore().pcName
    console.log("链接成功",stompClient,pcName)
    sockteStore().setsockteIsLink(true)
    // 订阅患者信息服务
    if(stompClient!==null){
        // 订阅患者信息事件
        stompClient.subscribe(`/queue/patient/info/${pcName}`,PatientCallback)
        // 订阅配置文件事件已经弃用  现在不用更新本地配置文件了
        // stompClient.subscribe(`/queue/workstation/config/set/${clientCode}/${pcName}`,configCallback)
        devices.forEach(el=>{
            if(stompClient!==null)
            stompClient.subscribe(`/queue/${clientCode}/${el.deviceName}/result`,callback)
        })
        // 发送配置文件到服务端
        stompClient.send(`/app/workstation/config/set/${clientCode}/${pcName}`,{},
        JSON.stringify(config))
    }
    // 更新sockte链接状态
    sockteStore().setsockteIsLink(true)
    console.log(sockteStore().isLink)
    if(stompClient!==null){
        if(devices.length>0){
            devices.forEach(de=>{
                stompClient.subscribe(`/queue/${clientCode}/${de.deviceName}/result`,callback)
                stompClient.send(`/app/device/request/${clientCode}/${de.deviceName}`, {}, JSON.stringify({"deviceNumber":de.deviceName}));
            })
        }
    }
}
const error_callback=function(error:any){
    console.log('链接错误',error);
    sockteStore().setsockteIsLink(false)
    setTimeout(()=>{
        console.log('10秒之后重连')
        console.log(devices,'设备列表')
        console.log('60秒之后重连',sockteNum++)
        const socket = new WebSocket('ws://hemobs.icoldchain.cn/broadcast')
        stompClient = Stomp.over(socket)
        stompClient.connect({}, connectCallback,error_callback)
    },10000)
        stompClient?.disconnect(()=>{
            console.log("disconnected!");
            stompClient = Stomp.over(socket)
            stompClient.connect({}, connectCallback,error_callback)
        },{})
    },60000)
}
// 创建客户端链接
const creatorClient=(devices2:any,clientCode2:any)=>{
    devices=devices2
    clientCode=clientCode2
    console.log(devices,'设备列表')
const creatorClient=(configObj:any)=>{
    config=configObj
    // devices=configObj.deviceList
    clientCode=configObj.clientCode
    console.log(devices,'设备列表','chong')
    const socket = new WebSocket('ws://hemobs.icoldchain.cn/broadcast')
    stompClient = Stomp.over(socket)
    stompClient.connect({}, connectCallback,error_callback)
}
export {creatorClient}
/**
 * 发送患者卡号返回患者信息
 * @param codeStr
 */
const sendPationCode=(codeStr:string)=>{
    const pcName= sockteStore().pcName
    const mode={
        clientCode:clientCode,
        queryCode:codeStr
    }
    if(stompClient!==null){
        stompClient.send(`/app/patient/info/get/${pcName}`,{},JSON.stringify(mode))
    }
}
/**
 *
 * @param mode 发送结果到sockt服务
 */
const sendPationSet=(mode:any)=>{
    if(stompClient!==null){
        stompClient.send(`/app/patient/info/set`,{},JSON.stringify(mode))
    }
}
export {creatorClient,sendPationCode,sendPationSet,writeResult,writePatient}