System.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 types = typeof(T).GetGenericArguments();
  104. var list= World.Devices.Where(v => types.All(d => v.HasProtocol(d)))
  105. .Where(v => Select(v))
  106. .Select(v => Activator.CreateInstance(typeof(T), v)).OfType<T>().ToList();//此时才实例化Protocol
  107. return list;
  108. }
  109. /// <summary>
  110. /// 筛选出需要实例化Protocol的Device
  111. /// </summary>
  112. /// <param name="dev"></param>
  113. /// <returns></returns>
  114. public abstract bool Select(Device dev);
  115. //public abstract List<Device> CreateDevices();
  116. }
  117. public abstract class ServiceSystem<T, TR> : SystemBase
  118. {
  119. ConcurrentQueue<Action<List<WorkTimes>>> Actions = new ConcurrentQueue<Action<List<WorkTimes>>>();
  120. public TR Invoke(T obj)
  121. {
  122. var flag = false;
  123. TR result = default(TR);
  124. Actions.Enqueue(list =>
  125. {
  126. var sw = new Stopwatch();
  127. sw.Start();
  128. try
  129. {
  130. result = InvokeDo(obj);
  131. }
  132. finally
  133. {
  134. sw.Stop();
  135. list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
  136. flag = true;
  137. }
  138. });
  139. SpinWait.SpinUntil(() => flag);
  140. return result;
  141. }
  142. protected abstract TR Do(T obj);
  143. public override List<object> GetObjects()
  144. {
  145. return new List<object>();
  146. }
  147. public override void Update(List<WorkTimes> list)
  148. {
  149. while (Actions.TryDequeue(out var act))
  150. {
  151. act(list);
  152. }
  153. }
  154. TR InvokeDo(T obj)
  155. {
  156. var channel = new Channel
  157. {
  158. World = World.Description,
  159. Stage = "DoLogics",
  160. System = Description,
  161. Item = obj.ToString()
  162. };
  163. try
  164. {
  165. Ltc.SetChannel(channel);
  166. World.OnInternalLog(channel,"开始");
  167. return Do(obj);
  168. }
  169. catch (Exception ex)
  170. {
  171. throw;
  172. }
  173. finally
  174. {
  175. World.OnInternalLog(channel, "结束");
  176. World.Publish();
  177. }
  178. }
  179. }
  180. }