Ejemplo n.º 1
0
        public void ReadNumericalTest()
        {
            byte[] bytes = "7E 01 00 10 00 00 00 20 00 00 00 40 7E".ToHexBytes();
            JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes);

            Assert.Equal(JT808Package.BeginFlag, jT808MessagePackReader.ReadStart());
            Assert.Equal(0x01, jT808MessagePackReader.ReadByte());
            Assert.Equal(16, jT808MessagePackReader.ReadUInt16());
            Assert.Equal(32, jT808MessagePackReader.ReadInt32());
            Assert.Equal((uint)64, jT808MessagePackReader.ReadUInt32());
            Assert.Equal(JT808Package.EndFlag, jT808MessagePackReader.ReadEnd());
        }
Ejemplo n.º 2
0
        public void ReadEncodeTest()
        {
            byte[] bytes = "7E 7D 02 7D 01 7D 02 7D 01 7E".ToHexBytes();
            JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes);

            jT808MessagePackReader.Decode(new byte[4096]);
            Assert.Equal(JT808Package.BeginFlag, jT808MessagePackReader.ReadStart());
            Assert.Equal(0x7E, jT808MessagePackReader.ReadByte());
            Assert.Equal(0x7D, jT808MessagePackReader.ReadByte());
            Assert.Equal(0x7E, jT808MessagePackReader.ReadByte());
            Assert.Equal(0x7D, jT808MessagePackReader.ReadByte());
            Assert.Equal(JT808Package.EndFlag, jT808MessagePackReader.ReadEnd());
            Assert.Equal(6, jT808MessagePackReader.ReaderCount);
        }
Ejemplo n.º 3
0
        public JT808HeaderPackage Deserialize(ref JT808MessagePackReader reader, IJT808Config config)
        {
            // 1. 验证校验和
            if (!config.SkipCRCCode)
            {
                if (reader.CheckXorCodeVali)
                {
                    throw new JT808Exception(JT808ErrorCode.CheckCodeNotEqual, $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}");
                }
            }
            JT808HeaderPackage jT808Package = new JT808HeaderPackage();

            // ---------------开始解包--------------
            // 2.读取起始位置
            jT808Package.Begin = reader.ReadStart();
            // 3.读取头部信息
            jT808Package.Header = new JT808Header();
            //  3.1.读取消息Id
            jT808Package.Header.MsgId = reader.ReadUInt16();
            //  3.2.读取消息体属性
            jT808Package.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty();
            ushort messageBodyPropertyValue = reader.ReadUInt16();

            //    3.2.1.解包消息体属性
            jT808Package.Header.MessageBodyProperty.Unwrap(messageBodyPropertyValue, config);
            // 3.3.读取终端手机号
            jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength);
            // 3.4.读取消息流水号
            jT808Package.Header.MsgNum = reader.ReadUInt16();
            // 3.5.判断有无分包
            if (jT808Package.Header.MessageBodyProperty.IsPackge)
            {
                //3.5.1.读取消息包总数
                jT808Package.Header.MessageBodyProperty.PackgeCount = reader.ReadUInt16();
                //3.5.2.读取消息包序号
                jT808Package.Header.MessageBodyProperty.PackageIndex = reader.ReadUInt16();
            }
            // 4.处理数据体
            //  4.1.判断有无数据体
            if (jT808Package.Header.MessageBodyProperty.DataLength > 0)
            {
                jT808Package.Bodies = reader.ReadContent().ToArray();
            }
            // 5.读取校验码
            jT808Package.CheckCode = reader.ReadByte();
            // 6.读取终止位置
            jT808Package.End = reader.ReadEnd();
            // ---------------解包完成--------------
            return(jT808Package);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public JT808Package Deserialize(ref JT808MessagePackReader reader, IJT808Config config)
        {
            // 1. 验证校验和
            if (!config.SkipCRCCode)
            {
                if (!reader.CheckXorCodeVali)
                {
                    throw new JT808Exception(JT808ErrorCode.CheckCodeNotEqual, $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}");
                }
            }
            JT808Package jT808Package = new JT808Package();

            // ---------------开始解包--------------
            // 2.读取起始位置
            jT808Package.Begin = reader.ReadStart();
            // 3.读取头部信息
            jT808Package.Header = new JT808Header();
            //  3.1.读取消息Id
            jT808Package.Header.MsgId = reader.ReadUInt16();
            //  3.2.读取消息体属性
            jT808Package.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(reader.ReadUInt16());
            if (reader.Version == JT808Version.JTT2013Force)
            {
                jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim);
                reader.Version = JT808Version.JTT2013;
            }
            else
            {
                if (reader.Version == JT808Version.JTT2019 || jT808Package.Header.MessageBodyProperty.VersionFlag)
                {
                    //2019版本
                    jT808Package.Header.ProtocolVersion = reader.ReadByte();
                    //  3.4.读取终端手机号
                    jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(20, config.Trim);
                    reader.Version = JT808Version.JTT2019;
                }
                else
                {
                    //2013版本
                    //  3.3.读取终端手机号
                    jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim);
                }
            }
            jT808Package.Version = reader.Version;
            //  3.4.读取消息流水号
            jT808Package.Header.MsgNum = reader.ReadUInt16();
            //  3.5.判断有无分包
            if (jT808Package.Header.MessageBodyProperty.IsPackage)
            {
                //3.5.1.读取消息包总数
                jT808Package.Header.PackgeCount = reader.ReadUInt16();
                //3.5.2.读取消息包序号
                jT808Package.Header.PackageIndex = reader.ReadUInt16();
            }
            // 4.处理数据体
            //  4.1.判断有无数据体
            if (jT808Package.Header.MessageBodyProperty.DataLength > 0)
            {
                if (config.MsgIdFactory.TryGetValue(jT808Package.Header.MsgId, out object instance))
                {
                    if (jT808Package.Header.MessageBodyProperty.IsPackage)
                    {
                        //读取分包的数据体
                        try
                        {
                            jT808Package.SubDataBodies = reader.ReadArray(jT808Package.Header.MessageBodyProperty.DataLength).ToArray();
                        }
                        catch (Exception ex)
                        {
                            throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
                        }
                    }
                    else
                    {
                        try
                        {
                            //4.2.处理消息体
                            jT808Package.Bodies = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(
                                instance,
                                ref reader, config);
                        }
                        catch (Exception ex)
                        {
                            throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
                        }
                    }
                }
            }
            // 5.读取校验码
            jT808Package.CheckCode = reader.ReadByte();
            // 6.读取终止位置
            jT808Package.End = reader.ReadEnd();
            // ---------------解包完成--------------
            return(jT808Package);
        }
Ejemplo n.º 5
0
        public JT808Package Deserialize(ref JT808MessagePackReader reader, IJT808Config config)
        {
            // 1. 验证校验和
            if (!config.SkipCRCCode)
            {
                if (!reader.CheckXorCodeVali)
                {
                    throw new JT808Exception(JT808ErrorCode.CheckCodeNotEqual, $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}");
                }
            }
            JT808Package jT808Package = new JT808Package();

            // ---------------开始解包--------------
            // 2.读取起始位置
            jT808Package.Begin = reader.ReadStart();
            // 3.读取头部信息
            jT808Package.Header = new JT808Header();
            //  3.1.读取消息Id
            jT808Package.Header.MsgId = reader.ReadUInt16();
            //  3.2.读取消息体属性
            jT808Package.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty();
            ushort messageBodyPropertyValue = reader.ReadUInt16();

            //    3.2.1.解包消息体属性
            jT808Package.Header.MessageBodyProperty.Unwrap(messageBodyPropertyValue, config);
            // 3.3.读取终端手机号
            jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength);
            // 3.4.读取消息流水号
            jT808Package.Header.MsgNum = reader.ReadUInt16();
            // 3.5.判断有无分包
            if (jT808Package.Header.MessageBodyProperty.IsPackge)
            {
                //3.5.1.读取消息包总数
                jT808Package.Header.MessageBodyProperty.PackgeCount = reader.ReadUInt16();
                //3.5.2.读取消息包序号
                jT808Package.Header.MessageBodyProperty.PackageIndex = reader.ReadUInt16();
            }
            // 4.处理数据体
            //  4.1.判断有无数据体
            if (jT808Package.Header.MessageBodyProperty.DataLength > 0)
            {
                Type jT808BodiesImplType = config.MsgIdFactory.GetBodiesImplTypeByMsgId(jT808Package.Header.MsgId, jT808Package.Header.TerminalPhoneNo);
                if (jT808BodiesImplType != null)
                {
                    if (jT808Package.Header.MessageBodyProperty.IsPackge)
                    {
                        if (jT808Package.Header.MessageBodyProperty.PackageIndex > 1)
                        {
                            try
                            {
                                //4.2处理第二包之后的分包数据消息体
                                jT808Package.Bodies = JT808SplitPackageBodiesFormatter.Instance.Deserialize(ref reader, config);
                            }
                            catch (Exception ex)
                            {
                                throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
                            }
                        }
                        else
                        {
                            try
                            {
                                //4.2.处理消息体
                                jT808Package.Bodies = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(
                                    config.GetMessagePackFormatterByType(jT808BodiesImplType),
                                    ref reader, config);
                            }
                            catch (Exception ex)
                            {
                                throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
                            }
                        }
                    }
                    else
                    {
                        try
                        {
                            //4.2.处理消息体
                            jT808Package.Bodies = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(
                                config.GetMessagePackFormatterByType(jT808BodiesImplType),
                                ref reader, config);
                        }
                        catch (Exception ex)
                        {
                            throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
                        }
                    }
                }
            }
            // 5.读取校验码
            jT808Package.CheckCode = reader.ReadByte();
            // 6.读取终止位置
            jT808Package.End = reader.ReadEnd();
            // ---------------解包完成--------------
            return(jT808Package);
        }
Ejemplo n.º 6
0
        public JT808HeaderPackage(ref JT808MessagePackReader reader, IJT808Config config)
        {
            // 1. 验证校验和
            if (!config.SkipCRCCode)
            {
                if (!reader.CheckXorCodeVali)
                {
                    throw new JT808Exception(JT808ErrorCode.CheckCodeNotEqual, $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}");
                }
            }
            // ---------------开始解包--------------
            // 2.读取起始位置
            this.Begin = reader.ReadStart();
            // 3.读取头部信息
            this.Header = new JT808Header();
            //  3.1.读取消息Id
            this.Header.MsgId = reader.ReadUInt16();
            //  3.2.读取消息体属性
            ushort messageBodyPropertyValue = reader.ReadUInt16();

            //    3.2.1.解包消息体属性
            this.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(messageBodyPropertyValue);
            if (this.Header.MessageBodyProperty.VersionFlag)
            {
                //2019版本
                //  3.3.读取协议版本号
                this.Header.ProtocolVersion = reader.ReadByte();
                //  3.4.读取终端手机号
                this.Header.TerminalPhoneNo = reader.ReadBCD(20, config.Trim);
                reader.Version = JT808Version.JTT2019;
            }
            else
            {
                //2013版本
                //  3.3.读取终端手机号
                this.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim);
            }
            // 3.4.读取消息流水号
            this.Header.MsgNum = reader.ReadUInt16();
            // 3.5.判断有无分包
            if (this.Header.MessageBodyProperty.IsPackage)
            {
                //3.5.1.读取消息包总数
                this.Header.PackgeCount = reader.ReadUInt16();
                //3.5.2.读取消息包序号
                this.Header.PackageIndex = reader.ReadUInt16();
            }
            // 4.处理数据体
            //  4.1.判断有无数据体
            if (this.Header.MessageBodyProperty.DataLength > 0)
            {
                this.Bodies = reader.ReadContent();
            }
            else
            {
                this.Bodies = ReadOnlySpan <byte> .Empty;
            }
            // 5.读取校验码
            this.CheckCode = reader.ReadByte();
            // 6.读取终止位置
            this.End = reader.ReadEnd();
            // ---------------解包完成--------------
            this.OriginalData = reader.SrcBuffer;
        }