You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.4 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_0x82_CustomBodyTest
  12. {
  13. [Fact]
  14. public void Test1()
  15. {
  16. JTNEGlobalConfigs.Instance.Register_JTNE0x82CustomBody(0x80, typeof(JTNE_0x82_0x80));
  17. JTNE_0x82 jTNE_0X82 = new JTNE_0x82();
  18. jTNE_0X82.ControlTime = DateTime.Parse("2019-01-22 23:55:56");
  19. jTNE_0X82.ParamID = 0x80;
  20. jTNE_0X82.Parameter = new JTNE_0x82_0x80
  21. {
  22. ParamValue=100
  23. };
  24. var hex = JTNESerializer.Serialize(jTNE_0X82).ToHexString();
  25. Assert.Equal("1301161737388064", hex);
  26. }
  27. [Fact]
  28. public void Test1_1()
  29. {
  30. JTNEGlobalConfigs.Instance.Register_JTNE0x82CustomBody(0x80, typeof(JTNE_0x82_0x80));
  31. var data = "1301161737388064".ToHexBytes();
  32. JTNE_0x82 jTNE_0x82 = JTNESerializer.Deserialize<JTNE_0x82>(data);
  33. Assert.Equal(DateTime.Parse("2019-01-22 23:55:56"), jTNE_0x82.ControlTime);
  34. Assert.Equal(Newtonsoft.Json.JsonConvert.SerializeObject(new JTNE_0x82_0x80
  35. {
  36. ParamValue = 100
  37. }), Newtonsoft.Json.JsonConvert.SerializeObject(jTNE_0x82.Parameter));
  38. }
  39. }
  40. [JTNEFormatter(typeof(JTNE_0x82_0x80Formatter))]
  41. public class JTNE_0x82_0x80 : JTNE_0x82_Body
  42. {
  43. public override byte ParamId { get; set; }= 0x80;
  44. public override byte ParamLength { get; set; } = 1;
  45. public byte ParamValue { get; set; }
  46. }
  47. public class JTNE_0x82_0x80Formatter : IJTNEFormatter<JTNE_0x82_0x80>
  48. {
  49. public JTNE_0x82_0x80 Deserialize(ReadOnlySpan<byte> bytes, out int readSize)
  50. {
  51. int offset = 0;
  52. JTNE_0x82_0x80 jTNE_0x82_0x80 = new JTNE_0x82_0x80();
  53. jTNE_0x82_0x80.ParamValue = JTNEBinaryExtensions.ReadByteLittle(bytes, ref offset);
  54. readSize = offset;
  55. return jTNE_0x82_0x80;
  56. }
  57. public int Serialize(ref byte[] bytes, int offset, JTNE_0x82_0x80 value)
  58. {
  59. offset += JTNEBinaryExtensions.WriteByteLittle(bytes, offset, value.ParamValue);
  60. return offset;
  61. }
  62. }
  63. }