using FreeRedis; using PlcSiemens.Core.Extension; using System.ComponentModel; using System.Diagnostics; using WCS.Core; namespace WCS.WorkEngineering.Systems { // [BelongTo(typeof(MainWorld))] [Description("数据采集系统")] public class 数据采集 : SystemBase { private RedisClient Redis = new RedisClient(""); public 数据采集() { 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 sw = new Stopwatch(); //sw.Start(); //var pack = new DataPack(); //pack.Frame = DateTime.Now; //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); // var arr = Device.All.Where(v => v.HasProtocol(protType)) // .Select(v => // { // try // { // var obj = Activator.CreateInstance(t); // t.GetProperty("Code").SetValue(obj, v.Code); // var value = v.Protocol(protType, World).Copy(entType); // t.GetProperty("Data").SetValue(obj, value); // 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); // } //} //Redis.RPush("Packs", pack); //if (Redis.LLen("Packs") > 50000) //{ // Redis.LTrim("Packs", 5000, -1); //} //sw.Stop(); //list.Add(new WorkTimes { Total = sw.ElapsedMilliseconds, Key = "采集数据" }); } private Type GetProtocolType(Type source) { var t = source.GetInterfaces().FirstOrDefault(v => v.GetInterfaces().Any(d => d.Name == "IProtocol")); return t; } } }