System.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. var wt = typeof(World);
  19. if (attr != null)
  20. {
  21. wt = attr.WorldType;
  22. }
  23. this.World = World.Worlds.Where(v => v.GetType() == wt).First();
  24. }
  25. public abstract List<object> GetObjects();
  26. public abstract void Update(List<WorkTimes> list);
  27. public void Log(string msg)
  28. {
  29. Ltc.Log(msg, LogLevel.低, ErrorType.已知);
  30. }
  31. public void Error(string msg,LogLevel level)
  32. {
  33. throw new KnownException(msg, level);
  34. }
  35. }
  36. public abstract class SystemBase<T> : SystemBase
  37. {
  38. public List<T> Objects { get; set; }
  39. /// <summary>
  40. /// 对所有Objects并行循环执行Do
  41. /// </summary>
  42. protected abstract bool ParallelDo { get; }
  43. /// <summary>
  44. /// 保存日志到文件
  45. /// </summary>
  46. protected abstract bool SaveLogsToFile { get; }
  47. public SystemBase()
  48. {
  49. Objects = Create();//.Select(v=>Activator.CreateInstance(typeof(T),v)).OfType<T>().ToList();
  50. }
  51. public override void Update(List<WorkTimes> list)
  52. {
  53. var logs = new List<LogInfo>();
  54. if (ParallelDo)
  55. {
  56. Parallel.ForEach(Objects, new ParallelOptions { MaxDegreeOfParallelism = 256 }, obj =>
  57. {
  58. var sw= new Stopwatch();
  59. sw.Start();
  60. InvokeDo(obj);
  61. sw.Stop();
  62. list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
  63. //var log = Ltc.GetLogStr();
  64. logs.AddSafe(Ltc.GetLogInfo());
  65. });
  66. }
  67. else
  68. {
  69. foreach (var obj in Objects)
  70. {
  71. var sw = new Stopwatch();
  72. sw.Start();
  73. InvokeDo(obj);
  74. sw.Stop();
  75. list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
  76. //var log = Ltc.GetLogStr();
  77. logs.AddSafe(Ltc.GetLogInfo());
  78. }
  79. }
  80. if (SaveLogsToFile)
  81. {
  82. var arr = logs.GroupBy(v => v.Channel).Select(v => new LogInfo { Channel = v.Key, Level = v.Max(d => d.Level), Type = v.Any(d => d.Type == ErrorType.未知) ? ErrorType.未知 : ErrorType.已知, Message = "\n"+string.Join('\n', v.Select(d => d.Message)) }).ToArray();
  83. Configs.OnLog?.Invoke(arr);
  84. }
  85. }
  86. void InvokeDo(T obj)
  87. {
  88. var channel = new Channel
  89. {
  90. World = World.Description,
  91. System = Description,
  92. Item = obj.ToString()
  93. };
  94. try
  95. {
  96. Ltc.SetChannel(channel);
  97. Ltc.ClearChannel();
  98. Ltc.Log("开始", LogLevel.低, ErrorType.已知);
  99. Do(obj);
  100. }
  101. catch (KnownException ex)
  102. {
  103. Ltc.Log(ex.Message, ex.Level, ErrorType.已知);
  104. }
  105. catch (Exception ex)
  106. {
  107. //Console.WriteLine($"{channel}:\n{ex.GetBaseException().Message}");
  108. Ltc.Log(ex.GetBaseException().Message, LogLevel.高, ErrorType.未知);
  109. }
  110. finally
  111. {
  112. Ltc.Log("结束", LogLevel.低, ErrorType.已知);
  113. Ltc.Publish(this.World);
  114. }
  115. }
  116. public abstract List<T> Create();
  117. public abstract void Do(T obj);
  118. public override List<object> GetObjects()
  119. {
  120. return Objects.OfType<object>().ToList();
  121. }
  122. }
  123. public abstract class DeviceSystem<T> : SystemBase<T> where T : EntityEx<Device>
  124. {
  125. public override List<T> Create()
  126. {
  127. var types = typeof(T).GetGenericArguments();
  128. var list= World.Devices.Where(v => types.All(d => v.HasProtocol(d)))
  129. .Where(v => Select(v))
  130. .Select(v => Activator.CreateInstance(typeof(T), v)).OfType<T>().ToList();
  131. if (list.Count == 0)
  132. {
  133. Log($"{GetType().Name}系统未匹配到任何设备");
  134. }
  135. return list;
  136. }
  137. public abstract bool Select(Device dev);
  138. //public abstract List<Device> CreateDevices();
  139. }
  140. }