Bläddra i källkod

update 世界外部调用数据

林豪 左 2 år sedan
förälder
incheckning
40e0ce1b81
4 ändrade filer med 74 tillägg och 8 borttagningar
  1. 3 3
      WCS.Core/Configs.cs
  2. 11 2
      WCS.Core/Extentions.cs
  3. 11 0
      WCS.Core/Ltc.cs
  4. 49 3
      WCS.Core/World.cs

+ 3 - 3
WCS.Core/Configs.cs

@@ -6,7 +6,8 @@ namespace WCS.Core
 {
     public static class Configs
     {
-       /// 设置协议通讯代理类的基类,继承至DataObject,IBaseType
+        /// <summary>
+        /// 设置协议通讯代理类的基类,继承至DataObject,IBaseType
         /// </summary>
         public static Type? ProtocolProxyBaseType { get; set; }
 
@@ -23,8 +24,7 @@ namespace WCS.Core
             PublishEvent?.Invoke();
         }
 
-        public static Action<string, string> UploadException      /// <summary>
-    { get; set; }
+        public static Action<string, string> UploadException { get; set; }
 
         public static IPLCAccessorCreater? PLCAccessorCreater { get; set; }
 

+ 11 - 2
WCS.Core/Extentions.cs

@@ -58,14 +58,23 @@ namespace WCS.Core
             ExObjsOfWorld.TryGetValue(source, out var dic);
             var res= dic.Values.OfType<DataBlock>().ToArray();
             return res;
-        }
-
+        } 
 
         public static WorldEx Ex(this World source)
         {
             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)
         {
             lock (source)

+ 11 - 0
WCS.Core/Ltc.cs

@@ -9,6 +9,17 @@ namespace WCS.Core
 {
     public static class Ltc
     {
+        public static T GetWorld<T>() where T : World
+        {
+            return (T)World.Worlds.Where(v => v.GetType() == typeof(T)).First();
+        }
+
+        public static T GetSystem<T>() where T : SystemBase
+        {
+            return (T)World.Worlds.SelectMany(v => v.Systems).Where(v => v.GetType() == typeof(T)).First();
+        }
+
+
         private static ConcurrentDictionary<Thread, Channel> Channels = new ConcurrentDictionary<Thread, Channel>(); 
 
         public static void SetChannel(Channel channel)

+ 49 - 3
WCS.Core/World.cs

@@ -28,7 +28,7 @@ namespace WCS.Core
 
         #region Static
         static List<World> _Worlds;
-        public static List<World> Worlds
+        internal static List<World> Worlds
         {
             get
             {
@@ -43,6 +43,7 @@ namespace WCS.Core
             }
         }
 
+         
         public static void StartAll()
         { 
             //MM_BeginPeriod(1);
@@ -236,7 +237,45 @@ namespace WCS.Core
             DoLogics(wt.Items);
             sw.Stop();
             wt.Total = sw.ElapsedMilliseconds;
-            list.AddSafe(wt); 
+            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;
+        }
+
+        internal void _Do<TWorld>(Action<TWorld> action) where TWorld : World
+        {
+            _Do<TWorld, int>(w =>
+            {
+                action(w);
+                return 1;
+            });
         }
 
         void LoadPlcData(List<WorkTimes> list)
@@ -319,11 +358,18 @@ namespace WCS.Core
          
         public T GetSystem<T>() where T : SystemBase
         {
-            var sys = SystemGroups.SelectMany(v => v.Value).Where(v => v.GetType() == typeof(T)).FirstOrDefault() as T;
+            var sys = Systems.Where(v => v.GetType() == typeof(T)).FirstOrDefault() as T;
             if (sys == null)
                 throw new Exception($"世界{GetType().Name}中不存在系统{typeof(T).Name}");
             return sys;
         }
+
+        public SystemBase[] Systems
+        {
+            get {
+                return SystemGroups.SelectMany(v => v.Value).ToArray();
+            }
+        }
         
     }