System.cs 6.5 KB

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