MqttHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 
  2. using System;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using static IoTIntegrationPlatform.Model.Enum.EnumMqtt;
  6. using IoTIntegrationPlatform.Model.Model;
  7. using IoTIntegrationPlatform.Model.MqttModel;
  8. namespace IoTIntegrationPlatform.Common
  9. {
  10. /// <summary>
  11. /// mqtt公共类
  12. /// </summary>
  13. public static class MqttHelper
  14. {
  15. /// <summary>
  16. /// 获取设备网关编号
  17. /// </summary>
  18. /// <param name="topic"></param>
  19. /// <returns></returns>
  20. public static string GetDeviceMac(string topic)
  21. {
  22. string deviceMac = string.Empty;
  23. try
  24. {
  25. if (!string.IsNullOrEmpty(topic))
  26. {
  27. var topicArray = topic.Split('/');
  28. if (topicArray != null && topicArray.Length > 2)
  29. {
  30. deviceMac = topicArray[2].ToString();//网关id
  31. }
  32. }
  33. }
  34. catch (Exception ex)
  35. {
  36. Logging.Error(ex, ex.Message);
  37. }
  38. return deviceMac;
  39. }
  40. /// <summary>
  41. /// 获取mqttTopic类型
  42. /// </summary>
  43. /// <param name="topic"></param>
  44. /// <returns></returns>
  45. public static EnumMqttTopicType GetMqttTopicType(string topic)
  46. {
  47. EnumMqttTopicType topicType = EnumMqttTopicType.client_connect;
  48. try
  49. {
  50. var topicArray = topic.Split('/');
  51. var topicName = topicArray[topicArray.Length - 1];//topic名称
  52. if (!string.IsNullOrEmpty(topic))
  53. {
  54. Enum.TryParse(topicName, out topicType);
  55. }
  56. }
  57. catch (Exception ex)
  58. {
  59. Logging.Error(ex, ex.Message);
  60. }
  61. return topicType;
  62. }
  63. /// <summary>
  64. /// 获取任务执行结果
  65. /// </summary>
  66. /// <param name="taskId"></param>
  67. /// <returns></returns>
  68. public static bool GetTaskExecutionResult(string taskId)
  69. {
  70. bool executionResult = false;
  71. try
  72. {
  73. //最大秒数
  74. taskId = RedisKeyHelper.MqttTask + "_" + taskId;
  75. int maxSeconds = 5;
  76. int totalCount = maxSeconds * 20;
  77. for (int i = 0; i < totalCount; i++)
  78. {
  79. Thread.Sleep(50);
  80. MqttTaskModel taskModel = RedisHelper.Instance.StringGet<MqttTaskModel>(taskId);
  81. if (taskModel != null && taskModel.Status == 2)
  82. {
  83. return true;
  84. }
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. Logging.Error(ex, ex.Message);
  90. }
  91. return executionResult;
  92. }
  93. /// <summary>
  94. /// 执行超时任务
  95. /// </summary>
  96. /// <param name="conditionFunc"></param>
  97. /// <param name="timeout"></param>
  98. /// <returns></returns>
  99. public static async Task PerformTaskWithTimeout(Func<bool> conditionFunc, TimeSpan timeout)
  100. {
  101. var tokenSource = new CancellationTokenSource();
  102. var token = tokenSource.Token;
  103. try
  104. {
  105. while (!conditionFunc() && !token.IsCancellationRequested)
  106. {
  107. await Task.Delay(1000, token); // 等待1秒,然后再次检查条件,如果取消任务则跳出循环
  108. }
  109. }
  110. finally
  111. {
  112. tokenSource.Cancel(); // 无论条件是否满足,都会取消任务
  113. }
  114. }
  115. }
  116. }