using FreeRedis; using MessagePack; using MessagePack.Resolvers; using NetTaste; using NPOI.SS.Formula.Functions; using OracleInternal.Common; using PlcSiemens.Core.Extension; using ServiceCenter.Redis; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using WCS.Core; using WCS.Entity; using WCS.Entity.Protocol.DataStructure; using WCS.Entity.Protocol.Station; using WCS.WorkEngineering.Extensions; using WCS.WorkEngineering.Worlds; namespace WCS.WorkEngineering.Systems { [BelongTo(typeof(MainWorld))] [Description("数据采集系统")] public class DataCollecctor : SystemBase { public static DataPack Pack; public DataCollecctor() { var gs = Device.All.SelectMany(v => v.Protocols.Select(d=>new { DB=$"{d.Value.DBInfo.No}:{d.Value.DBInfo.PLCInfo.IP}" ,d.Value.Position,TypeStr= d.Key ,Dev=v})) .GroupBy(v => v.DB); foreach (var g in gs) { var min = g.OrderBy(v => v.Position).First(); var max = g.OrderByDescending(v => v.Position).First(); var t = Type.GetType(min.TypeStr); min.Dev.Protocol(t, this.World); max.Dev.Protocol(t, this.World); } } public override List GetObjects() { return new List(); } public override void Update(List list) { var channel = Ltc.GetChannel(); Ltc.SetChannel(null); var sw = new Stopwatch(); sw.Start(); Pack = new DataPack(); Pack.Frame = World.Frame; var ps = Pack.GetType().GetProperties(); foreach (var p in ps) { if (!p.PropertyType.IsArray) continue; var t = p.PropertyType.GetElementType(); if (t.IsGenericType) { var entType = t.GetGenericArguments()[0]; var protType =GetProtocolType( entType); if (protType == null) continue; var arr = Device.All.Where(v => v.HasProtocol(protType)) .Select(v => { try { var protObj = v.Protocol(protType, World) as ProtocolProxyBase; if (protObj.Db.Failed) { return null; } var obj = Activator.CreateInstance(t); t.GetProperty("Code").SetValue(obj, v.Code); var value = WCS.Core.Extentions.Copy(protObj, entType); t.GetProperty("Data").SetValue(obj, value); t.GetProperty("Frame").SetValue(obj, protObj.Frame); return obj; } catch (Exception ex) { return null; } }).Where(v => v != null).ToArray(); var m = typeof(Enumerable).GetMethod("OfType", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public); m = m.MakeGenericMethod(t); var arr2 = m.Invoke(null, new object[] { arr }); m = typeof(Enumerable).GetMethod("ToArray", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public); m = m.MakeGenericMethod(t); var arr3 = m.Invoke(null, new object[] { arr2 }); p.SetValue(Pack, arr3); } } sw.Stop(); list.Add(new WorkTimes { Total = sw.ElapsedMilliseconds, Key = "采集数据" }); Ltc.SetChannel(channel); } Type GetProtocolType(Type source) { var t = source.GetInterfaces() .Where(v => v.GetInterfaces().Any(d => d.Name == "IProtocol")).FirstOrDefault(); return t; } } }