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"))
.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 =>
{

View File

@ -1,45 +1,48 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.10" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.10" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\JiShe.CollectBus.Core\JiShe.CollectBus.Core.csproj" />
<ProjectReference Include="..\JiShe.CollectBus.EntityFrameworkCore\JiShe.CollectBus.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\JiShe.CollectBus.Protocol.Contracts\JiShe.CollectBus.Protocol.Contracts.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\JiShe.CollectBus.ClickHouse\JiShe.CollectBus.ClickHouse.csproj" />
<ProjectReference Include="..\JiShe.CollectBus.Core\JiShe.CollectBus.Core.csproj" />
<ProjectReference Include="..\JiShe.CollectBus.EntityFrameworkCore\JiShe.CollectBus.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\JiShe.CollectBus.Protocol.Contracts\JiShe.CollectBus.Protocol.Contracts.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="Plugins\JiShe.CollectBus.Protocol.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Plugins\JiShe.CollectBus.Protocol.Test.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update="Plugins\JiShe.CollectBus.Protocol.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Plugins\JiShe.CollectBus.Protocol.Test.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

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

View File

@ -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": {

View File

@ -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();

View File

@ -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<IProtocolPlugin>(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();
}
}

View File

@ -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] 收到");

View File

@ -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("服务已停止");

View File

@ -9,7 +9,6 @@ namespace JiShe.CollectBus.EntityFrameworkCore.AuditLogs
{
public class AuditLog : EntityBase<Guid>,ICreationAudited<long?>
{
public string Re
public long? CreatorId { 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 void Received(ReceivedDataEventArgs e);
public abstract void Received(ITcpSessionClient client, ReceivedDataEventArgs e);
public abstract void Send();

View File

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

View File

@ -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();
}

View File

@ -26,6 +26,8 @@ namespace JiShe.CollectBus.Protocol
static List<int> MSA = new List<int>();
static Dictionary<string, List<int>> usingMSA = new Dictionary<string, List<int>>();
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
/// </summary>
/// <param name="a"></param>
/// <param name="pn"></param>
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,
@ -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;
}

View File

@ -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}