您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

108 行
4.0 KiB

  1. using JT808.Gateway.Client;
  2. using JT808.Protocol.MessageBody;
  3. using JT808.Protocol;
  4. using JT808.Protocol.Enums;
  5. using JT808.Protocol.Extensions;
  6. using Microsoft.Extensions.Hosting;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Net;
  13. using JT808.Gateway.NBIotSimpleClient.Services;
  14. namespace JT808.Gateway.NBIotSimpleClient.Jobs
  15. {
  16. public class Up2019Service : IHostedService
  17. {
  18. IJT808TcpClientFactory jT808TcpClientFactory;
  19. JT808Serializer Serializer;
  20. DeviceInfoService DeviceInfoService;
  21. public Up2019Service(
  22. DeviceInfoService deviceInfoService,
  23. IJT808Config jT808Config,
  24. IJT808TcpClientFactory jT808TcpClientFactory)
  25. {
  26. this.jT808TcpClientFactory = jT808TcpClientFactory;
  27. Serializer = jT808Config.GetSerializer();
  28. DeviceInfoService = deviceInfoService;
  29. }
  30. public async Task StartAsync(CancellationToken cancellationToken)
  31. {
  32. string sim = "22222222222";
  33. var address = Dns.GetHostAddresses("jtt808.ctwing.cn");
  34. JT808TcpClient client1 = await jT808TcpClientFactory.Create(new JT808DeviceConfig(sim, address[0].ToString(), 6001, version:JT808Version.JTT2019), cancellationToken);
  35. await Task.Delay(1000);
  36. var p1 = JT808MsgId.终端注册.Create(sim, new JT808_0x0100()
  37. {
  38. PlateNo = "粤A12346",
  39. PlateColor = 0,
  40. AreaID = 0,
  41. CityOrCountyId = 0,
  42. MakerId = "12345",
  43. TerminalModel = "123456".PadRight(20,'\0'), //设备型号
  44. TerminalId = "1234567", //设备编号
  45. });
  46. var p1_1 = Serializer.Serialize(p1).ToHexString();
  47. //1.终端注册
  48. await client1.SendAsync(p1);
  49. _ = Task.Run(async () =>
  50. {
  51. while (!cancellationToken.IsCancellationRequested)
  52. {
  53. await Task.Delay(3000);
  54. if (DeviceInfoService.DeviceInfos.TryGetValue(sim, out var deviceInfo))
  55. {
  56. if (deviceInfo.Successed)
  57. {
  58. break;
  59. }
  60. if (!string.IsNullOrEmpty(deviceInfo.Code))
  61. {
  62. //2.终端鉴权
  63. await client1.SendAsync(JT808MsgId.终端鉴权.Create(sim, new JT808_0x0102()
  64. {
  65. Code= deviceInfo.Code,
  66. }));
  67. }
  68. }
  69. }
  70. }, cancellationToken);
  71. _ = Task.Run(async() =>
  72. {
  73. while (!cancellationToken.IsCancellationRequested)
  74. {
  75. var i = 0;
  76. //3.每5秒发一次
  77. if(DeviceInfoService.DeviceInfos.TryGetValue(sim,out var deviceInfo))
  78. {
  79. if (deviceInfo.Successed)
  80. {
  81. await client1.SendAsync(JT808MsgId.位置信息汇报.Create(sim, new JT808_0x0200()
  82. {
  83. Lat = 110000 + i,
  84. Lng = 100000 + i,
  85. GPSTime = DateTime.Now,
  86. Speed = 50,
  87. Direction = 30,
  88. AlarmFlag = 5,
  89. Altitude = 50,
  90. StatusFlag = 10
  91. }));
  92. }
  93. }
  94. i++;
  95. await Task.Delay(5000);
  96. }
  97. }, cancellationToken);
  98. }
  99. public Task StopAsync(CancellationToken cancellationToken)
  100. {
  101. return Task.CompletedTask;
  102. }
  103. }
  104. }