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.

61 rivejä
3.0 KiB

  1. using JT809.Protocol.JT809Attributes;
  2. using JT809.Protocol.JT809Enums;
  3. using JT809.Protocol.JT809Exceptions;
  4. using JT809.Protocol.JT809Extensions;
  5. using JT809.Protocol.JT809MessageBody;
  6. using System;
  7. using System.Buffers;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. namespace JT809.Protocol.JT809Formatters.JT809MessageBodyFormatters
  11. {
  12. public class JT809_0x1300Formatter : IJT809Formatter<JT809_0x1300>
  13. {
  14. public JT809_0x1300 Deserialize(ReadOnlySpan<byte> bytes, out int readSize)
  15. {
  16. int offset = 0;
  17. JT809_0x1300 jT809_0X1300 = new JT809_0x1300();
  18. jT809_0X1300.SubBusinessType = (JT809SubBusinessType)JT809BinaryExtensions.ReadUInt16Little(bytes, ref offset);
  19. jT809_0X1300.DataLength = JT809BinaryExtensions.ReadUInt32Little(bytes, ref offset);
  20. //JT809.Protocol.JT809Enums.JT809BusinessType 映射对应消息特性
  21. JT809BodiesTypeAttribute jT809SubBodiesTypeAttribute = jT809_0X1300.SubBusinessType.GetAttribute<JT809BodiesTypeAttribute>();
  22. if (jT809SubBodiesTypeAttribute == null)
  23. {
  24. throw new JT809Exception(JT809ErrorCode.GetAttributeError, $"JT809BodiesTypeAttribute Not Found>{jT809_0X1300.SubBusinessType.ToString()}");
  25. }
  26. try
  27. {
  28. jT809_0X1300.JT809SubBodies = JT809FormatterResolverExtensions.JT809DynamicDeserialize(JT809FormatterExtensions.GetFormatter(jT809SubBodiesTypeAttribute.JT809BodiesType), bytes.Slice(offset, (int)jT809_0X1300.DataLength), out readSize);
  29. }
  30. catch
  31. {
  32. throw new JT809Exception(JT809ErrorCode.SubBodiesParseError, $"SubBusinessType>{jT809_0X1300.SubBusinessType.ToString()}");
  33. }
  34. readSize = offset;
  35. return jT809_0X1300;
  36. }
  37. public int Serialize(IMemoryOwner<byte> memoryOwner, int offset, JT809_0x1300 value)
  38. {
  39. offset += JT809BinaryExtensions.WriteUInt16Little(memoryOwner, offset, (ushort)value.SubBusinessType);
  40. offset += JT809BinaryExtensions.WriteUInt32Little(memoryOwner, offset, value.DataLength);
  41. //JT809.Protocol.JT809Enums.JT809BusinessType 映射对应消息特性
  42. JT809BodiesTypeAttribute jT809SubBodiesTypeAttribute = value.SubBusinessType.GetAttribute<JT809BodiesTypeAttribute>();
  43. if (jT809SubBodiesTypeAttribute == null)
  44. {
  45. throw new JT809Exception(JT809ErrorCode.GetAttributeError, $"JT809BodiesTypeAttribute Not Found>{value.SubBusinessType.ToString()}");
  46. }
  47. try
  48. {
  49. offset = JT809FormatterResolverExtensions.JT809DynamicSerialize(JT809FormatterExtensions.GetFormatter(jT809SubBodiesTypeAttribute.JT809BodiesType), memoryOwner, offset, value.JT809SubBodies);
  50. }
  51. catch
  52. {
  53. throw new JT809Exception(JT809ErrorCode.SubBodiesParseError, $"SubBusinessType>{value.SubBusinessType.ToString()}");
  54. }
  55. return offset;
  56. }
  57. }
  58. }