DataCollectionSysyem.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using Microsoft.OpenApi.Extensions;
  2. using Newtonsoft.Json;
  3. using ServiceCenter;
  4. using ServiceCenter.Extensions;
  5. using ServiceCenter.Redis;
  6. using ServiceCenter.SqlSugars;
  7. using System.ComponentModel;
  8. using WCS.Core;
  9. using WCS.Entity;
  10. using WCS.Entity.Protocol.BCR;
  11. using WCS.Entity.Protocol.DataStructure;
  12. using WCS.Entity.Protocol.SRM;
  13. using WCS.Entity.Protocol.Station;
  14. using WCS.Service.Worlds;
  15. using WCS.WorkEngineering.Extensions;
  16. namespace WCS.Service.Systems
  17. {
  18. /// <summary>
  19. /// 数据采集系统
  20. /// </summary>
  21. [BelongTo(typeof(DataCollectionWorld))]
  22. [Description("数据采集系统")]
  23. public class DataCollectionSysyem : DeviceSystem<SRM>
  24. {
  25. public List<Station> ConvList;
  26. public List<Device<IStation91>> Conv91List;
  27. public List<Device<IBCR81>> Bcrs;
  28. public List<SRM> Srms;
  29. public DataCollectionSysyem()
  30. {
  31. ConvList = Device.All.Where(v => v.HasProtocol(typeof(IStation523))).Select(v => new Station(v, this.World)).ToList();
  32. Conv91List = Device.All.Where(v => v.HasProtocol(typeof(IStation91))).Select(v => new Device<IStation91>(v, this.World)).ToList();
  33. Bcrs = Device.All.Where(v => v.HasProtocol(typeof(IBCR81))).Select(v => new Device<IBCR81>(v, this.World)).ToList();
  34. Srms = Device.All.Where(v => v.HasProtocol(typeof(ISRM520))).Select(v => new SRM(v, this.World)).ToList();
  35. }
  36. protected override bool ParallelDo => true;
  37. protected override bool SaveLogsToFile => true;
  38. public override bool Select(Device dev)
  39. {
  40. return dev.Code == "SRM1";
  41. }
  42. public override void Do(SRM obj)
  43. {
  44. SqlSugarHelper.Do(db =>
  45. {
  46. DeviceDataPack pack = new DeviceDataPack();
  47. List<StationData> DevList = new List<StationData>();
  48. foreach (var item in ConvList)
  49. {
  50. var dev = new StationData()
  51. {
  52. Code = item.Entity.Code,
  53. Frame = DateTime.Now,
  54. D520 = TypeExtension.Mapper<WCS_Station520, IStation520>(item.Data),
  55. D521 = TypeExtension.Mapper<WCS_Station521, IStation521>(item.Data2),
  56. D523 = TypeExtension.Mapper<WCS_Station523, IStation523>(item.Data3),
  57. D90 = new WCS_Station90(),
  58. D91 = new WCS_Station91(),
  59. D80 = new WCS_BCR80(),
  60. D81 = new WCS_BCR81()
  61. };
  62. if (Conv91List.Any(v => v.Entity.Code == item.Entity.Code))
  63. {
  64. var d91 = Conv91List.First(v => v.Entity.Code == item.Entity.Code);
  65. dev.D91 = TypeExtension.Mapper<WCS_Station91, IStation91>(d91.Data);
  66. }
  67. if (Bcrs.Any(v => v.Entity.Code == item.Entity.Code))
  68. {
  69. var d81 = Bcrs.First(v => v.Entity.Code == item.Entity.Code);
  70. dev.D81 = TypeExtension.Mapper<WCS_BCR81, IBCR81>(d81.Data);
  71. }
  72. RedisHub.WMS.Set(item.Entity.Code, item.Data2.Mode);
  73. DevList.Add(dev);
  74. }
  75. pack.StationDatas = new DeviceDataCollection<StationData>(DateTime.Now, DevList.ToArray());
  76. List<SRMData> srmList = new List<SRMData>();
  77. foreach (var item in Srms)
  78. {
  79. var dev = new SRMData()
  80. {
  81. Code = item.Entity.Code,
  82. Frame = DateTime.Now,
  83. D520 = TypeExtension.Mapper<WCS_SRM520, ISRM520>(item.Data),
  84. D521 = TypeExtension.Mapper<WCS_SRM521, ISRM521>(item.Data2),
  85. D537 = TypeExtension.Mapper<WCS_SRM537, ISRM537>(item.Data3),
  86. };
  87. RedisHub.WMS.Set(item.Entity.Code, item.Data2.AutoStatus);
  88. srmList.Add(dev);
  89. }
  90. pack.SRMDatas = new DeviceDataCollection<SRMData>(DateTime.Now, srmList.ToArray());
  91. pack.Frame = DateTime.Now;
  92. var plcData = new WCS_PlcData()
  93. {
  94. AddWho = "WCS",
  95. WAREHOUSE = ServiceHub.WarehouseName,
  96. CONTENT = JsonConvert.SerializeObject(pack, null, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }),
  97. };
  98. RedisHub.Monitor.RPush("Packs", pack);
  99. RedisHub.Monitor.Set(nameof(DeviceDataPack), pack);
  100. if (RedisHub.Monitor.LLen("Packs") > 500000)
  101. {
  102. RedisHub.Monitor.LTrim("Packs", 5000, -1);
  103. }
  104. db.Default.Insertable(plcData).ExecuteCommand();
  105. });
  106. }
  107. }
  108. }