package com.mes.interaction.flow;
|
|
import com.mes.device.entity.DeviceConfig;
|
import com.mes.device.service.DeviceInteractionService;
|
import com.mes.device.vo.DevicePlcVO;
|
import com.mes.interaction.DeviceInteraction;
|
import com.mes.interaction.base.InteractionContext;
|
import com.mes.interaction.base.InteractionResult;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 上大车交互实现
|
*/
|
@Component
|
@RequiredArgsConstructor
|
public class LoadVehicleInteraction implements DeviceInteraction {
|
|
private final DeviceInteractionService deviceInteractionService;
|
|
@Override
|
public String getDeviceType() {
|
return DeviceConfig.DeviceType.LOAD_VEHICLE;
|
}
|
|
@Override
|
public InteractionResult execute(InteractionContext context) {
|
try {
|
// 前置条件验证
|
if (context.getCurrentDevice() == null) {
|
return InteractionResult.fail("设备配置不存在");
|
}
|
|
List<String> glassIds = context.getParameters().getGlassIds();
|
if (CollectionUtils.isEmpty(glassIds)) {
|
return InteractionResult.waitResult("未提供玻璃ID,等待输入", null);
|
}
|
|
// 验证玻璃ID格式
|
for (String glassId : glassIds) {
|
if (glassId == null || glassId.trim().isEmpty()) {
|
return InteractionResult.fail("玻璃ID不能为空");
|
}
|
}
|
|
// 构建PLC写入参数
|
Map<String, Object> params = new HashMap<>();
|
params.put("glassIds", glassIds);
|
params.put("positionCode", context.getParameters().getPositionCode());
|
params.put("positionValue", context.getParameters().getPositionValue());
|
params.put("triggerRequest", true);
|
|
// 执行实际的PLC写入操作
|
DevicePlcVO.OperationResult plcResult = deviceInteractionService.executeOperation(
|
context.getCurrentDevice().getId(),
|
"feedGlass",
|
params
|
);
|
|
// 检查PLC写入结果
|
if (plcResult == null || !Boolean.TRUE.equals(plcResult.getSuccess())) {
|
String errorMsg = plcResult != null ? plcResult.getMessage() : "PLC写入操作返回空结果";
|
return InteractionResult.fail("PLC写入失败: " + errorMsg);
|
}
|
|
// 执行上大车操作(数据流转)
|
List<String> copied = new ArrayList<>(glassIds);
|
context.setLoadedGlassIds(copied);
|
context.getSharedData().put("glassesFromVehicle", copied);
|
context.getSharedData().put("loadVehicleTime", System.currentTimeMillis());
|
|
// 后置条件检查
|
if (context.getLoadedGlassIds().isEmpty()) {
|
return InteractionResult.fail("上大车操作失败:玻璃ID列表为空");
|
}
|
|
Map<String, Object> data = new HashMap<>();
|
data.put("loaded", copied);
|
data.put("glassCount", copied.size());
|
data.put("deviceId", context.getCurrentDevice().getId());
|
data.put("deviceCode", context.getCurrentDevice().getDeviceCode());
|
data.put("plcResult", plcResult.getMessage());
|
return InteractionResult.success(data);
|
} catch (Exception e) {
|
return InteractionResult.fail("上大车交互执行异常: " + e.getMessage());
|
}
|
}
|
}
|