MainSysyem.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using RestSharp;
  2. using ServiceCenter.Redis;
  3. using ServiceCenter.SqlSugars;
  4. using ServiceCenter.WebApi;
  5. using SqlSugar;
  6. using System.ComponentModel;
  7. using System.Diagnostics;
  8. using System.Text;
  9. using WCS.Core;
  10. using WCS.Entity.Protocol.BCR;
  11. using WCS.Entity.Protocol.DataStructure;
  12. using WCS.Entity.Protocol.RGV;
  13. using WCS.Entity.Protocol.Robot;
  14. using WCS.Entity.Protocol.SRM;
  15. using WCS.Entity.Protocol.Station;
  16. using WCS.Entity.Protocol.Truss;
  17. using WCS.WorkEngineering.Extensions;
  18. using WCS.WorkEngineering.Worlds;
  19. namespace WCS.WorkEngineering.Systems
  20. {
  21. /// <summary>
  22. /// 数据处理
  23. /// </summary>
  24. //[BelongTo(typeof(MainWorld))]
  25. [Description("数据处理")]
  26. public class MainSysyem : SystemBase
  27. {
  28. public override void Update(List<WorkTimes> list)
  29. {
  30. try
  31. {
  32. var channel = new Channel
  33. {
  34. World = World.Description,
  35. Stage = "DoLogics",
  36. System = Description,
  37. Item = this.Description
  38. };
  39. Ltc.SetChannel(channel);
  40. var sw = new Stopwatch();
  41. sw.Start();
  42. var number = new List<int>();
  43. number.AddRange(new[] { 1, 2, 3, 4, 5 });
  44. var sql1 = "";
  45. Parallel.ForEach(number, i =>
  46. {
  47. var db = new SqlSugarHelper().PLC;
  48. var pack = RedisHub.Monitor.LPop<DeviceDataPack>("DataCollectionpacks");
  49. ;
  50. if (pack == null) return;
  51. Parallel.ForEach(pack.GetType().GetProperties().OrderBy(x => x.Name), ps =>
  52. {
  53. var sw10 = new Stopwatch();
  54. sw10.Start();
  55. var typeName = "";
  56. try
  57. {
  58. //if (ps.PropertyType == typeof(ProtocolData<WCS_BCR80>[]))
  59. //{
  60. // if (pack.BCR80.Any())
  61. // {
  62. // var value = pack.BCR80.Select(x => x.Data).ToList();
  63. // var sql = db.Insertable(value).ToSqlString();
  64. // db.Ado.ExecuteCommand(sql);
  65. // typeName = typeof(WCS_BCR80).Name;
  66. // }
  67. //}
  68. //else if (ps.PropertyType == typeof(ProtocolData<WCS_BCR81>[]))
  69. //{
  70. // if (pack.BCR81.Any())
  71. // {
  72. // var value = pack.BCR81.Select(x => x.Data).ToList();
  73. // var sql = db.Insertable(value).ToSqlString();
  74. // db.Ado.ExecuteCommand(sql);
  75. // typeName = typeof(WCS_BCR81).Name;
  76. // }
  77. //}
  78. //else if (ps.PropertyType == typeof(ProtocolData<WCS_BCR83>[]))
  79. //{
  80. // if (pack.BCR83.Any())
  81. // {
  82. // var value = pack.BCR83.Select(x => x.Data).ToList();
  83. // var sql = db.Insertable(value).ToSqlString();
  84. // db.Ado.ExecuteCommand(sql);
  85. // typeName = typeof(WCS_BCR83).Name;
  86. // }
  87. //}
  88. //else if (ps.PropertyType == typeof(ProtocolData<WCS_RGV520>[]))
  89. //{
  90. // if (pack.RGV520.Any())
  91. // {
  92. // var value = pack.RGV520.Select(x => x.Data).ToList();
  93. // var sql = db.Insertable(value).ToSqlString();
  94. // db.Ado.ExecuteCommand(sql);
  95. // typeName = typeof(WCS_RGV520).Name;
  96. // }
  97. //}
  98. //else if (ps.PropertyType == typeof(ProtocolData<WCS_RGV521>[]))
  99. //{
  100. // if (pack.RGV521.Any())
  101. // {
  102. // var value = pack.RGV521.Select(x => x.Data).ToList();
  103. // var sql = db.Insertable(value).ToSqlString();
  104. // db.Ado.ExecuteCommand(sql);
  105. // typeName = typeof(WCS_RGV521).Name;
  106. // }
  107. //}
  108. //else if (ps.PropertyType == typeof(ProtocolData<WCS_RGV523>[]))
  109. //{
  110. // if (pack.RGV523.Any())
  111. // {
  112. // var value = pack.RGV523.Select(x => new QuestDb_RGV523()
  113. // {
  114. // Alarm = Convert.ToString(x.Data.Alarm),
  115. // Code = x.Data.Code,
  116. // Frame = x.Data.Frame.ToLocalTime()
  117. // }).ToList();
  118. // var sql = db.Insertable(value).ToSqlString();
  119. // db.Ado.ExecuteCommand(GetString(sql));
  120. // typeName = typeof(WCS_RGV523).Name;
  121. // }
  122. //}
  123. //else
  124. if (ps.PropertyType == typeof(ProtocolData<WCS_Robot520>[]))
  125. {
  126. if (pack.Robot520.Any())
  127. {
  128. var value = pack.Robot520.Select(x => x.Data).ToList();
  129. var sql = db.Insertable(value).ToSqlString();
  130. db.Ado.ExecuteCommand(sql);
  131. typeName = typeof(WCS_Robot520).Name;
  132. }
  133. }
  134. else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot521>[]))
  135. {
  136. if (pack.Robot521.Any())
  137. {
  138. var value = pack.Robot521.Select(x => x.Data).ToList();
  139. var sql = db.Insertable(value).ToSqlString();
  140. db.Ado.ExecuteCommand(sql);
  141. typeName = typeof(WCS_Robot521).Name;
  142. }
  143. }
  144. else if (ps.PropertyType == typeof(ProtocolData<WCS_Station5>[]))
  145. {
  146. if (pack.Station5.Any())
  147. {
  148. var value = pack.Station5.Select(x => x.Data).ToList();
  149. var sql = db.Insertable(value).ToSqlString();
  150. db.Ado.ExecuteCommand(sql);
  151. typeName = typeof(WCS_Station5).Name;
  152. }
  153. }
  154. //else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot522>[]))
  155. //{
  156. // if (pack.Robot522.Any())
  157. // {
  158. // var value = pack.Robot522.Select(x => new QuestDb_Robot522()
  159. // {
  160. // Alarm = Convert.ToString(x.Data.Alarm),
  161. // Code = x.Data.Code,
  162. // Frame = x.Data.Frame.ToLocalTime()
  163. // }).ToList();
  164. // var sql = db.Insertable(value).ToSqlString();
  165. // db.Ado.ExecuteCommand(sql);
  166. // typeName = typeof(WCS_Robot522).Name;
  167. // }
  168. //}
  169. //else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot530>[]))
  170. //{
  171. // if (pack.Robot530.Any())
  172. // {
  173. // var value = pack.Robot530.Select(x => x.Data).ToList();
  174. // var sql = db.Insertable(value).ToSqlString();
  175. // db.Ado.ExecuteCommand(sql);
  176. // typeName = typeof(WCS_Robot530).Name;
  177. // }
  178. //}
  179. //else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot531>[]))
  180. //{
  181. // if (pack.Robot531.Any())
  182. // {
  183. // var value = pack.Robot531.Select(x => x.Data).ToList();
  184. // var sql = db.Insertable(value).ToSqlString();
  185. // db.Ado.ExecuteCommand(sql);
  186. // typeName = typeof(WCS_Robot531).Name;
  187. // }
  188. //}
  189. else if (ps.PropertyType == typeof(ProtocolData<WCS_SRM520>[]))
  190. {
  191. if (pack.SRM520.Any())
  192. {
  193. var value = pack.SRM520.Select(x => x.Data).ToList();
  194. var sql = db.Insertable(value).ToSqlString();
  195. db.Ado.ExecuteCommand(sql);
  196. typeName = typeof(WCS_SRM520).Name;
  197. }
  198. }
  199. else if (ps.PropertyType == typeof(ProtocolData<WCS_SRM521>[]))
  200. {
  201. if (pack.SRM521.Any())
  202. {
  203. var value = pack.SRM521.Select(x => x.Data).ToList();
  204. var sql = db.Insertable(value).ToSqlString();
  205. db.Ado.ExecuteCommand(sql);
  206. typeName = typeof(WCS_SRM521).Name;
  207. }
  208. }
  209. //else if (ps.PropertyType == typeof(ProtocolData<WCS_SRM523>[]))
  210. //{
  211. // if (pack.SRM523.Any())
  212. // {
  213. // var value = pack.SRM523.Select(x => new QuestDb_SRM523()
  214. // {
  215. // Alarm = $"{Convert.ToString(x.Data.Alarm1)},{Convert.ToString(x.Data.Alarm1)}",
  216. // Code = x.Data.Code,
  217. // Frame = x.Data.Frame.ToLocalTime()
  218. // }).ToList();
  219. // var sql = db.Insertable(value).ToSqlString();
  220. // db.Ado.ExecuteCommand(GetString(sql));
  221. // typeName = typeof(WCS_SRM523).Name;
  222. // }
  223. //}
  224. //else if (ps.PropertyType == typeof(ProtocolData<WCS_Station520>[]))
  225. //{
  226. // if (pack.Station520.Any())
  227. // {
  228. // var value = pack.Station520.Select(x => x.Data).ToList();
  229. // var sql = db.Insertable(value).ToSqlString();
  230. // db.Ado.ExecuteCommand(sql);
  231. // typeName = typeof(WCS_Station520).Name;
  232. // }
  233. //}
  234. //else if (ps.PropertyType == typeof(ProtocolData<WCS_Station521>[]))
  235. //{
  236. // if (pack.Station521.Any())
  237. // {
  238. // var value = pack.Station521.Select(x => x.Data).ToList();
  239. // var sql = db.Insertable(value).ToSqlString();
  240. // db.Ado.ExecuteCommand(sql);
  241. // typeName = typeof(WCS_Station521).Name;
  242. // }
  243. //}
  244. //else if (ps.PropertyType == typeof(ProtocolData<WCS_Station523>[]))
  245. //{
  246. // if (pack.Station523.Any())
  247. // {
  248. // var value = pack.Station523.Select(x => x.Data).ToList();
  249. // var sql = db.Insertable(value).ToSqlString();
  250. // db.Ado.ExecuteCommand(sql);
  251. // typeName = typeof(WCS_Station523).Name;
  252. // }
  253. //}
  254. //else if (ps.PropertyType == typeof(ProtocolData<WCS_Station524>[]))
  255. //{
  256. // if (pack.Station524.Any())
  257. // {
  258. // var value = pack.Station524.Select(x => x.Data).ToList();
  259. // var sql = db.Insertable(value).ToSqlString();
  260. // db.Ado.ExecuteCommand(sql);
  261. // typeName = typeof(WCS_Station524).Name;
  262. // }
  263. //}
  264. //else if (ps.PropertyType == typeof(ProtocolData<WCS_Station525>[]))
  265. //{
  266. // if (pack.Station525.Any())
  267. // {
  268. // var value = pack.Station525.Select(x => x.Data).ToList();
  269. // var sql = db.Insertable(value).ToSqlString();
  270. // db.Ado.ExecuteCommand(sql);
  271. // typeName = typeof(WCS_Station525).Name;
  272. // }
  273. //}
  274. //else if (ps.PropertyType == typeof(ProtocolData<WCS_Station90>[]))
  275. //{
  276. // if (pack.Station90.Any())
  277. // {
  278. // var value = pack.Station90.Select(x => x.Data).ToList();
  279. // var sql = db.Insertable(value).ToSqlString();
  280. // db.Ado.ExecuteCommand(sql);
  281. // typeName = typeof(WCS_Station90).Name;
  282. // }
  283. //}
  284. //else if (ps.PropertyType == typeof(ProtocolData<WCS_Station91>[]))
  285. //{
  286. // if (pack.Station91.Any())
  287. // {
  288. // var value = pack.Station91.Select(x => x.Data).ToList();
  289. // var sql = db.Insertable(value).ToSqlString();
  290. // db.Ado.ExecuteCommand(sql);
  291. // typeName = typeof(WCS_Station91).Name;
  292. // }
  293. //}
  294. else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss520>[]))
  295. {
  296. if (pack.Truss520.Any())
  297. {
  298. var value = pack.Truss520.Select(x => x.Data).ToList();
  299. var sql = db.Insertable(value).ToSqlString();
  300. db.Ado.ExecuteCommand(sql);
  301. typeName = typeof(WCS_Truss520).Name;
  302. }
  303. }
  304. else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss521>[]))
  305. {
  306. if (pack.Truss521.Any())
  307. {
  308. var value = pack.Truss521.Select(x => x.Data).ToList();
  309. var sql = db.Insertable(value).ToSqlString();
  310. db.Ado.ExecuteCommand(sql);
  311. typeName = typeof(WCS_Truss521).Name;
  312. }
  313. }
  314. //else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss523>[]))
  315. //{
  316. // if (pack.Truss523.Any())
  317. // {
  318. // var value = pack.Truss523.Select(x => new QuestDb_Truss523()
  319. // {
  320. // Alarm = Convert.ToString(x.Data.Alarm),
  321. // Code = x.Data.Code,
  322. // Frame = x.Data.Frame.ToLocalTime()
  323. // }).ToList();
  324. // var sql = db.Insertable(value).ToSqlString();
  325. // db.Ado.ExecuteCommand(sql);
  326. // typeName = typeof(WCS_Truss523).Name;
  327. // }
  328. //}
  329. //else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss530>[]))
  330. //{
  331. // if (pack.Truss530.Any())
  332. // {
  333. // var value = pack.Truss530.Select(x => x.Data).ToList();
  334. // var sql = db.Insertable(value).ToSqlString();
  335. // db.Ado.ExecuteCommand(sql);
  336. // typeName = typeof(WCS_Truss530).Name;
  337. // }
  338. //}
  339. //else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss531>[]))
  340. //{
  341. // if (pack.Truss531.Any())
  342. // {
  343. // var value = pack.Truss531.Select(x => x.Data).ToList();
  344. // var sql = db.Insertable(value).ToSqlString();
  345. // db.Ado.ExecuteCommand(sql);
  346. // typeName = typeof(WCS_Truss531).Name;
  347. // }
  348. //}
  349. }
  350. catch (Exception e)
  351. {
  352. World.Log($"错误内容:{e.Message}");
  353. }
  354. finally
  355. {
  356. sw10.Stop();
  357. if (sw10.ElapsedMilliseconds > 1000)
  358. {
  359. World.Log($"执行耗时:{typeName}:{sw10.ElapsedMilliseconds}");
  360. }
  361. }
  362. });
  363. });
  364. sw.Stop();
  365. if (sw.ElapsedMilliseconds > 1000)
  366. {
  367. World.Log($"数据处理耗时:{sw.ElapsedMilliseconds}");
  368. }
  369. }
  370. catch (Exception e)
  371. {
  372. World.Log($"错误内容:{e.Message}");
  373. }
  374. }
  375. private Type GetProtocolType(Type source)
  376. {
  377. var t = source.GetInterfaces().FirstOrDefault(v => v.GetInterfaces().Any(d => d.Name == "IProtocol"));
  378. var t1 = source.GetInterfaces().FirstOrDefault(v => v.GetInterfaces().Any(d => d.Name == "IProtocol"));
  379. return t;
  380. }
  381. private object AppendLock = new object();
  382. public StringBuilder Append(StringBuilder sql, string value)
  383. {
  384. lock (AppendLock)
  385. {
  386. return sql.Append(value);
  387. }
  388. }
  389. public string GetString(string value)
  390. {
  391. return value.Replace(",N'", ",'")
  392. .Replace("\0", "")
  393. .Replace("(N'", "('") + "\r";
  394. }
  395. public override List<object> GetObjects()
  396. {
  397. return new List<object>();
  398. }
  399. public bool QuestDbInsert(string query)
  400. {
  401. var client = new RestClient($"http://10.30.37.2:9000");
  402. var request = new RestRequest("exec")
  403. .AddParameter("limit", "0,1000")
  404. .AddParameter("explain", "true")
  405. .AddParameter("count", "true")
  406. .AddParameter("src", "con")
  407. .AddParameter("query", query)
  408. .AddParameter("timings", "true");
  409. var response = client.Get<QuestDbResponse>(request);
  410. Console.WriteLine(response.ddl);
  411. var res = APICaller.CallApi1<QuestDbResponse>("http://10.30.37.2:9000" + "/exec", $"?limit=0,1000&explain=true&count=true&src=con&query={query}&timings=true", "GET");
  412. return true;
  413. }
  414. }
  415. /// <summary>
  416. /// QuestDb接口返回结果
  417. /// </summary>
  418. public class QuestDbResponse
  419. {
  420. /// <summary>
  421. /// 执行INSERT语句成功后会返回OK
  422. /// </summary>
  423. public string ddl { get; set; } = "fail";
  424. }
  425. public class QuestDbRequest
  426. {
  427. /// <summary>
  428. /// 分页,limit=0,20相当于返回前20行
  429. /// </summary>
  430. public string limit { get; set; }
  431. /// <summary>
  432. /// 异常时,返回详细的信息
  433. /// </summary>
  434. public string explain { get; set; }
  435. /// <summary>
  436. /// 返回执行SQL获取数据的数量
  437. /// </summary>
  438. public string count { get; set; }
  439. /// <summary>
  440. /// 执行INSERT语句成功后会返回OK
  441. /// </summary>
  442. public string src { get; set; }
  443. /// <summary>
  444. /// 需要执行的SQL语句
  445. /// </summary>
  446. public string query { get; set; }
  447. /// <summary>
  448. /// 详细的执行耗时信息
  449. /// </summary>
  450. public string timings { get; set; }
  451. }
  452. }