|
@@ -1,6 +1,9 @@
|
|
|
package com.jpsoft.smart.lapi.handler;
|
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.jpsoft.smart.modules.lapi.service.ILapiService;
|
|
|
+import com.jpsoft.smart.modules.lapi.utils.LapiSessionUtil;
|
|
|
import com.jpsoft.smart.modules.lapi.channel.LapiAttrKeys;
|
|
|
import com.jpsoft.smart.modules.lapi.channel.LapiChannel;
|
|
|
import com.jpsoft.smart.lapi.dto.HeartReportInfo;
|
|
@@ -19,7 +22,9 @@ import org.joda.time.Minutes;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import java.lang.reflect.Array;
|
|
|
import java.net.SocketAddress;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
@@ -33,10 +38,10 @@ public class HeartReportHandler extends SimpleChannelInboundHandler<HeartReportI
|
|
|
private DeviceInfoService deviceInfoService;
|
|
|
|
|
|
@Autowired
|
|
|
- private Map<String, LapiChannel> activeLapiChannelMap;
|
|
|
+ private SysLogService sysLogService;
|
|
|
|
|
|
@Autowired
|
|
|
- private SysLogService sysLogService;
|
|
|
+ private ILapiService lapiService;
|
|
|
|
|
|
private final String X_FRAME_OPTIONS = "X-Frame-Options";
|
|
|
private final String SAMEORIGIN = "SAMEORIGIN";
|
|
@@ -47,13 +52,13 @@ public class HeartReportHandler extends SimpleChannelInboundHandler<HeartReportI
|
|
|
|
|
|
@Override
|
|
|
protected void channelRead0(ChannelHandlerContext ctx, HeartReportInfo heartReportInfo) throws Exception {
|
|
|
- log.warn(heartReportInfo.toString());
|
|
|
+ log.debug(heartReportInfo.toString());
|
|
|
String ipAndPort = String.valueOf(ctx.channel().remoteAddress());
|
|
|
String deviceNo = heartReportInfo.getDeviceCode();
|
|
|
String ip = ipAndPort.substring(ipAndPort.indexOf("/")+1,ipAndPort.indexOf(":"));
|
|
|
|
|
|
- log.warn("收到设备心跳,deviceNo=" + deviceNo);
|
|
|
- log.warn("ip =="+ip);
|
|
|
+ log.debug("收到设备心跳,deviceNo=" + deviceNo);
|
|
|
+ log.debug("ip =="+ip);
|
|
|
|
|
|
DateTime lastHeartTime = ctx.channel().attr(LapiAttrKeys.LAST_HEART_TIME).get();
|
|
|
|
|
@@ -91,22 +96,61 @@ public class HeartReportHandler extends SimpleChannelInboundHandler<HeartReportI
|
|
|
//新连接,则设置连接对应设备号
|
|
|
if(ctx.channel().attr(LapiAttrKeys.DEVICE_NO).get()==null){
|
|
|
ctx.channel().attr(LapiAttrKeys.DEVICE_NO).set(deviceNo);
|
|
|
- }
|
|
|
|
|
|
- if (activeLapiChannelMap.containsKey(deviceNo)) {
|
|
|
- activeLapiChannelMap.get(deviceNo).setChannel(ctx.channel());
|
|
|
+ new Thread(()->{
|
|
|
+ SysLog sysLog = new SysLog();
|
|
|
+ sysLog.setPointcut(deviceNo);
|
|
|
+ sysLog.setUrl("/LAPI/V1.0/PACS/Controller/HeartReportInfo");
|
|
|
+ sysLog.setRemark("设备上线->设置连接对应设备编号");
|
|
|
+ SocketAddress remoteAddress = ctx.channel().remoteAddress();
|
|
|
+ sysLog.setRemoteIp(remoteAddress.toString());
|
|
|
+ sysLog.setCreateTime(new Date());
|
|
|
+ sysLogService.insert(sysLog);
|
|
|
+
|
|
|
+ deviceInfoService.updateByDeviceNo(deviceNo,true);
|
|
|
+ }).start();
|
|
|
}
|
|
|
- else {
|
|
|
+
|
|
|
+ if (!LapiSessionUtil.containsKey(deviceNo)){
|
|
|
LapiChannel lapiChannel = new LapiChannel();
|
|
|
lapiChannel.setChannel(ctx.channel());
|
|
|
lapiChannel.setReceivePacketQueueMap(new ConcurrentHashMap<>());
|
|
|
|
|
|
- activeLapiChannelMap.put(deviceNo, lapiChannel);
|
|
|
+ LapiSessionUtil.put(deviceNo, lapiChannel);
|
|
|
+
|
|
|
+ new Thread(()->{
|
|
|
+ SysLog sysLog = new SysLog();
|
|
|
+ sysLog.setPointcut(deviceNo);
|
|
|
+ sysLog.setUrl("/LAPI/V1.0/PACS/Controller/HeartReportInfo");
|
|
|
+ sysLog.setRemark("设备上线->加入全局map");
|
|
|
+ SocketAddress remoteAddress = ctx.channel().remoteAddress();
|
|
|
+ sysLog.setRemoteIp(remoteAddress.toString());
|
|
|
+ sysLog.setCreateTime(new Date());
|
|
|
+ sysLogService.insert(sysLog);
|
|
|
+
|
|
|
+ deviceInfoService.updateByDeviceNo(deviceNo,true);
|
|
|
+
|
|
|
+// String localAddress = ctx.channel().localAddress().toString();
|
|
|
+// String newServerAddress = "122.189.98.152";
|
|
|
+// String newPort = "7988";
|
|
|
+//
|
|
|
+// if (localAddress.indexOf(newServerAddress)==-1){
|
|
|
+// try {
|
|
|
+// lapiService.updateDeviceData(Arrays.asList(deviceNo),newServerAddress,newPort);
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// else {
|
|
|
+ //执行上线后任务
|
|
|
+ //deviceOnlineTask.run(deviceNo);
|
|
|
+// }
|
|
|
+ }).start();
|
|
|
}
|
|
|
|
|
|
//返回数据
|
|
|
try {
|
|
|
- log.warn("===心跳记录回包处理 start===");
|
|
|
+ log.debug("===心跳记录回包处理 start===");
|
|
|
Map<String,Object> responseInfo = new HashMap<>();
|
|
|
Map<String,Object> dataMap = new HashMap<>();
|
|
|
|
|
@@ -126,7 +170,7 @@ public class HeartReportHandler extends SimpleChannelInboundHandler<HeartReportI
|
|
|
|
|
|
ctx.channel().writeAndFlush(response);
|
|
|
|
|
|
- log.warn("===心跳记录回包处理 end===");
|
|
|
+ log.debug("===心跳记录回包处理 end===");
|
|
|
}
|
|
|
catch (Exception ex){
|
|
|
log.error(ex.getMessage(),ex);
|
|
@@ -147,21 +191,21 @@ public class HeartReportHandler extends SimpleChannelInboundHandler<HeartReportI
|
|
|
|
|
|
sysLog.setCreateTime(new Date());
|
|
|
|
|
|
- if(StringUtils.isNotEmpty(deviceNo)) {
|
|
|
+ if(StringUtils.isNotEmpty(deviceNo)){
|
|
|
log.warn(deviceNo + " channelInactive");
|
|
|
|
|
|
//增加判断remoteAddress是否相同,避免客户端离线后,延迟通知服务端,导致服务器将新连接删除
|
|
|
String remoteAddr1 = ctx.channel().remoteAddress().toString();
|
|
|
|
|
|
- if (activeLapiChannelMap.containsKey(deviceNo)) {
|
|
|
- String remoteAddr2 = activeLapiChannelMap.get(deviceNo).getChannel().remoteAddress().toString();
|
|
|
+ if(LapiSessionUtil.containsKey(deviceNo)) {
|
|
|
+ String remoteAddr2 = LapiSessionUtil.get(deviceNo).getChannel().remoteAddress().toString();
|
|
|
sysLog.setData("当前设备对应连接->" + remoteAddr2);
|
|
|
|
|
|
if (remoteAddr1.equals(remoteAddr2)) {
|
|
|
- activeLapiChannelMap.remove(deviceNo);
|
|
|
+ LapiSessionUtil.remove(deviceNo);
|
|
|
sysLog.setRemark("设备离线,已从全局map中删除");
|
|
|
|
|
|
- new Thread(() -> {
|
|
|
+ new Thread(()->{
|
|
|
try {
|
|
|
//防止channelRead0和channelInactive同时发生
|
|
|
Thread.sleep(1000);
|
|
@@ -169,12 +213,13 @@ public class HeartReportHandler extends SimpleChannelInboundHandler<HeartReportI
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
- deviceInfoService.updateByDeviceNo(deviceNo, false);
|
|
|
+ deviceInfoService.updateByDeviceNo(deviceNo,false);
|
|
|
}).start();
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else{
|
|
|
sysLog.setRemark("设备离线,但有新连接接入" + remoteAddr2);
|
|
|
}
|
|
|
- } else {
|
|
|
+ }else{
|
|
|
sysLog.setRemark("设备离线," + "未加入全局map");
|
|
|
}
|
|
|
|