ScriptVariable.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. *
  3. * @author seagle
  4. * @date 2024-7-22
  5. * @Description 脚本访问代码中数据中心变量的方法
  6. */
  7. using MECF.Framework.Common.Equipment;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace MECF.Framework.UI.Core.DxfScript
  14. {
  15. public class ScriptVariables
  16. {
  17. private static SubscriptionManager subscriptionManager = new SubscriptionManager();
  18. public static Dictionary<string, Variable> fakeVariables { get; set; } = new Dictionary<string, Variable>();
  19. /// <summary>
  20. /// 是否请求数据
  21. /// </summary>
  22. public static bool IsFetchRT { get; set; } = true;
  23. /// <summary>
  24. /// 获取Variable类型
  25. /// </summary>
  26. /// <param name="name"></param>
  27. /// <returns></returns>
  28. public static VariableDataType GetVariableDataTypeByName(string name)
  29. {
  30. EnsureVariableIsUpToDate(name);
  31. return fakeVariables[name].DataType;
  32. }
  33. /// <summary>
  34. /// 根据变量名称获取boo值
  35. /// </summary>
  36. /// <param name="name"></param>
  37. /// <returns></returns>
  38. /// <exception cref="Exception"></exception>
  39. public static bool GetBoolByName(string name)
  40. {
  41. EnsureVariableIsUpToDate(name);
  42. if (fakeVariables[name].DataType == VariableDataType.BOOL)
  43. {
  44. return fakeVariables[name].BoolValue;
  45. }
  46. return false;
  47. }
  48. /// <summary>
  49. /// 根据变量名称获取double值
  50. /// </summary>
  51. /// <param name="name"></param>
  52. /// <returns></returns>
  53. /// <exception cref="Exception"></exception>
  54. public static double GetDoubleByName(string name)
  55. {
  56. EnsureVariableIsUpToDate(name);
  57. if (fakeVariables[name].DataType == VariableDataType.DOUBLE)
  58. {
  59. return fakeVariables[name].DoubleValue;
  60. }
  61. return 0.00;
  62. }
  63. /// <summary>
  64. /// 根据变量名称获取string值
  65. /// </summary>
  66. /// <param name="name"></param>
  67. /// <returns></returns>
  68. /// <exception cref="Exception"></exception>
  69. public static string GetStringByName(string name)
  70. {
  71. EnsureVariableIsUpToDate(name);
  72. if (fakeVariables[name].DataType == VariableDataType.STRING)
  73. {
  74. return fakeVariables[name].StringValue;
  75. }
  76. return "";
  77. }
  78. /// <summary>
  79. /// 设置静态变量池bool值
  80. /// </summary>
  81. /// <param name="name"></param>
  82. /// <param name="value"></param>
  83. /// <exception cref="Exception"></exception>
  84. public static void SetBoolByName(string name, bool value)
  85. {
  86. if (fakeVariables.ContainsKey(name) && fakeVariables[name].DataType == VariableDataType.BOOL)
  87. {
  88. fakeVariables[name].BoolValue = value;
  89. subscriptionManager.Subscribe(name, value); // 更新 SubscriptionManager 中的数据
  90. }
  91. else
  92. {
  93. throw new Exception($"Boolean variable '{name}' not found.");
  94. }
  95. }/// <summary>
  96. /// 设置静态变量池double值
  97. /// </summary>
  98. /// <param name="name"></param>
  99. /// <param name="value"></param>
  100. /// <exception cref="Exception"></exception>
  101. public static void SetDoubleByName(string name, double value)
  102. {
  103. if (fakeVariables.ContainsKey(name) && fakeVariables[name].DataType == VariableDataType.DOUBLE)
  104. {
  105. fakeVariables[name].DoubleValue = value;
  106. subscriptionManager.Subscribe(name, value); // 更新 SubscriptionManager 中的数据
  107. }
  108. else
  109. {
  110. throw new Exception($"Double variable '{name}' not found.");
  111. }
  112. }
  113. /// <summary>
  114. /// 设置string值
  115. /// </summary>
  116. /// <param name="name"></param>
  117. /// <param name="value"></param>
  118. /// <exception cref="Exception"></exception>
  119. public static void SetStringByName(string name, string value)
  120. {
  121. if (fakeVariables.ContainsKey(name) && fakeVariables[name].DataType == VariableDataType.STRING)
  122. {
  123. fakeVariables[name].StringValue = value;
  124. subscriptionManager.Subscribe(name, value); // 更新 SubscriptionManager 中的数据
  125. }
  126. else
  127. {
  128. throw new Exception($"String variable '{name}' not found.");
  129. }
  130. }
  131. /// <summary>
  132. /// 变量池中是否包含该变量
  133. /// </summary>
  134. /// <param name="name"></param>
  135. /// <returns></returns>
  136. public static bool ContainsVariable(string name)
  137. {
  138. EnsureVariableIsUpToDate(name);
  139. return fakeVariables.ContainsKey(name);
  140. }
  141. /// <summary>
  142. /// 根据变量名称返回Variable
  143. /// </summary>
  144. /// <param name="name"></param>
  145. /// <returns></returns>
  146. public static Variable GetVariableByName(string name)
  147. {
  148. EnsureVariableIsUpToDate(name);
  149. if (ContainsVariable(name))
  150. {
  151. return fakeVariables[name];
  152. }
  153. return null;
  154. }
  155. /// <summary>
  156. /// 通过调用 SubscriptionManager 的 GetVariableValue 方法来获取最新的变量值,并更新 fakeVariables 字典中的变量值。
  157. /// </summary>
  158. /// <param name="name"></param>
  159. private static void EnsureVariableIsUpToDate(string name)
  160. {
  161. if (string.IsNullOrEmpty(name)) return;
  162. if (!IsFetchRT)
  163. {
  164. return;
  165. }
  166. var value = subscriptionManager.GetData(name);
  167. if (value == null) return;
  168. if (fakeVariables.TryGetValue(name, out var variable))
  169. {
  170. switch (variable.DataType)
  171. {
  172. case VariableDataType.BOOL:
  173. variable.BoolValue = Convert.ToBoolean(value);
  174. break;
  175. case VariableDataType.DOUBLE:
  176. variable.DoubleValue = Convert.ToDouble(value);
  177. break;
  178. case VariableDataType.STRING:
  179. variable.StringValue = Convert.ToString(value);
  180. break;
  181. }
  182. }
  183. else
  184. {
  185. // 如果变量不存在,重新订阅并写入
  186. subscriptionManager.ReSubscribeAndWrite(name);
  187. }
  188. }
  189. public static void InitializeDefaultSubscriptions(Dictionary<string, object> initialData)
  190. {
  191. foreach (var item in initialData)
  192. {
  193. subscriptionManager.Subscribe(item.Key, item.Value);
  194. }
  195. }
  196. }
  197. }