using ServiceCenter.Logs; using System.Collections.Concurrent; using System.ComponentModel; using System.Diagnostics; using WCS.Core; using LogInfo = ServiceCenter.Logs.LogInfo; namespace WCS.WorkEngineering.Worlds { /// /// 主世界,所有的系统(交互点)默认在该世界下执行。 /// 如有系统需独立,请自行增加对应世界 /// 新增世界应当继承此世界,而不是直接继承World /// [Description("主世界")] public class MainWorld : World { /// /// 构造函数 /// public MainWorld() { } /// /// 日志队列 /// protected ConcurrentQueue Logs = new ConcurrentQueue(); /// /// 世界执行周期间隔 /// 单位:毫秒 /// protected override int Interval => 300; /// /// 更新前执行,重写改方法后请自行添加执行内容 /// 执行内容:清空日志队列 /// protected override void BeforeUpdate(List list) { // 清空日志队列,确保日志队列中只会有当前周期日志 Logs.Clear(); } /// /// 更新后执行,重写改方法后请自行添加执行内容 /// 执行内容:清空日志队列 /// protected override void AfterUpdate(List list) { //LogHub.WorldPublish(Logs, this.GetType().Name); LogHub.WorldPublish(Logs, this.GetType().Name); } /// /// 异常处理,重写改方法后请自行添加执行内容 /// 执行内容:Exception as KnownException并添加至日志队列 /// /// /// /// protected override void OnError(Channel channel, Exception exception) { if (exception is KnownException) { var ex = exception as KnownException; var log = new LogInfo { Level = ex.Level, Type = ErrorTypeEnum.Kown, LogUpLoad = ex.logUpLoad, Message = ex.Message }; Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now }); } else { var log = new LogInfo { Level = LogLevelEnum.High, Type = ErrorTypeEnum.Unkown, LogUpLoad = LogUpLoadEnum.UpLoadWMS, Message = exception.Message }; Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now }); } } /// /// 日志处理,重写改方法后请自行添加执行内容 /// 执行内容:LogInfo as KeyLog并添加至日志队列 /// /// /// /// protected override void OnLog(Channel channel, object logObj) { if (channel == null) return; if (logObj.GetType() == typeof(string)) { Logs.Enqueue(new KeyLog { Channel = channel, Log = new LogInfo() { Level = LogLevelEnum.High, LogUpLoad = LogUpLoadEnum.UpLoadWMS, Message = logObj as string, }, Time = DateTime.Now }); } else { var log = (LogInfo)logObj; Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now }); } } /// /// 日志处理,重写改方法后请自行添加执行内容 /// /// /// /// protected override void OnInternalLog(Channel channel, string msg) { var log = new LogInfo { Level = LogLevelEnum.Low, Message = msg }; if (msg != "开始" && msg != "结束") { Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now }); } } /// /// 获取日志,重写改方法后请自行添加执行内容 /// /// /// /// protected override IEnumerable GetChannelMsg(Channel channel) { return Logs.Where(v => v.Channel.ToString() == channel.ToString()).Select(v => v.Log.ToString()); } } }