MainSysyem.cs 22 KB

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