using DotNetty.Buffers; using DotNetty.Codecs; using DotNetty.Transport.Channels; using HybirdFrameworkCore.Utils; using HybirdFrameworkDriver.Session; using log4net; using Newtonsoft.Json; using Service.Fire.Common; using Service.Fire.Msg; namespace Service.Ups.Codec; public class Encoder: MessageToByteEncoder { private ILog? Log(string? chargerSn) { if (ObjUtils.IsNotNullOrWhiteSpace(chargerSn)) { return LogManager.GetLogger("fire" + chargerSn); } return LogManager.GetLogger(typeof(Fire.Codec.Encoder)); } /// /// /// /// /// /// protected override void Encode(IChannelHandlerContext context, APCI obj, IByteBuffer output) { string? sn = ChannelUtils.GetAttr(context.Channel, FireConst.FireSn); byte[] bytes = obj.ToBytes(); Log(sn)?.Info( $"send {BitUtls.BytesToHexStr(bytes)}:{JsonConvert.SerializeObject(obj)} to {ChannelUtils.GetAttr(context.Channel, FireConst.FireSn)}"); context.WriteAndFlushAsync(bytes); } }