CDTFrameBean.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package wei.yigulu.cdt.cdtframe;
  2. import io.netty.buffer.ByteBuf;
  3. import lombok.Getter;
  4. import lombok.NoArgsConstructor;
  5. import lombok.Setter;
  6. import wei.yigulu.utils.CrcUtils;
  7. import java.nio.ByteBuffer;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. /**
  11. * cdt数据帧的模型
  12. *
  13. * @author 修唯xiuwei
  14. **/
  15. @NoArgsConstructor
  16. @Getter
  17. public class CDTFrameBean {
  18. /**
  19. * 同步字
  20. */
  21. private final byte[] HEAD = new byte[]{(byte) 0xEB, (byte) 0x90, (byte) 0xEB, (byte) 0x90, (byte) 0xEB, (byte) 0x90};
  22. /**
  23. * 控制字节 b7 默认0x71
  24. */
  25. @Setter
  26. private byte control = 0x71;
  27. /**
  28. * 帧类别码 b8
  29. */
  30. @Setter
  31. private CDTType cdtType;
  32. /**
  33. * 信息字数 b9 信息字的个数 即dates的长度
  34. */
  35. private int num;
  36. /**
  37. * 源地址 b10
  38. */
  39. private int sourceAddress = 1;
  40. /**
  41. * 目的地址 b11
  42. */
  43. private int destinationAddress = 1;
  44. /**
  45. * 校验码 b12
  46. */
  47. private int crc;
  48. private List<BaseDateType> dates = new ArrayList<>();
  49. public CDTFrameBean(ByteBuf byteBuf) throws InstantiationException, IllegalAccessException {
  50. loadBytes(byteBuf);
  51. }
  52. public CDTFrameBean(List<BaseDateType> dates) {
  53. if (dates.size() > 0) {
  54. this.dates = dates;
  55. this.num = dates.size();
  56. if (dates.get(0) instanceof IntegerDataType) {
  57. this.cdtType = CDTType.COMMONYC;
  58. } else {
  59. this.cdtType = CDTType.YX;
  60. }
  61. }
  62. }
  63. public void loadBytes(ByteBuf byteBuf) throws IllegalAccessException, InstantiationException {
  64. byte[] bs = new byte[5];
  65. byteBuf.readBytes(bs);
  66. if (this.control != bs[0]) {
  67. return;
  68. }
  69. this.crc = byteBuf.readByte();
  70. if ((this.crc & 0xff) != CrcUtils.generateCRC8(bs)) {
  71. return;
  72. }
  73. this.cdtType = CDTType.getByNo(bs[1]);
  74. if (this.cdtType == null) {
  75. return;
  76. }
  77. this.num = Math.abs(bs[2]);
  78. this.sourceAddress = bs[3];
  79. this.destinationAddress = bs[4];
  80. BaseDateType dateType;
  81. this.dates = new ArrayList<>(this.num);
  82. for (int i = 0; i < this.num; i++) {
  83. dateType = (BaseDateType) this.cdtType.typeClass.newInstance();
  84. dateType.loadBytes(byteBuf);
  85. dates.add(dateType);
  86. }
  87. }
  88. public byte[] encode() {
  89. ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
  90. byteBuffer.put(HEAD);
  91. byte[] bs = new byte[]{this.control, (byte) this.cdtType.no, (byte) this.num, (byte) this.sourceAddress, (byte) this.destinationAddress};
  92. byteBuffer.put(bs);
  93. byteBuffer.put((byte) CrcUtils.generateCRC8(bs));
  94. for (BaseDateType data : dates) {
  95. data.encode(byteBuffer);
  96. }
  97. byte[] bytes = new byte[byteBuffer.position()];
  98. byteBuffer.rewind();
  99. byteBuffer.get(bytes);
  100. return bytes;
  101. }
  102. @Override
  103. public String toString() {
  104. String s = "\n----------------------------------\n";
  105. if (this.cdtType != null) {
  106. s += "数据类型:" + this.cdtType.name + "\n";
  107. }
  108. s += "源地址:" + this.sourceAddress + "\n";
  109. s += "目标地址:" + this.destinationAddress + "\n";
  110. if (this.dates != null) {
  111. for (BaseDateType<?> b : this.dates) {
  112. s += b.toString();
  113. }
  114. }
  115. return s;
  116. }
  117. }