System.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using System.Collections.Concurrent;
  2. using System.Diagnostics;
  3. using System.Reflection;
  4. namespace WCS.Core
  5. {
  6. public abstract class SystemBase : DescriptionClass
  7. {
  8. public World World { get; private set; }
  9. public SystemBase()
  10. {
  11. var attr = this.GetType().GetCustomAttribute<BelongToAttribute>();
  12. if (attr != null)
  13. {
  14. var wt = attr.WorldType;
  15. this.World = World.Worlds.Where(v => v.GetType() == wt).First();
  16. }
  17. }
  18. public abstract List<object> GetObjects();
  19. public abstract void Update(List<WorkTimes> list);
  20. }
  21. public abstract class SystemBase<T> : SystemBase
  22. {
  23. public List<T> Objects { get; set; }
  24. /// <summary>
  25. /// 对所有Objects并行循环执行Do
  26. /// </summary>
  27. protected abstract bool ParallelDo { get; }
  28. /// <summary>
  29. /// 保存日志到文件
  30. /// </summary>
  31. protected abstract bool SaveLogsToFile { get; }
  32. public SystemBase()
  33. {
  34. Objects = Create();//.Select(v=>Activator.CreateInstance(typeof(T),v)).OfType<T>().ToList();
  35. }
  36. public override void Update(List<WorkTimes> list)
  37. {
  38. if (ParallelDo)
  39. {
  40. Parallel.ForEach(Objects, new ParallelOptions { MaxDegreeOfParallelism = 256 }, obj =>
  41. {
  42. var sw = new Stopwatch();
  43. sw.Start();
  44. InvokeDo(obj);
  45. sw.Stop();
  46. list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
  47. });
  48. }
  49. else
  50. {
  51. foreach (var obj in Objects)
  52. {
  53. var sw = new Stopwatch();
  54. sw.Start();
  55. InvokeDo(obj);
  56. sw.Stop();
  57. list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
  58. }
  59. }
  60. }
  61. private void InvokeDo(T obj)
  62. {
  63. var channel = new Channel
  64. {
  65. World = World.Description,
  66. Stage = "DoLogics",
  67. System = Description,
  68. Item = obj.ToString()
  69. };
  70. try
  71. {
  72. if (channel.Item == "1")
  73. {
  74. var a = 1;
  75. }
  76. Ltc.SetChannel(channel);
  77. World.OnInternalLog(channel, "开始");
  78. Do(obj);
  79. }
  80. catch (Exception ex)
  81. {
  82. World.OnError(channel, ex);
  83. }
  84. finally
  85. {
  86. World.OnInternalLog(channel, "结束");
  87. World.Publish();
  88. }
  89. }
  90. public abstract List<T> Create();
  91. public abstract void Do(T obj);
  92. public override List<object> GetObjects()
  93. {
  94. return Objects.OfType<object>().ToList();
  95. }
  96. }
  97. public abstract class DeviceSystem<T> : SystemBase<T> where T : EntityEx<Device>
  98. {
  99. public override List<T> Create()
  100. {
  101. var t = typeof(T);
  102. while (true)
  103. {
  104. if (t.IsGenericType)
  105. {
  106. break;
  107. }
  108. t = t.BaseType;
  109. }
  110. var types = t.GetGenericArguments();
  111. var list = Device.All.Where(v => types.All(d => v.HasProtocol(d)))
  112. .Where(v => Select(v))
  113. .Select(v => Activator.CreateInstance(typeof(T), v, this.World)).OfType<T>().ToList();//此时才实例化Protocol
  114. if (list.Count == 0)
  115. {
  116. //throw new Exception($"{this.GetType().Name}未匹配到任何Device");
  117. }
  118. return list;
  119. }
  120. /// <summary>
  121. /// 筛选出需要实例化Protocol的Device
  122. /// </summary>
  123. /// <param name="dev"></param>
  124. /// <returns></returns>
  125. public abstract bool Select(Device dev);
  126. //public abstract List<Device> CreateDevices();
  127. }
  128. public abstract class ServiceSystem<T, TR> : SystemBase
  129. {
  130. private ConcurrentQueue<Action<List<WorkTimes>>> Actions = new ConcurrentQueue<Action<List<WorkTimes>>>();
  131. public TR Invoke(T obj)
  132. {
  133. var flag = false;
  134. TR result = default(TR);
  135. Actions.Enqueue(list =>
  136. {
  137. var sw = new Stopwatch();
  138. sw.Start();
  139. try
  140. {
  141. result = InvokeDo(obj);
  142. }
  143. finally
  144. {
  145. sw.Stop();
  146. list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
  147. flag = true;
  148. }
  149. });
  150. SpinWait.SpinUntil(() => flag);
  151. return result;
  152. }
  153. protected abstract TR Do(T obj);
  154. public override List<object> GetObjects()
  155. {
  156. return new List<object>();
  157. }
  158. public override void Update(List<WorkTimes> list)
  159. {
  160. while (Actions.TryDequeue(out var act))
  161. {
  162. act(list);
  163. }
  164. }
  165. private TR InvokeDo(T obj)
  166. {
  167. var channel = new Channel
  168. {
  169. World = World.Description,
  170. Stage = "DoLogics",
  171. System = Description,
  172. Item = obj.ToString()
  173. };
  174. try
  175. {
  176. Ltc.SetChannel(channel);
  177. World.OnInternalLog(channel, "开始");
  178. return Do(obj);
  179. }
  180. catch (Exception ex)
  181. {
  182. throw;
  183. }
  184. finally
  185. {
  186. World.OnInternalLog(channel, "结束");
  187. World.Publish();
  188. }
  189. }
  190. }
  191. public abstract class ServiceSystem<T> : SystemBase
  192. {
  193. private ConcurrentQueue<Action<List<WorkTimes>>> Actions = new ConcurrentQueue<Action<List<WorkTimes>>>();
  194. public void Invoke(T obj)
  195. {
  196. Actions.Enqueue(list =>
  197. {
  198. var sw = new Stopwatch();
  199. sw.Start();
  200. try
  201. {
  202. InvokeDo(obj);
  203. }
  204. finally
  205. {
  206. sw.Stop();
  207. list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
  208. }
  209. });
  210. }
  211. protected abstract void Do(T obj);
  212. private void InvokeDo(T obj)
  213. {
  214. var channel = new Channel
  215. {
  216. World = World.Description,
  217. Stage = "DoLogics",
  218. System = Description,
  219. Item = obj.ToString()
  220. };
  221. try
  222. {
  223. Ltc.SetChannel(channel);
  224. World.OnInternalLog(channel, "开始");
  225. Do(obj);
  226. }
  227. catch (Exception ex)
  228. {
  229. throw;
  230. }
  231. finally
  232. {
  233. World.OnInternalLog(channel, "结束");
  234. World.Publish();
  235. }
  236. }
  237. public override List<object> GetObjects()
  238. {
  239. return new List<object>();
  240. }
  241. public override void Update(List<WorkTimes> list)
  242. {
  243. while (Actions.TryDequeue(out var act))
  244. {
  245. act(list);
  246. }
  247. }
  248. }
  249. }