diff --git a/Service/Car/Codec/Decoder.cs b/Service/Car/Codec/Decoder.cs index 933134a..e09bab5 100644 --- a/Service/Car/Codec/Decoder.cs +++ b/Service/Car/Codec/Decoder.cs @@ -1,8 +1,12 @@ using DotNetty.Buffers; using DotNetty.Codecs; using DotNetty.Transport.Channels; +using HybirdFrameworkCore.Utils; using log4net; using Service.Car.Common; +using Service.Car.Msg; +using Service.Car.Msg.Car.Req; +using Service.Car.Msg.Car.Resp; namespace Service.Car.Codec; @@ -20,7 +24,7 @@ public class Decoder : ByteToMessageDecoder //分隔符索引 int delimiterIndex = IndexOf(buffer, delimiter); //帧长索引 - int frameLengthIndex = delimiterIndex + delimiter.Capacity; + int frameLengthIndex = delimiterIndex + CarConst.StartChar.Length; if (delimiterIndex > 0) { @@ -28,7 +32,7 @@ public class Decoder : ByteToMessageDecoder return; } - if (buffer.ReadableBytes < frameLengthIndex + 2) + if (buffer.ReadableBytes < frameLengthIndex) { // 数据不足,等待更多数据 return; @@ -43,10 +47,30 @@ public class Decoder : ByteToMessageDecoder return; } + var msg = Parse(buffer, frameLength); + output.Add(msg); + buffer.Clear(); } } + private BaseMsg Parse(IByteBuffer buffer, int length) + { + byte[] data = new byte[length]; + buffer.ReadBytes(data); + + return data[5] switch + { + 0x01 => ModelConvert.Decode(data), + 0x03 => ModelConvert.Decode(data), + 0x06 => ModelConvert.Decode(data), + 0x08 => ModelConvert.Decode(data), + 0x10 => ModelConvert.Decode(data), + 0x12 => ModelConvert.Decode(data), + _ => new BaseMsg() + }; + } + private IByteBuffer? FindDelimiter(IByteBuffer buffer) { foreach (IByteBuffer delimiter in _delimiters) diff --git a/Service/Car/Codec/Encoder.cs b/Service/Car/Codec/Encoder.cs index 7d24036..f64cf63 100644 --- a/Service/Car/Codec/Encoder.cs +++ b/Service/Car/Codec/Encoder.cs @@ -2,6 +2,7 @@ using DotNetty.Codecs; using DotNetty.Transport.Channels; using log4net; +using Service.Car.Common; using Service.Car.Msg; namespace Service.Car.Codec; @@ -23,5 +24,6 @@ public class Encoder : MessageToByteEncoder { byte[] bytes = obj.ToBytes(); output.WriteBytes(bytes); + output.WriteBytes(CarConst.EndChar); } } \ No newline at end of file diff --git a/Service/Car/Msg/BaseMsg.cs b/Service/Car/Msg/BaseMsg.cs index 4e861f0..ce5fed6 100644 --- a/Service/Car/Msg/BaseMsg.cs +++ b/Service/Car/Msg/BaseMsg.cs @@ -1,10 +1,11 @@ using HybirdFrameworkCore.Autofac.Attribute; +using HybirdFrameworkCore.Utils; using HybirdFrameworkDriver.Common; using Service.Car.Common; namespace Service.Car.Msg; -public class BaseMsg: IToBytes +public class BaseMsg : IToBytes { [Property(0, 8 * 3)] public byte[] StartChar { get; set; } = CarConst.StartChar; [Property(24, 16)] public ushort Length { get; set; } @@ -51,6 +52,6 @@ public class BaseMsg: IToBytes public byte[] ToBytes() { - throw new NotImplementedException(); + return ModelConvert.Encode(this); } } \ No newline at end of file diff --git a/Service/Car/Msg/Car/Req/ElecMsg.cs b/Service/Car/Msg/Car/Req/ElecMsg.cs index 0f3a653..8bc9233 100644 --- a/Service/Car/Msg/Car/Req/ElecMsg.cs +++ b/Service/Car/Msg/Car/Req/ElecMsg.cs @@ -2,11 +2,23 @@ namespace Service.Car.Msg.Car.Req; -public class ElecMsg +public class ElecMsg : BaseMsg { /// /// 累计放电量 /// [Property(248, 24, scale: 0.01, round: 2)] public double AccDischargeCount { get; set; } + + /// + /// 累计放电量 + /// + [Property(272, 24, scale: 0.01, round: 2)] + public double AccFallbackCount { get; set; } + + /// + /// 累计放电量 + /// + [Property(296, 24, scale: 0.01, round: 2)] + public double AccChargeCount { get; set; } } \ No newline at end of file diff --git a/Service/Car/Msg/Car/Req/HeartBeatMsg.cs b/Service/Car/Msg/Car/Req/HeartBeatMsg.cs index 62d40bc..0be6a80 100644 --- a/Service/Car/Msg/Car/Req/HeartBeatMsg.cs +++ b/Service/Car/Msg/Car/Req/HeartBeatMsg.cs @@ -1,6 +1,22 @@ -namespace Service.Car.Msg.Car.Req; +using HybirdFrameworkCore.Autofac.Attribute; -public class HeartBeatMsg +namespace Service.Car.Msg.Car.Req; + +public class HeartBeatMsg: BaseMsg { - + /// + /// 锁止状态 1 解锁状态 2 上锁状态 + /// + [Property(248, 8)] + public byte LockStatus { get; set; } + /// + /// 钥匙状态 0: OFF 1: ACC 2: ON 0xFF: 不支持 + /// + [Property(256, 8)] + public byte KeyStatus { get; set; } + /// + /// 电磁阀驱动状态 0:驱动关闭 1:驱动打开 + /// + [Property(264, 8)] + public byte SolenoidValveStatus { get; set; } } \ No newline at end of file diff --git a/Service/Car/Msg/Car/Resp/LockMsgResp.cs b/Service/Car/Msg/Car/Resp/LockMsgResp.cs index 010482a..0a8ad11 100644 --- a/Service/Car/Msg/Car/Resp/LockMsgResp.cs +++ b/Service/Car/Msg/Car/Resp/LockMsgResp.cs @@ -1,6 +1,12 @@ -namespace Service.Car.Msg.Car.Resp; +using HybirdFrameworkCore.Autofac.Attribute; -public class LockMsgResp +namespace Service.Car.Msg.Car.Resp; + +public class LockMsgResp : BaseMsg { - + /// + /// 0、加锁成功;1、加锁失败;2、车架号不匹配;FF、 异常 + /// + [Property(248, 8)] + public byte Result { get; set; } } \ No newline at end of file diff --git a/Service/Car/Msg/Car/Resp/SettleConfirmMsgResp.cs b/Service/Car/Msg/Car/Resp/SettleConfirmMsgResp.cs index 7639b8b..f0eadcd 100644 --- a/Service/Car/Msg/Car/Resp/SettleConfirmMsgResp.cs +++ b/Service/Car/Msg/Car/Resp/SettleConfirmMsgResp.cs @@ -1,6 +1,12 @@ -namespace Service.Car.Msg.Car.Resp; +using HybirdFrameworkCore.Autofac.Attribute; -public class SettleConfirmMsgResp +namespace Service.Car.Msg.Car.Resp; + +public class SettleConfirmMsgResp : BaseMsg { - + /// + /// 0、确认成功;1、确认失败;2、车架号不匹配;FF、 异常 + /// + [Property(248, 8)] + public byte Result { get; set; } } \ No newline at end of file diff --git a/Service/Car/Msg/Car/Resp/UnLockMsgResp.cs b/Service/Car/Msg/Car/Resp/UnLockMsgResp.cs index 6da0dbb..86e2d6b 100644 --- a/Service/Car/Msg/Car/Resp/UnLockMsgResp.cs +++ b/Service/Car/Msg/Car/Resp/UnLockMsgResp.cs @@ -1,6 +1,12 @@ -namespace Service.Car.Msg.Car.Resp; +using HybirdFrameworkCore.Autofac.Attribute; -public class UnLockMsgResp +namespace Service.Car.Msg.Car.Resp; + +public class UnLockMsgResp : BaseMsg { - + /// + /// 0、解锁成功;1、非 ACC OFF 状态;2、车架号不匹配;3、 解锁失败;FF、异常 + /// + [Property(248, 8)] + public byte Result { get; set; } } \ No newline at end of file diff --git a/Service/Car/Msg/Host/Req/LockMsg.cs b/Service/Car/Msg/Host/Req/LockMsg.cs index 4badc37..01884cf 100644 --- a/Service/Car/Msg/Host/Req/LockMsg.cs +++ b/Service/Car/Msg/Host/Req/LockMsg.cs @@ -1,6 +1,11 @@ -namespace Service.Car.Msg.Host.Req; +using HybirdFrameworkCore.Autofac.Attribute; -public class LockMsg +namespace Service.Car.Msg.Host.Req; + +public class LockMsg : BaseMsg { - + /// + /// + [Property(248, 8)] + public byte Command { get; set; } = 0x55; } \ No newline at end of file diff --git a/Service/Car/Msg/Host/Req/SetParamMsg.cs b/Service/Car/Msg/Host/Req/SetParamMsg.cs index 3f6b2d6..e109768 100644 --- a/Service/Car/Msg/Host/Req/SetParamMsg.cs +++ b/Service/Car/Msg/Host/Req/SetParamMsg.cs @@ -1,6 +1,6 @@ namespace Service.Car.Msg.Host.Req; -public class SetParamMsg +public class SetParamMsg: BaseMsg { } \ No newline at end of file diff --git a/Service/Car/Msg/Host/Req/SettleConfirmMsg.cs b/Service/Car/Msg/Host/Req/SettleConfirmMsg.cs index ec39a6d..d21ca79 100644 --- a/Service/Car/Msg/Host/Req/SettleConfirmMsg.cs +++ b/Service/Car/Msg/Host/Req/SettleConfirmMsg.cs @@ -1,6 +1,11 @@ -namespace Service.Car.Msg.Host.Req; +using HybirdFrameworkCore.Autofac.Attribute; -public class SettleConfirmMsg +namespace Service.Car.Msg.Host.Req; + +public class SettleConfirmMsg : BaseMsg { - + /// + /// + [Property(248, 8)] + public byte Command { get; set; } = 0x55; } \ No newline at end of file diff --git a/Service/Car/Msg/Host/Req/UnLockMsg.cs b/Service/Car/Msg/Host/Req/UnLockMsg.cs index 06d6fa6..4591a80 100644 --- a/Service/Car/Msg/Host/Req/UnLockMsg.cs +++ b/Service/Car/Msg/Host/Req/UnLockMsg.cs @@ -1,6 +1,11 @@ -namespace Service.Car.Msg.Host.Req; +using HybirdFrameworkCore.Autofac.Attribute; -public class UnLockMsg +namespace Service.Car.Msg.Host.Req; + +public class UnLockMsg: BaseMsg { - + /// + /// + [Property(248, 8)] + public byte Command { get; set; } = 0x55; } \ No newline at end of file diff --git a/Service/Car/Msg/Host/Resp/ElecMsgResp.cs b/Service/Car/Msg/Host/Resp/ElecMsgResp.cs index cdd03fa..1746d5b 100644 --- a/Service/Car/Msg/Host/Resp/ElecMsgResp.cs +++ b/Service/Car/Msg/Host/Resp/ElecMsgResp.cs @@ -1,6 +1,20 @@ -namespace Service.Car.Msg.Host.Resp; +using HybirdFrameworkCore.Autofac.Attribute; -public class ElecMsgResp +namespace Service.Car.Msg.Host.Resp; + +public class ElecMsgResp : BaseMsg { - + /// + /// 0:成功;1:失败 + /// + [Property(248, 8)] public byte Result { get; set; } + + /// + /// 0:成功;1:失败 + /// + /// + public ElecMsgResp(byte result) + { + this.Result = result; + } } \ No newline at end of file diff --git a/Service/Car/Msg/Host/Resp/HeartBeatMsgResp.cs b/Service/Car/Msg/Host/Resp/HeartBeatMsgResp.cs index b9d39bc..39f8362 100644 --- a/Service/Car/Msg/Host/Resp/HeartBeatMsgResp.cs +++ b/Service/Car/Msg/Host/Resp/HeartBeatMsgResp.cs @@ -1,6 +1,12 @@ -namespace Service.Car.Msg.Host.Resp; +using HybirdFrameworkCore.Autofac.Attribute; -public class HeartBeatMsgResp +namespace Service.Car.Msg.Host.Resp; + +public class HeartBeatMsgResp : BaseMsg { - + /// + /// 锁止状态 1 待机 2 换电 + /// + [Property(248, 8)] + public byte HostStatus { get; set; } } \ No newline at end of file