You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.2 KiB

using DotNetty.Buffers;
using DotNetty.Codecs;
using DotNetty.Transport.Channels;
using HybirdFrameworkCore.Utils;
using log4net;
using Newtonsoft.Json;
using Service.TBox.Msg;
using Service.TBox.Msg.TBox;
namespace Service.TBox.Codec;
public class Decoder : ByteToMessageDecoder
{
private static readonly ILog Log = LogManager.GetLogger(typeof(Decoder));
private readonly int _fixedLength = 13;
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List<object> output)
{
if (_fixedLength <= input.ReadableBytes)
{
byte[] bytes = new byte[_fixedLength-1];
input.SkipBytes(1);
input.ReadBytes(bytes);
Log.Info($"receive {BitUtls.BytesToHexStr(bytes)}");
int id = BitConverter.ToInt32(new byte[] { bytes[3], bytes[2], bytes[1], bytes[0] }, 0);
BaseMsg baseMsg = id switch
{
0x1882D0F3 => ModelConvert.Decode<SocMsg>(bytes),
0x18FF48A8 => ModelConvert.Decode<StatusMsg>(bytes),
5 months ago
0x18E1D0F3=> ModelConvert.Decode<BatteryOneSn>(bytes),
0x18E2D0F3=> ModelConvert.Decode<BatteryTwoSn>(bytes),
0x18E3D0F3=> ModelConvert.Decode<BatteryThreeSn>(bytes),
0x18E4D0F3=> ModelConvert.Decode<BatteryFourSn>(bytes),
5 months ago
0x18E1F3D0=> ModelConvert.Decode<VinMsg>(bytes),
0x1801D8A7=> ModelConvert.Decode<VersionMsg>(bytes),
//0x1880D0F3=> ModelConvert.Decode<StatusMsg>(bytes),
0x18E5D0F3=> ModelConvert.Decode<BatteryInfo1>(bytes),
0x18F1D0F3=> ModelConvert.Decode<SohMsg>(bytes),
0x18F2D0F3=> ModelConvert.Decode<SubMileMsg>(bytes),
0x18FEDA17=> ModelConvert.Decode<TotalMileMsg>(bytes),
//0x18E1F3D1=> ModelConvert.Decode<StatusMsg>(bytes),
//0x18FFF8A8=> ModelConvert.Decode<StatusMsg>(bytes),
0x18FFF8A7=> ModelConvert.Decode<LockStatusMsg>(bytes),
_ => ModelConvert.Decode<OtherMsg>(bytes),
};
Log.Info($"receive={BitUtls.BytesToHexStr(bytes)}, msg={JsonConvert.SerializeObject(baseMsg)}");
output.Add(baseMsg);
}
}
}