|
@@ -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
|
|
|
+ {
|
|
|
|
|
|
}
|
|
|
|