public JT808_0x0200 Deserialize(ReadOnlySpan <byte> bytes, out int readSize) { int offset = 0; JT808_0x0200 jT808_0X0200 = new JT808_0x0200 { AlarmFlag = JT808BinaryExtensions.ReadUInt32Little(bytes, ref offset), StatusFlag = JT808BinaryExtensions.ReadUInt32Little(bytes, ref offset), Lat = JT808BinaryExtensions.ReadInt32Little(bytes, ref offset), Lng = JT808BinaryExtensions.ReadInt32Little(bytes, ref offset) }; JT808StatusProperty jT808StatusProperty = new JT808StatusProperty(Convert.ToString(jT808_0X0200.StatusFlag, 2).PadLeft(32, '0')); if (jT808StatusProperty.Bit28 == '1')//西经 { jT808_0X0200.Lng = -jT808_0X0200.Lng; } if (jT808StatusProperty.Bit29 == '1')//南纬 { jT808_0X0200.Lat = -jT808_0X0200.Lat; } jT808_0X0200.Altitude = JT808BinaryExtensions.ReadUInt16Little(bytes, ref offset); jT808_0X0200.Speed = JT808BinaryExtensions.ReadUInt16Little(bytes, ref offset); jT808_0X0200.Direction = JT808BinaryExtensions.ReadUInt16Little(bytes, ref offset); jT808_0X0200.GPSTime = JT808BinaryExtensions.ReadDateTime6Little(bytes, ref offset); // 位置附加信息 jT808_0X0200.JT808LocationAttachData = new Dictionary <byte, JT808_0x0200_BodyBase>(); jT808_0X0200.JT808CustomLocationAttachOriginalData = new Dictionary <byte, byte[]>(); if (bytes.Length > 28) { int attachOffset = 0; ReadOnlySpan <byte> locationAttachMemory = bytes; ReadOnlySpan <byte> locationAttachSpan = locationAttachMemory.Slice(28); while (locationAttachSpan.Length > attachOffset) { int attachId = 1; int attachLen = 1; try { Type jT808LocationAttachType; if (JT808_0x0200_BodyBase.JT808LocationAttachMethod.TryGetValue(locationAttachSpan[attachOffset], out jT808LocationAttachType)) { int attachContentLen = locationAttachSpan[attachOffset + 1]; int locationAttachTotalLen = attachId + attachLen + attachContentLen; ReadOnlySpan <byte> attachBuffer = locationAttachSpan.Slice(attachOffset, locationAttachTotalLen); object attachImplObj = JT808FormatterExtensions.GetFormatter(jT808LocationAttachType); dynamic attachImpl = JT808FormatterResolverExtensions.JT808DynamicDeserialize(attachImplObj, attachBuffer, out readSize); attachOffset = attachOffset + locationAttachTotalLen; jT808_0X0200.JT808LocationAttachData.Add(attachImpl.AttachInfoId, attachImpl); } else if (JT808_0x0200_CustomBodyBase.CustomAttachIds.Contains(locationAttachSpan[attachOffset])) { int attachContentLen = locationAttachSpan[attachOffset + 1]; int locationAttachTotalLen = attachId + attachLen + attachContentLen; ReadOnlySpan <byte> attachBuffer = locationAttachSpan.Slice(attachOffset, locationAttachTotalLen); jT808_0X0200.JT808CustomLocationAttachOriginalData.Add(locationAttachSpan[attachOffset], attachBuffer.ToArray()); attachOffset = attachOffset + locationAttachTotalLen; } else { int attachContentLen = locationAttachSpan[attachOffset + 1]; int locationAttachTotalLen = attachId + attachLen + attachContentLen; attachOffset = attachOffset + locationAttachTotalLen; } } catch (Exception) { int attachContentLen = locationAttachSpan[attachOffset + 1]; int locationAttachTotalLen = attachId + attachLen + attachContentLen; attachOffset = attachOffset + locationAttachTotalLen; } } offset = offset + attachOffset; } readSize = offset; return(jT808_0X0200); }
public JT808_0x0200 Deserialize(ref JT808MessagePackReader reader, IJT808Config config) { JT808_0x0200 jT808_0X0200 = new JT808_0x0200(); jT808_0X0200.AlarmFlag = reader.ReadUInt32(); jT808_0X0200.StatusFlag = reader.ReadUInt32(); jT808_0X0200.Lat = reader.ReadInt32(); jT808_0X0200.Lng = reader.ReadInt32(); JT808StatusProperty jT808StatusProperty = new JT808StatusProperty(Convert.ToString(jT808_0X0200.StatusFlag, 2).PadLeft(32, '0')); if (jT808StatusProperty.Bit28 == '1')//西经 { jT808_0X0200.Lng = -jT808_0X0200.Lng; } if (jT808StatusProperty.Bit29 == '1')//南纬 { jT808_0X0200.Lat = -jT808_0X0200.Lat; } jT808_0X0200.Altitude = reader.ReadUInt16(); jT808_0X0200.Speed = reader.ReadUInt16(); jT808_0X0200.Direction = reader.ReadUInt16(); jT808_0X0200.GPSTime = reader.ReadDateTime6(); // 位置附加信息 jT808_0X0200.JT808LocationAttachData = new Dictionary <byte, JT808_0x0200_BodyBase>(); jT808_0X0200.JT808CustomLocationAttachOriginalData = new Dictionary <byte, byte[]>(); jT808_0X0200.JT808UnknownLocationAttachOriginalData = new Dictionary <byte, byte[]>(); while (reader.ReadCurrentRemainContentLength() > 0) { try { ReadOnlySpan <byte> attachSpan = reader.GetVirtualReadOnlySpan(2); byte attachId = attachSpan[0]; byte attachLen = attachSpan[1]; if (config.JT808_0X0200_Factory.JT808LocationAttachMethod.TryGetValue(attachId, out Type jT808LocationAttachType)) { object attachImplObj = config.GetMessagePackFormatterByType(jT808LocationAttachType); dynamic attachImpl = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(attachImplObj, ref reader, config); jT808_0X0200.JT808LocationAttachData.Add(attachImpl.AttachInfoId, attachImpl); } else if (config.JT808_0X0200_Custom_Factory.AttachIds.Contains(attachId)) { reader.Skip(2); jT808_0X0200.JT808CustomLocationAttachOriginalData.Add(attachId, reader.ReadArray(reader.ReaderCount - 2, attachLen + 2).ToArray()); reader.Skip(attachLen); } else { reader.Skip(2); jT808_0X0200.JT808UnknownLocationAttachOriginalData.Add(attachId, reader.ReadArray(reader.ReaderCount - 2, attachLen + 2).ToArray()); reader.Skip(attachLen); } } catch { try { byte attachId = reader.ReadByte(); byte attachLen = reader.ReadByte(); jT808_0X0200.JT808UnknownLocationAttachOriginalData.Add(attachId, reader.ReadArray(reader.ReaderCount - 2, attachLen + 2).ToArray()); reader.Skip(attachLen); } catch (Exception ex) { throw; } } } return(jT808_0X0200); }