DataCollectionSysyem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using Newtonsoft.Json;
  2. using PlcSiemens.Core.Extension;
  3. using ServiceCenter.Extensions;
  4. using ServiceCenter.Logs;
  5. using ServiceCenter.Redis;
  6. using ServiceCenter.SqlSugars;
  7. using System.Collections.Concurrent;
  8. using System.ComponentModel;
  9. using System.Data.SqlTypes;
  10. using System.Diagnostics;
  11. using System.Text;
  12. using WCS.Core;
  13. using WCS.Entity.Protocol.BCR;
  14. using WCS.Entity.Protocol.DataStructure;
  15. using WCS.Entity.Protocol.RGV;
  16. using WCS.Entity.Protocol.Robot;
  17. using WCS.Entity.Protocol.SRM;
  18. using WCS.Entity.Protocol.Station;
  19. using WCS.Entity.Protocol.Truss;
  20. using WCS.WorkEngineering.Extensions;
  21. using WCS.WorkEngineering.Worlds;
  22. namespace WCS.WorkEngineering.Systems
  23. {
  24. /// <summary>
  25. /// 数据采集
  26. /// </summary>
  27. [BelongTo(typeof(DataWorld))]
  28. [Description("数据采集系统")]
  29. public class DataCollectionSysyem : SystemBase
  30. {
  31. public static DeviceDataPack pack = new DeviceDataPack();
  32. private static object locker = new object();
  33. public DataCollectionSysyem()
  34. {
  35. 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 }))
  36. .GroupBy(v => v.DB);
  37. foreach (var g in gs)
  38. {
  39. var min = g.OrderBy(v => v.Position).First();
  40. var max = g.OrderByDescending(v => v.Position).First();
  41. var t = Type.GetType(min.TypeStr);
  42. min.Dev.Protocol(t, this.World);
  43. max.Dev.Protocol(t, this.World);
  44. }
  45. }
  46. public override List<object> GetObjects()
  47. {
  48. return new List<object>();
  49. }
  50. public override void Update(List<WorkTimes> list)
  51. {
  52. try
  53. {
  54. var sw = new Stopwatch();
  55. sw.Start();
  56. #region 处理数据
  57. var sw1 = new Stopwatch();
  58. sw1.Start();
  59. var pack = new DeviceDataPack();
  60. var frame = DateTime.Now;
  61. pack.Frame = World.Frame;
  62. var ps = pack.GetType().GetProperties().OrderBy(x => x.Name);
  63. Parallel.ForEach(ps, p =>
  64. {
  65. if (!p.PropertyType.IsArray) return;
  66. var t = p.PropertyType.GetElementType();
  67. if (t.IsGenericType)
  68. {
  69. var entType = t.GetGenericArguments()[0];
  70. var protType = GetProtocolType(entType);
  71. if (protType == null) return;
  72. var devices = Device.All.Where(v => v.HasProtocol(protType));
  73. List<object> arr = new List<object>();
  74. Parallel.ForEach(devices, x =>
  75. {
  76. try
  77. {
  78. var protObj = x.Protocol(protType, World) as ProtocolProxyBase;
  79. if (protObj.Frame < DateTime.Now.AddYears(-24))
  80. {
  81. protObj.Frame = frame;
  82. }
  83. if (protObj.Db.Failed)
  84. {
  85. return;
  86. }
  87. var obj = Activator.CreateInstance(t);
  88. t.GetProperty("Code").SetValue(obj, x.Code);
  89. var value = WCS.Core.Extentions.Copy(protObj, entType, TimeZoneInfo.ConvertTimeToUtc(frame));
  90. t.GetProperty("Data").SetValue(obj, value);
  91. t.GetProperty("Frame").SetValue(obj, protObj.Frame);
  92. entType.GetProperty("Code").SetValue(value, x.Code);
  93. arr.Add(obj);
  94. }
  95. catch
  96. {
  97. }
  98. });
  99. var m = typeof(Enumerable).GetMethod("OfType",
  100. System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
  101. m = m.MakeGenericMethod(t);
  102. var arr2 = m.Invoke(null, new object[] { arr });
  103. m = typeof(Enumerable).GetMethod("ToArray",
  104. System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
  105. m = m.MakeGenericMethod(t);
  106. var arr3 = m.Invoke(null, new object[] { arr2 });
  107. p.SetValue(pack, arr3);
  108. }
  109. });
  110. sw1.Stop();
  111. #endregion 处理数据
  112. #region 存储监控数据
  113. var sw2 = new Stopwatch();
  114. sw2.Start();
  115. //开始存储设备信息
  116. RedisHub.Monitor.RPush("Packs", pack);
  117. if (RedisHub.Monitor.LLen("Packs") > 70000)
  118. {
  119. RedisHub.Monitor.LTrim("Packs", 5000, -1);
  120. }
  121. sw2.Stop();
  122. #endregion 存储监控数据
  123. #region 存储设备报警信息
  124. var sw3 = new Stopwatch();
  125. sw3.Start();
  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.SRM523.Where(x => x.Data.Alarm1 != 0 || x.Data.Alarm2 != 0).Select(x => new EquipmentAlarm()
  134. {
  135. Code = x.Code,
  136. Msg = $"{Convert.ToString(x.Data.Alarm1)},{Convert.ToString(x.Data.Alarm2)}",
  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. sw3.Stop();
  153. #endregion 存储设备报警信息
  154. #region 存储设备状态信息
  155. var sw4 = new Stopwatch();
  156. sw4.Start();
  157. List<EquipmentStatus> equipmentStatus = new List<EquipmentStatus>();
  158. equipmentStatus.AddRange(pack.RGV521.Where(x => x.Data.WorkMode != 0).Select(x => new EquipmentStatus()
  159. {
  160. Code = x.Code,
  161. con = x.Data.WorkMode.GetDescription(),
  162. Status = x.Data.WorkMode.ToInt(),
  163. Time = x.Frame
  164. }));
  165. equipmentStatus.AddRange(pack.Robot521.Where(x => x.Data.RobotMode != 0).Select(x => new EquipmentStatus()
  166. {
  167. Code = x.Code,
  168. con = x.Data.RobotMode.GetDescription(),
  169. Status = x.Data.RobotMode.ToInt(),
  170. Time = x.Frame
  171. }));
  172. equipmentStatus.AddRange(pack.SRM521.Where(x => x.Data.AutoStatus != 0).Select(x => new EquipmentStatus()
  173. {
  174. Code = x.Code,
  175. con = x.Data.AutoStatus.GetDescription(),
  176. Status = x.Data.AutoStatus.ToInt(),
  177. Time = x.Frame
  178. }));
  179. equipmentStatus.AddRange(pack.Station521.Where(x => x.Data.Mode != 0).Select(x => new EquipmentStatus()
  180. {
  181. Code = x.Code,
  182. con = x.Data.Mode.GetDescription(),
  183. Status = x.Data.Mode.ToInt(),
  184. Time = x.Frame
  185. }));
  186. equipmentStatus.AddRange(pack.Truss521.Where(x => x.Data.Status != 0).Select(x => new EquipmentStatus()
  187. {
  188. Code = x.Code,
  189. con = x.Data.Status.GetDescription(),
  190. Status = x.Data.Status.ToInt(),
  191. Time = x.Frame
  192. }));
  193. RedisHub.Default.Set(nameof(EquipmentStatus), JsonConvert.SerializeObject(equipmentStatus));
  194. sw4.Stop();
  195. #endregion 存储设备状态信息
  196. #region 存储数采数据
  197. var sw5 = new Stopwatch();
  198. sw5.Start();
  199. RedisHub.Monitor.RPush("DataCollectionpacks", pack);
  200. sw5.Stop();
  201. #endregion 存储数采数据
  202. sw.Stop();
  203. World.Log($"业务耗时:[{sw.ElapsedMilliseconds}]--处理数据:[{sw1.ElapsedMilliseconds}]--存储监控数据:[{sw2.ElapsedMilliseconds}]--存储设备报警信息:[{sw3.ElapsedMilliseconds}]--存储设备状态数据:[{sw4.ElapsedMilliseconds}]--存储数采数据:[{sw5.ElapsedMilliseconds}]");
  204. }
  205. catch (Exception e)
  206. {
  207. World.Log($"错误内容:{e.Message}");
  208. }
  209. }
  210. public void Set(StringBuilder sql, string cSql)
  211. {
  212. lock (locker)
  213. {
  214. sql.Append(cSql);
  215. }
  216. }
  217. private Type GetProtocolType(Type source)
  218. {
  219. var t = source.GetInterfaces().FirstOrDefault(v => v.GetInterfaces().Any(d => d.Name == "IProtocol"));
  220. var t1 = source.GetInterfaces().FirstOrDefault(v => v.GetInterfaces().Any(d => d.Name == "IProtocol"));
  221. return t;
  222. }
  223. private object AppendLock = new object();
  224. public StringBuilder Append(StringBuilder sql, string value)
  225. {
  226. lock (AppendLock)
  227. {
  228. return sql.Append(value);
  229. }
  230. }
  231. public string GetString(string value)
  232. {
  233. return value.Replace("INSERT INTO ", "")
  234. .Replace(",N'", ",'")
  235. .Replace("\0", "")
  236. .Replace("wcs_", "")
  237. .Replace("(N'", "('") + "\r";
  238. }
  239. }
  240. /// <summary>
  241. /// 设备报警
  242. /// </summary>
  243. public class EquipmentAlarm
  244. {
  245. /// <summary>
  246. /// 设备号
  247. /// </summary>
  248. public string Code { get; set; }
  249. /// <summary>
  250. /// 内容
  251. /// </summary>
  252. public string Msg { get; set; }
  253. /// <summary>
  254. /// 时间
  255. /// </summary>
  256. public DateTime Time { get; set; }
  257. }
  258. /// <summary>
  259. /// 设备状态信息
  260. /// </summary>
  261. public class EquipmentStatus
  262. {
  263. /// <summary>
  264. /// 设备号
  265. /// </summary>
  266. public string Code { get; set; }
  267. /// <summary>
  268. /// 内容
  269. /// </summary>
  270. public string con { get; set; }
  271. /// <summary>
  272. /// 内容
  273. /// </summary>
  274. public int Status { get; set; }
  275. /// <summary>
  276. /// 时间
  277. /// </summary>
  278. public DateTime Time { get; set; }
  279. }
  280. /// <summary>
  281. ///
  282. /// </summary>
  283. /// <typeparam name="T"></typeparam>
  284. public class Quest<T>
  285. {
  286. public T Data { get; set; }
  287. }
  288. }