123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using MECF.Framework.Common.DataCenter;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MECF.Framework.UI.Core.DxfScript
- {
- public class SubscriptionManager
- {
- private Dictionary<string, (object Value, long LastUpdateTime)> subscriptions = new Dictionary<string, (object, long)>();
- private static readonly long TimeoutMs = 300; // 超时时间,单位:毫秒
- /// <summary>
- /// 订阅数据
- /// </summary>
- /// <param name="fullName"></param>
- /// <param name="initialValue"></param>
- public void Subscribe(string fullName, object initialValue)
- {
- long currentTime = GetCurrentTimeMillis();
- subscriptions[fullName] = (initialValue, currentTime);
-
- UpdateScriptVariables(fullName, initialValue);
- }
- /// <summary>
- /// 获取最新值
- /// </summary>
- /// <param name="fullName"></param>
- /// <returns></returns>
- /// <exception cref="ArgumentNullException"></exception>
- public object GetData(string fullName)
- {
- if (string.IsNullOrEmpty(fullName))
- {
- // 选择抛出异常
- throw new ArgumentNullException(nameof(fullName), "Parameter 'name' cannot be null or empty.");
- // 或者记录日志并返回 null
- }
- long currentTime = GetCurrentTimeMillis();
-
- if (subscriptions.TryGetValue(fullName, out var subscription))
- {
- object value = subscription.Value;
- long lastUpdateTime = subscription.LastUpdateTime;
- if (currentTime - lastUpdateTime > TimeoutMs)
- {
- // 超时,重新获取数据并更新
- var newValue = FetchFromServer(new List<string> { fullName });
- if (newValue.ContainsKey(fullName))
- {
- subscriptions[fullName] = (newValue[fullName], currentTime);
- value = newValue[fullName];
- UpdateScriptVariables(fullName, value);
- }
-
- }
- return value;
- }
- else
- {
- // 如果订阅不存在,初始化订阅
- var newValue = FetchFromServer(new List<string> { fullName });
- if (newValue.ContainsKey(fullName))
- {
- Subscribe(fullName, newValue[fullName]);
- return newValue[fullName];
- }
- return null;
- }
- }
- /// <summary>
- /// 获取时间间隔
- /// </summary>
- /// <returns></returns>
- private long GetCurrentTimeMillis()
- {
- return Stopwatch.GetTimestamp() / TimeSpan.TicksPerMillisecond;
- }
- /// <summary>
- /// 获取远端数据
- /// </summary>
- /// <param name="allNames"></param>
- /// <returns></returns>
- private Dictionary<string, object> FetchFromServer(List<string> allNames)
- {
- // 使用提供的方法从服务器获取数据
- var rtDatas = QueryDataClient.Instance.Service.PollData(allNames);
- // 假设 RtDatas 是一个包含请求数据的字典
- Dictionary<string, object> result = new Dictionary<string, object>();
- foreach (var name in allNames)
- {
- if (rtDatas.ContainsKey(name) && rtDatas[name] != null)
- {
- result[name] = rtDatas[name];
- }
- }
- return result;
- }
- /// <summary>
- /// 订阅RT数据并写入变量时间池
- /// </summary>
- /// <param name="varName"></param>
- public void ReSubscribeAndWrite(string varName)
- {
- // 变量不存在,重新订阅并写入
- var newValue = FetchFromServer(new List<string> { varName });
- Subscribe(varName, newValue[varName]);
- }
- /// <summary>
- /// 更新 ScriptVariables.fakeVariables 静态变量池中的数据
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- public void UpdateScriptVariables(string name, object value)
- {
- if (ScriptVariables.fakeVariables.ContainsKey(name))
- {
- var variable = ScriptVariables.fakeVariables[name];
- 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
- {
- // 如果变量不存在,可以选择添加新变量或者抛出异常
- // 这里选择添加新变量
- AddNewVariable(name, value);
- }
- }
- /// <summary>
- /// 添加新的变量
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- private static void AddNewVariable(string name, object value)
- {
- if (value == null)
- {
- // 处理 value 为空的情况,可以抛出异常或设置默认值
- value = "";
- }
- VariableDataType dataType = DetermineDataType(value);
- var variable = new Variable("", dataType);
- switch (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;
- }
- ScriptVariables.fakeVariables[name] = variable;
- }
- /// <summary>
- /// 创建默认变量的值的时候 判断值是什么类型
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- /// <exception cref="ArgumentException"></exception>
- private static VariableDataType DetermineDataType(object value)
- {
- if (value is bool)
- return VariableDataType.BOOL;
- if (value is double)
- return VariableDataType.DOUBLE;
- if (value is string)
- return VariableDataType.STRING;
- throw new ArgumentException("Unsupported variable type");
- }
- }
- }
|