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.

40 lines
1.3 KiB

using DotNetty.Buffers;
using DotNetty.Codecs;
using DotNetty.Transport.Channels;
using HybirdFrameworkCore.Utils;
using log4net;
using Newtonsoft.Json;
using Service.WaterCool.Msg;
using Service.WaterCool.Msg.WaterCool;
namespace Service.WaterCool.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
{
0x18FFC13A => ModelConvert.Decode<WaterCoolStatus>(bytes),
_ => throw new InvalidOperationException("This should never be reached"),
};
Log.Info($"receive={BitUtls.BytesToHexStr(bytes)}, msg={JsonConvert.SerializeObject(baseMsg)}");
output.Add(baseMsg);
}
}
}