|
@@ -1,21 +1,16 @@
|
|
|
-using System;
|
|
|
-using System.Collections.Concurrent;
|
|
|
-using System.Collections.Generic;
|
|
|
-using System.ComponentModel;
|
|
|
+using System.Collections.Concurrent;
|
|
|
using System.Diagnostics;
|
|
|
-using System.Linq;
|
|
|
using System.Reflection;
|
|
|
-using System.Text;
|
|
|
-using System.Threading.Tasks;
|
|
|
|
|
|
namespace WCS.Core
|
|
|
{
|
|
|
- public abstract class SystemBase: DescriptionClass
|
|
|
+ public abstract class SystemBase : DescriptionClass
|
|
|
{
|
|
|
public World World { get; private set; }
|
|
|
+
|
|
|
public SystemBase()
|
|
|
{
|
|
|
- var attr = this.GetType().GetCustomAttribute<BelongToAttribute>();
|
|
|
+ var attr = this.GetType().GetCustomAttribute<BelongToAttribute>();
|
|
|
if (attr != null)
|
|
|
{
|
|
|
var wt = attr.WorldType;
|
|
@@ -24,16 +19,14 @@ namespace WCS.Core
|
|
|
}
|
|
|
|
|
|
public abstract List<object> GetObjects();
|
|
|
+
|
|
|
public abstract void Update(List<WorkTimes> list);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
public abstract class SystemBase<T> : SystemBase
|
|
|
- {
|
|
|
+ {
|
|
|
public List<T> Objects { get; set; }
|
|
|
-
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 对所有Objects并行循环执行Do
|
|
|
/// </summary>
|
|
@@ -55,12 +48,11 @@ namespace WCS.Core
|
|
|
{
|
|
|
Parallel.ForEach(Objects, new ParallelOptions { MaxDegreeOfParallelism = 256 }, obj =>
|
|
|
{
|
|
|
- var sw= new Stopwatch();
|
|
|
+ var sw = new Stopwatch();
|
|
|
sw.Start();
|
|
|
InvokeDo(obj);
|
|
|
sw.Stop();
|
|
|
list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
|
|
|
-
|
|
|
});
|
|
|
}
|
|
|
else
|
|
@@ -72,13 +64,11 @@ namespace WCS.Core
|
|
|
InvokeDo(obj);
|
|
|
sw.Stop();
|
|
|
list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
- void InvokeDo(T obj)
|
|
|
+
|
|
|
+ private void InvokeDo(T obj)
|
|
|
{
|
|
|
var channel = new Channel
|
|
|
{
|
|
@@ -88,11 +78,15 @@ namespace WCS.Core
|
|
|
Item = obj.ToString()
|
|
|
};
|
|
|
try
|
|
|
- {
|
|
|
+ {
|
|
|
+ if (channel.Item == "1")
|
|
|
+ {
|
|
|
+ var a = 1;
|
|
|
+ }
|
|
|
Ltc.SetChannel(channel);
|
|
|
World.OnInternalLog(channel, "开始");
|
|
|
Do(obj);
|
|
|
- }
|
|
|
+ }
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
World.OnError(channel, ex);
|
|
@@ -105,11 +99,13 @@ namespace WCS.Core
|
|
|
}
|
|
|
|
|
|
public abstract List<T> Create();
|
|
|
+
|
|
|
public abstract void Do(T obj);
|
|
|
+
|
|
|
public override List<object> GetObjects()
|
|
|
{
|
|
|
return Objects.OfType<object>().ToList();
|
|
|
- }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public abstract class DeviceSystem<T> : SystemBase<T> where T : EntityEx<Device>
|
|
@@ -127,9 +123,9 @@ namespace WCS.Core
|
|
|
}
|
|
|
|
|
|
var types = t.GetGenericArguments();
|
|
|
- var list= Device.All.Where(v => types.All(d => v.HasProtocol(d)))
|
|
|
+ var list = Device.All.Where(v => types.All(d => v.HasProtocol(d)))
|
|
|
.Where(v => Select(v))
|
|
|
- .Select(v => Activator.CreateInstance(typeof(T), v,this.World)).OfType<T>().ToList();//此时才实例化Protocol
|
|
|
+ .Select(v => Activator.CreateInstance(typeof(T), v, this.World)).OfType<T>().ToList();//此时才实例化Protocol
|
|
|
if (list.Count == 0)
|
|
|
{
|
|
|
//throw new Exception($"{this.GetType().Name}未匹配到任何Device");
|
|
@@ -147,10 +143,10 @@ namespace WCS.Core
|
|
|
//public abstract List<Device> CreateDevices();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
public abstract class ServiceSystem<T, TR> : SystemBase
|
|
|
{
|
|
|
- ConcurrentQueue<Action<List<WorkTimes>>> Actions = new ConcurrentQueue<Action<List<WorkTimes>>>();
|
|
|
+ private ConcurrentQueue<Action<List<WorkTimes>>> Actions = new ConcurrentQueue<Action<List<WorkTimes>>>();
|
|
|
+
|
|
|
public TR Invoke(T obj)
|
|
|
{
|
|
|
var flag = false;
|
|
@@ -160,12 +156,12 @@ namespace WCS.Core
|
|
|
var sw = new Stopwatch();
|
|
|
sw.Start();
|
|
|
try
|
|
|
- {
|
|
|
+ {
|
|
|
result = InvokeDo(obj);
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
- sw.Stop();
|
|
|
+ sw.Stop();
|
|
|
list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
|
|
|
flag = true;
|
|
|
}
|
|
@@ -173,21 +169,23 @@ namespace WCS.Core
|
|
|
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)
|
|
|
- {
|
|
|
+ {
|
|
|
while (Actions.TryDequeue(out var act))
|
|
|
{
|
|
|
- act(list);
|
|
|
+ act(list);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- TR InvokeDo(T obj)
|
|
|
+
|
|
|
+ private TR InvokeDo(T obj)
|
|
|
{
|
|
|
var channel = new Channel
|
|
|
{
|
|
@@ -198,25 +196,26 @@ namespace WCS.Core
|
|
|
};
|
|
|
try
|
|
|
{
|
|
|
- Ltc.SetChannel(channel);
|
|
|
- World.OnInternalLog(channel,"开始");
|
|
|
+ Ltc.SetChannel(channel);
|
|
|
+ World.OnInternalLog(channel, "开始");
|
|
|
return Do(obj);
|
|
|
- }
|
|
|
+ }
|
|
|
catch (Exception ex)
|
|
|
- {
|
|
|
+ {
|
|
|
throw;
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
- World.OnInternalLog(channel, "结束");
|
|
|
+ World.OnInternalLog(channel, "结束");
|
|
|
World.Publish();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public abstract class ServiceSystem<T> :SystemBase
|
|
|
+ public abstract class ServiceSystem<T> : SystemBase
|
|
|
{
|
|
|
- ConcurrentQueue<Action<List<WorkTimes>>> Actions = new ConcurrentQueue<Action<List<WorkTimes>>>();
|
|
|
+ private ConcurrentQueue<Action<List<WorkTimes>>> Actions = new ConcurrentQueue<Action<List<WorkTimes>>>();
|
|
|
+
|
|
|
public void Invoke(T obj)
|
|
|
{
|
|
|
Actions.Enqueue(list =>
|
|
@@ -230,14 +229,14 @@ namespace WCS.Core
|
|
|
finally
|
|
|
{
|
|
|
sw.Stop();
|
|
|
- list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
|
|
|
+ list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
protected abstract void Do(T obj);
|
|
|
|
|
|
- void InvokeDo(T obj)
|
|
|
+ private void InvokeDo(T obj)
|
|
|
{
|
|
|
var channel = new Channel
|
|
|
{
|