林豪 左 3 жил өмнө
parent
commit
9486b53170

+ 18 - 48
Projects/永冠OPP/WCS.Service/WebApi/WMS/WMS.cs

@@ -26,7 +26,7 @@ namespace WCS.Service
         {
             var res = APICaller.CallApi<I_WCS_GetInTaskResponse>(Url + "/api/Task/I_WCS_GetInTask", new List<I_WCS_GetInTaskRequest>()
             {
-                new I_WCS_GetInTaskRequest(){
+                new(){
                      ContainerBarCode = barcode,
                      WareHouseId = wareHouseId,
                      EquipmentNo = devCode,
@@ -50,52 +50,28 @@ namespace WCS.Service
         /// <param name="barcode2">产品2条码</param>
         /// <param name="devCode2">设备2编号</param>
         /// <param name="getTunnel2">产品2是否直接分配巷道</param>
+        /// <exception cref="WarnException"></exception>
         /// <returns></returns>
-        /// <exception cref="Exception"></exception>
         public static List<I_WCS_GetInTaskResponseItem> I_WCS_GetInTask(string barcode1, string devCode1, string barcode2, string devCode2, bool getTunnel1 = false, bool getTunnel2 = false)
         {
             var res = APICaller.CallApi<I_WCS_GetInTaskResponse>(Url + "/api/Task/I_WCS_GetInTask", new List<I_WCS_GetInTaskRequest>()
             {
-                new I_WCS_GetInTaskRequest(){
+                new(){
                      ContainerBarCode = barcode1,
                      WareHouseId = wareHouseId,
                      EquipmentNo = devCode1,
                      Memo1 = getTunnel1 ? "1" : "" //1:分巷道  2:分货位
                 },
-                new I_WCS_GetInTaskRequest(){
+                new(){
                      ContainerBarCode = barcode2,
                      WareHouseId = wareHouseId,
                      EquipmentNo = devCode2,
                      Memo1 = getTunnel2 ? "1" : "" //1:分巷道  2:分货位
                 },
             });
-            if (!res.ResType)
-            {
-                TaskException($"G{devCode1}", res.ResMessage);
-                throw new WarnException(res.ResMessage);
-            }
-            return res.TaskList;
-        }
-
-        //public static I_WCS_GetInTaskResponse PalletInbound(DB db, string barcode, string dev)
-        //{
-        //    var res = APICaller.CallApi<Result>(Url + "/api/PDA/PVCSemiFinishedProductContainerReBack", new PVCSemiFinishedProductReBackRequest
-        //    {
-        //        BarCode = barcode,
-        //        ContainerCode = barcode,
-        //        Quantity = 1
-        //    });
-        //    if (res.StatusCode != Result.Success && res.StatusCode != 4004)
-        //    {
-        //        TaskException(dev, res.Message);
-        //        throw new Exception(res.Message);
-        //    }
-        //    return I_WCS_GetInTask(barcode, dev);
-        //}
-
-        public static short GetPalletType(string rollCode)
-        {
-            return 1;
+            if (res.ResType) return res.TaskList;
+            TaskException($"G{devCode1}", res.ResMessage);
+            throw new WarnException(res.ResMessage);
         }
 
         /// <summary>
@@ -105,8 +81,8 @@ namespace WCS.Service
         /// <param name="tunnel">巷道</param>
         /// <param name="device">设备号</param>
         /// <param name="ForkNum">申请任务对应的货叉</param>
+        /// <exception cref="WarnException"></exception>
         /// <returns></returns>
-        /// <exception cref="Exception"></exception>
         public static I_WCS_GetWareCellResponse GetLocalIn(int wmstaskid, string tunnel, string device, WareCellForkNum ForkNum)
         {
             var res = APICaller.CallApi<I_WCS_GetWareCellResponse>(Url + "/api/Task/I_WCS_GetWareCell", new I_WCS_GetWareCellRequest
@@ -116,19 +92,16 @@ namespace WCS.Service
                 WMSTaskNum = wmstaskid.ToString(),
                 ForkNum = ForkNum
             });
-            if (!res.ResType)
-            {
-                TaskException(device, res.ResMessage);
-                throw new WarnException(res.ResMessage);
-            }
-            return res;
+            if (res.ResType) return res;
+            TaskException(device, res.ResMessage);
+            throw new WarnException(res.ResMessage);
         }
 
         public static void UpdateTask(string POSIDNEXT, int wmstaskid, int status, int size = -1)
         {
             var res = APICaller.CallApi<List<I_WCS_PutTaskStepResponse>>(Url + "/api/Task/I_WCS_PutTaskStep", new List<I_WCS_TASKRequest>
             {
-                new I_WCS_TASKRequest
+                new()
                 {
                     TASK_WKSTATUS = status,
                     TASK_WMSNO = wmstaskid.ToString(),
@@ -154,13 +127,13 @@ namespace WCS.Service
             {
                 if (!res.ResType)
                     throw new WarnException(res.ResMessage);
-                if (res.ChildContainerType <= 0 || res.ChildContainerType > 9)
+                if (res.ChildContainerType is <= 0 or > 9)
                     throw new WarnException("托盘类型错误");
                 if (res.LoadCount <= 0)
                     throw new WarnException("最大组盘数量错误");
                 if (res.DocId == 0)
                     throw new WarnException("单据ID错误");
-                if (res.ProLine < 1 || res.ProLine > 9)
+                if (res.ProLine is < 1 or > 9)
                     throw new WarnException("产线编号" + res.ProLine + "错误");
             }
             catch (Exception ex)
@@ -211,12 +184,9 @@ namespace WCS.Service
                 ContainerCode = containerCode,
                 BarCode = barcode
             });
-            if (!res.ResType)
-            {
-                TaskException(dev, res.ResMessage);
-                throw new WarnException(res.ResMessage);
-            }
-            return res;
+            if (res.ResType) return res;
+            TaskException(dev, res.ResMessage);
+            throw new WarnException(res.ResMessage);
         }
 
         /// <summary>
@@ -264,7 +234,7 @@ namespace WCS.Service
             });
         }
 
-        private static List<I_WCS_PutDevInfoRequest> DevInfoList = new List<I_WCS_PutDevInfoRequest>();
+        private static List<I_WCS_PutDevInfoRequest> DevInfoList = new();
 
         public static void DevInfo(string device, string exMsg)
         {

+ 4 - 1
WCS.Core/DebugPublisher.cs

@@ -17,7 +17,10 @@ namespace WCS.Core
             {
                 Redis.Publish(channel, msg);
             }
-            catch { }
+            catch
+            {
+                // TODO:日常日志
+            }
         }
     }
 }

+ 18 - 10
WCS.Core/LogHelper.cs

@@ -1,10 +1,14 @@
 using System;
+using System.Collections;
 using System.Collections.Concurrent;
 using System.Linq.Expressions;
 using System.Threading;
 
 namespace WCS.Core
 {
+    /// <summary>
+    /// Rdeis 数据上抛
+    /// </summary>
     public static class Ltc
     {
         private static ConcurrentDictionary<Thread, string> Channels = new ConcurrentDictionary<Thread, string>();
@@ -35,15 +39,18 @@ namespace WCS.Core
             {
                 return "null";
             }
-            else if (obj is bool)
-            {
-                var b = obj as Boolean?;
-                return b.Value ? "成立" : "不成立";
-            }
-            else if (obj is System.Collections.ICollection)
-            {
-                var coll = obj as System.Collections.ICollection;
-                return coll.Count.ToString() + "元素";
+
+            switch (obj)
+            {
+                case bool:
+                    {
+                        var b = obj as bool?;
+                        return b.Value ? "成立" : "不成立";
+                    }
+                case ICollection collection:
+                    {
+                        return collection.Count + "元素";
+                    }
             }
 
             return obj.ToString();
@@ -79,8 +86,9 @@ namespace WCS.Core
                 {
                     msg = exp.ExpToString();
                 }
-                catch (Exception ex2)
+                catch (Exception)
                 {
+                    //TODO:日志记录
                 }
                 msg += "  结果:";
                 var res = exp.Compile().Invoke(obj);

+ 75 - 107
WCS.Core/LogicHandler.cs

@@ -1,7 +1,6 @@
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
-using System.IO;
 using System.Linq;
 using System.Reflection;
 using System.Threading;
@@ -23,17 +22,17 @@ namespace WCS.Core
         /// <summary>
         /// 描述
         /// </summary>
-        protected string Description { get; private set; }
+        protected string Description { get; }
 
         /// <summary>
         /// 所有对象
         /// </summary>
-        public static List<object> AllObjects = new List<object>();
+        public static List<object> AllObjects = new();
 
         /// <summary>
         /// 所有处理器
         /// </summary>
-        private static List<LogicHandler> Handlers = new List<LogicHandler>();
+        private static readonly List<LogicHandler> Handlers = new();
 
         /// <summary>
         /// 日志委托
@@ -43,13 +42,10 @@ namespace WCS.Core
         /// <summary>
         /// 逻辑处理器
         /// </summary>
-        public LogicHandler()
+        protected LogicHandler()
         {
             var attr = this.GetType().GetCustomAttributes(false).OfType<DescriptionAttribute>().FirstOrDefault();
-            if (attr != null)
-                Description = attr.Description;
-            else
-                Description = this.GetType().Name;
+            Description = attr != null ? attr.Description : this.GetType().Name;
         }
 
         /// <summary>
@@ -60,13 +56,7 @@ namespace WCS.Core
         /// <summary>
         /// 是否并行
         /// </summary>
-        public virtual bool ParallelRun
-        {
-            get
-            {
-                return false;
-            }
-        }
+        public virtual bool ParallelRun => false;
 
         /// <summary>
         /// 数据刷新时触发
@@ -100,20 +90,22 @@ namespace WCS.Core
             {
                 h.Start();
             }
-            var th = new Thread(new ThreadStart(Loop));
-            th.IsBackground = true; //前台线程即主线程关闭,立即结束该线程
+            var th = new Thread(Loop)
+            {
+                IsBackground = true //前台线程即主线程关闭,立即结束该线程
+            };
             th.Start();
         }
 
         /// <summary>
         /// 最后一次执行结束的时间
         /// </summary>
-        private static DateTime last = DateTime.Now;
+        private static DateTime _last = DateTime.Now;
 
         /// <summary>
         ///逻辑耗时
         /// </summary>
-        private static int logicTimes;
+        private static int _logicTimes;
 
         public static DateTime Frame { get; private set; }
 
@@ -145,16 +137,16 @@ namespace WCS.Core
                 });
                 var dbTimes = (DateTime.Now - dd).TotalMilliseconds;
 
-                var total = (DateTime.Now - last).TotalMilliseconds;
+                var total = (DateTime.Now - _last).TotalMilliseconds;
                 var s = (int)(600 - total);
                 if (s > 0)
                     Thread.Sleep(s);
-                total = (DateTime.Now - last).TotalMilliseconds;
+                total = (DateTime.Now - _last).TotalMilliseconds;
                 Console.ForegroundColor = ConsoleColor.Blue;
                 //此处打印的logicTimes实际是上一个周期的业务处理时长
-                Console.WriteLine("------刷新DB块数据耗时:" + ((int)dbTimes).ToString().PadRight(4, ' ') + ";业务逻辑耗时:" + ((int)logicTimes).ToString().PadRight(4, ' ') + ";周期总耗时" + ((int)total).ToString().PadRight(4, ' ') + "");
+                Console.WriteLine("------刷新DB块数据耗时:" + ((int)dbTimes).ToString().PadRight(4, ' ') + ";业务逻辑耗时:" + ((int)_logicTimes).ToString().PadRight(4, ' ') + ";周期总耗时" + ((int)total).ToString().PadRight(4, ' ') + "");
                 Console.ResetColor();
-                last = DateTime.Now;
+                _last = DateTime.Now;
 
                 Parallel.ForEach(Handlers, m =>
                 {
@@ -163,7 +155,7 @@ namespace WCS.Core
                     {
                         m.Update(total);
                     }
-                    catch (Exception ex)
+                    catch (Exception)
                     {
                         //TODO:增加一个异常记录
                     }
@@ -171,7 +163,7 @@ namespace WCS.Core
                 });
 
                 Configs.Publish();
-                logicTimes = (int)(DateTime.Now - last).TotalMilliseconds;
+                _logicTimes = (int)(DateTime.Now - _last).TotalMilliseconds;
             }
         }
 
@@ -186,7 +178,10 @@ namespace WCS.Core
                 {
                     h.Stop();
                 }
-                catch { }
+                catch
+                {
+                    // TODO:等待处理
+                }
             }
         }
     }
@@ -200,36 +195,41 @@ namespace WCS.Core
         /// <summary>
         /// 业务类集合
         /// </summary>
-        protected List<WorkInfo<T>> Works = new List<WorkInfo<T>>();
+        protected List<WorkInfo<T>> Works = new();
 
         /// <summary>
         /// TODO:?
         /// </summary>
-        private IEnumerable<T> _Objects = null;
+        private IEnumerable<T> _objects = null;
 
         protected IEnumerable<T> Objects
         {
             get
             {
-                if (_Objects == null)
-                {
-                    _Objects = AllObjects.OfType<WCS_DEVICE>().Where(v => SelectDevice(v)).Select(v => Activator.CreateInstance(typeof(T), v)).OfType<T>().ToArray();
-                }
-                return _Objects.Where(v => v.Entity.ENABLED && v.Entity.PROTOCOLS.All(d => d.ENABLED && d.DB.ENABLED && d.DB.PLC.ENABLED));
+                _objects ??= AllObjects.OfType<WCS_DEVICE>().Where(SelectDevice)
+                    .Select(v => Activator.CreateInstance(typeof(T), v)).OfType<T>().ToArray();
+                return _objects.Where(v => v.Entity.ENABLED && v.Entity.PROTOCOLS.All(d => d.ENABLED && d.DB.ENABLED && d.DB.PLC.ENABLED));
             }
         }
 
-        private bool SelectDevice(WCS_DEVICE dev)
+        /// <summary>
+        /// 写入设备信息
+        /// </summary>
+        /// <param name="dev">设备</param>
+        /// <returns></returns>
+        private static bool SelectDevice(WCS_DEVICE dev)
         {
             var typenames = typeof(T).GenericTypeArguments.Select(v => v.AssemblyQualifiedName).ToArray();
             var res = typenames.All(v => dev.PROTOCOLS.Any(d => d.DB.PROTOCOL == v));
             return res;
         }
 
-        public LogicHandler()
-        {
-        }
-
+        /// <summary>
+        /// 添加Work
+        /// </summary>
+        /// <param name="condition"></param>
+        /// <param name="work">work</param>
+        /// <param name="parallel">是否并发</param>
         public void AddWork(Func<T, bool> condition, Action<T> work, bool parallel = false)
         {
             var title = work.Method.Name;
@@ -240,6 +240,12 @@ namespace WCS.Core
             this.Works.Add(new WorkInfo<T> { Work = work, Params = arr, Title = title, Parallel = parallel });
         }
 
+        /// <summary>
+        ///
+        /// </summary>
+        /// <param name="arr"></param>
+        /// <param name="work"></param>
+        /// <param name="parallel"></param>
         public void AddWork(IEnumerable<T> arr, Action<T> work, bool parallel = false)
         {
             var title = work.Method.Name;
@@ -250,14 +256,15 @@ namespace WCS.Core
             this.Works.Add(new WorkInfo<T> { Work = work, Params = arr, Title = title, Parallel = parallel });
         }
 
+        /// <summary>
+        /// 开始执行业务流程
+        /// </summary>
+        /// <param name="milliseconds"></param>
         public override void Update(double milliseconds)
         {
             if (ParallelRun)
             {
-                Parallel.ForEach(Works, w =>
-                {
-                    DoWork(w);
-                });
+                Parallel.ForEach(Works, DoWork);
             }
             else
             {
@@ -268,6 +275,10 @@ namespace WCS.Core
             }
         }
 
+        /// <summary>
+        /// 执行Work
+        /// </summary>
+        /// <param name="work"></param>
         protected virtual void DoWork(WorkInfo<T> work)
         {
             if (work.Parallel)
@@ -286,9 +297,14 @@ namespace WCS.Core
             }
         }
 
+        /// <summary>
+        /// 开始执行
+        /// </summary>
+        /// <param name="wi"></param>
+        /// <param name="p"></param>
         protected virtual void Do(WorkInfo<T> wi, T p)
         {
-            var channel = Description + "." + wi.Title + "." + p.ToString();
+            var channel = Description + "." + wi.Title + "." + p;
             try
             {
                 Ltc.SetChannel(channel);
@@ -323,32 +339,9 @@ namespace WCS.Core
 
                 DbLog.Invoke(msg, con[1], con[2]);
             }
-            catch (Exception ex2)
+            catch (Exception)
             {
-            }
-        }
-
-        private static object lockobj = new object();
-
-        protected void Log(string msg)
-        {
-            lock (lockobj)
-            {
-                try
-                {
-                    var str = "-----------" + DateTime.Now.ToString("yyyyMMdd HH:mm:ss:fff") + "-----------\r\n";
-                    str += msg;
-                    if (!Directory.Exists(logPath))
-                        Directory.CreateDirectory(logPath);
-                    var file = logPath + DateTime.Now.ToString("yyyyMMdd") + ".log";
-                    var writer = File.AppendText(file);
-                    writer.WriteLine(str);
-                    writer.Flush();
-                    writer.Close();
-                }
-                catch (Exception ex)
-                {
-                }
+                //TODO:增加一个异常记录
             }
         }
 
@@ -370,9 +363,10 @@ namespace WCS.Core
         /// </summary>
         protected List<WorkInfo> Works = new List<WorkInfo>();
 
-        public WorkHandler()
+        protected WorkHandler()
         {
-            var arr = Assembly.GetEntryAssembly().GetTypes().Where(v => v.IsSubclassOf(typeof(Work))).Where(v =>
+            var arr = Assembly.GetEntryAssembly()
+                ?.GetTypes().Where(v => v.IsSubclassOf(typeof(Work))).Where(v =>
             {
                 var attr = v.GetCustomAttribute<WorkTitleAttribute>();
                 if (attr == null)
@@ -399,10 +393,7 @@ namespace WCS.Core
         {
             if (ParallelRun)
             {
-                Parallel.ForEach(Works, w =>
-                {
-                    DoWork(w);
-                });
+                Parallel.ForEach(Works, DoWork);
             }
             else
             {
@@ -477,32 +468,9 @@ namespace WCS.Core
 
                 DbLog.Invoke(msg, con[1], con[2]);
             }
-            catch (Exception ex2)
-            {
-            }
-        }
-
-        private static object lockobj = new object();
-
-        protected void Log(string msg)
-        {
-            lock (lockobj)
+            catch (Exception)
             {
-                try
-                {
-                    var str = "-----------" + DateTime.Now.ToString("yyyyMMdd HH:mm:ss:fff") + "-----------\r\n";
-                    str += msg;
-                    if (!Directory.Exists(logPath))
-                        Directory.CreateDirectory(logPath);
-                    var file = logPath + DateTime.Now.ToString("yyyyMMdd") + ".log";
-                    var writer = File.AppendText(file);
-                    writer.WriteLine(str);
-                    writer.Flush();
-                    writer.Close();
-                }
-                catch (Exception ex)
-                {
-                }
+                //TODO:增加一个异常记录
             }
         }
     }
@@ -523,7 +491,7 @@ namespace WCS.Core
 
     public abstract class Work
     {
-        public abstract IEnumerable<Object> GetObjs();
+        public abstract IEnumerable<object> GetObjs();
 
         public abstract void Execute(object obj);
     }
@@ -547,7 +515,7 @@ namespace WCS.Core
         protected virtual IEnumerable<T> InitObjects()
         {
             var arr = Device.Where(v => v.ENABLED)
-                     .Where(v => SelectDevice(v)).ToArray();
+                     .Where(SelectDevice).ToArray();
             var res = arr.Select(v => (T)Activator.CreateInstance(typeof(T), v));
             return res;
         }
@@ -555,11 +523,11 @@ namespace WCS.Core
 
     public abstract class DeviceWork<T> : Work<T> where T : EntityEx<WCS_DEVICE>
     {
-        private string[] typenames;
+        private readonly string[] _typenames;
 
-        public DeviceWork()
+        protected DeviceWork()
         {
-            typenames = typeof(T).GenericTypeArguments.Select(v => v.AssemblyQualifiedName).ToArray();
+            _typenames = typeof(T).GenericTypeArguments.Select(v => v.AssemblyQualifiedName).ToArray();
         }
 
         protected abstract override void Do(T obj);
@@ -567,8 +535,8 @@ namespace WCS.Core
         protected override sealed IEnumerable<T> InitObjects()
         {
             var arr = Device.Where(v => v.ENABLED && v.PROTOCOLS.All(d => d.ENABLED && d.DB.ENABLED && d.DB.PLC.ENABLED))
-                   .Where(v => typenames.All(d => v.PROTOCOLS.Any(e => e.DB.PROTOCOL == d)))
-                   .Where(v => SelectDevice(v)).ToArray();
+                   .Where(v => _typenames.All(d => v.PROTOCOLS.Any(e => e.DB.PROTOCOL == d)))
+                   .Where(SelectDevice).ToArray();
             var res = arr.Select(v => Activator.CreateInstance(typeof(T), v) as T);
 
             return res;