123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- using System.Collections.Concurrent;
- using System.ComponentModel;
- using System.Reflection;
- using Dapper;
- using FreeRedis;
- using Npgsql;
- using ServiceCenter.Logs;
- using ServiceCenter.SqlSugars;
- using SqlSugar;
- using WCS.Core;
- using LogInfo = ServiceCenter.Logs.LogInfo;
- namespace WCS.WorkEngineering.Worlds;
- /// <summary>
- /// 主世界,所有的系统(交互点)默认在该世界下执行。
- /// 如有系统需独立,请自行增加对应世界
- /// 新增世界应当继承此世界,而不是直接继承World
- /// </summary>
- [Description("主世界")]
- public class MainWorld : World
- {
- /// <summary>
- /// 数据队列
- /// </summary>
- public static ConcurrentQueue<ProtocolProxyBase> DataQueue = new();
- /// <summary>
- /// redis链接
- /// </summary>
- public static RedisClient Redis = new(Configs.DebugRedisUrl);
- /// <summary>
- /// 日志队列
- /// </summary>
- protected ConcurrentQueue<KeyLog> Logs = new();
- /// <summary>
- /// 构造函数
- /// </summary>
- public MainWorld()
- {
- }
- /// <summary>
- /// 世界执行周期间隔
- /// 单位:毫秒
- /// </summary>
- protected override int Interval => 500;
- /// <summary>
- /// 更新前执行,重写改方法后请自行添加执行内容
- /// 执行内容:清空日志队列
- /// </summary>
- protected override void BeforeUpdate(List<WorkTimes> list)
- {
- // 清空日志队列,确保日志队列中只会有当前周期日志
- Logs.Clear();
- }
- /// <summary>
- /// 更新后执行,重写改方法后请自行添加执行内容
- /// 执行内容:清空日志队列
- /// </summary>
- protected override void AfterUpdate(List<WorkTimes> list)
- {
- //LogHub.WorldPublish(Logs, this.GetType().Name);
- using (var conn = new NpgsqlConnection(Configs.QdbConnString))
- {
- var frameStr = Frame.ToString("yyyy-MM-dd HH:mm:ss.ffffff");
- conn.Open();
- var trans = conn.BeginTransaction();
- try
- {
- if (DataQueue.Count > 0)
- {
- var gs = DataQueue.GroupBy(v => new { type = v.ProtocolDataType, itype = v.ProtocolType }).ToList();
- foreach (var g in gs)
- {
- var tableName = ((SugarTable)g.Key.type.GetCustomAttribute(typeof(SugarTable))).TableName;
- var cmd = $"insert into {tableName} values('{frameStr}',@Code,";
- cmd += string.Join(',', g.Key.itype.GetProperties().Select(v => $"@{v.Name}")) + ")";
- var arr = g.ToArray();
- conn.Execute(cmd, arr);
- }
- }
- if (DataQueue.Count > 0) conn.Execute($"insert into Frames(Frame) values('{frameStr}')");
- trans.Commit();
- }
- catch (Exception ex)
- {
- trans.Rollback();
- throw;
- }
- DataQueue.Clear();
- }
- LogHub.WorldPublish(Logs, GetType().Name);
- }
- /// <summary>
- /// 异常处理,重写改方法后请自行添加执行内容
- /// 执行内容:Exception as KnownException并添加至日志队列
- /// </summary>
- /// <param name="channel"></param>
- /// <param name="exception"></param>
- /// <exception cref="NotImplementedException"></exception>
- 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 });
- }
- }
- /// <summary>
- /// 日志处理,重写改方法后请自行添加执行内容
- /// 执行内容:LogInfo as KeyLog并添加至日志队列
- /// </summary>
- /// <param name="channel"></param>
- /// <param name="logObj"></param>
- /// <exception cref="NotImplementedException"></exception>
- 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 });
- }
- }
- /// <summary>
- /// 日志处理,重写改方法后请自行添加执行内容
- /// </summary>
- /// <param name="channel"></param>
- /// <param name="msg"></param>
- /// <exception cref="NotImplementedException"></exception>
- 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 });
- }
- /// <summary>
- /// 获取日志,重写改方法后请自行添加执行内容
- /// </summary>
- /// <param name="channel"></param>
- /// <returns></returns>
- /// <exception cref="NotImplementedException"></exception>
- protected override IEnumerable<string> GetChannelMsg(Channel channel)
- {
- return Logs.Where(v => v.Channel.ToString() == channel.ToString()).Select(v => v.Log.ToString());
- }
- }
|