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.
 
 
 

71 lines
2.5 KiB

  1. using JT808.Gateway.Client;
  2. using JT808.Protocol.Enums;
  3. using JT808.Protocol.Extensions;
  4. using JT808.Protocol.MessageBody;
  5. using Microsoft.Extensions.Hosting;
  6. using Microsoft.Extensions.Logging;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace JT808.Gateway.TestHosting.Jobs
  13. {
  14. public class UpJob : IHostedService
  15. {
  16. private readonly IJT808TcpClientFactory jT808TcpClientFactory;
  17. private readonly ILogger Logger;
  18. public UpJob(
  19. ILoggerFactory loggerFactory,
  20. IJT808TcpClientFactory jT808TcpClientFactory)
  21. {
  22. Logger = loggerFactory.CreateLogger("UpJob");
  23. this.jT808TcpClientFactory = jT808TcpClientFactory;
  24. }
  25. public Task StartAsync(CancellationToken cancellationToken)
  26. {
  27. Task.Run(async () =>
  28. {
  29. await Task.Delay(2 * 1000);
  30. var client = await jT808TcpClientFactory.Create(new JT808DeviceConfig("1234567890", "127.0.0.1", 808), cancellationToken);
  31. if (client != null)
  32. {
  33. while (!cancellationToken.IsCancellationRequested)
  34. {
  35. try
  36. {
  37. int lat = new Random(1000).Next(100000, 180000);
  38. int Lng = new Random(1000).Next(100000, 180000);
  39. client.Send(JT808MsgId.位置信息汇报.Create(client.DeviceConfig.TerminalPhoneNo, new JT808_0x0200()
  40. {
  41. Lat = lat,
  42. Lng = Lng,
  43. GPSTime = DateTime.Now,
  44. Speed = 50,
  45. Direction = 30,
  46. AlarmFlag = 5,
  47. Altitude = 50,
  48. StatusFlag = 10
  49. }));
  50. }
  51. catch (Exception ex)
  52. {
  53. Logger.LogError(ex.Message);
  54. }
  55. await Task.Delay(3 * 1000);
  56. }
  57. }
  58. }, cancellationToken);
  59. return Task.CompletedTask;
  60. }
  61. public Task StopAsync(CancellationToken cancellationToken)
  62. {
  63. jT808TcpClientFactory.Dispose();
  64. return Task.CompletedTask;
  65. }
  66. }
  67. }