定义消息

master
smartwyy 6 months ago
parent 40945a784f
commit 32da256113

@ -1,8 +1,12 @@
using DotNetty.Buffers; using DotNetty.Buffers;
using DotNetty.Codecs; using DotNetty.Codecs;
using DotNetty.Transport.Channels; using DotNetty.Transport.Channels;
using HybirdFrameworkCore.Utils;
using log4net; using log4net;
using Service.Car.Common; using Service.Car.Common;
using Service.Car.Msg;
using Service.Car.Msg.Car.Req;
using Service.Car.Msg.Car.Resp;
namespace Service.Car.Codec; namespace Service.Car.Codec;
@ -20,7 +24,7 @@ public class Decoder : ByteToMessageDecoder
//分隔符索引 //分隔符索引
int delimiterIndex = IndexOf(buffer, delimiter); int delimiterIndex = IndexOf(buffer, delimiter);
//帧长索引 //帧长索引
int frameLengthIndex = delimiterIndex + delimiter.Capacity; int frameLengthIndex = delimiterIndex + CarConst.StartChar.Length;
if (delimiterIndex > 0) if (delimiterIndex > 0)
{ {
@ -28,7 +32,7 @@ public class Decoder : ByteToMessageDecoder
return; return;
} }
if (buffer.ReadableBytes < frameLengthIndex + 2) if (buffer.ReadableBytes < frameLengthIndex)
{ {
// 数据不足,等待更多数据 // 数据不足,等待更多数据
return; return;
@ -43,10 +47,30 @@ public class Decoder : ByteToMessageDecoder
return; return;
} }
var msg = Parse(buffer, frameLength);
output.Add(msg);
buffer.Clear(); buffer.Clear();
} }
} }
private BaseMsg Parse(IByteBuffer buffer, int length)
{
byte[] data = new byte[length];
buffer.ReadBytes(data);
return data[5] switch
{
0x01 => ModelConvert.Decode<ElecMsg>(data),
0x03 => ModelConvert.Decode<HeartBeatMsg>(data),
0x06 => ModelConvert.Decode<LockMsgResp>(data),
0x08 => ModelConvert.Decode<UnLockMsgResp>(data),
0x10 => ModelConvert.Decode<SettleConfirmMsgResp>(data),
0x12 => ModelConvert.Decode<SetParamMsgResp>(data),
_ => new BaseMsg()
};
}
private IByteBuffer? FindDelimiter(IByteBuffer buffer) private IByteBuffer? FindDelimiter(IByteBuffer buffer)
{ {
foreach (IByteBuffer delimiter in _delimiters) foreach (IByteBuffer delimiter in _delimiters)

@ -2,6 +2,7 @@
using DotNetty.Codecs; using DotNetty.Codecs;
using DotNetty.Transport.Channels; using DotNetty.Transport.Channels;
using log4net; using log4net;
using Service.Car.Common;
using Service.Car.Msg; using Service.Car.Msg;
namespace Service.Car.Codec; namespace Service.Car.Codec;
@ -23,5 +24,6 @@ public class Encoder : MessageToByteEncoder<BaseMsg>
{ {
byte[] bytes = obj.ToBytes(); byte[] bytes = obj.ToBytes();
output.WriteBytes(bytes); output.WriteBytes(bytes);
output.WriteBytes(CarConst.EndChar);
} }
} }

@ -1,10 +1,11 @@
using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Utils;
using HybirdFrameworkDriver.Common; using HybirdFrameworkDriver.Common;
using Service.Car.Common; using Service.Car.Common;
namespace Service.Car.Msg; namespace Service.Car.Msg;
public class BaseMsg: IToBytes public class BaseMsg : IToBytes
{ {
[Property(0, 8 * 3)] public byte[] StartChar { get; set; } = CarConst.StartChar; [Property(0, 8 * 3)] public byte[] StartChar { get; set; } = CarConst.StartChar;
[Property(24, 16)] public ushort Length { get; set; } [Property(24, 16)] public ushort Length { get; set; }
@ -51,6 +52,6 @@ public class BaseMsg: IToBytes
public byte[] ToBytes() public byte[] ToBytes()
{ {
throw new NotImplementedException(); return ModelConvert.Encode(this);
} }
} }

@ -2,11 +2,23 @@
namespace Service.Car.Msg.Car.Req; namespace Service.Car.Msg.Car.Req;
public class ElecMsg public class ElecMsg : BaseMsg
{ {
/// <summary> /// <summary>
/// 累计放电量 /// 累计放电量
/// </summary> /// </summary>
[Property(248, 24, scale: 0.01, round: 2)] [Property(248, 24, scale: 0.01, round: 2)]
public double AccDischargeCount { get; set; } public double AccDischargeCount { get; set; }
/// <summary>
/// 累计放电量
/// </summary>
[Property(272, 24, scale: 0.01, round: 2)]
public double AccFallbackCount { get; set; }
/// <summary>
/// 累计放电量
/// </summary>
[Property(296, 24, scale: 0.01, round: 2)]
public double AccChargeCount { get; set; }
} }

@ -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
{ {
/// <summary>
/// 锁止状态 1 解锁状态 2 上锁状态
/// </summary>
[Property(248, 8)]
public byte LockStatus { get; set; }
/// <summary>
/// 钥匙状态 0: OFF 1: ACC 2: ON 0xFF: 不支持
/// </summary>
[Property(256, 8)]
public byte KeyStatus { get; set; }
/// <summary>
/// 电磁阀驱动状态 0驱动关闭 1驱动打开
/// </summary>
[Property(264, 8)]
public byte SolenoidValveStatus { get; set; }
} }

@ -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
{ {
/// <summary>
/// 0、加锁成功1、加锁失败2、车架号不匹配FF、 异常
/// </summary>
[Property(248, 8)]
public byte Result { get; set; }
} }

@ -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
{ {
/// <summary>
/// 0、确认成功1、确认失败2、车架号不匹配FF、 异常
/// </summary>
[Property(248, 8)]
public byte Result { get; set; }
} }

@ -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
{ {
/// <summary>
/// 0、解锁成功1、非 ACC OFF 状态2、车架号不匹配3、 解锁失败FF、异常
/// </summary>
[Property(248, 8)]
public byte Result { get; set; }
} }

@ -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
{ {
/// <summary>
/// </summary>
[Property(248, 8)]
public byte Command { get; set; } = 0x55;
} }

@ -1,6 +1,6 @@
namespace Service.Car.Msg.Host.Req; namespace Service.Car.Msg.Host.Req;
public class SetParamMsg public class SetParamMsg: BaseMsg
{ {
} }

@ -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
{ {
/// <summary>
/// </summary>
[Property(248, 8)]
public byte Command { get; set; } = 0x55;
} }

@ -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
{ {
/// <summary>
/// </summary>
[Property(248, 8)]
public byte Command { get; set; } = 0x55;
} }

@ -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
{ {
/// <summary>
/// 0成功1失败
/// </summary>
[Property(248, 8)] public byte Result { get; set; }
/// <summary>
/// 0成功1失败
/// </summary>
/// <param name="result"></param>
public ElecMsgResp(byte result)
{
this.Result = result;
}
} }

@ -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
{ {
/// <summary>
/// 锁止状态 1 待机 2 换电
/// </summary>
[Property(248, 8)]
public byte HostStatus { get; set; }
} }
Loading…
Cancel
Save