From 90f6383675a32c7b743f0bb02ece904fb2ef7134 Mon Sep 17 00:00:00 2001 From: SmallChi <564952747@qq.com> Date: Fri, 8 Jun 2018 18:15:08 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E6=94=B9=E8=BD=A6=E8=BE=86=E7=99=BB?= =?UTF-8?q?=E5=85=A5=E7=99=BB=E5=87=BA=E5=8F=8A=E6=B5=8B=E8=AF=95=202.?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=85=AC=E5=85=B1=E5=B1=9E=E6=80=A7=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=203.=E4=BF=AE=E6=94=B9=E5=BC=BA=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEPackageTest.cs | 154 ++++++++++-------- .../BufferedEntityBase.cs | 25 --- .../Enums/{AskId.cs => NEAskId.cs} | 2 +- .../{EncryptMethod.cs => NEEncryptMethod.cs} | 2 +- .../Enums/{ErrorCode.cs => NEErrorCode.cs} | 2 +- .../Enums/{MsgId.cs => NEMsgId.cs} | 2 +- .../Exceptions/NEException.cs | 6 +- .../Extensions/NEExtensions.cs | 32 +++- .../GBNewEnergy.Protocol.csproj | 9 +- src/GBNewEnergy.Protocol/INEParameter.cs | 14 ++ src/GBNewEnergy.Protocol/INEProperties.cs | 10 ++ src/GBNewEnergy.Protocol/NEBodies.cs | 18 +- src/GBNewEnergy.Protocol/NEBodiesFactory.cs | 6 +- .../NEBufferedEntityBase.cs | 36 ++++ src/GBNewEnergy.Protocol/NEPackage.cs | 109 ++++++------- .../NEProperties/NELoginProperty.cs | 19 +++ .../NEProperties/NELogoutProperty.cs | 11 ++ .../NEProperties/NEPackageProperty.cs | 16 ++ .../UpStream/NELoginUpStream.cs | 93 ++++++----- .../UpStream/NELogoutUpStream.cs | 26 +-- 20 files changed, 359 insertions(+), 233 deletions(-) delete mode 100644 src/GBNewEnergy.Protocol/BufferedEntityBase.cs rename src/GBNewEnergy.Protocol/Enums/{AskId.cs => NEAskId.cs} (95%) rename src/GBNewEnergy.Protocol/Enums/{EncryptMethod.cs => NEEncryptMethod.cs} (92%) rename src/GBNewEnergy.Protocol/Enums/{ErrorCode.cs => NEErrorCode.cs} (94%) rename src/GBNewEnergy.Protocol/Enums/{MsgId.cs => NEMsgId.cs} (97%) create mode 100644 src/GBNewEnergy.Protocol/INEParameter.cs create mode 100644 src/GBNewEnergy.Protocol/INEProperties.cs create mode 100644 src/GBNewEnergy.Protocol/NEBufferedEntityBase.cs create mode 100644 src/GBNewEnergy.Protocol/NEProperties/NELoginProperty.cs create mode 100644 src/GBNewEnergy.Protocol/NEProperties/NELogoutProperty.cs create mode 100644 src/GBNewEnergy.Protocol/NEProperties/NEPackageProperty.cs diff --git a/src/GBNewEnergy.Protocol.Test/NEPackageTest.cs b/src/GBNewEnergy.Protocol.Test/NEPackageTest.cs index 98e163c..a99f8e3 100644 --- a/src/GBNewEnergy.Protocol.Test/NEPackageTest.cs +++ b/src/GBNewEnergy.Protocol.Test/NEPackageTest.cs @@ -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() { "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() { "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 diff --git a/src/GBNewEnergy.Protocol/BufferedEntityBase.cs b/src/GBNewEnergy.Protocol/BufferedEntityBase.cs deleted file mode 100644 index bd8a0d6..0000000 --- a/src/GBNewEnergy.Protocol/BufferedEntityBase.cs +++ /dev/null @@ -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(); - } - } -} diff --git a/src/GBNewEnergy.Protocol/Enums/AskId.cs b/src/GBNewEnergy.Protocol/Enums/NEAskId.cs similarity index 95% rename from src/GBNewEnergy.Protocol/Enums/AskId.cs rename to src/GBNewEnergy.Protocol/Enums/NEAskId.cs index ae7b62e..b6b5a6f 100644 --- a/src/GBNewEnergy.Protocol/Enums/AskId.cs +++ b/src/GBNewEnergy.Protocol/Enums/NEAskId.cs @@ -7,7 +7,7 @@ namespace GBNewEnergy.Protocol.Enums /// /// 应答标志 /// - public enum AskId:byte + public enum NEAskId:byte { /// /// 接收到的信息正确 diff --git a/src/GBNewEnergy.Protocol/Enums/EncryptMethod.cs b/src/GBNewEnergy.Protocol/Enums/NEEncryptMethod.cs similarity index 92% rename from src/GBNewEnergy.Protocol/Enums/EncryptMethod.cs rename to src/GBNewEnergy.Protocol/Enums/NEEncryptMethod.cs index b5ee7ff..03b4d09 100644 --- a/src/GBNewEnergy.Protocol/Enums/EncryptMethod.cs +++ b/src/GBNewEnergy.Protocol/Enums/NEEncryptMethod.cs @@ -8,7 +8,7 @@ namespace GBNewEnergy.Protocol.Enums /// 数据单元加密方式 /// 0x01:数据不加密;0x02:数据经过 RSA 算法加密;0x03:数据经过 AES128 位算法加密;“0xFE”表示异常,“0xFF”表示无效 /// - public enum EncryptMethod:byte + public enum NEEncryptMethod:byte { None= 0x01, RSA= 0x02, diff --git a/src/GBNewEnergy.Protocol/Enums/ErrorCode.cs b/src/GBNewEnergy.Protocol/Enums/NEErrorCode.cs similarity index 94% rename from src/GBNewEnergy.Protocol/Enums/ErrorCode.cs rename to src/GBNewEnergy.Protocol/Enums/NEErrorCode.cs index 0e45987..1b98a28 100644 --- a/src/GBNewEnergy.Protocol/Enums/ErrorCode.cs +++ b/src/GBNewEnergy.Protocol/Enums/NEErrorCode.cs @@ -4,7 +4,7 @@ using System.Text; namespace GBNewEnergy.Protocol.Enums { - public enum ErrorCode + public enum NEErrorCode { /// /// 开始标识错误 diff --git a/src/GBNewEnergy.Protocol/Enums/MsgId.cs b/src/GBNewEnergy.Protocol/Enums/NEMsgId.cs similarity index 97% rename from src/GBNewEnergy.Protocol/Enums/MsgId.cs rename to src/GBNewEnergy.Protocol/Enums/NEMsgId.cs index db14c56..60eb234 100644 --- a/src/GBNewEnergy.Protocol/Enums/MsgId.cs +++ b/src/GBNewEnergy.Protocol/Enums/NEMsgId.cs @@ -7,7 +7,7 @@ namespace GBNewEnergy.Protocol.Enums /// /// 命令单元 /// - public enum MsgId:byte + public enum NEMsgId:byte { /// /// 车辆登入 diff --git a/src/GBNewEnergy.Protocol/Exceptions/NEException.cs b/src/GBNewEnergy.Protocol/Exceptions/NEException.cs index 25c19b3..748a9d8 100644 --- a/src/GBNewEnergy.Protocol/Exceptions/NEException.cs +++ b/src/GBNewEnergy.Protocol/Exceptions/NEException.cs @@ -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; } } } diff --git a/src/GBNewEnergy.Protocol/Extensions/NEExtensions.cs b/src/GBNewEnergy.Protocol/Extensions/NEExtensions.cs index 683c47f..318d327 100644 --- a/src/GBNewEnergy.Protocol/Extensions/NEExtensions.cs +++ b/src/GBNewEnergy.Protocol/Extensions/NEExtensions.cs @@ -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++; + } } /// diff --git a/src/GBNewEnergy.Protocol/GBNewEnergy.Protocol.csproj b/src/GBNewEnergy.Protocol/GBNewEnergy.Protocol.csproj index 364d0ef..a652fc6 100644 --- a/src/GBNewEnergy.Protocol/GBNewEnergy.Protocol.csproj +++ b/src/GBNewEnergy.Protocol/GBNewEnergy.Protocol.csproj @@ -5,10 +5,17 @@ - + + + + + + + + diff --git a/src/GBNewEnergy.Protocol/INEParameter.cs b/src/GBNewEnergy.Protocol/INEParameter.cs new file mode 100644 index 0000000..16a6583 --- /dev/null +++ b/src/GBNewEnergy.Protocol/INEParameter.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace GBNewEnergy.Protocol +{ + public interface INEParameter + { + /// + /// 车架号 + /// + string VIN { get; set; } + } +} diff --git a/src/GBNewEnergy.Protocol/INEProperties.cs b/src/GBNewEnergy.Protocol/INEProperties.cs new file mode 100644 index 0000000..8b639a0 --- /dev/null +++ b/src/GBNewEnergy.Protocol/INEProperties.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace GBNewEnergy.Protocol +{ + public interface INEProperties:INEParameter + { + } +} diff --git a/src/GBNewEnergy.Protocol/NEBodies.cs b/src/GBNewEnergy.Protocol/NEBodies.cs index 9a47245..fad8858 100644 --- a/src/GBNewEnergy.Protocol/NEBodies.cs +++ b/src/GBNewEnergy.Protocol/NEBodies.cs @@ -5,7 +5,7 @@ using System.Text; namespace GBNewEnergy.Protocol { - public abstract class NEBodies : IBuffer, IBuffered + public abstract class NEBodies : NEBufferedEntityBase { /// /// VIN - 登录流水号,过期时间(每天置1) @@ -28,20 +28,10 @@ namespace GBNewEnergy.Protocol /// 数据采集时间 /// 采用北京时间 /// - 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){} } } diff --git a/src/GBNewEnergy.Protocol/NEBodiesFactory.cs b/src/GBNewEnergy.Protocol/NEBodiesFactory.cs index 1a6c3b7..c175c9c 100644 --- a/src/GBNewEnergy.Protocol/NEBodiesFactory.cs +++ b/src/GBNewEnergy.Protocol/NEBodiesFactory.cs @@ -14,13 +14,13 @@ namespace GBNewEnergy.Protocol /// /// /// - 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; diff --git a/src/GBNewEnergy.Protocol/NEBufferedEntityBase.cs b/src/GBNewEnergy.Protocol/NEBufferedEntityBase.cs new file mode 100644 index 0000000..627f8ad --- /dev/null +++ b/src/GBNewEnergy.Protocol/NEBufferedEntityBase.cs @@ -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(); + } +} diff --git a/src/GBNewEnergy.Protocol/NEPackage.cs b/src/GBNewEnergy.Protocol/NEPackage.cs index f9f4b53..6fbb95e 100644 --- a/src/GBNewEnergy.Protocol/NEPackage.cs +++ b/src/GBNewEnergy.Protocol/NEPackage.cs @@ -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 { /// /// 新能源包 /// - 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) + { } /// /// 固定为24个字节长度 @@ -85,11 +39,11 @@ namespace GBNewEnergy.Protocol /// /// 命令标识 /// - public MsgId MsgId { get; private set; } + public NEMsgId MsgId { get; private set; } /// /// 应答标志 /// - public AskId AskId { get; private set; } + public NEAskId AskId { get; private set; } /// /// 车辆识别码 /// @@ -98,7 +52,7 @@ namespace GBNewEnergy.Protocol /// 数据加密方式 /// 0x01:数据不加密;0x02:数据经过 RSA 算法加密;0x03:数据经过 AES128 位算法加密;“0xFE”表示异常,“0xFF”表示无效 /// - public EncryptMethod EncryptMethod { get; private set; } + public NEEncryptMethod EncryptMethod { get; private set; } /// /// 数据单元长度是数据单元的总字节数,有效值范围:0-65531 /// @@ -115,11 +69,9 @@ namespace GBNewEnergy.Protocol /// /// 数据体 /// - 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); + } } } diff --git a/src/GBNewEnergy.Protocol/NEProperties/NELoginProperty.cs b/src/GBNewEnergy.Protocol/NEProperties/NELoginProperty.cs new file mode 100644 index 0000000..0a558a3 --- /dev/null +++ b/src/GBNewEnergy.Protocol/NEProperties/NELoginProperty.cs @@ -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 BatteryNos { get; set;} + } +} diff --git a/src/GBNewEnergy.Protocol/NEProperties/NELogoutProperty.cs b/src/GBNewEnergy.Protocol/NEProperties/NELogoutProperty.cs new file mode 100644 index 0000000..e002a99 --- /dev/null +++ b/src/GBNewEnergy.Protocol/NEProperties/NELogoutProperty.cs @@ -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; } + } +} diff --git a/src/GBNewEnergy.Protocol/NEProperties/NEPackageProperty.cs b/src/GBNewEnergy.Protocol/NEProperties/NEPackageProperty.cs new file mode 100644 index 0000000..c5f7d56 --- /dev/null +++ b/src/GBNewEnergy.Protocol/NEProperties/NEPackageProperty.cs @@ -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; } + } +} diff --git a/src/GBNewEnergy.Protocol/UpStream/NELoginUpStream.cs b/src/GBNewEnergy.Protocol/UpStream/NELoginUpStream.cs index e8f89c1..f36349d 100644 --- a/src/GBNewEnergy.Protocol/UpStream/NELoginUpStream.cs +++ b/src/GBNewEnergy.Protocol/UpStream/NELoginUpStream.cs @@ -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 batteryNos = new List(); - 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 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(); } /// @@ -77,7 +41,7 @@ namespace GBNewEnergy.Protocol.UpStream /// public IEnumerable 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 batteryNos = new List(); + for (int i = 0; i < BatteryCount; i++) + { + batteryNos.Add(Buffer.ReadStringLittle(i * BatteryLength + 30, BatteryLength)); + } + BatteryNos = batteryNos; + } } } diff --git a/src/GBNewEnergy.Protocol/UpStream/NELogoutUpStream.cs b/src/GBNewEnergy.Protocol/UpStream/NELogoutUpStream.cs index b4b244b..25aa27a 100644 --- a/src/GBNewEnergy.Protocol/UpStream/NELogoutUpStream.cs +++ b/src/GBNewEnergy.Protocol/UpStream/NELogoutUpStream.cs @@ -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 { /// - /// 车辆登入 + /// 车辆登出 /// 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);