SubscriptionManager.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using MECF.Framework.Common.DataCenter;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace MECF.Framework.UI.Core.DxfScript
  9. {
  10. public class SubscriptionManager
  11. {
  12. private Dictionary<string, (object Value, long LastUpdateTime)> subscriptions = new Dictionary<string, (object, long)>();
  13. private static readonly long TimeoutMs = 300; // 超时时间,单位:毫秒
  14. /// <summary>
  15. /// 订阅数据
  16. /// </summary>
  17. /// <param name="fullName"></param>
  18. /// <param name="initialValue"></param>
  19. public void Subscribe(string fullName, object initialValue)
  20. {
  21. long currentTime = GetCurrentTimeMillis();
  22. subscriptions[fullName] = (initialValue, currentTime);
  23. UpdateScriptVariables(fullName, initialValue);
  24. }
  25. /// <summary>
  26. /// 获取最新值
  27. /// </summary>
  28. /// <param name="fullName"></param>
  29. /// <returns></returns>
  30. /// <exception cref="ArgumentNullException"></exception>
  31. public object GetData(string fullName)
  32. {
  33. if (string.IsNullOrEmpty(fullName))
  34. {
  35. // 选择抛出异常
  36. throw new ArgumentNullException(nameof(fullName), "Parameter 'name' cannot be null or empty.");
  37. // 或者记录日志并返回 null
  38. }
  39. long currentTime = GetCurrentTimeMillis();
  40. if (subscriptions.TryGetValue(fullName, out var subscription))
  41. {
  42. object value = subscription.Value;
  43. long lastUpdateTime = subscription.LastUpdateTime;
  44. if (currentTime - lastUpdateTime > TimeoutMs)
  45. {
  46. // 超时,重新获取数据并更新
  47. var newValue = FetchFromServer(new List<string> { fullName });
  48. if (newValue.ContainsKey(fullName))
  49. {
  50. subscriptions[fullName] = (newValue[fullName], currentTime);
  51. value = newValue[fullName];
  52. UpdateScriptVariables(fullName, value);
  53. }
  54. }
  55. return value;
  56. }
  57. else
  58. {
  59. // 如果订阅不存在,初始化订阅
  60. var newValue = FetchFromServer(new List<string> { fullName });
  61. if (newValue.ContainsKey(fullName))
  62. {
  63. Subscribe(fullName, newValue[fullName]);
  64. return newValue[fullName];
  65. }
  66. return null;
  67. }
  68. }
  69. /// <summary>
  70. /// 获取时间间隔
  71. /// </summary>
  72. /// <returns></returns>
  73. private long GetCurrentTimeMillis()
  74. {
  75. return Stopwatch.GetTimestamp() / TimeSpan.TicksPerMillisecond;
  76. }
  77. /// <summary>
  78. /// 获取远端数据
  79. /// </summary>
  80. /// <param name="allNames"></param>
  81. /// <returns></returns>
  82. private Dictionary<string, object> FetchFromServer(List<string> allNames)
  83. {
  84. // 使用提供的方法从服务器获取数据
  85. var rtDatas = QueryDataClient.Instance.Service.PollData(allNames);
  86. // 假设 RtDatas 是一个包含请求数据的字典
  87. Dictionary<string, object> result = new Dictionary<string, object>();
  88. foreach (var name in allNames)
  89. {
  90. if (rtDatas.ContainsKey(name) && rtDatas[name] != null)
  91. {
  92. result[name] = rtDatas[name];
  93. }
  94. }
  95. return result;
  96. }
  97. /// <summary>
  98. /// 订阅RT数据并写入变量时间池
  99. /// </summary>
  100. /// <param name="varName"></param>
  101. public void ReSubscribeAndWrite(string varName)
  102. {
  103. // 变量不存在,重新订阅并写入
  104. var newValue = FetchFromServer(new List<string> { varName });
  105. Subscribe(varName, newValue[varName]);
  106. }
  107. /// <summary>
  108. /// 更新 ScriptVariables.fakeVariables 静态变量池中的数据
  109. /// </summary>
  110. /// <param name="name"></param>
  111. /// <param name="value"></param>
  112. public void UpdateScriptVariables(string name, object value)
  113. {
  114. if (ScriptVariables.fakeVariables.ContainsKey(name))
  115. {
  116. var variable = ScriptVariables.fakeVariables[name];
  117. switch (variable.DataType)
  118. {
  119. case VariableDataType.BOOL:
  120. variable.BoolValue = Convert.ToBoolean(value);
  121. break;
  122. case VariableDataType.DOUBLE:
  123. variable.DoubleValue = Convert.ToDouble(value);
  124. break;
  125. case VariableDataType.STRING:
  126. variable.StringValue = Convert.ToString(value);
  127. break;
  128. }
  129. }
  130. else
  131. {
  132. // 如果变量不存在,可以选择添加新变量或者抛出异常
  133. // 这里选择添加新变量
  134. AddNewVariable(name, value);
  135. }
  136. }
  137. /// <summary>
  138. /// 添加新的变量
  139. /// </summary>
  140. /// <param name="name"></param>
  141. /// <param name="value"></param>
  142. private static void AddNewVariable(string name, object value)
  143. {
  144. if (value == null)
  145. {
  146. // 处理 value 为空的情况,可以抛出异常或设置默认值
  147. value = "";
  148. }
  149. VariableDataType dataType = DetermineDataType(value);
  150. var variable = new Variable("", dataType);
  151. switch (dataType)
  152. {
  153. case VariableDataType.BOOL:
  154. variable.BoolValue = Convert.ToBoolean(value);
  155. break;
  156. case VariableDataType.DOUBLE:
  157. variable.DoubleValue = Convert.ToDouble(value);
  158. break;
  159. case VariableDataType.STRING:
  160. variable.StringValue = Convert.ToString(value);
  161. break;
  162. }
  163. ScriptVariables.fakeVariables[name] = variable;
  164. }
  165. /// <summary>
  166. /// 创建默认变量的值的时候 判断值是什么类型
  167. /// </summary>
  168. /// <param name="value"></param>
  169. /// <returns></returns>
  170. /// <exception cref="ArgumentException"></exception>
  171. private static VariableDataType DetermineDataType(object value)
  172. {
  173. if (value is bool)
  174. return VariableDataType.BOOL;
  175. if (value is double)
  176. return VariableDataType.DOUBLE;
  177. if (value is string)
  178. return VariableDataType.STRING;
  179. throw new ArgumentException("Unsupported variable type");
  180. }
  181. }
  182. }