up
chenyc
2022-07-05 ee839f3fadece627daec9c4113ba5f5974133b7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import Stomp from 'stompjs'
import os from 'os'
var stompClient: Stomp.Client | null=null
 
import { userInfoStore } from '@/stores/userInfo'
import {sockteStore} from '@/stores/sockteInfo'
 
 
 
 
interface device{
    deviceName:string
    deviceType:string
}
 
let devices:Array<device>=[]
let clientCode:string=''
/**
 * 保存结果到vuex
 * @param resultInfo 结果写入
 */
const writeResult=(resultInfo:any)=>{
    if(devices.length>0){
        const deviceInfo=devices.find(de=>{
            return de.deviceName===resultInfo.deviceNumber
        })
        console.log('---1-',deviceInfo)
        if(deviceInfo!==undefined){
            switch(deviceInfo.deviceType){
                case '读卡器':
                    sockteStore().setdkqSockte(
                        {
                            deviceName:resultInfo.deviceNumber,
                            type:"读卡器",
                            result:resultInfo.result,
                            resultTime:resultInfo.resultTime,
                            state:0
                        }
                    )
                    break
                case "体重秤":
                    console.log(resultInfo,'体重秤')
                    sockteStore().setweightSockte(
                        {
                            deviceName:resultInfo.deviceNumber,
                            type:"体重秤",
                            result:resultInfo.result,
                            resultTime:resultInfo.resultTime,
                            state:0
                        }
                    )
                    break
                case "血压计":
                    sockteStore().setxyjSockte(
                        {
                            deviceName:resultInfo.deviceNumber,
                            type:"血压计",
                            result:resultInfo.result,
                            resultTime:resultInfo.resultTime,
                            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
                default:
                    console.log('有配置类型没有匹配')
            }
        }
    }
 
}
 
// 订阅结果事件返回函数
const  callback = function(message:any) {
    console.log('接收到数据-----',message.body)
    if (message.body!==undefined) {
        const data=JSON.parse(message.body)
        console.log(data,"体重数据")
        writeResult(data)
    }
    else {
        alert("接收数据异常");
    }
};
const callbackState=function(message:any) {
    if (message.body) {
        const data=JSON.parse(message.body)
        if(data){
            writeStatu(data)
        }
        console.log(data,'设备心跳包数据')
    }
}
const PatientCallback=function(message:any){
    if (message.body) {
        const data=JSON.parse(message.body)
        console.log(data,'患者信息读取')
    }
    
}
const connectCallback=function(){
    const pcName= os.hostname()
    // 订阅患者信息服务
    if(stompClient!==null){
        stompClient.subscribe(`/queue/patient/info/${pcName}`,PatientCallback)
    }
    console.log("链接成功",stompClient,pcName)
    // 更新sockte链接状态
    sockteStore().setsockteIsLink(true)
    console.log(sockteStore().isLink)
    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){
    console.log('链接错误',error);
    setTimeout(()=>{
        console.log('10秒之后重连')
        console.log(devices,'设备列表')
        const socket = new WebSocket('ws://hemobs.icoldchain.cn/broadcast')
        stompClient = Stomp.over(socket)
        stompClient.connect({}, connectCallback,error_callback)
 
    },10000)
}
// 创建客户端链接
const creatorClient=(devices2:any,clientCode2:any)=>{
    devices=devices2
    clientCode=clientCode2
    console.log(devices,'设备列表')
    const socket = new WebSocket('ws://hemobs.icoldchain.cn/broadcast')
    stompClient = Stomp.over(socket)
    stompClient.connect({}, connectCallback,error_callback)
 
}
const sendPationCode=(codeStr:string)=>{
    if(stompClient!==null){
        stompClient.send(`/app/patient/info/get/${os.hostname()}`,{},codeStr)
    }
}
export {creatorClient,sendPationCode}