From 65d5c9c3d91bbed150ae5b22fbde1f60d8fdfffa Mon Sep 17 00:00:00 2001 From: rszn <645583145@qq.com> Date: Wed, 5 Jun 2024 18:23:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=85=E7=94=B5=E6=9C=BA=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=8B=86=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Service/Charger/Client/ChargerClient.cs | 14 +- Service/Charger/Client/ClientMgr.cs | 3 +- Service/Charger/Codec/Decoder.cs | 15 ++- Service/Charger/Codec/Encoder.cs | 25 ++-- WebStarter/log4net.config | 171 ++++++++++++++++++++++-- 5 files changed, 197 insertions(+), 31 deletions(-) diff --git a/Service/Charger/Client/ChargerClient.cs b/Service/Charger/Client/ChargerClient.cs index 0542ea1..5e8d4cd 100644 --- a/Service/Charger/Client/ChargerClient.cs +++ b/Service/Charger/Client/ChargerClient.cs @@ -28,10 +28,14 @@ namespace Service.Charger.Client; [Scope("InstancePerDependency")] public class ChargerClient : TcpClient { - private static readonly ILog Log = LogManager.GetLogger(typeof(ChargerClient)); #region 属性 + /// + /// 充电机编号 + /// + public string Sn { get; set; } + public ushort AuthTimes { get; set; } = 0; public bool IsAuthed { get; set; } = false; @@ -187,6 +191,11 @@ public class ChargerClient : TcpClient _binInfoRepository = binInfoRepository; } + private ILog Log() + { + return LogManager.GetLogger("charger_" + this.Sn); + } + /// /// /// @@ -253,8 +262,7 @@ public class ChargerClient : TcpClient chargeOrderNo = ChargerUtils.GenChargeOrderSn(); } - - Log.Info( + Log().Info( $"SendRemoteStartCharging soc={socLimit}, changePower={changePower}, changePowerCmdType={changePowerCmdType}, chargeOrderNo={chargeOrderNo}"); var remoteStartCharging = new RemoteStartCharging(socLimit, changePowerCmdType, changePower, chargeOrderNo); CurrentCmd = JsonConvert.SerializeObject(remoteStartCharging, Formatting.Indented) + "\r\n" + diff --git a/Service/Charger/Client/ClientMgr.cs b/Service/Charger/Client/ClientMgr.cs index 5fc0e97..543d401 100644 --- a/Service/Charger/Client/ClientMgr.cs +++ b/Service/Charger/Client/ClientMgr.cs @@ -15,7 +15,7 @@ namespace Service.Charger.Client; /// /// 示例程序 /// -[Scope("SingleInstance")] +[Scope] public static class ClientMgr { private static readonly ILog Log = LogManager.GetLogger(typeof(ClientMgr)); @@ -91,6 +91,7 @@ public static class ClientMgr Log.Info($"begin to connect {netInfo.Code} {netInfo.NetAddr}:{netInfo.NetPort}"); ChargerClient client = AppInfo.Container.Resolve(); client.AutoReconnect = true; + client.Sn = netInfo.Code; client.BinNo = binInfo?.No; client.BatteryNo = binInfo?.BatteryNo; client.InitBootstrap(netInfo.NetAddr, int.Parse(netInfo.NetPort)); diff --git a/Service/Charger/Codec/Decoder.cs b/Service/Charger/Codec/Decoder.cs index 6704736..e417e30 100644 --- a/Service/Charger/Codec/Decoder.cs +++ b/Service/Charger/Codec/Decoder.cs @@ -16,12 +16,19 @@ namespace Service.Charger.Codec; public class Decoder : ByteToMessageDecoder { - private static readonly ILog Log = LogManager.GetLogger(typeof(Decoder)); - private readonly IByteBuffer[] _delimiters = { Unpooled.CopiedBuffer(ChargerConst.StartChar) }; + private ILog Log(string? chargerSn) + { + if (ObjUtils.IsNotNullOrWhiteSpace(chargerSn)) + { + LogManager.GetLogger("charger_" + chargerSn); + } + return LogManager.GetLogger(typeof(Decoder)); + } protected override void Decode(IChannelHandlerContext context, IByteBuffer buffer, List output) { + string? chargerSn = ChannelUtils.GetAttr(context.Channel, ChargerConst.ChargerSn); IByteBuffer? delimiter = FindDelimiter(buffer); if (delimiter != null) { @@ -62,13 +69,13 @@ public class Decoder : ByteToMessageDecoder try { ASDU asdu = Parse(buffer, totalFrameLength, out data); - Log.Info($"receive {JsonConvert.SerializeObject(asdu)}:{data} from {ChannelUtils.GetAttr(context.Channel, ChargerConst.ChargerSn)}"); + Log(chargerSn).Info($"receive {BitUtls.BytesToHexStr(data)}:{JsonConvert.SerializeObject(asdu)} from {chargerSn}"); output.Add(asdu); buffer.Clear(); } catch (Exception e) { - Log.Error($"decode fail msg={BitUtls.BytesToHexStr(data)}"); + Log(chargerSn).Error($"decode fail msg={BitUtls.BytesToHexStr(data)}"); } } } diff --git a/Service/Charger/Codec/Encoder.cs b/Service/Charger/Codec/Encoder.cs index 39ac48c..ef8dc76 100644 --- a/Service/Charger/Codec/Encoder.cs +++ b/Service/Charger/Codec/Encoder.cs @@ -11,13 +11,21 @@ using Service.Charger.Msg; namespace Service.Charger.Codec; /// -/// +/// /// public class Encoder : MessageToByteEncoder { - private static readonly ILog Log = LogManager.GetLogger(typeof(Encoder)); + private ILog Log(string? chargerSn) + { + if (ObjUtils.IsNotNullOrWhiteSpace(chargerSn)) + { + LogManager.GetLogger("charger_" + chargerSn); + } + return LogManager.GetLogger(typeof(Decoder)); + } + /// - /// + /// /// /// /// @@ -25,6 +33,7 @@ public class Encoder : MessageToByteEncoder protected override void Encode(IChannelHandlerContext context, APCI obj, IByteBuffer output) { string? s = ChannelUtils.GetAttr(context.Channel, ChargerConst.DestAddr); + string? sn = ChannelUtils.GetAttr(context.Channel, ChargerConst.ChargerSn); if (s != null) { byte[] destAddr = s.Split(",").Select(b => Convert.ToByte(b)).ToArray(); @@ -32,14 +41,8 @@ public class Encoder : MessageToByteEncoder } byte[] bytes = obj.ToBytes(); - Log.Info($"send {JsonConvert.SerializeObject(obj)}:{BitUtls.BytesToHexStr(bytes)} to {ChannelUtils.GetAttr(context.Channel, ChargerConst.ChargerSn)}"); - //TODO::仅测试使用 - //string ou = string.Empty; - //for(int i = 0; i < bytes.Length; i++) - //{ - // ou += bytes[i].ToString("X2") + " "; - //} + Log(sn).Info($"send {BitUtls.BytesToHexStr(bytes)}:{JsonConvert.SerializeObject(obj)} to {ChannelUtils.GetAttr(context.Channel, ChargerConst.ChargerSn)}"); output.WriteBytes(bytes); } -} \ No newline at end of file +} diff --git a/WebStarter/log4net.config b/WebStarter/log4net.config index c533256..ce9dad4 100644 --- a/WebStarter/log4net.config +++ b/WebStarter/log4net.config @@ -1,32 +1,179 @@  + - + - - + + - + - + - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + - \ No newline at end of file +