EntityBase.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using DbCommon.BusinessCore.BaseCore;
  2. using DbCommon.Enties.DbModels;
  3. using HslCommunication.Core;
  4. using HslCommunication.Profinet.Siemens;
  5. using ProjectManagementSystem.Common.Config;
  6. using ProjectManagementSystem.Common.Core;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Data;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace ProjectManagementSystem.DispatchPack.Entity
  14. {
  15. public class EntityBase
  16. {
  17. public int Index { get; set; }
  18. public LineInfo LineInfo { get; set; }
  19. public PackEngine PackEngine { get; set; }
  20. public List<StationInfo> StationInfoList { get; set; }
  21. public virtual void Init()
  22. {
  23. StationInfoList = new List<StationInfo>();
  24. bool hasPLC地址 = LineInfo.DetailTable.Columns.Contains("IP地址");
  25. bool has放行DB = LineInfo.DetailTable.Columns.Contains("放行DB");
  26. bool has行驶状态DB = LineInfo.DetailTable.Columns.Contains("行驶状态DB");
  27. bool has故障DB = LineInfo.DetailTable.Columns.Contains("故障DB");
  28. bool hasAGVDB = LineInfo.DetailTable.Columns.Contains("AGVDB");
  29. for (int i = 0; i < LineInfo.DetailTable.Rows.Count; i++)
  30. {
  31. var row = LineInfo.DetailTable.Rows[i];
  32. StationInfo info = new StationInfo();
  33. string markString = row["工位地标"].ToString();
  34. if (string.IsNullOrEmpty(markString))
  35. {
  36. break;
  37. }
  38. info.LineName = LineInfo.Name;
  39. info.SerialNumber = i;
  40. info.Id = int.Parse(row["工位地标"].ToString());
  41. info.Name = row["工位名称"].ToString();
  42. info.Mark = int.Parse(row["工位地标"].ToString());
  43. string routeString = row["放行路线"].ToString();
  44. info.ReleaseRoute = string.IsNullOrEmpty(routeString) ? 0 : int.Parse(routeString);
  45. PackEngine.StationInfoDict.AddOrUpdate(info.Mark, info, (key, value) => info);
  46. StationInfoList.Add(info);
  47. info.PlcIPAddress = hasPLC地址 ? row["IP地址"].ToString().Trim() : null;
  48. info.PlcRelease = has放行DB ? row["放行DB"].ToString().Trim() : null;
  49. info.PlcAgvStatus = has行驶状态DB ? row["行驶状态DB"].ToString().Trim() : null;
  50. info.PlcAgvFalut = has故障DB ? row["故障DB"].ToString().Trim() : null;
  51. info.PlcAgvId = hasAGVDB ? row["AGVDB"].ToString().Trim() : null;
  52. string plcIpAddr = info.PlcIPAddress;
  53. if (!string.IsNullOrEmpty(plcIpAddr)
  54. && !PackEngine.PlcDict.ContainsKey(plcIpAddr))
  55. {
  56. var plc = new HslCommunication.Profinet.Siemens.SiemensS7Net(SiemensPLCS.S1200, plcIpAddr);
  57. plc.ConnectTimeOut = 500;
  58. plc.ReceiveTimeOut = 500;
  59. plc.SetPersistentConnection();
  60. PackEngine.PlcDict.AddOrUpdate(plcIpAddr, plc, (key, value) => plc);
  61. }
  62. }
  63. }
  64. private void StopPoint()
  65. {
  66. var infoAgv = DeviceControl.Instance.Communication.InfoAgvDictionary.Values.FirstOrDefault(d => d.Mark == LineInfo.Mark && d.InStation);
  67. if(infoAgv != null)
  68. {
  69. var station = StationInfoList.OrderBy(d => d.Priority).FirstOrDefault(d => d.Status == LocationStatus.Empty);
  70. if(station != null)
  71. {
  72. DeviceControl.Instance.ReleaseAgvOnStop(LineInfo.Mark, station.ReleaseRoute, infoAgv.AgvId);
  73. }
  74. }
  75. }
  76. private void PLC()
  77. {
  78. for (int i = 0; i < StationInfoList.Count; i++)
  79. {
  80. var info = StationInfoList[i];
  81. if (string.IsNullOrEmpty(info.PlcIPAddress)
  82. || !PackEngine.PlcDict.ContainsKey(info.PlcIPAddress))
  83. {
  84. continue;
  85. }
  86. var plc = PackEngine.PlcDict[info.PlcIPAddress];
  87. var result = plc.Read(info.PlcRelease, 1);
  88. if (!result.IsSuccess)
  89. {
  90. continue;
  91. }
  92. if (info.Online)
  93. {
  94. byte plcRelease = result.Content[0];
  95. switch (plcRelease)
  96. {
  97. case (byte)DBControlValues.OKRelease:
  98. if (info.HasCar)
  99. {
  100. DeviceControl.Instance.ReleaseAgvOnStop(info.Mark, info.ReleaseRoute, info.AgvId);
  101. }
  102. break;
  103. case (byte)DBControlValues.NGRelease:
  104. if (info.HasCar)
  105. {
  106. DeviceControl.Instance.ReleaseAgvOnStop(info.Mark, info.ReleaseNGRoute, info.AgvId);
  107. }
  108. break;
  109. case (byte)DBControlValues.StopImmediate:
  110. if (!info.HasCar)
  111. {
  112. DeviceControl.Instance.RailAgv_StopButton(info.AgvId);
  113. }
  114. break;
  115. case (byte)DBControlValues.StopRelease:
  116. if (!info.HasCar)
  117. {
  118. DeviceControl.Instance.RailAgv_StartButton(info.AgvId);
  119. }
  120. break;
  121. default:
  122. break;
  123. }
  124. }
  125. byte agvId = StationInfoTools.GetDBAgvId(info);
  126. plc.Write(info.PlcAgvId, agvId);
  127. byte agvStatus = StationInfoTools.GetDBRunValues(info);
  128. plc.Write(info.PlcAgvStatus, agvStatus);
  129. byte agvFalut = StationInfoTools.GetDBAgvFalut(info);
  130. plc.Write(info.PlcAgvFalut, agvFalut);
  131. }
  132. }
  133. private void MES()
  134. {
  135. }
  136. public virtual void CustomUpdate()
  137. {
  138. switch (LineInfo.CommunicationType)
  139. {
  140. case 1:
  141. PLC();
  142. break;
  143. case 2:
  144. MES();
  145. break;
  146. default:
  147. break;
  148. }
  149. }
  150. }
  151. }