Beispiel #1
0
        public static T Deserialize <T>(ReadOnlySpan <byte> bytes)
        {
            var formatter = JT809FormatterExtensions.GetFormatter <T>();
            int readSize;

            return(formatter.Deserialize(bytes, out readSize));
        }
        public int Serialize(ref byte[] bytes, int offset, JT809Package value)
        {
            // 1. 先序列化数据体,根据数据体的长度赋值给头部,在序列化头部。
            int messageBodyOffset = 0;
            JT809BodiesTypeAttribute jT809BodiesTypeAttribute = value.Header.BusinessType.GetAttribute <JT809BodiesTypeAttribute>();

            if (jT809BodiesTypeAttribute != null)
            {
                if (value.Bodies != null)
                {
                    // 1.1 处理数据体
                    messageBodyOffset = JT809FormatterResolverExtensions.JT809DynamicSerialize(JT809FormatterExtensions.GetFormatter(jT809BodiesTypeAttribute.JT809BodiesType), ref bytes, messageBodyOffset, value.Bodies);
                }
            }
            byte[] messageBodyData = null;
            if (messageBodyOffset != 0)
            {
                messageBodyData = bytes.AsSpan(0, messageBodyOffset).ToArray();
                // 1.2 数据加密
                switch (value.Header.EncryptFlag)
                {
                case JT809Header_Encrypt.None:
                    break;

                case JT809Header_Encrypt.Common:
                    messageBodyData = JT809GlobalConfig.Instance.Encrypt.Encrypt(messageBodyData, value.Header.EncryptKey);
                    break;
                }
            }
            // ------------------------------------开始组包
            // 1.起始符
            offset += JT809BinaryExtensions.WriteByteLittle(bytes, offset, value.BeginFlag);
            // 2.赋值头数据长度
            if (messageBodyOffset != 0)
            {
                value.Header.MsgLength = (uint)(JT809Package.FixedByteLength + messageBodyData.Length);
            }
            else
            {
                value.Header.MsgLength = JT809Package.FixedByteLength;
            }
            // 2.1写入头部数据
            offset = JT809FormatterExtensions.GetFormatter <JT809Header>().Serialize(ref bytes, offset, value.Header);
            if (messageBodyOffset != 0)
            {
                // 3. 写入数据体
                Array.Copy(messageBodyData, 0, bytes, offset, messageBodyData.Length);
                offset         += messageBodyData.Length;
                messageBodyData = null;
            }
            // 4.校验码
            offset += JT809BinaryExtensions.WriteUInt16Little(bytes, offset, bytes.ToCRC16_CCITT(1, offset));
            // 5.终止符
            offset += JT809BinaryExtensions.WriteByteLittle(bytes, offset, value.EndFlag);
            // 6.转义
            byte[] temp = JT809Escape(bytes.AsSpan(0, offset));
            Array.Copy(temp, 0, bytes, 0, temp.Length);
            return(temp.Length);
        }
Beispiel #3
0
        public static byte[] Serialize <T>(T obj, int minBufferSize = 1024)
        {
            var formatter = JT809FormatterExtensions.GetFormatter <T>();

            byte[] buffer = JT809ArrayPool.Rent(minBufferSize);
            try
            {
                var len = formatter.Serialize(ref buffer, 0, obj);
                return(buffer.AsSpan(0, len).ToArray());
            }
            finally
            {
                JT809ArrayPool.Return(buffer);
            }
        }
Beispiel #4
0
 public int Serialize(ref byte[] bytes, int offset, JT809_0x9200_0x9203 value)
 {
     offset += JT809BinaryExtensions.WriteByteLittle(bytes, offset, (byte)value.GNSS.Count);
     foreach (var item in value.GNSS)
     {
         try
         {
             int positionOffset = JT809FormatterExtensions.GetFormatter <JT809_0x9200_0x9202>().Serialize(ref bytes, offset, item);
             offset = positionOffset;
         }
         catch (Exception ex)
         {
         }
     }
     return(offset);
 }
Beispiel #5
0
        public JT809_0x9200_0x9203 Deserialize(ReadOnlySpan <byte> bytes, out int readSize)
        {
            int offset = 0;
            JT809_0x9200_0x9203 jT809_0X1200_0x9203 = new JT809_0x9200_0x9203();

            jT809_0X1200_0x9203.GNSSCount = JT809BinaryExtensions.ReadByteLittle(bytes, ref offset);
            jT809_0X1200_0x9203.GNSS      = new List <JT809_0x9200_0x9202>();
            if (jT809_0X1200_0x9203.GNSSCount > 0)
            {
                int bufReadSize;
                int tempOffset = 0;
                for (int i = 0; i < jT809_0X1200_0x9203.GNSSCount; i++)
                {
                    try
                    {
                        if (i == 0)
                        {
                            tempOffset = offset + 36;
                            JT809_0x9200_0x9202 jT809_0x1200_0x1202 = JT809FormatterExtensions.GetFormatter <JT809_0x9200_0x9202>()
                                                                      .Deserialize(bytes.Slice(offset, 36), out bufReadSize);
                            jT809_0X1200_0x9203.GNSS.Add(jT809_0x1200_0x1202);
                        }
                        else
                        {
                            JT809_0x9200_0x9202 jT809_0x1200_0x9202 = JT809FormatterExtensions.GetFormatter <JT809_0x9200_0x9202>()
                                                                      .Deserialize(bytes.Slice(tempOffset, 36), out bufReadSize);
                            tempOffset += 36;
                            jT809_0X1200_0x9203.GNSS.Add(jT809_0x1200_0x9202);
                        }
                    }
                    catch (Exception)
                    {
                        tempOffset += 36;
                    }
                }
            }
            readSize = jT809_0X1200_0x9203.GNSSCount * 36 + 1;
            return(jT809_0X1200_0x9203);
        }
Beispiel #6
0
        public int Serialize(ref byte[] bytes, int offset, JT809_0x9300 value)
        {
            offset += JT809BinaryExtensions.WriteUInt16Little(bytes, offset, (ushort)value.SubBusinessType);
            //offset += JT809BinaryExtensions.WriteUInt32Little(memoryOwner, offset, value.DataLength);
            //JT809.Protocol.Enums.JT809BusinessType 映射对应消息特性
            JT809BodiesTypeAttribute jT809SubBodiesTypeAttribute = value.SubBusinessType.GetAttribute <JT809BodiesTypeAttribute>();

            if (jT809SubBodiesTypeAttribute == null)
            {
                throw new JT809Exception(JT809ErrorCode.GetAttributeError, $"JT809BodiesTypeAttribute Not Found>{value.SubBusinessType.ToString()}");
            }
            try
            {
                // 先写入内容,然后在根据内容反写内容长度
                offset = offset + 4;
                int contentOffset = JT809FormatterResolverExtensions.JT809DynamicSerialize(JT809FormatterExtensions.GetFormatter(jT809SubBodiesTypeAttribute.JT809BodiesType), ref bytes, offset, value.SubBodies);
                JT809BinaryExtensions.WriteUInt32Little(bytes, offset - 4, (uint)(contentOffset - offset));
                offset = contentOffset;
            }
            catch
            {
                throw new JT809Exception(JT809ErrorCode.SubBodiesParseError, $"SubBusinessType>{value.SubBusinessType.ToString()}");
            }
            return(offset);
        }
Beispiel #7
0
        public JT809_0x9300 Deserialize(ReadOnlySpan <byte> bytes, out int readSize)
        {
            int          offset       = 0;
            JT809_0x9300 jT809_0X9300 = new JT809_0x9300();

            jT809_0X9300.SubBusinessType = (JT809SubBusinessType)JT809BinaryExtensions.ReadUInt16Little(bytes, ref offset);
            jT809_0X9300.DataLength      = JT809BinaryExtensions.ReadUInt32Little(bytes, ref offset);
            //JT809.Protocol.Enums.JT809BusinessType 映射对应消息特性
            JT809BodiesTypeAttribute jT809SubBodiesTypeAttribute = jT809_0X9300.SubBusinessType.GetAttribute <JT809BodiesTypeAttribute>();

            if (jT809SubBodiesTypeAttribute == null)
            {
                throw new JT809Exception(JT809ErrorCode.GetAttributeError, $"JT809BodiesTypeAttribute Not Found>{jT809_0X9300.SubBusinessType.ToString()}");
            }
            try
            {
                jT809_0X9300.SubBodies = JT809FormatterResolverExtensions.JT809DynamicDeserialize(JT809FormatterExtensions.GetFormatter(jT809SubBodiesTypeAttribute.JT809BodiesType), bytes.Slice(offset, (int)jT809_0X9300.DataLength), out readSize);
            }
            catch
            {
                throw new JT809Exception(JT809ErrorCode.SubBodiesParseError, $"SubBusinessType>{jT809_0X9300.SubBusinessType.ToString()}");
            }
            readSize = offset;
            return(jT809_0X9300);
        }
Beispiel #8
0
        public JT809Package Deserialize(ReadOnlySpan <byte> bytes, out int readSize)
        {
            int          offset       = 0;
            JT809Package jT809Package = new JT809Package();
            // 转义还原——>验证校验码——>解析消息
            // 1. 解码(转义还原)
            ReadOnlySpan <byte> buffer = JT809DeEscape(bytes);
            // 2. 验证校验码
            //  2.1. 获取校验位索引
            int checkIndex = buffer.Length - 3;
            //  2.2. 获取校验码
            int crcCodeOffset = 0;

            jT809Package.CRCCode = JT809BinaryExtensions.ReadUInt16Little(buffer.Slice(checkIndex, 2), ref crcCodeOffset);
            if (!JT809GlobalConfig.Instance.SkipCRCCode)
            {
                //  2.3. 从消息头到校验码前一个字节
                ushort checkCode = buffer.ToCRC16_CCITT(1, checkIndex);
                //  2.4. 验证校验码
                if (jT809Package.CRCCode != checkCode)
                {
                    throw new JT809Exception(JT809ErrorCode.CRC16CheckInvalid, $"{jT809Package.CRCCode.ToString()}!={checkCode.ToString()}");
                }
            }
            jT809Package.BeginFlag = JT809BinaryExtensions.ReadByteLittle(buffer, ref offset);
            // 3.初始化消息头
            try
            {
                jT809Package.Header = JT809FormatterExtensions.GetFormatter <JT809Header>().Deserialize(buffer.Slice(offset, JT809Header.FixedByteLength), out readSize);
            }
            catch (Exception ex)
            {
                throw new JT809Exception(JT809ErrorCode.HeaderParseError, $"offset>{offset.ToString()}", ex);
            }
            offset += readSize;
            // 5.数据体处理
            //  5.1 判断是否有数据体(总长度-固定长度)> 0
            if ((jT809Package.Header.MsgLength - JT809Package.FixedByteLength) > 0)
            {
                //JT809.Protocol.JT809Enums.JT809BusinessType 映射对应消息特性
                JT809BodiesTypeAttribute jT809BodiesTypeAttribute = jT809Package.Header.BusinessType.GetAttribute <JT809BodiesTypeAttribute>();
                if (jT809BodiesTypeAttribute != null)
                {
                    try
                    {
                        // 5.2 是否加密
                        switch (jT809Package.Header.EncryptFlag)
                        {
                        case JT809Header_Encrypt.None:
                            //5.3 处理消息体
                            jT809Package.Bodies = JT809FormatterResolverExtensions.JT809DynamicDeserialize(JT809FormatterExtensions.GetFormatter(jT809BodiesTypeAttribute.JT809BodiesType), buffer.Slice(offset, checkIndex - offset), out readSize);
                            break;

                        case JT809Header_Encrypt.Common:
                            byte[] bodiesData = JT809GlobalConfig.Instance.Encrypt.Decrypt(buffer.Slice(offset, checkIndex - offset).ToArray(), jT809Package.Header.EncryptKey);
                            jT809Package.Bodies = JT809FormatterResolverExtensions.JT809DynamicDeserialize(JT809FormatterExtensions.GetFormatter(jT809BodiesTypeAttribute.JT809BodiesType), bodiesData, out readSize);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new JT809Exception(JT809ErrorCode.BodiesParseError, $"offset>{offset.ToString()}", ex);
                    }
                }
            }
            jT809Package.EndFlag = buffer[buffer.Length - 1];
            readSize             = buffer.Length;
            return(jT809Package);
        }