数据采集.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using FreeRedis;
  2. using MessagePack;
  3. using MessagePack.Resolvers;
  4. using NetTaste;
  5. using NPOI.SS.Formula.Functions;
  6. using OracleInternal.Common;
  7. using PlcSiemens.Core.Extension;
  8. using ServiceCenter.Redis;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Diagnostics;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using WCS.Core;
  17. using WCS.Entity;
  18. using WCS.Entity.Protocol.DataStructure;
  19. using WCS.Entity.Protocol.Station;
  20. using WCS.WorkEngineering.Extensions;
  21. using WCS.WorkEngineering.Worlds;
  22. namespace WCS.WorkEngineering.Systems
  23. {
  24. [BelongTo(typeof(MainWorld))]
  25. [Description("数据采集系统")]
  26. public class DataCollecctor : SystemBase
  27. {
  28. public static DataPack Pack;
  29. public DataCollecctor()
  30. {
  31. 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}))
  32. .GroupBy(v => v.DB);
  33. foreach (var g in gs)
  34. {
  35. var min = g.OrderBy(v => v.Position).First();
  36. var max = g.OrderByDescending(v => v.Position).First();
  37. var t = Type.GetType(min.TypeStr);
  38. min.Dev.Protocol(t, this.World);
  39. max.Dev.Protocol(t, this.World);
  40. }
  41. }
  42. public override List<object> GetObjects()
  43. {
  44. return new List<object>();
  45. }
  46. public override void Update(List<WorkTimes> list)
  47. {
  48. var channel = Ltc.GetChannel();
  49. Ltc.SetChannel(null);
  50. var sw = new Stopwatch();
  51. sw.Start();
  52. Pack = new DataPack();
  53. Pack.Frame = World.Frame;
  54. var ps = Pack.GetType().GetProperties();
  55. foreach (var p in ps)
  56. {
  57. if (!p.PropertyType.IsArray)
  58. continue;
  59. var t = p.PropertyType.GetElementType();
  60. if (t.IsGenericType)
  61. {
  62. var entType = t.GetGenericArguments()[0];
  63. var protType =GetProtocolType( entType);
  64. if (protType == null)
  65. continue;
  66. var arr = Device.All.Where(v => v.HasProtocol(protType))
  67. .Select(v =>
  68. {
  69. try
  70. {
  71. var protObj = v.Protocol(protType, World) as ProtocolProxyBase;
  72. if (protObj.Db.Failed)
  73. {
  74. return null;
  75. }
  76. var obj = Activator.CreateInstance(t);
  77. t.GetProperty("Code").SetValue(obj, v.Code);
  78. var value = WCS.Core.Extentions.Copy(protObj, entType);
  79. t.GetProperty("Data").SetValue(obj, value);
  80. t.GetProperty("Frame").SetValue(obj, protObj.Frame);
  81. return obj;
  82. }
  83. catch (Exception ex)
  84. {
  85. return null;
  86. }
  87. }).Where(v => v != null).ToArray();
  88. var m = typeof(Enumerable).GetMethod("OfType", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
  89. m = m.MakeGenericMethod(t);
  90. var arr2 = m.Invoke(null, new object[] { arr });
  91. m = typeof(Enumerable).GetMethod("ToArray", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
  92. m = m.MakeGenericMethod(t);
  93. var arr3 = m.Invoke(null, new object[] { arr2 });
  94. p.SetValue(Pack, arr3);
  95. }
  96. }
  97. sw.Stop();
  98. list.Add(new WorkTimes { Total = sw.ElapsedMilliseconds, Key = "采集数据" });
  99. Ltc.SetChannel(channel);
  100. }
  101. Type GetProtocolType(Type source)
  102. {
  103. var t = source.GetInterfaces()
  104. .Where(v => v.GetInterfaces().Any(d => d.Name == "IProtocol")).FirstOrDefault();
  105. return t;
  106. }
  107. }
  108. }