Przeglądaj źródła

fix 变更使用方式

林豪 左 2 lat temu
rodzic
commit
dd648794fe
4 zmienionych plików z 101 dodań i 59 usunięć
  1. 1 10
      WCS.Core/Extentions.cs
  2. 10 4
      WCS.Core/Protocols.cs
  3. 88 5
      WCS.Core/System.cs
  4. 2 40
      WCS.Core/World.cs

+ 1 - 10
WCS.Core/Extentions.cs

@@ -64,16 +64,7 @@ namespace WCS.Core
         {
             return GetEx<WorldEx>(source);
         }
-
-        public static T Do<T,TWorld>(this TWorld source, Func<TWorld,T> func) where TWorld : World
-        {
-            return source._Do<TWorld, T>(w => func(w));
-        }
-
-        public static void Do<TWorld>(this TWorld source, Action<TWorld> action) where TWorld : World
-        {
-            source._Do<TWorld>(action);
-        }
+        
 
         public static void AddSafe<T>(this List<T> source,T item)
         {

+ 10 - 4
WCS.Core/Protocols.cs

@@ -24,6 +24,12 @@ namespace WCS.Core
             return info;
         }
 
+
+        /// <summary>
+        /// 实例化Device,并未实例化Protocol
+        /// </summary>
+        /// <param name="world"></param>
+        /// <returns></returns>
         public static List<Device> Generate(World world)
         {
             List<Device> list = new List<Device>();
@@ -31,10 +37,10 @@ namespace WCS.Core
             foreach (var g in gs)
             {
                 var dev = new Device { Code = g.Key, World = world };
-                foreach (var i in g)
-                {
-                    //dev.Protocol(i.type);
-                }
+                //foreach (var i in g)
+                //{
+                //    //dev.Protocol(i.type);
+                //}
                 list.Add(dev);
             }
 

+ 88 - 5
WCS.Core/System.cs

@@ -21,7 +21,7 @@ namespace WCS.Core
             {
                 wt = attr.WorldType;
             }
-            this.World = World.Worlds.Where(v => v.GetType() == wt).First(); 
+            this.World = World.Worlds.Where(v => v.GetType() == wt).First();
         }
 
         public abstract List<object> GetObjects();
@@ -125,10 +125,8 @@ namespace WCS.Core
             }
         }
 
-
         public abstract List<T> Create();
         public abstract void Do(T obj);
-
         public override List<object> GetObjects()
         {
             return Objects.OfType<object>().ToList();
@@ -142,7 +140,8 @@ namespace WCS.Core
             var types = typeof(T).GetGenericArguments();
             var list= World.Devices.Where(v => types.All(d => v.HasProtocol(d)))
                 .Where(v => Select(v))
-                .Select(v => Activator.CreateInstance(typeof(T), v)).OfType<T>().ToList();
+                .Select(v => Activator.CreateInstance(typeof(T), v)).OfType<T>().ToList();//此时才实例化Protocol
+
             if (list.Count == 0)
             {
                 Log($"{GetType().Name}系统未匹配到任何设备"); 
@@ -150,10 +149,94 @@ namespace WCS.Core
             return list;
         }
 
+        /// <summary>
+        /// 筛选出需要实例化Protocol的Device
+        /// </summary>
+        /// <param name="dev"></param>
+        /// <returns></returns>
         public abstract bool Select(Device dev);
 
         //public abstract List<Device> CreateDevices();
     }
 
-  
+
+    public abstract class ServiceSystem<T, TR> : SystemBase
+    {
+        ConcurrentQueue<Action<List<WorkTimes>>> Actions = new ConcurrentQueue<Action<List<WorkTimes>>>();
+        public TR Invoke(T obj)
+        {
+            var flag = false;
+            TR result = default(TR);
+            Actions.Enqueue(list =>
+            {
+                var sw = new Stopwatch();
+                sw.Start();
+                try
+                { 
+                    result = InvokeDo(obj);
+                }
+                finally
+                {
+                    sw.Stop(); 
+                    list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
+                    flag = true;
+                }
+            });
+            SpinWait.SpinUntil(() => flag);
+            return result;
+        }
+        protected abstract TR Do(T obj);
+        public override List<object> GetObjects()
+        {
+            return new List<object>();
+        }
+
+        public override void Update(List<WorkTimes> list)
+        {
+            var logs = new List<LogInfo>();
+            while (Actions.TryDequeue(out var act))
+            {
+                act(list);
+                logs.AddSafe(Ltc.GetLogInfo());
+            }
+            if (logs.Count > 0)
+            {
+                var arr = logs.GroupBy(v => v.Channel).Select(v => new LogInfo { Channel = v.Key, Level = v.Max(d => d.Level), Type = v.Any(d => d.Type == ErrorType.未知) ? ErrorType.未知 : ErrorType.已知, Message = "\n" + string.Join('\n', v.Select(d => d.Message)) }).ToArray();
+                Configs.OnLog?.Invoke(arr);
+            }
+        }
+         
+        TR InvokeDo(T obj)
+        {
+            var channel = new Channel
+            {
+                World = World.Description,
+                System = Description,
+                Item = obj.ToString()
+            };
+            try
+            {
+                Ltc.SetChannel(channel);
+                Ltc.ClearChannel();
+                Ltc.Log("开始", LogLevel.低, ErrorType.已知);
+                return Do(obj);
+            }
+            catch (KnownException ex)
+            { 
+                Ltc.Log(ex.Message, ex.Level, ErrorType.已知);
+                throw;
+            }
+            catch (Exception ex)
+            {
+                //Console.WriteLine($"{channel}:\n{ex.GetBaseException().Message}");
+                Ltc.Log(ex.GetBaseException().Message, LogLevel.高, ErrorType.未知);
+                throw;
+            }
+            finally
+            {
+                Ltc.Log("结束", LogLevel.低, ErrorType.已知);
+                Ltc.Publish(this.World);
+            }
+        }
+    } 
 }

+ 2 - 40
WCS.Core/World.cs

@@ -237,47 +237,9 @@ namespace WCS.Core
             DoLogics(wt.Items);
             sw.Stop();
             wt.Total = sw.ElapsedMilliseconds;
-            list.AddSafe(wt);
-
-            DoAddtional();
-
-
-        }
-
-        ConcurrentQueue<Action> Actions = new ConcurrentQueue<Action>();
-        void DoAddtional()
-        {
-            while (Actions.TryDequeue(out Action? action))
-            {
-                if (action == null)
-                    throw new Exception("World.DoAddtional未知错误!");
-                action();
-            }
-        }
-
-        internal T _Do<TWorld,T>(Func<TWorld,T> func) where TWorld:World
-        {
-            var flag = false;
-            T result=default(T);
-            Actions.Enqueue(() =>
-            {
-                result = func((TWorld)this);
-                flag = true;
-            });
-            while (!flag)
-                Thread.Sleep(1);
-            return result;
+            list.AddSafe(wt); 
         }
-
-        internal void _Do<TWorld>(Action<TWorld> action) where TWorld : World
-        {
-            _Do<TWorld, int>(w =>
-            {
-                action(w);
-                return 1;
-            });
-        }
-
+         
         void LoadPlcData(List<WorkTimes> list)
         {
             var logs = new List<LogInfo>();