DataCollectionSysyem.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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.BCR;
  9. using WCS.Entity.Protocol.DataStructure;
  10. using WCS.Entity.Protocol.RGV;
  11. using WCS.Entity.Protocol.Robot;
  12. using WCS.Entity.Protocol.SRM;
  13. using WCS.Entity.Protocol.Station;
  14. using WCS.Entity.Protocol.Truss;
  15. using WCS.WorkEngineering.Extensions;
  16. using WCS.WorkEngineering.Worlds;
  17. namespace WCS.WorkEngineering.Systems
  18. {
  19. /// <summary>
  20. /// 数据采集系统
  21. /// </summary>
  22. [BelongTo(typeof(DataCollectionWorld))]
  23. [Description("数据采集系统")]
  24. public class DataCollectionSysyem : DeviceSystem<SRM>
  25. {
  26. public static DeviceDataPack pack = new DeviceDataPack();
  27. private static object locker = new object();
  28. public DataCollectionSysyem()
  29. {
  30. 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 }))
  31. .GroupBy(v => v.DB);
  32. foreach (var g in gs)
  33. {
  34. var min = g.OrderBy(v => v.Position).First();
  35. var max = g.OrderByDescending(v => v.Position).First();
  36. var t = Type.GetType(min.TypeStr);
  37. min.Dev.Protocol(t, this.World);
  38. max.Dev.Protocol(t, this.World);
  39. }
  40. }
  41. /// <summary>
  42. /// 所有设备数据
  43. /// Key 是不同设备所使用的类型 例如DeviceDataCollection<SRMData>
  44. /// value 不同设备的具体数据
  45. /// </summary>
  46. public static ConcurrentDictionary<string, DeviceData> AllDatas = new ConcurrentDictionary<string, DeviceData>();
  47. protected override bool ParallelDo => true;
  48. protected override bool SaveLogsToFile => true;
  49. public override bool Select(Device dev)
  50. {
  51. return dev.Code == "SRM1";
  52. }
  53. public override void Do(SRM obj)
  54. {
  55. var sw = new Stopwatch();
  56. sw.Start();
  57. var pack = new DeviceDataPack();
  58. pack.Frame = DateTime.Now;
  59. var sql = new StringBuilder();
  60. sql.Append("INSERT INTO ");
  61. var ps = pack.GetType().GetProperties().OrderBy(x => x.Name);
  62. foreach (var p in ps)
  63. {
  64. if (!p.PropertyType.IsArray)
  65. continue;
  66. var dev = p.PropertyType.GetElementType();
  67. if (dev.GetInterfaces().Any(v => v.GetInterfaces().Any(d => d.Name == "IProtocol")))
  68. {
  69. var t = p.PropertyType.GetElementType();
  70. var protType = GetProtocolType(t);
  71. var arr = Device.All.Where(v => v.HasProtocol(protType))
  72. .Select(v =>
  73. {
  74. try
  75. {
  76. var obj = Activator.CreateInstance(t);
  77. t.GetProperty("Code").SetValue(obj, v.Code);
  78. dynamic protObj = v.Protocol(protType, World);
  79. protObj.Copy(obj);
  80. var value = GetSql(t, obj);
  81. t.GetProperty("Frame").SetValue(obj, protObj.Frame);
  82. //var value = ServiceCenter.Extensions.TypeExtension.Copy(protObj, t);
  83. //t.GetProperty("Data").SetValue(obj, value);
  84. sql.Append(value);
  85. return obj;
  86. }
  87. catch (Exception ex)
  88. {
  89. return null;
  90. }
  91. }).Where(v => v != null).ToArray();
  92. var m = typeof(Enumerable).GetMethod("OfType", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
  93. m = m.MakeGenericMethod(t);
  94. var arr2 = m.Invoke(null, new object[] { arr });
  95. m = typeof(Enumerable).GetMethod("ToArray", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
  96. m = m.MakeGenericMethod(t);
  97. var arr3 = m.Invoke(null, new object[] { arr2 });
  98. p.SetValue(pack, arr3);
  99. }
  100. }
  101. //Redis.RPush("Packs", pack);
  102. //if (Redis.LLen("Packs") > 50000)
  103. //{
  104. // Redis.LTrim("Packs", 5000, -1);
  105. //}
  106. sw.Stop();
  107. //var sw = new Stopwatch();
  108. //sw.Start();
  109. //var pack = new DeviceDataPack();
  110. //pack.Frame = DateTime.Now;
  111. //var ps = pack.GetType().GetProperties();
  112. //foreach (var p in ps)
  113. //{
  114. // if (!p.PropertyType.IsClass) continue;
  115. // var packAct = Activator.CreateInstance(p.PropertyType);
  116. // var prs = p.PropertyType.GetProperties();
  117. // foreach (var pr in prs)
  118. // {
  119. // if (!pr.PropertyType.IsArray) continue;
  120. // var yt = pr.PropertyType.GetElementType();
  121. // if (yt.IsClass)
  122. // {
  123. // var pros = yt.GetProperties();
  124. // //var entType = yt.GetGenericArguments()[0];
  125. // //var protType = GetProtocolType(entType);
  126. // var dataAct = Activator.CreateInstance(yt);
  127. // Parallel.ForEach(pros, pro =>
  128. // {
  129. // try
  130. // {
  131. // if (pro.PropertyType != typeof(DateTime))
  132. // {
  133. // if (pro.PropertyType != typeof(string))
  134. // {
  135. // var protType = GetProtocolType(pro.PropertyType);
  136. // var dev = Device.All
  137. // .Where(v => v.HasProtocol(protType)).Select(v =>
  138. // {
  139. // try
  140. // {
  141. // var obj = Activator.CreateInstance(pro.PropertyType);
  142. // pro.PropertyType.GetProperty("Code").SetValue(obj, v.Code);
  143. // var a = v.Protocol(protType, World);
  144. // var value = v.Protocol(protType, World).Copy(pro.PropertyType);
  145. // pro.SetValue(obj, value);
  146. // return obj;
  147. // }
  148. // catch (Exception ex)
  149. // {
  150. // return null;
  151. // }
  152. // }).FirstOrDefault(v => v != null);
  153. // if (dev != null)
  154. // {
  155. // pro.SetValue(dataAct, dev);
  156. // }
  157. // }
  158. // else
  159. // {
  160. // }
  161. // }
  162. // }
  163. // catch (Exception e)
  164. // {
  165. // Console.WriteLine(e);
  166. // }
  167. // });
  168. // var a = 1;
  169. // //var m = typeof(Enumerable).GetMethod("OfType", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
  170. // //m = m.MakeGenericMethod(yt);
  171. // //var arr2 = m.Invoke(null, new object[] { datasAct });
  172. // //m = typeof(Enumerable).GetMethod("ToArray", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
  173. // //m = m.MakeGenericMethod(yt);
  174. // //var arr3 = m.Invoke(null, new object[] { arr2 });
  175. // //p.SetValue(pack, arr3);
  176. // //var entType = yt.GetGenericArguments()[0];
  177. // //var protType = GetProtocolType(entType);
  178. // //var arr = Device.All.Where(v => v.HasProtocol(protType))
  179. // // .Select(v =>
  180. // // {
  181. // // try
  182. // // {
  183. // // var obj = Activator.CreateInstance(yt);
  184. // // yt.GetProperty("Code").SetValue(obj, v.Code);
  185. // // //var value = v.Protocol(protType, World).Copy(entType);
  186. // // //t.GetProperty("Data").SetValue(obj, value);
  187. // // return obj;
  188. // // }
  189. // // catch (Exception ex)
  190. // // {
  191. // // return null;
  192. // // }
  193. // // }).Where(v => v != null).ToArray();
  194. // //var m = typeof(Enumerable).GetMethod("OfType", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
  195. // //m = m.MakeGenericMethod(yt);
  196. // //var arr2 = m.Invoke(null, new object[] { arr });
  197. // //m = typeof(Enumerable).GetMethod("ToArray", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
  198. // //m = m.MakeGenericMethod(yt);
  199. // //var arr3 = m.Invoke(null, new object[] { arr2 });
  200. // //p.SetValue(pack, arr3);
  201. // }
  202. // }
  203. // //var t = p.PropertyType.GetElementType();
  204. // //if (t.IsGenericType)
  205. // //{
  206. // // var entType = t.GetGenericArguments()[0];
  207. // // var protType = GetProtocolType(entType);
  208. // // var arr = Device.All.Where(v => v.HasProtocol(protType))
  209. // // .Select(v =>
  210. // // {
  211. // // try
  212. // // {
  213. // // var obj = Activator.CreateInstance(t);
  214. // // t.GetProperty("Code").SetValue(obj, v.Code);
  215. // // //var value = v.Protocol(protType, World).Copy(entType);
  216. // // //t.GetProperty("Data").SetValue(obj, value);
  217. // // return obj;
  218. // // }
  219. // // catch (Exception ex)
  220. // // {
  221. // // return null;
  222. // // }
  223. // // }).Where(v => v != null).ToArray();
  224. // // var m = typeof(Enumerable).GetMethod("OfType", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
  225. // // m = m.MakeGenericMethod(t);
  226. // // var arr2 = m.Invoke(null, new object[] { arr });
  227. // // m = typeof(Enumerable).GetMethod("ToArray", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
  228. // // m = m.MakeGenericMethod(t);
  229. // // var arr3 = m.Invoke(null, new object[] { arr2 });
  230. // // p.SetValue(pack, arr3);
  231. // //}
  232. //}
  233. ////Redis.RPush("Packs", pack);
  234. ////if (Redis.LLen("Packs") > 50000)
  235. ////{
  236. //// Redis.LTrim("Packs", 5000, -1);
  237. ////}
  238. //sw.Stop();
  239. //list.Add(new WorkTimes { Total = sw.ElapsedMilliseconds, Key = "采集数据" });
  240. return;
  241. //var sw = new Stopwatch();
  242. //sw.Start();
  243. //try
  244. //{
  245. // var sw1 = new Stopwatch();
  246. // sw1.Start();
  247. // var gs = AllDatas.GroupBy(v => v.Value.GetType());
  248. // DeviceDataPack pack = new DeviceDataPack();
  249. // pack.Frame = DateTime.Now;
  250. // foreach (var g in gs)
  251. // {
  252. // var value = g.Select(v => v.Value).ToArray();
  253. // var etype = g.Key;
  254. // var type = typeof(DeviceDataCollection<>).MakeGenericType(etype);
  255. // var coll = Activator.CreateInstance(type, DateTime.Now, value);
  256. // var p = pack.GetType().GetProperties().First(v => v.PropertyType == type);
  257. // p.SetValue(pack, coll);
  258. // }
  259. // sw1.Stop();
  260. // World.Log($"数据解析耗时:{sw1.ElapsedMilliseconds}");
  261. // var sw2 = new Stopwatch();
  262. // sw2.Start();
  263. // SqlSugarHelper.Do(_db =>
  264. // {
  265. // try
  266. // {
  267. // var sw3 = new Stopwatch();
  268. // sw3.Start();
  269. // var sql = new StringBuilder();
  270. // sql.Append("INSERT INTO ");
  271. // var db = _db.PLC;
  272. // if (pack.SRMDatas != null && pack.SRMDatas.Datas.Any())
  273. // {
  274. // Parallel.ForEach(pack.SRMDatas.Datas, x =>
  275. // {
  276. // if (x.D520 != null) Set(sql, x.D520.CreateSql);
  277. // if (x.D521 != null) Set(sql, x.D521.CreateSql);
  278. // if (x.D537 != null) Set(sql, x.D537.CreateSql);
  279. // });
  280. // }
  281. // if (pack.RGVDatas != null && pack.RGVDatas.Datas.Any())
  282. // {
  283. // Parallel.ForEach(pack.RGVDatas.Datas, x =>
  284. // {
  285. // if (x.D520 != null) Set(sql, x.D520.CreateSql);
  286. // if (x.D521 != null) Set(sql, x.D521.CreateSql);
  287. // if (x.D81 != null) Set(sql, x.D81.CreateSql);
  288. // });
  289. // }
  290. // if (pack.StationDatas != null && pack.StationDatas.Datas.Any())
  291. // {
  292. // Parallel.ForEach(pack.StationDatas.Datas, x =>
  293. // {
  294. // if (x.D520 != null) Set(sql, x.D520.CreateSql);
  295. // if (x.D521 != null) Set(sql, x.D521.CreateSql);
  296. // if (x.D523 != null) Set(sql, x.D523.CreateSql);
  297. // if (x.D90 != null) Set(sql, x.D90.CreateSql);
  298. // if (x.D91 != null) Set(sql, x.D91.CreateSql);
  299. // if (x.D80 != null) Set(sql, x.D80.CreateSql);
  300. // if (x.D81 != null) Set(sql, x.D81.CreateSql);
  301. // if (x.D83 != null) Set(sql, x.D83.CreateSql);
  302. // if (x.D524 != null) Set(sql, x.D524.CreateSql);
  303. // if (x.D525 != null) Set(sql, x.D525.CreateSql);
  304. // if (x.D530 != null) Set(sql, x.D530.CreateSql);
  305. // if (x.D5531 != null) Set(sql, x.D5531.CreateSql);
  306. // if (x.DR530 != null) Set(sql, x.DR530.CreateSql);
  307. // if (x.DR531 != null) Set(sql, x.DR531.CreateSql);
  308. // });
  309. // }
  310. // if (pack.RobotData != null && pack.RobotData.Datas.Any())
  311. // {
  312. // Parallel.ForEach(pack.RobotData.Datas, x =>
  313. // {
  314. // if (x.D520 != null) Set(sql, x.D520.CreateSql);
  315. // if (x.D521 != null) Set(sql, x.D521.CreateSql);
  316. // if (x.D522 != null) Set(sql, x.D522.CreateSql);
  317. // });
  318. // }
  319. // if (pack.TrussData != null && pack.TrussData.Datas.Any())
  320. // {
  321. // Parallel.ForEach(pack.TrussData.Datas, x =>
  322. // {
  323. // if (x.D520 != null) Set(sql, x.D520.CreateSql);
  324. // if (x.D521 != null) Set(sql, x.D521.CreateSql);
  325. // if (x.D523 != null) Set(sql, x.D523.CreateSql);
  326. // });
  327. // }
  328. // sw3.Stop();
  329. // World.Log($"转换SQL耗时:{sw3.ElapsedMilliseconds}");
  330. // var sw4 = new Stopwatch();
  331. // sw4.Start();
  332. // var sqlText = sql.ToString();
  333. // db.Ado.ExecuteCommand(sql.ToString());
  334. // sw4.Stop();
  335. // World.Log($"执行SQL耗时:{sw4.ElapsedMilliseconds}");
  336. // }
  337. // catch (Exception e)
  338. // {
  339. // World.Log($"数据采集错误1:{e.StackTrace}"); Console.WriteLine(e);
  340. // }
  341. // });
  342. // sw2.Stop();
  343. // World.Log($"数据保存数据库总耗时间:{sw2.ElapsedMilliseconds}");
  344. //}
  345. //catch (Exception e)
  346. //{
  347. // World.Log($"数据采集错误:{e.StackTrace}");
  348. //}
  349. //sw.Stop();
  350. //World.Log($"数据采集总耗时:{sw.ElapsedMilliseconds}");
  351. }
  352. public void Set(StringBuilder sql, string cSql)
  353. {
  354. lock (locker)
  355. {
  356. sql.Append(cSql);
  357. }
  358. }
  359. private Type GetProtocolType(Type source)
  360. {
  361. var t = source.GetInterfaces().FirstOrDefault(v => v.GetInterfaces().Any(d => d.Name == "IProtocol"));
  362. return t;
  363. }
  364. public string GetSql(Type type, object entity)
  365. {
  366. string sql = string.Empty;
  367. var db = new SqlSugarHelper().PLC;
  368. var ty = entity.GetType().GetProperty("CreateSql");
  369. switch (type.Name)
  370. {
  371. case nameof(WCS_SRM520):
  372. var item = entity as WCS_SRM520;
  373. sql = GetString(db.Insertable(item).ToSqlString());
  374. ty.SetValue(entity, sql);
  375. break;
  376. case nameof(WCS_SRM521):
  377. var item1 = entity as WCS_SRM521;
  378. sql = GetString(db.Insertable(item1).ToSqlString());
  379. ty.SetValue(entity, sql);
  380. break;
  381. case nameof(WCS_SRM537):
  382. var item2 = entity as WCS_SRM537;
  383. sql = GetString(db.Insertable(item2).ToSqlString());
  384. ty.SetValue(entity, GetString(db.Insertable(item2).ToSqlString()));
  385. break;
  386. case nameof(WCS_RGV520):
  387. var item3 = entity as WCS_RGV520; sql = GetString(db.Insertable(item3).ToSqlString());
  388. ty.SetValue(entity, sql);
  389. break;
  390. case nameof(WCS_RGV521):
  391. var item4 = entity as WCS_RGV521; sql = GetString(db.Insertable(item4).ToSqlString());
  392. ty.SetValue(entity, sql);
  393. break;
  394. case nameof(WCS_BCR80):
  395. var item5 = entity as WCS_BCR80; sql = GetString(db.Insertable(item5).ToSqlString());
  396. ty.SetValue(entity, sql);
  397. break;
  398. case nameof(WCS_BCR81):
  399. var item6 = entity as WCS_BCR81; sql = GetString(db.Insertable(item6).ToSqlString());
  400. ty.SetValue(entity, sql);
  401. break;
  402. case nameof(WCS_BCR83):
  403. var item7 = entity as WCS_BCR83; sql = GetString(db.Insertable(item7).ToSqlString());
  404. ty.SetValue(entity, sql);
  405. break;
  406. case nameof(WCS_Station520):
  407. var item8 = entity as WCS_Station520; sql = GetString(db.Insertable(item8).ToSqlString());
  408. ty.SetValue(entity, sql);
  409. break;
  410. case nameof(WCS_Station521):
  411. var item9 = entity as WCS_Station521; sql = GetString(db.Insertable(item9).ToSqlString());
  412. ty.SetValue(entity, sql);
  413. break;
  414. case nameof(WCS_Station523):
  415. var item10 = entity as WCS_Station523; sql = GetString(db.Insertable(item10).ToSqlString());
  416. ty.SetValue(entity, sql);
  417. break;
  418. case nameof(WCS_Station524):
  419. var item11 = entity as WCS_Station524; sql = GetString(db.Insertable(item11).ToSqlString());
  420. ty.SetValue(entity, sql);
  421. break;
  422. case nameof(WCS_Station525):
  423. var item12 = entity as WCS_Station525; sql = GetString(db.Insertable(item12).ToSqlString());
  424. ty.SetValue(entity, sql);
  425. break;
  426. case nameof(WCS_Station90):
  427. var item13 = entity as WCS_Station90; sql = GetString(db.Insertable(item13).ToSqlString());
  428. ty.SetValue(entity, sql);
  429. break;
  430. case nameof(WCS_Station91):
  431. var item14 = entity as WCS_Station91; sql = GetString(db.Insertable(item14).ToSqlString());
  432. ty.SetValue(entity, sql);
  433. break;
  434. case nameof(WCS_Truss520):
  435. var item15 = entity as WCS_Truss520; sql = GetString(db.Insertable(item15).ToSqlString());
  436. ty.SetValue(entity, sql);
  437. break;
  438. case nameof(WCS_Truss521):
  439. var item16 = entity as WCS_Truss521; sql = GetString(db.Insertable(item16).ToSqlString());
  440. ty.SetValue(entity, sql);
  441. break;
  442. case nameof(WCS_Truss523):
  443. var item17 = entity as WCS_Truss523; sql = GetString(db.Insertable(item17).ToSqlString());
  444. ty.SetValue(entity, sql);
  445. break;
  446. case nameof(WCS_Truss530):
  447. var item18 = entity as WCS_Truss530; sql = GetString(db.Insertable(item18).ToSqlString());
  448. ty.SetValue(entity, sql);
  449. break;
  450. case nameof(WCS_Truss531):
  451. var item19 = entity as WCS_Truss531; sql = GetString(db.Insertable(item19).ToSqlString());
  452. ty.SetValue(entity, sql);
  453. break;
  454. case nameof(WCS_Robot520):
  455. var item20 = entity as WCS_Robot520; sql = GetString(db.Insertable(item20).ToSqlString());
  456. ty.SetValue(entity, sql);
  457. break;
  458. case nameof(WCS_Robot521):
  459. var item21 = entity as WCS_Robot521; sql = GetString(db.Insertable(item21).ToSqlString());
  460. ty.SetValue(entity, sql);
  461. break;
  462. case nameof(WCS_Robot522):
  463. var item22 = entity as WCS_Robot522; sql = GetString(db.Insertable(item22).ToSqlString());
  464. ty.SetValue(entity, sql);
  465. break;
  466. case nameof(WCS_Robot530):
  467. var item23 = entity as WCS_Robot530; sql = GetString(db.Insertable(item23).ToSqlString());
  468. ty.SetValue(entity, sql);
  469. break;
  470. case nameof(WCS_Robot531):
  471. var item24 = entity as WCS_Robot531; sql = GetString(db.Insertable(item24).ToSqlString());
  472. ty.SetValue(entity, sql);
  473. break;
  474. }
  475. return sql;
  476. }
  477. public string GetString(string value)
  478. {
  479. return value.Replace("INSERT INTO ", "")
  480. .Replace(",N'", ",'")
  481. .Replace("\0", "")
  482. .Replace("wcs_", "")
  483. .Replace("(N'", "('") + "\r";
  484. }
  485. }
  486. /// <summary>
  487. /// 设备报警
  488. /// </summary>
  489. public class EquipmentAlarm
  490. {
  491. /// <summary>
  492. /// 设备号
  493. /// </summary>
  494. public string Code { get; set; }
  495. /// <summary>
  496. /// 内容
  497. /// </summary>
  498. public string Msg { get; set; }
  499. /// <summary>
  500. /// 时间
  501. /// </summary>
  502. public DateTime Time { get; set; }
  503. }
  504. }