浏览代码

指令配置相关

wanggaokun 1 年之前
父节点
当前提交
4fdf8c95c0

+ 16 - 2
PHM-admin/phm-common/src/main/java/com/phm/common/core/domain/BaseEntity.java

@@ -3,10 +3,13 @@ package com.phm.common.core.domain;
 import com.fasterxml.jackson.annotation.JsonFormat;
 import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.fasterxml.jackson.annotation.JsonInclude;
+import com.phm.common.constant.HttpStatus;
 import com.phm.common.core.domain.model.LoginUser;
+import com.phm.common.exception.ServiceException;
 import com.phm.common.utils.DateUtils;
 import com.phm.common.utils.SecurityUtils;
 import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
 
 import java.io.Serializable;
@@ -20,6 +23,7 @@ import java.util.Map;
  * @author phm
  */
 @Data
+@Slf4j
 public class BaseEntity implements Serializable {
     private static final long serialVersionUID = 1L;
 
@@ -78,10 +82,15 @@ public class BaseEntity implements Serializable {
      * 新增数据SET创建和更新信息
      */
     public void initCreatInfo() {
-        LoginUser loginUser = SecurityUtils.getLoginUser();
         Date nowDate = DateUtils.getNowDate();
         this.createTime = nowDate;
         this.updateTime = nowDate;
+        LoginUser loginUser = null;
+        try {
+            loginUser = (LoginUser)SecurityUtils.getAuthentication().getPrincipal();
+        } catch (Exception ignored) {
+            log.error("获取用户信息异常");
+        }
         if (ObjectUtils.isNotEmpty(loginUser)) {
             this.createBy = loginUser.getUsername();
             this.updateBy = loginUser.getUsername();
@@ -95,8 +104,13 @@ public class BaseEntity implements Serializable {
      * 更新数据时,SET更新信息
      */
     public void initUpdateInfo() {
-        LoginUser loginUser = SecurityUtils.getLoginUser();
         this.updateTime = DateUtils.getNowDate();
+        LoginUser loginUser = null;
+        try {
+            loginUser = (LoginUser)SecurityUtils.getAuthentication().getPrincipal();
+        } catch (Exception ignored) {
+            log.error("获取用户信息异常");
+        }
         this.updateBy = ObjectUtils.isNotEmpty(loginUser) ? loginUser.getUsername() : "ANONYMITY";
     }
 }

+ 8 - 2
PHM-admin/phm-manage/src/main/java/com/phm/manage/controller/OrderConfigController.java

@@ -7,7 +7,9 @@ import com.phm.common.core.page.TableDataInfo;
 import com.phm.common.enums.BusinessType;
 import com.phm.common.utils.poi.ExcelUtil;
 import com.phm.manage.domain.OrderConfig;
+import com.phm.manage.domain.common.CommonResult;
 import com.phm.manage.service.IOrderConfigService;
+import org.apache.commons.lang3.ObjectUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.DeleteMapping;
@@ -72,8 +74,12 @@ public class OrderConfigController extends BaseController {
     @PreAuthorize("@ss.hasPermi('manage:orderConfig:add')")
     @Log(title = "指令配置", businessType = BusinessType.INSERT)
     @PostMapping
-    public AjaxResult add(@RequestBody OrderConfig orderConfig) {
-        return toAjax(orderConfigService.insertOrderConfig(orderConfig));
+    public CommonResult<Object> add(@RequestBody OrderConfig orderConfig) {
+        OrderConfig order = orderConfigService.selectOrderConfigByCode(orderConfig.getOrderCode());
+        if (ObjectUtils.isNotEmpty(order)) {
+            return CommonResult.error("指令编码已存在,请重新输入!");
+        }
+        return CommonResult.success(orderConfigService.insertOrderConfig(orderConfig));
     }
 
     /**

+ 17 - 0
PHM-admin/phm-manage/src/main/java/com/phm/manage/domain/FaultCase.java

@@ -52,4 +52,21 @@ public class FaultCase extends BaseEntity {
      */
     @Excel(name = "案例编号")
     private String caseNumber;
+
+    /**
+     * 案例特征参数
+     */
+    @Excel(name = "案例特征参数")
+    private String parameter;
+
+    /**
+     * 故障定位
+     */
+    @Excel(name = "故障定位")
+    private String location;
+    /**
+     * 故障描述
+     */
+    @Excel(name = "故障描述")
+    private String solution;
 }

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

@@ -37,6 +37,13 @@ public class CommonResult<T> implements Serializable {
         return result;
     }
 
+    public static <T> CommonResult<T> error(String message) {
+        CommonResult<T> result = new CommonResult<>();
+        result.code = GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode();
+        result.msg = message;
+        return result;
+    }
+
     public static <T> CommonResult<T> error(ErrorCode errorCode) {
         return error(errorCode.getCode(), errorCode.getMsg());
     }

+ 1 - 1
PHM-admin/phm-manage/src/main/java/com/phm/manage/service/impl/OrderInfoServiceImpl.java

@@ -54,7 +54,7 @@ public class OrderInfoServiceImpl implements IOrderInfoService {
      */
     @Override
     public int insertOrderInfo(OrderInfo orderInfo) {
-        // orderInfo.initCreatInfo();
+        orderInfo.initCreatInfo();
         orderInfo.setId(snowFlakeIdGenerator.nextId());
         return orderInfoMapper.insertOrderInfo(orderInfo);
     }

+ 12 - 0
PHM-admin/phm-netty/src/main/java/com/phm/netty/domain/CommonResponse.java

@@ -1,5 +1,7 @@
 package com.phm.netty.domain;
 
+import com.phm.manage.domain.common.CommonResult;
+import com.phm.manage.enums.GlobalErrorCodeConstants;
 import lombok.Data;
 
 /**
@@ -14,4 +16,14 @@ public class CommonResponse {
     public CommonResponse(String id) {
         this.id = id;
     }
+
+    /**
+     * 成功
+     *
+     * @param id id
+     * @return 结果
+     */
+    public static CommonResponse buildSuccess(String id) {
+        return new CommonResponse(id);
+    }
 }

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

@@ -6,6 +6,7 @@ import com.alibaba.fastjson2.JSONObject;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.phm.manage.domain.common.CommonResult;
+import com.phm.netty.enums.OrderEnum;
 import com.phm.netty.utils.BitUtils;
 import com.phm.netty.utils.ByteUtils;
 import io.netty.util.CharsetUtil;
@@ -14,6 +15,7 @@ import lombok.experimental.Accessors;
 
 import javax.xml.bind.annotation.XmlRootElement;
 import java.io.Serializable;
+import java.util.Arrays;
 
 /**
  * @Description Message
@@ -41,7 +43,7 @@ public class Message implements Serializable {
     /**
      * 毫秒时间戳,从2020.1.1开始
      */
-    private int timestamp = (int) (DateUtil.current(false) - DateUtil.parse("2020-01-01").getTime());
+    private long timestamp = DateUtil.current(false) - DateUtil.parse("2020-01-01").getTime();
 
     /**
      * 数据源
@@ -77,8 +79,8 @@ public class Message implements Serializable {
         index += 8;
         msg.target = BitUtils.toString(buf, index);
         index += 8;
-        msg.timestamp = BitUtils.toInt(buf, index);
-        index += 4;
+        msg.timestamp = BitUtils.toLong(buf, index);
+        index += 8;
         msg.reserver = BitUtils.toInt(buf, index);
         index += 4;
         byte[] bytes = new byte[len - index];
@@ -87,11 +89,28 @@ public class Message implements Serializable {
     }
 
     public static void main(String[] args) {
+        byte[] test1 = Message.testMsg();
+        byte[] test2 = Message.testMsg2();
+
+        // 使用Arrays.toString()方法打印字节数组
+        System.out.println("使用Arrays.toString()方法打印字节数组: " + Arrays.toString(test1));
+
+        // 以十六进制形式打印字节数组
+        System.out.print("以十六进制形式打印字节数组: ");
+        for (byte b : test1) {
+            System.out.print(String.format("%02X ", b));
+        }
     }
 
     public static byte[] testMsg() {
         Message message = new Message();
-        message.setType((short) 1).setTarget("GPHM").setSource("SPHM").setData("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><orderName>故障预测</orderName><orderType>ERTYU</orderType></root>");
+        message.setType(OrderEnum.COMMON.getType()).setTarget("GPHM").setSource("SPHM").setData("{\"id\": \"ZL001\",\"sortieNo\":\"JH001\"}");
+        return Message.msgToBytes(message);
+    }
+
+    public static byte[] testMsg2() {
+        Message message = new Message();
+        message.setType(OrderEnum.RESPONSE.getType()).setTarget("GPHM").setSource("SPHM").setData("{\"id\": \"ZL001\",\"response\":200}");
         return Message.msgToBytes(message);
     }
 
@@ -100,7 +119,7 @@ public class Message implements Serializable {
             return new byte[0];
         }
         int dataLen = msg.data.getBytes().length;
-        msg.setLen(dataLen + 26);
+        msg.setLen(dataLen + 30);
         byte[] bytes = new byte[msg.len + 4];
         byte[] msgLenBytes = BitUtils.getBytes(msg.len);
         System.arraycopy(msgLenBytes, 0, bytes, 0, 4);
@@ -111,11 +130,11 @@ public class Message implements Serializable {
         byte[] msgTargetBytes = BitUtils.getBytes(msg.target, 8);
         System.arraycopy(msgTargetBytes, 0, bytes, 14, 8);
         byte[] msgTimeStampBytes = BitUtils.getBytes(msg.timestamp);
-        System.arraycopy(msgTimeStampBytes, 0, bytes, 22, 4);
-        byte[] msgReserverBytes = BitUtils.getBytes(msg.reserver);
-        System.arraycopy(msgReserverBytes, 0, bytes, 26, 4);
+        System.arraycopy(msgTimeStampBytes, 0, bytes, 22, 8);
+        byte[] msgReserveBytes = BitUtils.getBytes(msg.reserver);
+        System.arraycopy(msgReserveBytes, 0, bytes, 30, 4);
         byte[] msgDataBytes = msg.getData().getBytes();
-        System.arraycopy(msgDataBytes, 0, bytes, 30, msgDataBytes.length);
+        System.arraycopy(msgDataBytes, 0, bytes, 34, msgDataBytes.length);
         return bytes;
     }
 
@@ -138,12 +157,12 @@ public class Message implements Serializable {
                 "}";
     }
 
-    public String errorDate() throws JsonProcessingException {
+    public String errorDate(String code) throws JsonProcessingException {
         ObjectMapper objectMapper = new ObjectMapper();
-        return objectMapper.writeValueAsString(CommonResult.buildSuccess());
+        return objectMapper.writeValueAsString(CommonResponse.buildSuccess(code));
     }
-    public static String successDate() throws JsonProcessingException {
+    public static String successDate(String code) throws JsonProcessingException {
         ObjectMapper objectMapper = new ObjectMapper();
-        return objectMapper.writeValueAsString(CommonResult.buildSuccess());
+        return objectMapper.writeValueAsString(CommonResponse.buildSuccess(code));
     }
 }

+ 1 - 1
PHM-admin/phm-netty/src/main/java/com/phm/netty/server/NettyServerInitialize.java

@@ -22,7 +22,7 @@ public class NettyServerInitialize extends ChannelInitializer<SocketChannel> {
     protected void initChannel(SocketChannel socketChannel) throws Exception {
         ChannelPipeline pipeline = socketChannel.pipeline();
         // 添加IdleStateHandler来处理空闲状态
-        pipeline.addLast(new IdleStateHandler(0, 5, 0));
+        pipeline.addLast(new IdleStateHandler(0, 20, 0));
         // 添加心跳处理器
         pipeline.addLast(new HeartbeatHandler());
         pipeline.addLast(new ByteArrayDecoder());

+ 8 - 18
PHM-admin/phm-netty/src/main/java/com/phm/netty/server/handler/ByteArrayMessageHandler.java

@@ -30,17 +30,17 @@ import java.nio.ByteBuffer;
 @ChannelHandler.Sharable
 @Slf4j
 @Component
-public class ByteArrayMessageHandler extends SimpleChannelInboundHandler<Object> {
+public class ByteArrayMessageHandler extends SimpleChannelInboundHandler<byte[]> {
     @Autowired
     IProcessService processService;
 
     private static ByteArrayMessageHandler byteArrayMessageHandler;
 
-
     @PostConstruct
     public void init() {
         byteArrayMessageHandler = this;
     }
+
     /**
      * 设备接入连接时处理
      *
@@ -49,19 +49,10 @@ public class ByteArrayMessageHandler extends SimpleChannelInboundHandler<Object>
     @Override
     public void handlerAdded(ChannelHandlerContext ctx) {
         log.info("有新的连接:[{}]", ctx.channel().id().asLongText());
-        // Message msg = new Message();
-        // msg.setData(OrderEnum.RESPONSE.getContent()).setType(MessageEnum.SUCCESS.getType());
-        // msg.setTarget("Client").setSource("Serve");
-        // log.info("handlerAdded->Message Info : {}", msg);
-        // ctx.channel().writeAndFlush(Message.msgToBytes(msg));
     }
 
     @Override
-    protected void channelRead0(ChannelHandlerContext ctx, Object object) throws Exception {
-        Message errorMsg = new Message();
-        errorMsg.setType(OrderEnum.RESPONSE.getType());
-        byte[] binaryData = (byte[]) object;
-        // byte[] binaryData = Message.testMsg();
+    protected void channelRead0(ChannelHandlerContext ctx, byte[] binaryData) throws Exception {
         // 处理接收到的二进制消息
         int len = 0;
         if (binaryData.length >= 4) {
@@ -78,19 +69,18 @@ public class ByteArrayMessageHandler extends SimpleChannelInboundHandler<Object>
         System.arraycopy(binaryData, 4, subArray, 0, len);
         Message msg = new Message();
         Message.bytesToMsg(msg, subArray, len);
-        if(msg.getType() != OrderEnum.COMMON.getType()) {
+        if (msg.getType() != OrderEnum.COMMON.getType()) {
+            log.info("非通用指令,不做处理直接返回。");
             return;
         }
         Message lastMsg = byteArrayMessageHandler.processService.orderHandle(msg);
         if (ObjectUtil.isEmpty(lastMsg)) {
-            log.info("Message:{}", lastMsg);
-            errorMsg.setData("Message数据为空");
-            ctx.writeAndFlush(Message.msgToBytes(errorMsg));
+            log.info("Message数据为空");
             return;
         }
         log.info("输出传输的内容对象:{}", lastMsg);
         // 转byte[] 返回消息
-        ctx.writeAndFlush(Message.msgToBytes(lastMsg));
+        ctx.writeAndFlush(Message.testMsg2());
     }
 
     /**
@@ -114,7 +104,7 @@ public class ByteArrayMessageHandler extends SimpleChannelInboundHandler<Object>
     @Override
     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
         // 打印异常
-        log.info("异常:{}", cause.getMessage());
+        log.error("异常:{}", cause.getMessage());
         // map中移除channel
         removeId(ctx);
         // 关闭连接

+ 0 - 108
PHM-admin/phm-netty/src/main/java/com/phm/netty/server/handler/InputStreamHandler.java

@@ -1,108 +0,0 @@
-package com.phm.netty.server.handler;
-
-import cn.hutool.core.collection.CollectionUtil;
-import com.phm.netty.domain.Message;
-import com.phm.netty.enums.MessageEnum;
-import com.phm.netty.utils.ChannelMap;
-import io.netty.channel.Channel;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.channel.SimpleChannelInboundHandler;
-import io.netty.util.AttributeKey;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.stereotype.Component;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-/**
- * @Description InputStreamHandler
- * @Author WGK
- * @Date 2023/10/7 11:25
- */
-@Slf4j
-@Component
-public class InputStreamHandler extends SimpleChannelInboundHandler<InputStream> {
-
-    /**
-     * 设备接入连接时处理
-     *
-     * @param ctx ctx
-     */
-    @Override
-    public void handlerAdded(ChannelHandlerContext ctx) {
-        log.info("有新的客户端连接:[{}]", ctx.channel().id().asLongText());
-        Message msg = new Message();
-        msg.setData(MessageEnum.SUCCESS.getContent()).setType(MessageEnum.SUCCESS.getType());
-        msg.setTarget("Client").setSource("Serve");
-        log.info("handlerAdded->Message Info : {}", msg);
-        ctx.channel().writeAndFlush(Message.msgToBytes(msg));
-    }
-
-    @Override
-    protected void channelRead0(ChannelHandlerContext ctx, InputStream inputStream) throws IOException {
-        // 在这里处理从 C++ 客户端发送的 InputStream 格式数据
-        // 具体的处理逻辑取决于数据的格式和内容
-        // 你可能需要使用 Java 的 IO 操作来读取 InputStream 中的数据
-        // 例如:
-        // BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
-        // String data = reader.readLine();
-        byte[] buf = readBytes(inputStream, 4);
-    }
-
-    private byte[] readBytes(InputStream is, int readLen) throws IOException {
-        byte[] buf = new byte[readLen];
-        int pos = 0;
-        int hasReadLength = 0;
-        while ((pos = is.read(buf, hasReadLength, readLen - hasReadLength)) > 0) {
-            hasReadLength = hasReadLength + pos;
-        }
-        return buf;
-    }
-
-    /**
-     * 设备下线处理
-     *
-     * @param ctx ctx
-     */
-    @Override
-    public void handlerRemoved(ChannelHandlerContext ctx) {
-        log.info("设备下线了:{}", ctx.channel().id().asLongText());
-        // map中移除channelz
-        removeId(ctx);
-    }
-
-    /**
-     * 设备连接异常处理
-     *
-     * @param ctx   ctx
-     * @param cause cause
-     */
-    @Override
-    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
-        // 打印异常
-        log.info("异常:{}", cause.getMessage());
-        // map中移除channel
-        removeId(ctx);
-        // 关闭连接
-        ctx.close();
-    }
-
-    private void removeId(ChannelHandlerContext ctx) {
-        AttributeKey<String> key = AttributeKey.valueOf("id");
-        // 获取channel中id
-        String id = ctx.channel().attr(key).get();
-        // map移除channel
-        if (CollectionUtil.isNotEmpty(ChannelMap.getChannelMap())) {
-            ChannelMap.getChannelMap().remove(id);
-        } else {
-            log.info("ChannelMap === 为空");
-        }
-    }
-
-    public void channelWrite(Channel channel, Message message) throws Exception {
-        if (null == channel) {
-            throw new RuntimeException("客户端已离线");
-        }
-        channel.writeAndFlush(message);
-    }
-}

+ 6 - 23
PHM-admin/phm-netty/src/main/java/com/phm/netty/service/impl/ProcessService.java

@@ -49,7 +49,7 @@ public class ProcessService implements IProcessService {
     private IOrderConfigService orderConfigService;
 
     @Override
-    public Message orderHandle(Message message) throws JsonProcessingException {
+    public Message orderHandle(Message message) {
         MessageEnum type = MessageEnum.getStructureEnum(message);
         JSONObject jsonObject = JSONObject.parseObject(message.getData());
         String code = jsonObject.getString("id");
@@ -61,28 +61,11 @@ public class ProcessService implements IProcessService {
         orderInfo.setStatus(OrderStatus.S_0.getCode());
         orderInfoService.insertOrderInfo(orderInfo);
         Message msg = new Message();
-        msg.setType(OrderEnum.RESPONSE.getType()).setData(Message.successDate());
+        try {
+            msg.setType(OrderEnum.RESPONSE.getType()).setData(Message.successDate(code));
+        } catch (JsonProcessingException e) {
+            throw new RuntimeException(e);
+        }
         return msg;
-        // switch (Objects.requireNonNull(type)) {
-        //     case GET_DATA_BY_ONBOARD:
-        //         // 机载数据获取指令
-        //     case GET_DATA_BY_CHAIN:
-        //         // 数据链与数据仿真数据获取指令
-        //     case FALSE_ALARM:
-        //         // 机载故障虚警抑制指令
-        //     case ISOLATE:
-        //         // 机载故障深度隔离指令
-        //     case GROUND_FAULT:
-        //         // 地面故障诊断指令
-        //     case FAILURE_PREDICTION:
-        //         // 故障预测指令
-        //     case GET_SORTIE:
-        //         // 获取架次信息
-        //     case NTP:
-        //         // 校时
-        //     default:
-        //         // 默认
-        //         return null;
-        // }
     }
 }

+ 1 - 1
PHM-admin/phm-netty/src/main/java/com/phm/netty/utils/BitUtils.java

@@ -407,7 +407,7 @@ public class BitUtils {
      * @return 结果
      */
     public static String toString(byte[] bytes, int startIndex) {
-        return new String(copyFrom(bytes, startIndex, 2), CharsetUtil.UTF_8);
+        return new String(copyFrom(bytes, startIndex, 8), CharsetUtil.UTF_8);
     }
 
     /**