Пример #1
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);
        }
Пример #2
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);
        }