MainWorld.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using ServiceCenter.Logs;
  2. using System.Collections.Concurrent;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using WCS.Core;
  6. using LogInfo = ServiceCenter.Logs.LogInfo;
  7. namespace WCS.WorkEngineering.Worlds
  8. {
  9. /// <summary>
  10. /// 主世界,所有的系统(交互点)默认在该世界下执行。
  11. /// 如有系统需独立,请自行增加对应世界
  12. /// 新增世界应当继承此世界,而不是直接继承World
  13. /// </summary>
  14. [Description("主世界")]
  15. public class MainWorld : World
  16. {
  17. /// <summary>
  18. /// 构造函数
  19. /// </summary>
  20. public MainWorld()
  21. {
  22. }
  23. /// <summary>
  24. /// 日志队列
  25. /// </summary>
  26. protected ConcurrentQueue<KeyLog> Logs = new ConcurrentQueue<KeyLog>();
  27. /// <summary>
  28. /// 世界执行周期间隔
  29. /// 单位:毫秒
  30. /// </summary>
  31. protected override int Interval => 300;
  32. /// <summary>
  33. /// 更新前执行,重写改方法后请自行添加执行内容
  34. /// 执行内容:清空日志队列
  35. /// </summary>
  36. protected override void BeforeUpdate(List<WorkTimes> list)
  37. {
  38. // 清空日志队列,确保日志队列中只会有当前周期日志
  39. Logs.Clear();
  40. }
  41. /// <summary>
  42. /// 更新后执行,重写改方法后请自行添加执行内容
  43. /// 执行内容:清空日志队列
  44. /// </summary>
  45. protected override void AfterUpdate(List<WorkTimes> list)
  46. {
  47. ////LogHub.WorldPublish(Logs, this.GetType().Name);
  48. ////通过异步处理,加快周期速度
  49. //var wt = new WorkTimes
  50. //{
  51. // Key = "更新后执行内容"
  52. //};
  53. //var sw = new Stopwatch();
  54. //sw.Start();
  55. //Task.Run(() =>
  56. //{
  57. // try
  58. // {
  59. // LogHub.WorldPublish(Logs, this.GetType().Name);
  60. // }
  61. // catch (Exception e)
  62. // {
  63. // //Console.WriteLine(e);
  64. // //throw;
  65. // }
  66. //}); //通过异步处理,加快周期速度
  67. //sw.Stop();
  68. //wt.Total = sw.ElapsedMilliseconds;
  69. //list.AddSafe(wt);
  70. }
  71. /// <summary>
  72. /// 异常处理,重写改方法后请自行添加执行内容
  73. /// 执行内容:Exception as KnownException并添加至日志队列
  74. /// </summary>
  75. /// <param name="channel"></param>
  76. /// <param name="exception"></param>
  77. /// <exception cref="NotImplementedException"></exception>
  78. protected override void OnError(Channel channel, Exception exception)
  79. {
  80. if (exception is KnownException)
  81. {
  82. var ex = exception as KnownException;
  83. var log = new LogInfo { Level = ex.Level, Type = ErrorTypeEnum.Kown, LogUpLoad = ex.logUpLoad, Message = ex.Message };
  84. Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
  85. }
  86. else
  87. {
  88. var log = new LogInfo { Level = LogLevelEnum.High, Type = ErrorTypeEnum.Unkown, LogUpLoad = LogUpLoadEnum.UpLoadWMS, Message = exception.Message };
  89. Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
  90. }
  91. }
  92. /// <summary>
  93. /// 日志处理,重写改方法后请自行添加执行内容
  94. /// 执行内容:LogInfo as KeyLog并添加至日志队列
  95. /// </summary>
  96. /// <param name="channel"></param>
  97. /// <param name="logObj"></param>
  98. /// <exception cref="NotImplementedException"></exception>
  99. protected override void OnLog(Channel channel, object logObj)
  100. {
  101. if (channel == null) return;
  102. if (logObj.GetType() == typeof(string))
  103. {
  104. Logs.Enqueue(new KeyLog
  105. {
  106. Channel = channel,
  107. Log = new LogInfo()
  108. {
  109. Level = LogLevelEnum.High,
  110. LogUpLoad = LogUpLoadEnum.UpLoadWMS,
  111. Message = logObj as string,
  112. },
  113. Time = DateTime.Now
  114. });
  115. }
  116. else
  117. {
  118. var log = (LogInfo)logObj;
  119. Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
  120. }
  121. }
  122. /// <summary>
  123. /// 日志处理,重写改方法后请自行添加执行内容
  124. /// </summary>
  125. /// <param name="channel"></param>
  126. /// <param name="msg"></param>
  127. /// <exception cref="NotImplementedException"></exception>
  128. protected override void OnInternalLog(Channel channel, string msg)
  129. {
  130. var log = new LogInfo { Level = LogLevelEnum.Low, Message = msg };
  131. if (msg != "开始" && msg != "结束")
  132. {
  133. Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
  134. }
  135. }
  136. /// <summary>
  137. /// 获取日志,重写改方法后请自行添加执行内容
  138. /// </summary>
  139. /// <param name="channel"></param>
  140. /// <returns></returns>
  141. /// <exception cref="NotImplementedException"></exception>
  142. protected override IEnumerable<string> GetChannelMsg(Channel channel)
  143. {
  144. return Logs.Where(v => v.Channel.ToString() == channel.ToString()).Select(v => v.Log.ToString());
  145. }
  146. }
  147. }