HisConnectionHandler.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package com.coffee.bus.websocket;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.json.JSONUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.coffee.bus.entity.BusHospitalEntity;
  6. import com.coffee.bus.hospital.his.HisScriptSession;
  7. import com.coffee.bus.hospital.his.HisScriptSessionManager;
  8. import com.coffee.bus.service.LocalBusHospitalService;
  9. import com.coffee.common.config.websocket.MessagingRequest;
  10. import com.coffee.common.config.websocket.WebSocketConstant;
  11. import com.coffee.common.config.websocket.handler.Subscribe;
  12. import com.coffee.common.result.R;
  13. import lombok.AllArgsConstructor;
  14. import org.springframework.stereotype.Component;
  15. import org.tio.core.ChannelContext;
  16. import org.tio.core.Tio;
  17. import org.tio.websocket.common.WsResponse;
  18. import java.util.List;
  19. import java.util.concurrent.Executors;
  20. import java.util.concurrent.ScheduledExecutorService;
  21. import java.util.concurrent.TimeUnit;
  22. /**
  23. * @author lifang
  24. * @version 1.0.0
  25. * @ClassName DeviceRepeatHandler.java
  26. * @Description his脚本连接通道
  27. * @createTime 2022年03月25日 14:21:00
  28. */
  29. @Component
  30. @AllArgsConstructor
  31. public class HisConnectionHandler extends Subscribe {
  32. private final HisScriptSessionManager scriptSessionManager;
  33. private final LocalBusHospitalService hospitalService;
  34. private static ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
  35. @Override
  36. public String getId() {
  37. return WebSocketConstant.HIS_CONNECTION;
  38. }
  39. @Override
  40. public void onMessage(MessagingRequest message, ChannelContext channelContext) {
  41. //医院his脚本逻辑单独处理
  42. String tenantId = message.getTenantId();
  43. boolean close=false;
  44. if(!"-1".equals(tenantId)){
  45. Tio.send(channelContext, WsResponse.fromText(JSONUtil.toJsonStr(R.fail("连接参数错误,拒绝处理")),"utf-8"));
  46. close=true;
  47. }
  48. List<String> params = message.getParams();
  49. if (!close&&CollectionUtil.isEmpty(params)) {
  50. Tio.send(channelContext, WsResponse.fromText(JSONUtil.toJsonStr(R.fail("连接参数为空,拒绝处理")),"utf-8"));
  51. close=true;
  52. }
  53. BusHospitalEntity hospital = hospitalService.getOne(new QueryWrapper<BusHospitalEntity>().lambda().eq(BusHospitalEntity::getCode, params.get(0)));
  54. if(!close&&hospital==null){
  55. Tio.send(channelContext, WsResponse.fromText(JSONUtil.toJsonStr(R.fail(String.format("医院编码[{%s}]不存在,拒绝处理",params.get(0)))),"utf-8"));
  56. close=true;
  57. }
  58. if(close){
  59. executorService.schedule(()-> Tio.close(channelContext,"连接参数错误【"+JSONUtil.toJsonStr(message)+"】"),2, TimeUnit.SECONDS);
  60. return ;
  61. }
  62. channelContext.set("tenantId",hospital.getId());
  63. HisScriptSession hisScriptSession = scriptSessionManager.get(hospital.getId());
  64. //绑定
  65. hisScriptSession.bindChannel(channelContext);
  66. }
  67. @Override
  68. public void close(ChannelContext channelContext) {
  69. }
  70. @Override
  71. public boolean needParam() {
  72. return false;
  73. }
  74. }