DataCollectionSysyem.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. if (ps.PropertyType == typeof(ProtocolData<WCS_BCR80>[]))
  199. {
  200. if (pack.BCR80.Any()) db.Insertable(pack.BCR80.Select(x => x.Data).ToList()).ExecuteCommand();
  201. }
  202. else if (ps.PropertyType == typeof(ProtocolData<WCS_BCR81>[]))
  203. {
  204. if (pack.BCR81.Any()) db.Insertable(pack.BCR81.Select(x => x.Data).ToList()).ExecuteCommand();
  205. }
  206. else if (ps.PropertyType == typeof(ProtocolData<WCS_BCR83>[]))
  207. {
  208. if (pack.BCR83.Any()) db.Insertable(pack.BCR83.Select(x => x.Data).ToList()).ExecuteCommand();
  209. }
  210. else if (ps.PropertyType == typeof(ProtocolData<WCS_RGV520>[]))
  211. {
  212. if (pack.RGV520.Any()) db.Insertable(pack.RGV520.Select(x => x.Data).ToList()).ExecuteCommand();
  213. }
  214. else if (ps.PropertyType == typeof(ProtocolData<WCS_RGV521>[]))
  215. {
  216. if (pack.RGV521.Any()) db.Insertable(pack.RGV521.Select(x => x.Data).ToList()).ExecuteCommand();
  217. }
  218. else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot520>[]))
  219. {
  220. if (pack.Robot520.Any()) db.Insertable(pack.Robot520.Select(x => x.Data).ToList()).ExecuteCommand();
  221. }
  222. else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot521>[]))
  223. {
  224. if (pack.Robot521.Any()) db.Insertable(pack.Robot521.Select(x => x.Data).ToList()).ExecuteCommand();
  225. }
  226. else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot522>[]))
  227. {
  228. if (pack.Robot522.Any()) db.Insertable(pack.Robot522.Select(x => x.Data).ToList()).ExecuteCommand();
  229. }
  230. else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot530>[]))
  231. {
  232. if (pack.Robot530.Any()) db.Insertable(pack.Robot530.Select(x => x.Data).ToList()).ExecuteCommand();
  233. }
  234. else if (ps.PropertyType == typeof(ProtocolData<WCS_Robot531>[]))
  235. {
  236. if (pack.Robot531.Any()) db.Insertable(pack.Robot531.Select(x => x.Data).ToList()).ExecuteCommand();
  237. }
  238. else if (ps.PropertyType == typeof(ProtocolData<WCS_SRM520>[]))
  239. {
  240. if (pack.SRM520.Any()) db.Insertable(pack.SRM520.Select(x => x.Data).ToList()).ExecuteCommand();
  241. }
  242. else if (ps.PropertyType == typeof(ProtocolData<WCS_SRM521>[]))
  243. {
  244. if (pack.SRM521.Any()) db.Insertable(pack.SRM521.Select(x => x.Data).ToList()).ExecuteCommand();
  245. }
  246. else if (ps.PropertyType == typeof(ProtocolData<WCS_SRM537>[]))
  247. {
  248. if (pack.SRM537.Any()) db.Insertable(pack.SRM537.Select(x => x.Data).ToList()).ExecuteCommand();
  249. }
  250. else if (ps.PropertyType == typeof(ProtocolData<WCS_Station520>[]))
  251. {
  252. if (pack.Station520.Any()) db.Insertable(pack.Station520.Select(x => x.Data).ToList()).ExecuteCommand();
  253. }
  254. else if (ps.PropertyType == typeof(ProtocolData<WCS_Station521>[]))
  255. {
  256. if (pack.Station521.Any()) db.Insertable(pack.Station521.Select(x => x.Data).ToList()).ExecuteCommand();
  257. }
  258. else if (ps.PropertyType == typeof(ProtocolData<WCS_Station523>[]))
  259. {
  260. if (pack.Station523.Any()) db.Insertable(pack.Station523.Select(x => x.Data).ToList()).UseParameter().ExecuteCommand();
  261. }
  262. else if (ps.PropertyType == typeof(ProtocolData<WCS_Station524>[]))
  263. {
  264. if (pack.Station524.Any()) db.Insertable(pack.Station524.Select(x => x.Data).ToList()).UseParameter().ExecuteCommand();
  265. }
  266. else if (ps.PropertyType == typeof(ProtocolData<WCS_Station525>[]))
  267. {
  268. if (pack.Station525.Any()) db.Insertable(pack.Station525.Select(x => x.Data).ToList()).ExecuteCommand();
  269. }
  270. else if (ps.PropertyType == typeof(ProtocolData<WCS_Station90>[]))
  271. {
  272. if (pack.Station90.Any()) db.Insertable(pack.Station90.Select(x => x.Data).ToList()).ExecuteCommand();
  273. }
  274. else if (ps.PropertyType == typeof(ProtocolData<WCS_Station91>[]))
  275. {
  276. if (pack.Station91.Any()) db.Insertable(pack.Station91.Select(x => x.Data).ToList()).ExecuteCommand();
  277. }
  278. else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss520>[]))
  279. {
  280. if (pack.Truss520.Any()) db.Insertable(pack.Truss520.Select(x => x.Data).ToList()).ExecuteCommand();
  281. }
  282. else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss521>[]))
  283. {
  284. if (pack.Truss521.Any()) db.Insertable(pack.Truss521.Select(x => x.Data).ToList()).ExecuteCommand();
  285. }
  286. else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss523>[]))
  287. {
  288. if (pack.Truss523.Any()) db.Insertable(pack.Truss523.Select(x => x.Data).ToList()).ExecuteCommand();
  289. }
  290. else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss530>[]))
  291. {
  292. if (pack.Truss530.Any()) db.Insertable(pack.Truss530.Select(x => x.Data).ToList()).ExecuteCommand();
  293. }
  294. else if (ps.PropertyType == typeof(ProtocolData<WCS_Truss531>[]))
  295. {
  296. if (pack.Truss531.Any()) db.Insertable(pack.Truss531.Select(x => x.Data).ToList()).ExecuteCommand();
  297. }
  298. });
  299. sw4.Stop();
  300. World.Log($"执行SQL耗时:{sw4.ElapsedMilliseconds}");
  301. sw.Stop();
  302. World.Log($"数据采集耗时:{sw.ElapsedMilliseconds}");
  303. }
  304. catch (Exception e)
  305. {
  306. World.Log($"错误内容:{e.Message}");
  307. }
  308. }
  309. public void Set(StringBuilder sql, string cSql)
  310. {
  311. lock (locker)
  312. {
  313. sql.Append(cSql);
  314. }
  315. }
  316. private Type GetProtocolType(Type source)
  317. {
  318. var t = source.GetInterfaces().FirstOrDefault(v => v.GetInterfaces().Any(d => d.Name == "IProtocol"));
  319. var t1 = source.GetInterfaces().FirstOrDefault(v => v.GetInterfaces().Any(d => d.Name == "IProtocol"));
  320. return t;
  321. }
  322. private object AppendLock = new object();
  323. public StringBuilder Append(StringBuilder sql, string value)
  324. {
  325. lock (AppendLock)
  326. {
  327. return sql.Append(value);
  328. }
  329. }
  330. public string GetString(string value)
  331. {
  332. return value.Replace("INSERT INTO ", "")
  333. .Replace(",N'", ",'")
  334. .Replace("\0", "")
  335. .Replace("wcs_", "")
  336. .Replace("(N'", "('") + "\r";
  337. }
  338. }
  339. /// <summary>
  340. /// 设备报警
  341. /// </summary>
  342. public class EquipmentAlarm
  343. {
  344. /// <summary>
  345. /// 设备号
  346. /// </summary>
  347. public string Code { get; set; }
  348. /// <summary>
  349. /// 内容
  350. /// </summary>
  351. public string Msg { get; set; }
  352. /// <summary>
  353. /// 时间
  354. /// </summary>
  355. public DateTime Time { get; set; }
  356. }
  357. /// <summary>
  358. /// 设备状态信息
  359. /// </summary>
  360. public class EquipmentStatus
  361. {
  362. /// <summary>
  363. /// 设备号
  364. /// </summary>
  365. public string Code { get; set; }
  366. /// <summary>
  367. /// 内容
  368. /// </summary>
  369. public string con { get; set; }
  370. /// <summary>
  371. /// 内容
  372. /// </summary>
  373. public int Status { get; set; }
  374. /// <summary>
  375. /// 时间
  376. /// </summary>
  377. public DateTime Time { get; set; }
  378. }
  379. /// <summary>
  380. ///
  381. /// </summary>
  382. /// <typeparam name="T"></typeparam>
  383. public class Quest<T>
  384. {
  385. public T Data { get; set; }
  386. }
  387. }