|
|
@@ -1,8 +1,15 @@
|
|
|
package com.zd.laboratory.netty;
|
|
|
|
|
|
+import com.zd.common.core.alert.DingTalkAlert;
|
|
|
import com.zd.common.core.redis.RedisService;
|
|
|
+import com.zd.common.core.utils.DateUtils;
|
|
|
import com.zd.common.core.utils.ReUtil;
|
|
|
import com.zd.common.core.utils.SpringUtils;
|
|
|
+import com.zd.laboratory.domain.LabAbnormal;
|
|
|
+import com.zd.laboratory.domain.LabHardware;
|
|
|
+import com.zd.laboratory.domain.vo.LabHardwareVO;
|
|
|
+import com.zd.laboratory.mapper.LabAbnormalMapper;
|
|
|
+import com.zd.laboratory.mapper.LabHardwareMapper;
|
|
|
import com.zd.laboratory.socket.runner.TCPServer;
|
|
|
import com.zd.laboratory.utils.CRCCHECK;
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
@@ -10,6 +17,10 @@ import io.netty.channel.ChannelInboundHandler;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.Executors;
|
|
|
+import java.util.concurrent.ScheduledExecutorService;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
@@ -17,7 +28,7 @@ import java.util.concurrent.TimeUnit;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
public class NettyServerHandler implements ChannelInboundHandler {
|
|
|
-
|
|
|
+ ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
|
|
/**
|
|
|
* 读取客户端发送的消息
|
|
|
*/
|
|
|
@@ -146,11 +157,59 @@ public class NettyServerHandler implements ChannelInboundHandler {
|
|
|
@Override
|
|
|
public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception {
|
|
|
log.info("netty客户端断开连接" + channelHandlerContext.toString());
|
|
|
- ChannelMap.getChannelMap().entrySet().forEach(a -> {
|
|
|
- if(a.getValue() == channelHandlerContext.channel()){
|
|
|
+
|
|
|
+ for (Map.Entry<String, ChannelHandlerContext> a : ChannelMap.getChannelMap().entrySet()) {
|
|
|
+
|
|
|
+ if(a.getValue() == channelHandlerContext){
|
|
|
ChannelMap.getChannelMap().remove(a.getKey());
|
|
|
+ log.info("netty客户端断开连接清除key: " + a.getKey());
|
|
|
+
|
|
|
+ executorService.schedule(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+
|
|
|
+ String relayCode = a.getKey();
|
|
|
+
|
|
|
+ if(ChannelMap.getChannelMap().get(relayCode) != null){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ LabHardwareMapper labHardwareMapper = SpringUtils.getBean(LabHardwareMapper.class);
|
|
|
+
|
|
|
+ LabHardware labHardware = new LabHardware();
|
|
|
+ labHardware.setRelayCode(relayCode);
|
|
|
+
|
|
|
+ List<LabHardwareVO> list = labHardwareMapper.selectHardwareAndSubInfoByRelay(labHardware);
|
|
|
+
|
|
|
+ if(list.size() == 0){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ LabAbnormalMapper labAbnormalMapper = SpringUtils.getBean(LabAbnormalMapper.class);
|
|
|
+ for (LabHardwareVO hardwareVO : list) {
|
|
|
+ LabAbnormal labAbnormal = new LabAbnormal();
|
|
|
+ labAbnormal.setName(hardwareVO.getName());
|
|
|
+ labAbnormal.setType(1);
|
|
|
+ labAbnormal.setAbnormalReason("netty断开链接超过三分钟");
|
|
|
+ labAbnormal.setHardwareId(hardwareVO.getId());
|
|
|
+ labAbnormal.setDeptId(hardwareVO.getDeptId());
|
|
|
+ labAbnormal.setDeptName(hardwareVO.getDeptName());
|
|
|
+ labAbnormal.setCreateBy("系统");
|
|
|
+ labAbnormal.setCreateTime(DateUtils.getNowDate());
|
|
|
+ labAbnormalMapper.insertLabAbnormal(labAbnormal);
|
|
|
+
|
|
|
+
|
|
|
+ String content = hardwareVO.getDeptName() + hardwareVO.getSubjectName() + hardwareVO.getName()
|
|
|
+ + "netty连接已掉线三分钟未连接!请确认处理!";
|
|
|
+ DingTalkAlert.sendAlert(content);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }, 180, TimeUnit.SECONDS);
|
|
|
+
|
|
|
+ break;
|
|
|
}
|
|
|
- });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|