/** * * @author seagle * @date 2024-7-22 * @Description 脚本访问代码中数据中心变量的方法 */ using MECF.Framework.Common.Equipment; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MECF.Framework.UI.Core.DxfScript { public class ScriptVariables { private static SubscriptionManager subscriptionManager = new SubscriptionManager(); public static Dictionary fakeVariables { get; set; } = new Dictionary(); /// /// 是否请求数据 /// public static bool IsFetchRT { get; set; } = true; /// /// 获取Variable类型 /// /// /// public static VariableDataType GetVariableDataTypeByName(string name) { EnsureVariableIsUpToDate(name); return fakeVariables[name].DataType; } /// /// 根据变量名称获取boo值 /// /// /// /// public static bool GetBoolByName(string name) { EnsureVariableIsUpToDate(name); if (fakeVariables[name].DataType == VariableDataType.BOOL) { return fakeVariables[name].BoolValue; } return false; } /// /// 根据变量名称获取double值 /// /// /// /// public static double GetDoubleByName(string name) { EnsureVariableIsUpToDate(name); if (fakeVariables[name].DataType == VariableDataType.DOUBLE) { return fakeVariables[name].DoubleValue; } return 0.00; } /// /// 根据变量名称获取string值 /// /// /// /// public static string GetStringByName(string name) { EnsureVariableIsUpToDate(name); if (fakeVariables[name].DataType == VariableDataType.STRING) { return fakeVariables[name].StringValue; } return ""; } /// /// 设置静态变量池bool值 /// /// /// /// public static void SetBoolByName(string name, bool value) { if (fakeVariables.ContainsKey(name) && fakeVariables[name].DataType == VariableDataType.BOOL) { fakeVariables[name].BoolValue = value; subscriptionManager.Subscribe(name, value); // 更新 SubscriptionManager 中的数据 } else { throw new Exception($"Boolean variable '{name}' not found."); } }/// /// 设置静态变量池double值 /// /// /// /// public static void SetDoubleByName(string name, double value) { if (fakeVariables.ContainsKey(name) && fakeVariables[name].DataType == VariableDataType.DOUBLE) { fakeVariables[name].DoubleValue = value; subscriptionManager.Subscribe(name, value); // 更新 SubscriptionManager 中的数据 } else { throw new Exception($"Double variable '{name}' not found."); } } /// /// 设置string值 /// /// /// /// public static void SetStringByName(string name, string value) { if (fakeVariables.ContainsKey(name) && fakeVariables[name].DataType == VariableDataType.STRING) { fakeVariables[name].StringValue = value; subscriptionManager.Subscribe(name, value); // 更新 SubscriptionManager 中的数据 } else { throw new Exception($"String variable '{name}' not found."); } } /// /// 变量池中是否包含该变量 /// /// /// public static bool ContainsVariable(string name) { EnsureVariableIsUpToDate(name); return fakeVariables.ContainsKey(name); } /// /// 根据变量名称返回Variable /// /// /// public static Variable GetVariableByName(string name) { EnsureVariableIsUpToDate(name); if (ContainsVariable(name)) { return fakeVariables[name]; } return null; } /// /// 通过调用 SubscriptionManager 的 GetVariableValue 方法来获取最新的变量值,并更新 fakeVariables 字典中的变量值。 /// /// private static void EnsureVariableIsUpToDate(string name) { if (string.IsNullOrEmpty(name)) return; if (!IsFetchRT) { return; } var value = subscriptionManager.GetData(name); if (value == null) return; if (fakeVariables.TryGetValue(name, out var variable)) { switch (variable.DataType) { case VariableDataType.BOOL: variable.BoolValue = Convert.ToBoolean(value); break; case VariableDataType.DOUBLE: variable.DoubleValue = Convert.ToDouble(value); break; case VariableDataType.STRING: variable.StringValue = Convert.ToString(value); break; } } else { // 如果变量不存在,重新订阅并写入 subscriptionManager.ReSubscribeAndWrite(name); } } public static void InitializeDefaultSubscriptions(Dictionary initialData) { foreach (var item in initialData) { subscriptionManager.Subscribe(item.Key, item.Value); } } } }