فهرست منبع

设备修改IP和端口

xiao547607 5 سال پیش
والد
کامیت
1b6ee5d39e

+ 7 - 0
common/src/main/java/com/jpsoft/smart/modules/lapi/exception/OfflineException.java

@@ -0,0 +1,7 @@
+package com.jpsoft.smart.modules.lapi.exception;
+
+public class OfflineException extends Exception {
+    public OfflineException(String message){
+        super(message);
+    }
+}

+ 10 - 12
common/src/main/java/com/jpsoft/smart/modules/lapi/request/LapiRequest.java

@@ -1,10 +1,12 @@
 package com.jpsoft.smart.modules.lapi.request;
 
-import cn.hutool.http.HttpRequest;
 import com.alibaba.fastjson.JSONObject;
+import com.jpsoft.smart.modules.lapi.exception.OfflineException;
+import com.jpsoft.smart.modules.lapi.utils.LapiSessionUtil;
 import com.jpsoft.smart.modules.lapi.channel.LapiChannel;
 import com.jpsoft.smart.modules.sys.entity.SysLog;
 import com.jpsoft.smart.modules.sys.service.SysLogService;
+import com.sun.javaws.exceptions.OfflineLaunchException;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import io.netty.handler.codec.http.*;
@@ -13,30 +15,26 @@ import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-import java.io.UnsupportedEncodingException;
 import java.net.SocketAddress;
 import java.util.Date;
 import java.util.HashMap;
-import java.util.Map;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.TimeUnit;
 
 @Slf4j
 @Component
 public class LapiRequest {
-    @Autowired
-    private Map<String, LapiChannel> activeLapiChannelMap;
-
     @Autowired
     private SysLogService sysLogService;
 
     public JSONObject send(String deviceNo, String uri,HttpMethod httpMethod,HashMap map) throws Exception{
-        LapiChannel lapiChannel = activeLapiChannelMap.get(deviceNo);
+        LapiChannel lapiChannel = LapiSessionUtil.get(deviceNo);
 
         if (lapiChannel==null){
-            log.error(deviceNo + "不在线!");
-            throw new Exception("设备不在线");
-     //       return null;
+            throw new OfflineException("设备已离线!");
+        }
+        else if(lapiChannel.getChannel()!=null && !lapiChannel.getChannel().isOpen()){
+            throw new OfflineException("设备未联网!");
         }
 
         String msg = "";
@@ -103,8 +101,8 @@ public class LapiRequest {
             return jsonObject;
         }
         else{
-            throw new Exception("设备不在线");
-        //    return null;
+            throw new OfflineException("设备不在线");
+            //    return null;
         }
     }
 }

+ 3 - 3
common/src/main/java/com/jpsoft/smart/modules/lapi/service/impl/LapiServiceImpl.java

@@ -1038,15 +1038,14 @@ public class LapiServiceImpl implements ILapiService {
 
                     if (dataJson.getString("ResponseString").equals("Succeed") && dataJson.getString("StatusString").equals("Succeed")) {
                         //  deviceInfo.setDeviceNo(newDeviceNo);
-                        deviceInfo.setUpdateTime(new Date());
-                        deviceInfoService.update(deviceInfo);
-
                         SysLog sysLog = new SysLog();
                         sysLog.setCreateTime(new Date());
                         sysLog.setData("ip:"+newAddress+",port:"+newPort);
                         sysLog.setPointcut(deviceNo);
                         sysLog.setRemark("修改设备ip和端口");
                         sysLogService.insert(sysLog);
+                        deviceInfo.setUpdateTime(new Date());
+                        deviceInfoService.update(deviceInfo);
                     }
                 }
 
@@ -1055,6 +1054,7 @@ public class LapiServiceImpl implements ILapiService {
                 log.error(ex.getMessage(), ex);
             }
         }
+
     }
 
 

+ 31 - 0
common/src/main/java/com/jpsoft/smart/modules/lapi/utils/LapiSessionUtil.java

@@ -0,0 +1,31 @@
+package com.jpsoft.smart.modules.lapi.utils;
+
+import com.jpsoft.smart.modules.lapi.channel.LapiChannel;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class LapiSessionUtil {
+    private static final Map<String, LapiChannel> activeChannelMap = new ConcurrentHashMap<>();
+
+    public static void put(String deviceNo, LapiChannel channel) {
+        activeChannelMap.put(deviceNo, channel);
+    }
+
+    public static LapiChannel get(String deviceNo) {
+        return activeChannelMap.get(deviceNo);
+    }
+
+    public static boolean containsKey(String deviceNo) {
+        return activeChannelMap.containsKey(deviceNo);
+    }
+
+    public static void remove(String deviceNo) {
+        activeChannelMap.remove(deviceNo);
+    }
+
+    public static Set<String> keySet(){
+        return activeChannelMap.keySet();
+    }
+}

+ 65 - 20
lapi/src/main/java/com/jpsoft/smart/lapi/handler/HeartReportHandler.java

@@ -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");
             }
 

+ 128 - 0
web/src/main/java/com/jpsoft/smart/modules/base/controller/DeviceInfoController.java

@@ -2,14 +2,18 @@ package com.jpsoft.smart.modules.base.controller;
 
 import com.github.pagehelper.Page;
 import com.jpsoft.smart.modules.base.entity.CompanyDeviceRelation;
+import com.jpsoft.smart.modules.base.entity.CompanyInfo;
 import com.jpsoft.smart.modules.base.service.CompanyDeviceRelationService;
+import com.jpsoft.smart.modules.base.service.CompanyInfoService;
 import com.jpsoft.smart.modules.common.utils.HttpUtil;
 import com.jpsoft.smart.modules.common.utils.PojoUtils;
 import com.jpsoft.smart.modules.common.dto.Sort;
 import com.jpsoft.smart.modules.common.dto.MessageResult;
 import com.jpsoft.smart.modules.base.entity.DeviceInfo;
 import com.jpsoft.smart.modules.base.service.DeviceInfoService;
+import com.jpsoft.smart.modules.lapi.channel.LapiChannel;
 import com.jpsoft.smart.modules.lapi.service.ILapiService;
+import com.jpsoft.smart.modules.lapi.utils.LapiSessionUtil;
 import com.jpsoft.smart.modules.sys.entity.SysLog;
 import com.jpsoft.smart.modules.sys.entity.User;
 import com.jpsoft.smart.modules.sys.service.DataDictionaryService;
@@ -55,6 +59,9 @@ public class DeviceInfoController {
     @Autowired
     private SysLogService sysLogService;
 
+    @Autowired
+    private CompanyInfoService companyInfoService;
+
 
 
     @ApiOperation(value="添加设备")
@@ -616,4 +623,125 @@ public class DeviceInfoController {
         return msgResult;
     }
 
+    @ResponseBody
+    @ApiOperation(value="设备连接列表")
+    @GetMapping("channelList")
+    public MessageResult<List> channelList(){
+        MessageResult<List> msgResult = new MessageResult<>();
+
+        try {
+            List<Map> mapList = new ArrayList<>();
+
+            Set<String> set = LapiSessionUtil.keySet();
+
+            for (String key : set) {
+                Map<String,Object> map = new HashMap<>();
+                map.put("deviceNo",key);
+
+                //所属单位
+                DeviceInfo deviceInfo = deviceInfoService.getByDeviceNo(key);
+
+                if(deviceInfo!=null){
+                    //设备别名
+                    map.put("aliasName", deviceInfo.getAliasName());
+
+                    CompanyInfo companyInfo = companyInfoService.get(deviceInfo.getCompanyId());
+
+                    if(companyInfo!=null){
+                        map.put("companyName", companyInfo.getName());
+                    }
+                }
+
+                LapiChannel lapiChannel = LapiSessionUtil.get(key);
+
+                if (lapiChannel!=null){
+                    map.put("remoteAddress",lapiChannel.getChannel().remoteAddress().toString());
+                    map.put("isOpen",lapiChannel.getChannel().isOpen());
+                }
+
+                mapList.add(map);
+            }
+
+            mapList.sort((Map o1, Map o2)->{
+                try {
+                    String companyName1 = (String) o1.get("companyName");
+                    String companyName2 = (String) o2.get("companyName");
+
+                    String deviceNo1 = (String) o1.get("deviceNo");
+                    String deviceNo2 = (String) o2.get("deviceNo");
+
+                    int result1 = companyName1.compareTo(companyName2);
+
+                    if (result1 != 0) {
+                        return result1;
+                    } else {
+                        return deviceNo1.compareTo(deviceNo2);
+                    }
+                }
+                catch (Exception ex){
+                    logger.error(ex.getMessage(),ex);
+                    return -1;
+                }
+            });
+
+            //增加序号
+            for (int i=0;i<mapList.size();i++){
+                mapList.get(i).put("index", i+1);
+            }
+
+            msgResult.setData(mapList);
+            msgResult.setResult(true);
+        }
+        catch (Exception ex){
+            msgResult.setResult(false);
+            msgResult.setMessage(ex.getMessage());
+            ex.printStackTrace();
+        }
+
+        return msgResult;
+    }
+
+
+    @ResponseBody
+    @ApiOperation(value="批量修改设备IP和端口")
+    @PostMapping("updateDeviceData")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "deviceNos",value = "设备编号", required = true,paramType = "query"),
+            @ApiImplicitParam(name = "newAddress",value = "新IP", required = true,paramType = "query"),
+            @ApiImplicitParam(name = "newPort",value = "新端口", required = true,paramType = "query")
+    })
+    public MessageResult<String> updateDeviceData(
+            String deviceNos,
+            String newAddress,
+            String newPort,
+            @RequestAttribute String subject,
+            HttpServletRequest request){
+        MessageResult<String> msgResult = new MessageResult<>();
+
+        try {
+            List<String> deviceNoList = null;
+            if(StringUtils.isEmpty(deviceNos)){
+                throw new Exception("未选择修改设备");
+            }else{
+                deviceNoList = Arrays.asList(deviceNos.split(","));
+            }
+
+            if(StringUtils.isEmpty(newAddress)){
+                throw new Exception("新IP不能为空");
+            }
+            if(StringUtils.isEmpty(newPort)){
+                throw new Exception("新端口不能为空");
+            }
+            lapiService.updateDeviceData(deviceNoList,newAddress,newPort);
+            msgResult.setResult(true);
+            msgResult.setMessage("修改成功!");
+
+        }
+        catch (Exception ex){
+            msgResult.setResult(false);
+            msgResult.setMessage(ex.getMessage());
+        }
+        return msgResult;
+    }
+
 }