Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

112 rader
4.2 KiB

  1. using JTNE.Protocol.MessageBody;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Xunit;
  6. using JTNE.Protocol.Extensions;
  7. using JTNE.Protocol.Formatters;
  8. using JTNE.Protocol.Attributes;
  9. namespace JTNE.Protocol.Test.MessageBody
  10. {
  11. public class JTNE_0x81_CustomBodyTest
  12. {
  13. [Fact]
  14. public void Test1()
  15. {
  16. JTNEGlobalConfigs.Instance.Register_JTNE0x81CustomBody(0x80, typeof(JTNE_0x81_0x80));
  17. JTNEGlobalConfigs.Instance.Register_JTNE0x81CustomBody(0x81, typeof(JTNE_0x81_0x81));
  18. JTNEGlobalConfigs.Instance.Register_JTNE0x81CustomDepenedBody(0x81, 0x80);
  19. JTNE_0x81 jTNE_0X81 = new JTNE_0x81();
  20. jTNE_0X81.OperateTime = DateTime.Parse("2019-01-22 23:55:56");
  21. jTNE_0X81.ParamNum = 2;
  22. jTNE_0X81.ParamList = new List<JTNE_0x81_Body> {
  23. new JTNE_0x81_0x80{
  24. ParamValue=6
  25. },
  26. new JTNE_0x81_0x81{
  27. ParamLength=6,
  28. ParamValue=new byte[]{ 1,2,3,4,5,6 }
  29. }
  30. };
  31. var hex = JTNESerializer.Serialize(jTNE_0X81).ToHexString();
  32. Assert.Equal("13011617373802800681010203040506", hex);
  33. }
  34. [Fact]
  35. public void Test1_1()
  36. {
  37. JTNEGlobalConfigs.Instance.Register_JTNE0x81CustomBody(0x80, typeof(JTNE_0x81_0x80));
  38. JTNEGlobalConfigs.Instance.Register_JTNE0x81CustomBody(0x81, typeof(JTNE_0x81_0x81));
  39. JTNEGlobalConfigs.Instance.Register_JTNE0x81CustomDepenedBody(0x81, 0x80);
  40. var data = "13011617373802800681010203040506".ToHexBytes();
  41. JTNE_0x81 jTNE_0X81 = JTNESerializer.Deserialize<JTNE_0x81>(data);
  42. Assert.Equal(DateTime.Parse("2019-01-22 23:55:56"), jTNE_0X81.OperateTime);
  43. Assert.Equal(jTNE_0X81.ParamList.Count, jTNE_0X81.ParamNum);
  44. Assert.Equal(Newtonsoft.Json.JsonConvert.SerializeObject(new List<JTNE_0x81_Body> {
  45. new JTNE_0x81_0x80{
  46. ParamValue=6
  47. },
  48. new JTNE_0x81_0x81{
  49. ParamLength=6,
  50. ParamValue=new byte[]{ 1,2,3,4,5,6 }
  51. }
  52. }), Newtonsoft.Json.JsonConvert.SerializeObject(jTNE_0X81.ParamList));
  53. }
  54. }
  55. [JTNEFormatter(typeof(JTNE_0x81_0x80Formatter))]
  56. public class JTNE_0x81_0x80 : JTNE_0x81_Body
  57. {
  58. public override byte ParamId { get; set; }= 0x80;
  59. public override byte ParamLength { get; set; } = 1;
  60. public byte ParamValue { get; set; }
  61. }
  62. [JTNEFormatter(typeof(JTNE_0x81_0x81Formatter))]
  63. public class JTNE_0x81_0x81 : JTNE_0x81_Body
  64. {
  65. public override byte ParamId { get; set; } = 0x81;
  66. public override byte ParamLength { get; set; }
  67. public byte[] ParamValue { get; set; }
  68. }
  69. public class JTNE_0x81_0x80Formatter : IJTNEFormatter<JTNE_0x81_0x80>
  70. {
  71. public JTNE_0x81_0x80 Deserialize(ReadOnlySpan<byte> bytes, out int readSize)
  72. {
  73. int offset = 0;
  74. JTNE_0x81_0x80 jTNE_0x81_0x80 = new JTNE_0x81_0x80();
  75. jTNE_0x81_0x80.ParamValue = JTNEBinaryExtensions.ReadByteLittle(bytes, ref offset);
  76. readSize = offset;
  77. return jTNE_0x81_0x80;
  78. }
  79. public int Serialize(ref byte[] bytes, int offset, JTNE_0x81_0x80 value)
  80. {
  81. offset += JTNEBinaryExtensions.WriteByteLittle(bytes, offset, value.ParamValue);
  82. return offset;
  83. }
  84. }
  85. public class JTNE_0x81_0x81Formatter : IJTNEFormatter<JTNE_0x81_0x81>
  86. {
  87. public JTNE_0x81_0x81 Deserialize(ReadOnlySpan<byte> bytes, out int readSize)
  88. {
  89. int offset = 0;
  90. JTNE_0x81_0x81 jTNE_0x81_0x81 = new JTNE_0x81_0x81();
  91. jTNE_0x81_0x81.ParamValue = JTNEBinaryExtensions.ReadBytesLittle(bytes, ref offset);
  92. jTNE_0x81_0x81.ParamLength = (byte)bytes.Length;
  93. readSize = offset;
  94. return jTNE_0x81_0x81;
  95. }
  96. public int Serialize(ref byte[] bytes, int offset, JTNE_0x81_0x81 value)
  97. {
  98. offset += JTNEBinaryExtensions.WriteBytesLittle(bytes, offset, value.ParamValue);
  99. return offset;
  100. }
  101. }
  102. }