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 regels
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. public CleintBenchmarkHostedService(
  24. ILoggerFactory loggerFactory,
  25. IJT808TcpClientFactory jT808TcpClientFactory,
  26. IOptions<ClientBenchmarkOptions> clientBenchmarkOptionsAccessor)
  27. {
  28. this.jT808TcpClientFactory = jT808TcpClientFactory;
  29. clientBenchmarkOptions = clientBenchmarkOptionsAccessor.Value;
  30. logger = loggerFactory.CreateLogger("CleintBenchmarkHostedService");
  31. }
  32. public Task StartAsync(CancellationToken cancellationToken)
  33. {
  34. logger.LogInformation("StartAsync...");
  35. ThreadPool.GetMinThreads(out var minWorkerThreads, out var minCompletionPortThreads);
  36. ThreadPool.GetMaxThreads(out var maxWorkerThreads, out var maxCompletionPortThreads);
  37. logger.LogInformation($"GetMinThreads:{minWorkerThreads}-{minCompletionPortThreads}");
  38. logger.LogInformation($"GetMaxThreads:{maxWorkerThreads}-{maxCompletionPortThreads}");
  39. for (int i=0;i< clientBenchmarkOptions.DeviceCount; i++)
  40. {
  41. string deviceNo = (i + 1 + clientBenchmarkOptions.DeviceTemplate).ToString();
  42. var client = jT808TcpClientFactory.Create(new JT808DeviceConfig(deviceNo,
  43. clientBenchmarkOptions.IP,
  44. clientBenchmarkOptions.Port), cancellationToken);
  45. }
  46. ThreadPool.QueueUserWorkItem((state) =>
  47. {
  48. while (!cancellationToken.IsCancellationRequested)
  49. {
  50. foreach (var item in jT808TcpClientFactory.GetAll())
  51. {
  52. try
  53. {
  54. int lat = new Random(1000).Next(100000, 180000);
  55. int Lng = new Random(1000).Next(100000, 180000);
  56. item.Send(JT808MsgId.位置信息汇报.Create(item.DeviceConfig.TerminalPhoneNo, new JT808_0x0200()
  57. {
  58. Lat = lat,
  59. Lng = Lng,
  60. GPSTime = DateTime.Now,
  61. Speed = 50,
  62. Direction = 30,
  63. AlarmFlag = 5,
  64. Altitude = 50,
  65. StatusFlag = 10
  66. }));
  67. }
  68. catch (Exception ex)
  69. {
  70. logger.LogError(ex.Message);
  71. }
  72. }
  73. Thread.Sleep(clientBenchmarkOptions.Interval);
  74. }
  75. });
  76. return Task.CompletedTask;
  77. }
  78. public Task StopAsync(CancellationToken cancellationToken)
  79. {
  80. jT808TcpClientFactory.Dispose();
  81. logger.LogInformation("StopAsync...");
  82. return Task.CompletedTask;
  83. }
  84. }
  85. }