System.cs 7.7 KB

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