123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- /**
- *
- * @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<string, Variable> fakeVariables { get; set; } = new Dictionary<string, Variable>();
- /// <summary>
- /// 是否请求数据
- /// </summary>
- public static bool IsFetchRT { get; set; } = true;
- /// <summary>
- /// 获取Variable类型
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public static VariableDataType GetVariableDataTypeByName(string name)
- {
- EnsureVariableIsUpToDate(name);
- return fakeVariables[name].DataType;
- }
- /// <summary>
- /// 根据变量名称获取boo值
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- /// <exception cref="Exception"></exception>
- public static bool GetBoolByName(string name)
- {
- EnsureVariableIsUpToDate(name);
- if (fakeVariables[name].DataType == VariableDataType.BOOL)
- {
- return fakeVariables[name].BoolValue;
- }
- return false;
- }
- /// <summary>
- /// 根据变量名称获取double值
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- /// <exception cref="Exception"></exception>
- public static double GetDoubleByName(string name)
- {
- EnsureVariableIsUpToDate(name);
- if (fakeVariables[name].DataType == VariableDataType.DOUBLE)
- {
- return fakeVariables[name].DoubleValue;
- }
- return 0.00;
- }
- /// <summary>
- /// 根据变量名称获取string值
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- /// <exception cref="Exception"></exception>
- public static string GetStringByName(string name)
- {
- EnsureVariableIsUpToDate(name);
- if (fakeVariables[name].DataType == VariableDataType.STRING)
- {
- return fakeVariables[name].StringValue;
- }
- return "";
- }
- /// <summary>
- /// 设置静态变量池bool值
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <exception cref="Exception"></exception>
- 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.");
- }
- }/// <summary>
- /// 设置静态变量池double值
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <exception cref="Exception"></exception>
- 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.");
- }
- }
- /// <summary>
- /// 设置string值
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <exception cref="Exception"></exception>
- 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.");
- }
- }
- /// <summary>
- /// 变量池中是否包含该变量
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public static bool ContainsVariable(string name)
- {
- EnsureVariableIsUpToDate(name);
- return fakeVariables.ContainsKey(name);
- }
- /// <summary>
- /// 根据变量名称返回Variable
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public static Variable GetVariableByName(string name)
- {
- EnsureVariableIsUpToDate(name);
- if (ContainsVariable(name))
- {
- return fakeVariables[name];
- }
- return null;
- }
- /// <summary>
- /// 通过调用 SubscriptionManager 的 GetVariableValue 方法来获取最新的变量值,并更新 fakeVariables 字典中的变量值。
- /// </summary>
- /// <param name="name"></param>
- 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<string, object> initialData)
- {
- foreach (var item in initialData)
- {
- subscriptionManager.Subscribe(item.Key, item.Value);
- }
- }
- }
- }
|