| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package com.mes.step;
- import com.mes.core.StationContext;
- import com.mes.tcp.MessageSender;
- /**
- * 物料绑定步骤
- * 发送MBDW消息将物料与工件绑定
- */
- public class BindMaterialStep extends AbstractStep {
- private MessageSender messageSender;
- private boolean requireValidation = true; // 是否需要先校验
- public BindMaterialStep() {
- super("bind_material", "物料绑定");
- this.messageType = "MBDW";
- this.async = true; // 需要等待服务端响应
- this.requiresUserInteraction = false;
- this.craft = "400004"; // 默认工艺号
- }
- public void setMessageSender(MessageSender messageSender) {
- this.messageSender = messageSender;
- }
- @Override
- public void configure(java.util.Map<String, Object> config) {
- super.configure(config);
- this.requireValidation = getConfigBoolean("require_validation", true);
- }
- @Override
- public boolean execute(StationContext context) {
- String productSn = context.getProcessedProductSn();
- String materialSn = context.getMaterialSn();
- if (productSn == null || productSn.isEmpty()) {
- log.error("[{}] 物料绑定失败: 工件码为空", context.getStationCode());
- return false;
- }
- if (materialSn == null || materialSn.isEmpty()) {
- log.error("[{}] 物料绑定失败: 物料码为空", context.getStationCode());
- return false;
- }
- // 检查是否需要先校验
- if (requireValidation && !context.isMaterialValidated()) {
- log.error("[{}] 物料绑定失败: 物料未校验", context.getStationCode());
- return false;
- }
- context.setStatusMessage("正在绑定物料...", 0);
- log.info("[{}] 发送物料绑定: product={}, material={}, craft={}",
- context.getStationCode(), productSn, materialSn, craft);
- // 获取不带后缀的工位号(绑定时使用)
- String stationCodeWithoutSuffix = context.getStationCode().replaceAll("[A-Z]$", "");
- // 发送绑定消息
- if (messageSender != null) {
- return messageSender.sendBindMaterial(
- productSn,
- materialSn,
- context.getUser(),
- craft,
- stationCodeWithoutSuffix
- );
- } else {
- log.error("[{}] MessageSender未设置", context.getStationCode());
- return false;
- }
- }
- @Override
- public void onServerResponse(StationContext context, String result, String message) {
- if ("OK".equalsIgnoreCase(result)) {
- context.setMaterialBound(true);
- context.setStatusMessage("物料绑定成功", 0);
- log.info("[{}] 物料绑定成功", context.getStationCode());
- onSuccess(context);
- } else {
- context.setMaterialBound(false);
- String errorMsg = getErrorMessage(result, message);
- context.setStatusMessage(errorMsg, -1);
- log.warn("[{}] 物料绑定失败: {}", context.getStationCode(), errorMsg);
- onFailure(context, errorMsg);
- }
- }
- @Override
- protected String getErrorMessage(String resultCode, String message) {
- switch (resultCode.toUpperCase()) {
- case "BD":
- return "绑定失败";
- case "AE":
- return "物料已绑定其他工件";
- case "NE":
- return "工件或物料不存在";
- default:
- return "物料绑定失败: " + resultCode;
- }
- }
- @Override
- public boolean canExecute(StationContext context) {
- if (requireValidation) {
- return context.isMaterialValidated() && !context.isMaterialBound();
- } else {
- return context.getMaterialSn() != null &&
- !context.getMaterialSn().isEmpty() &&
- !context.isMaterialBound();
- }
- }
- // Getters & Setters
- public boolean isRequireValidation() {
- return requireValidation;
- }
- public void setRequireValidation(boolean requireValidation) {
- this.requireValidation = requireValidation;
- }
- }
|