Exemplo n.º 1
0
        public JTNE_0x82 Deserialize(ReadOnlySpan <byte> bytes, out int readSize)
        {
            int       offset    = 0;
            JTNE_0x82 jTNE_0x82 = new JTNE_0x82();

            jTNE_0x82.ControlTime = JTNEBinaryExtensions.ReadDateTime6Little(bytes, ref offset);
            jTNE_0x82.ParamID     = JTNEBinaryExtensions.ReadByteLittle(bytes, ref offset);//参数ID

            if (JTNE_0x82_Body.JTNE_0x82Method.TryGetValue(jTNE_0x82.ParamID, out Type type))
            {
                int readSubBodySize = 0;
                jTNE_0x82.Parameter = JTNEFormatterResolverExtensions.JTNEDynamicDeserialize(JTNEFormatterExtensions.GetFormatter(type), bytes.Slice(offset), out readSubBodySize);
                offset = offset + readSubBodySize;
            }
            readSize = offset;
            return(jTNE_0x82);
        }
Exemplo n.º 2
0
        public JTNE_0x81 Deserialize(ReadOnlySpan <byte> bytes, out int readSize)
        {
            int       offset    = 0;
            JTNE_0x81 jTNE_0X81 = new JTNE_0x81();

            jTNE_0X81.OperateTime = JTNEBinaryExtensions.ReadDateTime6Little(bytes, ref offset);
            jTNE_0X81.ParamNum    = JTNEBinaryExtensions.ReadByteLittle(bytes, ref offset);
            for (int i = 0; i < jTNE_0X81.ParamNum; i++)
            {
                var paramId         = JTNEBinaryExtensions.ReadByteLittle(bytes, ref offset);//参数ID
                int readSubBodySize = 0;
                if (JTNE_0x81_Body.JTNE_0x81Method.TryGetValue(paramId, out Type type))
                {
                    ReadOnlySpan <byte> readOnlySpan = new ReadOnlySpan <byte>();
                    if (JTNE_0x81_Body.JTNE_0x81LengthOfADependOnValueOfB.TryGetValue(paramId, out byte dependOnParamId))
                    {
                        var length     = jTNE_0X81.ParamList.FirstOrDefault(m => m.ParamId == dependOnParamId).ParamLength;
                        int tempOffset = 0;
                        int lengthVal  = JTNEBinaryExtensions.ReadByteLittle(bytes.Slice(offset - length - 1, length), ref tempOffset);
                        readOnlySpan = bytes.Slice(offset, lengthVal);
                    }
                    else
                    {
                        readOnlySpan = bytes.Slice(offset);
                    }
                    if (jTNE_0X81.ParamList != null)
                    {
                        jTNE_0X81.ParamList.Add(JTNEFormatterResolverExtensions.JTNEDynamicDeserialize(JTNEFormatterExtensions.GetFormatter(type), readOnlySpan, out readSubBodySize));
                    }
                    else
                    {
                        jTNE_0X81.ParamList = new List <JTNE_0x81_Body> {
                            JTNEFormatterResolverExtensions.JTNEDynamicDeserialize(JTNEFormatterExtensions.GetFormatter(type), readOnlySpan, out readSubBodySize)
                        };
                    }
                }
                offset = offset + readSubBodySize;
            }
            readSize = offset;
            return(jTNE_0X81);
        }
Exemplo n.º 3
0
        public JTNE_0x02 Deserialize(ReadOnlySpan <byte> bytes, out int readSize)
        {
            int       offset = 0, bodyReadSize = 0;
            JTNE_0x02 jTNE_0X02 = new JTNE_0x02();

            jTNE_0X02.Values       = new Dictionary <byte, JTNE_0x02_Body>();
            jTNE_0X02.CusotmValues = new Dictionary <byte, byte[]>();
            while (offset < bytes.Length)
            {
                byte typeCode = JTNEBinaryExtensions.ReadByteLittle(bytes, ref offset);
                if (JTNE_0x02_Body.TypeCodes.TryGetValue(typeCode, out Type jTNE_0x02_BodyImpl))
                {
                    var bodyImplFormatter = JTNEFormatterExtensions.GetFormatter(jTNE_0x02_BodyImpl);
                    //从类型编码开始取 offset - 1
                    var bodyData = JTNEFormatterResolverExtensions.JTNEDynamicDeserialize(bodyImplFormatter, bytes.Slice(offset - 1), out bodyReadSize);
                    jTNE_0X02.Values.Add(typeCode, bodyData);
                    offset += bodyReadSize - 1;
                }
                else if (JTNE_0x02_CustomBody.CustomTypeCodes.TryGetValue(typeCode, out Type jTNE_0x02_CustomBodyImpl))
                {
                    int length = JTNEBinaryExtensions.ReadUInt16Little(bytes, ref offset);
                    //从类型编码开始取 offset - 1 - 2
                    //1:类型编码
                    //2:自定义数据长度
                    byte[] customBodyData = bytes.Slice(offset - 1 - 2, length + 1 + 2).ToArray();
                    jTNE_0X02.CusotmValues.Add(typeCode, customBodyData);
                    offset += customBodyData.Length - 1 - 2;
                }
                else
                {
                    offset += bodyReadSize;
                    break;
                }
            }
            readSize = offset;
            return(jTNE_0X02);
        }
Exemplo n.º 4
0
        public static dynamic Deserialize(ReadOnlySpan <byte> bytes, Type type)
        {
            var formatter = JTNEFormatterExtensions.GetFormatter(type);

            return(JTNEFormatterResolverExtensions.JTNEDynamicDeserialize(formatter, bytes, out int readSize));
        }
Exemplo n.º 5
0
        public JTNEPackage Deserialize(ReadOnlySpan <byte> bytes, out int readSize)
        {
            int offset = 0;

            // 1.进行固定头校验
            if (bytes[offset] != JTNEPackage.BeginFlag && bytes[offset + 1] == JTNEPackage.BeginFlag)
            {
                throw new JTNEException(JTNEErrorCode.BeginFlagError, $"{bytes[offset]},{bytes[offset + 1]}");
            }
            // 2.进行BCC校验码
            // 校验位 = 报文长度 - 最后一位(校验位)
            if (!JTNEGlobalConfigs.Instance.SkipCRCCode)
            {
                byte bCCCode  = bytes[bytes.Length - 1];
                byte bCCCode2 = bytes.ToXor(2, bytes.Length - 1);
                if (bCCCode != bCCCode2)
                {
                    throw new JTNEException(JTNEErrorCode.BCCCodeError, $"request:{bCCCode}!=calculate:{bCCCode2}");
                }
            }
            JTNEPackage jTNEPackage = new JTNEPackage();

            offset += 2;
            // 3.命令标识
            jTNEPackage.MsgId = JTNEBinaryExtensions.ReadByteLittle(bytes, ref offset);
            // 4.应答标识
            jTNEPackage.AskId = JTNEBinaryExtensions.ReadByteLittle(bytes, ref offset);
            // 5.VIN
            jTNEPackage.VIN = JTNEBinaryExtensions.ReadStringLittle(bytes, ref offset, 17);
            // 6.数据加密方式
            jTNEPackage.EncryptMethod = JTNEBinaryExtensions.ReadByteLittle(bytes, ref offset);
            // 7.数据单元长度是数据单元的总字节数
            jTNEPackage.DataUnitLength = JTNEBinaryExtensions.ReadUInt16Little(bytes, ref offset);
            // 8.数据体
            // 8.1.根据数据加密方式进行解码
            // 8.2.解析出对应数据体
            if (jTNEPackage.DataUnitLength > 0)
            {
                Type jTNEBodiesImplType = JTNEMsgIdFactory.GetBodiesImplTypeByMsgId(jTNEPackage.MsgId);
                if (jTNEBodiesImplType != null)
                {
                    int bodyReadSize = 0;
                    try
                    {
                        if (jTNEPackage.EncryptMethod == 0x01)
                        {
                            jTNEPackage.Bodies = JTNEFormatterResolverExtensions.JTNEDynamicDeserialize(
                                JTNEFormatterExtensions.GetFormatter(jTNEBodiesImplType),
                                bytes.Slice(offset, jTNEPackage.DataUnitLength),
                                out bodyReadSize);
                        }
                        else
                        {
                            if (JTNEGlobalConfigs.Instance.DataBodiesEncrypt != null)
                            {
                                var data = JTNEGlobalConfigs.Instance.DataBodiesEncrypt(jTNEPackage.EncryptMethod)
                                           .Decrypt(bytes.Slice(offset, jTNEPackage.DataUnitLength).ToArray());
                                jTNEPackage.Bodies = JTNEFormatterResolverExtensions.JTNEDynamicDeserialize(
                                    JTNEFormatterExtensions.GetFormatter(jTNEBodiesImplType),
                                    data,
                                    out bodyReadSize);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new JTNEException(JTNEErrorCode.BodiesParseError, ex);
                    }
                    offset += bodyReadSize;
                }
            }
            // 9.校验码
            jTNEPackage.BCCCode = JTNEBinaryExtensions.ReadByteLittle(bytes, ref offset);
            readSize            = offset;
            return(jTNEPackage);
        }