Esempio n. 1
0
 public int Serialize(ref byte[] bytes, int offset, JTNE_0x02 value)
 {
     if (value.Values != null && value.Values.Count > 0)
     {
         foreach (var item in value.Values)
         {
             if (JTNE_0x02_Body.TypeCodes.TryGetValue(item.Key, out Type jTNE_0x02_BodyImpl))
             {
                 var bodyImplFormatter = JTNEFormatterExtensions.GetFormatter(jTNE_0x02_BodyImpl);
                 offset = JTNEFormatterResolverExtensions.JTNEDynamicSerialize(bodyImplFormatter, ref bytes, offset, item.Value);
             }
         }
     }
     if (value.CusotmSerializeObjectValues != null && value.CusotmSerializeObjectValues.Count > 0)
     {
         foreach (var item in value.CusotmSerializeObjectValues)
         {
             if (JTNE_0x02_CustomBody.CustomTypeCodes.TryGetValue(item.Key, out Type jTNE_0x02_CustomBodyImpl))
             {
                 var customBodyImplFormatter = JTNEFormatterExtensions.GetFormatter(jTNE_0x02_CustomBodyImpl);
                 offset = JTNEFormatterResolverExtensions.JTNEDynamicSerialize(customBodyImplFormatter, ref bytes, offset, item.Value);
             }
         }
     }
     return(offset);
 }
Esempio n. 2
0
 public int Serialize(ref byte[] bytes, int offset, JTNE_0x82 value)
 {
     offset += JTNEBinaryExtensions.WriteDateTime6Little(bytes, offset, value.ControlTime);
     offset += JTNEBinaryExtensions.WriteByteLittle(bytes, offset, value.ParamID);
     if (JTNE_0x82_Body.JTNE_0x82Method.TryGetValue(value.ParamID, out Type type))
     {
         offset = JTNEFormatterResolverExtensions.JTNEDynamicSerialize(JTNEFormatterExtensions.GetFormatter(type), ref bytes, offset, value.Parameter);
     }
     return(offset);
 }
Esempio n. 3
0
 public int Serialize(ref byte[] bytes, int offset, JTNE_0x81 value)
 {
     offset += JTNEBinaryExtensions.WriteDateTime6Little(bytes, offset, value.OperateTime);
     offset += JTNEBinaryExtensions.WriteByteLittle(bytes, offset, value.ParamNum);
     foreach (var item in value.ParamList)
     {
         offset += JTNEBinaryExtensions.WriteByteLittle(bytes, offset, item.ParamId);
         object obj = JTNEFormatterExtensions.GetFormatter(item.GetType());
         offset = JTNEFormatterResolverExtensions.JTNEDynamicSerialize(obj, ref bytes, offset, item);
     }
     return(offset);
 }
        public int Serialize(ref byte[] bytes, int offset, JTNEPackage value)
        {
            // 1.起始符1
            offset += JTNEBinaryExtensions.WriteByteLittle(bytes, offset, value.BeginFlag1);
            // 2.起始符2
            offset += JTNEBinaryExtensions.WriteByteLittle(bytes, offset, value.BeginFlag2);
            // 3.命令标识
            offset += JTNEBinaryExtensions.WriteByteLittle(bytes, offset, value.MsgId);
            // 4.应答标识
            offset += JTNEBinaryExtensions.WriteByteLittle(bytes, offset, value.AskId);
            // 5.VIN
            offset += JTNEBinaryExtensions.WriteStringPadRightLittle(bytes, offset, value.VIN, 17);
            // 6.数据加密方式
            offset += JTNEBinaryExtensions.WriteByteLittle(bytes, offset, value.EncryptMethod);
            // 7.记录当前偏移量
            int tmpOffset = offset;
            // 8.数据体
            Type jTNEBodiesImplType = JTNEMsgIdFactory.GetBodiesImplTypeByMsgId(value.MsgId);
            int  messageBodyOffset  = 0;

            if (jTNEBodiesImplType != null)
            {
                if (value.Bodies != null)
                {
                    // 8.1.处理数据体
                    // 8.2.判断是否有加密
                    messageBodyOffset = JTNEFormatterResolverExtensions.JTNEDynamicSerialize(
                        JTNEFormatterExtensions.GetFormatter(jTNEBodiesImplType),
                        ref bytes,
                        offset + FixedDataBodyLength,
                        value.Bodies);
                    if (value.EncryptMethod == 0x01)
                    {
                        // 9.通过tmpOffset反写数据单元长度
                        JTNEBinaryExtensions.WriteUInt16Little(bytes, tmpOffset, (ushort)(messageBodyOffset - offset - FixedDataBodyLength));
                        offset = messageBodyOffset;
                    }
                    else
                    {
                        if (JTNEGlobalConfigs.Instance.DataBodiesEncrypt != null)
                        {
                            // 8.1.先进行分割数据体
                            var bodiesData = bytes.AsSpan(tmpOffset + FixedDataBodyLength, messageBodyOffset - offset - FixedDataBodyLength).ToArray();
                            // 8.2.将数据体进行加密
                            var data = JTNEGlobalConfigs.Instance.DataBodiesEncrypt(value.EncryptMethod)
                                       .Encrypt(bodiesData);
                            // 9.通过tmpOffset反写加密后数据单元长度
                            JTNEBinaryExtensions.WriteUInt16Little(bytes, tmpOffset, (ushort)data.Length);
                            // 8.3.写入加密后的数据体
                            offset += FixedDataBodyLength;
                            offset += JTNEBinaryExtensions.WriteBytesLittle(bytes, offset, data);
                        }
                    }
                }
                else
                {
                    // 9.数据单元长度
                    offset += JTNEBinaryExtensions.WriteUInt16Little(bytes, offset, 0);
                }
            }
            else
            {
                // 9.数据单元长度
                offset += JTNEBinaryExtensions.WriteUInt16Little(bytes, offset, 0);
            }
            // 10.校验码
            var bccCode = bytes.ToXor(2, offset);

            offset += JTNEBinaryExtensions.WriteByteLittle(bytes, offset, bccCode);
            return(offset);
        }