Skip to content

Commit

Permalink
增加808接口配置属性是否需要去0判断
Browse files Browse the repository at this point in the history
  • Loading branch information
SmallChi committed Aug 12, 2019
1 parent 8a84da9 commit 973bf71
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/JT808.Protocol/Formatters/JT808HeaderFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public JT808Header Deserialize(ref JT808MessagePackReader reader, IJT808Config c
// 2.消息体属性
jT808Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(reader.ReadUInt16());
// 3.终端手机号
jT808Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength);
jT808Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim);
jT808Header.MsgNum = reader.ReadUInt16();
// 4.判断有无分包
if (jT808Header.MessageBodyProperty.IsPackage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public JT808HeaderPackage Deserialize(ref JT808MessagePackReader reader, IJT808C
// 3.2.1.解包消息体属性
jT808Package.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(messageBodyPropertyValue);
// 3.3.读取终端手机号
jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength);
jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim);
// 3.4.读取消息流水号
jT808Package.Header.MsgNum = reader.ReadUInt16();
// 3.5.判断有无分包
Expand Down
2 changes: 1 addition & 1 deletion src/JT808.Protocol/Formatters/JT808PackageFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public JT808Package Deserialize(ref JT808MessagePackReader reader, IJT808Config
// 3.2.读取消息体属性
jT808Package.Header.MessageBodyProperty=new JT808HeaderMessageBodyProperty(reader.ReadUInt16());
// 3.3.读取终端手机号
jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength);
jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim);
// 3.4.读取消息流水号
jT808Package.Header.MsgNum= reader.ReadUInt16();
// 3.5.判断有无分包
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public JT808_0x0107 Deserialize(ref JT808MessagePackReader reader, IJT808Config
jT808_0X0107.MakerId = reader.ReadString(5);
jT808_0X0107.TerminalModel = reader.ReadString(20);
jT808_0X0107.TerminalId = reader.ReadString(7);
jT808_0X0107.Terminal_SIM_ICCID = reader.ReadBCD(10);
jT808_0X0107.Terminal_SIM_ICCID = reader.ReadBCD(10, config.Trim);
jT808_0X0107.Terminal_Hardware_Version_Length = reader.ReadByte();
jT808_0X0107.Terminal_Hardware_Version_Num = reader.ReadString(jT808_0X0107.Terminal_Hardware_Version_Length);
jT808_0X0107.Terminal_Firmware_Version_Length = reader.ReadByte();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public JT808_0x8103 Deserialize(ref JT808MessagePackReader reader, IJT808Config
}
}
}
catch (Exception ex) { }
catch (Exception ex)
{

}
return jT808_0x8103;
}

Expand Down
2 changes: 2 additions & 0 deletions src/JT808.Protocol/Interfaces/GlobalConfigBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected GlobalConfigBase()
JT808_0X8103_Custom_Factory = new JT808_0x8103_Custom_Factory();
JT808_0X8103_Factory = new JT808_0x8103_Factory();
TerminalPhoneNoLength = 12;
Trim = true;
}
public abstract string ConfigId { get; }
public virtual IJT808MsgSNDistributed MsgSNDistributed { get; set; }
Expand All @@ -37,6 +38,7 @@ protected GlobalConfigBase()
public virtual IJT808_0x8103_Custom_Factory JT808_0X8103_Custom_Factory { get; set; }
public virtual IJT808_0x8103_Factory JT808_0X8103_Factory { get; set; }
public virtual int TerminalPhoneNoLength { get; set; }
public virtual bool Trim { get; set; }
public virtual IJT808Config Register(params Assembly[] externalAssemblies)
{
if (externalAssemblies != null)
Expand Down
6 changes: 6 additions & 0 deletions src/JT808.Protocol/Interfaces/IJT808Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public interface IJT808Config
/// </summary>
bool SkipCRCCode { get; set; }
/// <summary>
/// ReadBCD是否需要去0操作
/// 默认是去0
/// 注意:有时候对协议来说是有意义的0
/// </summary>
bool Trim { get; set; }
/// <summary>
/// 设备终端号(默认12位)
/// </summary>
int TerminalPhoneNoLength { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions src/JT808.Protocol/JT808HeaderMessageBodyProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ public ushort Wrap()
{
// 1.是否分包
int tmpIsPacke = 0;
// 2.是否加密
int tmpEncrypt = 0;
if (IsPackage)
{
tmpIsPacke = 1 << 13;
}
// 2.是否加密
int tmpEncrypt;
// 2.3.数据加密方式
switch (Encrypt)
{
case JT808EncryptMethod.None:
tmpEncrypt = 0;
break;
case JT808EncryptMethod.RSA:
tmpEncrypt=1 << 10;
tmpEncrypt = 1 << 10;
break;
default:
tmpEncrypt = 0;
Expand Down
16 changes: 11 additions & 5 deletions src/JT808.Protocol/MessagePack/JT808MessagePackReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public ref struct JT808MessagePackReader
/// 主要用来一次性读取所有数据体内容操作
/// </summary>
private bool _decoded;
private static byte[] decode7d01 = new byte[] { 0x7d, 0x01 };
private static byte[] decode7d02 = new byte[] { 0x7d, 0x02 };
private static readonly byte[] decode7d01 = new byte[] { 0x7d, 0x01 };
private static readonly byte[] decode7d02 = new byte[] { 0x7d, 0x02 };
/// <summary>
/// 解码(转义还原),计算校验和
/// </summary>
Expand Down Expand Up @@ -308,7 +308,7 @@ public DateTime ReadUTCDateTime()
}
return d;
}
public string ReadBCD(int len)
public string ReadBCD(int len , bool trim = true)
{
int count = len / 2;
var readOnlySpan = GetReadOnlySpan(count);
Expand All @@ -317,8 +317,14 @@ public string ReadBCD(int len)
{
bcdSb.Append(readOnlySpan[i].ToString("X2"));
}
// todo:对于协议来说这个0是有意义的,下个版本在去掉
return bcdSb.ToString().TrimStart('0');
if (trim)
{
return bcdSb.ToString().TrimStart('0');
}
else
{
return bcdSb.ToString();
}
}
private ReadOnlySpan<byte> GetReadOnlySpan(int count)
{
Expand Down

0 comments on commit 973bf71

Please sign in to comment.