diff --git a/SicUI/TimeredMainViewModel.cs b/SicUI/TimeredMainViewModel.cs index 716a60f5..854c189f 100644 --- a/SicUI/TimeredMainViewModel.cs +++ b/SicUI/TimeredMainViewModel.cs @@ -1,5 +1,4 @@ -using Aitex.Core.Util; -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; @@ -7,7 +6,6 @@ using System.Reflection; using System.Runtime.InteropServices; using System.Threading.Tasks; using System.Windows; -using Aitex.Core.RT.Log; using Cali = Caliburn.Micro.Core; using MECF.Framework.Common.DataCenter; @@ -15,21 +13,7 @@ namespace SicUI { public class TimeredMainViewModel : Cali.Conductor.Collection.OneActive { - PeriodicJob _timer; - ConcurrentBag _subscribedKeys = new ConcurrentBag(); - - Func _isSubscriptionAttribute; - Func _hasSubscriptionAttribute; - - public TimeredMainViewModel() - { - _timer = new PeriodicJob(1000, this.OnTimer, "UIUpdaterThread - " + GetType().Name); - - _isSubscriptionAttribute = attribute => attribute is SubscriptionAttribute; - _hasSubscriptionAttribute = mi => mi.GetCustomAttributes(false).Any(_isSubscriptionAttribute); - - SubscribeKeys(this); - } + #region Variables [StructLayout(LayoutKind.Sequential)] internal struct LASTINPUTINFO @@ -42,6 +26,30 @@ namespace SicUI [DllImport("user32.dll")] internal static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); + + private readonly PeriodicJob _timer; + private readonly ConcurrentBag _subscribedKeys = new(); + private readonly Func _isSubscriptionAttribute; + private readonly Func _hasSubscriptionAttribute; + + #endregion + + #region Constructors + + public TimeredMainViewModel() + { + _timer = new PeriodicJob(1000, this.OnTimer, "UIUpdaterThread - " + GetType().Name); + + _isSubscriptionAttribute = attribute => attribute is SubscriptionAttribute; + _hasSubscriptionAttribute = mi => mi.GetCustomAttributes(false).Any(_isSubscriptionAttribute); + + SubscribeKeys(this); + } + + #endregion + + #region Methods + /// /// 获取鼠标键盘不活动的时间 /// @@ -61,106 +69,12 @@ namespace SicUI return ((idleTime > 0) ? (idleTime / 1000) : 0); } - protected virtual bool OnTimer() - { - try - { - Poll(); - } - catch (Exception ex) - { - LOG.Error(ex.Message); - } - - return true; - } - - public virtual void EnableTimer(bool enable) { if (enable) _timer.Start(); else _timer.Pause(); } - - protected virtual void Poll() - { - if (_subscribedKeys.Count > 0) - { - Dictionary result = QueryDataClient.Instance.Service.PollData(_subscribedKeys); - - if (result == null) - { - LOG.Error("获取RT数据失败"); - return; - } - - if (result.Count != _subscribedKeys.Count) - { - string unknowKeys = string.Empty; - foreach (string key in _subscribedKeys) - { - if (!result.ContainsKey(key)) - { - unknowKeys += key + "\r\n"; - } - } - //System.Diagnostics.Debug.Assert(false, unknowKeys); - } - - InvokeBeforeUpdateProperty(result); - - UpdateValue(result); - - Application.Current.Dispatcher.Invoke(new System.Action(() => - { - InvokePropertyChanged(); - - InvokeAfterUpdateProperty(result); - })); - - - } - } - - private void InvokePropertyChanged() - { - Refresh(); - } - - protected virtual void InvokeBeforeUpdateProperty(Dictionary data) - { - - } - - protected virtual void InvokeAfterUpdateProperty(Dictionary data) - { - - } - - void UpdateValue(Dictionary data) - { - if (data == null) - return; - - UpdateSubscribe(data, this); - - var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttribute() != null); - foreach (var property in properties) - { - var moduleAttr = property.GetCustomAttribute(); - UpdateSubscribe(data, property.GetValue(this), moduleAttr.Module); - } - } - - protected void Subscribe(string key) - { - if (!string.IsNullOrEmpty(key)) - { - _subscribedKeys.Add(key); - } - } - public void SubscribeKeys(TimeredMainViewModel target) { Parallel.ForEach(target.GetType().GetProperties().Where(_hasSubscriptionAttribute), @@ -239,5 +153,98 @@ namespace SicUI }); } + protected virtual void InvokeBeforeUpdateProperty(Dictionary data) + { + + } + + protected virtual void InvokeAfterUpdateProperty(Dictionary data) + { + + } + + protected void Subscribe(string key) + { + if (!string.IsNullOrEmpty(key)) + { + _subscribedKeys.Add(key); + } + } + + protected virtual bool OnTimer() + { + try + { + Poll(); + } + catch (Exception ex) + { + LOG.Error(ex.Message); + } + + return true; + } + + protected virtual void Poll() + { + if (_subscribedKeys.Count > 0) + { + Dictionary result = QueryDataClient.Instance.Service.PollData(_subscribedKeys); + + if (result == null) + { + LOG.Error("获取RT数据失败"); + return; + } + + if (result.Count != _subscribedKeys.Count) + { + string unknowKeys = string.Empty; + foreach (string key in _subscribedKeys) + { + if (!result.ContainsKey(key)) + { + unknowKeys += key + "\r\n"; + } + } + //System.Diagnostics.Debug.Assert(false, unknowKeys); + } + + InvokeBeforeUpdateProperty(result); + + UpdateValue(result); + + Application.Current.Dispatcher.Invoke(new Action(() => + { + InvokePropertyChanged(); + + InvokeAfterUpdateProperty(result); + })); + + + } + } + + private void UpdateValue(Dictionary data) + { + if (data == null) + return; + + UpdateSubscribe(data, this); + + var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttribute() != null); + foreach (var property in properties) + { + var moduleAttr = property.GetCustomAttribute(); + UpdateSubscribe(data, property.GetValue(this), moduleAttr.Module); + } + } + + private void InvokePropertyChanged() + { + Refresh(); + } + + #endregion } }