Message.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.phm.netty.domain;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import com.alibaba.fastjson2.JSONObject;
  5. import com.phm.netty.utils.BitUtils;
  6. import com.phm.netty.utils.ByteUtils;
  7. import io.netty.util.CharsetUtil;
  8. import lombok.Data;
  9. import lombok.experimental.Accessors;
  10. import javax.xml.bind.annotation.XmlRootElement;
  11. import java.io.Serializable;
  12. /**
  13. * @Description Message
  14. * @Author WGK
  15. * @Date 2023/9/14 11:41
  16. */
  17. @Data
  18. @Accessors(chain = true)
  19. public class Message implements Serializable {
  20. /**
  21. * 数据长度
  22. */
  23. private int len;
  24. /**
  25. * 接收的通讯数据body
  26. */
  27. private String data;
  28. /**
  29. * 消息类型
  30. */
  31. private short type;
  32. /**
  33. * 毫秒时间戳,从2020.1.1开始
  34. */
  35. private int timestamp = (int) (DateUtil.current(false) - DateUtil.parse("2020-01-01").getTime());
  36. /**
  37. * 数据源
  38. */
  39. private String source = "GPHM";
  40. /**
  41. * 目标
  42. */
  43. private String target = "SPHM";
  44. /**
  45. * 目标
  46. */
  47. private int reserver = 0;
  48. public Message() {
  49. }
  50. /**
  51. * 字节数组解析为 Message 对象
  52. *
  53. * @param msg Message 对象
  54. * @param buf 字节数组
  55. * @param len 要解析的长度
  56. */
  57. public static void bytesToMsg(Message msg, byte[] buf, int len) {
  58. int index = 0;
  59. msg.len = len;
  60. msg.type = BitUtils.toShort(buf, index);
  61. index += 2;
  62. msg.source = BitUtils.toString(buf, index);
  63. index += 8;
  64. msg.target = BitUtils.toString(buf, index);
  65. index += 8;
  66. msg.timestamp = BitUtils.toInt(buf, index);
  67. index += 4;
  68. msg.reserver = BitUtils.toInt(buf, index);
  69. index += 4;
  70. byte[] bytes = new byte[len - index];
  71. System.arraycopy(buf, index, bytes, 0, len - index);
  72. msg.data = new String(bytes, CharsetUtil.UTF_8);
  73. }
  74. public static void main(String[] args) {
  75. }
  76. public static byte[] testMsg() {
  77. Message message = new Message();
  78. message.setType((short) 1).setTarget("GPHM").setSource("SPHM").setData("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><orderName>故障预测</orderName><orderType>ERTYU</orderType></root>");
  79. return Message.msgToBytes(message);
  80. }
  81. public static byte[] msgToBytes(Message msg) {
  82. if (ObjectUtil.isEmpty(msg)) {
  83. return new byte[0];
  84. }
  85. int dataLen = msg.data.getBytes().length;
  86. msg.setLen(dataLen + 26);
  87. byte[] bytes = new byte[msg.len + 4];
  88. byte[] msgLenBytes = BitUtils.getBytes(msg.len);
  89. System.arraycopy(msgLenBytes, 0, bytes, 0, 4);
  90. byte[] msgTypeBytes = BitUtils.getBytes(msg.type);
  91. System.arraycopy(msgTypeBytes, 0, bytes, 4, 2);
  92. byte[] msgSourceBytes = BitUtils.getBytes(msg.source, 8);
  93. System.arraycopy(msgSourceBytes, 0, bytes, 6, 8);
  94. byte[] msgTargetBytes = BitUtils.getBytes(msg.target, 8);
  95. System.arraycopy(msgTargetBytes, 0, bytes, 14, 8);
  96. byte[] msgTimeStampBytes = BitUtils.getBytes(msg.timestamp);
  97. System.arraycopy(msgTimeStampBytes, 0, bytes, 22, 4);
  98. byte[] msgReserverBytes = BitUtils.getBytes(msg.reserver);
  99. System.arraycopy(msgReserverBytes, 0, bytes, 26, 4);
  100. byte[] msgDataBytes = msg.getData().getBytes();
  101. System.arraycopy(msgDataBytes, 0, bytes, 30, msgDataBytes.length);
  102. return bytes;
  103. }
  104. public Message(Object object) {
  105. String str = object.toString();
  106. JSONObject jsonObject = JSONObject.parseObject(str);
  107. type = Short.parseShort(jsonObject.getString("type"));
  108. data = jsonObject.getString("data");
  109. len = str.length();
  110. }
  111. public String toJsonString() {
  112. return "{" +
  113. "\n \"len\": " + len + ",\n" +
  114. " \"type\": " + type + ",\n" +
  115. " \"source\": " + source + ",\n" +
  116. " \"target\": " + target + ",\n" +
  117. " \"timestamp\": " + timestamp + ",\n" +
  118. " \"data\": " + data + "\n" +
  119. "}";
  120. }
  121. }