RecordKeySwitch.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Wit.SDK.Modular.Sensor.Device;
  9. using Wit.SDK.Utils;
  10. namespace Wit.SDK.Modular.Sensor.Modular.DataProcessor.Utils
  11. {
  12. /// <summary>
  13. /// 记录key值切换器
  14. /// 功能:自动匹配记录的key
  15. /// </summary>
  16. public class RecordKeySwitch
  17. {
  18. /// <summary>
  19. /// 记录触发器列表
  20. /// </summary>
  21. public List<string> recordTriggerList = new List<string> {};
  22. /// <summary>
  23. /// 字典 string:key值 long:刷新时间
  24. /// </summary>
  25. private Dictionary<string, long> keyUpdateTimeDict = new Dictionary<string, long>();
  26. /// <summary>
  27. /// 是否启用切换触发器线程
  28. /// </summary>
  29. public bool BolSwitchRecrodThEnable = false;
  30. /// <summary>
  31. /// 上次更新时间
  32. /// </summary>
  33. public long LastUpdateTs = 0;
  34. /// <summary>
  35. /// 基础设备
  36. /// </summary>
  37. public DeviceModel DeviceModel { get; private set; }
  38. /// <summary>
  39. /// 初始化 启动
  40. /// </summary>
  41. /// <param name="deviceModel"></param>
  42. public void Open(DeviceModel deviceModel, List<string> keyList)
  43. {
  44. // 设备
  45. this.DeviceModel = deviceModel;
  46. // 记录触发器列表
  47. this.recordTriggerList = keyList;
  48. DeviceModel.OnKeyUpdate -= DeviceModel_OnKeyUpdate;
  49. DeviceModel.OnKeyUpdate += DeviceModel_OnKeyUpdate;
  50. DeviceModel.OnListenKeyUpdate -= DeviceModel_OnListenKeyUpdate;
  51. DeviceModel.OnListenKeyUpdate += DeviceModel_OnListenKeyUpdate;
  52. // 创建线程
  53. Thread th = new Thread(SwitchKeyTh) { IsBackground = true };
  54. BolSwitchRecrodThEnable = true;
  55. th.Start();
  56. }
  57. /// <summary>
  58. /// 切换刷新用的key值
  59. /// </summary>
  60. private void SwitchKeyTh()
  61. {
  62. // 数据刷新有问题就启动线程
  63. while (BolSwitchRecrodThEnable) {
  64. Thread.Sleep(3000);
  65. // 实时时间
  66. var ts = DateTimeUtils.GetTimeStamp();
  67. if (ts - LastUpdateTs > 3000)
  68. {
  69. SwitchListenerKey();
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// 切换监听的key
  75. /// </summary>
  76. private void SwitchListenerKey()
  77. {
  78. if (recordTriggerList.Count == 0)
  79. {
  80. return;
  81. }
  82. // 最后切换的key值
  83. string keyLast = recordTriggerList[0];
  84. double min = 0;
  85. for (int i = 0; i < recordTriggerList.Count; i++)
  86. {
  87. string item = recordTriggerList[i];
  88. if (keyUpdateTimeDict.ContainsKey(item))
  89. {
  90. double tsVal = keyUpdateTimeDict[item];
  91. if (tsVal > min)
  92. {
  93. min = tsVal;
  94. keyLast = item;
  95. }
  96. }
  97. }
  98. // 切换监听的key值 让数据刷新继续下去
  99. DeviceModel.ListenerKey = keyLast;
  100. }
  101. /// <summary>
  102. /// 判断自动更新数据有无问题
  103. /// </summary>
  104. /// <param name="deviceModel"></param>
  105. private void DeviceModel_OnListenKeyUpdate(DeviceModel deviceModel)
  106. {
  107. LastUpdateTs = DateTimeUtils.GetTimeStamp();
  108. }
  109. /// <summary>
  110. /// 记录key值刷新时对应的时间
  111. /// </summary>
  112. /// <param name="deviceModel"></param>
  113. /// <param name="key"></param>
  114. /// <param name="value"></param>
  115. private void DeviceModel_OnKeyUpdate(DeviceModel deviceModel, string key, object value)
  116. {
  117. // 如果是可切换的key就赋值给字典
  118. if (recordTriggerList.Contains(key))
  119. {
  120. // 记录数据更新时间戳
  121. keyUpdateTimeDict[key] = DateTimeUtils.GetTimeStamp();
  122. }
  123. }
  124. /// <summary>
  125. /// 关闭所有线程
  126. /// </summary>
  127. public void Close() {
  128. // 关线程
  129. BolSwitchRecrodThEnable = false;
  130. if (DeviceModel != null && DeviceModel.IsOpen)
  131. {
  132. // 取消事件委托
  133. DeviceModel.OnKeyUpdate -= DeviceModel_OnKeyUpdate;
  134. DeviceModel.OnListenKeyUpdate -= DeviceModel_OnListenKeyUpdate;
  135. }
  136. }
  137. }
  138. }