From 20ebbf5ab368eabe9d4bf113a6f1d88cef9c2661 Mon Sep 17 00:00:00 2001 From: SmallChi <564952747@qq.com> Date: Mon, 11 Jun 2018 23:01:13 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E6=94=B9=E6=B6=88=E6=81=AF=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=202.=E5=A2=9E=E5=8A=A0=E5=B9=B3=E5=8F=B0=E7=99=BB?= =?UTF-8?q?=E5=85=A5=E7=99=BB=E5=87=BA=E5=91=BD=E4=BB=A4=E5=8F=8A=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEPackageTest.cs | 72 ++++++++++++++- src/GBNewEnergy.Protocol/Enums/NEMsgId.cs | 20 +++-- .../Extensions/NEExtensions.cs | 5 ++ src/GBNewEnergy.Protocol/NEBodies.cs | 15 +++- .../NEProperties/NEPlatformLoginProperty.cs | 27 ++++++ .../NEProperties/NEPlatformLogoutProperty.cs | 16 ++++ .../UpStream/NEPlatformLoginUpStream.cs | 88 +++++++++++++++++++ .../UpStream/NEPlatformLogoutUpStream.cs | 47 ++++++++++ 8 files changed, 282 insertions(+), 8 deletions(-) create mode 100644 src/GBNewEnergy.Protocol/NEProperties/NEPlatformLoginProperty.cs create mode 100644 src/GBNewEnergy.Protocol/NEProperties/NEPlatformLogoutProperty.cs create mode 100644 src/GBNewEnergy.Protocol/UpStream/NEPlatformLoginUpStream.cs create mode 100644 src/GBNewEnergy.Protocol/UpStream/NEPlatformLogoutUpStream.cs diff --git a/src/GBNewEnergy.Protocol.Test/NEPackageTest.cs b/src/GBNewEnergy.Protocol.Test/NEPackageTest.cs index a75962e..6e7997b 100644 --- a/src/GBNewEnergy.Protocol.Test/NEPackageTest.cs +++ b/src/GBNewEnergy.Protocol.Test/NEPackageTest.cs @@ -16,6 +16,7 @@ namespace GBNewEnergy.Protocol.Test { }; + #region 车辆登入 [Fact] public void NELoginUpStreamConstructor4_1() @@ -70,7 +71,7 @@ namespace GBNewEnergy.Protocol.Test } #endregion - #region 车辆登出(依赖车辆登录的流水号所有必须先进行登录产生流水号) + #region 车辆登出(依赖车辆登入的流水号所有必须先进行登入产生流水号) [Fact] public void NELogoutUpStream1_1() @@ -155,5 +156,74 @@ namespace GBNewEnergy.Protocol.Test #endregion + #region 平台登入 + + [Fact] + public void NEPlatformLogin1_1() + { + NEPlatformLoginProperty nEPlatformLoginProperty = new NEPlatformLoginProperty + { + EncryptMethod = NEEncryptMethod.None, + Password = "123456", + UserName = "smallchi", + }; + NEPlatformLoginUpStream nEPlatformLoginUpStream = new NEPlatformLoginUpStream(nEPlatformLoginProperty, NEGlobalConfigs); + + INEProperties nEPackageProperty = new NEPackageProperty + { + Bodies = nEPlatformLoginUpStream, + MsgId = Enums.NEMsgId.platformlogin, + AskId = Enums.NEAskId.cmd, + VIN = "LGHC4V1D3HE202652" + }; + NEPackage nEPackage = new NEPackage(nEPackageProperty, NEGlobalConfigs); + string headerHex = nEPackage.Header.ToHexString(); + string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); + string packageHex = nEPackage.Buffer.ToHexString(); + } + + #endregion + + #region 平台登出(依赖平台登出的流水号所有必须先进行登入产生流水号) + + [Fact] + public void NEPlatformLogin2_1() + { + NEPlatformLoginProperty nEPlatformLoginProperty = new NEPlatformLoginProperty + { + EncryptMethod = NEEncryptMethod.None, + Password = "123456", + UserName = "smallchi", + }; + NEPlatformLoginUpStream nEPlatformLoginUpStream = new NEPlatformLoginUpStream(nEPlatformLoginProperty, NEGlobalConfigs); + INEProperties nEPackageProperty = new NEPackageProperty + { + Bodies = nEPlatformLoginUpStream, + MsgId = Enums.NEMsgId.platformlogin, + AskId = Enums.NEAskId.cmd, + VIN = "LGHC4V1D3HE202652" + }; + NEPackage nEPackage = new NEPackage(nEPackageProperty, NEGlobalConfigs); + + NEPlatformLogoutProperty nEPlatformLogoutProperty = new NEPlatformLogoutProperty + { + UserName = "smallchi", + }; + NEPlatformLoginUpStream nEPlatformLogoutUpStream = new NEPlatformLoginUpStream(nEPlatformLoginProperty, NEGlobalConfigs); + INEProperties logoutNEPackageProperty = new NEPackageProperty + { + Bodies = nEPlatformLogoutUpStream, + MsgId = Enums.NEMsgId.platformlogout, + AskId = Enums.NEAskId.cmd, + VIN = "LGHC4V1D3HE202652" + }; + NEPackage loginNEPackage = new NEPackage(logoutNEPackageProperty, NEGlobalConfigs); + string headerHex = loginNEPackage.Header.ToHexString(); + string bodiesHex = loginNEPackage.Bodies.Buffer.ToHexString(); + string packageHex = loginNEPackage.Buffer.ToHexString(); + } + + + #endregion } } diff --git a/src/GBNewEnergy.Protocol/Enums/NEMsgId.cs b/src/GBNewEnergy.Protocol/Enums/NEMsgId.cs index 60eb234..b4aaa1b 100644 --- a/src/GBNewEnergy.Protocol/Enums/NEMsgId.cs +++ b/src/GBNewEnergy.Protocol/Enums/NEMsgId.cs @@ -18,17 +18,25 @@ namespace GBNewEnergy.Protocol.Enums /// uploadim = 0x02, /// - /// 心跳 - /// - heartbeat = 0x03, - /// /// 补传信息上传 /// - uploadsup = 0x04, + uploadsup = 0x03, /// /// 车辆登出 /// - loginout = 0x05, + loginout = 0x04, + /// + /// 平台登入 + /// + platformlogin = 0x05, + /// + /// 平台登出 + /// + platformlogout = 0x06, + /// + /// 心跳 + /// + heartbeat = 0x07, /// /// 终端校时 /// diff --git a/src/GBNewEnergy.Protocol/Extensions/NEExtensions.cs b/src/GBNewEnergy.Protocol/Extensions/NEExtensions.cs index aba558e..2dc7bd3 100644 --- a/src/GBNewEnergy.Protocol/Extensions/NEExtensions.cs +++ b/src/GBNewEnergy.Protocol/Extensions/NEExtensions.cs @@ -82,6 +82,11 @@ namespace GBNewEnergy.Protocol.Extensions Array.Copy(bytes, 0, write, offset, len); } + public static void WriteLittle(this byte[] write, byte bit, int offset) + { + write[offset] = bit; + } + public static void WriteLittle(this byte[] write, DateTime date, int offset,int len) { write[offset++] = (byte)(date.Year - DateLimitYear); diff --git a/src/GBNewEnergy.Protocol/NEBodies.cs b/src/GBNewEnergy.Protocol/NEBodies.cs index 7c48638..b2a3ff2 100644 --- a/src/GBNewEnergy.Protocol/NEBodies.cs +++ b/src/GBNewEnergy.Protocol/NEBodies.cs @@ -8,14 +8,21 @@ namespace GBNewEnergy.Protocol public abstract class NEBodies : NEBufferedEntityBase { /// - /// VIN - 登录流水号,过期时间(每天置1) + /// VIN - 登入流水号,过期时间(每天置1) /// 车载终端登入一次,登入流水号自动加1,从1开始循环累加,最大值为65531,循环周期为天 /// protected static readonly ConcurrentDictionary LoginNumDict; + /// + /// 平台登入 登录流水号,过期时间(每天置1) + /// 下级平台登入一次,登入流水号自动加1,从1开始循环累加,最大值为65531,循环周期为天 + /// + protected static readonly ConcurrentDictionary PlatformLoginNumDict; + static NEBodies() { LoginNumDict = new ConcurrentDictionary(); + PlatformLoginNumDict = new ConcurrentDictionary(); } /// @@ -39,5 +46,11 @@ namespace GBNewEnergy.Protocol public ushort LoginNum { get; set; } public DateTime ExpirationTime { get; set; } } + + protected class PlatformLoginInfo + { + public ushort LoginNum { get; set; } + public DateTime ExpirationTime { get; set; } + } } } diff --git a/src/GBNewEnergy.Protocol/NEProperties/NEPlatformLoginProperty.cs b/src/GBNewEnergy.Protocol/NEProperties/NEPlatformLoginProperty.cs new file mode 100644 index 0000000..7ba7265 --- /dev/null +++ b/src/GBNewEnergy.Protocol/NEProperties/NEPlatformLoginProperty.cs @@ -0,0 +1,27 @@ +using GBNewEnergy.Protocol.Enums; +using System; +using System.Collections.Generic; +using System.Text; + +namespace GBNewEnergy.Protocol.NEProperties +{ + public class NEPlatformLoginProperty : INEProperties + { + [Obsolete("平台登入无用字段")] + public string VIN { get ; set; } + /// + /// 平台用户名 + /// + public string UserName { get; set; } + + /// + /// 加密规则 + /// + public NEEncryptMethod EncryptMethod { get; set; } + + /// + /// 平台密码 + /// + public string Password { get; set; } + } +} diff --git a/src/GBNewEnergy.Protocol/NEProperties/NEPlatformLogoutProperty.cs b/src/GBNewEnergy.Protocol/NEProperties/NEPlatformLogoutProperty.cs new file mode 100644 index 0000000..b319ab7 --- /dev/null +++ b/src/GBNewEnergy.Protocol/NEProperties/NEPlatformLogoutProperty.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace GBNewEnergy.Protocol.NEProperties +{ + public class NEPlatformLogoutProperty : INEProperties + { + [Obsolete("平台登入无用字段")] + public string VIN { get; set; } + /// + /// 平台用户名 + /// + public string UserName { get; set; } + } +} diff --git a/src/GBNewEnergy.Protocol/UpStream/NEPlatformLoginUpStream.cs b/src/GBNewEnergy.Protocol/UpStream/NEPlatformLoginUpStream.cs new file mode 100644 index 0000000..355ae3d --- /dev/null +++ b/src/GBNewEnergy.Protocol/UpStream/NEPlatformLoginUpStream.cs @@ -0,0 +1,88 @@ +using GBNewEnergy.Protocol.Enums; +using GBNewEnergy.Protocol.Extensions; +using GBNewEnergy.Protocol.NEProperties; +using System; +using System.Collections.Generic; +using System.Text; + +namespace GBNewEnergy.Protocol.UpStream +{ + /// + /// 平台登入 + /// + public class NEPlatformLoginUpStream : NEBodies + { + public NEPlatformLoginUpStream(byte[] buffer, NEGlobalConfigs nEConfigs) : base(buffer, nEConfigs) + { + } + + public NEPlatformLoginUpStream(INEProperties nEProperties, NEGlobalConfigs nEConfigs) : base(nEProperties, nEConfigs) + { + } + + /// + /// 平台用户名 + /// + public string UserName { get; set; } + + /// + /// 加密规则 + /// + public NEEncryptMethod EncryptMethod { get; set; } + + /// + /// 平台密码 + /// + public string Password { get; set; } + + protected override void InitializeProperties(INEProperties nEProperties) + { + NEPlatformLoginProperty nEPlatformLoginProperty = (NEPlatformLoginProperty)nEProperties; + if (PlatformLoginNumDict.ContainsKey(nEPlatformLoginProperty.UserName)) + { + PlatformLoginInfo temp; + if (PlatformLoginNumDict.TryGetValue(nEPlatformLoginProperty.UserName, out temp)) + { + // 不等于当天 + if (temp.ExpirationTime != DateTime.Now.Date) + { + LoginNum = 1; + PlatformLoginNumDict.TryUpdate(nEPlatformLoginProperty.UserName, new PlatformLoginInfo { LoginNum = LoginNum, ExpirationTime = DateTime.Now.Date }, temp); + } + else + {// 自增1 更新字典 + LoginNum = temp.LoginNum++; + PlatformLoginNumDict.TryUpdate(nEPlatformLoginProperty.UserName, new PlatformLoginInfo { LoginNum = LoginNum, ExpirationTime = DateTime.Now.Date }, temp); + } + } + } + else + { + LoginNum = 1; + PlatformLoginNumDict.TryAdd(nEPlatformLoginProperty.UserName, new PlatformLoginInfo { LoginNum = LoginNum, ExpirationTime = DateTime.Now.Date }); + } + UserName = nEPlatformLoginProperty.UserName; + EncryptMethod = nEPlatformLoginProperty.EncryptMethod; + Password = nEPlatformLoginProperty.Password; + } + + protected override void InitializePropertiesFromBuffer() + { + CurrentDateTime = Buffer.ReadDateTimeLittle(0, 6); + LoginNum = Buffer.ReadUShortH2LLittle(6, 2); + UserName = Buffer.ReadStringLittle(6, 12); + Password = Buffer.ReadStringLittle(12, 20); + EncryptMethod= (NEEncryptMethod)Buffer[21]; + } + + protected override void ToBuffer() + { + Buffer = new byte[6+2+12+20+1]; + Buffer.WriteLittle(CurrentDateTime, 0, 6); + Buffer.WriteLittle(LoginNum, 6, 2); + Buffer.WriteLittle(UserName, 12); + Buffer.WriteLittle(Password, 20); + Buffer.WriteLittle((byte)EncryptMethod, 21); + } + } +} diff --git a/src/GBNewEnergy.Protocol/UpStream/NEPlatformLogoutUpStream.cs b/src/GBNewEnergy.Protocol/UpStream/NEPlatformLogoutUpStream.cs new file mode 100644 index 0000000..4323a91 --- /dev/null +++ b/src/GBNewEnergy.Protocol/UpStream/NEPlatformLogoutUpStream.cs @@ -0,0 +1,47 @@ +using GBNewEnergy.Protocol.Exceptions; +using GBNewEnergy.Protocol.Extensions; +using GBNewEnergy.Protocol.NEProperties; + +namespace GBNewEnergy.Protocol.UpStream +{ + /// + /// 平台登出 + /// + public class NEPlatformLogoutUpStream : NEBodies + { + public NEPlatformLogoutUpStream(INEProperties nEProperties, NEGlobalConfigs nEConfigs) : base(nEProperties, nEConfigs) + { + } + + public NEPlatformLogoutUpStream(byte[] buffer, NEGlobalConfigs nEConfigs) : base(buffer, nEConfigs) + { + } + + protected override void InitializeProperties(INEProperties nEProperties) + { + NEPlatformLogoutProperty nEPlatformLogoutProperty = (NEPlatformLogoutProperty)nEProperties; + PlatformLoginInfo temp; + if (PlatformLoginNumDict.TryGetValue(nEPlatformLogoutProperty.UserName, out temp)) + { + LoginNum = temp.LoginNum; + } + else + { + throw new NEException(Enums.NEErrorCode.LoginSerialNoError, "Must Dependency NEPlatformLoginUpStream Class."); + } + } + + 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); + Buffer.WriteLittle(LoginNum, 6, 2); + } + } +}