DataCollectionSysyem.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. RedisHub.WMS.Set($"{nameof(EquipmentAlarm)}:{item.Entity.Code}", item.Data3.Alarm);
  74. DevList.Add(dev);
  75. }
  76. pack.StationDatas = new DeviceDataCollection<StationData>(DateTime.Now, DevList.ToArray());
  77. List<SRMData> srmList = new List<SRMData>();
  78. foreach (var item in Srms)
  79. {
  80. var dev = new SRMData()
  81. {
  82. Code = item.Entity.Code,
  83. Frame = DateTime.Now,
  84. D520 = TypeExtension.Mapper<WCS_SRM520, ISRM520>(item.Data),
  85. D521 = TypeExtension.Mapper<WCS_SRM521, ISRM521>(item.Data2),
  86. D537 = TypeExtension.Mapper<WCS_SRM537, ISRM537>(item.Data3),
  87. };
  88. RedisHub.WMS.Set(item.Entity.Code, item.Data2.AutoStatus);
  89. RedisHub.WMS.Set($"{nameof(EquipmentAlarm)}:{item.Entity.Code}", item.Data3.Alarm);
  90. srmList.Add(dev);
  91. }
  92. pack.SRMDatas = new DeviceDataCollection<SRMData>(DateTime.Now, srmList.ToArray());
  93. pack.Frame = DateTime.Now;
  94. var plcData = new WCS_PlcData()
  95. {
  96. AddWho = "WCS",
  97. WAREHOUSE = ServiceHub.WarehouseName,
  98. CONTENT = JsonConvert.SerializeObject(pack, null, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }),
  99. };
  100. RedisHub.Monitor.RPush("Packs", pack);
  101. RedisHub.Monitor.Set(nameof(DeviceDataPack), pack);
  102. if (RedisHub.Monitor.LLen("Packs") > 500000)
  103. {
  104. RedisHub.Monitor.LTrim("Packs", 5000, -1);
  105. }
  106. db.Default.Insertable(plcData).ExecuteCommand();
  107. });
  108. }
  109. }
  110. /// <summary>
  111. /// 设备报警
  112. /// </summary>
  113. public class EquipmentAlarm
  114. {
  115. /// <summary>
  116. /// 设备号
  117. /// </summary>
  118. public string Code { get; set; }
  119. /// <summary>
  120. /// 内容
  121. /// </summary>
  122. public string Msg { get; set; }
  123. /// <summary>
  124. /// 时间
  125. /// </summary>
  126. public DateTime Time { get; set; }
  127. }
  128. /// <summary>
  129. /// 业务报警
  130. /// </summary>
  131. public class BusinessAlarm
  132. {
  133. /// <summary>
  134. /// 设备号
  135. /// </summary>
  136. public string BusinessName { get; set; }
  137. /// <summary>
  138. /// 内容
  139. /// </summary>
  140. public string Pos { get; set; }
  141. /// <summary>
  142. /// 时间
  143. /// </summary>
  144. public DateTime Time { get; set; }
  145. }
  146. }