OperationManager.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 DoOperationWithoutCheck(string operation,params object[] args)
  125. {
  126. if(!ContainsOperation(operation))
  127. {
  128. return false;
  129. }
  130. return DoOperation(operation, args);
  131. }
  132. public bool DoOperation(string operation, params object[] args)
  133. {
  134. if (!ContainsOperation(operation))
  135. {
  136. throw new ApplicationException("调用了未发布的接口" + operation);
  137. }
  138. if (!CanDoOperation(operation, out string reason, args))
  139. {
  140. EV.PostWarningLog("OP", $"Can not execute {operation}, {reason}");
  141. return false;
  142. }
  143. string parameter = "";
  144. if (args.Length == 0)
  145. parameter = "()";
  146. else
  147. {
  148. parameter += "(";
  149. foreach (object o in args)
  150. {
  151. var param1 = "";
  152. if (o.GetType() == typeof(Dictionary<string, object>))
  153. {
  154. param1 += "[";
  155. foreach (var item in (Dictionary<string, object>)(o))
  156. {
  157. var value = item.Value.ToString();
  158. if (value.Length > 10)
  159. value = value.Substring(0, 7) + "...";
  160. param1 += item.Value.ToString();
  161. param1 += ", ";
  162. }
  163. if (param1.Length > 2)
  164. param1 = param1.Remove(param1.Length - 2, 2);
  165. param1 += "]";
  166. parameter += param1;
  167. }
  168. else
  169. {
  170. parameter += o.ToString();
  171. }
  172. parameter += ", ";
  173. }
  174. if (parameter.Length > 2)
  175. parameter = parameter.Remove(parameter.Length - 2, 2);
  176. parameter += ")";
  177. }
  178. if (parameter.Length > 50)
  179. {
  180. parameter = parameter.Substring(0, 50) + "...";
  181. //LOG.Write("long parameter: " + parameter);
  182. }
  183. if (operation != "PMB.SetPMBChillerState")
  184. //EV.PostInfoLog("OP", $"Invoke operation {operation}{parameter}");
  185. if (_operationMap.ContainsKey(operation))
  186. {
  187. _operationMap[operation](operation, args);
  188. }
  189. else
  190. {
  191. if (!_timeOperationMap[operation].Invoke(out string reason1, 0, args))
  192. {
  193. EV.PostWarningLog("OP", reason);
  194. return false;
  195. }
  196. }
  197. return true;
  198. }
  199. public void Traverse(object instance, string keyPrefix)
  200. {
  201. Parallel.ForEach(instance.GetType().GetMethods().Where<MethodInfo>(_hasSubscriptionAttribute), fi =>
  202. {
  203. string key = Parse(fi);
  204. key = string.IsNullOrWhiteSpace(keyPrefix) ? key : string.Format("{0}.{1}", keyPrefix, key);
  205. //Subscribe(key, fi.Invoke(instance, );
  206. });
  207. }
  208. string Parse(MethodInfo member)
  209. {
  210. return _hasSubscriptionAttribute(member) ? (member.GetCustomAttributes(false).First(_isSubscriptionAttribute) as SubscriptionAttribute).Key : null;
  211. }
  212. }
  213. }