@@ -0,0 +1,63 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.ComponentModel.DataAnnotations; | |||||
using System.IO; | |||||
using System.Linq; | |||||
using System.Text; | |||||
using System.Threading.Tasks; | |||||
using JT808.DotNetty.Client; | |||||
using JT808.DotNetty.Client.Metadata; | |||||
using JT808.DotNetty.Client.Services; | |||||
using Microsoft.AspNetCore.Authorization; | |||||
using Microsoft.AspNetCore.Cors; | |||||
using Microsoft.AspNetCore.Http; | |||||
using Microsoft.AspNetCore.Mvc; | |||||
using Microsoft.Extensions.Caching.Memory; | |||||
using Microsoft.Extensions.Primitives; | |||||
namespace JT808.DotNetty.CleintBenchmark | |||||
{ | |||||
/// <summary> | |||||
/// 车辆控制器 | |||||
/// </summary> | |||||
[Route("JT808WebApi")] | |||||
[ApiController] | |||||
[EnableCors("Domain")] | |||||
public class ReportController : ControllerBase | |||||
{ | |||||
private readonly IJT808TcpClientFactory clientFactory; | |||||
private readonly JT808ReceiveAtomicCounterService ReceiveAtomicCounterService; | |||||
private readonly JT808SendAtomicCounterService SendAtomicCounterService; | |||||
/// <summary> | |||||
/// | |||||
/// </summary> | |||||
public ReportController( | |||||
IJT808TcpClientFactory factory, | |||||
JT808ReceiveAtomicCounterService jT808ReceiveAtomicCounterService, | |||||
JT808SendAtomicCounterService jT808SendAtomicCounterService) | |||||
{ | |||||
clientFactory = factory; | |||||
ReceiveAtomicCounterService = jT808ReceiveAtomicCounterService; | |||||
SendAtomicCounterService = jT808SendAtomicCounterService; | |||||
} | |||||
[HttpPost] | |||||
[HttpGet] | |||||
[Route("QueryReport")] | |||||
public ActionResult<JT808Report> QueryReport() | |||||
{ | |||||
var clients = clientFactory.GetAll(); | |||||
JT808Report report = new JT808Report() | |||||
{ | |||||
SendTotalCount = SendAtomicCounterService.MsgSuccessCount, | |||||
ReceiveTotalCount = ReceiveAtomicCounterService.MsgSuccessCount, | |||||
CurrentDate = DateTime.Now, | |||||
Connections = clients.Count, | |||||
OnlineConnections = clients.Where(w => w.IsOpen).Count(), | |||||
OfflineConnections = clients.Where(w => !w.IsOpen).Count(), | |||||
}; | |||||
return report; | |||||
} | |||||
} | |||||
} |
@@ -1,4 +1,4 @@ | |||||
<Project Sdk="Microsoft.NET.Sdk"> | |||||
<Project Sdk="Microsoft.NET.Sdk.Web"> | |||||
<PropertyGroup> | <PropertyGroup> | ||||
<OutputType>Exe</OutputType> | <OutputType>Exe</OutputType> | ||||
@@ -6,6 +6,11 @@ | |||||
<LangVersion>7.3</LangVersion> | <LangVersion>7.3</LangVersion> | ||||
</PropertyGroup> | </PropertyGroup> | ||||
<ItemGroup> | |||||
<Content Remove="wwwroot\echarts.min.js" /> | |||||
<Content Remove="wwwroot\index.html" /> | |||||
</ItemGroup> | |||||
<ItemGroup> | <ItemGroup> | ||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.0" /> | <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.0" /> | ||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.0" /> | <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.0" /> | ||||
@@ -18,6 +23,21 @@ | |||||
<ProjectReference Include="..\JT808.DotNetty.Client\JT808.DotNetty.Client.csproj" /> | <ProjectReference Include="..\JT808.DotNetty.Client\JT808.DotNetty.Client.csproj" /> | ||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | |||||
<None Include="wwwroot\echarts.min.js"> | |||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | |||||
</None> | |||||
<None Include="wwwroot\index.html"> | |||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | |||||
</None> | |||||
</ItemGroup> | |||||
<ItemGroup> | |||||
<Content Update="appsettings.json"> | |||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | |||||
</Content> | |||||
</ItemGroup> | |||||
<ItemGroup> | <ItemGroup> | ||||
<None Update="appsettings.json"> | <None Update="appsettings.json"> | ||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||||
@@ -7,8 +7,10 @@ using System.Threading.Tasks; | |||||
using JT808.Protocol; | using JT808.Protocol; | ||||
using JT808.DotNetty.Client; | using JT808.DotNetty.Client; | ||||
using JT808.DotNetty.CleintBenchmark.Configs; | using JT808.DotNetty.CleintBenchmark.Configs; | ||||
using JT808.DotNetty.CleintBenchmark.Services; | |||||
using Microsoft.Extensions.Configuration; | using Microsoft.Extensions.Configuration; | ||||
using Microsoft.AspNetCore.Hosting; | |||||
using Microsoft.AspNetCore.Builder; | |||||
using JT808.DotNetty.CleintBenchmark.Services; | |||||
namespace JT808.DotNetty.CleintBenchmark | namespace JT808.DotNetty.CleintBenchmark | ||||
{ | { | ||||
@@ -22,6 +24,31 @@ namespace JT808.DotNetty.CleintBenchmark | |||||
config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory); | config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory); | ||||
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); | config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); | ||||
}) | }) | ||||
.ConfigureWebHostDefaults(webBuilder => | |||||
{ | |||||
webBuilder.Configure(app => | |||||
{ | |||||
app.UseRouting(); | |||||
app.UseCors("Domain"); | |||||
app.UseStaticFiles(); | |||||
app.UseDefaultFiles("/index.html"); | |||||
app.UseEndpoints(endpoints => | |||||
{ | |||||
endpoints.MapControllers(); | |||||
}); | |||||
}) | |||||
.ConfigureServices((hostContext, services) => | |||||
{ | |||||
services.AddControllers(); | |||||
services.AddCors(options => | |||||
options.AddPolicy("Domain", builder => | |||||
builder.AllowAnyOrigin() | |||||
.AllowAnyMethod() | |||||
.AllowAnyHeader() | |||||
.AllowAnyOrigin())); | |||||
}); | |||||
}) | |||||
.ConfigureLogging((context, logging) => | .ConfigureLogging((context, logging) => | ||||
{ | { | ||||
if (Environment.OSVersion.Platform == PlatformID.Unix) | if (Environment.OSVersion.Platform == PlatformID.Unix) | ||||
@@ -71,7 +71,7 @@ namespace JT808.DotNetty.CleintBenchmark.Services | |||||
} | } | ||||
catch (Exception ex) | catch (Exception ex) | ||||
{ | { | ||||
logger.LogError(ex, ""); | |||||
logger.LogError(ex.Message); | |||||
} | } | ||||
}); | }); | ||||
Thread.Sleep(clientBenchmarkOptions.Interval); | Thread.Sleep(clientBenchmarkOptions.Interval); | ||||
@@ -3,20 +3,22 @@ | |||||
"IncludeScopes": false, | "IncludeScopes": false, | ||||
"Debug": { | "Debug": { | ||||
"LogLevel": { | "LogLevel": { | ||||
"Default": "Trace" | |||||
"Default": "Error" | |||||
} | } | ||||
}, | }, | ||||
"Console": { | "Console": { | ||||
"LogLevel": { | "LogLevel": { | ||||
"Default": "Trace" | |||||
"Default": "Error" | |||||
} | } | ||||
} | } | ||||
}, | }, | ||||
"AllowedHosts": "*", | |||||
//"urls": "http://*:15004;", | |||||
"ClientBenchmarkOptions": { | "ClientBenchmarkOptions": { | ||||
"IP": "127.0.0.1", | "IP": "127.0.0.1", | ||||
"Port": 808, | "Port": 808, | ||||
"DeviceCount": 100, | "DeviceCount": 100, | ||||
"Interval": 1000, | "Interval": 1000, | ||||
"DeviceTemplate": 300000 //需要多台机器同时访问,那么可以根据这个避开重复终端号 100000-200000-300000 | |||||
"DeviceTemplate": 100000 //需要多台机器同时访问,那么可以根据这个避开重复终端号 100000-200000-300000 | |||||
} | } | ||||
} | } |
@@ -0,0 +1,153 @@ | |||||
<!DOCTYPE html> | |||||
<html> | |||||
<head> | |||||
<meta charset="utf-8" /> | |||||
<title>收发查看</title> | |||||
<script src="https://unpkg.com/dayjs"></script> | |||||
<script src="https://unpkg.com/axios/dist/axios.min.js"></script> | |||||
<script src="/echarts.min.js"></script> | |||||
</head> | |||||
<body> | |||||
<div id="tcpContainer" style="text-align:center;margin:20px auto;width:80%;height: 300px;"></div> | |||||
<div id="connContainer" style="text-align:center;margin:20px auto;width:80%;height: 300px;"></div> | |||||
<script type="text/javascript"> | |||||
var tcpDom = document.getElementById("tcpContainer"); | |||||
var connDom = document.getElementById("connContainer"); | |||||
var tcpChart = echarts.init(tcpDom); | |||||
var connChart = echarts.init(connDom); | |||||
var sendData=[]; | |||||
var receiveData=[]; | |||||
var onlineData=[]; | |||||
var offlineData=[]; | |||||
var timeData=[]; | |||||
var tcpOption = { | |||||
title: { | |||||
text: 'TCP收发数' | |||||
}, | |||||
tooltip: { | |||||
trigger: 'axis', | |||||
axisPointer: { | |||||
animation: true | |||||
} | |||||
}, | |||||
legend: { | |||||
data:['发送总次数','接收总次数'] | |||||
}, | |||||
xAxis: { | |||||
type: 'category', | |||||
boundaryGap: false, | |||||
data: timeData | |||||
}, | |||||
yAxis: { | |||||
type: 'value', | |||||
boundaryGap: [0, '100%'], | |||||
splitLine: { | |||||
show: true | |||||
} | |||||
}, | |||||
series: [{ | |||||
name: '发送总次数', | |||||
type: 'line', | |||||
color: "blue", | |||||
data: sendData | |||||
},{ | |||||
name: '接收总次数', | |||||
type: 'line', | |||||
color: "red", | |||||
data: receiveData | |||||
}] | |||||
}; | |||||
var connOption = { | |||||
title: { | |||||
text: 'TCP连接数' | |||||
}, | |||||
tooltip: { | |||||
trigger: 'axis', | |||||
axisPointer: { | |||||
animation: true | |||||
} | |||||
}, | |||||
legend: { | |||||
data:['tcp在线数','tcp离线数'] | |||||
}, | |||||
xAxis: { | |||||
type: 'category', | |||||
boundaryGap: false, | |||||
data: timeData | |||||
}, | |||||
yAxis: { | |||||
type: 'value', | |||||
boundaryGap: [0, '100%'], | |||||
splitLine: { | |||||
show: true | |||||
} | |||||
}, | |||||
series: [{ | |||||
name: 'tcp在线数', | |||||
type: 'line', | |||||
color: "blue", | |||||
data: onlineData | |||||
},{ | |||||
name: 'tcp离线数', | |||||
type: 'line', | |||||
color: "red", | |||||
data: offlineData | |||||
}] | |||||
}; | |||||
setInterval(function () { | |||||
axios.post('http://localhost:5000/JT808WebApi/QueryReport') | |||||
.then((response) => { | |||||
if (response.data) { | |||||
if(sendData.length>16){ | |||||
sendData.shift(); | |||||
receiveData.shift(); | |||||
onlineData.shift(); | |||||
offlineData.shift(); | |||||
timeData.shift(); | |||||
} | |||||
//console.log(response.data); | |||||
timeData.push(dayjs(response.data.currentDate).format('HH:mm:ss')); | |||||
sendData.push(response.data.sendTotalCount); | |||||
receiveData.push(response.data.receiveTotalCount); | |||||
onlineData.push(response.data.onlineConnections); | |||||
offlineData.push(response.data.offlineConnections); | |||||
tcpChart.setOption({ | |||||
series: [{ | |||||
data: sendData | |||||
},{ | |||||
data: receiveData | |||||
}], | |||||
xAxis:[{ | |||||
data: timeData | |||||
}] | |||||
}); | |||||
connChart.setOption({ | |||||
series: [{ | |||||
data: onlineData | |||||
},{ | |||||
data: offlineData | |||||
}], | |||||
xAxis:[{ | |||||
data: timeData | |||||
}] | |||||
}); | |||||
} else { | |||||
alert("没有数据"); | |||||
} | |||||
}) | |||||
.catch((error) => { | |||||
console.log(error); | |||||
}); | |||||
}, 1000); | |||||
if (tcpOption && typeof tcpOption === "object") { | |||||
tcpChart.setOption(tcpOption, true); | |||||
} | |||||
if (connOption && typeof connOption === "object") { | |||||
connChart.setOption(connOption, true); | |||||
} | |||||
</script> | |||||
</body> | |||||
</html> |
@@ -24,7 +24,7 @@ | |||||
<PackageReference Include="DotNetty.Handlers" Version="0.6.0" /> | <PackageReference Include="DotNetty.Handlers" Version="0.6.0" /> | ||||
<PackageReference Include="DotNetty.Transport.Libuv" Version="0.6.0" /> | <PackageReference Include="DotNetty.Transport.Libuv" Version="0.6.0" /> | ||||
<PackageReference Include="DotNetty.Codecs" Version="0.6.0" /> | <PackageReference Include="DotNetty.Codecs" Version="0.6.0" /> | ||||
<PackageReference Include="JT808" Version="2.2.1" /> | |||||
<PackageReference Include="JT808" Version="2.2.2" /> | |||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.0" /> | <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.0" /> | ||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.0" /> | <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.0" /> | ||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> | <PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> | ||||
@@ -64,14 +64,14 @@ namespace JT808.DotNetty.Client | |||||
}); | }); | ||||
} | } | ||||
public async void Send(JT808ClientRequest request) | |||||
public void Send(JT808ClientRequest request) | |||||
{ | { | ||||
if (disposed) return; | if (disposed) return; | ||||
if (clientChannel == null) throw new NullReferenceException("Channel is empty."); | if (clientChannel == null) throw new NullReferenceException("Channel is empty."); | ||||
if (request == null) throw new ArgumentNullException("JT808ClientRequest Parameter is empty."); | if (request == null) throw new ArgumentNullException("JT808ClientRequest Parameter is empty."); | ||||
if (clientChannel.Active && clientChannel.Open) | if (clientChannel.Active && clientChannel.Open) | ||||
{ | { | ||||
await clientChannel.WriteAndFlushAsync(request); | |||||
clientChannel.WriteAndFlushAsync(request); | |||||
} | } | ||||
} | } | ||||
@@ -294,11 +294,11 @@ namespace JT808.DotNetty.Core.Session | |||||
} | } | ||||
public IEnumerable<JT808TcpSession> GetTcpAll() | public IEnumerable<JT808TcpSession> GetTcpAll() | ||||
{ | { | ||||
return Sessions.Select(s => (JT808TcpSession)s.Value).Where(w => w.TransportProtocolType == JT808TransportProtocolType.tcp).ToList(); | |||||
return Sessions.Where(w => w.Value.TransportProtocolType == JT808TransportProtocolType.tcp).Select(s => (JT808TcpSession)s.Value).ToList(); | |||||
} | } | ||||
public IEnumerable<JT808UdpSession> GetUdpAll() | public IEnumerable<JT808UdpSession> GetUdpAll() | ||||
{ | { | ||||
return Sessions.Select(s => (JT808UdpSession)s.Value).Where(w => w.TransportProtocolType == JT808TransportProtocolType.udp).ToList(); | |||||
return Sessions.Where(w => w.Value.TransportProtocolType == JT808TransportProtocolType.udp).Select(s => (JT808UdpSession)s.Value).ToList(); | |||||
} | } | ||||
} | } | ||||
} | } |