up
chenyc
2022-07-04 359a3d84ba7d9378cb183710db221580d7ba845a
src/samples/sockteStomp.ts
@@ -1,4 +1,5 @@
import Stomp from 'stompjs'
import os from 'os'
var stompClient: Stomp.Client | null=null
import { userInfoStore } from '@/stores/userInfo'
@@ -14,7 +15,10 @@
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})
@@ -27,7 +31,7 @@
                            type:"读卡器",
                            result:resultInfo.result,
                            resultTime:resultInfo.resultTime,
                            state:true
                            state:0
                        }
                    )
                    break
@@ -38,7 +42,7 @@
                            type:"体重秤",
                            result:resultInfo.result,
                            resultTime:resultInfo.resultTime,
                            state:true
                            state:0
                        }
                    )
                    break
@@ -49,7 +53,47 @@
                            type:"血压计",
                            result:resultInfo.result,
                            resultTime:resultInfo.resultTime,
                            state:true
                            state:0
                        }
                    )
                    break
                default:
                    console.log('有配置类型没有匹配')
            }
        }
    }
}
const writeStatu=(resultInfo:any)=>{
    if(devices.length>0){
        const deviceInfo=devices.find(de=>{
            console.log(de.deviceName,resultInfo.deviceName)
            return de.deviceName===resultInfo.deviceName
        })
        if(deviceInfo!==undefined){
            switch(deviceInfo.deviceType){
                case '读卡器':
                    sockteStore().setdkqSockte(
                        {
                            deviceName:resultInfo.deviceName,
                            type:"读卡器",
                            result:resultInfo.result,
                            resultTime:resultInfo.resultTime,
                            state:0
                        }
                    )
                    break
                case "体重秤":
                    sockteStore().setweightState(resultInfo.status)
                    break
                case "血压计":
                    sockteStore().setxyjSockte(
                        {
                            deviceName:resultInfo.deviceName,
                            type:"血压计",
                            result:resultInfo.result,
                            resultTime:resultInfo.resultTime,
                            state:0
                        }
                    )
                    break
@@ -72,29 +116,41 @@
        alert("接收数据异常");
    }
};
const callbackState=function(message:any){
const callbackState=function(message:any) {
    if (message.body) {
        const data=JSON.parse(message.body)
    if(data.deviceName)
        if(data){
            writeStatu(data)
        }
        console.log(data,'设备心跳包数据')
        writeResult(data)
    } else {
        alert("接收数据异常");
    }
}
const PatientCallback=function(message:any){
    if (message.body) {
        const data=JSON.parse(message.body)
        console.log(data,'患者信息读取')
    }
}
const connectCallback=function(){
    console.log("链接成功",stompClient)
    const pcName= os.hostname()
    // 订阅患者信息服务
    stompClient.subscribe(`/queue/patient/info/${pcName}`,PatientCallback)
    console.log("链接成功",stompClient,pcName)
    // 更新sockte链接状态
    sockteStore().setsockteIsLink(true)
    console.log(sockteStore().isLink)
    if(stompClient!==null){
        if(devices.length>0){
            devices.forEach(de=>{
    console.log(devices)
    if(devices.length>0){
        devices.forEach(de=>{
            if(stompClient!==null){
                stompClient.subscribe(`/queue/${clientCode}/${de.deviceName}/result`,callback)
                // /queue/{clientCode}/{deviceName}/keepalive
                stompClient.subscribe(`/queue/${clientCode}/${de.deviceName}/keepalive`,callbackState)
                stompClient.send(`/app/device/request/${clientCode}/${de.deviceName}`, {}, JSON.stringify({"deviceNumber":de.deviceName}));
            })
        }
            }
        })
    }
}
const error_callback=function(error:any){
@@ -118,4 +174,9 @@
    stompClient.connect({}, connectCallback,error_callback)
}
export {creatorClient}
const sendPationCode=(codeStr:string)=>{
    if(stompClient!==null){
        stompClient.send(`/app/patient/info/get/${os.hostname()}`,{},codeStr)
    }
}
export {creatorClient,sendPationCode}