DataCollectionSysyem.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using ServiceCenter.Logs;
  2. using ServiceCenter.Redis;
  3. using System.Collections.Concurrent;
  4. using System.ComponentModel;
  5. using WCS.Core;
  6. using WCS.Entity.Protocol.DataStructure;
  7. using WCS.WorkEngineering.Extensions;
  8. using WCS.WorkEngineering.Worlds;
  9. namespace WCS.WorkEngineering.Systems
  10. {
  11. /// <summary>
  12. /// 数据采集系统
  13. /// </summary>
  14. [BelongTo(typeof(DataCollectionWorld))]
  15. [Description("数据采集系统")]
  16. public class DataCollectionSysyem : DeviceSystem<SRM>
  17. {
  18. public static DeviceDataPack pack = new DeviceDataPack();
  19. public DataCollectionSysyem()
  20. {
  21. }
  22. /// <summary>
  23. /// 所有设备数据
  24. /// Key 是不同设备所使用的类型 例如DeviceDataCollection<SRMData>
  25. /// value 不同设备的具体数据
  26. /// </summary>
  27. public static ConcurrentDictionary<string, DeviceData> AllDatas = new ConcurrentDictionary<string, DeviceData>();
  28. protected override bool ParallelDo => true;
  29. protected override bool SaveLogsToFile => true;
  30. public override bool Select(Device dev)
  31. {
  32. return dev.Code == "SRM1";
  33. }
  34. public override void Do(SRM obj)
  35. {
  36. try
  37. {
  38. var gs = AllDatas.GroupBy(v => v.Value.GetType());
  39. DeviceDataPack pack = new DeviceDataPack();
  40. pack.Frame = DateTime.Now;
  41. foreach (var g in gs)
  42. {
  43. var value = g.Select(v => v.Value).ToArray();
  44. var etype = g.Key;
  45. var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  46. var coll = Activator.CreateInstance(type, DateTime.Now, value);
  47. var p = pack.GetType().GetProperties().First(v => v.PropertyType == type);
  48. p.SetValue(pack, coll);
  49. }
  50. RedisHub.Monitor.RPush("Packs", pack);
  51. RedisHub.Monitor.Set(nameof(DeviceDataPack), pack);
  52. if (RedisHub.Monitor.LLen("Packs") > 200000)
  53. {
  54. RedisHub.Monitor.LTrim("Packs", 5000, -1);
  55. }
  56. }
  57. catch (Exception e)
  58. {
  59. throw new KnownException($"数据采集错误:{e.StackTrace}", LogLevelEnum.Low);
  60. }
  61. }
  62. }
  63. /// <summary>
  64. /// 设备报警
  65. /// </summary>
  66. public class EquipmentAlarm
  67. {
  68. /// <summary>
  69. /// 设备号
  70. /// </summary>
  71. public string Code { get; set; }
  72. /// <summary>
  73. /// 内容
  74. /// </summary>
  75. public string Msg { get; set; }
  76. /// <summary>
  77. /// 时间
  78. /// </summary>
  79. public DateTime Time { get; set; }
  80. }
  81. }