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.
 
 
 

72 rivejä
2.4 KiB

  1. using JT808.Gateway.Client;
  2. using JT808.Protocol.MessageBody;
  3. using JT808.Protocol.Enums;
  4. using JT808.Protocol.Extensions;
  5. using Microsoft.Extensions.Hosting;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace JT808.Gateway.SimpleClient.Services
  12. {
  13. public class UpService : IHostedService
  14. {
  15. private readonly IJT808TcpClientFactory jT808TcpClientFactory;
  16. public UpService(IJT808TcpClientFactory jT808TcpClientFactory)
  17. {
  18. this.jT808TcpClientFactory = jT808TcpClientFactory;
  19. }
  20. public async Task StartAsync(CancellationToken cancellationToken)
  21. {
  22. string sim = "11111111111";
  23. JT808TcpClient client1 = await jT808TcpClientFactory.Create(new JT808DeviceConfig(sim, "127.0.0.1", 808), cancellationToken);
  24. await Task.Delay(2000);
  25. //1.终端注册
  26. await client1.SendAsync(JT808MsgId.终端注册.Create(sim, new JT808_0x0100()
  27. {
  28. PlateNo = "粤A12345",
  29. PlateColor = 2,
  30. AreaID = 0,
  31. CityOrCountyId = 0,
  32. MakerId = "Koike001",
  33. TerminalId = "Koike001",
  34. TerminalModel = "Koike001"
  35. }));
  36. //2.终端鉴权
  37. await client1.SendAsync(JT808MsgId.终端鉴权.Create(sim, new JT808_0x0102()
  38. {
  39. Code = "1234"
  40. }));
  41. _= Task.Run(async() => {
  42. while (!cancellationToken.IsCancellationRequested)
  43. {
  44. var i = 0;
  45. //3.每5秒发一次
  46. await client1.SendAsync(JT808MsgId.位置信息汇报.Create(sim, new JT808_0x0200()
  47. {
  48. Lat = 110000 + i,
  49. Lng = 100000 + i,
  50. GPSTime = DateTime.Now,
  51. Speed = 50,
  52. Direction = 30,
  53. AlarmFlag = 5,
  54. Altitude = 50,
  55. StatusFlag = 10
  56. }));
  57. i++;
  58. await Task.Delay(5000);
  59. }
  60. }, cancellationToken);
  61. }
  62. public Task StopAsync(CancellationToken cancellationToken)
  63. {
  64. return Task.CompletedTask;
  65. }
  66. }
  67. }