MainWorld.cs 4.8 KB

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