@@ -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() | |||
{ | |||
@@ -2,7 +2,7 @@ | |||
<PropertyGroup> | |||
<OutputType>Exe</OutputType> | |||
<TargetFramework>netcoreapp2.2</TargetFramework> | |||
<TargetFramework>netcoreapp3.0</TargetFramework> | |||
<LangVersion>7.3</LangVersion> | |||
</PropertyGroup> | |||
@@ -34,4 +34,6 @@ | |||
</None> | |||
</ItemGroup> | |||
<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions> | |||
</Project> |
@@ -43,7 +43,6 @@ namespace JT808.DotNetty.CleintBenchmark | |||
services.AddJT808Configure() | |||
.AddJT808Client(); | |||
services.AddHostedService<CleintBenchmarkHostedService>(); | |||
services.AddHostedService<CleintBenchmarkReportHostedService>(); | |||
}); | |||
await serverHostBuilder.RunConsoleAsync(); | |||
} | |||
@@ -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) | |||
@@ -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; | |||
} | |||
} | |||
} |
@@ -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<JT808SendAtomicCounterService>(); | |||
jT808Builder.Services.AddSingleton<JT808ReceiveAtomicCounterService>(); | |||
jT808Builder.Services.AddSingleton<IJT808TcpClientFactory, JT808TcpClientFactory>(); | |||
jT808Builder.Services.AddSingleton<JT808ReportService>(); | |||
jT808Builder.Services.Configure<JT808ReportOptions>((options)=> { }); | |||
jT808Builder.Services.AddHostedService<JT808ReportHostedService>(); | |||
return jT808Builder; | |||
} | |||
public static IJT808Builder AddJT808Client(this IJT808Builder jT808Builder, IConfiguration Configuration) | |||
{ | |||
jT808Builder.Services.AddSingleton<JT808SendAtomicCounterService>(); | |||
jT808Builder.Services.AddSingleton<JT808ReceiveAtomicCounterService>(); | |||
jT808Builder.Services.AddSingleton<IJT808TcpClientFactory, JT808TcpClientFactory>(); | |||
jT808Builder.Services.Configure<JT808ReportOptions>(Configuration.GetSection("JT808ReportOptions")); | |||
jT808Builder.Services.AddHostedService<JT808ReportHostedService>(); | |||
return jT808Builder; | |||
} | |||
public static IJT808Builder AddJT808Client(this IJT808Builder jT808Builder, Action<JT808ReportOptions> reportAction) | |||
{ | |||
jT808Builder.Services.AddSingleton<JT808SendAtomicCounterService>(); | |||
jT808Builder.Services.AddSingleton<JT808ReceiveAtomicCounterService>(); | |||
jT808Builder.Services.AddSingleton<IJT808TcpClientFactory, JT808TcpClientFactory>(); | |||
jT808Builder.Services.Configure(reportAction); | |||
jT808Builder.Services.AddHostedService<JT808ReportHostedService>(); | |||
return jT808Builder; | |||
} | |||
@@ -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; |
@@ -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<JT808ReportOptions> | |||
{ | |||
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(); | |||
} | |||
} | |||
} | |||
} |
@@ -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<ILoggerFactory>(); | |||
@@ -9,7 +9,7 @@ namespace JT808.DotNetty.Client | |||
{ | |||
public interface IJT808TcpClientFactory : IDisposable | |||
{ | |||
JT808TcpClient Create(DeviceConfig deviceConfig); | |||
JT808TcpClient Create(JT808DeviceConfig deviceConfig); | |||
List<JT808TcpClient> 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)) | |||
{ | |||
@@ -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> jT808ReportOptions; | |||
private readonly JT808ReceiveAtomicCounterService jT808ReceiveAtomicCounterService; | |||
private readonly JT808SendAtomicCounterService jT808SendAtomicCounterService; | |||
private readonly IJT808TcpClientFactory jT808TcpClientFactory; | |||
private readonly ILogger logger; | |||
public JT808ReportHostedService( | |||
ILoggerFactory loggerFactory, | |||
IOptionsMonitor<JT808ReportOptions> 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); | |||
} | |||
} | |||
} | |||
} |
@@ -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<JT808Report> JT808Reports { get; private set; } | |||
public JT808ReportService( | |||
JT808ReceiveAtomicCounterService jT808ReceiveAtomicCounterService, | |||
JT808SendAtomicCounterService jT808SendAtomicCounterService, | |||
IJT808TcpClientFactory jT808TcpClientFactory) | |||
{ | |||
this.jT808ReceiveAtomicCounterService = jT808ReceiveAtomicCounterService; | |||
this.jT808SendAtomicCounterService = jT808SendAtomicCounterService; | |||
this.jT808TcpClientFactory = jT808TcpClientFactory; | |||
JT808Reports = new List<JT808Report>(); | |||
} | |||
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(), | |||
}); | |||
} | |||
} | |||
} |