万佳安设备数据
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.

67 lines
4.1KB

  1. using Confluent.Kafka;
  2. using TelpoPush.WanJiaAn.Worker.Common;
  3. using TelpoPush.WanJiaAn.Worker.Service.Mq;
  4. namespace TelpoPush.WanJiaAn.Worker.Handlers
  5. {
  6. public class KafkaSubscribe
  7. {
  8. private readonly ILogger<KafkaSubscribe> _logger;
  9. private readonly IHostEnvironment _env;
  10. private readonly IKafkaService _kafkaService;
  11. private readonly WanJiaAnProcess _WanJiaAnProcess;
  12. public KafkaSubscribe(
  13. ILogger<KafkaSubscribe> logger, IHostEnvironment env,
  14. IKafkaService kafkaService,
  15. WanJiaAnProcess WanJiaAnProcess)
  16. {
  17. _logger = logger;
  18. _env = env;
  19. _kafkaService = kafkaService;
  20. _WanJiaAnProcess = WanJiaAnProcess;
  21. }
  22. public async Task SubscribeAsync()
  23. {
  24. #if DEBUG
  25. //_logger.LogInformation("11312");
  26. //var temp = new Headers();
  27. //string topic = "topic.wanjiaan.push.telpo";
  28. //temp.Add(new Header("DataType", new byte[] { 0, 0, 0, 0 }));
  29. //temp.Add(new Header("AlarmType", new byte[] { 2, 0, 0, 0 }));
  30. //string json1 = "{\"msg_id\":\"33090000000023391723109173034\",\"device_id\":\"3309000000002339\",\"type\":\"DEVICE_REPORT\",\"device_type\":\"XZL-Q42\",\"event_time\":\"1723109173\",\"app_id\":6755,\"user_id\":\"tel1000\",\"data\":{\"alarm_event\":{\"event_id\":\"17231091733309000000002339\",\"event_start\":\"1723109173\",\"event_type\":2,\"report_type\":1}}}";
  31. //await _WanJiaAnProcess.SendWanJiaAn(json1, topic, temp);
  32. //string json2 = "{\"msg_id\":\"33090000000023391723109197082\",\"device_id\":\"3309000000002339\",\"type\":\"DEVICE_REPORT\",\"device_type\":\"XZL-Q42\",\"event_time\":\"1723109197\",\"app_id\":6755,\"user_id\":\"tel1000\",\"data\":{\"alarm_event\":{\"event_end\":\"1723109197\",\"event_id\":\"17231091733309000000002339\",\"event_start\":\"1723109173\",\"event_type\":2,\"report_type\":2}}}";
  33. //await _WanJiaAnProcess.SendWanJiaAn(json2, topic, temp);
  34. //string json3 = "{\"msg_id\":\"33090000000023391723109197036\",\"device_id\":\"3309000000002339\",\"type\":\"DEVICE_REPORT\",\"device_type\":\"XZL-Q42\",\"event_time\":\"1723109197\",\"app_id\":6755,\"user_id\":\"tel1000\",\"data\":{\"alarm_event\":{\"event_end\":\"1723109197\",\"event_id\":\"17231091733309000000002339\",\"event_start\":\"1723109173\",\"event_type\":2,\"image\":\"http://s-cn-xiaoz-1304525121.cos.ap-guangzhou.myqcloud.com/cloud_ts%2F7_3309000000002339%2F3309000000002339_1723109174_p37.jpeg?sign=q-sign-algorithm%3Dsha1%26q-ak%3DAKIDdtlQq8bpIWdZH4PBD1xe4Qgon9IXLLrB%26q-sign-time%3D1723109175%3B3446823150%26q-key-time%3D1723109175%3B3446823150%26q-header-list%3Dhost%26q-url-param-list%3D%26q-signature%3D708db58b15fdcaceb8bf6eeded5e2664b015cb6c\",\"report_type\":3,\"url\":\"http://s-cn-xiaoz-1304525121.cos.ap-guangzhou.myqcloud.com/cloud_ts%2F7_3309000000002339%2F1723046400_1723109171_1723109175.ts?sign=q-sign-algorithm%3Dsha1%26q-ak%3DAKIDdtlQq8bpIWdZH4PBD1xe4Qgon9IXLLrB%26q-sign-time%3D1723109176%3B3446823152%26q-key-time%3D1723109176%3B3446823152%26q-header-list%3Dhost%26q-url-param-list%3D%26q-signature%3D2fad04c97bfd9113491eada7d221cc5caab57e5b\",\"video_end\":\"1723109175\",\"video_start\":\"1723109171\"}}}";
  35. //await _WanJiaAnProcess.SendWanJiaAn(json3, topic, temp);
  36. // await _kafkaService.SubscribeAsync(DoReceive, CancellationToken.None);
  37. #else
  38. LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(5);
  39. TaskFactory factory = new TaskFactory(lcts);
  40. try
  41. {
  42. await factory.StartNew(async () =>
  43. {
  44. await _kafkaService.SubscribeAsync(DoReceive, CancellationToken.None);
  45. });
  46. }
  47. catch (Exception ex)
  48. {
  49. _logger.LogError($"Subscribe 处理Kafka数据发生异常 {ex.Message}|{ex.Source}|{ex.StackTrace}");
  50. }
  51. #endif
  52. }
  53. async void DoReceive(string topic, string message, Headers headers)
  54. {
  55. await _WanJiaAnProcess.SendWanJiaAn(message, topic, headers);
  56. }
  57. }
  58. }