Browse Source

WorkHandler

林豪 左 3 years ago
parent
commit
2c729c6348

+ 0 - 32
Projects/永冠OPP/WCS.Service/Exception.cs

@@ -1,32 +0,0 @@
-using System;
-
-namespace WCS.Service
-{
-    /// <summary>
-    /// 警告异常
-    /// </summary>
-    public class WarnException : Exception
-    {
-        /// <summary>
-        /// 警告异常
-        /// </summary>
-        /// <param name="message"></param>
-        public WarnException(string message) : base(message)
-        {
-        }
-    }
-
-    /// <summary>
-    /// 执行记录
-    /// </summary>
-    public class DoException : Exception
-    {
-        /// <summary>
-        /// 警告异常
-        /// </summary>
-        /// <param name="message"></param>
-        public DoException(string message) : base(message)
-        {
-        }
-    }
-}

+ 149 - 0
Projects/永冠OPP/WCS.Service/Handlers/WorkHandler.cs

@@ -0,0 +1,149 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Threading.Tasks;
+using WCS.Core;
+using WCS.Entity;
+using WCS.Service.Helpers;
+
+namespace WCS.Service.Handlers
+{
+    /// <summary>
+    /// 工作处理器
+    /// </summary>
+    public abstract class WorkHandler : LogicHandler
+    {
+        /// <summary>
+        /// 所有被申明的工作处理器
+        /// </summary>
+        protected List<WorkInfo> Works = new List<WorkInfo>();
+
+        protected WorkHandler()
+        {
+            //所有被声明的处理器
+            var arr = Assembly.GetEntryAssembly()
+                ?.GetTypes().Where(v => v.IsSubclassOf(typeof(Work))).Where(v =>
+                {
+                    var attr = v.GetCustomAttribute<WorkTitleAttribute>();
+                    if (attr == null)
+                        return false;
+                    return attr.Handler == this.GetType();
+                });
+            var works = arr.Select(v => Activator.CreateInstance(v) as Work).Select(v =>
+            {
+                var attr = v.GetType().GetCustomAttribute<WorkTitleAttribute>();
+                return new WorkInfo { Params = v.GetObjs(), Work = v.Execute, Title = attr.Title, Parallel = attr.Parallel };
+            }).ToArray();
+            Works.AddRange(works);
+        }
+
+        public override sealed void Start()
+        {
+        }
+
+        /// <summary>
+        /// 执行处理中心--中心级
+        /// </summary>
+        /// <param name="milliseconds"></param>
+        public override void Update(double milliseconds)
+        {
+            if (ParallelRun)
+            {
+                Parallel.ForEach(Works, DoWork);
+            }
+            else
+            {
+                foreach (var w in Works)
+                {
+                    DoWork(w);
+                }
+            }
+        }
+
+        /// <summary>
+        /// 执行级
+        /// </summary>
+        /// <param name="work"></param>
+        protected virtual void DoWork(WorkInfo work)
+        {
+            if (work.Parallel)
+            {
+                Parallel.ForEach(work.Params, p =>
+                {
+                    Do(work, p);
+                });
+            }
+            else
+            {
+                foreach (var p in work.Params)
+                {
+                    Do(work, p);
+                }
+            }
+        }
+
+        protected virtual void Do(WorkInfo wi, object p)
+        {
+            var dt = DateTime.Now;
+            var channel = Description + "." + wi.Title + "." + p;
+            var code = "";
+            if (p is IProtocol protocol)
+            {
+                code = protocol.PROTOCOL().DEVICE.CODE;
+            }
+            try
+            {
+                Ltc.SetChannel(channel);
+                Ltc.Log("开始---------------------------------------");
+                wi.Work(p);
+            }
+            //下述日志处理方案可根据项目情况自定义
+            //DoException为基础条件未满足,仅作记录文本日志
+            catch (DoException ex)
+            {
+                InfoLog.INFO_INFO($"[{code}]--{ex.Message}");
+            }
+            //WarnException进阶条件未满足,添加数据库,记录文本日志、数据库,上抛WCS,上抛WMS
+            catch (WarnException ex)
+            {
+                if (ex.RECORDTXT)
+                {
+                    InfoLog.INFO_WARN($"[{code}]--{ex.Message}");
+                }
+                if (ex.RECORDDB)
+                {
+                    LogHelper.AddWCS_EXCEPTION(ex.Message, code, WCS_EXCEPTIONTYPE.无.ToString());
+                }
+                if (ex.REPORTWCS)
+                {
+                    Ltc.Log(ex.GetBaseException().Message);
+                }
+
+                if (ex.REPORTWMS)
+                {
+                    Configs.UploadException?.Invoke(p.ToString(), ex.GetBaseException().Message);
+                }
+            }
+            //未知异常,仅记录文本日志,需定期排查该文件,检查系统是否有未知异常,并处理
+            catch (Exception ex)
+            {
+                InfoLog.INFO_ERROR($"[{code}]--{ex.Message}--{ex.StackTrace}");
+            }
+            finally
+            {
+                var dd = (DateTime.Now - dt).TotalMilliseconds;
+                if (dd > 500)
+                {
+                    Console.ForegroundColor = ConsoleColor.Red;
+                    Console.WriteLine(channel + "耗时" + dd);
+                    Console.ResetColor();
+                }
+
+                if (dd > 10000)
+                    Configs.UploadException?.Invoke(p.ToString(), wi.Title + "执行耗时" + Math.Floor(dd / 1000) + "秒");
+                Ltc.Log("结束\n");
+            }
+        }
+    }
+}

+ 7 - 56
Projects/永冠OPP/WCS.Service/Helpers/LogHelper.cs

@@ -1,66 +1,29 @@
 using DBHelper;
 using Microsoft.EntityFrameworkCore;
 using System;
-using System.Diagnostics;
 using System.Linq;
+using WCS.Core;
 using WCS.Entity;
-using WCS.Service.Log;
 
 namespace WCS.Service.Helpers
 {
     public class LogHelper
     {
-        ///// <summary>
-        ///// 添加异常记录
-        ///// </summary>
-        ///// <param name="msg">异常信息</param>
-        ///// <param name="time">时间</param>
-        //public static void AddWCS_EXCEPTION(string msg, DateTime time)
-        //{
-        //    DB.Do(db =>
-        //    {
-        //        var exp = db.Default.Set<WCS_EXCEPTION>()
-        //        .Where(v => EF.Functions.DateDiffSecond(v.UPDATETIME, DateTime.Now) < 5)
-        //        .Where(v => v.MSG == msg)
-        //        .OrderByDescending(v => v.ID)
-        //        .FirstOrDefault();
-
-        //        if (exp == null)
-        //        {
-        //            exp = db.Default.Set<WCS_EXCEPTION>().Add(new WCS_EXCEPTION
-        //            {
-        //                MSG = msg,
-        //                STARTTIME = DateTime.Now,
-        //                TIMES = 0,
-        //                UPDATETIME = DateTime.Now,
-        //                UPDATEUSER = "WCS"
-        //            }).Entity;
-        //        }
-        //        exp.TIMES++;
-        //        exp.UPDATETIME = DateTime.Now;
-        //        db.Default.SaveChanges();
-        //    });
-        //}
-
         /// <summary>
         /// 添加异常记录
         /// </summary>
         /// <param name="msg">异常信息</param>
         /// <param name="device">异常关联设备</param>
-        /// <param name="type">异常类型</param>
+        /// <param name="type">异常类型 关联WCS_EXCEPTIONTYPE枚举</param>
         public static void AddWCS_EXCEPTION(string msg, string device, string type)
         {
             DB.Do(db =>
             {
                 var exp = db.Default.Set<WCS_EXCEPTION>()
-                .Where(v => EF.Functions.DateDiffSecond(v.UPDATETIME, DateTime.Now) < 5)
-                .Where(v => v.MSG == msg)
-                .OrderByDescending(v => v.ID)
-                .FirstOrDefault();
-
-                if (exp == null)
-                {
-                    exp = db.Default.Set<WCS_EXCEPTION>().Add(new WCS_EXCEPTION
+                    .Where(v => EF.Functions.DateDiffSecond(v.UPDATETIME, DateTime.Now) < 5)
+                    .Where(v => v.MSG == msg)
+                    .OrderByDescending(v => v.ID)
+                    .FirstOrDefault() ?? db.Default.Set<WCS_EXCEPTION>().Add(new WCS_EXCEPTION
                     {
                         MSG = msg,
                         DEVICE = device,
@@ -70,7 +33,7 @@ namespace WCS.Service.Helpers
                         UPDATETIME = DateTime.Now,
                         UPDATEUSER = "WCS"
                     }).Entity;
-                }
+
                 exp.TIMES++;
                 exp.UPDATETIME = DateTime.Now;
                 db.Default.SaveChanges();
@@ -89,17 +52,5 @@ namespace WCS.Service.Helpers
             InfoLog.INFO_ERROR($"{type1.FullName}--{msg}--{device}--{type}");
             return $"{msg}|{device}|{type}";
         }
-
-        ///// <summary>
-        ///// 秒表
-        ///// </summary>
-        //public static void Timer<T>(Action<> act, string msg)
-        //{
-        //    var timer = new Stopwatch();
-        //    timer.Start();
-          
-        //    timer.Stop();
-        //    InfoLog.INFO_TIMING(msg + $"{timer.ElapsedMilliseconds}");
-        //}
     }
 }

+ 1 - 1
Projects/永冠OPP/WCS.Service/Program.cs

@@ -5,7 +5,7 @@ using System;
 using System.Runtime.InteropServices;
 using System.Threading;
 using System.Threading.Tasks;
-using WCS.Service.Log;
+using WCS.Core;
 
 namespace WCS.Service
 {

+ 0 - 1
Projects/永冠OPP/WCS.Service/Worker.cs

@@ -13,7 +13,6 @@ using System.Threading;
 using System.Threading.Tasks;
 using WCS.Core;
 using WCS.Entity;
-using WCS.Service.Log;
 
 namespace WCS.Service
 {

+ 2 - 2
Projects/永冠OPP/WCS.Service/Works/Station/涂布出库.cs

@@ -10,7 +10,7 @@ using WCS.Service.Entity;
 using WCS.Service.Extensions;
 using WCS.Service.Handlers;
 using WCS.Service.Helpers;
-using WCS.Service.Log;
+using InfoLog = WCS.Core.InfoLog;
 
 namespace WCS.Service.Works.Station
 {
@@ -131,7 +131,7 @@ namespace WCS.Service.Works.Station
                         }
                         var G1 = Device.Find("G1").Create<StationDeviceGroup>();
                         //只能有一组任务的下一个地址是交货点同时,放货点必须无货
-                        if (db.Default.Set<WCS_TASK>().Any(v => v.ADDRNEXT == "G1") || 
+                        if (db.Default.Set<WCS_TASK>().Any(v => v.ADDRNEXT == "G1") ||
                             G1.Items.Any(v => v.Data3.Status.HasFlag(StationStatus.运行状态位)
                                             || v.Data2.Status.HasFlag(IstationStatus.光电状态)
                                             || v.Data2.Tasknum > 10000

+ 40 - 1
WCS.Core/Exception.cs

@@ -8,11 +8,50 @@ namespace WCS.Service
     public class WarnException : Exception
     {
         /// <summary>
-        /// 警告异常
+        /// 报告WCS
+        /// </summary>
+        public bool REPORTWCS { get; set; }
+
+        /// <summary>
+        /// 报告WMS
+        /// </summary>
+        public bool REPORTWMS { get; set; }
+
+        /// <summary>
+        /// 记录文本日志
+        /// </summary>
+        public bool RECORDTXT { get; set; }
+
+        /// <summary>
+        /// 记录数据库
+        /// </summary>
+        public bool RECORDDB { get; set; }
+
+        /// <summary>
+        /// 警告异常 默认都记录
         /// </summary>
         /// <param name="message"></param>
         public WarnException(string message) : base(message)
         {
+            REPORTWCS = true;
+            REPORTWMS = true;
+            RECORDTXT = true;
+            RECORDDB = true;
+        }
+
+        /// <summary>
+        /// 警告异常,自定义上报
+        /// </summary>
+        /// <param name="message"></param>
+        /// <param name="rEPORTWCS"></param>
+        /// <param name="rEPORTWMS"></param>
+        /// <param name="rECORDTXT"></param>
+        public WarnException(string message, bool rEPORTWCS, bool rEPORTWMS, bool rECORDTXT, bool rECORDDB) : base(message)
+        {
+            REPORTWCS = rEPORTWCS;
+            REPORTWMS = rEPORTWMS;
+            RECORDTXT = rECORDTXT;
+            RECORDDB = rECORDDB;
         }
     }
 

+ 4 - 5
Projects/永冠OPP/WCS.Service/Log/InfoLog.cs → WCS.Core/InfoLog.cs

@@ -2,7 +2,7 @@
 using System.Collections.Generic;
 using WCS.Entity;
 
-namespace WCS.Service.Log
+namespace WCS.Core
 {
     /// <summary>
     /// 信息日志
@@ -26,8 +26,7 @@ namespace WCS.Service.Log
 
         static InfoLog()
         {
-            if (Log == null)
-                Log = new InfoLog();
+            Log ??= new InfoLog();
         }
 
         public InfoLog()
@@ -95,8 +94,8 @@ namespace WCS.Service.Log
         public static void INFO_WarnDb(string msg, string code, WCS_EXCEPTIONTYPE type)
         {
             Log.Warn(msg, "INFO_WARN");
-            WCS.Service.Helpers.LogHelper.AddWCS_EXCEPTION(msg, code, type.ToString());
-            WMS.TaskException(code, msg);
+            //WCS.Service.Helpers.LogHelper.AddWCS_EXCEPTION(msg, code, type.ToString());
+            //WMS.TaskException(code, msg);
         }
 
         /// <summary>

+ 2 - 124
WCS.Core/LogicHandler.cs

@@ -2,7 +2,6 @@
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Linq;
-using System.Reflection;
 using System.Threading;
 using System.Threading.Tasks;
 using WCS.Entity;
@@ -35,8 +34,9 @@ namespace WCS.Core
         private static readonly List<LogicHandler> Handlers = new();
 
         /// <summary>
-        /// 日志委托
+        /// 日志委托,暂时弃用
         /// </summary>
+        [Obsolete]
         public static Action<string, string, string> DbLog;
 
         /// <summary>
@@ -353,128 +353,6 @@ namespace WCS.Core
         protected abstract void Execute(T dev);
     }
 
-    /// <summary>
-    /// 工作处理器
-    /// </summary>
-    public abstract class WorkHandler : LogicHandler
-    {
-        /// <summary>
-        /// 所有被申明的工作处理器
-        /// </summary>
-        protected List<WorkInfo> Works = new List<WorkInfo>();
-
-        protected WorkHandler()
-        {
-            var arr = Assembly.GetEntryAssembly()
-                ?.GetTypes().Where(v => v.IsSubclassOf(typeof(Work))).Where(v =>
-            {
-                var attr = v.GetCustomAttribute<WorkTitleAttribute>();
-                if (attr == null)
-                    return false;
-                return attr.Handler == this.GetType();
-            });
-            var works = arr.Select(v => Activator.CreateInstance(v) as Work).Select(v =>
-             {
-                 var attr = v.GetType().GetCustomAttribute<WorkTitleAttribute>();
-                 return new WorkInfo { Params = v.GetObjs(), Work = v.Execute, Title = attr.Title, Parallel = attr.Parallel };
-             }).ToArray();
-            Works.AddRange(works);
-        }
-
-        public override sealed void Start()
-        {
-        }
-
-        /// <summary>
-        /// 执行处理中心
-        /// </summary>
-        /// <param name="milliseconds"></param>
-        public override void Update(double milliseconds)
-        {
-            if (ParallelRun)
-            {
-                Parallel.ForEach(Works, DoWork);
-            }
-            else
-            {
-                foreach (var w in Works)
-                {
-                    DoWork(w);
-                }
-            }
-        }
-
-        protected virtual void DoWork(WorkInfo work)
-        {
-            if (work.Parallel)
-            {
-                Parallel.ForEach(work.Params, p =>
-                {
-                    Do(work, p);
-                });
-            }
-            else
-            {
-                foreach (var p in work.Params)
-                {
-                    Do(work, p);
-                }
-            }
-        }
-
-        protected virtual void Do(WorkInfo wi, object p)
-        {
-            var dt = DateTime.Now;
-            var channel = Description + "." + wi.Title + "." + p.ToString();
-            try
-            {
-                Ltc.SetChannel(channel);
-                Ltc.Log("开始---------------------------------------");
-                wi.Work(p);
-            }
-            catch (Exception ex)
-            {
-                Ltc.Log(ex.GetBaseException().Message);
-                Log(wi, p, ex);
-                Configs.UploadException?.Invoke(p.ToString(), ex.GetBaseException().Message);
-            }
-            finally
-            {
-                var dd = (DateTime.Now - dt).TotalMilliseconds;
-                if (dd > 500)
-                {
-                    Console.ForegroundColor = ConsoleColor.Red;
-                    Console.WriteLine(channel + "耗时" + dd);
-                    Console.ResetColor();
-                }
-                if (dd > 10000)
-                    Configs.UploadException?.Invoke(p.ToString(), wi.Title + "执行耗时" + Math.Floor(dd / 1000) + "秒");
-                Ltc.Log("结束\n");
-            }
-        }
-
-        protected virtual void Log(WorkInfo wi, object p, Exception ex)
-        {
-            try
-            {
-                var msg = Description + "--" + wi.Title + "--";
-                if (p is IProtocol)
-                {
-                    msg += (p as IProtocol).PROTOCOL().DEVICE.CODE;
-                }
-                var con = ex.GetBaseException().Message.Split("|");
-                msg = msg + ":" + con[0];
-                //Console.WriteLine(msg);
-
-                DbLog.Invoke(msg, con[1], con[2]);
-            }
-            catch (Exception)
-            {
-                //TODO:增加一个异常记录
-            }
-        }
-    }
-
     public class WorkTitleAttribute : Attribute
     {
         public Type Handler { get; set; }