MainWorld.cs 5.2 KB

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