You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

92 line
3.7 KiB

  1. using JT808.Gateway.CleintBenchmark.Configs;
  2. using JT808.Gateway.Client;
  3. using JT808.Protocol.Enums;
  4. using JT808.Protocol.Extensions;
  5. using JT808.Protocol.MessageBody;
  6. using Microsoft.Extensions.Hosting;
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.Extensions.Options;
  9. using System;
  10. using System.Collections.Concurrent;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. namespace JT808.Gateway.CleintBenchmark.Services
  17. {
  18. public class CleintBenchmarkHostedService : IHostedService
  19. {
  20. private readonly ClientBenchmarkOptions clientBenchmarkOptions;
  21. private readonly ILogger logger;
  22. private readonly IJT808TcpClientFactory jT808TcpClientFactory;
  23. private TaskFactory taskFactory;
  24. public CleintBenchmarkHostedService(
  25. ILoggerFactory loggerFactory,
  26. IJT808TcpClientFactory jT808TcpClientFactory,
  27. IOptions<ClientBenchmarkOptions> clientBenchmarkOptionsAccessor)
  28. {
  29. this.jT808TcpClientFactory = jT808TcpClientFactory;
  30. clientBenchmarkOptions = clientBenchmarkOptionsAccessor.Value;
  31. logger = loggerFactory.CreateLogger<CleintBenchmarkHostedService>();
  32. }
  33. public Task StartAsync(CancellationToken cancellationToken)
  34. {
  35. logger.LogInformation("StartAsync...");
  36. ThreadPool.GetMinThreads(out var minWorkerThreads, out var minCompletionPortThreads);
  37. ThreadPool.GetMaxThreads(out var maxWorkerThreads, out var maxCompletionPortThreads);
  38. logger.LogInformation($"GetMinThreads:{minWorkerThreads}-{minCompletionPortThreads}");
  39. logger.LogInformation($"GetMaxThreads:{maxWorkerThreads}-{maxCompletionPortThreads}");
  40. taskFactory = new TaskFactory(cancellationToken);
  41. for (int i=0;i< clientBenchmarkOptions.DeviceCount; i++)
  42. {
  43. taskFactory.StartNew(async (state) => {
  44. string deviceNo = ((int)state + 1 + clientBenchmarkOptions.DeviceTemplate).ToString();
  45. var client = await jT808TcpClientFactory.Create(new JT808DeviceConfig(deviceNo,
  46. clientBenchmarkOptions.IP,
  47. clientBenchmarkOptions.Port), cancellationToken);
  48. while (!cancellationToken.IsCancellationRequested)
  49. {
  50. try
  51. {
  52. int lat = new Random(1000).Next(100000, 180000);
  53. int Lng = new Random(1000).Next(100000, 180000);
  54. await client.SendAsync(JT808MsgId.位置信息汇报.Create(client.DeviceConfig.TerminalPhoneNo, new JT808_0x0200()
  55. {
  56. Lat = lat,
  57. Lng = Lng,
  58. GPSTime = DateTime.Now,
  59. Speed = 50,
  60. Direction = 30,
  61. AlarmFlag = 5,
  62. Altitude = 50,
  63. StatusFlag = 10
  64. }));
  65. }
  66. catch (Exception ex)
  67. {
  68. logger.LogError(ex.Message);
  69. }
  70. await Task.Delay(clientBenchmarkOptions.Interval);
  71. }
  72. }, i);
  73. }
  74. return Task.CompletedTask;
  75. }
  76. public Task StopAsync(CancellationToken cancellationToken)
  77. {
  78. jT808TcpClientFactory.Dispose();
  79. logger.LogInformation("StopAsync...");
  80. return Task.CompletedTask;
  81. }
  82. }
  83. }