package com.coffee.bus.websocket; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.coffee.bus.entity.BusHospitalEntity; import com.coffee.bus.hospital.his.HisScriptSession; import com.coffee.bus.hospital.his.HisScriptSessionManager; import com.coffee.bus.service.LocalBusHospitalService; import com.coffee.common.config.websocket.MessagingRequest; import com.coffee.common.config.websocket.WebSocketConstant; import com.coffee.common.config.websocket.handler.Subscribe; import com.coffee.common.result.R; import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; import org.tio.core.ChannelContext; import org.tio.core.Tio; import org.tio.websocket.common.WsResponse; import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; /** * @author lifang * @version 1.0.0 * @ClassName DeviceRepeatHandler.java * @Description his脚本连接通道 * @createTime 2022年03月25日 14:21:00 */ @Component @AllArgsConstructor public class HisConnectionHandler extends Subscribe { private final HisScriptSessionManager scriptSessionManager; private final LocalBusHospitalService hospitalService; private static ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); @Override public String getId() { return WebSocketConstant.HIS_CONNECTION; } @Override public void onMessage(MessagingRequest message, ChannelContext channelContext) { //医院his脚本逻辑单独处理 String tenantId = message.getTenantId(); boolean close=false; if(!"-1".equals(tenantId)){ Tio.send(channelContext, WsResponse.fromText(JSONUtil.toJsonStr(R.fail("连接参数错误,拒绝处理")),"utf-8")); close=true; } List params = message.getParams(); if (!close&&CollectionUtil.isEmpty(params)) { Tio.send(channelContext, WsResponse.fromText(JSONUtil.toJsonStr(R.fail("连接参数为空,拒绝处理")),"utf-8")); close=true; } BusHospitalEntity hospital = hospitalService.getOne(new QueryWrapper().lambda().eq(BusHospitalEntity::getCode, params.get(0))); if(!close&&hospital==null){ Tio.send(channelContext, WsResponse.fromText(JSONUtil.toJsonStr(R.fail(String.format("医院编码[{%s}]不存在,拒绝处理",params.get(0)))),"utf-8")); close=true; } if(close){ executorService.schedule(()-> Tio.close(channelContext,"连接参数错误【"+JSONUtil.toJsonStr(message)+"】"),2, TimeUnit.SECONDS); return ; } channelContext.set("tenantId",hospital.getId()); HisScriptSession hisScriptSession = scriptSessionManager.get(hospital.getId()); //绑定 hisScriptSession.bindChannel(channelContext); } @Override public void close(ChannelContext channelContext) { } @Override public boolean needParam() { return false; } }