SubscriptionViewModelBase.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.Concurrent;
  6. using System.Reflection;
  7. using System.Threading.Tasks;
  8. using Aitex.Core.Util;
  9. using System.Windows;
  10. using System.Windows.Media;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using Aitex.Core.RT.Log;
  14. using Aitex.Core.UI.Control;
  15. using System.Windows.Input;
  16. namespace Aitex.Core.UI.MVVM
  17. {
  18. /*
  19. * 要求绑定的格式参考如下方式,命名统一为ElementData,这样才能统一由程序把需要从RT获取的数据项名称自动添加进来
  20. <my1:AnalogControl Command="{Binding Path=DeviceOperationCommand}" DeviceData="{Binding Path=ElementData[ReactorA.mfcNH3PushH2] }" Width="54" />
  21. */
  22. public class SubscriptionViewModelBase : TimerViewModelBase
  23. {
  24. const string BindingPathName = "ElementData";
  25. protected Func<IEnumerable<string>, Dictionary<string, object>> PollDataFunction;
  26. protected Action<string[]> InvokeFunction;
  27. protected Action<string, string> DeviceFunction;
  28. protected Action<object[]> DeviceControlFunction;
  29. protected Dictionary<string, Func<string, bool>> PreCommand = new Dictionary<string, Func<string, bool>>();
  30. ConcurrentBag<string> _subscribedKeys = new ConcurrentBag<string>();
  31. Func<object, bool> _isSubscriptionAttribute;
  32. Func<MemberInfo, bool> _hasSubscriptionAttribute;
  33. public ICommand InvokeCommand { get; set; }
  34. List<DelegateCommand<string>> _lstICommand = new List<DelegateCommand<string>>();
  35. /// <summary>
  36. /// 设备名称,设备操作
  37. /// </summary>
  38. public ICommand DeviceCommand { get; set; }
  39. public ICommand DeviceControlCommand { get; set; }
  40. public SubscriptionViewModelBase(string name)
  41. : base(name)
  42. {
  43. _isSubscriptionAttribute = attribute => attribute is SubscriptionAttribute;
  44. _hasSubscriptionAttribute = mi => mi.GetCustomAttributes(false).Any(_isSubscriptionAttribute);
  45. InvokeCommand = new DelegateCommand<string>(param =>{
  46. InvokeFunction(param.Split(','));
  47. }, functionName => { return true; });
  48. DeviceCommand = new DelegateCommand<string>(operation =>
  49. {
  50. string[] args = operation.Split(',');
  51. DeviceFunction(args[0], args[1]);
  52. }, functionName => { return true; });
  53. DeviceControlCommand = new DelegateCommand<object>(args =>
  54. {
  55. DeviceControlFunction((object[])args);
  56. }, functionName => { return true; });
  57. TraverseKeys();
  58. }
  59. public static string UIKey(string param1, string param2)
  60. {
  61. return param1 + "." + param2;
  62. }
  63. public static string UIKey(string param1, string param2, string param3)
  64. {
  65. return param1 + "." + param2 + "."+param3;
  66. }
  67. public static string UIKey(string param1, string param2, string param3, string param4)
  68. {
  69. return param1 + "." + param2 + "." + param3 + "."+param4;
  70. }
  71. protected override void Poll()
  72. {
  73. if (PollDataFunction != null && _subscribedKeys.Count > 0)
  74. {
  75. Dictionary<string, object> result = PollDataFunction(_subscribedKeys);
  76. if (result == null)
  77. {
  78. LOG.Error("获取RT数据失败");
  79. return;
  80. }
  81. if (result.Count != _subscribedKeys.Count)
  82. {
  83. string unknowKeys = string.Empty;
  84. foreach (string key in _subscribedKeys)
  85. {
  86. if (!result.ContainsKey(key))
  87. {
  88. unknowKeys += key + "\r\n";
  89. }
  90. }
  91. //if (unknowKeys.Length > 0)
  92. // LOG.Error("UI did not get data element:" + unknowKeys);
  93. }
  94. InvokeBeforeUpdateProperty(result);
  95. UpdateValue(result);
  96. }
  97. }
  98. protected virtual void InvokeBeforeUpdateProperty(Dictionary<string, object> data)
  99. {
  100. }
  101. void UpdateValue(Dictionary<string, object> data)
  102. {
  103. if (data == null)
  104. return;
  105. Parallel.ForEach(this.GetType().GetProperties().Where(_hasSubscriptionAttribute),
  106. property =>
  107. {
  108. PropertyInfo pi = (PropertyInfo)property;
  109. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  110. string key = subscription.ModuleKey;
  111. if (_subscribedKeys.Contains(key) && data.ContainsKey(key))
  112. {
  113. try
  114. {
  115. var convertedValue = Convert.ChangeType(data[key], pi.PropertyType);
  116. var originValue = Convert.ChangeType(pi.GetValue(this, null), pi.PropertyType);
  117. if (originValue != convertedValue)
  118. {
  119. if (pi.Name == "PumpLimitSetPoint")
  120. pi.SetValue(this, convertedValue, null);
  121. else
  122. pi.SetValue(this, convertedValue, null);
  123. }
  124. }
  125. catch (Exception ex)
  126. {
  127. LOG.Error("由RT返回的数据更新失败" + key, ex);
  128. }
  129. }
  130. });
  131. Parallel.ForEach(this.GetType().GetFields().Where(_hasSubscriptionAttribute),
  132. property =>
  133. {
  134. FieldInfo pi = (FieldInfo)property;
  135. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  136. string key = subscription.ModuleKey;
  137. if (_subscribedKeys.Contains(key) && data.ContainsKey(key))
  138. {
  139. try
  140. {
  141. var convertedValue = Convert.ChangeType(data[key], pi.FieldType);
  142. pi.SetValue(this, convertedValue);
  143. }
  144. catch (Exception ex)
  145. {
  146. LOG.Error("由RT返回的数据更新失败" + key, ex);
  147. }
  148. }
  149. });
  150. }
  151. void TraverseKeys()
  152. {
  153. Parallel.ForEach(this.GetType().GetProperties().Where(_hasSubscriptionAttribute),
  154. property =>
  155. {
  156. SubscriptionAttribute subscription = property.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  157. string key = subscription.ModuleKey;
  158. if (!_subscribedKeys.Contains(key))
  159. _subscribedKeys.Add(key);
  160. });
  161. Parallel.ForEach(this.GetType().GetFields().Where(_hasSubscriptionAttribute),
  162. method =>
  163. {
  164. SubscriptionAttribute subscription = method.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute;
  165. string key = subscription.ModuleKey;
  166. if (!_subscribedKeys.Contains(key))
  167. _subscribedKeys.Add(key);
  168. });
  169. }
  170. void Subscribe(Binding binding, string bindingPathName)
  171. {
  172. if (binding != null)
  173. {
  174. string path = binding.Path.Path;
  175. if (path.Contains(bindingPathName) && path.Contains('[') && path.Contains(']'))
  176. {
  177. try
  178. {
  179. Subscribe(path.Substring(path.IndexOf('[') + 1, path.IndexOf(']') - path.IndexOf('[') - 1));
  180. }
  181. catch (Exception ex)
  182. {
  183. LOG.Write(ex);
  184. }
  185. }
  186. }
  187. }
  188. protected void Subscribe(string key)
  189. {
  190. if (!string.IsNullOrEmpty(key) )
  191. {
  192. _subscribedKeys.Add(key);
  193. }
  194. }
  195. protected void Subscribe(string key, object value)
  196. {
  197. if (!string.IsNullOrEmpty(key) )
  198. {
  199. _subscribedKeys.Add(key);
  200. }
  201. }
  202. }
  203. }