| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.ComponentModel;
- 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 World World { get; private set; }
- public SystemBase()
- {
- var attr = this.GetType().GetCustomAttribute<BelongToAttribute>();
- var wt = typeof(World);
- if (attr != null)
- {
- wt = attr.WorldType;
- }
- 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.低, ErrorType.已知);
- }
- public void Error(string msg,LogLevel level)
- {
- throw new KnownException(msg, level);
- }
- }
-
- public abstract class SystemBase<T> : SystemBase
- {
- public List<T> Objects { get; set; }
-
- /// <summary>
- /// 对所有Objects并行循环执行Do
- /// </summary>
- protected abstract bool ParallelDo { get; }
- /// <summary>
- /// 保存日志到文件
- /// </summary>
- protected abstract bool SaveLogsToFile { get; }
- public SystemBase()
- {
- Objects = Create();//.Select(v=>Activator.CreateInstance(typeof(T),v)).OfType<T>().ToList();
- }
- public override void Update(List<WorkTimes> list)
- {
- var logs = new List<LogInfo>();
- if (ParallelDo)
- {
- Parallel.ForEach(Objects, new ParallelOptions { MaxDegreeOfParallelism = 256 }, obj =>
- {
- var sw= new Stopwatch();
- sw.Start();
- InvokeDo(obj);
- sw.Stop();
- list.AddSafe(new WorkTimes { Key = $"{obj?.ToString()}", Total = sw.ElapsedMilliseconds });
- //var log = Ltc.GetLogStr();
- logs.AddSafe(Ltc.GetLogInfo());
- });
- }
- else
- {
- foreach (var obj in Objects)
- {
- var sw = new Stopwatch();
- sw.Start();
- 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.未知) ? ErrorType.未知 : ErrorType.已知, Message = "\n"+string.Join('\n', v.Select(d => d.Message)) }).ToArray();
- Configs.OnLog?.Invoke(arr);
- }
- }
-
- void InvokeDo(T obj)
- {
- var channel = new Channel
- {
- World = World.Description,
- System = Description,
- Item = obj.ToString()
- };
- try
- {
- Ltc.SetChannel(channel);
- Ltc.ClearChannel();
- Ltc.Log("开始", LogLevel.低, ErrorType.已知);
- Do(obj);
- }
- catch (KnownException ex)
- {
- Ltc.Log(ex.Message, ex.Level, ErrorType.已知);
- }
- catch (Exception ex)
- {
- //Console.WriteLine($"{channel}:\n{ex.GetBaseException().Message}");
- Ltc.Log(ex.GetBaseException().Message, LogLevel.高, ErrorType.未知);
- }
- finally
- {
- Ltc.Log("结束", LogLevel.低, ErrorType.已知);
- Ltc.Publish(this.World);
- }
- }
- 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>
- {
- public override List<T> Create()
- {
- 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();
- if (list.Count == 0)
- {
- Log($"{GetType().Name}系统未匹配到任何设备");
- }
- return list;
- }
- public abstract bool Select(Device dev);
- //public abstract List<Device> CreateDevices();
- }
-
- }
|