瀏覽代碼

指令配置信息, 分批发送

wanggaokun 1 年之前
父節點
當前提交
6569225e66

+ 7 - 0
PHM-admin/phm-manage/src/main/java/com/phm/manage/domain/common/CommonResult.java

@@ -55,6 +55,13 @@ public class CommonResult<T> implements Serializable {
         result.msg = "";
         return result;
     }
+    public static <T> CommonResult<T> complete(T data) {
+        CommonResult<T> result = new CommonResult<>();
+        result.code = GlobalErrorCodeConstants.COMPLETE.getCode();
+        result.data = data;
+        result.msg = "";
+        return result;
+    }
 
     public static <T> CommonResult<T> buildSuccess() {
         CommonResult<T> result = new CommonResult<>();

+ 1 - 0
PHM-admin/phm-manage/src/main/java/com/phm/manage/enums/GlobalErrorCodeConstants.java

@@ -8,6 +8,7 @@ import com.phm.manage.util.ErrorCode;
 public interface GlobalErrorCodeConstants {
 
     ErrorCode SUCCESS = new ErrorCode(200, "成功");
+    ErrorCode COMPLETE = new ErrorCode(999, "完成");
 
     // ========== 客户端错误段 ==========
 

+ 1 - 1
PHM-admin/phm-netty/src/main/java/com/phm/netty/domain/Message.java

@@ -124,7 +124,7 @@ public class Message implements Serializable {
     }
     public static byte[] testMsg4() {
         Message message = new Message();
-        message.setType(OrderEnum.PARAMETER.getType()).setTarget("GPHM").setSource("SPHM").setData("");
+        message.setType(OrderEnum.ORDER_CONFIG.getType()).setTarget("GPHM").setSource("SPHM").setData("{\"cmdId\": \"getConfig\"}");
         return Message.msgToBytes(message);
     }
 

+ 2 - 1
PHM-admin/phm-netty/src/main/java/com/phm/netty/enums/OrderEnum.java

@@ -20,7 +20,8 @@ public enum OrderEnum {
     COMMON((short) 1, "通用指令发送,具体指令类型有指令本身定义"),
     RESPONSE((short) 2, "指令应答"),
     TIMING((short) 3, "校时"),
-    HEARTBEAT((short) 4, "心跳包");
+    ORDER_CONFIG((short) 4, "指令配置获取"),
+    HEARTBEAT((short) 5, "心跳包");
 
     public final short type;
     public final String content;

+ 20 - 3
PHM-admin/phm-netty/src/main/java/com/phm/netty/server/handler/ByteArrayMessageHandler.java

@@ -1,13 +1,17 @@
 package com.phm.netty.server.handler;
 
+import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.util.ObjectUtil;
+import com.phm.manage.domain.OrderConfig;
+import com.phm.manage.domain.common.CommonResult;
 import com.phm.netty.constant.MsgConstant;
 import com.phm.netty.domain.Message;
 import com.phm.netty.enums.OrderEnum;
 import com.phm.netty.service.IProcessService;
 import com.phm.netty.utils.BitUtils;
 import com.phm.netty.utils.ChannelMap;
+import com.phm.netty.utils.JsonUtils;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelHandler;
 import io.netty.channel.ChannelHandlerContext;
@@ -18,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.PostConstruct;
+import java.util.List;
 
 /**
  * @Description ByteArrayMessageHandler
@@ -87,15 +92,27 @@ public class ByteArrayMessageHandler extends SimpleChannelInboundHandler<byte[]>
             // 校时功能
             byteArrayMessageHandler.processService.timing(msg);
         }
-        if (OrderEnum.PARAMETER.getType() == type) {
+        if (OrderEnum.ORDER_CONFIG.getType() == type) {
             // 参数传递
-            lastMsg = byteArrayMessageHandler.processService.getParameterConfig(msg);
+            List<OrderConfig> configList = byteArrayMessageHandler.processService.getParameterConfig(msg);
+            // 分批处理
+            List<List<OrderConfig>> batches  = CollUtil.split(configList, 10);
+            Message msgParameter = new Message();
+            for (List<OrderConfig> batch : batches) {
+                msgParameter.setType(OrderEnum.ORDER_CONFIG.getType()).setData(JsonUtils.convertJson(CommonResult.success(batch)));
+                log.info("msgParameter==={}",msgParameter);
+                ctx.writeAndFlush(Message.msgToBytes(msgParameter));
+            }
+            // 结束传输标识
+            msgParameter.setType(OrderEnum.ORDER_CONFIG.getType()).setData(JsonUtils.convertJson(CommonResult.complete("{\"cmdId\":\"complete\"}")));
+            log.info("msgParameter end ==={}",msgParameter);
+            ctx.writeAndFlush(Message.msgToBytes(msgParameter));
+            return;
         }
         if (ObjectUtil.isEmpty(lastMsg)) {
             log.info("Message数据为空");
             return;
         }
-        log.info("应答内容:{}", lastMsg);
         // 转byte[] 返回消息
         ctx.writeAndFlush(Message.msgToBytes(lastMsg));
     }

+ 4 - 1
PHM-admin/phm-netty/src/main/java/com/phm/netty/service/IProcessService.java

@@ -1,7 +1,10 @@
 package com.phm.netty.service;
 
+import com.phm.manage.domain.OrderConfig;
 import com.phm.netty.domain.Message;
 
+import java.util.List;
+
 /**
  * @Description IProcessService
  * @Author WGK
@@ -33,5 +36,5 @@ public interface IProcessService {
      * @return res
      * @throws Exception exception
      */
-    Message getParameterConfig(Message message) throws Exception;
+    List<OrderConfig> getParameterConfig(Message message) throws Exception;
 }

+ 4 - 5
PHM-admin/phm-netty/src/main/java/com/phm/netty/service/impl/ProcessService.java

@@ -78,14 +78,13 @@ public class ProcessService implements IProcessService {
         } catch (Exception exception) {
             log.error(exception.getMessage());
         }
-       log.info("校时成功");
+        log.info("校时成功");
     }
 
     @Override
-    public Message getParameterConfig(Message message) throws Exception {
+    public List<OrderConfig> getParameterConfig(Message message) throws Exception {
         List<OrderConfig> configs = orderConfigService.selectOrderConfigList(null);
-        Message msg = new Message();
-        msg.setType(OrderEnum.PARAMETER.getType()).setData(JsonUtils.convertJson(CommonResult.success(configs)));
-        return msg;
+
+        return configs;
     }
 }