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=>{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().setweightSockte(
|
{
|
deviceName:resultInfo.deviceName,
|
type:"体重秤",
|
result:resultInfo.result,
|
resultTime:resultInfo.resultTime,
|
state:0
|
}
|
)
|
break
|
case "血压计":
|
sockteStore().setxyjSockte(
|
{
|
deviceName:resultInfo.deviceName,
|
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) {
|
if (message.body) {
|
const data=JSON.parse(message.body)
|
if(data.deviceName)
|
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()
|
// 订阅患者信息服务
|
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}
|