From ff1d0f37c663a80b7ae0c936eb802ecc7e0dad1d Mon Sep 17 00:00:00 2001
From: cli <377476583@qq.com>
Date: Tue, 22 Oct 2024 20:57:26 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../JiShe.CollectBus.ClickHouse.csproj | 13 ++++
.../ServiceCollectionExtensions.cs | 11 ++-
.../JiShe.CollectBus.Console.csproj | 75 ++++++++++---------
JiShe.CollectBus.Console/Program.cs | 1 -
JiShe.CollectBus.Console/appsettings.json | 3 +-
JiShe.CollectBus.Core/Plugins/ClosePlugin.cs | 8 +-
.../Plugins/TcpServiceReceivedPlugin.cs | 33 ++++----
.../Plugins/UdpServiceReceivedPlugin.cs | 4 +-
JiShe.CollectBus.Core/Services/BusService.cs | 4 +-
.../AuditLogs/AuditLog.cs | 1 -
.../Abstracts/BaseProtocolPlugin.cs | 2 +-
.../Interfaces/IProtocolPlugin.cs | 2 +-
.../TestProtocolPlugin.cs | 2 +-
.../StandardProtocolPlugin.cs | 24 +++---
JiShe.CollectBus.sln | 9 ++-
15 files changed, 111 insertions(+), 81 deletions(-)
create mode 100644 JiShe.CollectBus.ClickHouse/JiShe.CollectBus.ClickHouse.csproj
diff --git a/JiShe.CollectBus.ClickHouse/JiShe.CollectBus.ClickHouse.csproj b/JiShe.CollectBus.ClickHouse/JiShe.CollectBus.ClickHouse.csproj
new file mode 100644
index 0000000..77edadd
--- /dev/null
+++ b/JiShe.CollectBus.ClickHouse/JiShe.CollectBus.ClickHouse.csproj
@@ -0,0 +1,13 @@
+
+
+
+ net8.0
+ enable
+ enable
+
+
+
+
+
+
+
diff --git a/JiShe.CollectBus.Console/Extensions/ServiceCollections/ServiceCollectionExtensions.cs b/JiShe.CollectBus.Console/Extensions/ServiceCollections/ServiceCollectionExtensions.cs
index 685ad04..b39c6f3 100644
--- a/JiShe.CollectBus.Console/Extensions/ServiceCollections/ServiceCollectionExtensions.cs
+++ b/JiShe.CollectBus.Console/Extensions/ServiceCollections/ServiceCollectionExtensions.cs
@@ -19,7 +19,14 @@ namespace Microsoft.Extensions.DependencyInjection
config.SetListenIPHosts(int.Parse(configuration["TCP:Port"] ?? "10500"))
.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 =>
{
@@ -50,7 +57,7 @@ namespace Microsoft.Extensions.DependencyInjection
config.SetBindIPHost(int.Parse(configuration["UDP:Port"] ?? "10500"))
.ConfigureContainer(a => //容器的配置顺序应该在最前面
{
- a.AddConsoleLogger(); //添加一个控制台日志注入(注意:在maui中控制台日志不可用)
+ //a.AddConsoleLogger();
})
.ConfigurePlugins(a =>
{
diff --git a/JiShe.CollectBus.Console/JiShe.CollectBus.Console.csproj b/JiShe.CollectBus.Console/JiShe.CollectBus.Console.csproj
index ff9c209..aa9636a 100644
--- a/JiShe.CollectBus.Console/JiShe.CollectBus.Console.csproj
+++ b/JiShe.CollectBus.Console/JiShe.CollectBus.Console.csproj
@@ -1,45 +1,48 @@
-
- Exe
- net8.0
- enable
- enable
-
+
+ Exe
+ net8.0
+ enable
+ enable
+ true
+ true
+
-
-
-
+
+
+
-
-
- Always
-
-
+
+
+ Always
+
+
-
-
-
- all
- runtime; build; native; contentfiles; analyzers; buildtransitive
-
-
-
-
+
+
+
+ all
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
-
-
- Always
-
-
- Always
-
-
+
+
+ Always
+
+
+ Always
+
+
diff --git a/JiShe.CollectBus.Console/Program.cs b/JiShe.CollectBus.Console/Program.cs
index 8822f43..c26b9fa 100644
--- a/JiShe.CollectBus.Console/Program.cs
+++ b/JiShe.CollectBus.Console/Program.cs
@@ -33,7 +33,6 @@ namespace JiShe.CollectBus.Console
services.PluginServiceRegister();
services.AddTcp(configuration);
services.AddUdp(configuration);
-
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = configuration["RedisCache:ConnectionString"];
diff --git a/JiShe.CollectBus.Console/appsettings.json b/JiShe.CollectBus.Console/appsettings.json
index 46483bc..27b20f5 100644
--- a/JiShe.CollectBus.Console/appsettings.json
+++ b/JiShe.CollectBus.Console/appsettings.json
@@ -1,6 +1,7 @@
{
"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": {
diff --git a/JiShe.CollectBus.Core/Plugins/ClosePlugin.cs b/JiShe.CollectBus.Core/Plugins/ClosePlugin.cs
index 46ee464..df99a94 100644
--- a/JiShe.CollectBus.Core/Plugins/ClosePlugin.cs
+++ b/JiShe.CollectBus.Core/Plugins/ClosePlugin.cs
@@ -4,9 +4,10 @@ using TouchSocket.Sockets;
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
{
@@ -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)
{
throw new NotImplementedException();
diff --git a/JiShe.CollectBus.Core/Plugins/TcpServiceReceivedPlugin.cs b/JiShe.CollectBus.Core/Plugins/TcpServiceReceivedPlugin.cs
index 1240510..9e42266 100644
--- a/JiShe.CollectBus.Core/Plugins/TcpServiceReceivedPlugin.cs
+++ b/JiShe.CollectBus.Core/Plugins/TcpServiceReceivedPlugin.cs
@@ -5,10 +5,10 @@ using TouchSocket.Sockets;
namespace JiShe.CollectBus.Core.Plugins
{
- public class TcpServiceReceivedPlugin(IServiceProvider serviceProvider) : PluginBase, ITcpReceivedPlugin,
- ITcpConnectingPlugin, ITcpConnectedPlugin, ITcpClosedPlugin
+ public partial class TcpServiceReceivedPlugin(IServiceProvider serviceProvider) : PluginBase
{
- 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: 水表主站到集中器的协议分为118和645-97协议
@@ -18,43 +18,36 @@ namespace JiShe.CollectBus.Core.Plugins
var protocolPlugin = serviceProvider.GetKeyedService(protocolType);
client.Logger.Info($"{protocolPlugin?.Get().Name},{protocolPlugin?.Get().RegularExpression}");
-
//从客户端收到信息
var messageHexString = Convert.ToHexString(e.ByteBlock.Span);
client.Logger.Info($"[TCP] 已从{client.GetIPPort()}接收到信息:{messageHexString}");
- protocolPlugin?.Received(e);
+ protocolPlugin?.Received(client,e);
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:{sessionClient.Id} IP:{client.GetIPPort()}正在连接中...");
- }
+ client.Logger.Info($"[TCP] ID:{client.Id} IP:{client.GetIPPort()}正在连接中...");
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:{sessionClient.Id} IP:{client.GetIPPort()}已连接");
- }
+ client.Logger.Info($"[TCP] ID:{client.Id} IP:{client.GetIPPort()}已连接");
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:{sessionClient.Id} IP:{client.GetIPPort()}已关闭连接");
+ client.Logger.Info($"[TCP] ID:{client.Id} IP:{client.GetIPPort()}已关闭连接");
- }
await e.InvokeNext();
}
}
diff --git a/JiShe.CollectBus.Core/Plugins/UdpServiceReceivedPlugin.cs b/JiShe.CollectBus.Core/Plugins/UdpServiceReceivedPlugin.cs
index e98000c..186cd11 100644
--- a/JiShe.CollectBus.Core/Plugins/UdpServiceReceivedPlugin.cs
+++ b/JiShe.CollectBus.Core/Plugins/UdpServiceReceivedPlugin.cs
@@ -4,11 +4,11 @@ using TouchSocket.Sockets;
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)
{
-
var udpSession = client as UdpSession;
udpSession?.Logger.Info($"[UDP] 收到:{e.ByteBlock.Span.ToString(Encoding.UTF8)}");
await udpSession.SendAsync("[UDP] 收到");
diff --git a/JiShe.CollectBus.Core/Services/BusService.cs b/JiShe.CollectBus.Core/Services/BusService.cs
index ef306f5..9f1253e 100644
--- a/JiShe.CollectBus.Core/Services/BusService.cs
+++ b/JiShe.CollectBus.Core/Services/BusService.cs
@@ -8,8 +8,9 @@ using TouchSocket.Sockets;
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)
{
switch (sender)
@@ -34,6 +35,7 @@ namespace JiShe.CollectBus.Core.Services
return e.InvokeNext();
}
+ [GeneratorPlugin(typeof(IServerStopedPlugin))]
public Task OnServerStoped(IServiceBase sender, ServiceStateEventArgs e)
{
Console.WriteLine("服务已停止");
diff --git a/JiShe.CollectBus.EntityFrameworkCore/AuditLogs/AuditLog.cs b/JiShe.CollectBus.EntityFrameworkCore/AuditLogs/AuditLog.cs
index 28faa98..f84ec99 100644
--- a/JiShe.CollectBus.EntityFrameworkCore/AuditLogs/AuditLog.cs
+++ b/JiShe.CollectBus.EntityFrameworkCore/AuditLogs/AuditLog.cs
@@ -9,7 +9,6 @@ namespace JiShe.CollectBus.EntityFrameworkCore.AuditLogs
{
public class AuditLog : EntityBase,ICreationAudited
{
- public string Re
public long? CreatorId { get; set; }
public DateTime CreationTime { get; set; }
}
diff --git a/JiShe.CollectBus.Protocol.Contracts/Abstracts/BaseProtocolPlugin.cs b/JiShe.CollectBus.Protocol.Contracts/Abstracts/BaseProtocolPlugin.cs
index 838f9cc..33e823b 100644
--- a/JiShe.CollectBus.Protocol.Contracts/Abstracts/BaseProtocolPlugin.cs
+++ b/JiShe.CollectBus.Protocol.Contracts/Abstracts/BaseProtocolPlugin.cs
@@ -17,7 +17,7 @@ namespace JiShe.CollectBus.Protocol.Contracts.Abstracts
public abstract ProtocolInfo Get();
- public abstract void Received(ReceivedDataEventArgs e);
+ public abstract void Received(ITcpSessionClient client, ReceivedDataEventArgs e);
public abstract void Send();
diff --git a/JiShe.CollectBus.Protocol.Contracts/Interfaces/IProtocolPlugin.cs b/JiShe.CollectBus.Protocol.Contracts/Interfaces/IProtocolPlugin.cs
index ecbd143..54bb050 100644
--- a/JiShe.CollectBus.Protocol.Contracts/Interfaces/IProtocolPlugin.cs
+++ b/JiShe.CollectBus.Protocol.Contracts/Interfaces/IProtocolPlugin.cs
@@ -9,7 +9,7 @@ namespace JiShe.CollectBus.Protocol.Contracts.Interfaces
void Load();
- void Received(ReceivedDataEventArgs e);
+ void Received(ITcpSessionClient client, ReceivedDataEventArgs e);
void Send();
}
diff --git a/JiShe.CollectBus.Protocol.Test/TestProtocolPlugin.cs b/JiShe.CollectBus.Protocol.Test/TestProtocolPlugin.cs
index daa7e26..f3c0870 100644
--- a/JiShe.CollectBus.Protocol.Test/TestProtocolPlugin.cs
+++ b/JiShe.CollectBus.Protocol.Test/TestProtocolPlugin.cs
@@ -15,7 +15,7 @@ namespace JiShe.CollectBus.Protocol.Test
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();
}
diff --git a/JiShe.CollectBus.Protocol/StandardProtocolPlugin.cs b/JiShe.CollectBus.Protocol/StandardProtocolPlugin.cs
index 643c130..909c710 100644
--- a/JiShe.CollectBus.Protocol/StandardProtocolPlugin.cs
+++ b/JiShe.CollectBus.Protocol/StandardProtocolPlugin.cs
@@ -26,6 +26,8 @@ namespace JiShe.CollectBus.Protocol
static List MSA = new List();
static Dictionary> usingMSA = new Dictionary>();
+ private ITcpSessionClient tcpSessionClient;
+
static StandardProtocolPlugin()
{
for (int i = 1; i <= 127; i++)
@@ -37,7 +39,7 @@ namespace JiShe.CollectBus.Protocol
public override ProtocolInfo Get()
{
- return new ProtocolInfo("Standard", "376.1", "TCP","376.1协议","DTS1980");
+ return new ProtocolInfo("Standard", "376.1", "TCP", "376.1协议", "DTS1980");
}
public new void Load()
@@ -45,8 +47,9 @@ namespace JiShe.CollectBus.Protocol
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 cmdResult = AnalysisCmd(messageHexString);
if (cmdResult == null)
@@ -54,7 +57,6 @@ namespace JiShe.CollectBus.Protocol
return;
}
AnalysisData(cmdResult);
-
}
public override void Send()
@@ -170,7 +172,7 @@ namespace JiShe.CollectBus.Protocol
userDataIndex += 1;
var dt2 = DataConvert.HexToDec(hexStringList[userDataIndex]);
userDataIndex += 1;
- var fn = dt2 * 8 + dt1;
+ var fn = dt2 * 8 + dt1;
//数据单元
var datas = hexStringList.Skip(userDataIndex).Take(len + hearderLen - userDataIndex).ToList();
@@ -282,7 +284,7 @@ namespace JiShe.CollectBus.Protocol
{
TpV = TpV.附加信息域中无时间标签,
FIRFIN = FIRFIN.单帧,
- CON = CON.不需要对该帧进行确认,
+ CON = CON.需要对该帧进行确认,
PRSEQ = commandReulst.Seq.PRSEQ,
},
MSA = commandReulst.MSA,
@@ -290,6 +292,8 @@ namespace JiShe.CollectBus.Protocol
Fn = 1
};
commandReulst.ReplyBytes = GetCommandBytes(reqParam);
+ tcpSessionClient.SendAsync(tcpSessionClient.Id,commandReulst.ReplyBytes);
+
}
else if (commandReulst.Fn == 2)//退出登录
{
@@ -524,9 +528,9 @@ namespace JiShe.CollectBus.Protocol
///
///
///
- public void GetAmmterReading(string a,int pn)
+ public void GetAmmterReading(string a, int pn)
{
- var bytes = GetCommandBytes(new ReqParameter2()
+ var bytes = GetCommandBytes(new ReqParameter2()
{
AFN = AFN.请求实时数据,
FunCode = (int)CMasterStationFunCode.请求2级数据,
@@ -543,7 +547,7 @@ namespace JiShe.CollectBus.Protocol
Fn = 129
});
}
-
+
///
/// 组装电表阀控
///
@@ -717,7 +721,7 @@ namespace JiShe.CollectBus.Protocol
//TODO:主站地址和组地址标志
var a3Bin = $"{DataConvert.DecToBin(mSA).PadLeft(7, '0')}0";
- list.Add(DataConvert.BinToHex(a3Bin).PadLeft(2,'0'));
+ list.Add(DataConvert.BinToHex(a3Bin).PadLeft(2, '0'));
return list;
}
@@ -1006,6 +1010,6 @@ namespace JiShe.CollectBus.Protocol
#endregion
-
+
}
}
diff --git a/JiShe.CollectBus.sln b/JiShe.CollectBus.sln
index 7e140af..ece59cf 100644
--- a/JiShe.CollectBus.sln
+++ b/JiShe.CollectBus.sln
@@ -19,7 +19,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "core", "core", "{C7DEC9FB-3
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}"
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
Global
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}.Release|Any CPU.ActiveCfg = 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
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -67,6 +73,7 @@ Global
{1D3A5A4E-B977-4E33-A1AF-62508110C3B7} = {C7DEC9FB-3F75-4584-85B0-16EA3CB222E5}
{289196B4-FFBE-4E40-A3A1-FCFADBE945ED} = {3A04FB29-EA75-4499-BBF3-AF24C7D46A1D}
{16D42BCF-EDB8-4153-B37D-0B10FB6DF36C} = {C7DEC9FB-3F75-4584-85B0-16EA3CB222E5}
+ {65A2837C-A5EE-475B-8079-EE5A1BCD2E8F} = {C7DEC9FB-3F75-4584-85B0-16EA3CB222E5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {33261859-9CD1-4A43-B181-AB75C247D1CD}