diff --git a/HybirdFrameworkDriver/HybirdFrameworkDriver.csproj b/HybirdFrameworkDriver/HybirdFrameworkDriver.csproj index 959da77..f8a5754 100644 --- a/HybirdFrameworkDriver/HybirdFrameworkDriver.csproj +++ b/HybirdFrameworkDriver/HybirdFrameworkDriver.csproj @@ -6,6 +6,10 @@ net6.0;net6.0-windows + + bin\Debug\HybirdFrameworkDriver.xml + + diff --git a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs index 2e4e8dd..fee6009 100644 --- a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs +++ b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs @@ -8,6 +8,12 @@ public static class ModbusDecoder { private static readonly ILog Log = LogManager.GetLogger(typeof(ModbusDecoder)); + public static T Decode(byte[] bytes) where T : class, new() + { + T t = new T(); + return Decode(bytes, t); + } + public static T Decode(byte[] bytes, T t) where T : class, new() { var fields = t.GetType().GetProperties() diff --git a/HybirdFrameworkDriver/ModbusTcpMaster/PlcDate.cs b/HybirdFrameworkDriver/ModbusTcpMaster/PlcDate.cs deleted file mode 100644 index 9037c75..0000000 --- a/HybirdFrameworkDriver/ModbusTcpMaster/PlcDate.cs +++ /dev/null @@ -1,87 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace HybirdFrameworkDriver.ModbusTcpMaster -{ - /// - /// PLC地址 - /// - public class PlcDate - { - /// - /// 写编程使能 - /// - public ModbusProperty ProgrammingEnable { get; set; } = new(40960); - /// - /// 本机modbus地址 - /// - public ModbusProperty Modbus1Addres { get; set; } = new(40962); - /// - /// 波特率 - /// - public ModbusProperty BaudRate { get; set; } = new(40964); - /// - /// 校验位 - /// - public ModbusProperty CheckBit { get; set; } = new(2566); - /// - /// 秒 - /// - public ModbusProperty Seconds { get; set; } = new(0); - /// - /// 分 - /// - public ModbusProperty Points { get; set; } = new(2); - /// - /// 时 - /// - public ModbusProperty When { get; set; } = new(4); - /// - /// 周 - /// - public ModbusProperty Weeks { get; set; } = new(6); - /// - /// 日 - /// - public ModbusProperty Day { get; set; } = new(8); - /// - /// 月 - /// - public ModbusProperty Month { get; set; } = new(10); - /// - /// 年 - /// - public ModbusProperty Years { get; set; } = new(12); - /// - /// 本机modbus地址 - /// - public ModbusProperty Modbus1Addres2 { get; set; } = new(14); - /// - /// 保留 - /// - public ModbusProperty Reserve { get; set; } = new(16); - /// - /// 电压变比 - /// - public ModbusProperty VoltageRatio { get; set; } = new(18); - /// - /// 电流变比 - /// - public ModbusProperty CurrentRatio { get; set; } = new(20); - /// - /// 秒脉冲/无功电能选择 - /// - public ModbusProperty PulsePerSecond { get; set; } = new(42); - /// - /// 电流接线反向 - /// - public ModbusProperty CurrentReversal { get; set; } = new(48); - /// - /// 电表清零 - /// - public ModbusProperty MeterReset { get; set; } = new(4576); - } -} diff --git a/HybirdFrameworkDriver/Session/ChannelUtils.cs b/HybirdFrameworkDriver/Session/ChannelUtils.cs index 55aa910..b0eb9b4 100644 --- a/HybirdFrameworkDriver/Session/ChannelUtils.cs +++ b/HybirdFrameworkDriver/Session/ChannelUtils.cs @@ -15,8 +15,19 @@ public static class ChannelUtils */ public static void AddChannelSession(IChannel channel, IoSession session) { - var attribute = channel.GetAttribute(SessionKey); - attribute.Set(session); + AddAttr(channel, SessionKey, session); + } + + public static void AddAttr(IChannel channel, AttributeKey key, T t) where T : class + { + var attribute = channel.GetAttribute(key); + attribute.Set(t); + } + + public static T? GetAttr(IChannel channel, AttributeKey key) where T : class + { + var attribute = channel.GetAttribute(key); + return attribute.Get(); } public static IoSession GetSessionBy(IChannel channel) diff --git a/HybirdFrameworkDriver/TcpClient/ClientListenerHandler.cs b/HybirdFrameworkDriver/TcpClient/ClientListenerHandler.cs index fec6d85..ceb36be 100644 --- a/HybirdFrameworkDriver/TcpClient/ClientListenerHandler.cs +++ b/HybirdFrameworkDriver/TcpClient/ClientListenerHandler.cs @@ -11,10 +11,13 @@ public class ClientListenerHandler : ChannelHandlerAdapter where TH where TE : ChannelHandlerAdapter, new() { private static readonly ILog Log = LogManager.GetLogger(typeof(ClientListenerHandler)); + + private bool AutoReconnect { get; set; } - public ClientListenerHandler(TcpClient client) + public ClientListenerHandler(TcpClient client, bool autoReconnect) { Client = client; + AutoReconnect = autoReconnect; } public TcpClient Client { get; set; } @@ -47,7 +50,10 @@ public class ClientListenerHandler : ChannelHandlerAdapter where TH context.Channel.CloseAsync().Wait(); context.Channel.CloseCompletion.Wait(); - new Thread(Client.Connect).Start(); + if (AutoReconnect) + { + new Thread(Client.BaseConnect).Start(); + } } public override void UserEventTriggered(IChannelHandlerContext context, object evt) diff --git a/HybirdFrameworkDriver/TcpClient/TcpClient.cs b/HybirdFrameworkDriver/TcpClient/TcpClient.cs index 0a0df9e..9892036 100644 --- a/HybirdFrameworkDriver/TcpClient/TcpClient.cs +++ b/HybirdFrameworkDriver/TcpClient/TcpClient.cs @@ -13,54 +13,59 @@ using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.Autofac.Attribute; using log4net; using Microsoft.Extensions.Logging; +using Newtonsoft.Json; using LogLevel = DotNetty.Handlers.Logging.LogLevel; namespace HybirdFrameworkDriver.TcpClient; -public class TcpClient where TH : IChannelHandler +public class TcpClient : IDisposable where TH : IChannelHandler where TD : ByteToMessageDecoder, new() where TE : ChannelHandlerAdapter, new() { + [JsonIgnore] private static readonly ILog Log = LogManager.GetLogger(typeof(TcpClient)); + + [JsonIgnore] private Bootstrap? _bootstrap; - - public IChannel Channel { get; set; } + [JsonIgnore] + private IEventLoopGroup? _eventLoopGroup; + [JsonIgnore] + public IChannel? Channel { get; set; } public bool Connected { get; set; } public string Host { get; set; } public int Port { get; set; } + public bool AutoReconnect { get; set; } public LogLevel? LogLevel { get; set; } + public void InitBootstrap(string host, int port, Action? channelInactiveHandler = null) { Host = host; Port = port; - + if (LogLevel != null) { InternalLoggerFactory.DefaultFactory.AddProvider(new Log4NetProvider()); } _bootstrap = new Bootstrap(); + _eventLoopGroup = new MultithreadEventLoopGroup(); _bootstrap - .Group(new MultithreadEventLoopGroup()) + .Group(_eventLoopGroup) .Channel() .Option(ChannelOption.TcpNodelay, true) - .Handler(new LoggingHandler()) .Handler(new ActionChannelInitializer(channel => { - var clientListenerHandler = new ClientListenerHandler(this); + var clientListenerHandler = new ClientListenerHandler(this, AutoReconnect); - var pipeline = channel.Pipeline; - if (LogLevel != null) { pipeline.AddLast(new LoggingHandler(LogLevel.Value)); } - // 监听器 pipeline.AddLast(clientListenerHandler); pipeline.AddLast("idleStateHandler", new IdleStateHandler(30, 0, 0)); // 触发读取超时 @@ -125,12 +130,12 @@ public class TcpClient where TH : IChannelHandler } - public void Connect() + public void BaseConnect() { Connected = false; Log.Info($"begin to connect {Host}:{Port}"); - while (Connected) + while (!Connected) { try { @@ -147,4 +152,17 @@ public class TcpClient where TH : IChannelHandler Thread.Sleep(1000); } } + + public void Close() + { + this.Channel?.CloseAsync().Wait(); + this.Channel?.CloseCompletion.Wait(); + _eventLoopGroup?.ShutdownGracefullyAsync().Wait(); + } + + public void Dispose() + { + this.Close(); + this.Connected = false; + } } \ No newline at end of file diff --git a/Service/Charger/Client/ClientMgr.cs b/Service/Charger/Client/ClientMgr.cs index 4685093..9cbc586 100644 --- a/Service/Charger/Client/ClientMgr.cs +++ b/Service/Charger/Client/ClientMgr.cs @@ -19,7 +19,7 @@ public class ClientMgr var chargerClient2 = AppInfo.Container.Resolve(); chargerClient2.InitBootstrap("127.0.0.1", 9998); - chargerClient2.Connect(); + chargerClient2.BaseConnect(); chargerClient2.SessionAttr(12, 12, "12", "2"); chargerClient2.Channel.WriteAndFlushAsync(auth);