Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

64 řádky
1.7 KiB

  1. using JT808.Gateway.Abstractions;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using JT808.Gateway.SimpleServer.Services;
  9. namespace JT808.Gateway.SimpleServer.Impl
  10. {
  11. public class JT808SessionConsumer : IJT808SessionConsumer
  12. {
  13. public CancellationTokenSource Cts => new CancellationTokenSource();
  14. private readonly ILogger logger;
  15. public string TopicName { get; } = JT808GatewayConstants.SessionTopic;
  16. private readonly JT808SessionService JT808SessionService;
  17. public JT808SessionConsumer(
  18. JT808SessionService jT808SessionService,
  19. ILoggerFactory loggerFactory)
  20. {
  21. logger = loggerFactory.CreateLogger("JT808SessionConsumer");
  22. JT808SessionService = jT808SessionService;
  23. }
  24. public void OnMessage(Action<(string Notice, string TerminalNo)> callback)
  25. {
  26. Task.Run(async () =>
  27. {
  28. while (!Cts.IsCancellationRequested)
  29. {
  30. try
  31. {
  32. var item = await JT808SessionService.ReadAsync(Cts.Token);
  33. callback(item);
  34. }
  35. catch (Exception ex)
  36. {
  37. logger.LogError(ex, "");
  38. }
  39. }
  40. }, Cts.Token);
  41. }
  42. public void Unsubscribe()
  43. {
  44. Cts.Cancel();
  45. }
  46. public void Dispose()
  47. {
  48. Cts.Dispose();
  49. }
  50. public void Subscribe()
  51. {
  52. }
  53. }
  54. }