MainWorld.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. LogHub.WorldPublish(Logs, this.GetType().Name);
  49. }
  50. /// <summary>
  51. /// 异常处理,重写改方法后请自行添加执行内容
  52. /// 执行内容:Exception as KnownException并添加至日志队列
  53. /// </summary>
  54. /// <param name="channel"></param>
  55. /// <param name="exception"></param>
  56. /// <exception cref="NotImplementedException"></exception>
  57. protected override void OnError(Channel channel, Exception exception)
  58. {
  59. if (exception is KnownException)
  60. {
  61. var ex = exception as KnownException;
  62. var log = new LogInfo { Level = ex.Level, Type = ErrorTypeEnum.Kown, LogUpLoad = ex.logUpLoad, Message = ex.Message };
  63. Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
  64. }
  65. else
  66. {
  67. var log = new LogInfo { Level = LogLevelEnum.High, Type = ErrorTypeEnum.Unkown, LogUpLoad = LogUpLoadEnum.UpLoadWMS, Message = exception.Message };
  68. Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
  69. }
  70. }
  71. /// <summary>
  72. /// 日志处理,重写改方法后请自行添加执行内容
  73. /// 执行内容:LogInfo as KeyLog并添加至日志队列
  74. /// </summary>
  75. /// <param name="channel"></param>
  76. /// <param name="logObj"></param>
  77. /// <exception cref="NotImplementedException"></exception>
  78. protected override void OnLog(Channel channel, object logObj)
  79. {
  80. if (channel == null) return;
  81. if (logObj.GetType() == typeof(string))
  82. {
  83. Logs.Enqueue(new KeyLog
  84. {
  85. Channel = channel,
  86. Log = new LogInfo()
  87. {
  88. Level = LogLevelEnum.High,
  89. LogUpLoad = LogUpLoadEnum.UpLoadWMS,
  90. Message = logObj as string,
  91. },
  92. Time = DateTime.Now
  93. });
  94. }
  95. else
  96. {
  97. var log = (LogInfo)logObj;
  98. Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
  99. }
  100. }
  101. /// <summary>
  102. /// 日志处理,重写改方法后请自行添加执行内容
  103. /// </summary>
  104. /// <param name="channel"></param>
  105. /// <param name="msg"></param>
  106. /// <exception cref="NotImplementedException"></exception>
  107. protected override void OnInternalLog(Channel channel, string msg)
  108. {
  109. var log = new LogInfo { Level = LogLevelEnum.Low, Message = msg };
  110. if (msg != "开始" && msg != "结束")
  111. {
  112. Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
  113. }
  114. }
  115. /// <summary>
  116. /// 获取日志,重写改方法后请自行添加执行内容
  117. /// </summary>
  118. /// <param name="channel"></param>
  119. /// <returns></returns>
  120. /// <exception cref="NotImplementedException"></exception>
  121. protected override IEnumerable<string> GetChannelMsg(Channel channel)
  122. {
  123. return Logs.Where(v => v.Channel.ToString() == channel.ToString()).Select(v => v.Log.ToString());
  124. }
  125. }
  126. }