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