DataCollectionSysyem.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using PlcSiemens.Core.Extension;
  2. using ServiceCenter.SqlSugars;
  3. using System.Collections.Concurrent;
  4. using System.ComponentModel;
  5. using System.Diagnostics;
  6. using System.Text;
  7. using WCS.Core;
  8. using WCS.Entity.Protocol.DataStructure;
  9. using WCS.WorkEngineering.Extensions;
  10. using WCS.WorkEngineering.Worlds;
  11. namespace WCS.WorkEngineering.Systems
  12. {
  13. /// <summary>
  14. /// 数据采集系统
  15. /// </summary>
  16. [BelongTo(typeof(DataCollectionWorld))]
  17. [Description("数据采集系统")]
  18. public class DataCollectionSysyem : DeviceSystem<SRM>
  19. {
  20. public static DeviceDataPack pack = new DeviceDataPack();
  21. private static object locker = new object();
  22. public DataCollectionSysyem()
  23. {
  24. 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 }))
  25. .GroupBy(v => v.DB);
  26. foreach (var g in gs.Where(x => !x.Key.Contains("10.30.37.211") && !x.Key.Contains("10.30.37.217") && !x.Key.Contains("10.30.37.223")))
  27. {
  28. var min = g.OrderBy(v => v.Position).First();
  29. var max = g.OrderByDescending(v => v.Position).First();
  30. var t = Type.GetType(min.TypeStr);
  31. min.Dev.Protocol(t, this.World);
  32. max.Dev.Protocol(t, this.World);
  33. }
  34. }
  35. /// <summary>
  36. /// 所有设备数据
  37. /// Key 是不同设备所使用的类型 例如DeviceDataCollection<SRMData>
  38. /// value 不同设备的具体数据
  39. /// </summary>
  40. public static ConcurrentDictionary<string, DeviceData> AllDatas = new ConcurrentDictionary<string, DeviceData>();
  41. protected override bool ParallelDo => true;
  42. protected override bool SaveLogsToFile => true;
  43. public override bool Select(Device dev)
  44. {
  45. return dev.Code == "SRM1";
  46. }
  47. public override void Do(SRM obj)
  48. {
  49. return;
  50. var sw = new Stopwatch();
  51. sw.Start();
  52. try
  53. {
  54. var sw1 = new Stopwatch();
  55. sw1.Start();
  56. var gs = AllDatas.GroupBy(v => v.Value.GetType());
  57. DeviceDataPack pack = new DeviceDataPack();
  58. pack.Frame = DateTime.Now;
  59. foreach (var g in gs)
  60. {
  61. var value = g.Select(v => v.Value).ToArray();
  62. var etype = g.Key;
  63. var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  64. var coll = Activator.CreateInstance(type, DateTime.Now, value);
  65. var p = pack.GetType().GetProperties().First(v => v.PropertyType == type);
  66. p.SetValue(pack, coll);
  67. }
  68. sw1.Stop();
  69. World.Log($"数据解析耗时:{sw1.ElapsedMilliseconds}");
  70. var sw2 = new Stopwatch();
  71. sw2.Start();
  72. SqlSugarHelper.Do(_db =>
  73. {
  74. try
  75. {
  76. var sw3 = new Stopwatch();
  77. sw3.Start();
  78. var sql = new StringBuilder();
  79. sql.Append("INSERT INTO ");
  80. var db = _db.PLC;
  81. if (pack.SRMDatas != null && pack.SRMDatas.Datas.Any())
  82. {
  83. Parallel.ForEach(pack.SRMDatas.Datas, x =>
  84. {
  85. if (x.D520 != null) Set(sql, x.D520.CreateSql);
  86. if (x.D521 != null) Set(sql, x.D521.CreateSql);
  87. if (x.D537 != null) Set(sql, x.D537.CreateSql);
  88. });
  89. }
  90. if (pack.RGVDatas != null && pack.RGVDatas.Datas.Any())
  91. {
  92. Parallel.ForEach(pack.RGVDatas.Datas, x =>
  93. {
  94. if (x.D520 != null) Set(sql, x.D520.CreateSql);
  95. if (x.D521 != null) Set(sql, x.D521.CreateSql);
  96. if (x.D81 != null) Set(sql, x.D81.CreateSql);
  97. });
  98. }
  99. if (pack.StationDatas != null && pack.StationDatas.Datas.Any())
  100. {
  101. Parallel.ForEach(pack.StationDatas.Datas, x =>
  102. {
  103. if (x.D520 != null) Set(sql, x.D520.CreateSql);
  104. if (x.D521 != null) Set(sql, x.D521.CreateSql);
  105. if (x.D523 != null) Set(sql, x.D523.CreateSql);
  106. if (x.D90 != null) Set(sql, x.D90.CreateSql);
  107. if (x.D91 != null) Set(sql, x.D91.CreateSql);
  108. if (x.D80 != null) Set(sql, x.D80.CreateSql);
  109. if (x.D81 != null) Set(sql, x.D81.CreateSql);
  110. if (x.D83 != null) Set(sql, x.D83.CreateSql);
  111. if (x.D524 != null) Set(sql, x.D524.CreateSql);
  112. if (x.D525 != null) Set(sql, x.D525.CreateSql);
  113. if (x.D530 != null) Set(sql, x.D530.CreateSql);
  114. if (x.D5531 != null) Set(sql, x.D5531.CreateSql);
  115. if (x.DR530 != null) Set(sql, x.DR530.CreateSql);
  116. if (x.DR531 != null) Set(sql, x.DR531.CreateSql);
  117. });
  118. }
  119. if (pack.RobotData != null && pack.RobotData.Datas.Any())
  120. {
  121. Parallel.ForEach(pack.RobotData.Datas, x =>
  122. {
  123. if (x.D520 != null) Set(sql, x.D520.CreateSql);
  124. if (x.D521 != null) Set(sql, x.D521.CreateSql);
  125. if (x.D522 != null) Set(sql, x.D522.CreateSql);
  126. });
  127. }
  128. if (pack.TrussData != null && pack.TrussData.Datas.Any())
  129. {
  130. Parallel.ForEach(pack.TrussData.Datas, x =>
  131. {
  132. if (x.D520 != null) Set(sql, x.D520.CreateSql);
  133. if (x.D521 != null) Set(sql, x.D521.CreateSql);
  134. if (x.D523 != null) Set(sql, x.D523.CreateSql);
  135. });
  136. }
  137. sw3.Stop();
  138. World.Log($"转换SQL耗时:{sw3.ElapsedMilliseconds}");
  139. var sw4 = new Stopwatch();
  140. sw4.Start();
  141. var sqlText = sql.ToString();
  142. db.Ado.ExecuteCommand(sql.ToString());
  143. sw4.Stop();
  144. World.Log($"执行SQL耗时:{sw4.ElapsedMilliseconds}");
  145. }
  146. catch (Exception e)
  147. {
  148. World.Log($"数据采集错误1:{e.StackTrace}"); Console.WriteLine(e);
  149. }
  150. });
  151. sw2.Stop();
  152. World.Log($"数据保存数据库总耗时间:{sw2.ElapsedMilliseconds}");
  153. }
  154. catch (Exception e)
  155. {
  156. World.Log($"数据采集错误:{e.StackTrace}");
  157. }
  158. sw.Stop();
  159. World.Log($"数据采集总耗时:{sw.ElapsedMilliseconds}");
  160. }
  161. public void Set(StringBuilder sql, string cSql)
  162. {
  163. lock (locker)
  164. {
  165. sql.Append(cSql);
  166. }
  167. }
  168. }
  169. /// <summary>
  170. /// 设备报警
  171. /// </summary>
  172. public class EquipmentAlarm
  173. {
  174. /// <summary>
  175. /// 设备号
  176. /// </summary>
  177. public string Code { get; set; }
  178. /// <summary>
  179. /// 内容
  180. /// </summary>
  181. public string Msg { get; set; }
  182. /// <summary>
  183. /// 时间
  184. /// </summary>
  185. public DateTime Time { get; set; }
  186. }
  187. }