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.

65 rivejä
3.3 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_0x9300Formatter : IJT809Formatter<JT809_0x9300>
  13. {
  14. public JT809_0x9300 Deserialize(ReadOnlySpan<byte> bytes, out int readSize)
  15. {
  16. int offset = 0;
  17. JT809_0x9300 jT809_0X9300 = new JT809_0x9300();
  18. jT809_0X9300.SubBusinessType = (JT809SubBusinessType)JT809BinaryExtensions.ReadUInt16Little(bytes, ref offset);
  19. jT809_0X9300.DataLength = JT809BinaryExtensions.ReadUInt32Little(bytes, ref offset);
  20. //JT809.Protocol.JT809Enums.JT809BusinessType 映射对应消息特性
  21. JT809BodiesTypeAttribute jT809SubBodiesTypeAttribute = jT809_0X9300.SubBusinessType.GetAttribute<JT809BodiesTypeAttribute>();
  22. if (jT809SubBodiesTypeAttribute == null)
  23. {
  24. throw new JT809Exception(JT809ErrorCode.GetAttributeError, $"JT809BodiesTypeAttribute Not Found>{jT809_0X9300.SubBusinessType.ToString()}");
  25. }
  26. try
  27. {
  28. jT809_0X9300.JT809SubBodies = JT809FormatterResolverExtensions.JT809DynamicDeserialize(JT809FormatterExtensions.GetFormatter(jT809SubBodiesTypeAttribute.JT809BodiesType), bytes.Slice(offset, (int)jT809_0X9300.DataLength), out readSize);
  29. }
  30. catch
  31. {
  32. throw new JT809Exception(JT809ErrorCode.SubBodiesParseError, $"SubBusinessType>{jT809_0X9300.SubBusinessType.ToString()}");
  33. }
  34. readSize = offset;
  35. return jT809_0X9300;
  36. }
  37. public int Serialize(IMemoryOwner<byte> memoryOwner, int offset, JT809_0x9300 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. // 先写入内容,然后在根据内容反写内容长度
  50. offset = offset + 4;
  51. int contentOffset = JT809FormatterResolverExtensions.JT809DynamicSerialize(JT809FormatterExtensions.GetFormatter(jT809SubBodiesTypeAttribute.JT809BodiesType), memoryOwner, offset, value.JT809SubBodies);
  52. JT809BinaryExtensions.WriteUInt32Little(memoryOwner, offset - 4, (uint)(contentOffset - offset));
  53. offset = contentOffset;
  54. }
  55. catch
  56. {
  57. throw new JT809Exception(JT809ErrorCode.SubBodiesParseError, $"SubBusinessType>{value.SubBusinessType.ToString()}");
  58. }
  59. return offset;
  60. }
  61. }
  62. }