Prechádzať zdrojové kódy

校时功能, 指令配置获取

wanggaokun 1 rok pred
rodič
commit
343fd154a7

+ 12 - 0
PHM-admin/phm-manage/src/main/java/com/phm/manage/domain/OrderConfig.java

@@ -43,6 +43,18 @@ public class OrderConfig extends BaseEntity {
     @Excel(name = "指令名称")
     private String name;
 
+    /**
+     * 架次
+     */
+    @Excel(name = "架次")
+    private String sortie;
+
+    /**
+     * 步长
+     */
+    @Excel(name = "步长")
+    private String step;
+
     /**
      * 指令描述
      */

+ 0 - 8
PHM-admin/phm-manage/src/main/java/com/phm/manage/domain/common/OrderXmlVO.java

@@ -22,9 +22,6 @@ public class OrderXmlVO {
     // 指令名称
     private String orderName;
 
-    // 模型
-    private String model;
-
     // 架次号
     private String sortieNo;
 
@@ -41,11 +38,6 @@ public class OrderXmlVO {
         this.orderName = orderName;
     }
 
-    @XmlElement(name = "model")
-    public void setModel(String model) {
-        this.model = model;
-    }
-
     @XmlElement(name = "sortieNo")
     public void setSortieNo(String sortieNo) {
         this.sortieNo = sortieNo;

+ 12 - 0
PHM-admin/phm-manage/src/main/resources/mapper/manage/OrderConfigMapper.xml

@@ -9,6 +9,8 @@
         <result property="orderCode" column="order_code"/>
         <result property="type" column="type"/>
         <result property="name" column="name"/>
+        <result property="sortie" column="sortie"/>
+        <result property="step" column="step"/>
         <result property="description" column="description"/>
         <result property="isDelete" column="is_delete"/>
         <result property="createBy" column="create_by"/>
@@ -22,6 +24,8 @@
                order_code,
                type,
                name,
+               sortie,
+               step,
                description,
                is_delete,
                create_by,
@@ -37,6 +41,8 @@
             <if test="id != null  and id != ''">and id = #{id}</if>
             <if test="orderCode != null  and orderCode != ''">and order_code = #{orderCode}</if>
             <if test="type != null  and type != ''">and type = #{type}</if>
+            <if test="sortie != null  and sortie != ''">and sortie = #{sortie}</if>
+            <if test="step != null  and step != ''">and step = #{step}</if>
             <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
             <if test="description != null  and description != ''">and description = #{description}</if>
             <if test="isDelete != null ">and is_delete = #{isDelete}</if>
@@ -64,6 +70,8 @@
             <if test="orderCode != null">order_code,</if>
             <if test="type != null">type,</if>
             <if test="name != null">name,</if>
+            <if test="sortie != null">sortie,</if>
+            <if test="step != null">step,</if>
             <if test="description != null">description,</if>
             <if test="isDelete != null">is_delete,</if>
             <if test="createBy != null">create_by,</if>
@@ -76,6 +84,8 @@
             <if test="orderCode != null">#{orderCode},</if>
             <if test="type != null">#{type},</if>
             <if test="name != null">#{name},</if>
+            <if test="sortie != null">#{sortie},</if>
+            <if test="step != null">#{step},</if>
             <if test="description != null">#{description},</if>
             <if test="isDelete != null">#{isDelete},</if>
             <if test="createBy != null">#{createBy},</if>
@@ -91,6 +101,8 @@
             <if test="orderCode != null">order_code = #{orderCode},</if>
             <if test="type != null">type = #{type},</if>
             <if test="name != null">name = #{name},</if>
+            <if test="sortie != null">sortie = #{sortie},</if>
+            <if test="step != null">step = #{step},</if>
             <if test="description != null">description = #{description},</if>
             <if test="createBy != null">create_by = #{createBy},</if>
             <if test="createTime != null">create_time = #{createTime},</if>

+ 13 - 2
PHM-admin/phm-netty/src/main/java/com/phm/netty/domain/CommonResponse.java

@@ -1,7 +1,5 @@
 package com.phm.netty.domain;
 
-import com.phm.manage.domain.common.CommonResult;
-import com.phm.manage.enums.GlobalErrorCodeConstants;
 import lombok.Data;
 
 /**
@@ -13,10 +11,14 @@ import lombok.Data;
 public class CommonResponse {
     private String id;
     private Integer response = 200;
+
     public CommonResponse(String id) {
         this.id = id;
     }
 
+    public CommonResponse() {
+    }
+
     /**
      * 成功
      *
@@ -26,4 +28,13 @@ public class CommonResponse {
     public static CommonResponse buildSuccess(String id) {
         return new CommonResponse(id);
     }
+
+    /**
+     * 成功
+     *
+     * @return 结果
+     */
+    public static CommonResponse success() {
+        return new CommonResponse();
+    }
 }

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

@@ -37,6 +37,11 @@ public class Message implements Serializable {
 
     /**
      * 消息类型
+     * 0:参数传输
+     * 1:通用指令发送,具体指令类型有指令本身定义
+     * 2:指令应答
+     * 3:校时
+     * 4:心跳包
      */
     private short type;
 
@@ -165,4 +170,8 @@ public class Message implements Serializable {
         ObjectMapper objectMapper = new ObjectMapper();
         return objectMapper.writeValueAsString(CommonResponse.buildSuccess(code));
     }
+    public static String success() throws JsonProcessingException {
+        ObjectMapper objectMapper = new ObjectMapper();
+        return objectMapper.writeValueAsString(CommonResponse.success());
+    }
 }

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

@@ -19,7 +19,8 @@ public enum OrderEnum {
     PARAMETER((short) 0, "参数传输"),
     COMMON((short) 1, "通用指令发送,具体指令类型有指令本身定义"),
     RESPONSE((short) 2, "指令应答"),
-    HEARTBEAT((short) 3, "心跳包");
+    TIMING((short) 3, "校时"),
+    HEARTBEAT((short) 4, "心跳包");
 
     public final short type;
     public final String content;

+ 26 - 9
PHM-admin/phm-netty/src/main/java/com/phm/netty/server/handler/ByteArrayMessageHandler.java

@@ -2,13 +2,11 @@ package com.phm.netty.server.handler;
 
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.util.ObjectUtil;
-import com.phm.netty.enums.MessageEnum;
+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.service.impl.ProcessService;
 import com.phm.netty.utils.BitUtils;
-import com.phm.netty.utils.ByteUtils;
 import com.phm.netty.utils.ChannelMap;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelHandler;
@@ -20,7 +18,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.PostConstruct;
-import java.nio.ByteBuffer;
 
 /**
  * @Description ByteArrayMessageHandler
@@ -49,8 +46,19 @@ public class ByteArrayMessageHandler extends SimpleChannelInboundHandler<byte[]>
     @Override
     public void handlerAdded(ChannelHandlerContext ctx) {
         log.info("有新的连接:[{}]", ctx.channel().id().asLongText());
+        log.info("校时请求");
+        Message msg = new Message();
+        msg.setType(OrderEnum.TIMING.getType()).setData("{\"cmdId\": \"timing\"}");
+        ctx.writeAndFlush(Message.msgToBytes(msg));
     }
 
+    /**
+     * 客户端消息处理
+     *
+     * @param ctx ctx
+     * @param binaryData binaryData
+     * @throws Exception Exception
+     */
     @Override
     protected void channelRead0(ChannelHandlerContext ctx, byte[] binaryData) throws Exception {
         // 处理接收到的二进制消息
@@ -69,16 +77,25 @@ public class ByteArrayMessageHandler extends SimpleChannelInboundHandler<byte[]>
         System.arraycopy(binaryData, 4, subArray, 0, len);
         Message msg = new Message();
         Message.bytesToMsg(msg, subArray, len);
-        if (msg.getType() != OrderEnum.COMMON.getType()) {
-            log.info("非通用指令,不做处理直接返回。");
-            return;
+        short type = msg.getType();
+        Message lastMsg = null;
+        if (OrderEnum.COMMON.getType() == type) {
+            // 通用指令
+            lastMsg = byteArrayMessageHandler.processService.orderHandle(msg);
+        }
+        if (OrderEnum.TIMING.getType() == type) {
+            // 校时功能
+            lastMsg = byteArrayMessageHandler.processService.timing(msg);
+        }
+        if (OrderEnum.PARAMETER.getType() == type) {
+            // 校时功能
+            lastMsg = byteArrayMessageHandler.processService.timing(msg);
         }
-        Message lastMsg = byteArrayMessageHandler.processService.orderHandle(msg);
         if (ObjectUtil.isEmpty(lastMsg)) {
             log.info("Message数据为空");
             return;
         }
-        log.info("输出传输的内容对象:{}", lastMsg);
+        log.info("应答内容:{}", lastMsg);
         // 转byte[] 返回消息
         ctx.writeAndFlush(Message.testMsg2());
     }

+ 5 - 2
PHM-admin/phm-netty/src/main/java/com/phm/netty/service/IProcessService.java

@@ -1,6 +1,5 @@
 package com.phm.netty.service;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
 import com.phm.netty.domain.Message;
 
 /**
@@ -9,5 +8,9 @@ import com.phm.netty.domain.Message;
  * @Date 2023/9/22 9:52
  */
 public interface IProcessService {
-    Message orderHandle(Message message) throws JsonProcessingException;
+    Message orderHandle(Message message) throws Exception;
+
+    Message timing(Message message) throws Exception;
+
+    Message getParameterConfig(Message message) throws Exception;
 }

+ 41 - 19
PHM-admin/phm-netty/src/main/java/com/phm/netty/service/impl/ProcessService.java

@@ -1,31 +1,23 @@
 package com.phm.netty.service.impl;
 
-import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONObject;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.phm.common.utils.bean.BeanUtils;
 import com.phm.manage.domain.OrderConfig;
 import com.phm.manage.domain.OrderInfo;
-import com.phm.manage.domain.common.OrderXmlVO;
+import com.phm.manage.domain.common.CommonResult;
 import com.phm.manage.enums.OrderStatus;
 import com.phm.manage.service.IOrderConfigService;
 import com.phm.manage.service.IOrderInfoService;
 import com.phm.manage.service.ISortieService;
-import com.phm.manage.util.XMLParserWithJAXB;
-import com.phm.netty.enums.MessageEnum;
+import com.phm.netty.domain.Message;
 import com.phm.netty.enums.OrderEnum;
 import com.phm.netty.service.IProcessService;
-import com.phm.netty.domain.Message;
-import com.phm.system.service.ISysDictDataService;
+import com.phm.netty.utils.JsonUtils;
 import com.phm.system.service.ISysDictTypeService;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 
-import java.util.Objects;
-
-import static com.phm.netty.enums.MessageEnum.GET_DATA_BY_ONBOARD;
+import java.util.List;
 
 /**
  * @Description ProcessService
@@ -34,6 +26,7 @@ import static com.phm.netty.enums.MessageEnum.GET_DATA_BY_ONBOARD;
  */
 
 @Service
+@Slf4j
 public class ProcessService implements IProcessService {
 
     @Autowired
@@ -49,23 +42,52 @@ public class ProcessService implements IProcessService {
     private IOrderConfigService orderConfigService;
 
     @Override
-    public Message orderHandle(Message message) {
-        MessageEnum type = MessageEnum.getStructureEnum(message);
+    public Message orderHandle(Message message) throws Exception {
         JSONObject jsonObject = JSONObject.parseObject(message.getData());
         String code = jsonObject.getString("id");
         OrderConfig orderConfig = orderConfigService.selectOrderConfigByCode(code);
         OrderInfo orderInfo = new OrderInfo();
         orderInfo.setOrderName(orderConfig.getName());
         orderInfo.setOrderType(orderConfig.getType());
-        // set待执行状态
+        // 设置待执行状态
         orderInfo.setStatus(OrderStatus.S_0.getCode());
         orderInfoService.insertOrderInfo(orderInfo);
         Message msg = new Message();
+        msg.setType(OrderEnum.RESPONSE.getType()).setData(JsonUtils.convertJson(CommonResult.success("{\"id\": " + code + "}")));
+        return msg;
+    }
+
+    @Override
+    public Message timing(Message message) throws Exception {
+        log.info("校时功能");
+        JSONObject jsonObject = JSONObject.parseObject(message.getData());
+        String timestamp = jsonObject.getString("timestamp");
+        String osName = System.getProperty("os.name").toLowerCase();
         try {
-            msg.setType(OrderEnum.RESPONSE.getType()).setData(Message.successDate(code));
-        } catch (JsonProcessingException e) {
-            throw new RuntimeException(e);
+            if (osName.contains("windows")) {
+                String date = timestamp.substring(0, 10);
+                String time = timestamp.substring(11);
+                Runtime.getRuntime().exec("cmd /c date " + date); // 修改应用服务器年月日
+                Runtime.getRuntime().exec("cmd /c time " + time);// 修改应用服务器时分秒
+            }
+            if (osName.contains("linux")) {
+                String[] command = {"date", "-s", timestamp};
+                Process pr = Runtime.getRuntime().exec(command);
+                pr.waitFor();
+            }
+        } catch (Exception exception) {
+            log.error(exception.getMessage());
         }
+        Message msg = new Message();
+        msg.setType(OrderEnum.RESPONSE.getType()).setData(JsonUtils.convertJson(CommonResult.buildSuccess()));
+        return msg;
+    }
+
+    @Override
+    public Message getParameterConfig(Message message) throws Exception {
+        List<OrderConfig> configs = orderConfigService.selectOrderConfigList(null);
+        Message msg = new Message();
+        msg.setType(OrderEnum.RESPONSE.getType()).setData(JsonUtils.convertJson(CommonResult.success(configs)));
         return msg;
     }
 }

+ 25 - 0
PHM-admin/phm-netty/src/main/java/com/phm/netty/utils/JsonUtils.java

@@ -0,0 +1,25 @@
+package com.phm.netty.utils;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.phm.manage.domain.common.CommonResult;
+
+/**
+ * @Description JsonUtils
+ * @Author WGK
+ * @Date 2023/10/10 22:33
+ */
+public class JsonUtils {
+
+    /**
+     * 返回值转JSON
+     *
+     * @param commonResult 公共返回值
+     * @return 结果
+     * @throws JsonProcessingException Exception
+     */
+    public static String convertJson(CommonResult commonResult) throws JsonProcessingException {
+        ObjectMapper objectMapper = new ObjectMapper();
+        return objectMapper.writeValueAsString(commonResult);
+    }
+}