TaskBookBehavior_PLC.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using PmsSecondaryPackaging.Interface.Model.Carrier;
  2. using PmsSecondaryPackaging.Interface.Model.StepBook;
  3. using PmsSecondaryPackaging.Interface.Model.TaskBook;
  4. using PmsSecondaryPackaging.TaskManager.TaskBookBehavior;
  5. using ProjectManagementSystem.Common.Config;
  6. using ProjectManagementSystem.Common.Log;
  7. using ProjectManagementSystem.Common.Core;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace ProjectManagementSystem.TaskBookEvent
  14. {
  15. /// <summary>
  16. /// PLC交互行为码,对接地址动态扩展
  17. /// </summary>
  18. public class TaskBookBehavior_PLC : BaseTaskBookBehavior
  19. {
  20. private DateTime LastStepBookExecuting { get; set; }
  21. public PlcBehavior PlcBehavior { get; set; }
  22. public TaskBookBehavior_PLC(int behavior) : base(behavior)
  23. {
  24. PlcBehavior = ExcelConfig.Instance.PlcBehaviorList.FirstOrDefault(d => d.Behavior == behavior);
  25. }
  26. public override bool TaskBookOperator(TaskDetailInfoModel taskDetailInfo, List<StepDetailInfoModel> taskStepInfoList, CarrierInfoModel carrierInfo)
  27. {
  28. try
  29. {
  30. if (Math.Abs((DateTime.Now - LastStepBookExecuting).TotalSeconds) < 1) return false;
  31. LastStepBookExecuting = DateTime.Now;
  32. var stepInfo = TaskBookRegister.GetCurrentStepDetailInfo(taskDetailInfo, taskStepInfoList);
  33. //写入
  34. if (!Interaction(0, PlcBehavior.DbAddressWrite, PlcBehavior.DbValueWrite, taskDetailInfo, stepInfo))
  35. {
  36. return false;
  37. }
  38. //读取
  39. if (!Interaction(1, PlcBehavior.DbAddressRead, PlcBehavior.DbValueRead, taskDetailInfo, stepInfo))
  40. {
  41. return false;
  42. }
  43. //最终写入
  44. if (!Interaction(0, PlcBehavior.DbAddressFinalWrite, PlcBehavior.DbValueFinalWrite, taskDetailInfo, stepInfo))
  45. {
  46. return false;
  47. }
  48. TaskBookRegister.Log(taskDetailInfo, stepInfo, GetLog(taskDetailInfo, stepInfo));
  49. return true;
  50. }
  51. catch (Exception ex)
  52. {
  53. TaskBookRegister.LogException(ex);
  54. }
  55. return false;
  56. }
  57. private bool Interaction(int flag, string[] addrArray, bool[] valueArray, TaskDetailInfoModel taskDetailInfo, StepDetailInfoModel taskStepInfo)
  58. {
  59. if (addrArray.Length <= 0) return true;
  60. var routeConfig = ExcelConfig.Instance.GetRouteConfig(taskStepInfo.WareHouseID);
  61. if (routeConfig == null)
  62. {
  63. TaskBookRegister.Log(taskDetailInfo, taskStepInfo, "查找库位配置信息失败");
  64. return false;
  65. }
  66. if (string.IsNullOrEmpty(routeConfig.PlcIpAddr))
  67. {
  68. TaskBookRegister.Log(taskDetailInfo, taskStepInfo, "PlcIp地址为空");
  69. return false;
  70. }
  71. var plc = PlcControl.Instance.GetPlcReadWrite(routeConfig.PlcIpAddr);
  72. for (int i = 0; i < addrArray.Length; i++)
  73. {
  74. string addrDesc = addrArray[i];
  75. string plcAddr = PlcControl.Instance.GetPlcTaskAddr(taskStepInfo.WareHouseID, addrDesc);
  76. if (string.IsNullOrEmpty(plcAddr))
  77. {
  78. TaskBookRegister.Log(taskDetailInfo, taskStepInfo, $"查找地址失败:“{addrDesc}”");
  79. return false;
  80. }
  81. bool plcValue = GetArrayValue(valueArray, i);
  82. if(flag == 0)
  83. {
  84. if (!WriteBool(plc, plcAddr, plcValue, taskDetailInfo, taskStepInfo))
  85. {
  86. return false;
  87. }
  88. }
  89. else
  90. {
  91. if(!ReadBool(plc, plcAddr, plcValue, taskDetailInfo, taskStepInfo))
  92. {
  93. return false;
  94. }
  95. }
  96. }
  97. return true;
  98. }
  99. private bool WriteBool(HslCommunication.Core.IReadWriteNet plc, string plcAddr, bool plcValue, TaskDetailInfoModel taskDetailInfo, StepDetailInfoModel taskStepInfo)
  100. {
  101. var result = plc.Write(plcAddr, plcValue);
  102. if (!result.IsSuccess)
  103. {
  104. TaskBookRegister.Log(taskDetailInfo, taskStepInfo, $"写入失败({plcAddr}写入{plcValue})");
  105. return false;
  106. }
  107. var readResult = plc.ReadBool(plcAddr);
  108. if (readResult.IsSuccess)
  109. {
  110. if (readResult.Content != plcValue)
  111. {
  112. TaskBookRegister.Log(taskDetailInfo, taskStepInfo, $"写入检查失败(读取{plcAddr}!={plcValue})");
  113. return false;
  114. }
  115. }
  116. else
  117. {
  118. TaskBookRegister.Log(taskDetailInfo, taskStepInfo, $"写入检查失败(读取{plcAddr}失败)");
  119. return false;
  120. }
  121. return true;
  122. }
  123. private bool ReadBool(HslCommunication.Core.IReadWriteNet plc, string plcAddr, bool plcValue, TaskDetailInfoModel taskDetailInfo, StepDetailInfoModel taskStepInfo)
  124. {
  125. var result = plc.ReadBool(plcAddr);
  126. if (!result.IsSuccess
  127. || result.Content != plcValue)
  128. {
  129. TaskBookRegister.Log(taskDetailInfo, taskStepInfo, $"等待信号({plcAddr}={plcValue})");
  130. return false;
  131. }
  132. return true;
  133. }
  134. private string GetLog(TaskDetailInfoModel taskDetailInfo, StepDetailInfoModel taskStepInfo)
  135. {
  136. StringBuilder sb = new StringBuilder();
  137. sb.Append(PlcBehavior.Remark);
  138. sb.Append("【");
  139. for (int i = 0; i < PlcBehavior.DbAddressWrite.Length; i++)
  140. {
  141. string plcAddr = PlcControl.Instance.GetPlcTaskAddr(taskStepInfo.WareHouseID, PlcBehavior.DbAddressWrite[i]);
  142. bool plcValue = GetArrayValue(PlcBehavior.DbValueWrite, i);
  143. sb.Append($" {plcAddr}已写{plcValue};");
  144. }
  145. for (int i = 0; i < PlcBehavior.DbAddressRead.Length; i++)
  146. {
  147. string plcAddr = PlcControl.Instance.GetPlcTaskAddr(taskStepInfo.WareHouseID, PlcBehavior.DbAddressRead[i]);
  148. bool plcValue = GetArrayValue(PlcBehavior.DbValueRead, i);
  149. sb.Append($" 已等待{plcAddr}={plcValue};");
  150. }
  151. for (int i = 0; i < PlcBehavior.DbAddressFinalWrite.Length; i++)
  152. {
  153. string plcAddr = PlcControl.Instance.GetPlcTaskAddr(taskStepInfo.WareHouseID, PlcBehavior.DbAddressFinalWrite[i]);
  154. bool plcValue = GetArrayValue(PlcBehavior.DbValueFinalWrite, i);
  155. sb.Append($" {plcAddr}已最终写{plcValue};");
  156. }
  157. sb.Append("】");
  158. return sb.ToString();
  159. }
  160. private bool GetArrayValue(bool[] array, int index)
  161. {
  162. if (array.Length == 0) return false;
  163. return array.Length > index ? array[index] : array[array.Length - 1];
  164. }
  165. }
  166. }