DataCollectionSysyem.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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)
  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. var sw = new Stopwatch();
  50. sw.Start();
  51. try
  52. {
  53. var sw1 = new Stopwatch();
  54. sw1.Start();
  55. var gs = AllDatas.GroupBy(v => v.Value.GetType());
  56. DeviceDataPack pack = new DeviceDataPack();
  57. pack.Frame = DateTime.Now;
  58. foreach (var g in gs)
  59. {
  60. var value = g.Select(v => v.Value).ToArray();
  61. var etype = g.Key;
  62. var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  63. var coll = Activator.CreateInstance(type, DateTime.Now, value);
  64. var p = pack.GetType().GetProperties().First(v => v.PropertyType == type);
  65. p.SetValue(pack, coll);
  66. }
  67. sw1.Stop();
  68. World.Log($"数据解析耗时:{sw1.ElapsedMilliseconds}");
  69. var sw2 = new Stopwatch();
  70. sw2.Start();
  71. SqlSugarHelper.Do(_db =>
  72. {
  73. try
  74. {
  75. var sw3 = new Stopwatch();
  76. sw3.Start();
  77. var sql = new StringBuilder();
  78. sql.Append("INSERT INTO ");
  79. var db = _db.PLC;
  80. if (pack.SRMDatas != null && pack.SRMDatas.Datas.Any())
  81. {
  82. Parallel.ForEach(pack.SRMDatas.Datas, x =>
  83. {
  84. if (x.D520 != null) Set(sql, x.D520.CreateSql);
  85. if (x.D521 != null) Set(sql, x.D521.CreateSql);
  86. if (x.D537 != null) Set(sql, x.D537.CreateSql);
  87. });
  88. }
  89. if (pack.RGVDatas != null && pack.RGVDatas.Datas.Any())
  90. {
  91. Parallel.ForEach(pack.RGVDatas.Datas, x =>
  92. {
  93. if (x.D520 != null) Set(sql, x.D520.CreateSql);
  94. if (x.D521 != null) Set(sql, x.D521.CreateSql);
  95. if (x.D81 != null) Set(sql, x.D81.CreateSql);
  96. });
  97. }
  98. if (pack.StationDatas != null && pack.StationDatas.Datas.Any())
  99. {
  100. Parallel.ForEach(pack.StationDatas.Datas, x =>
  101. {
  102. if (x.D520 != null) Set(sql, x.D520.CreateSql);
  103. if (x.D521 != null) Set(sql, x.D521.CreateSql);
  104. if (x.D523 != null) Set(sql, x.D523.CreateSql);
  105. if (x.D90 != null) Set(sql, x.D90.CreateSql);
  106. if (x.D91 != null) Set(sql, x.D91.CreateSql);
  107. if (x.D80 != null) Set(sql, x.D80.CreateSql);
  108. if (x.D81 != null) Set(sql, x.D81.CreateSql);
  109. if (x.D83 != null) Set(sql, x.D83.CreateSql);
  110. if (x.D524 != null) Set(sql, x.D524.CreateSql);
  111. if (x.D525 != null) Set(sql, x.D525.CreateSql);
  112. if (x.D530 != null) Set(sql, x.D530.CreateSql);
  113. if (x.D5531 != null) Set(sql, x.D5531.CreateSql);
  114. if (x.DR530 != null) Set(sql, x.DR530.CreateSql);
  115. if (x.DR531 != null) Set(sql, x.DR531.CreateSql);
  116. });
  117. }
  118. if (pack.RobotData != null && pack.RobotData.Datas.Any())
  119. {
  120. Parallel.ForEach(pack.RobotData.Datas, x =>
  121. {
  122. if (x.D520 != null) Set(sql, x.D520.CreateSql);
  123. if (x.D521 != null) Set(sql, x.D521.CreateSql);
  124. if (x.D522 != null) Set(sql, x.D522.CreateSql);
  125. });
  126. }
  127. if (pack.TrussData != null && pack.TrussData.Datas.Any())
  128. {
  129. Parallel.ForEach(pack.TrussData.Datas, x =>
  130. {
  131. if (x.D520 != null) Set(sql, x.D520.CreateSql);
  132. if (x.D521 != null) Set(sql, x.D521.CreateSql);
  133. if (x.D523 != null) Set(sql, x.D523.CreateSql);
  134. });
  135. }
  136. sw3.Stop();
  137. World.Log($"转换SQL耗时:{sw3.ElapsedMilliseconds}");
  138. var sw4 = new Stopwatch();
  139. sw4.Start();
  140. var sqlText = sql.ToString();
  141. db.Ado.ExecuteCommand(sql.ToString());
  142. sw4.Stop();
  143. World.Log($"执行SQL耗时:{sw4.ElapsedMilliseconds}");
  144. }
  145. catch (Exception e)
  146. {
  147. World.Log($"数据采集错误1:{e.StackTrace}"); Console.WriteLine(e);
  148. }
  149. });
  150. sw2.Stop();
  151. World.Log($"数据保存数据库总耗时间:{sw2.ElapsedMilliseconds}");
  152. }
  153. catch (Exception e)
  154. {
  155. World.Log($"数据采集错误:{e.StackTrace}");
  156. }
  157. sw.Stop();
  158. World.Log($"数据采集总耗时:{sw.ElapsedMilliseconds}");
  159. }
  160. public void Set(StringBuilder sql, string cSql)
  161. {
  162. lock (locker)
  163. {
  164. sql.Append(cSql);
  165. }
  166. }
  167. }
  168. /// <summary>
  169. /// 设备报警
  170. /// </summary>
  171. public class EquipmentAlarm
  172. {
  173. /// <summary>
  174. /// 设备号
  175. /// </summary>
  176. public string Code { get; set; }
  177. /// <summary>
  178. /// 内容
  179. /// </summary>
  180. public string Msg { get; set; }
  181. /// <summary>
  182. /// 时间
  183. /// </summary>
  184. public DateTime Time { get; set; }
  185. }
  186. }