BindMaterialStep.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package com.mes.step;
  2. import com.mes.core.StationContext;
  3. import com.mes.tcp.MessageSender;
  4. /**
  5. * 物料绑定步骤
  6. * 发送MBDW消息将物料与工件绑定
  7. */
  8. public class BindMaterialStep extends AbstractStep {
  9. private MessageSender messageSender;
  10. private boolean requireValidation = true; // 是否需要先校验
  11. public BindMaterialStep() {
  12. super("bind_material", "物料绑定");
  13. this.messageType = "MBDW";
  14. this.async = true; // 需要等待服务端响应
  15. this.requiresUserInteraction = false;
  16. this.craft = "400004"; // 默认工艺号
  17. }
  18. public void setMessageSender(MessageSender messageSender) {
  19. this.messageSender = messageSender;
  20. }
  21. @Override
  22. public void configure(java.util.Map<String, Object> config) {
  23. super.configure(config);
  24. this.requireValidation = getConfigBoolean("require_validation", true);
  25. }
  26. @Override
  27. public boolean execute(StationContext context) {
  28. String productSn = context.getProcessedProductSn();
  29. String materialSn = context.getMaterialSn();
  30. if (productSn == null || productSn.isEmpty()) {
  31. log.error("[{}] 物料绑定失败: 工件码为空", context.getStationCode());
  32. return false;
  33. }
  34. if (materialSn == null || materialSn.isEmpty()) {
  35. log.error("[{}] 物料绑定失败: 物料码为空", context.getStationCode());
  36. return false;
  37. }
  38. // 检查是否需要先校验
  39. if (requireValidation && !context.isMaterialValidated()) {
  40. log.error("[{}] 物料绑定失败: 物料未校验", context.getStationCode());
  41. return false;
  42. }
  43. context.setStatusMessage("正在绑定物料...", 0);
  44. log.info("[{}] 发送物料绑定: product={}, material={}, craft={}",
  45. context.getStationCode(), productSn, materialSn, craft);
  46. // 获取不带后缀的工位号(绑定时使用)
  47. String stationCodeWithoutSuffix = context.getStationCode().replaceAll("[A-Z]$", "");
  48. // 发送绑定消息
  49. if (messageSender != null) {
  50. return messageSender.sendBindMaterial(
  51. productSn,
  52. materialSn,
  53. context.getUser(),
  54. craft,
  55. stationCodeWithoutSuffix
  56. );
  57. } else {
  58. log.error("[{}] MessageSender未设置", context.getStationCode());
  59. return false;
  60. }
  61. }
  62. @Override
  63. public void onServerResponse(StationContext context, String result, String message) {
  64. if ("OK".equalsIgnoreCase(result)) {
  65. context.setMaterialBound(true);
  66. context.setStatusMessage("物料绑定成功", 0);
  67. log.info("[{}] 物料绑定成功", context.getStationCode());
  68. onSuccess(context);
  69. } else {
  70. context.setMaterialBound(false);
  71. String errorMsg = getErrorMessage(result, message);
  72. context.setStatusMessage(errorMsg, -1);
  73. log.warn("[{}] 物料绑定失败: {}", context.getStationCode(), errorMsg);
  74. onFailure(context, errorMsg);
  75. }
  76. }
  77. @Override
  78. protected String getErrorMessage(String resultCode, String message) {
  79. switch (resultCode.toUpperCase()) {
  80. case "BD":
  81. return "绑定失败";
  82. case "AE":
  83. return "物料已绑定其他工件";
  84. case "NE":
  85. return "工件或物料不存在";
  86. default:
  87. return "物料绑定失败: " + resultCode;
  88. }
  89. }
  90. @Override
  91. public boolean canExecute(StationContext context) {
  92. if (requireValidation) {
  93. return context.isMaterialValidated() && !context.isMaterialBound();
  94. } else {
  95. return context.getMaterialSn() != null &&
  96. !context.getMaterialSn().isEmpty() &&
  97. !context.isMaterialBound();
  98. }
  99. }
  100. // Getters & Setters
  101. public boolean isRequireValidation() {
  102. return requireValidation;
  103. }
  104. public void setRequireValidation(boolean requireValidation) {
  105. this.requireValidation = requireValidation;
  106. }
  107. }