Ejemplo n.º 1
0
        public JT809_0x9600 Deserialize(ref JT809MessagePackReader reader, IJT809Config config)
        {
            JT809_0x9600 value = new JT809_0x9600();

            value.VehicleNo       = reader.ReadString(21);
            value.VehicleColor    = (JT809VehicleColorType)reader.ReadByte();
            value.SubBusinessType = reader.ReadUInt16();
            value.DataLength      = reader.ReadUInt32();
            try
            {
                if (config.SubBusinessTypeFactory.TryGetValue(value.SubBusinessType, out object instance))
                {
                    if (instance is JT809SubBodies subBodies)
                    {
                        if (!subBodies.SkipSerialization)
                        {
                            value.SubBodies = JT809MessagePackFormatterResolverExtensions.JT809DynamicDeserialize(
                                instance,
                                ref reader, config);
                        }
                    }
                }
            }
            catch
            {
                throw new JT809Exception(JT809ErrorCode.SubBodiesParseError, $"SubBusinessType>{value.SubBusinessType.ToString()}");
            }
            return(value);
        }
Ejemplo n.º 2
0
        public JT809_0x9200 Deserialize(ref JT809MessagePackReader reader, IJT809Config config)
        {
            JT809_0x9200 value = new JT809_0x9200();

            //1078qq群808432702:大兄弟提供的
            //由于车辆注册信息应答消息0x9201子业务不存在车牌号和颜色需要跳过
            if (config.Version == JT809Version.JTT2019)
            {
                var subBusinessType = reader.ReadVirtualUInt16();
                if (subBusinessType != JT809SubBusinessType.车辆注册信息应答消息.ToUInt16Value())
                {
                    value.VehicleNo    = reader.ReadString(21);
                    value.VehicleColor = (JT809VehicleColorType)reader.ReadByte();
                }
            }
            else
            {
                value.VehicleNo    = reader.ReadString(21);
                value.VehicleColor = (JT809VehicleColorType)reader.ReadByte();
            }
            value.SubBusinessType = reader.ReadUInt16();
            value.DataLength      = reader.ReadUInt32();
            try
            {
                if (config.SubBusinessTypeFactory.TryGetValue(value.SubBusinessType, out object instance))
                {
                    if (instance is JT809SubBodies subBodies)
                    {
                        if (!subBodies.SkipSerialization)
                        {
                            value.SubBodies = JT809MessagePackFormatterResolverExtensions.JT809DynamicDeserialize(
                                instance,
                                ref reader, config);
                        }
                    }
                }
            }
            catch
            {
                throw new JT809Exception(JT809ErrorCode.SubBodiesParseError, $"SubBusinessType>{value.SubBusinessType.ToString()}");
            }
            return(value);
        }
Ejemplo n.º 3
0
        public JT808_JT1078_0x1700 Deserialize(ref JT809MessagePackReader reader, IJT809Config config)
        {
            JT808_JT1078_0x1700 jT808_JT1078_0X1700 = new JT808_JT1078_0x1700();

            jT808_JT1078_0X1700.SubBusinessType = reader.ReadUInt16();
            try
            {
                Type jT809SubBodiesImplType = config.SubBusinessTypeFactory.GetSubBodiesImplTypeBySubBusinessType(jT808_JT1078_0X1700.SubBusinessType);
                if (jT809SubBodiesImplType != null)
                {
                    jT808_JT1078_0X1700.SubBodies = JT809MessagePackFormatterResolverExtensions.JT809DynamicDeserialize(
                        config.GetMessagePackFormatterByType(jT809SubBodiesImplType),
                        ref reader, config);
                }
            }
            catch
            {
                throw new JT809Exception(JT809ErrorCode.SubBodiesParseError, $"SubBusinessType>{jT808_JT1078_0X1700.SubBusinessType.ToString()}");
            }
            return(jT808_JT1078_0X1700);
        }
Ejemplo n.º 4
0
        public TJT809Bodies Deserialize(ref JT809MessagePackReader reader, IJT809Config config)
        {
            TJT809Bodies jT809Bodies = new TJT809Bodies();

            jT809Bodies.VehicleNo       = reader.ReadString(21);
            jT809Bodies.VehicleColor    = (JT809VehicleColorType)reader.ReadByte();
            jT809Bodies.SubBusinessType = reader.ReadUInt16();
            jT809Bodies.DataLength      = reader.ReadUInt32();
            try
            {
                Type jT809SubBodiesImplType = config.SubBusinessTypeFactory.GetSubBodiesImplTypeBySubBusinessType(jT809Bodies.SubBusinessType);
                if (jT809SubBodiesImplType != null)
                {
                    jT809Bodies.SubBodies = JT809MessagePackFormatterResolverExtensions.JT809DynamicDeserialize(
                        config.GetMessagePackFormatterByType(jT809SubBodiesImplType),
                        ref reader, config);
                }
            }
            catch
            {
                throw new JT809Exception(JT809ErrorCode.SubBodiesParseError, $"SubBusinessType>{jT809Bodies.SubBusinessType.ToString()}");
            }
            return(jT809Bodies);
        }
Ejemplo n.º 5
0
        public JT809Package Deserialize(ref JT809MessagePackReader reader, IJT809Config config)
        {
            // 1. 验证校验码
            if (!config.SkipCRCCode)
            {
                //  1.2. 验证校验码
                if (!reader.CheckXorCodeVali)
                {
                    throw new JT809Exception(JT809ErrorCode.CRC16CheckInvalid, $"{reader.CalculateCheckXorCode.ToString()}!={reader.RealCheckXorCode.ToString()}");
                }
            }
            JT809Package jT809Package = new JT809Package();

            // 2.读取起始头
            jT809Package.BeginFlag = reader.ReadStart();
            // 3.初始化消息头
            try
            {
                //todo:JT809HeaderFormatter
                //jT809Package.Header = JT809HeaderFormatter.Instance.Deserialize(ref reader, config);
            }
            catch (Exception ex)
            {
                throw new JT809Exception(JT809ErrorCode.HeaderParseError, $"offset>{reader.ReadCurrentRemainContentLength().ToString()}", ex);
            }
            // 5.数据体处理
            //  5.1 判断是否有数据体(总长度-固定长度)> 0
            if ((jT809Package.Header.MsgLength - JT809Package.FixedByteLength) > 0)
            {
                try
                {
                    Type jT809BodiesImplType = config.BusinessTypeFactory.GetBodiesImplTypeByBusinessType(jT809Package.Header.BusinessType, jT809Package.Header.MsgGNSSCENTERID);
                    if (jT809BodiesImplType != null)
                    {
                        // 5.2 是否加密
                        switch (jT809Package.Header.EncryptFlag)
                        {
                        case JT809Header_Encrypt.None:
                            // 5.3 处理消息体
                            jT809Package.Bodies = JT809MessagePackFormatterResolverExtensions.JT809DynamicDeserialize(
                                config.GetMessagePackFormatterByType(jT809BodiesImplType),
                                ref reader, config);
                            break;

                        case JT809Header_Encrypt.Common:
                            // 5.4. 处理加密消息体
                            byte[] bodiesData = config.Encrypt.Decrypt(reader.ReadContent(), config.EncryptOptions, jT809Package.Header.EncryptKey);
                            JT809MessagePackReader bodiesReader = new JT809MessagePackReader(bodiesData);
                            jT809Package.Bodies = JT809MessagePackFormatterResolverExtensions.JT809DynamicDeserialize(
                                config.GetMessagePackFormatterByType(jT809BodiesImplType),
                                ref bodiesReader, config);
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new JT809Exception(JT809ErrorCode.BodiesParseError, $"offset>{reader.ReadCurrentRemainContentLength().ToString()}", ex);
                }
            }
            jT809Package.CRCCode = reader.CalculateCheckXorCode;
            jT809Package.EndFlag = reader.ReadEnd();
            return(jT809Package);
        }
Ejemplo n.º 6
0
        public JT809Package Deserialize(ref JT809MessagePackReader reader, IJT809Config config)
        {
            // 1. 验证校验码
            if (!config.SkipCRCCode)
            {
                //  1.2. 验证校验码
                if (!reader.CheckXorCodeVali)
                {
                    throw new JT809Exception(JT809ErrorCode.CRC16CheckInvalid, $"{reader.CalculateCheckXorCode}!={reader.RealCheckXorCode}");
                }
            }
            JT809Package jT809Package = new JT809Package();

            // 2.读取起始头
            jT809Package.BeginFlag = reader.ReadStart();
            // 3.初始化消息头
            try
            {
                jT809Package.Header = config.GetMessagePackFormatter <JT809Header>().Deserialize(ref reader, config);
            }
            catch (Exception ex)
            {
                throw new JT809Exception(JT809ErrorCode.HeaderParseError, $"offset>{reader.ReadCurrentRemainContentLength()}", ex);
            }
            // 5.数据体处理
            //  5.1 判断是否有数据体(总长度-固定长度)> 0
            int fixedByteLength = config.Version == JT809Version.JTT2019 ? FixedByteLength_2019 : FixedByteLength;

            if ((jT809Package.Header.MsgLength - fixedByteLength) > 0)
            {
                if (config.BusinessTypeFactory.TryGetValue(jT809Package.Header.BusinessType, config.Version, out object instance))
                {
                    try
                    {
                        // 5.2 是否加密
                        switch (jT809Package.Header.EncryptFlag)
                        {
                        case JT809Header_Encrypt.None:
                            // 5.3 处理消息体
                            jT809Package.Bodies = JT809MessagePackFormatterResolverExtensions.JT809DynamicDeserialize(instance, ref reader, config);
                            break;

                        case JT809Header_Encrypt.Common:
                            // 5.4. 处理加密消息体
                            byte[] bodiesData = config.Encrypt.Decrypt(reader.ReadContent(), config.EncryptOptions, jT809Package.Header.EncryptKey);
                            JT809MessagePackReader bodiesReader = new JT809MessagePackReader(bodiesData);
                            jT809Package.Bodies = JT809MessagePackFormatterResolverExtensions.JT809DynamicDeserialize(instance, ref bodiesReader, config);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new JT809Exception(JT809ErrorCode.BodiesParseError, $"offset>{reader.ReadCurrentRemainContentLength().ToString()}", ex);
                    }
                }
            }

            jT809Package.CRCCode = reader.CalculateCheckXorCode;
            jT809Package.EndFlag = reader.ReadEnd();
            return(jT809Package);
        }