同步框架代码

master
smartwyy 6 months ago
parent c47d97278f
commit ed6873ecaa

@ -6,6 +6,10 @@
<TargetFrameworks>net6.0;net6.0-windows</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DocumentationFile>bin\Debug\HybirdFrameworkDriver.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="7.0.1"/>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0"/>

@ -8,6 +8,12 @@ public static class ModbusDecoder
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ModbusDecoder));
public static T Decode<T>(byte[] bytes) where T : class, new()
{
T t = new T();
return Decode<T>(bytes, t);
}
public static T Decode<T>(byte[] bytes, T t) where T : class, new()
{
var fields = t.GetType().GetProperties()

@ -1,87 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HybirdFrameworkDriver.ModbusTcpMaster
{
/// <summary>
/// PLC地址
/// </summary>
public class PlcDate
{
/// <summary>
/// 写编程使能
/// </summary>
public ModbusProperty<UInt16> ProgrammingEnable { get; set; } = new(40960);
/// <summary>
/// 本机modbus地址
/// </summary>
public ModbusProperty<UInt16> Modbus1Addres { get; set; } = new(40962);
/// <summary>
/// 波特率
/// </summary>
public ModbusProperty<UInt16> BaudRate { get; set; } = new(40964);
/// <summary>
/// 校验位
/// </summary>
public ModbusProperty<UInt16> CheckBit { get; set; } = new(2566);
/// <summary>
/// 秒
/// </summary>
public ModbusProperty<UInt16> Seconds { get; set; } = new(0);
/// <summary>
/// 分
/// </summary>
public ModbusProperty<UInt16> Points { get; set; } = new(2);
/// <summary>
/// 时
/// </summary>
public ModbusProperty<UInt16> When { get; set; } = new(4);
/// <summary>
/// 周
/// </summary>
public ModbusProperty<UInt16> Weeks { get; set; } = new(6);
/// <summary>
/// 日
/// </summary>
public ModbusProperty<UInt16> Day { get; set; } = new(8);
/// <summary>
/// 月
/// </summary>
public ModbusProperty<UInt16> Month { get; set; } = new(10);
/// <summary>
/// 年
/// </summary>
public ModbusProperty<UInt16> Years { get; set; } = new(12);
/// <summary>
/// 本机modbus地址
/// </summary>
public ModbusProperty<UInt16> Modbus1Addres2 { get; set; } = new(14);
/// <summary>
/// 保留
/// </summary>
public ModbusProperty<UInt16> Reserve { get; set; } = new(16);
/// <summary>
/// 电压变比
/// </summary>
public ModbusProperty<UInt16> VoltageRatio { get; set; } = new(18);
/// <summary>
/// 电流变比
/// </summary>
public ModbusProperty<UInt16> CurrentRatio { get; set; } = new(20);
/// <summary>
/// 秒脉冲/无功电能选择
/// </summary>
public ModbusProperty<UInt16> PulsePerSecond { get; set; } = new(42);
/// <summary>
/// 电流接线反向
/// </summary>
public ModbusProperty<UInt16> CurrentReversal { get; set; } = new(48);
/// <summary>
/// 电表清零
/// </summary>
public ModbusProperty<UInt16> MeterReset { get; set; } = new(4576);
}
}

@ -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<T>(IChannel channel, AttributeKey<T> key, T t) where T : class
{
var attribute = channel.GetAttribute(key);
attribute.Set(t);
}
public static T? GetAttr<T>(IChannel channel, AttributeKey<T> key) where T : class
{
var attribute = channel.GetAttribute(key);
return attribute.Get();
}
public static IoSession GetSessionBy(IChannel channel)

@ -11,10 +11,13 @@ public class ClientListenerHandler<TH, TD, TE> : ChannelHandlerAdapter where TH
where TE : ChannelHandlerAdapter, new()
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ClientListenerHandler<TH, TD, TE>));
private bool AutoReconnect { get; set; }
public ClientListenerHandler(TcpClient<TH, TD, TE> client)
public ClientListenerHandler(TcpClient<TH, TD, TE> client, bool autoReconnect)
{
Client = client;
AutoReconnect = autoReconnect;
}
public TcpClient<TH, TD, TE> Client { get; set; }
@ -47,7 +50,10 @@ public class ClientListenerHandler<TH, TD, TE> : 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)

@ -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<TH, TD, TE> where TH : IChannelHandler
public class TcpClient<TH, TD, TE> : IDisposable where TH : IChannelHandler
where TD : ByteToMessageDecoder, new()
where TE : ChannelHandlerAdapter, new()
{
[JsonIgnore]
private static readonly ILog Log = LogManager.GetLogger(typeof(TcpClient<TH, TD, TE>));
[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<TcpSocketChannel>()
.Option(ChannelOption.TcpNodelay, true)
.Handler(new LoggingHandler())
.Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
{
var clientListenerHandler = new ClientListenerHandler<TH, TD, TE>(this);
var clientListenerHandler = new ClientListenerHandler<TH, TD, TE>(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<TH, TD, TE> 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<TH, TD, TE> 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;
}
}

@ -19,7 +19,7 @@ public class ClientMgr
var chargerClient2 = AppInfo.Container.Resolve<ChargerClient>();
chargerClient2.InitBootstrap("127.0.0.1", 9998);
chargerClient2.Connect();
chargerClient2.BaseConnect();
chargerClient2.SessionAttr(12, 12, "12", "2");
chargerClient2.Channel.WriteAndFlushAsync(auth);

Loading…
Cancel
Save