Ver código fonte

继电器 netty 方式

liubo 2 anos atrás
pai
commit
fc980960f4

+ 4 - 6
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/controller/device/DeviceRemoteController.java

@@ -5,7 +5,7 @@ import com.zd.common.core.utils.ReUtil;
 import com.zd.laboratory.api.entity.CabinetLock;
 import com.zd.laboratory.api.entity.CabinetV2Lock;
 import com.zd.laboratory.mqtt.service.impl.CommonSend;
-import com.zd.laboratory.netty.PushMsgService;
+import com.zd.laboratory.netty.NettyPushMsgService;
 import com.zd.laboratory.socket.runner.TCPServer;
 import com.zd.laboratory.socket.service.SocketService;
 import com.zd.laboratory.utils.CRCCHECK;
@@ -36,8 +36,6 @@ public class DeviceRemoteController {
     private RedisService redisService;
     @Autowired
     private CommonSend commonSend;
-    @Autowired
-    private PushMsgService pushMsgService;
 
     @Value("${sys.lockTimer:30}")
     private Integer lockTimer;
@@ -72,7 +70,7 @@ public class DeviceRemoteController {
 //            ops.write(ReUtil.hexStringToByteArray(instruct));
 //            ops.flush();
             for (int i = 0; i < 5; i++) {
-                pushMsgService.push(cabinetV2Lock.getRelayCode(), ReUtil.hexStringToByteArray(instruct));
+                NettyPushMsgService.push(cabinetV2Lock.getRelayCode(), ReUtil.hexStringToByteArray(instruct));
                 logger.info("柜锁开锁指令:" + instruct);
 
                 Thread.sleep(1000);
@@ -92,7 +90,7 @@ public class DeviceRemoteController {
 
 //                ops.write(ReUtil.hexStringToByteArray(instruct));
 //                ops.flush();
-                pushMsgService.push(cabinetV2Lock.getRelayCode(), ReUtil.hexStringToByteArray(instruct));
+                NettyPushMsgService.push(cabinetV2Lock.getRelayCode(), ReUtil.hexStringToByteArray(instruct));
                 timer-=3;
 
                 Thread.sleep(3000);
@@ -177,7 +175,7 @@ public class DeviceRemoteController {
 
 //                ops.write(ReUtil.hexStringToByteArray(instruct));
 //                ops.flush();
-                pushMsgService.push(cabinetV2Lock.getRelayCode(), ReUtil.hexStringToByteArray(instruct));
+                NettyPushMsgService.push(cabinetV2Lock.getRelayCode(), ReUtil.hexStringToByteArray(instruct));
                 timer-=2;
 
                 Thread.sleep(1900);

+ 5 - 5
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/netty/PushMsgService.java

@@ -2,16 +2,16 @@ package com.zd.laboratory.netty;
 
 import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelHandlerContext;
-import org.springframework.stereotype.Service;
 
-@Service
-public class PushMsgService {
+import java.io.IOException;
 
-    public void push(String relayCode, byte[] bytes) {
+public class NettyPushMsgService {
+
+    public static void push(String relayCode, byte[] bytes) throws IOException {
         // 客户端ID
         ChannelHandlerContext channelHandlerContext = ChannelMap.getChannel(relayCode);
         if (null == channelHandlerContext) {
-            throw new RuntimeException("柜锁已离线");
+            throw new IOException("netty推送设备失败:" + relayCode);
         }
         channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer(bytes));
     }

+ 25 - 12
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/netty/NettyServerHandler.java

@@ -9,8 +9,10 @@ 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.constant.JXCTPacket;
 import com.zd.laboratory.socket.constant.SocketTypes;
 import com.zd.laboratory.socket.runner.TCPServer;
+import com.zd.laboratory.socket.service.BaseRouter;
 import com.zd.laboratory.utils.CRCCHECK;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelInboundHandler;
@@ -29,20 +31,20 @@ import java.util.concurrent.TimeUnit;
 public class NettyServerHandler implements ChannelInboundHandler {
     ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
     /**
-    * 读取客户端发送的消息
-    */
+     * 读取客户端发送的消息
+     */
     @Override
     public void channelRead(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
         byte[] msg  = (byte[]) o;
         String data = TCPServer.bytesToHexString(msg).toUpperCase();
         log.info("netty服务端接受消息为:" + data);
-        if(data.equals("6862")){
-            return;
-        }
-        // 采集器规范定义 hex发送  结构  31 11 01 01  后两位递增方式增加
-        String relayCode = data.substring(0, 8);
 
-        if(relayCode.startsWith(SocketTypes.SZZQ_PREFIX)){
+        if(data.startsWith(SocketTypes.RELAY_PREFIX.replaceAll(" ", "").toUpperCase())){
+
+            analyticRelayData(data, channelHandlerContext);
+        }else if(data.startsWith(SocketTypes.SZZQ_PREFIX)){
+            // 采集器规范定义 hex发送  结构  31 11 01 01  后两位递增方式增加
+            String relayCode = data.substring(0, 8);
             // 将通道加入ChannelMap
             if(relayCode.length() == data.length()){
                 ChannelMap.getChannelMap().put(data, channelHandlerContext);
@@ -66,15 +68,26 @@ public class NettyServerHandler implements ChannelInboundHandler {
                 redisService.setCacheObject(relayCode + ":" + bit, status, 3 * 60L, TimeUnit.SECONDS);
             }
         }
+    }
 
+    private void analyticRelayData(String data, ChannelHandlerContext channelHandlerContext) {
+        JXCTPacket packet = new JXCTPacket(data);
+        BaseRouter baseRouter = BaseRouter.routerMap.get(packet.order);
+        if (baseRouter == null) {
+            log.info("netty继电器上报不识别指令:" + packet.order);
+            return;
+        }
 
+        log.info("netty继电器状态上报:" + packet.toString());
+        ChannelMap.getChannelMap().put(packet.deviceNumber, channelHandlerContext);
+        baseRouter.routePacket(packet);
 
     }
 
 
     /**
-    * @description: 读取消息后开始的操作
-    */
+     * @description: 读取消息后开始的操作
+     */
     @Override
     public void channelReadComplete(ChannelHandlerContext channelHandlerContext) throws Exception {
 
@@ -93,8 +106,8 @@ public class NettyServerHandler implements ChannelInboundHandler {
     }
 
     /**
-    * @description: 异常发生时
-    */
+     * @description: 异常发生时
+     */
     @Override
     public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable throwable) throws Exception {
         throwable.printStackTrace();

+ 18 - 4
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/socket/command/Control.java

@@ -1,8 +1,11 @@
 package com.zd.laboratory.socket.command;
 
+import com.zd.laboratory.netty.ChannelMap;
+import com.zd.laboratory.netty.NettyPushMsgService;
 import com.zd.laboratory.socket.constant.JXCTPacket;
 import com.zd.laboratory.socket.runner.TCPServer;
 import com.zd.laboratory.socket.util.SocketUtils;
+import io.netty.channel.ChannelHandlerContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -77,9 +80,14 @@ public class Control {
         if (bit > (relayCount - 1) || state > 1 || state < 0) {
             return "指令错误";
         }
-        OutputStream ops = TCPServer.cacheMap.get(deviceNumber);
-        if (ops == null) {
-            return "设备不在线";
+        OutputStream ops = null;
+        ChannelHandlerContext channelHandlerContext = ChannelMap.getChannelMap().get(deviceNumber);
+        if (channelHandlerContext == null) {
+
+            ops = TCPServer.cacheMap.get(deviceNumber);
+            if(ops == null){
+                return "设备不在线";
+            }
         }
         //控制位
         StringBuilder bitBuilder = new StringBuilder();
@@ -108,7 +116,13 @@ public class Control {
         log.info(deviceNumber + " 下发控制:" + bitBuilder + "  hex:" + packet.toString());
         byte[] sendBody = packet.encode();
         try {
-            ops.write(sendBody);
+            if(channelHandlerContext == null){
+                log.info("socket方式下发继电器控制");
+                ops.write(sendBody);
+            }else {
+                log.info("netty方式下发继电器控制");
+                NettyPushMsgService.push(deviceNumber, sendBody);
+            }
         } catch (IOException e) {
             log.error("下发失败:" + e.toString());
             TCPServer.cacheMap.remove(deviceNumber);