整理TimeredMainViewModel.cs代码格式。
This commit is contained in:
parent
b9ab3770bf
commit
8ed0c8561f
|
@ -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<Cali.Screen>.Collection.OneActive
|
||||
{
|
||||
PeriodicJob _timer;
|
||||
ConcurrentBag<string> _subscribedKeys = new ConcurrentBag<string>();
|
||||
|
||||
Func<object, bool> _isSubscriptionAttribute;
|
||||
Func<MemberInfo, bool> _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<string> _subscribedKeys = new();
|
||||
private readonly Func<object, bool> _isSubscriptionAttribute;
|
||||
private readonly Func<MemberInfo, bool> _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
|
||||
|
||||
/// <summary>
|
||||
/// 获取鼠标键盘不活动的时间
|
||||
/// </summary>
|
||||
|
@ -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<string, object> 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<string, object> data)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected virtual void InvokeAfterUpdateProperty(Dictionary<string, object> data)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void UpdateValue(Dictionary<string, object> data)
|
||||
{
|
||||
if (data == null)
|
||||
return;
|
||||
|
||||
UpdateSubscribe(data, this);
|
||||
|
||||
var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttribute<SubscriptionModuleAttribute>() != null);
|
||||
foreach (var property in properties)
|
||||
{
|
||||
var moduleAttr = property.GetCustomAttribute<SubscriptionModuleAttribute>();
|
||||
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<string, object> data)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected virtual void InvokeAfterUpdateProperty(Dictionary<string, object> 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<string, object> 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<string, object> data)
|
||||
{
|
||||
if (data == null)
|
||||
return;
|
||||
|
||||
UpdateSubscribe(data, this);
|
||||
|
||||
var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttribute<SubscriptionModuleAttribute>() != null);
|
||||
foreach (var property in properties)
|
||||
{
|
||||
var moduleAttr = property.GetCustomAttribute<SubscriptionModuleAttribute>();
|
||||
UpdateSubscribe(data, property.GetValue(this), moduleAttr.Module);
|
||||
}
|
||||
}
|
||||
|
||||
private void InvokePropertyChanged()
|
||||
{
|
||||
Refresh();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue