DataCollectionSysyem.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. using Newtonsoft.Json;
  2. using PlcSiemens.Core.Extension;
  3. using ServiceCenter.Extensions;
  4. using ServiceCenter.Redis;
  5. using ServiceCenter.SqlSugars;
  6. using SqlSugar;
  7. using System.Collections.Concurrent;
  8. using System.ComponentModel;
  9. using System.Diagnostics;
  10. using System.Text;
  11. using WCS.Core;
  12. using WCS.Entity.Protocol.BCR;
  13. using WCS.Entity.Protocol.DataStructure;
  14. using WCS.Entity.Protocol.RGV;
  15. using WCS.Entity.Protocol.Robot;
  16. using WCS.Entity.Protocol.SRM;
  17. using WCS.Entity.Protocol.Station;
  18. using WCS.Entity.Protocol.Truss;
  19. using WCS.WorkEngineering.Extensions;
  20. using WCS.WorkEngineering.Worlds;
  21. namespace WCS.WorkEngineering.Systems
  22. {
  23. /// <summary>
  24. /// 数据采集系统
  25. /// </summary>
  26. [BelongTo(typeof(MainWorld))]
  27. [Description("数据采集系统")]
  28. public class DataCollectionSysyem : DeviceSystem<Device<IStation520>>
  29. {
  30. public static DeviceDataPack pack = new DeviceDataPack();
  31. private static object locker = new object();
  32. public DataCollectionSysyem()
  33. {
  34. 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 }))
  35. .GroupBy(v => v.DB);
  36. foreach (var g in gs)
  37. {
  38. var min = g.OrderBy(v => v.Position).First();
  39. var max = g.OrderByDescending(v => v.Position).First();
  40. var t = Type.GetType(min.TypeStr);
  41. min.Dev.Protocol(t, this.World);
  42. max.Dev.Protocol(t, this.World);
  43. }
  44. }
  45. /// <summary>
  46. /// 所有设备数据
  47. /// Key 是不同设备所使用的类型 例如DeviceDataCollection<SRMData>
  48. /// value 不同设备的具体数据
  49. /// </summary>
  50. public static ConcurrentDictionary<string, DeviceData> AllDatas = new ConcurrentDictionary<string, DeviceData>();
  51. protected override bool ParallelDo => true;
  52. protected override bool SaveLogsToFile => true;
  53. public override bool Select(Device dev)
  54. {
  55. return dev.Code == "1";
  56. }
  57. public override void Do(Device<IStation520> objDev)
  58. {
  59. var sql = new StringBuilder();
  60. try
  61. {
  62. var sw = new Stopwatch();
  63. sw.Start();
  64. var pack = new DeviceDataPack();
  65. var frame = DateTime.Now;
  66. pack.Frame = World.Frame;
  67. sql.Append("INSERT INTO ");
  68. var ps = pack.GetType().GetProperties().OrderBy(x => x.Name);
  69. var db = new SqlSugarHelper().PLC;
  70. Parallel.ForEach(ps, p =>
  71. {
  72. if (!p.PropertyType.IsArray) return;
  73. var t = p.PropertyType.GetElementType();
  74. if (t.IsGenericType)
  75. {
  76. var entType = t.GetGenericArguments()[0];
  77. var protType = GetProtocolType(entType);
  78. if (protType == null) return;
  79. var devices = Device.All.Where(v => v.HasProtocol(protType));
  80. List<object> arr = new List<object>();
  81. Parallel.ForEach(devices, x =>
  82. {
  83. try
  84. {
  85. var protObj = x.Protocol(protType, World) as ProtocolProxyBase;
  86. if (protObj.Frame < DateTime.Now.AddYears(-24))
  87. {
  88. protObj.Frame = frame;
  89. }
  90. if (protObj.Db.failed)
  91. {
  92. return;
  93. }
  94. var obj = Activator.CreateInstance(t);
  95. t.GetProperty("Code").SetValue(obj, x.Code);
  96. var value = WCS.Core.Extentions.Copy(protObj, entType, frame);
  97. t.GetProperty("Data").SetValue(obj, value);
  98. t.GetProperty("Frame").SetValue(obj, protObj.Frame);
  99. entType.GetProperty("Code").SetValue(value, x.Code);
  100. arr.Add(obj);
  101. }
  102. catch
  103. {
  104. }
  105. });
  106. var m = typeof(Enumerable).GetMethod("OfType",
  107. System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
  108. m = m.MakeGenericMethod(t);
  109. var arr2 = m.Invoke(null, new object[] { arr });
  110. m = typeof(Enumerable).GetMethod("ToArray",
  111. System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
  112. m = m.MakeGenericMethod(t);
  113. var arr3 = m.Invoke(null, new object[] { arr2 });
  114. p.SetValue(pack, arr3);
  115. }
  116. });
  117. var sw3 = new Stopwatch();
  118. sw3.Start();
  119. //开始存储设备信息
  120. RedisHub.Monitor.RPush("Packs", pack);
  121. if (RedisHub.Monitor.LLen("Packs") > 50000)
  122. {
  123. RedisHub.Monitor.LTrim("Packs", 5000, -1);
  124. }
  125. #region 存储设备报警信息
  126. List<EquipmentAlarm> equipmentAlarms = new List<EquipmentAlarm>();
  127. equipmentAlarms.AddRange(pack.Robot522.Where(x => x.Data.Alarm != 0).Select(x => new EquipmentAlarm()
  128. {
  129. Code = x.Code,
  130. Msg = x.Data.Alarm.ToString(),
  131. Time = x.Frame
  132. }));
  133. equipmentAlarms.AddRange(pack.SRM537.Where(x => x.Data.Alarm != 0).Select(x => new EquipmentAlarm()
  134. {
  135. Code = x.Code,
  136. Msg = x.Data.Alarm.ToString(),
  137. Time = x.Frame
  138. }));
  139. equipmentAlarms.AddRange(pack.Station523.Where(x => x.Data.Alarm != 0).Select(x => new EquipmentAlarm()
  140. {
  141. Code = x.Code,
  142. Msg = x.Data.Alarm.ToString(),
  143. Time = x.Frame
  144. }));
  145. equipmentAlarms.AddRange(pack.Truss523.Where(x => x.Data.Alarm != 0).Select(x => new EquipmentAlarm()
  146. {
  147. Code = x.Code,
  148. Msg = x.Data.Alarm.ToString(),
  149. Time = x.Frame
  150. }));
  151. RedisHub.Default.Set(nameof(EquipmentAlarm), JsonConvert.SerializeObject(equipmentAlarms));
  152. #endregion 存储设备报警信息
  153. #region 存储设备状态信息
  154. List<EquipmentStatus> equipmentStatus = new List<EquipmentStatus>();
  155. equipmentStatus.AddRange(pack.RGV521.Where(x => x.Data.WorkMode != 0).Select(x => new EquipmentStatus()
  156. {
  157. Code = x.Code,
  158. con = x.Data.WorkMode.GetDescription(),
  159. Status = x.Data.WorkMode.ToInt(),
  160. Time = x.Frame
  161. }));
  162. equipmentStatus.AddRange(pack.Robot521.Where(x => x.Data.RobotMode != 0).Select(x => new EquipmentStatus()
  163. {
  164. Code = x.Code,
  165. con = x.Data.RobotMode.GetDescription(),
  166. Status = x.Data.RobotMode.ToInt(),
  167. Time = x.Frame
  168. }));
  169. equipmentStatus.AddRange(pack.SRM521.Where(x => x.Data.AutoStatus != 0).Select(x => new EquipmentStatus()
  170. {
  171. Code = x.Code,
  172. con = x.Data.AutoStatus.GetDescription(),
  173. Status = x.Data.AutoStatus.ToInt(),
  174. Time = x.Frame
  175. }));
  176. equipmentStatus.AddRange(pack.Station521.Where(x => x.Data.Mode != 0).Select(x => new EquipmentStatus()
  177. {
  178. Code = x.Code,
  179. con = x.Data.Mode.GetDescription(),
  180. Status = x.Data.Mode.ToInt(),
  181. Time = x.Frame
  182. }));
  183. equipmentStatus.AddRange(pack.Truss521.Where(x => x.Data.Status != 0).Select(x => new EquipmentStatus()
  184. {
  185. Code = x.Code,
  186. con = x.Data.Status.GetDescription(),
  187. Status = x.Data.Status.ToInt(),
  188. Time = x.Frame
  189. }));
  190. RedisHub.Default.Set(nameof(EquipmentStatus), JsonConvert.SerializeObject(equipmentStatus));
  191. #endregion 存储设备状态信息
  192. sw3.Stop();
  193. World.Log($"redis存储耗时:{sw3.ElapsedMilliseconds}");
  194. var sw4 = new Stopwatch();
  195. sw4.Start();
  196. Parallel.ForEach(pack.GetType().GetProperties().OrderBy(x => x.Name), ps =>
  197. {
  198. try
  199. {
  200. if (ps.PropertyType == typeof(ProtocolData<WCS_BCR80>[]))
  201. {
  202. if (pack.BCR80.Any())
  203. db.Insertable(pack.BCR80.Select(x => x.Data).ToList()).ExecuteCommand();
  204. }
  205. else if (ps.PropertyType == typeof(ProtocolData<WCS_BCR81>[]))
  206. {
  207. if (pack.BCR81.Any())
  208. db.Insertable(pack.BCR81.Select(x => x.Data).ToList()).ExecuteCommand();
  209. }
  210. else if (ps.PropertyType == typeof(ProtocolData<WCS_BCR83>[]))
  211. {
  212. if (pack.BCR83.Any())
  213. db.Insertable(pack.BCR83.Select(x => x.Data).ToList()).ExecuteCommand();
  214. }
  215. else if (ps.PropertyType == typeof(ProtocolData<WCS_RGV520>[]))
  216. {
  217. if (pack.RGV520.Any())
  218. db.Insertable(pack.RGV520.Select(x => x.Data).ToList()).ExecuteCommand();
  219. }
  220. else if (ps.PropertyType == typeof(ProtocolData<WCS_RGV521>[]))
  221. {
  222. if (pack.RGV521.Any())
  223. db.Insertable(pack.RGV521.Select(x => x.Data).ToList()).ExecuteCommand();
  224. }
  225. else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot520>[]))
  226. {
  227. if (pack.Robot520.Any())
  228. db.Insertable(pack.Robot520.Select(x => x.Data).ToList()).ExecuteCommand();
  229. }
  230. else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot521>[]))
  231. {
  232. if (pack.Robot521.Any())
  233. db.Insertable(pack.Robot521.Select(x => x.Data).ToList()).ExecuteCommand();
  234. }
  235. else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot522>[]))
  236. {
  237. if (pack.Robot522.Any())
  238. db.Insertable(pack.Robot522.Select(x => x.Data).ToList()).ExecuteCommand();
  239. }
  240. else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot530>[]))
  241. {
  242. if (pack.Robot530.Any())
  243. db.Insertable(pack.Robot530.Select(x => x.Data).ToList()).ExecuteCommand();
  244. }
  245. else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot531>[]))
  246. {
  247. if (pack.Robot531.Any())
  248. db.Insertable(pack.Robot531.Select(x => x.Data).ToList()).ToSqlString();
  249. }
  250. else if (ps.PropertyType == typeof(ProtocolData<WCS_SRM520>[]))
  251. {
  252. if (pack.SRM520.Any())
  253. db.Insertable(pack.SRM520.Select(x => x.Data).ToList()).ExecuteCommand();
  254. }
  255. else if (ps.PropertyType == typeof(ProtocolData<WCS_SRM521>[]))
  256. {
  257. if (pack.SRM521.Any())
  258. db.Insertable(pack.SRM521.Select(x => x.Data).ToList()).ExecuteCommand();
  259. }
  260. else if (ps.PropertyType == typeof(ProtocolData<WCS_SRM537>[]))
  261. {
  262. if (pack.SRM537.Any())
  263. db.Insertable(pack.SRM537.Select(x => x.Data).Where(x => x.Alarm == 0).ToList()).ExecuteCommand();
  264. }
  265. else if (ps.PropertyType == typeof(ProtocolData<WCS_Station520>[]))
  266. {
  267. if (pack.Station520.Any())
  268. db.Insertable(pack.Station520.Select(x => x.Data).ToList()).ExecuteCommand();
  269. }
  270. else if (ps.PropertyType == typeof(ProtocolData<WCS_Station521>[]))
  271. {
  272. if (pack.Station521.Any())
  273. db.Insertable(pack.Station521.Select(x => x.Data).ToList()).ExecuteCommand();
  274. }
  275. else if (ps.PropertyType == typeof(ProtocolData<WCS_Station523>[]))
  276. {
  277. if (pack.Station523.Any())
  278. db.Insertable(pack.Station523.Select(x => x.Data).ToList()).UseParameter()
  279. .ExecuteCommand();
  280. }
  281. else if (ps.PropertyType == typeof(ProtocolData<WCS_Station524>[]))
  282. {
  283. if (pack.Station524.Any())
  284. db.Insertable(pack.Station524.Select(x => x.Data).ToList()).UseParameter()
  285. .ExecuteCommand();
  286. }
  287. else if (ps.PropertyType == typeof(ProtocolData<WCS_Station525>[]))
  288. {
  289. if (pack.Station525.Any())
  290. db.Insertable(pack.Station525.Select(x => x.Data).ToList()).ExecuteCommand();
  291. }
  292. else if (ps.PropertyType == typeof(ProtocolData<WCS_Station90>[]))
  293. {
  294. if (pack.Station90.Any())
  295. db.Insertable(pack.Station90.Select(x => x.Data).ToList()).ExecuteCommand();
  296. }
  297. else if (ps.PropertyType == typeof(ProtocolData<WCS_Station91>[]))
  298. {
  299. if (pack.Station91.Any())
  300. db.Insertable(pack.Station91.Select(x => x.Data).ToList()).ExecuteCommand();
  301. }
  302. else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss520>[]))
  303. {
  304. if (pack.Truss520.Any())
  305. db.Insertable(pack.Truss520.Select(x => x.Data).ToList()).ExecuteCommand();
  306. }
  307. else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss521>[]))
  308. {
  309. if (pack.Truss521.Any())
  310. db.Insertable(pack.Truss521.Select(x => x.Data).ToList()).ExecuteCommand();
  311. }
  312. else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss523>[]))
  313. {
  314. if (pack.Truss523.Any())
  315. db.Insertable(pack.Truss523.Select(x => x.Data).ToList()).ExecuteCommand();
  316. }
  317. else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss530>[]))
  318. {
  319. if (pack.Truss530.Any())
  320. db.Insertable(pack.Truss530.Select(x => x.Data).ToList()).ExecuteCommand();
  321. }
  322. else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss531>[]))
  323. {
  324. if (pack.Truss531.Any())
  325. db.Insertable(pack.Truss531.Select(x => x.Data).ToList()).ExecuteCommand();
  326. }
  327. }
  328. catch (Exception e)
  329. {
  330. World.Log($"{e.Message}:{e.StackTrace}");
  331. }
  332. });
  333. sw4.Stop();
  334. World.Log($"执行SQL耗时:{sw4.ElapsedMilliseconds}");
  335. sw.Stop();
  336. World.Log($"数据采集耗时:{sw.ElapsedMilliseconds}");
  337. }
  338. catch (Exception e)
  339. {
  340. World.Log($"错误内容:{e.Message}");
  341. }
  342. }
  343. public void Set(StringBuilder sql, string cSql)
  344. {
  345. lock (locker)
  346. {
  347. sql.Append(cSql);
  348. }
  349. }
  350. private Type GetProtocolType(Type source)
  351. {
  352. var t = source.GetInterfaces().FirstOrDefault(v => v.GetInterfaces().Any(d => d.Name == "IProtocol"));
  353. var t1 = source.GetInterfaces().FirstOrDefault(v => v.GetInterfaces().Any(d => d.Name == "IProtocol"));
  354. return t;
  355. }
  356. private object AppendLock = new object();
  357. public StringBuilder Append(StringBuilder sql, string value)
  358. {
  359. lock (AppendLock)
  360. {
  361. return sql.Append(value);
  362. }
  363. }
  364. public string GetString(string value)
  365. {
  366. return value.Replace("INSERT INTO ", "")
  367. .Replace(",N'", ",'")
  368. .Replace("\0", "")
  369. .Replace("wcs_", "")
  370. .Replace("(N'", "('") + "\r";
  371. }
  372. }
  373. /// <summary>
  374. /// 设备报警
  375. /// </summary>
  376. public class EquipmentAlarm
  377. {
  378. /// <summary>
  379. /// 设备号
  380. /// </summary>
  381. public string Code { get; set; }
  382. /// <summary>
  383. /// 内容
  384. /// </summary>
  385. public string Msg { get; set; }
  386. /// <summary>
  387. /// 时间
  388. /// </summary>
  389. public DateTime Time { get; set; }
  390. }
  391. /// <summary>
  392. /// 设备状态信息
  393. /// </summary>
  394. public class EquipmentStatus
  395. {
  396. /// <summary>
  397. /// 设备号
  398. /// </summary>
  399. public string Code { get; set; }
  400. /// <summary>
  401. /// 内容
  402. /// </summary>
  403. public string con { get; set; }
  404. /// <summary>
  405. /// 内容
  406. /// </summary>
  407. public int Status { get; set; }
  408. /// <summary>
  409. /// 时间
  410. /// </summary>
  411. public DateTime Time { get; set; }
  412. }
  413. /// <summary>
  414. ///
  415. /// </summary>
  416. /// <typeparam name="T"></typeparam>
  417. public class Quest<T>
  418. {
  419. public T Data { get; set; }
  420. }
  421. }