From d094eb9fc83fdce4ceb5dc8a38a73f441bb85a81 Mon Sep 17 00:00:00 2001
From: SmallChi <564952747@qq.com>
Date: Wed, 6 Jun 2018 20:27:46 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8C=85=E4=BD=93=E5=8F=8A?=
=?UTF-8?q?=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../BufferedEntityBase.cs | 12 +++-
.../DownStream/NELoginDownStream.cs | 6 +-
.../Extensions/BinaryExtensions.cs | 54 ++++++++++++++
.../GBNewEnergy.Protocol.csproj | 3 +
src/GBNewEnergy.Protocol/IBuffered.cs | 2 +-
src/GBNewEnergy.Protocol/NEBodies.cs | 72 +++++++++++++++++++
src/GBNewEnergy.Protocol/NEPackage.cs | 44 ++++++++++--
src/GBNewEnergy.Protocol/NEUpStreamBase.cs | 2 +-
.../UpStream/NELoginUpStream.cs | 16 ++---
9 files changed, 191 insertions(+), 20 deletions(-)
create mode 100644 src/GBNewEnergy.Protocol/NEBodies.cs
diff --git a/src/GBNewEnergy.Protocol/BufferedEntityBase.cs b/src/GBNewEnergy.Protocol/BufferedEntityBase.cs
index 5652afe..bd8a0d6 100644
--- a/src/GBNewEnergy.Protocol/BufferedEntityBase.cs
+++ b/src/GBNewEnergy.Protocol/BufferedEntityBase.cs
@@ -3,7 +3,7 @@ using System.IO;
namespace GBNewEnergy.Protocol
{
- public abstract class BufferedEntityBase : IBuffer
+ public abstract class BufferedEntityBase : IBuffer, IBuffered
{
public byte[] Buffer { get; protected set; }
@@ -11,5 +11,15 @@ namespace GBNewEnergy.Protocol
{
Buffer = buffer;
}
+
+ protected BufferedEntityBase(params object[] parameter)
+ {
+
+ }
+
+ public byte[] ToBuffer()
+ {
+ throw new NotImplementedException();
+ }
}
}
diff --git a/src/GBNewEnergy.Protocol/DownStream/NELoginDownStream.cs b/src/GBNewEnergy.Protocol/DownStream/NELoginDownStream.cs
index 9d1f35f..db43a0d 100644
--- a/src/GBNewEnergy.Protocol/DownStream/NELoginDownStream.cs
+++ b/src/GBNewEnergy.Protocol/DownStream/NELoginDownStream.cs
@@ -4,8 +4,10 @@ using System.Text;
namespace GBNewEnergy.Protocol.Response
{
- public class NELoginDownStream : NEDownStreamBase
+ public class NELoginDownStream : NEBodies
{
-
+ protected NELoginDownStream(byte[] buffer) : base(buffer)
+ {
+ }
}
}
diff --git a/src/GBNewEnergy.Protocol/Extensions/BinaryExtensions.cs b/src/GBNewEnergy.Protocol/Extensions/BinaryExtensions.cs
index 23397d8..4d32bb0 100644
--- a/src/GBNewEnergy.Protocol/Extensions/BinaryExtensions.cs
+++ b/src/GBNewEnergy.Protocol/Extensions/BinaryExtensions.cs
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Linq;
+using System.Text;
namespace GBNewEnergy.Protocol.Extensions
{
@@ -24,6 +25,59 @@ namespace GBNewEnergy.Protocol.Extensions
return result;
}
+ ///
+ /// 字符串到字节数组
+ ///
+ ///
+ ///
+ public static byte[] ToBytes(this string data, Encoding coding)
+ {
+ return coding.GetBytes(data);
+ }
+
+ ///
+ /// 字符串到字节数组
+ ///
+ ///
+ public static byte[] ToBytes(this string data)
+ {
+ return Encoding.ASCII.GetBytes(data);
+ }
+
+ ///
+ /// 整型到字节数组
+ ///
+ /// int数据
+ /// int填入长度
+ public static byte[] ToBytes(this int data, int len)
+ {
+ byte[] bytes = new byte[len];
+ int n = 1;
+ for (int i = 0; i < len; i++)
+ {
+ bytes[i] = (byte)(data >> 8 * (len - n));
+ n++;
+ }
+ return bytes;
+ }
+
+ ///
+ /// 异或
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static byte ToXor(this byte[] buf, int offset, int len)
+ {
+ byte result = buf[offset];
+ for (int i = offset + 1; i < offset + len; i++)
+ {
+ result = (byte)(result ^ buf[i]);
+ }
+ return result;
+ }
+
///
/// 字节数组转16进制字符串
///
diff --git a/src/GBNewEnergy.Protocol/GBNewEnergy.Protocol.csproj b/src/GBNewEnergy.Protocol/GBNewEnergy.Protocol.csproj
index 349e074..364d0ef 100644
--- a/src/GBNewEnergy.Protocol/GBNewEnergy.Protocol.csproj
+++ b/src/GBNewEnergy.Protocol/GBNewEnergy.Protocol.csproj
@@ -5,7 +5,10 @@
+
+
+
diff --git a/src/GBNewEnergy.Protocol/IBuffered.cs b/src/GBNewEnergy.Protocol/IBuffered.cs
index 02034a0..8ea9cd8 100644
--- a/src/GBNewEnergy.Protocol/IBuffered.cs
+++ b/src/GBNewEnergy.Protocol/IBuffered.cs
@@ -2,6 +2,6 @@
{
public interface IBuffered
{
- void ToBuffer();
+ byte[] ToBuffer();
}
}
diff --git a/src/GBNewEnergy.Protocol/NEBodies.cs b/src/GBNewEnergy.Protocol/NEBodies.cs
new file mode 100644
index 0000000..08d1018
--- /dev/null
+++ b/src/GBNewEnergy.Protocol/NEBodies.cs
@@ -0,0 +1,72 @@
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Text;
+
+namespace GBNewEnergy.Protocol
+{
+ public abstract class NEBodies : IBuffer, IBuffered
+ {
+ ///
+ /// VIN - 登录流水号,过期时间(每天置1)
+ /// 车载终端登入一次,登入流水号自动加1,从1开始循环累加,最大值为65531,循环周期为天
+ ///
+ private static readonly ConcurrentDictionary LoginNumDict;
+
+ static NEBodies()
+ {
+ LoginNumDict = new ConcurrentDictionary();
+ }
+
+ ///
+ /// 登入流水号
+ /// 作用:看数据是否是连续请求
+ ///
+ public ushort LoginNum { get; protected set; }
+
+ ///
+ /// 数据采集时间
+ ///
+ public DateTime CurrentDateTime { get; protected set; }
+
+ public byte[] Buffer { get; protected set; }
+
+ protected NEBodies(byte[] buffer)
+ {
+ Buffer = buffer;
+ }
+
+ protected NEBodies(string vin,object[] parameter)
+ {
+ 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));
+ }
+ CurrentDateTime = DateTime.Now;
+ }
+
+ public byte[] ToBuffer()
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
diff --git a/src/GBNewEnergy.Protocol/NEPackage.cs b/src/GBNewEnergy.Protocol/NEPackage.cs
index 02295fe..86df19a 100644
--- a/src/GBNewEnergy.Protocol/NEPackage.cs
+++ b/src/GBNewEnergy.Protocol/NEPackage.cs
@@ -11,9 +11,9 @@ namespace GBNewEnergy.Protocol
///
/// 新能源包
///
- public class NEPackage: BufferedEntityBase
+ public class NEPackage:IBuffer
{
- public NEPackage(byte[] header,byte[] body):base(body)
+ public NEPackage(byte[] header,byte[] body)
{
if (header[0] != BeginFlag && header[1] == BeginFlag) throw new NEException(ErrorCode.BeginFlagError, $"{header[0]},{header[1]}");
MsgId = (MsgId)header[2];
@@ -23,6 +23,17 @@ namespace GBNewEnergy.Protocol
DataUnitLength = header.ToIntH2L(22, 2);
Header = header;
}
+
+ public NEPackage(string vin, MsgId msgId, AskId askId, NEBodies bodies,EncryptMethod encryptMethod)
+ {
+ MsgId = msgId;
+ AskId = askId;
+ VIN = vin;
+ EncryptMethod = encryptMethod;
+ Bodies = bodies;
+ ToBuffer();
+ }
+
///
/// 固定为24个字节长度
///
@@ -53,19 +64,42 @@ namespace GBNewEnergy.Protocol
///
public int DataUnitLength { get; private set; }
///
- /// BCC校验码
+ /// 采用BCC(异或检验)法,校验范围从命令单元的第一个字节开始,同后一个字节异或,直到校验码前一个字节为止,
+ /// 校验码占用一个字节,当数据单元存在加密时,应先加密后检验,先校验后解密
///
public byte BCCCode { get; private set; }
///
/// 头数据
///
public byte[] Header { get; private set; }
+ ///
+ /// 数据体
+ ///
+ public NEBodies Bodies { get;protected set; }
- public NEUpStreamBase NEUpStreamBase { get;protected set; }
+ public byte[] Buffer { get; private set; }
public override string ToString()
{
- return Header.ToHexString()+" "+this.Buffer.ToHexString();
+ return this.Header.ToHexString()+" " +this.Buffer.ToHexString();
+ }
+
+ private void ToBuffer()
+ {
+ // 固定单元长度
+ DataUnitLength = Bodies.Buffer.Length;
+ Buffer = new byte[HeaderFixedByteLength + 1 + DataUnitLength];
+ Buffer[0] = BeginFlag;
+ Buffer[1] = BeginFlag;
+ Buffer[2] = (byte)MsgId;
+ Buffer[3] = (byte)AskId;
+ Array.Copy(VIN.ToBytes(), 0, Buffer, 4, 20);
+ Buffer[21] = (byte)EncryptMethod;
+ Array.Copy(DataUnitLength.ToBytes(2), 0, Buffer, 22, 23);
+ Array.Copy(Bodies.Buffer, 0, Buffer, 24, DataUnitLength);
+ BCCCode = Buffer.ToXor(2, 23 + DataUnitLength);
+ Buffer[HeaderFixedByteLength + DataUnitLength] = BCCCode;
+ Array.Copy(Buffer,0, Header, 0, 22);
}
}
}
diff --git a/src/GBNewEnergy.Protocol/NEUpStreamBase.cs b/src/GBNewEnergy.Protocol/NEUpStreamBase.cs
index 26b5b1e..f94a94e 100644
--- a/src/GBNewEnergy.Protocol/NEUpStreamBase.cs
+++ b/src/GBNewEnergy.Protocol/NEUpStreamBase.cs
@@ -5,7 +5,7 @@ using System.Text;
namespace GBNewEnergy.Protocol
{
- public abstract class NEUpStreamBase: IBuffered, IBuffer
+ public abstract class NEUpStreamBase
{
public DateTime Utc{ get; set; }
public byte[] Buffer { get;protected set; }
diff --git a/src/GBNewEnergy.Protocol/UpStream/NELoginUpStream.cs b/src/GBNewEnergy.Protocol/UpStream/NELoginUpStream.cs
index cb832be..775fcd5 100644
--- a/src/GBNewEnergy.Protocol/UpStream/NELoginUpStream.cs
+++ b/src/GBNewEnergy.Protocol/UpStream/NELoginUpStream.cs
@@ -4,12 +4,13 @@ using System.Text;
namespace GBNewEnergy.Protocol.Request
{
- public class NELoginUpStream: NEUpStreamBase
+ public class NELoginUpStream: NEBodies
{
- ///
- /// 登入流水号
- ///
- public int LoginNum { get; set; }
+ protected NELoginUpStream(byte[] buffer) : base(buffer)
+ {
+
+ }
+
///
/// SIM 卡号
///
@@ -26,10 +27,5 @@ namespace GBNewEnergy.Protocol.Request
/// 电池编码
///
public IEnumerable BatteryNos { get; set; }
-
- public override void ToBuffer()
- {
- throw new NotImplementedException();
- }
}
}