diff --git a/simples/JT808.DotNetty.SimpleClient/Services/UpService.cs b/simples/JT808.DotNetty.SimpleClient/Services/UpService.cs
index 38eae07..01b123b 100644
--- a/simples/JT808.DotNetty.SimpleClient/Services/UpService.cs
+++ b/simples/JT808.DotNetty.SimpleClient/Services/UpService.cs
@@ -20,7 +20,7 @@ namespace JT808.DotNetty.SimpleClient.Services
public Task StartAsync(CancellationToken cancellationToken)
{
- JT808TcpClient client1 = jT808TcpClientFactory.Create(new DeviceConfig("12345678910", "127.0.0.1", 808));
+ JT808TcpClient client1 = jT808TcpClientFactory.Create(new JT808DeviceConfig("12345678910", "127.0.0.1", 808));
//1.终端注册
client1.Send(new JT808_0x0100()
{
diff --git a/src/JT808.DotNetty.CleintBenchmark/JT808.DotNetty.CleintBenchmark.csproj b/src/JT808.DotNetty.CleintBenchmark/JT808.DotNetty.CleintBenchmark.csproj
index 8b5a8a0..74f2ebe 100644
--- a/src/JT808.DotNetty.CleintBenchmark/JT808.DotNetty.CleintBenchmark.csproj
+++ b/src/JT808.DotNetty.CleintBenchmark/JT808.DotNetty.CleintBenchmark.csproj
@@ -2,7 +2,7 @@
Exe
- netcoreapp2.2
+ netcoreapp3.0
7.3
@@ -34,4 +34,6 @@
+
+
diff --git a/src/JT808.DotNetty.CleintBenchmark/Program.cs b/src/JT808.DotNetty.CleintBenchmark/Program.cs
index 45d5d8f..97eb163 100644
--- a/src/JT808.DotNetty.CleintBenchmark/Program.cs
+++ b/src/JT808.DotNetty.CleintBenchmark/Program.cs
@@ -43,7 +43,6 @@ namespace JT808.DotNetty.CleintBenchmark
services.AddJT808Configure()
.AddJT808Client();
services.AddHostedService();
- services.AddHostedService();
});
await serverHostBuilder.RunConsoleAsync();
}
diff --git a/src/JT808.DotNetty.CleintBenchmark/Services/CleintBenchmarkHostedService.cs b/src/JT808.DotNetty.CleintBenchmark/Services/CleintBenchmarkHostedService.cs
index ce7a395..7644dc7 100644
--- a/src/JT808.DotNetty.CleintBenchmark/Services/CleintBenchmarkHostedService.cs
+++ b/src/JT808.DotNetty.CleintBenchmark/Services/CleintBenchmarkHostedService.cs
@@ -49,7 +49,7 @@ namespace JT808.DotNetty.CleintBenchmark.Services
{
taskFactory.StartNew((item) =>
{
- var client = jT808TcpClientFactory.Create(new DeviceConfig(((int)item).ToString(), clientBenchmarkOptions.IP, clientBenchmarkOptions.Port));
+ var client = jT808TcpClientFactory.Create(new JT808DeviceConfig(((int)item+1).ToString(), clientBenchmarkOptions.IP, clientBenchmarkOptions.Port));
int lat = new Random(1000).Next(100000, 180000);
int Lng = new Random(1000).Next(100000, 180000);
while (!cts.IsCancellationRequested)
diff --git a/src/JT808.DotNetty.CleintBenchmark/Services/CleintBenchmarkReportHostedService.cs b/src/JT808.DotNetty.CleintBenchmark/Services/CleintBenchmarkReportHostedService.cs
deleted file mode 100644
index 6eddfdd..0000000
--- a/src/JT808.DotNetty.CleintBenchmark/Services/CleintBenchmarkReportHostedService.cs
+++ /dev/null
@@ -1,55 +0,0 @@
-using JT808.DotNetty.CleintBenchmark.Configs;
-using JT808.DotNetty.Client;
-using JT808.DotNetty.Client.Services;
-using JT808.Protocol.MessageBody;
-using Microsoft.Extensions.Hosting;
-using Microsoft.Extensions.Logging;
-using Microsoft.Extensions.Options;
-using Newtonsoft.Json;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading;
-using System.Threading.Tasks;
-
-
-namespace JT808.DotNetty.CleintBenchmark.Services
-{
- public class CleintBenchmarkReportHostedService : IHostedService
- {
- private readonly JT808ReportService jT808ReportService;
-
- private CancellationTokenSource cts=new CancellationTokenSource();
-
- private readonly ILogger logger;
- public CleintBenchmarkReportHostedService(
- ILoggerFactory loggerFactory,
- JT808ReportService jT808ReportService)
- {
- this.jT808ReportService = jT808ReportService;
- logger = loggerFactory.CreateLogger("CleintBenchmarkReportHostedService");
- }
- public Task StartAsync(CancellationToken cancellationToken)
- {
- logger.LogInformation("StartAsync...");
- Task.Run(() => {
- while (!cts.IsCancellationRequested)
- {
- logger.LogInformation(JsonConvert.SerializeObject(jT808ReportService.JT808Reports.LastOrDefault()));
- Thread.Sleep(3000);
- }
- }, cts.Token);
- return Task.CompletedTask;
- }
-
- public Task StopAsync(CancellationToken cancellationToken)
- {
- logger.LogInformation("StopAsync...");
- cts.Cancel();
- logger.LogInformation("正在生成报表...");
- logger.LogInformation(JsonConvert.SerializeObject(jT808ReportService.JT808Reports,Formatting.Indented));
- return Task.CompletedTask;
- }
- }
-}
diff --git a/src/JT808.DotNetty.Client/JT808ClientDotnettyExtensions.cs b/src/JT808.DotNetty.Client/JT808ClientDotnettyExtensions.cs
index aa22498..f935bcc 100644
--- a/src/JT808.DotNetty.Client/JT808ClientDotnettyExtensions.cs
+++ b/src/JT808.DotNetty.Client/JT808ClientDotnettyExtensions.cs
@@ -4,17 +4,37 @@ using System;
using System.Collections.Generic;
using System.Text;
using JT808.Protocol;
+using Microsoft.Extensions.Configuration;
namespace JT808.DotNetty.Client
{
public static class JT808ClientDotnettyExtensions
{
- public static IJT808Builder AddJT808Client(this IJT808Builder jT808Builder)
+ public static IJT808Builder AddJT808Client(this IJT808Builder jT808Builder)
{
jT808Builder.Services.AddSingleton();
jT808Builder.Services.AddSingleton();
jT808Builder.Services.AddSingleton();
- jT808Builder.Services.AddSingleton();
+ jT808Builder.Services.Configure((options)=> { });
+ jT808Builder.Services.AddHostedService();
+ return jT808Builder;
+ }
+ public static IJT808Builder AddJT808Client(this IJT808Builder jT808Builder, IConfiguration Configuration)
+ {
+ jT808Builder.Services.AddSingleton();
+ jT808Builder.Services.AddSingleton();
+ jT808Builder.Services.AddSingleton();
+ jT808Builder.Services.Configure(Configuration.GetSection("JT808ReportOptions"));
+ jT808Builder.Services.AddHostedService();
+ return jT808Builder;
+ }
+
+ public static IJT808Builder AddJT808Client(this IJT808Builder jT808Builder, Action reportAction)
+ {
+ jT808Builder.Services.AddSingleton();
+ jT808Builder.Services.AddSingleton();
+ jT808Builder.Services.AddSingleton();
+ jT808Builder.Services.Configure(reportAction);
jT808Builder.Services.AddHostedService();
return jT808Builder;
}
diff --git a/src/JT808.DotNetty.Client/DeviceConfig.cs b/src/JT808.DotNetty.Client/JT808DeviceConfig.cs
similarity index 85%
rename from src/JT808.DotNetty.Client/DeviceConfig.cs
rename to src/JT808.DotNetty.Client/JT808DeviceConfig.cs
index 332d6cf..1282d7f 100644
--- a/src/JT808.DotNetty.Client/DeviceConfig.cs
+++ b/src/JT808.DotNetty.Client/JT808DeviceConfig.cs
@@ -6,9 +6,9 @@ using System.Text;
namespace JT808.DotNetty.Client
{
- public class DeviceConfig
+ public class JT808DeviceConfig
{
- public DeviceConfig(string terminalPhoneNo, string tcpHost,int tcpPort)
+ public JT808DeviceConfig(string terminalPhoneNo, string tcpHost,int tcpPort)
{
TerminalPhoneNo = terminalPhoneNo;
TcpHost = tcpHost;
diff --git a/src/JT808.DotNetty.Client/JT808ReportOptions.cs b/src/JT808.DotNetty.Client/JT808ReportOptions.cs
new file mode 100644
index 0000000..6305ffd
--- /dev/null
+++ b/src/JT808.DotNetty.Client/JT808ReportOptions.cs
@@ -0,0 +1,25 @@
+using Microsoft.Extensions.Options;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+
+namespace JT808.DotNetty.Client
+{
+ public class JT808ReportOptions:IOptions
+ {
+ public string FileName { get; set; } = $"JT808Report.{DateTime.Now.ToString("yyyyMMddHHssmm")}.txt";
+ public string FilePath { get; set; } = AppDomain.CurrentDomain.BaseDirectory;
+ public string FileFullPath { get { return Path.Combine(FilePath, FileName); } }
+ public int Interval { get; set; } = 3;
+ public JT808ReportOptions Value => this;
+
+ public void FileExistsAndCreate()
+ {
+ if(!File.Exists(FileFullPath))
+ {
+ File.Create(FileFullPath).Close();
+ }
+ }
+ }
+}
diff --git a/src/JT808.DotNetty.Client/JT808TcpClient.cs b/src/JT808.DotNetty.Client/JT808TcpClient.cs
index 66ecdde..7baefd9 100644
--- a/src/JT808.DotNetty.Client/JT808TcpClient.cs
+++ b/src/JT808.DotNetty.Client/JT808TcpClient.cs
@@ -25,11 +25,11 @@ namespace JT808.DotNetty.Client
private bool disposed = false;
- public DeviceConfig DeviceConfig { get; private set; }
+ public JT808DeviceConfig DeviceConfig { get; private set; }
public ILoggerFactory LoggerFactory { get; private set; }
- public JT808TcpClient(DeviceConfig deviceConfig, IServiceProvider serviceProvider)
+ public JT808TcpClient(JT808DeviceConfig deviceConfig, IServiceProvider serviceProvider)
{
DeviceConfig = deviceConfig;
LoggerFactory = serviceProvider.GetRequiredService();
diff --git a/src/JT808.DotNetty.Client/JT808TcpClientFactory.cs b/src/JT808.DotNetty.Client/JT808TcpClientFactory.cs
index 0f15ed3..40d5f41 100644
--- a/src/JT808.DotNetty.Client/JT808TcpClientFactory.cs
+++ b/src/JT808.DotNetty.Client/JT808TcpClientFactory.cs
@@ -9,7 +9,7 @@ namespace JT808.DotNetty.Client
{
public interface IJT808TcpClientFactory : IDisposable
{
- JT808TcpClient Create(DeviceConfig deviceConfig);
+ JT808TcpClient Create(JT808DeviceConfig deviceConfig);
List GetAll();
}
@@ -26,7 +26,7 @@ namespace JT808.DotNetty.Client
this.serviceProvider = serviceProvider;
}
- public JT808TcpClient Create(DeviceConfig deviceConfig)
+ public JT808TcpClient Create(JT808DeviceConfig deviceConfig)
{
if(dict.TryGetValue(deviceConfig.TerminalPhoneNo,out var client))
{
diff --git a/src/JT808.DotNetty.Client/Services/JT808ReportHostedService.cs b/src/JT808.DotNetty.Client/Services/JT808ReportHostedService.cs
index 063afa1..17e0220 100644
--- a/src/JT808.DotNetty.Client/Services/JT808ReportHostedService.cs
+++ b/src/JT808.DotNetty.Client/Services/JT808ReportHostedService.cs
@@ -1,37 +1,67 @@
-using Microsoft.Extensions.Hosting;
+using JT808.DotNetty.Client.Metadata;
+using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Options;
+using Newtonsoft.Json;
using System;
using System.Collections.Generic;
+using System.IO;
+using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace JT808.DotNetty.Client.Services
{
- public class JT808ReportHostedService : IHostedService
+ public class JT808ReportHostedService : BackgroundService
{
- private readonly JT808ReportService jT808ReportService;
- private CancellationTokenSource cts = new CancellationTokenSource();
- public JT808ReportHostedService(JT808ReportService jT808ReportService)
+ private readonly IOptionsMonitor jT808ReportOptions;
+ private readonly JT808ReceiveAtomicCounterService jT808ReceiveAtomicCounterService;
+ private readonly JT808SendAtomicCounterService jT808SendAtomicCounterService;
+ private readonly IJT808TcpClientFactory jT808TcpClientFactory;
+ private readonly ILogger logger;
+
+ public JT808ReportHostedService(
+ ILoggerFactory loggerFactory,
+ IOptionsMonitor jT808ReportOptionsAccessor,
+ JT808ReceiveAtomicCounterService jT808ReceiveAtomicCounterService,
+ JT808SendAtomicCounterService jT808SendAtomicCounterService,
+ IJT808TcpClientFactory jT808TcpClientFactory)
{
- this.jT808ReportService = jT808ReportService;
+ logger = loggerFactory.CreateLogger("JT808ReportHostedService");
+ jT808ReportOptions = jT808ReportOptionsAccessor;
+ jT808ReportOptions.CurrentValue.FileExistsAndCreate();
+ this.jT808ReceiveAtomicCounterService = jT808ReceiveAtomicCounterService;
+ this.jT808SendAtomicCounterService = jT808SendAtomicCounterService;
+ this.jT808TcpClientFactory = jT808TcpClientFactory;
+ jT808ReportOptions.OnChange((options) => { options.FileExistsAndCreate(); });
}
- public Task StartAsync(CancellationToken cancellationToken)
+
+ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
- Task.Run(() =>
+ while (!stoppingToken.IsCancellationRequested)
{
- while (!cts.IsCancellationRequested)
+ var clients = jT808TcpClientFactory.GetAll();
+ JT808Report report = new JT808Report()
{
- jT808ReportService.Create();
- Thread.Sleep(1000);
- //Task.Delay(TimeSpan.FromSeconds(1), cts.Token);
+ SendTotalCount = jT808SendAtomicCounterService.MsgSuccessCount,
+ ReceiveTotalCount = jT808ReceiveAtomicCounterService.MsgSuccessCount,
+ CurrentDate = DateTime.Now,
+ Connections = clients.Count,
+ OnlineConnections = clients.Where(w => w.IsOpen).Count(),
+ OfflineConnections = clients.Where(w => !w.IsOpen).Count(),
+ };
+ string json = JsonConvert.SerializeObject(report);
+ if (logger.IsEnabled(LogLevel.Debug))
+ {
+ logger.LogDebug(json);
}
- }, cts.Token);
- return Task.CompletedTask;
- }
- public Task StopAsync(CancellationToken cancellationToken)
- {
- cts.Cancel();
- return Task.CompletedTask;
+ using (var sw=new StreamWriter(jT808ReportOptions.CurrentValue.FileFullPath,true))
+ {
+ sw.WriteLine(json);
+ }
+ await Task.Delay(TimeSpan.FromSeconds(jT808ReportOptions.CurrentValue.Interval), stoppingToken);
+ }
}
}
}
diff --git a/src/JT808.DotNetty.Client/Services/JT808ReportService.cs b/src/JT808.DotNetty.Client/Services/JT808ReportService.cs
deleted file mode 100644
index ef94b7b..0000000
--- a/src/JT808.DotNetty.Client/Services/JT808ReportService.cs
+++ /dev/null
@@ -1,42 +0,0 @@
-using JT808.DotNetty.Client.Metadata;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
-namespace JT808.DotNetty.Client.Services
-{
- public class JT808ReportService
- {
- private readonly JT808ReceiveAtomicCounterService jT808ReceiveAtomicCounterService;
- private readonly JT808SendAtomicCounterService jT808SendAtomicCounterService;
- private readonly IJT808TcpClientFactory jT808TcpClientFactory;
-
- public List JT808Reports { get; private set; }
-
- public JT808ReportService(
- JT808ReceiveAtomicCounterService jT808ReceiveAtomicCounterService,
- JT808SendAtomicCounterService jT808SendAtomicCounterService,
- IJT808TcpClientFactory jT808TcpClientFactory)
- {
- this.jT808ReceiveAtomicCounterService = jT808ReceiveAtomicCounterService;
- this.jT808SendAtomicCounterService = jT808SendAtomicCounterService;
- this.jT808TcpClientFactory = jT808TcpClientFactory;
- JT808Reports = new List();
- }
-
- public void Create()
- {
- var clients = jT808TcpClientFactory.GetAll();
- JT808Reports.Add(new JT808Report()
- {
- SendTotalCount= jT808SendAtomicCounterService.MsgSuccessCount,
- ReceiveTotalCount= jT808ReceiveAtomicCounterService.MsgSuccessCount,
- CurrentDate=DateTime.Now,
- Connections= clients.Count,
- OnlineConnections= clients.Where(w => w.IsOpen).Count(),
- OfflineConnections= clients.Where(w => !w.IsOpen).Count(),
- });
- }
- }
-}