OperationManager.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 Aitex.Core.RT.DataCenter;
  10. using Aitex.Core.RT.Event;
  11. using Aitex.Core.RT.Log;
  12. using Aitex.Core.WCF;
  13. using MECF.Framework.Common.DataCenter;
  14. using MECF.Framework.Common.OperationCenter;
  15. namespace Aitex.Core.RT.OperationCenter
  16. {
  17. public class OperationManager : ICommonOperation, IOperation
  18. {
  19. private ConcurrentDictionary<string, Func<string, object[], bool>> _operationMap = new ConcurrentDictionary<string, Func<string, object[], bool>>();
  20. private ConcurrentDictionary<string, OperationFunction> _timeOperationMap = new ConcurrentDictionary<string, OperationFunction>();
  21. private Func<object, bool> _isSubscriptionAttribute;
  22. private Func<MemberInfo, bool> _hasSubscriptionAttribute;
  23. private Dictionary<string, List<IInterlockChecker>> _operationInterlockCheck = new Dictionary<string, List<IInterlockChecker>>();
  24. public OperationManager()
  25. {
  26. }
  27. public void Initialize(bool enableService)
  28. {
  29. _isSubscriptionAttribute = attribute => attribute is SubscriptionAttribute;
  30. _hasSubscriptionAttribute = mi => mi.GetCustomAttributes(false).Any(_isSubscriptionAttribute);
  31. OP.InnerOperationManager = this;
  32. OP.OperationManager = this;
  33. if (enableService)
  34. {
  35. Singleton<WcfServiceManager>.Instance.Initialize(new Type[]
  36. {
  37. typeof(InvokeService)
  38. });
  39. }
  40. }
  41. public void Initialize()
  42. {
  43. Initialize(true);
  44. }
  45. public void Subscribe<T>(T instance, string keyPrefix = null) where T : class
  46. {
  47. if (instance == null)
  48. throw new ArgumentNullException("instance");
  49. Traverse(instance, keyPrefix);
  50. }
  51. public void Subscribe(string key, OperationFunction op)
  52. {
  53. if (string.IsNullOrWhiteSpace(key))
  54. throw new ArgumentNullException("key");
  55. if (_timeOperationMap.ContainsKey(key))
  56. throw new Exception(string.Format("Duplicated Key:{0}", key));
  57. if (op == null)
  58. throw new ArgumentNullException("op");
  59. _timeOperationMap[key] = op;
  60. }
  61. public void Subscribe(string key, Func<string, object[], bool> op)
  62. {
  63. if (string.IsNullOrWhiteSpace(key))
  64. throw new ArgumentNullException("key");
  65. if (_operationMap.ContainsKey(key))
  66. throw new Exception(string.Format("Duplicated Key:{0}", key));
  67. if (op == null)
  68. throw new ArgumentNullException("op");
  69. _operationMap[key] = op;
  70. }
  71. /// <summary>
  72. ///
  73. /// </summary>
  74. /// <param name="operation">操作名</param>
  75. /// <param name="time">毫秒</param>
  76. /// <param name="reason">失败原因</param>
  77. /// <param name="args">参数</param>
  78. /// <returns></returns>
  79. public bool DoOperation(string operation, out string reason, int time, params object[] args)
  80. {
  81. if (!CanDoOperation(operation, out reason, args))
  82. {
  83. EV.PostWarningLog("OP", $"Can not execute {operation}, {reason}");
  84. return false;
  85. }
  86. if (!_timeOperationMap.ContainsKey(operation) || !_timeOperationMap[operation].Invoke(out reason, time, args))
  87. {
  88. LOG.Error($"Do operation {operation} failed, {reason}");
  89. return false;
  90. }
  91. return true;
  92. }
  93. public bool ContainsOperation(string operation)
  94. {
  95. return _timeOperationMap.ContainsKey(operation) || _operationMap.ContainsKey(operation);
  96. }
  97. public bool AddCheck(string operation, IInterlockChecker check)
  98. {
  99. if (!_operationInterlockCheck.ContainsKey(operation))
  100. _operationInterlockCheck[operation] = new List<IInterlockChecker>();
  101. _operationInterlockCheck[operation].Add(check);
  102. return true;
  103. }
  104. public bool CanDoOperation(string operation, out string reason, params object[] args)
  105. {
  106. if (!ContainsOperation(operation))
  107. {
  108. reason = $"Call unregistered function, {operation}";
  109. return false;
  110. }
  111. if (_operationInterlockCheck.ContainsKey(operation))
  112. {
  113. foreach (var interlockChecker in _operationInterlockCheck[operation])
  114. {
  115. if (!interlockChecker.CanDo(out reason, args))
  116. {
  117. return false;
  118. }
  119. }
  120. }
  121. reason = string.Empty;
  122. return true;
  123. }
  124. public bool DoOperation(string operation, params object[] args)
  125. {
  126. if (!ContainsOperation(operation))
  127. {
  128. throw new ApplicationException("调用了未发布的接口" + operation);
  129. }
  130. if (!CanDoOperation(operation, out string reason, args))
  131. {
  132. EV.PostWarningLog("OP", $"Can not execute {operation}, {reason}");
  133. return false;
  134. }
  135. string parameter = "";
  136. if (args.Length == 0)
  137. parameter = "()";
  138. else
  139. {
  140. parameter += "(";
  141. foreach (object o in args)
  142. {
  143. var param1 = "";
  144. if (o.GetType() == typeof(Dictionary<string, object>))
  145. {
  146. param1 += "[";
  147. foreach (var item in (Dictionary<string, object>)(o))
  148. {
  149. var value = item.Value.ToString();
  150. if (value.Length > 10)
  151. value = value.Substring(0, 7) + "...";
  152. param1 += item.Value.ToString();
  153. param1 += ", ";
  154. }
  155. if (param1.Length > 2)
  156. param1 = param1.Remove(param1.Length - 2, 2);
  157. param1 += "]";
  158. parameter += param1;
  159. }
  160. else
  161. {
  162. parameter += o.ToString();
  163. }
  164. parameter += ", ";
  165. }
  166. if (parameter.Length > 2)
  167. parameter = parameter.Remove(parameter.Length - 2, 2);
  168. parameter += ")";
  169. }
  170. if (parameter.Length > 50)
  171. {
  172. parameter = parameter.Substring(0, 50) + "...";
  173. LOG.Write("long parameter: " + parameter);
  174. }
  175. if (operation != "PMB.SetPMBChillerState")
  176. EV.PostInfoLog("OP", $"Invoke operation {operation}{parameter}");
  177. if (_operationMap.ContainsKey(operation))
  178. {
  179. _operationMap[operation](operation, args);
  180. }
  181. else
  182. {
  183. if (!_timeOperationMap[operation].Invoke(out string reason1, 0, args))
  184. {
  185. EV.PostWarningLog("OP", reason);
  186. return false;
  187. }
  188. }
  189. return true;
  190. }
  191. public void Traverse(object instance, string keyPrefix)
  192. {
  193. Parallel.ForEach(instance.GetType().GetMethods().Where<MethodInfo>(_hasSubscriptionAttribute), fi =>
  194. {
  195. string key = Parse(fi);
  196. key = string.IsNullOrWhiteSpace(keyPrefix) ? key : string.Format("{0}.{1}", keyPrefix, key);
  197. //Subscribe(key, fi.Invoke(instance, );
  198. });
  199. }
  200. string Parse(MethodInfo member)
  201. {
  202. return _hasSubscriptionAttribute(member) ? (member.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute).Key : null;
  203. }
  204. }
  205. }