DataCollectionSysyem.cs 12 KB

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