林豪 左 2 years ago
parent
commit
ae8dd59e58

+ 1 - 1
ServiceCenter/ServiceCenter.csproj

@@ -12,7 +12,7 @@
     <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.5" />
     <PackageReference Include="PlcSiemens" Version="1.0.0.2" />
     <PackageReference Include="WCS.Core" Version="1.0.0.4" />
-    <PackageReference Include="WCS.Entity" Version="1.0.0.9" />
+    <PackageReference Include="WCS.Entity" Version="1.0.0.10" />
   </ItemGroup>
 
 </Project>

+ 27 - 0
WCS.Core/Device.cs

@@ -93,4 +93,31 @@ namespace WCS.Core
             Data3 = Entity.Protocol<T3>();
         }
     }
+
+    public class Device<T, T2, T3, T4> : Device<T, T2, T3>
+    {
+        public T4 Data4 { get; set; }
+        public Device(Device device) : base(device)
+        {
+            Data4 = Entity.Protocol<T4>();
+        }
+    }
+
+    public class Device<T, T2, T3, T4, T5> : Device<T, T2, T3, T4>
+    {
+        public T5 Data5 { get; set; }
+        public Device(Device device) : base(device)
+        {
+            Data5 = Entity.Protocol<T5>();
+        }
+    }
+
+    public class Device<T, T2, T3, T4, T5, T6> : Device<T, T2, T3, T4, T5>
+    {
+        public T6 Data6 { get; set; }
+        public Device(Device device) : base(device)
+        {
+            Data6 = Entity.Protocol<T6>();
+        }
+    }
 }

+ 17 - 8
WCS.Core/Ltc.cs

@@ -17,7 +17,14 @@ namespace WCS.Core
 
         public static T GetSystem<T>() where T : SystemBase
         {
-            return (T)World.Worlds.SelectMany(v => v.Systems).Where(v => v.GetType() == typeof(T)).First();
+            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");
+            }
         }
 
 
@@ -26,6 +33,7 @@ namespace WCS.Core
         public static void SetChannel(Channel channel)
         {
             Channels[Thread.CurrentThread] = channel;
+            ClearChannel();
         }
 
         public static Channel GetChannel()
@@ -36,7 +44,7 @@ namespace WCS.Core
         static ConcurrentDictionary<Channel, List<LogInfo>> Msgs = new ConcurrentDictionary<Channel, List<LogInfo>>();
 
 
-        public static void ClearChannel()
+        static void ClearChannel()
         {
             if (Msgs.TryGetValue(GetChannel(), out var list))
                 list.Clear();
@@ -201,7 +209,7 @@ namespace WCS.Core
         public ErrorType Type { get; set; }
         public LogLevel Level { get; set; }
         public Channel Channel { get; set; }
-        public string Message { get; set; } = "";
+        public string Message { get; set; }
 
         public override string ToString()
         {
@@ -212,14 +220,15 @@ namespace WCS.Core
 
     public class Channel
     {
-        public string World;
-        public string System;
-        public string Item;
+        public string World = "";
+        public string Stage = "";
+        public string System = "";
+        public string Item = "";
+
 
         public override string ToString()
         {
-            
-            return $"{World}-{System}-{Item}";
+            return $"{World}-{Stage}-{System}-{Item}";
         }
     }
 

+ 14 - 22
WCS.Core/Protocols.cs

@@ -24,6 +24,17 @@ namespace WCS.Core
             return info;
         }
 
+        public static void Add(Type protocolType, string code, ProtocolInfo info)
+        {
+            if (!ItemOfType.TryGetValue(protocolType, out var item))
+            {
+                item = new ConcurrentDictionary<string, ProtocolInfo>();
+                ItemOfType[protocolType] = item;
+            }
+            if (!item.TryAdd(code, info))
+                throw new Exception($"Protocols {protocolType} 添加了重复的设备号");
+        }
+
 
         /// <summary>
         /// 实例化Device,并未实例化Protocol
@@ -36,37 +47,18 @@ namespace WCS.Core
             var gs = ItemOfType.SelectMany(v => v.Value.Select(d => new { code = d.Key, proto = d.Value, type = v.Key })).GroupBy(v=>v.code).ToList();
             foreach (var g in gs)
             {
-                var dev = new Device { Code = g.Key, World = world };
-                //foreach (var i in g)
-                //{
-                //    //dev.Protocol(i.type);
-                //}
+                var dev = new Device { Code = g.Key, World = world }; 
                 list.Add(dev);
-            }
-
+            } 
             return list;
         }
     }
 
     public class Protocols<T> : Protocols
     {
-        static ConcurrentDictionary<string, ProtocolInfo> Items
-        {
-            get
-            {
-                if (!ItemOfType.TryGetValue(typeof(T), out var item))
-                {
-                    item = new ConcurrentDictionary<string, ProtocolInfo>();
-                    ItemOfType[typeof(T)] = item;
-                }
-                return item;
-            }
-        } 
-
         public static void Add(string code, ProtocolInfo info)
         {
-            if (!Items.TryAdd(code, info))
-                throw new Exception($"Protocols<{typeof(T)}>.Add()添加了重复的设备号");
+            Protocols.Add(typeof(T), code, info);
         }
 
 

+ 14 - 27
WCS.Core/System.cs

@@ -15,26 +15,17 @@ namespace WCS.Core
         public World World { get; private set; }
         public SystemBase()
         {
-            var attr = this.GetType().GetCustomAttribute<BelongToAttribute>();
-            var wt = typeof(World);
+            var attr = this.GetType().GetCustomAttribute<BelongToAttribute>(); 
             if (attr != null)
             {
-                wt = attr.WorldType;
+                var 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();
         public abstract void Update(List<WorkTimes> list);
-
-        public void Log(string msg)
-        { 
-            Ltc.Log(msg, LogLevel.Low, ErrorType.Kown);
-        } 
-        public void Error(string msg,LogLevel level)
-        {
-            throw new KnownException(msg, level);
-        }
+         
     }
 
    
@@ -89,8 +80,8 @@ namespace WCS.Core
             }
             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(arr);
+                //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);
             }
         }
          
@@ -99,13 +90,13 @@ namespace WCS.Core
             var channel = new Channel
             {
                 World = World.Description,
+                Stage = "DoLogics",
                 System = Description,
                 Item = obj.ToString()
             };
             try
             { 
-                Ltc.SetChannel(channel);
-                Ltc.ClearChannel();
+                Ltc.SetChannel(channel); 
                 Ltc.Log("开始", LogLevel.Low, ErrorType.Kown);
                 Do(obj);
             }
@@ -117,6 +108,7 @@ namespace WCS.Core
             {
                 //Console.WriteLine($"{channel}:\n{ex.GetBaseException().Message}");
                 Ltc.Log(ex.GetBaseException().Message, LogLevel.High, ErrorType.Unkown);
+                World.OnError(channel, ex);
             }
             finally
             {
@@ -140,12 +132,7 @@ 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();//此时才实例化Protocol
-
-            if (list.Count == 0)
-            {
-                Log($"{GetType().Name}系统未匹配到任何设备"); 
-            }
+                .Select(v => Activator.CreateInstance(typeof(T), v)).OfType<T>().ToList();//此时才实例化Protocol            
             return list;
         }
 
@@ -201,8 +188,8 @@ namespace WCS.Core
             }
             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(arr);
+                //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);
             }
         }
          
@@ -211,13 +198,13 @@ namespace WCS.Core
             var channel = new Channel
             {
                 World = World.Description,
+                Stage = "DoLogics",
                 System = Description,
                 Item = obj.ToString()
             };
             try
             {
-                Ltc.SetChannel(channel);
-                Ltc.ClearChannel();
+                Ltc.SetChannel(channel); 
                 Ltc.Log("开始", LogLevel.Low, ErrorType.Kown);
                 return Do(obj);
             }

+ 48 - 20
WCS.Core/World.cs

@@ -19,7 +19,7 @@ namespace WCS.Core
     /// 世界用来管理下属System的执行周期,此为默认世界。也可以通过继承此类创建多个不同世界,不同世界的执行周期相互独立,不受其它世界延迟干扰。
     /// </summary>
     [Description("默认世界")]
-    public class World: DescriptionClass
+    public abstract class World: DescriptionClass
     {
         [DllImport("winmm.dll", EntryPoint = "timeBeginPeriod")]
         public static extern uint MM_BeginPeriod(uint uMilliseconds);
@@ -35,7 +35,7 @@ namespace WCS.Core
                 if (_Worlds == null)
                 {
                     _Worlds = new List<World>();
-                    _Worlds.Add(new World());//默认世界
+                    //_Worlds.Add(new World());//默认世界
                     var arr = Assembly.GetEntryAssembly().GetTypes().Where(v => typeof(World).IsAssignableFrom(v)).Select(v => Activator.CreateInstance(v)).OfType<World>().ToArray();//自定义世界
                     _Worlds.AddRange(arr);
                 }
@@ -89,7 +89,10 @@ namespace WCS.Core
         /// <summary>
         /// 周期最小间隔时间(毫秒)
         /// </summary>
-        protected int Interval = 800; 
+        protected abstract int Interval {
+            get;
+        }
+
         public World()
         {
             SystemTypes = GetSystemTypes(); 
@@ -121,13 +124,14 @@ namespace WCS.Core
         /// </summary>
         public virtual void Init()
         {
-            Ltc.SetChannel(new Channel { World = Description, Item = "初始化" });
-            Ltc.ClearChannel();
+         
             try
             {
                 Devices = Protocols.Generate(this);
                 foreach (var type in SystemTypes)
                 {
+                    var sysDesc = type.GetCustomAttribute<DescriptionAttribute>()?.Description;
+                    Ltc.SetChannel(new Channel { World = Description, Stage = "Init", System = sysDesc ?? type.Name, Item = "排序" });
                     Set(type, 0);
                 }
                 var arr = TypeOrder.OrderBy(v => v.Value).Select(v => v.Key).ToArray();
@@ -136,7 +140,12 @@ namespace WCS.Core
                 {
                     var g = gs[i];
                     var list = new List<SystemBase>();
-                    var sysArr = g.Select(v => Activator.CreateInstance(v.Key)).OfType<SystemBase>().ToArray();
+                    var sysArr = g.Select(v =>
+                    {
+                        var sysDesc = v.Key.GetCustomAttribute<DescriptionAttribute>()?.Description;
+                        Ltc.SetChannel(new Channel { World = Description, Stage="Init", System = sysDesc ?? v.Key.Name, Item = "构造" });
+                        return Activator.CreateInstance(v.Key);
+                    }).OfType<SystemBase>().ToArray();
                     list.AddRange(sysArr);
                     SystemGroups.Add(i, list);
                 }
@@ -189,7 +198,7 @@ namespace WCS.Core
             while (!Stoped)
             {
                 WorkTimes wt = new WorkTimes();
-                wt.Key = this.GetType().Name;
+                wt.Key = $"{this.GetType().Name} 周期:{Interval}";
                 sw.Restart();
                 BeforeUpdate();
                 Update(wt.Items);
@@ -197,21 +206,15 @@ namespace WCS.Core
                 sw.Stop();
                 var workTimes = (int)sw.ElapsedMilliseconds;
                 var ms = Interval - workTimes;
-                sw.Start();
+                //sw.Start();
                 if (ms > 0)
                 {
                     Thread.Sleep(ms);//不要使用Task.Delay().Wait()      
                 }
-                else
-                {
-                    Console.ForegroundColor = ConsoleColor.Red;
-                }
-                sw.Stop();
-                var total = sw.ElapsedMilliseconds;
-                wt.Total = total; 
-                var aa = wt.GetInfo();
-                Console.WriteLine(aa); 
-                Console.ResetColor(); 
+                //sw.Stop();
+                //var total = sw.ElapsedMilliseconds;
+                wt.Total = workTimes;
+                FrameInfo(wt);
             } 
         }
 
@@ -248,7 +251,8 @@ namespace WCS.Core
                 var channel = new Channel
                 {
                     World = GetType().Name,
-                    System = "读取PLC",
+                    Stage = "LoadPlcData",
+                    System = "",
                     Item = $"{db.Entity.PLCInfo.IP}_{db.Entity.No}"
                 };
                 var sw = new Stopwatch();
@@ -265,7 +269,7 @@ namespace WCS.Core
                 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);
+                    this.Ex().Publish(channel, ex.GetBaseException().Message); 
                 }
                 sw.Stop();
                 list.AddSafe(new WorkTimes { Key = $"{db.Entity.PLCInfo.IP}/{db.Entity.No}", Total = sw.ElapsedMilliseconds }); 
@@ -332,6 +336,30 @@ namespace WCS.Core
                 return SystemGroups.SelectMany(v => v.Value).ToArray();
             }
         }
+         
+        protected virtual void FrameInfo(WorkTimes wt)
+        {
+            if (wt.Total > this.Interval)
+            {
+                Console.ForegroundColor = ConsoleColor.Red; 
+            }
+            Console.WriteLine(wt.GetInfo());
+            Console.ResetColor();
+        }
+
+        public void Log<T>(T log) where T : ILog
+        {
+            OnLog(Ltc.GetChannel(), log);
+        }
+
+        internal protected abstract void OnError(Channel channel, Exception exception);
+
+        protected abstract void OnLog(Channel channel, object logObj);
+
+    }
+
+    public interface ILog
+    { 
         
     }
 

+ 1 - 1
WCS.Entity.Protocol/WCS.Entity.Protocol.csproj

@@ -7,7 +7,7 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="WCS.Entity" Version="1.0.0.9" />
+    <PackageReference Include="WCS.Entity" Version="1.0.0.10" />
   </ItemGroup>
 
 </Project>

+ 6 - 0
WCS.Entity/WCS_TASK.cs

@@ -39,6 +39,12 @@ namespace WCS.Entity
         [SugarColumn(ColumnDescription = "目标地址", Length = 20)]
         public string ADDRTO { get; set; }
 
+        /// <summary>
+        /// 最后一个交互点
+        /// </summary>
+        [SugarColumn(ColumnDescription = "最后一个交互点", Length = 20)]
+        public string LastInteractionPoint { get; set; }
+
         /// <summary>
         /// 下一个地址
         /// </summary>

+ 6 - 0
WCS.Entity/WCS_TASK_OLD.cs

@@ -38,6 +38,12 @@ namespace WCS.Entity.Protocol
         [SugarColumn(ColumnDescription = "目标地址", Length = 20)]
         public string ADDRTO { get; set; }
 
+        /// <summary>
+        /// 最后一个交互点
+        /// </summary>
+        [SugarColumn(ColumnDescription = "最后一个交互点", Length = 20)]
+        public string LastInteractionPoint { get; set; }
+
         /// <summary>
         /// 下一个地址
         /// </summary>

+ 1 - 0
WCS.Service/Worker.cs

@@ -95,6 +95,7 @@ namespace WCS.Service
                             db.Default.CodeFirst.InitTables(typeof(WCS_PathDtl));
                             db.Default.CodeFirst.InitTables(typeof(WCS_ROUTE));
                             db.Default.CodeFirst.InitTables(typeof(WCS_TASK));
+                            db.Default.CodeFirst.InitTables(typeof(WCS_TASK_DTL));
                             db.Default.CodeFirst.InitTables(typeof(WCS_TASK_OLD));
                             db.Default.CodeFirst.InitTables(typeof(WCS_PlcData));
                             db.Default.CodeFirst.InitTables(typeof(WCS_AGVTask));

+ 1 - 1
WCS.WorkEngineering/WCS.WorkEngineering.csproj

@@ -7,7 +7,7 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="ServiceCenter" Version="1.0.0.15" />
+    <PackageReference Include="ServiceCenter" Version="1.0.0.16" />
   </ItemGroup>
 
   <ItemGroup>