This commit is contained in:
cli 2024-10-22 20:57:26 +08:00
parent 2c4f401a55
commit ff1d0f37c6
15 changed files with 111 additions and 81 deletions

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
</ItemGroup>
</Project>

View File

@ -19,7 +19,14 @@ namespace Microsoft.Extensions.DependencyInjection
config.SetListenIPHosts(int.Parse(configuration["TCP:Port"] ?? "10500")) config.SetListenIPHosts(int.Parse(configuration["TCP:Port"] ?? "10500"))
.ConfigureContainer(a => //容器的配置顺序应该在最前面 .ConfigureContainer(a => //容器的配置顺序应该在最前面
{ {
a.AddConsoleLogger(); //添加一个控制台日志注入注意在maui中控制台日志不可用
//a.AddFileLogger(fileLogger =>
//{
// fileLogger.MaxSize = 1024 * 1024;
// fileLogger.LogLevel = LogLevel.Debug;
// fileLogger.CreateLogFolder = level => $"logs\\{DateTime.Now:yyyy-MM-dd}\\{level}";
//});
a.AddConsoleLogger();
}) })
.ConfigurePlugins(a => .ConfigurePlugins(a =>
{ {
@ -50,7 +57,7 @@ namespace Microsoft.Extensions.DependencyInjection
config.SetBindIPHost(int.Parse(configuration["UDP:Port"] ?? "10500")) config.SetBindIPHost(int.Parse(configuration["UDP:Port"] ?? "10500"))
.ConfigureContainer(a => //容器的配置顺序应该在最前面 .ConfigureContainer(a => //容器的配置顺序应该在最前面
{ {
a.AddConsoleLogger(); //添加一个控制台日志注入注意在maui中控制台日志不可用 //a.AddConsoleLogger();
}) })
.ConfigurePlugins(a => .ConfigurePlugins(a =>
{ {

View File

@ -5,6 +5,8 @@
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@ -28,6 +30,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\JiShe.CollectBus.ClickHouse\JiShe.CollectBus.ClickHouse.csproj" />
<ProjectReference Include="..\JiShe.CollectBus.Core\JiShe.CollectBus.Core.csproj" /> <ProjectReference Include="..\JiShe.CollectBus.Core\JiShe.CollectBus.Core.csproj" />
<ProjectReference Include="..\JiShe.CollectBus.EntityFrameworkCore\JiShe.CollectBus.EntityFrameworkCore.csproj" /> <ProjectReference Include="..\JiShe.CollectBus.EntityFrameworkCore\JiShe.CollectBus.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\JiShe.CollectBus.Protocol.Contracts\JiShe.CollectBus.Protocol.Contracts.csproj" /> <ProjectReference Include="..\JiShe.CollectBus.Protocol.Contracts\JiShe.CollectBus.Protocol.Contracts.csproj" />

View File

@ -33,7 +33,6 @@ namespace JiShe.CollectBus.Console
services.PluginServiceRegister(); services.PluginServiceRegister();
services.AddTcp(configuration); services.AddTcp(configuration);
services.AddUdp(configuration); services.AddUdp(configuration);
services.AddStackExchangeRedisCache(options => services.AddStackExchangeRedisCache(options =>
{ {
options.Configuration = configuration["RedisCache:ConnectionString"]; options.Configuration = configuration["RedisCache:ConnectionString"];

View File

@ -1,6 +1,7 @@
{ {
"ConnectionStrings": { "ConnectionStrings": {
"Default": "Data Source=192.168.111.248;Port=3306;Database=JiSheCollectBus;uid=root;pwd=123456abcD;charset=utf8mb4;Allow User Variables=true;AllowLoadLocalInfile=true" "Default": "Data Source=192.168.111.248;Port=3306;Database=JiSheCollectBus;uid=root;pwd=123456abcD;charset=utf8mb4;Allow User Variables=true;AllowLoadLocalInfile=true",
"ClickHouse": "host=localhost;port=8123;user=default;password=;database=default"
}, },
"TCP": { "TCP": {

View File

@ -4,9 +4,10 @@ using TouchSocket.Sockets;
namespace JiShe.CollectBus.Core.Plugins namespace JiShe.CollectBus.Core.Plugins
{ {
public class TcpClosePlugin(ILog logger) : PluginBase, ITcpReceivedPlugin public partial class TcpClosePlugin(ILog logger) : PluginBase
{ {
public async Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e) [GeneratorPlugin(typeof(ITcpReceivedPlugin))]
public async Task OnTcpReceived(ITcpSessionClient client, ReceivedDataEventArgs e)
{ {
try try
{ {
@ -27,8 +28,9 @@ namespace JiShe.CollectBus.Core.Plugins
} }
} }
public class UdpClosePlugin(ILog logger) : PluginBase, IUdpReceivedPlugin public partial class UdpClosePlugin(ILog logger) : PluginBase
{ {
[GeneratorPlugin(typeof(IUdpReceivedPlugin))]
public Task OnUdpReceived(IUdpSessionBase client, UdpReceivedDataEventArgs e) public Task OnUdpReceived(IUdpSessionBase client, UdpReceivedDataEventArgs e)
{ {
throw new NotImplementedException(); throw new NotImplementedException();

View File

@ -5,10 +5,10 @@ using TouchSocket.Sockets;
namespace JiShe.CollectBus.Core.Plugins namespace JiShe.CollectBus.Core.Plugins
{ {
public class TcpServiceReceivedPlugin(IServiceProvider serviceProvider) : PluginBase, ITcpReceivedPlugin, public partial class TcpServiceReceivedPlugin(IServiceProvider serviceProvider) : PluginBase
ITcpConnectingPlugin, ITcpConnectedPlugin, ITcpClosedPlugin
{ {
public async Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e) [GeneratorPlugin(typeof(ITcpReceivedPlugin))]
public async Task OnTcpReceived(ITcpSessionClient client, ReceivedDataEventArgs e)
{ {
//TODO: 电表主站到集中器的协议都是376.1协议集中器下发到电表协议分为645-07和modbus //TODO: 电表主站到集中器的协议都是376.1协议集中器下发到电表协议分为645-07和modbus
//TODO: 水表主站到集中器的协议分为118和645-97协议 //TODO: 水表主站到集中器的协议分为118和645-97协议
@ -18,43 +18,36 @@ namespace JiShe.CollectBus.Core.Plugins
var protocolPlugin = serviceProvider.GetKeyedService<IProtocolPlugin>(protocolType); var protocolPlugin = serviceProvider.GetKeyedService<IProtocolPlugin>(protocolType);
client.Logger.Info($"{protocolPlugin?.Get().Name},{protocolPlugin?.Get().RegularExpression}"); client.Logger.Info($"{protocolPlugin?.Get().Name},{protocolPlugin?.Get().RegularExpression}");
//从客户端收到信息 //从客户端收到信息
var messageHexString = Convert.ToHexString(e.ByteBlock.Span); var messageHexString = Convert.ToHexString(e.ByteBlock.Span);
client.Logger.Info($"[TCP] 已从{client.GetIPPort()}接收到信息:{messageHexString}"); client.Logger.Info($"[TCP] 已从{client.GetIPPort()}接收到信息:{messageHexString}");
protocolPlugin?.Received(e); protocolPlugin?.Received(client,e);
await e.InvokeNext(); await e.InvokeNext();
} }
public async Task OnTcpConnecting(ITcpSession client, ConnectingEventArgs e) [GeneratorPlugin(typeof(ITcpConnectingPlugin))]
public async Task OnTcpConnecting(ITcpSessionClient client, ConnectingEventArgs e)
{ {
if (client is ITcpSessionClient sessionClient) client.Logger.Info($"[TCP] ID:{client.Id} IP:{client.GetIPPort()}正在连接中...");
{
client.Logger.Info($"[TCP] ID:{sessionClient.Id} IP:{client.GetIPPort()}正在连接中...");
}
await e.InvokeNext(); await e.InvokeNext();
} }
public async Task OnTcpConnected(ITcpSession client, ConnectedEventArgs e) [GeneratorPlugin(typeof(ITcpConnectedPlugin))]
public async Task OnTcpConnected(ITcpSessionClient client, ConnectedEventArgs e)
{ {
if (client is ITcpSessionClient sessionClient) client.Logger.Info($"[TCP] ID:{client.Id} IP:{client.GetIPPort()}已连接");
{
client.Logger.Info($"[TCP] ID:{sessionClient.Id} IP:{client.GetIPPort()}已连接");
}
await e.InvokeNext(); await e.InvokeNext();
} }
public async Task OnTcpClosed(ITcpSession client, ClosedEventArgs e) [GeneratorPlugin(typeof(ITcpClosedPlugin))]
public async Task OnTcpClosed(ITcpSessionClient client, ClosedEventArgs e)
{ {
if (client is ITcpSessionClient sessionClient) client.Logger.Info($"[TCP] ID:{client.Id} IP:{client.GetIPPort()}已关闭连接");
{
client.Logger.Info($"[TCP] ID:{sessionClient.Id} IP:{client.GetIPPort()}已关闭连接");
}
await e.InvokeNext(); await e.InvokeNext();
} }
} }

View File

@ -4,11 +4,11 @@ using TouchSocket.Sockets;
namespace JiShe.CollectBus.Core.Plugins namespace JiShe.CollectBus.Core.Plugins
{ {
public class UdpServiceReceivedPlugin : PluginBase, IUdpReceivedPlugin public partial class UdpServiceReceivedPlugin : PluginBase
{ {
[GeneratorPlugin(typeof(IUdpReceivedPlugin))]
public async Task OnUdpReceived(IUdpSessionBase client, UdpReceivedDataEventArgs e) public async Task OnUdpReceived(IUdpSessionBase client, UdpReceivedDataEventArgs e)
{ {
var udpSession = client as UdpSession; var udpSession = client as UdpSession;
udpSession?.Logger.Info($"[UDP] 收到:{e.ByteBlock.Span.ToString(Encoding.UTF8)}"); udpSession?.Logger.Info($"[UDP] 收到:{e.ByteBlock.Span.ToString(Encoding.UTF8)}");
await udpSession.SendAsync("[UDP] 收到"); await udpSession.SendAsync("[UDP] 收到");

View File

@ -8,8 +8,9 @@ using TouchSocket.Sockets;
namespace JiShe.CollectBus.Core.Services namespace JiShe.CollectBus.Core.Services
{ {
public class BusService : PluginBase, IServerStartedPlugin, IServerStopedPlugin public partial class BusService : PluginBase
{ {
[GeneratorPlugin(typeof(IServerStartedPlugin))]
public Task OnServerStarted(IServiceBase sender, ServiceStateEventArgs e) public Task OnServerStarted(IServiceBase sender, ServiceStateEventArgs e)
{ {
switch (sender) switch (sender)
@ -34,6 +35,7 @@ namespace JiShe.CollectBus.Core.Services
return e.InvokeNext(); return e.InvokeNext();
} }
[GeneratorPlugin(typeof(IServerStopedPlugin))]
public Task OnServerStoped(IServiceBase sender, ServiceStateEventArgs e) public Task OnServerStoped(IServiceBase sender, ServiceStateEventArgs e)
{ {
Console.WriteLine("服务已停止"); Console.WriteLine("服务已停止");

View File

@ -9,7 +9,6 @@ namespace JiShe.CollectBus.EntityFrameworkCore.AuditLogs
{ {
public class AuditLog : EntityBase<Guid>,ICreationAudited<long?> public class AuditLog : EntityBase<Guid>,ICreationAudited<long?>
{ {
public string Re
public long? CreatorId { get; set; } public long? CreatorId { get; set; }
public DateTime CreationTime { get; set; } public DateTime CreationTime { get; set; }
} }

View File

@ -17,7 +17,7 @@ namespace JiShe.CollectBus.Protocol.Contracts.Abstracts
public abstract ProtocolInfo Get(); public abstract ProtocolInfo Get();
public abstract void Received(ReceivedDataEventArgs e); public abstract void Received(ITcpSessionClient client, ReceivedDataEventArgs e);
public abstract void Send(); public abstract void Send();

View File

@ -9,7 +9,7 @@ namespace JiShe.CollectBus.Protocol.Contracts.Interfaces
void Load(); void Load();
void Received(ReceivedDataEventArgs e); void Received(ITcpSessionClient client, ReceivedDataEventArgs e);
void Send(); void Send();
} }

View File

@ -15,7 +15,7 @@ namespace JiShe.CollectBus.Protocol.Test
return new ProtocolInfo("Test", "376.1", "TCP", "376.1协议", "DTSU1980"); return new ProtocolInfo("Test", "376.1", "TCP", "376.1协议", "DTSU1980");
} }
public override void Received(ReceivedDataEventArgs e) public override void Received(ITcpSessionClient client, ReceivedDataEventArgs e)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -26,6 +26,8 @@ namespace JiShe.CollectBus.Protocol
static List<int> MSA = new List<int>(); static List<int> MSA = new List<int>();
static Dictionary<string, List<int>> usingMSA = new Dictionary<string, List<int>>(); static Dictionary<string, List<int>> usingMSA = new Dictionary<string, List<int>>();
private ITcpSessionClient tcpSessionClient;
static StandardProtocolPlugin() static StandardProtocolPlugin()
{ {
for (int i = 1; i <= 127; i++) for (int i = 1; i <= 127; i++)
@ -45,8 +47,9 @@ namespace JiShe.CollectBus.Protocol
base.Load(); base.Load();
} }
public override void Received(ReceivedDataEventArgs e) public override void Received(ITcpSessionClient client, ReceivedDataEventArgs e)
{ {
tcpSessionClient = client;
var messageHexString = Convert.ToHexString(e.ByteBlock.Span); var messageHexString = Convert.ToHexString(e.ByteBlock.Span);
var cmdResult = AnalysisCmd(messageHexString); var cmdResult = AnalysisCmd(messageHexString);
if (cmdResult == null) if (cmdResult == null)
@ -54,7 +57,6 @@ namespace JiShe.CollectBus.Protocol
return; return;
} }
AnalysisData(cmdResult); AnalysisData(cmdResult);
} }
public override void Send() public override void Send()
@ -282,7 +284,7 @@ namespace JiShe.CollectBus.Protocol
{ {
TpV = TpV., TpV = TpV.,
FIRFIN = FIRFIN., FIRFIN = FIRFIN.,
CON = CON., CON = CON.,
PRSEQ = commandReulst.Seq.PRSEQ, PRSEQ = commandReulst.Seq.PRSEQ,
}, },
MSA = commandReulst.MSA, MSA = commandReulst.MSA,
@ -290,6 +292,8 @@ namespace JiShe.CollectBus.Protocol
Fn = 1 Fn = 1
}; };
commandReulst.ReplyBytes = GetCommandBytes(reqParam); commandReulst.ReplyBytes = GetCommandBytes(reqParam);
tcpSessionClient.SendAsync(tcpSessionClient.Id,commandReulst.ReplyBytes);
} }
else if (commandReulst.Fn == 2)//退出登录 else if (commandReulst.Fn == 2)//退出登录
{ {

View File

@ -19,7 +19,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "core", "core", "{C7DEC9FB-3
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JiShe.CollectBus.Protocol.Test", "JiShe.CollectBus.Protocol.Test\JiShe.CollectBus.Protocol.Test.csproj", "{289196B4-FFBE-4E40-A3A1-FCFADBE945ED}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JiShe.CollectBus.Protocol.Test", "JiShe.CollectBus.Protocol.Test\JiShe.CollectBus.Protocol.Test.csproj", "{289196B4-FFBE-4E40-A3A1-FCFADBE945ED}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JiShe.CollectBus.EntityFrameworkCore", "JiShe.CollectBus.EntityFrameworkCore\JiShe.CollectBus.EntityFrameworkCore.csproj", "{16D42BCF-EDB8-4153-B37D-0B10FB6DF36C}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JiShe.CollectBus.EntityFrameworkCore", "JiShe.CollectBus.EntityFrameworkCore\JiShe.CollectBus.EntityFrameworkCore.csproj", "{16D42BCF-EDB8-4153-B37D-0B10FB6DF36C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JiShe.CollectBus.ClickHouse", "JiShe.CollectBus.ClickHouse\JiShe.CollectBus.ClickHouse.csproj", "{65A2837C-A5EE-475B-8079-EE5A1BCD2E8F}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -55,6 +57,10 @@ Global
{16D42BCF-EDB8-4153-B37D-0B10FB6DF36C}.Debug|Any CPU.Build.0 = Debug|Any CPU {16D42BCF-EDB8-4153-B37D-0B10FB6DF36C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{16D42BCF-EDB8-4153-B37D-0B10FB6DF36C}.Release|Any CPU.ActiveCfg = Release|Any CPU {16D42BCF-EDB8-4153-B37D-0B10FB6DF36C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{16D42BCF-EDB8-4153-B37D-0B10FB6DF36C}.Release|Any CPU.Build.0 = Release|Any CPU {16D42BCF-EDB8-4153-B37D-0B10FB6DF36C}.Release|Any CPU.Build.0 = Release|Any CPU
{65A2837C-A5EE-475B-8079-EE5A1BCD2E8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{65A2837C-A5EE-475B-8079-EE5A1BCD2E8F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65A2837C-A5EE-475B-8079-EE5A1BCD2E8F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65A2837C-A5EE-475B-8079-EE5A1BCD2E8F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -67,6 +73,7 @@ Global
{1D3A5A4E-B977-4E33-A1AF-62508110C3B7} = {C7DEC9FB-3F75-4584-85B0-16EA3CB222E5} {1D3A5A4E-B977-4E33-A1AF-62508110C3B7} = {C7DEC9FB-3F75-4584-85B0-16EA3CB222E5}
{289196B4-FFBE-4E40-A3A1-FCFADBE945ED} = {3A04FB29-EA75-4499-BBF3-AF24C7D46A1D} {289196B4-FFBE-4E40-A3A1-FCFADBE945ED} = {3A04FB29-EA75-4499-BBF3-AF24C7D46A1D}
{16D42BCF-EDB8-4153-B37D-0B10FB6DF36C} = {C7DEC9FB-3F75-4584-85B0-16EA3CB222E5} {16D42BCF-EDB8-4153-B37D-0B10FB6DF36C} = {C7DEC9FB-3F75-4584-85B0-16EA3CB222E5}
{65A2837C-A5EE-475B-8079-EE5A1BCD2E8F} = {C7DEC9FB-3F75-4584-85B0-16EA3CB222E5}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {33261859-9CD1-4A43-B181-AB75C247D1CD} SolutionGuid = {33261859-9CD1-4A43-B181-AB75C247D1CD}