@@ -6,6 +6,7 @@ using GBNewEnergy.Protocol.Extensions; | |||
using GBNewEnergy.Protocol.UpStream; | |||
using GBNewEnergy.Protocol.Enums; | |||
using GBNewEnergy.Protocol.Exceptions; | |||
using GBNewEnergy.Protocol.NEProperties; | |||
namespace GBNewEnergy.Protocol.Test | |||
{ | |||
@@ -13,70 +14,53 @@ namespace GBNewEnergy.Protocol.Test | |||
{ | |||
#region 车辆登入 | |||
[Fact] | |||
public void NELoginUpStreamConstructor1_1() | |||
public void NELoginUpStreamConstructor4_1() | |||
{ | |||
// "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F" | |||
// "12 06 07 11 04 1B 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31" | |||
// "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F 12 06 07 11 04 1B 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31 D7" | |||
NELoginUpStream nELoginUpStream = new NELoginUpStream("LGHC4V1D3HE202652", "64743066405", 1, 1, new string[] { "1" }); | |||
NEPackage nEPackage = new NEPackage("LGHC4V1D3HE202652", Enums.MsgId.login, Enums.AskId.cmd, nELoginUpStream, EncryptMethod.None); | |||
// "12 06 08 11 09 14 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31" | |||
// "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F 12 06 08 11 09 14 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31 DA" | |||
INEProperties nELoginProperty = new NELoginProperty | |||
{ | |||
VIN= "LGHC4V1D3HE202652", | |||
BatteryCount=1, | |||
BatteryLength=1, | |||
BatteryNos=new List<string>() { "1" }, | |||
SIM= "64743066405" | |||
}; | |||
NELoginUpStream nELoginUpStream = new NELoginUpStream(nELoginProperty); | |||
INEProperties nEPackageProperty = new NEPackageProperty | |||
{ | |||
Bodies= nELoginUpStream, | |||
MsgId = Enums.NEMsgId.login, | |||
AskId= Enums.NEAskId.cmd, | |||
EncryptMethod= NEEncryptMethod.None, | |||
VIN= "LGHC4V1D3HE202652" | |||
}; | |||
NEPackage nEPackage = new NEPackage(nEPackageProperty); | |||
string headerHex = nEPackage.Header.ToHexString(); | |||
string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); | |||
string packageHex = nEPackage.Buffer.ToHexString(); | |||
} | |||
[Fact] | |||
public void NELoginUpStreamConstructor1_2() | |||
{ | |||
byte[] header = "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F".ToHexBytes(); | |||
byte[] body = "12 06 07 11 04 1B 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31 D7".ToHexBytes(); | |||
NEPackage nEPackage = new NEPackage(header, body); | |||
string headerHex = nEPackage.Header.ToHexString(); | |||
string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); | |||
} | |||
[Fact] | |||
public void NELoginUpStreamConstructor2_1() | |||
public void NELoginUpStreamConstructor4_2() | |||
{ | |||
// "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F" | |||
// "12 06 07 12 23 3B 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31" | |||
// "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F 12 06 07 12 23 3B 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31 D3" | |||
NELoginUpStream nELoginUpStream = new NELoginUpStream("LGHC4V1D3HE202652", "64743066405", 1, 1, new string[] { "1" }); | |||
NEPackage nEPackage = new NEPackage("LGHC4V1D3HE202652", Enums.MsgId.login, Enums.AskId.cmd, nELoginUpStream, EncryptMethod.None); | |||
string headerHex = nEPackage.Header.ToHexString(); | |||
string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); | |||
string packageHex = nEPackage.Buffer.ToHexString(); | |||
} | |||
[Fact] | |||
public void NELoginUpStreamConstructor2_2() | |||
{ | |||
// "12 06 07 11 04 1B 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31" | |||
// "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F 12 06 07 11 04 1B 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31 D7" | |||
byte[] header = "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F".ToHexBytes(); | |||
byte[] body = "12 06 07 12 23 3B 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31 D3".ToHexBytes(); | |||
byte[] body = "12 06 07 11 04 1B 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31 D7".ToHexBytes(); | |||
NEPackage nEPackage = new NEPackage(header, body); | |||
string headerHex = nEPackage.Header.ToHexString(); | |||
string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); | |||
} | |||
[Fact] | |||
public void NELoginUpStreamConstructor3_1() | |||
{ | |||
// "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F" | |||
// "12 06 08 0A 26 2E 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31" | |||
// "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F 12 06 08 0A 26 2E 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31 D4" | |||
NELoginUpStream nELoginUpStream = new NELoginUpStream("LGHC4V1D3HE202652", "64743066405", 1, 1, new string[] { "1" }); | |||
NEPackage nEPackage = new NEPackage("LGHC4V1D3HE202652", Enums.MsgId.login, Enums.AskId.cmd, nELoginUpStream, EncryptMethod.None); | |||
string headerHex = nEPackage.Header.ToHexString(); | |||
string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); | |||
string packageHex = nEPackage.Buffer.ToHexString(); | |||
} | |||
[Fact] | |||
public void NELoginUpStreamConstructor3_2() | |||
public void NELoginUpStreamConstructor4_3() | |||
{ | |||
byte[] header = "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F".ToHexBytes(); | |||
byte[] body = "12 06 08 0A 26 2E 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31 D4".ToHexBytes(); | |||
NEPackage nEPackage = new NEPackage(header, body); | |||
// "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F 12 06 07 11 04 1B 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31 D7" | |||
byte[] packageBytes = "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F 12 06 07 11 04 1B 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31 D7".ToHexBytes(); | |||
NEPackage nEPackage = new NEPackage(packageBytes); | |||
string headerHex = nEPackage.Header.ToHexString(); | |||
string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); | |||
string packageHex = nEPackage.Buffer.ToHexString(); | |||
@@ -90,43 +74,83 @@ namespace GBNewEnergy.Protocol.Test | |||
{ | |||
try | |||
{ | |||
NELoginUpStream nELoginUpStream = new NELoginUpStream("LGHC4V1D3HE202652", "64743066405", 1, 1, new string[] { "1" }); | |||
NELogoutUpStream eLogoutUpStream = new NELogoutUpStream("LGHC4V1D3HE202652"); | |||
NEPackage nEPackage = new NEPackage("LGHC4V1D3HE202652", Enums.MsgId.loginout, Enums.AskId.cmd, eLogoutUpStream, EncryptMethod.None); | |||
INEProperties nELogoutProperty = new NELogoutProperty() | |||
{ | |||
VIN = "LGHC4V1D3HE202652" | |||
}; | |||
NELogoutUpStream nELogoutUpStream1 = new NELogoutUpStream(nELogoutProperty); | |||
INEProperties nEPackageProperty1 = new NEPackageProperty | |||
{ | |||
Bodies = nELogoutUpStream1, | |||
MsgId = Enums.NEMsgId.login, | |||
AskId = Enums.NEAskId.cmd, | |||
EncryptMethod = NEEncryptMethod.None, | |||
VIN = "LGHC4V1D3HE202652" | |||
}; | |||
NEPackage nEPackage = new NEPackage(nEPackageProperty1); | |||
string headerHex = nEPackage.Header.ToHexString(); | |||
string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); | |||
string packageHex = nEPackage.Buffer.ToHexString(); | |||
} | |||
catch (NEException ex) | |||
{ | |||
Assert.Equal(ErrorCode.LoginSerialNoError, ex.ErrorCode); | |||
Assert.Equal(NEErrorCode.LoginSerialNoError, ex.ErrorCode); | |||
} | |||
} | |||
[Fact] | |||
public void NELogoutUpStream2_1() | |||
{ | |||
// 先进行车辆登入 | |||
NELoginUpStream nELoginUpStream = new NELoginUpStream("LGHC4V1D3HE202652", "64743066405", 1, 1, new string[] { "1" }); | |||
INEProperties nELoginProperty = new NELoginProperty | |||
{ | |||
VIN = "LGHC4V1D3HE202652", | |||
BatteryCount = 1, | |||
BatteryLength = 1, | |||
BatteryNos = new List<string>() { "1" }, | |||
SIM = "64743066405" | |||
}; | |||
NELoginUpStream nELoginUpStream = new NELoginUpStream(nELoginProperty); | |||
INEProperties nEPackageProperty = new NEPackageProperty | |||
{ | |||
Bodies = nELoginUpStream, | |||
MsgId = Enums.NEMsgId.login, | |||
AskId = Enums.NEAskId.cmd, | |||
EncryptMethod = NEEncryptMethod.None, | |||
VIN = "LGHC4V1D3HE202652" | |||
}; | |||
NEPackage nEPackageLogin = new NEPackage(nEPackageProperty); | |||
INEProperties nELogoutProperty = new NELogoutProperty() | |||
{ | |||
VIN = "LGHC4V1D3HE202652" | |||
}; | |||
NELogoutUpStream nELogoutUpStream1 = new NELogoutUpStream(nELogoutProperty); | |||
INEProperties nEPackageProperty1 = new NEPackageProperty | |||
{ | |||
Bodies = nELogoutUpStream1, | |||
MsgId = Enums.NEMsgId.loginout, | |||
AskId = Enums.NEAskId.cmd, | |||
EncryptMethod = NEEncryptMethod.None, | |||
VIN = "LGHC4V1D3HE202652" | |||
}; | |||
NEPackage nEPackage = new NEPackage(nEPackageProperty1); | |||
// "23 23 05 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 08" | |||
// "12 06 08 0B 2B 0A 00 01" | |||
// "23 23 05 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 08 12 06 08 0B 2B 0A 00 01 ED" | |||
// 车辆登出 | |||
NELogoutUpStream eLogoutUpStream = new NELogoutUpStream("LGHC4V1D3HE202652"); | |||
NEPackage nEPackage = new NEPackage("LGHC4V1D3HE202652", Enums.MsgId.loginout, Enums.AskId.cmd, eLogoutUpStream, EncryptMethod.None); | |||
string headerHex = nEPackage.Header.ToHexString(); | |||
string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); | |||
string packageHex = nEPackage.Buffer.ToHexString(); | |||
// "12 06 08 12 06 3A 00 01" | |||
// "23 23 05 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 08 12 06 08 12 06 3A 00 01 E9" | |||
string headerHex = nEPackage.Header.ToHexString(); | |||
string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); | |||
string packageHex = nEPackage.Buffer.ToHexString(); | |||
} | |||
[Fact] | |||
public void NELogoutUpStream2_2() | |||
{ | |||
byte[] header = "23 23 05 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 08".ToHexBytes(); | |||
byte[] body = "12 06 08 0B 2B 0A 00 01 ED".ToHexBytes(); | |||
NEPackage nEPackage = new NEPackage(header, body); | |||
byte[] packageBytes = "23 23 05 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 08 12 06 08 12 06 3A 00 01 E9".ToHexBytes(); | |||
NEPackage nEPackage = new NEPackage(packageBytes); | |||
string headerHex = nEPackage.Header.ToHexString(); | |||
string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); | |||
string packageHex = nEPackage.Buffer.ToHexString(); | |||
Assert.Equal("23 23 05 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 08", headerHex); | |||
Assert.Equal("12 06 08 0B 2B 0A 00 01 ED", bodiesHex); | |||
Assert.Equal("12 06 08 12 06 3A 00 01 E9", bodiesHex); | |||
} | |||
#endregion | |||
@@ -1,25 +0,0 @@ | |||
using System; | |||
using System.IO; | |||
namespace GBNewEnergy.Protocol | |||
{ | |||
public abstract class BufferedEntityBase : IBuffer, IBuffered | |||
{ | |||
public byte[] Buffer { get; protected set; } | |||
protected BufferedEntityBase(byte[] buffer) | |||
{ | |||
Buffer = buffer; | |||
} | |||
protected BufferedEntityBase(params object[] parameter) | |||
{ | |||
} | |||
public byte[] ToBuffer() | |||
{ | |||
throw new NotImplementedException(); | |||
} | |||
} | |||
} |
@@ -7,7 +7,7 @@ namespace GBNewEnergy.Protocol.Enums | |||
/// <summary> | |||
/// 应答标志 | |||
/// </summary> | |||
public enum AskId:byte | |||
public enum NEAskId:byte | |||
{ | |||
/// <summary> | |||
/// 接收到的信息正确 |
@@ -8,7 +8,7 @@ namespace GBNewEnergy.Protocol.Enums | |||
/// 数据单元加密方式 | |||
/// 0x01:数据不加密;0x02:数据经过 RSA 算法加密;0x03:数据经过 AES128 位算法加密;“0xFE”表示异常,“0xFF”表示无效 | |||
/// </summary> | |||
public enum EncryptMethod:byte | |||
public enum NEEncryptMethod:byte | |||
{ | |||
None= 0x01, | |||
RSA= 0x02, |
@@ -4,7 +4,7 @@ using System.Text; | |||
namespace GBNewEnergy.Protocol.Enums | |||
{ | |||
public enum ErrorCode | |||
public enum NEErrorCode | |||
{ | |||
/// <summary> | |||
/// 开始标识错误 |
@@ -7,7 +7,7 @@ namespace GBNewEnergy.Protocol.Enums | |||
/// <summary> | |||
/// 命令单元 | |||
/// </summary> | |||
public enum MsgId:byte | |||
public enum NEMsgId:byte | |||
{ | |||
/// <summary> | |||
/// 车辆登入 |
@@ -7,17 +7,17 @@ namespace GBNewEnergy.Protocol.Exceptions | |||
{ | |||
public class NEException:Exception | |||
{ | |||
public NEException(ErrorCode errorCode) : base(errorCode.ToString()) | |||
public NEException(NEErrorCode errorCode) : base(errorCode.ToString()) | |||
{ | |||
this.ErrorCode = errorCode; | |||
} | |||
public NEException(ErrorCode errorCode, string message) : base(message) | |||
public NEException(NEErrorCode errorCode, string message) : base(message) | |||
{ | |||
this.ErrorCode = errorCode; | |||
} | |||
public ErrorCode ErrorCode { get; } | |||
public NEErrorCode ErrorCode { get; } | |||
} | |||
} |
@@ -84,12 +84,32 @@ namespace GBNewEnergy.Protocol.Extensions | |||
public static void WriteLittle(this byte[] write, DateTime date, int offset,int len) | |||
{ | |||
write[offset] = (byte)(date.Year - DateLimitYear); | |||
write[++offset] = (byte)date.Month; | |||
write[++offset] = (byte)date.Day; | |||
write[++offset] = (byte)date.Hour; | |||
write[++offset] = (byte)date.Minute; | |||
write[++offset] = (byte)date.Second; | |||
write[offset++] = (byte)(date.Year - DateLimitYear); | |||
write[offset++] = (byte)date.Month; | |||
write[offset++] = (byte)date.Day; | |||
write[offset++] = (byte)date.Hour; | |||
write[offset++] = (byte)date.Minute; | |||
write[offset++] = (byte)date.Second; | |||
} | |||
public static void WriteLittle(this BinaryWriter write, string str,Encoding coding) | |||
{ | |||
write.Write(coding.GetBytes(str)); | |||
} | |||
public static void WriteLittle(this BinaryWriter write, string str) | |||
{ | |||
write.Write(Encoding.ASCII.GetBytes(str)); | |||
} | |||
public static void WriteLittle(this BinaryWriter write, int data, int len) | |||
{ | |||
int n = 1; | |||
for (int i = 0; i < len; i++) | |||
{ | |||
write.Write((byte)(data >> 8 * (len - n))); | |||
n++; | |||
} | |||
} | |||
/// <summary> | |||
@@ -5,10 +5,17 @@ | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<Compile Remove="BufferedEntityBase.cs" /> | |||
<Compile Remove="DownStream\NELoginDownStream.cs" /> | |||
<Compile Remove="IBuffer.cs" /> | |||
<Compile Remove="IBuffered.cs" /> | |||
<Compile Remove="NEDownStreamBase.cs" /> | |||
<Compile Remove="NEHeader.cs" /> | |||
<Compile Remove="NEUpStreamBase.cs" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<Folder Include="DownStream\" /> | |||
<Folder Include="Properties\" /> | |||
</ItemGroup> | |||
</Project> |
@@ -0,0 +1,14 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace GBNewEnergy.Protocol | |||
{ | |||
public interface INEParameter | |||
{ | |||
/// <summary> | |||
/// 车架号 | |||
/// </summary> | |||
string VIN { get; set; } | |||
} | |||
} |
@@ -0,0 +1,10 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace GBNewEnergy.Protocol | |||
{ | |||
public interface INEProperties:INEParameter | |||
{ | |||
} | |||
} |
@@ -5,7 +5,7 @@ using System.Text; | |||
namespace GBNewEnergy.Protocol | |||
{ | |||
public abstract class NEBodies : IBuffer, IBuffered | |||
public abstract class NEBodies : NEBufferedEntityBase | |||
{ | |||
/// <summary> | |||
/// VIN - 登录流水号,过期时间(每天置1) | |||
@@ -28,20 +28,10 @@ namespace GBNewEnergy.Protocol | |||
/// 数据采集时间 | |||
/// 采用北京时间 | |||
/// </summary> | |||
public DateTime CurrentDateTime { get; protected set; } | |||
public DateTime CurrentDateTime { get; protected set; } = DateTime.Now; | |||
public byte[] Buffer { get; protected set; } | |||
protected NEBodies(byte[] buffer):base(buffer){} | |||
protected NEBodies(byte[] buffer) | |||
{ | |||
Buffer = buffer; | |||
} | |||
protected NEBodies(string vin) | |||
{ | |||
CurrentDateTime = DateTime.Now; | |||
} | |||
public abstract void ToBuffer(); | |||
protected NEBodies(INEProperties nEProperties) : base(nEProperties){} | |||
} | |||
} |
@@ -14,13 +14,13 @@ namespace GBNewEnergy.Protocol | |||
/// <param name="msgId"></param> | |||
/// <param name="buf"></param> | |||
/// <returns></returns> | |||
public static NEBodies GetNEBodiesByMsgId(MsgId msgId,byte[] buf) | |||
public static NEBodies GetNEBodiesByMsgId(NEMsgId msgId,byte[] buf) | |||
{ | |||
switch (msgId) | |||
{ | |||
case MsgId.login: | |||
case NEMsgId.login: | |||
return new NELoginUpStream(buf); | |||
case MsgId.loginout: | |||
case NEMsgId.loginout: | |||
return new NELogoutUpStream(buf); | |||
default: | |||
return null; | |||
@@ -0,0 +1,36 @@ | |||
using System; | |||
using System.IO; | |||
namespace GBNewEnergy.Protocol | |||
{ | |||
public abstract class NEBufferedEntityBase | |||
{ | |||
public byte[] Buffer { get; protected set; } | |||
protected NEBufferedEntityBase(byte[] buffer) | |||
{ | |||
Buffer = buffer; | |||
InitializePropertiesFromBuffer(); | |||
} | |||
protected NEBufferedEntityBase(byte[] headerBuffer, byte[] bodyBuffer) | |||
{ | |||
Buffer = new byte[headerBuffer.Length + bodyBuffer.Length]; | |||
Array.Copy(headerBuffer, 0, Buffer, 0, headerBuffer.Length); | |||
Array.Copy(bodyBuffer, 0, Buffer, headerBuffer.Length, bodyBuffer.Length); | |||
InitializePropertiesFromBuffer(); | |||
} | |||
protected NEBufferedEntityBase(INEProperties nEProperties) | |||
{ | |||
InitializeProperties(nEProperties); | |||
ToBuffer(); | |||
} | |||
protected abstract void InitializeProperties(INEProperties nEProperties); | |||
protected abstract void ToBuffer(); | |||
protected abstract void InitializePropertiesFromBuffer(); | |||
} | |||
} |
@@ -1,74 +1,28 @@ | |||
using GBNewEnergy.Protocol.Enums; | |||
using GBNewEnergy.Protocol.Exceptions; | |||
using GBNewEnergy.Protocol.Extensions; | |||
using GBNewEnergy.Protocol.NEProperties; | |||
using System; | |||
using System.IO; | |||
namespace GBNewEnergy.Protocol | |||
{ | |||
/// <summary> | |||
/// 新能源包 | |||
/// </summary> | |||
public class NEPackage:IBuffer | |||
public class NEPackage : NEBufferedEntityBase | |||
{ | |||
public NEPackage(byte[] header,byte[] body) | |||
public NEPackage(byte[] header, byte[] body) : base(header, body) | |||
{ | |||
// 判断头部异常 | |||
if (header[0] != BeginFlag && header[1] == BeginFlag) throw new NEException(ErrorCode.BeginFlagError, $"{header[0]},{header[1]}"); | |||
// 组包 | |||
byte[] packageBuffer = new byte[header.Length + body.Length]; | |||
Array.Copy(header, 0, packageBuffer, 0, header.Length); | |||
Array.Copy(body, 0, packageBuffer, header.Length, body.Length); | |||
// 获取数据单元长度 | |||
DataUnitLength = header.ReadUShortH2LLittle(22, 2); | |||
// 进行BCC校验码 | |||
// 校验位=报文长度 - 最后一位(校验位) - 偏移量(2) | |||
int checkBit = packageBuffer.Length - 1 - 2; | |||
byte bCCCode = packageBuffer.ToXor(2, checkBit); | |||
byte bCCCode2 = body[body.Length - 1]; | |||
if (bCCCode != bCCCode2) throw new NEException(ErrorCode.BCCCodeError, $"request:{bCCCode2}!=calculate:{bCCCode}"); | |||
MsgId = (MsgId)header[2]; | |||
AskId = (AskId)header[3]; | |||
VIN = header.ReadStringLittle(4, 17); | |||
EncryptMethod = (EncryptMethod)header[21]; | |||
// 通过命令id获取数据体 | |||
Bodies = NEBodiesFactory.GetNEBodiesByMsgId(MsgId, body); | |||
Buffer = packageBuffer; | |||
Header = header; | |||
} | |||
public NEPackage(byte[] buf) | |||
public NEPackage(byte[] buf) : base(buf) | |||
{ | |||
if (buf[0] != BeginFlag && buf[1] == BeginFlag) throw new NEException(ErrorCode.BeginFlagError, $"{buf[0]},{buf[1]}"); | |||
MsgId = (MsgId)buf[2]; | |||
AskId = (AskId)buf[3]; | |||
VIN = buf.ReadStringLittle(4, 17); | |||
EncryptMethod = (EncryptMethod)buf[21]; | |||
DataUnitLength = buf.ReadUShortH2LLittle(22, 2); | |||
// 进行BCC校验码 | |||
// 校验位 = 报文长度 - 最后一位(校验位) - 偏移量(2) | |||
int checkBit = buf.Length - CheckBit - 2; | |||
byte bCCCode = buf.ToXor(2, checkBit); | |||
byte bCCCode2 = buf[buf.Length - CheckBit]; | |||
if (bCCCode != bCCCode2) | |||
{ | |||
throw new NEException(ErrorCode.BCCCodeError, $"request:{bCCCode2}!=calculate:{bCCCode}"); | |||
} | |||
Bodies = NEBodiesFactory.GetNEBodiesByMsgId(MsgId, buf); | |||
Buffer = new byte[buf.Length + Bodies.Buffer.Length]; | |||
Array.Copy(buf, 0, Buffer, 0, buf.Length); | |||
Array.Copy(Bodies.Buffer, 0, Buffer, buf.Length, Bodies.Buffer.Length); | |||
} | |||
public NEPackage(string vin, MsgId msgId, AskId askId, NEBodies bodies,EncryptMethod encryptMethod) | |||
{ | |||
MsgId = msgId; | |||
AskId = askId; | |||
VIN = vin; | |||
EncryptMethod = encryptMethod; | |||
Bodies = bodies; | |||
ToBuffer(); | |||
} | |||
public NEPackage(INEProperties nEProperties) | |||
: base(nEProperties) | |||
{ } | |||
/// <summary> | |||
/// 固定为24个字节长度 | |||
@@ -85,11 +39,11 @@ namespace GBNewEnergy.Protocol | |||
/// <summary> | |||
/// 命令标识 | |||
/// </summary> | |||
public MsgId MsgId { get; private set; } | |||
public NEMsgId MsgId { get; private set; } | |||
/// <summary> | |||
/// 应答标志 | |||
/// </summary> | |||
public AskId AskId { get; private set; } | |||
public NEAskId AskId { get; private set; } | |||
/// <summary> | |||
/// 车辆识别码 | |||
/// </summary> | |||
@@ -98,7 +52,7 @@ namespace GBNewEnergy.Protocol | |||
/// 数据加密方式 | |||
/// 0x01:数据不加密;0x02:数据经过 RSA 算法加密;0x03:数据经过 AES128 位算法加密;“0xFE”表示异常,“0xFF”表示无效 | |||
/// </summary> | |||
public EncryptMethod EncryptMethod { get; private set; } | |||
public NEEncryptMethod EncryptMethod { get; private set; } | |||
/// <summary> | |||
/// 数据单元长度是数据单元的总字节数,有效值范围:0-65531 | |||
/// </summary> | |||
@@ -115,11 +69,9 @@ namespace GBNewEnergy.Protocol | |||
/// <summary> | |||
/// 数据体 | |||
/// </summary> | |||
public NEBodies Bodies { get;protected set; } | |||
public NEBodies Bodies { get; protected set; } | |||
public byte[] Buffer { get; private set; } | |||
private void ToBuffer() | |||
protected override void ToBuffer() | |||
{ | |||
// 固定单元长度 | |||
DataUnitLength = Bodies.Buffer.Length; | |||
@@ -137,5 +89,40 @@ namespace GBNewEnergy.Protocol | |||
Header = new byte[HeaderFixedByteLength]; | |||
Array.Copy(Buffer, 0, Header, 0, HeaderFixedByteLength); | |||
} | |||
protected override void InitializeProperties(INEProperties nEProperties) | |||
{ | |||
NEPackageProperty nEPackageProperty = (NEPackageProperty)nEProperties; | |||
VIN = nEPackageProperty.VIN; | |||
MsgId = nEPackageProperty.MsgId; | |||
AskId = nEPackageProperty.AskId; | |||
Bodies = nEPackageProperty.Bodies; | |||
EncryptMethod = nEPackageProperty.EncryptMethod; | |||
} | |||
protected override void InitializePropertiesFromBuffer() | |||
{ | |||
if (Buffer[0] != BeginFlag && Buffer[1] == BeginFlag) throw new NEException(NEErrorCode.BeginFlagError, $"{Buffer[0]},{Buffer[1]}"); | |||
MsgId = (NEMsgId)Buffer[2]; | |||
AskId = (NEAskId)Buffer[3]; | |||
VIN = Buffer.ReadStringLittle(4, 17); | |||
EncryptMethod = (NEEncryptMethod)Buffer[21]; | |||
DataUnitLength = Buffer.ReadUShortH2LLittle(22, 2); | |||
// 进行BCC校验码 | |||
// 校验位 = 报文长度 - 最后一位(校验位) - 偏移量(2) | |||
int checkBit = Buffer.Length - CheckBit - 2; | |||
byte bCCCode = Buffer.ToXor(2, checkBit); | |||
byte bCCCode2 = Buffer[Buffer.Length - CheckBit]; | |||
if (bCCCode != bCCCode2) | |||
{ | |||
throw new NEException(NEErrorCode.BCCCodeError, $"request:{bCCCode2}!=calculate:{bCCCode}"); | |||
} | |||
BCCCode = bCCCode2; | |||
byte[] bodiesBytes = new byte[DataUnitLength + CheckBit]; | |||
Array.Copy(Buffer, HeaderFixedByteLength, bodiesBytes, 0, bodiesBytes.Length); | |||
Bodies = NEBodiesFactory.GetNEBodiesByMsgId(MsgId, bodiesBytes); | |||
Header = new byte[HeaderFixedByteLength]; | |||
Array.Copy(Buffer, 0, Header, 0, HeaderFixedByteLength); | |||
} | |||
} | |||
} |
@@ -0,0 +1,19 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace GBNewEnergy.Protocol.NEProperties | |||
{ | |||
public class NELoginProperty : INEProperties | |||
{ | |||
public string VIN { get; set; } | |||
public string SIM { get; set; } | |||
public byte BatteryCount { get; set; } | |||
public byte BatteryLength { get; set; } | |||
public IEnumerable<string> BatteryNos { get; set;} | |||
} | |||
} |
@@ -0,0 +1,11 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace GBNewEnergy.Protocol.NEProperties | |||
{ | |||
public class NELogoutProperty : INEProperties | |||
{ | |||
public string VIN { get; set; } | |||
} | |||
} |
@@ -0,0 +1,16 @@ | |||
using GBNewEnergy.Protocol.Enums; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace GBNewEnergy.Protocol.NEProperties | |||
{ | |||
public class NEPackageProperty : INEProperties | |||
{ | |||
public string VIN { get; set; } | |||
public NEMsgId MsgId { get; set; } | |||
public NEAskId AskId { get; set; } | |||
public NEBodies Bodies { get; set; } | |||
public NEEncryptMethod EncryptMethod { get; set; } | |||
} | |||
} |
@@ -1,6 +1,8 @@ | |||
using GBNewEnergy.Protocol.Extensions; | |||
using GBNewEnergy.Protocol.NEProperties; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.IO; | |||
using System.Text; | |||
namespace GBNewEnergy.Protocol.UpStream | |||
@@ -12,49 +14,11 @@ namespace GBNewEnergy.Protocol.UpStream | |||
{ | |||
public NELoginUpStream(byte[] buffer) : base(buffer) | |||
{ | |||
CurrentDateTime = buffer.ReadDateTimeLittle(0, 6); | |||
LoginNum = buffer.ReadUShortH2LLittle(6, 2); | |||
SIM = buffer.ReadStringLittle(8, 20); | |||
BatteryCount = buffer[28]; | |||
BatteryLength = buffer[29]; | |||
List<string> batteryNos = new List<string>(); | |||
for (int i = 0; i < BatteryCount; i++) | |||
{ | |||
batteryNos.Add(buffer.ReadStringLittle(i * BatteryLength + 30, BatteryLength)); | |||
} | |||
BatteryNos = batteryNos; | |||
} | |||
public NELoginUpStream(string vin, string sim, byte batteryCount, byte batteryLength, IEnumerable<string> batteryNos) : base(vin) | |||
public NELoginUpStream(INEProperties nEProperties) : base(nEProperties) | |||
{ | |||
if (LoginNumDict.ContainsKey(vin)) | |||
{ | |||
(ushort LoginNum, DateTime ExpirationTime) temp; | |||
if (LoginNumDict.TryGetValue(vin, out temp)) | |||
{ | |||
// 不等于当天 | |||
if (temp.ExpirationTime != DateTime.Now.Date) | |||
{ | |||
LoginNum = 1; | |||
LoginNumDict.TryUpdate(vin, (LoginNum, DateTime.Now.Date), temp); | |||
} | |||
else | |||
{// 自增1 更新字典 | |||
LoginNum = temp.LoginNum++; | |||
LoginNumDict.TryUpdate(vin, (LoginNum, DateTime.Now.Date), temp); | |||
} | |||
} | |||
} | |||
else | |||
{ | |||
LoginNum = 1; | |||
LoginNumDict.TryAdd(vin, (LoginNum, DateTime.Now.Date)); | |||
} | |||
SIM = sim; | |||
BatteryCount = batteryCount; | |||
BatteryLength = batteryLength; | |||
BatteryNos = batteryNos; | |||
ToBuffer(); | |||
} | |||
/// <summary> | |||
@@ -77,7 +41,7 @@ namespace GBNewEnergy.Protocol.UpStream | |||
/// </summary> | |||
public IEnumerable<string> BatteryNos { get; set; } | |||
public override void ToBuffer() | |||
protected override void ToBuffer() | |||
{ | |||
// 根据协议说明书 | |||
Buffer = new byte[6 + 2 + 20 + 1 + 1 + (BatteryCount * BatteryLength)]; | |||
@@ -92,5 +56,52 @@ namespace GBNewEnergy.Protocol.UpStream | |||
Buffer.WriteLittle(str, 30); | |||
} | |||
} | |||
protected override void InitializeProperties(INEProperties nEProperties) | |||
{ | |||
NELoginProperty nELoginProperty = (NELoginProperty)nEProperties; | |||
if (LoginNumDict.ContainsKey(nELoginProperty.VIN)) | |||
{ | |||
(ushort LoginNum, DateTime ExpirationTime) temp; | |||
if (LoginNumDict.TryGetValue(nELoginProperty.VIN, out temp)) | |||
{ | |||
// 不等于当天 | |||
if (temp.ExpirationTime != DateTime.Now.Date) | |||
{ | |||
LoginNum = 1; | |||
LoginNumDict.TryUpdate(nELoginProperty.VIN, (LoginNum, DateTime.Now.Date), temp); | |||
} | |||
else | |||
{// 自增1 更新字典 | |||
LoginNum = temp.LoginNum++; | |||
LoginNumDict.TryUpdate(nELoginProperty.VIN, (LoginNum, DateTime.Now.Date), temp); | |||
} | |||
} | |||
} | |||
else | |||
{ | |||
LoginNum = 1; | |||
LoginNumDict.TryAdd(nELoginProperty.VIN, (LoginNum, DateTime.Now.Date)); | |||
} | |||
SIM = nELoginProperty.SIM; | |||
BatteryCount = nELoginProperty.BatteryCount; | |||
BatteryLength = nELoginProperty.BatteryLength; | |||
BatteryNos = nELoginProperty.BatteryNos; | |||
} | |||
protected override void InitializePropertiesFromBuffer() | |||
{ | |||
CurrentDateTime = Buffer.ReadDateTimeLittle(0, 6); | |||
LoginNum = Buffer.ReadUShortH2LLittle(6, 2); | |||
SIM = Buffer.ReadStringLittle(8, 20); | |||
BatteryCount = Buffer[28]; | |||
BatteryLength = Buffer[29]; | |||
List<string> batteryNos = new List<string>(); | |||
for (int i = 0; i < BatteryCount; i++) | |||
{ | |||
batteryNos.Add(Buffer.ReadStringLittle(i * BatteryLength + 30, BatteryLength)); | |||
} | |||
BatteryNos = batteryNos; | |||
} | |||
} | |||
} |
@@ -3,35 +3,41 @@ using GBNewEnergy.Protocol.Extensions; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
using GBNewEnergy.Protocol.NEProperties; | |||
namespace GBNewEnergy.Protocol.UpStream | |||
{ | |||
/// <summary> | |||
/// 车辆登入 | |||
/// 车辆登出 | |||
/// </summary> | |||
public class NELogoutUpStream : NEBodies | |||
{ | |||
public NELogoutUpStream(byte[] buffer) : base(buffer) | |||
{ | |||
CurrentDateTime = buffer.ReadDateTimeLittle(0, 6); | |||
LoginNum = buffer.ReadUShortH2LLittle(6, 2); | |||
} | |||
{} | |||
public NELogoutUpStream(INEProperties nEProperties) : base(nEProperties){} | |||
public NELogoutUpStream(string vin) : base(vin) | |||
protected override void InitializeProperties(INEProperties nEProperties) | |||
{ | |||
NELogoutProperty nELogoutProperty = (NELogoutProperty)nEProperties; | |||
(ushort LoginNum, DateTime ExpirationTime) temp; | |||
if (LoginNumDict.TryGetValue(vin, out temp)) | |||
if (LoginNumDict.TryGetValue(nELogoutProperty.VIN, out temp)) | |||
{ | |||
LoginNum = temp.LoginNum; | |||
} | |||
else | |||
{ | |||
throw new NEException(Enums.ErrorCode.LoginSerialNoError, "Must Dependency NELoginUpStream Class."); | |||
throw new NEException(Enums.NEErrorCode.LoginSerialNoError, "Must Dependency NELoginUpStream Class."); | |||
} | |||
ToBuffer(); | |||
} | |||
public override void ToBuffer() | |||
protected override void InitializePropertiesFromBuffer() | |||
{ | |||
CurrentDateTime = Buffer.ReadDateTimeLittle(0, 6); | |||
LoginNum = Buffer.ReadUShortH2LLittle(6, 2); | |||
} | |||
protected override void ToBuffer() | |||
{ | |||
Buffer = new byte[8]; | |||
Buffer.WriteLittle(CurrentDateTime, 0, 6); | |||