林豪 左 il y a 1 an
Parent
commit
8f423721ff
5 fichiers modifiés avec 261 ajouts et 296 suppressions
  1. 4 15
      WCS.Core/Configs.cs
  2. 201 219
      WCS.Core/Ltc.cs
  3. 5 2
      WCS.Core/ProtocolProxyBase.cs
  4. 16 43
      WCS.Core/System.cs
  5. 35 17
      WCS.Core/World.cs

+ 4 - 15
WCS.Core/Configs.cs

@@ -9,25 +9,14 @@ namespace WCS.Core
         /// <summary>
         /// 设置协议通讯代理类的基类,继承至DataObject,IBaseType
         /// </summary>
-        public static Type? ProtocolProxyBaseType { get; set; }
+        public static Type? ProtocolProxyBaseType { get; set; } 
 
+        public static Encoding StringEncoding { get; set; } 
 
-        public static Encoding StringEncoding { get; set; }
-
-
-        public static string DebugRedisUrl { get; set; }
-
-        public static event Action PublishEvent;
-
-        internal static void Publish()
-        {
-            PublishEvent?.Invoke();
-        }
-
-        public static Action<string, string> UploadException { get; set; }
+        public static string DebugRedisUrl { get; set; }  
 
         public static IPLCAccessorCreater? PLCAccessorCreater { get; set; }
 
-        public static Action<IEnumerable<LogInfo>> OnLog;
+        
     }
 }

+ 201 - 219
WCS.Core/Ltc.cs

@@ -8,32 +8,14 @@ using System.Threading.Channels;
 
 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
-        {
-            try
-            {
-                return (T)World.Worlds.SelectMany(v => v.Systems).Where(v => v.GetType() == typeof(T)).First();
-            }
-            catch (Exception ex)
-            {
-                throw new Exception($"系统:{typeof(T).Name}未设置BelongToAttribute");
-            }
-        }
-
-
+    internal static class Ltc
+    {  
         private static ConcurrentDictionary<Thread, Channel> Channels = new ConcurrentDictionary<Thread, Channel>(); 
 
         public static void SetChannel(Channel channel)
         {
             Channels[Thread.CurrentThread] = channel;
-            ClearChannel();
+            //ClearChannel();
         }
 
         public static Channel GetChannel()
@@ -41,182 +23,182 @@ namespace WCS.Core
             return Channels[Thread.CurrentThread];
         }
 
-        static ConcurrentDictionary<Channel, List<LogInfo>> Msgs = new ConcurrentDictionary<Channel, List<LogInfo>>();
-
-
-        static void ClearChannel()
-        {
-            if (Msgs.TryGetValue(GetChannel(), out var list))
-                list.Clear();
-        }
-
-        public static string GetLogStr()
-        {
-            var channel = GetChannel();
-            if (Msgs.TryGetValue(channel, out var list))
-            {
-                var msg = "-------------------"+channel + "--------------------\n" + string.Join('\n', list.Select(v => $"{string.Join('\n', v)}"));
-                return msg;
-            }
-            else
-            {
-                return "";
-            } 
-        }
-
-        public static List<LogInfo> GetLogInfo()
-        {
-            var channel = GetChannel();
-            if (Msgs.TryGetValue(channel, out var list))
-            {
-                return list;
-            }
-            return new List<LogInfo>();
-        }
-
-        public static void Log(string msg,LogLevel level,ErrorType type)
-        { 
-            var channel = GetChannel();
+        //static ConcurrentDictionary<Channel, List<LogInfo>> Msgs = new ConcurrentDictionary<Channel, List<LogInfo>>();
+
+
+        //static void ClearChannel()
+        //{
+        //    if (Msgs.TryGetValue(GetChannel(), out var list))
+        //        list.Clear();
+        //}
+
+        //public static string GetLogStr()
+        //{
+        //    var channel = GetChannel();
+        //    if (Msgs.TryGetValue(channel, out var list))
+        //    {
+        //        var msg = "-------------------"+channel + "--------------------\n" + string.Join('\n', list.Select(v => $"{string.Join('\n', v)}"));
+        //        return msg;
+        //    }
+        //    else
+        //    {
+        //        return "";
+        //    } 
+        //}
+
+        //public static List<LogInfo> GetLogInfo()
+        //{
+        //    var channel = GetChannel();
+        //    if (Msgs.TryGetValue(channel, out var list))
+        //    {
+        //        return list;
+        //    }
+        //    return new List<LogInfo>();
+        //}
+
+        //public static void Log(string msg,LogLevel level,ErrorType type)
+        //{ 
+        //    var channel = GetChannel();
             
-            if (!Msgs.TryGetValue(channel, out var list))
-            {
-                list = new List<LogInfo>();
-                Msgs[channel] = list;
-            }
-            list.Add(new LogInfo { Message = msg, Level = level, Type = type, Channel = channel });
-        }
+        //    if (!Msgs.TryGetValue(channel, out var list))
+        //    {
+        //        list = new List<LogInfo>();
+        //        Msgs[channel] = list;
+        //    }
+        //    list.Add(new LogInfo { Message = msg, Level = level, Type = type, Channel = channel });
+        //}
   
-        public static void Publish(World world)
-        { 
-            var channel = GetChannel();
-            if (Msgs.TryGetValue(channel, out var list))
-            {
-                var msg = string.Join('\n', list); 
-                world.Ex().Publish(channel, msg); 
-            } 
-        } 
-
-        private static string ResultString<T>(T obj)
-        {
-            if (obj == null)
-            {
-                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() + "元素";
-            }
-
-            return obj.ToString();
-        }
-
-        public static T Do<T>(Expression<Func<T>> exp)
-        {
-            var msg = exp.ExpToString();
-            msg += "  结果:";
-            try
-            {
-                var res = exp.Compile().Invoke();
-                msg += res;
-                return res;
-            }
-            catch (Exception ex)
-            { 
-                throw;
-            }
-            finally
-            {
-                Log(msg, LogLevel.Low, ErrorType.Kown);
-            }
-        }
-
-        public static T Do<T1, T>(T1 obj, Expression<Func<T1, T>> exp)
-        {
-            var msg = "";
-            try
-            {
-                try
-                {
-                    msg = exp.ExpToString();
-                }
-                catch (Exception ex2)
-                {
-                }
-                msg += "  结果:";
-                var res = exp.Compile().Invoke(obj);
-                msg += ResultString(res);
-                return res;
-            }
-            catch (Exception ex)
-            {  
-                throw;
-            }
-            finally
-            {
-                Log(msg, LogLevel.Low, ErrorType.Kown);
-            }
-        }
-
-        public static T Do<T1, T2, T>(T1 obj, T2 obj2, Expression<Func<T1, T2, T>> exp)
-        {
-            var msg = exp.ExpToString();
-            msg += "  结果:";
-            try
-            {
-                var res = exp.Compile().Invoke(obj, obj2);
-                msg += ResultString(res);
-                return res;
-            }
-            catch (Exception ex)
-            { 
-                throw;
-            }
-            finally
-            {
-                Log(msg, LogLevel.Low, ErrorType.Kown);
-            }
-        }
-
-        public static T Do<T1, T2, T3, T>(T1 obj, T2 obj2, T3 obj3, Expression<Func<T1, T2, T3, T>> exp)
-        {
-            var msg = exp.ExpToString();
-            msg += "  结果:";
-            try
-            {
-                var res = exp.Compile().Invoke(obj, obj2, obj3);
-                msg += ResultString(res);
-                return res;
-            }
-            catch (Exception ex)
-            { 
-                throw;
-            }
-            finally
-            {
-                Log(msg, LogLevel.Low, ErrorType.Kown);
-            }
-        }
+        //public static void Publish(World world)
+        //{ 
+        //    var channel = GetChannel();
+        //    if (Msgs.TryGetValue(channel, out var list))
+        //    {
+        //        var msg = string.Join('\n', list); 
+        //        world.Ex().Publish(channel, msg); 
+        //    } 
+        //} 
+
+        //private static string ResultString<T>(T obj)
+        //{
+        //    if (obj == null)
+        //    {
+        //        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() + "元素";
+        //    }
+
+        //    return obj.ToString();
+        //}
+
+        //public static T Do<T>(Expression<Func<T>> exp)
+        //{
+        //    var msg = exp.ExpToString();
+        //    msg += "  结果:";
+        //    try
+        //    {
+        //        var res = exp.Compile().Invoke();
+        //        msg += res;
+        //        return res;
+        //    }
+        //    catch (Exception ex)
+        //    { 
+        //        throw;
+        //    }
+        //    finally
+        //    {
+        //        Log(msg, LogLevel.Low, ErrorType.Kown);
+        //    }
+        //}
+
+        //public static T Do<T1, T>(T1 obj, Expression<Func<T1, T>> exp)
+        //{
+        //    var msg = "";
+        //    try
+        //    {
+        //        try
+        //        {
+        //            msg = exp.ExpToString();
+        //        }
+        //        catch (Exception ex2)
+        //        {
+        //        }
+        //        msg += "  结果:";
+        //        var res = exp.Compile().Invoke(obj);
+        //        msg += ResultString(res);
+        //        return res;
+        //    }
+        //    catch (Exception ex)
+        //    {  
+        //        throw;
+        //    }
+        //    finally
+        //    {
+        //        Log(msg, LogLevel.Low, ErrorType.Kown);
+        //    }
+        //}
+
+        //public static T Do<T1, T2, T>(T1 obj, T2 obj2, Expression<Func<T1, T2, T>> exp)
+        //{
+        //    var msg = exp.ExpToString();
+        //    msg += "  结果:";
+        //    try
+        //    {
+        //        var res = exp.Compile().Invoke(obj, obj2);
+        //        msg += ResultString(res);
+        //        return res;
+        //    }
+        //    catch (Exception ex)
+        //    { 
+        //        throw;
+        //    }
+        //    finally
+        //    {
+        //        Log(msg, LogLevel.Low, ErrorType.Kown);
+        //    }
+        //}
+
+        //public static T Do<T1, T2, T3, T>(T1 obj, T2 obj2, T3 obj3, Expression<Func<T1, T2, T3, T>> exp)
+        //{
+        //    var msg = exp.ExpToString();
+        //    msg += "  结果:";
+        //    try
+        //    {
+        //        var res = exp.Compile().Invoke(obj, obj2, obj3);
+        //        msg += ResultString(res);
+        //        return res;
+        //    }
+        //    catch (Exception ex)
+        //    { 
+        //        throw;
+        //    }
+        //    finally
+        //    {
+        //        Log(msg, LogLevel.Low, ErrorType.Kown);
+        //    }
+        //}
     }
 
-    public class LogInfo
-    {
-        public ErrorType Type { get; set; }
-        public LogLevel Level { get; set; }
-        public Channel Channel { get; set; }
-        public string Message { get; set; }
+    //public class LogInfo
+    //{
+    //    public ErrorType Type { get; set; }
+    //    public LogLevel Level { get; set; }
+    //    public Channel Channel { get; set; }
+    //    public string Message { get; set; }
 
-        public override string ToString()
-        {
-            //var a = ErrorType.Unkown;
-            return $"类型:{Type.Description()},级别:{Level.Description()},内容:{Message}";
-        }
-    }
+    //    public override string ToString()
+    //    {
+    //        //var a = ErrorType.Unkown;
+    //        return $"类型:{Type.Description()},级别:{Level.Description()},内容:{Message}";
+    //    }
+    //}
 
     public class Channel
     {
@@ -232,31 +214,31 @@ namespace WCS.Core
         }
     }
 
-    public enum ErrorType
-    {
-        [Description("未知")]
-        Unkown = 0,
-        [Description("已知")]
-        Kown = 1
-    }
-
-    public enum LogLevel
-    {
-        [Description("低")]
-        Low = 0,
-        [Description("中")]
-        Mid = 1,
-        [Description("高")]
-        High = 2
-    }
-
-    public class KnownException : Exception
-    { 
-        public LogLevel Level { get; set; }
-
-        public KnownException(string msg, LogLevel level) : base(msg)
-        {
-            this.Level = level;
-        }
-    }
+    //public enum ErrorType
+    //{
+    //    [Description("未知")]
+    //    Unkown = 0,
+    //    [Description("已知")]
+    //    Kown = 1
+    //}
+
+    //public enum LogLevel
+    //{
+    //    [Description("低")]
+    //    Low = 0,
+    //    [Description("中")]
+    //    Mid = 1,
+    //    [Description("高")]
+    //    High = 2
+    //}
+
+    //public class KnownException : Exception
+    //{ 
+    //    public LogLevel Level { get; set; }
+
+    //    public KnownException(string msg, LogLevel level) : base(msg)
+    //    {
+    //        this.Level = level;
+    //    }
+    //}
 }

+ 5 - 2
WCS.Core/ProtocolProxyBase.cs

@@ -7,6 +7,7 @@ using System.Net.Sockets;
 using System.Reflection;
 using System.Runtime.InteropServices;
 using System.Text;
+using System.Threading.Channels;
 using System.Threading.Tasks;
 
 namespace WCS.Core
@@ -135,7 +136,8 @@ namespace WCS.Core
         {
             var item = Items[propertyName] as PlcItem<T>;
             var res = item.Value;
-            Ltc.Log($"{propertyName}:{res}", LogLevel.Low, ErrorType.Kown);
+            var channel = Ltc.GetChannel();
+            Device.World.OnInternalLog(channel, $"{propertyName}:{res}");
             return res;
         }
 
@@ -143,7 +145,8 @@ namespace WCS.Core
         {
             var item = Items[propertyName] as PlcItem<T>;
             item.Value = value;
-            Ltc.Log($"{propertyName}={value}", LogLevel.Low, ErrorType.Kown);
+            var channel = Ltc.GetChannel();
+            Device.World.OnInternalLog(channel, $"{propertyName}={value}");
         }
         #endregion
     }

+ 16 - 43
WCS.Core/System.cs

@@ -51,7 +51,6 @@ namespace WCS.Core
 
         public override void Update(List<WorkTimes> list)
         {
-            var logs = new List<LogInfo>();
             if (ParallelDo)
             {
                 Parallel.ForEach(Objects, new ParallelOptions { MaxDegreeOfParallelism = 256 }, obj =>
@@ -61,8 +60,7 @@ namespace WCS.Core
                     InvokeDo(obj);
                     sw.Stop();
                     list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
-                    //var log = Ltc.GetLogStr();
-                    logs.AddSafe(Ltc.GetLogInfo());
+
                 });
             }
             else
@@ -74,15 +72,10 @@ namespace WCS.Core
                     InvokeDo(obj);
                     sw.Stop();
                     list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
-                    //var log = Ltc.GetLogStr();
-                    logs.AddSafe(Ltc.GetLogInfo());
+
                 }
             }
-            if (SaveLogsToFile)
-            { 
-                //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.Unkown) ? ErrorType.Unkown : ErrorType.Kown, Message = "\n"+string.Join('\n', v.Select(d => d.Message)) }).ToArray(); 
-                Configs.OnLog?.Invoke(logs);
-            }
+         
         }
          
         void InvokeDo(T obj)
@@ -96,24 +89,18 @@ namespace WCS.Core
             };
             try
             { 
-                Ltc.SetChannel(channel); 
-                Ltc.Log("开始", LogLevel.Low, ErrorType.Kown);
+                Ltc.SetChannel(channel);
+                World.OnInternalLog(channel, "开始");
                 Do(obj);
-            }
-            catch (KnownException ex)
-            {
-                Ltc.Log(ex.Message, ex.Level, ErrorType.Kown);
-            }
+            } 
             catch (Exception ex)
             {
-                //Console.WriteLine($"{channel}:\n{ex.GetBaseException().Message}");
-                Ltc.Log(ex.GetBaseException().Message, LogLevel.High, ErrorType.Unkown);
                 World.OnError(channel, ex);
             }
             finally
             {
-                Ltc.Log("结束", LogLevel.Low, ErrorType.Kown);
-                Ltc.Publish(this.World);
+                World.OnInternalLog(channel, "结束");
+                World.Publish();
             }
         }
 
@@ -179,17 +166,10 @@ namespace WCS.Core
         }
 
         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.Unkown) ? ErrorType.Unkown : ErrorType.Kown, Message = "\n" + string.Join('\n', v.Select(d => d.Message)) }).ToArray();
-                Configs.OnLog?.Invoke(logs);
+                act(list); 
             }
         }
          
@@ -204,25 +184,18 @@ namespace WCS.Core
             };
             try
             {
-                Ltc.SetChannel(channel); 
-                Ltc.Log("开始", LogLevel.Low, ErrorType.Kown);
+                Ltc.SetChannel(channel);  
+                World.OnInternalLog(channel,"开始");
                 return Do(obj);
-            }
-            catch (KnownException ex)
-            { 
-                Ltc.Log(ex.Message, ex.Level, ErrorType.Kown);
-                throw;
-            }
+            } 
             catch (Exception ex)
-            {
-                //Console.WriteLine($"{channel}:\n{ex.GetBaseException().Message}");
-                Ltc.Log(ex.GetBaseException().Message, LogLevel.High, ErrorType.Unkown);
+            { 
                 throw;
             }
             finally
             {
-                Ltc.Log("结束", LogLevel.Low, ErrorType.Kown);
-                Ltc.Publish(this.World);
+                World.OnInternalLog(channel, "结束"); 
+                World.Publish();
             }
         }
     } 

+ 35 - 17
WCS.Core/World.cs

@@ -43,7 +43,23 @@ namespace WCS.Core
             }
         }
 
-         
+        public static T GetWorldInstance<T>() where T : World
+        {
+            return (T)Worlds.Where(v => v.GetType() == typeof(T)).First();
+        }
+
+        public static T GetSystemInstance<T>() where T : SystemBase
+        {
+            try
+            {
+                return (T)World.Worlds.SelectMany(v => v.Systems).Where(v => v.GetType() == typeof(T)).First();
+            }
+            catch (Exception ex)
+            {
+                throw new Exception($"系统:{typeof(T).Name}未设置BelongToAttribute");
+            }
+        }
+
         public static void StartAll()
         { 
             //MM_BeginPeriod(1);
@@ -151,13 +167,11 @@ namespace WCS.Core
                 }
             }
             catch (Exception ex)
-            {
-                Ltc.Log(ex.GetBaseException().Message, LogLevel.High, ErrorType.Unkown);
+            { 
                 throw;
             }
             finally 
-            {
-                Configs.OnLog?.Invoke(Ltc.GetLogInfo());
+            { 
             }
         }
 
@@ -244,8 +258,7 @@ namespace WCS.Core
         }
          
         void LoadPlcData(List<WorkTimes> list)
-        {
-            var logs = new List<LogInfo>();
+        { 
             Parallel.ForEach(this.GetDataBlocks(), db =>
             {
                 var channel = new Channel
@@ -260,21 +273,14 @@ namespace WCS.Core
                 try
                 {
                     db.RefreshData();
-                }
-                catch (KnownException ex)
-                {
-                    logs.AddSafe(new LogInfo { Channel = channel, Message = $"{ex.GetBaseException().Message}", Level = ex.Level, Type = ErrorType.Kown });
-                    this.Ex().Publish(channel, ex.GetBaseException().Message);
-                }
+                } 
                 catch (Exception ex)
-                {
-                    logs.AddSafe(new LogInfo { Channel = channel, Message = $"{ex.GetBaseException().Message}", Level = LogLevel.High, Type = ErrorType.Unkown });
+                { 
                     this.Ex().Publish(channel, ex.GetBaseException().Message); 
                 }
                 sw.Stop();
                 list.AddSafe(new WorkTimes { Key = $"{db.Entity.PLCInfo.IP}/{db.Entity.No}", Total = sw.ElapsedMilliseconds }); 
-            }); 
-            Configs.OnLog?.Invoke(logs);
+            });  
         }
 
         void DoLogics(List<WorkTimes> list)
@@ -353,9 +359,21 @@ namespace WCS.Core
         }
 
         internal protected abstract void OnError(Channel channel, Exception exception);
+        internal protected abstract void OnInternalLog(Channel channel, string msg);
 
         protected abstract void OnLog(Channel channel, object logObj);
 
+        
+
+        protected abstract IEnumerable<string> GetChannelMsg(Channel channel);
+
+        internal void Publish()
+        {
+            var channel = Ltc.GetChannel();
+            var msgs = GetChannelMsg(channel);
+            var msg = string.Join("\n", msgs);
+            this.Ex().Publish(channel,msg);
+        }
     }
 
     public interface ILog