| | |
| | | import com.mes.device.entity.DeviceConfig; |
| | | import com.mes.interaction.BaseDeviceLogicHandler; |
| | | import com.mes.device.service.DevicePlcOperationService; |
| | | import com.mes.device.service.GlassInfoService; |
| | | import com.mes.device.vo.DevicePlcVO; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Qualifier; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.ArrayList; |
| | |
| | | @Component |
| | | public class LoadVehicleLogicHandler extends BaseDeviceLogicHandler { |
| | | |
| | | public LoadVehicleLogicHandler(DevicePlcOperationService devicePlcOperationService) { |
| | | private final GlassInfoService glassInfoService; |
| | | |
| | | public LoadVehicleLogicHandler( |
| | | DevicePlcOperationService devicePlcOperationService, |
| | | @Qualifier("deviceGlassInfoService") GlassInfoService glassInfoService) { |
| | | super(devicePlcOperationService); |
| | | this.glassInfoService = glassInfoService; |
| | | } |
| | | |
| | | @Override |
| | |
| | | return handleTriggerReport(deviceConfig, params, logicParams); |
| | | case "reset": |
| | | return handleReset(deviceConfig, params, logicParams); |
| | | case "clearGlass": |
| | | case "clearPlc": |
| | | case "clear": |
| | | return handleClearGlass(deviceConfig, params, logicParams); |
| | | default: |
| | | log.warn("不支持的操作类型: {}", operation); |
| | | return DevicePlcVO.OperationResult.builder() |
| | |
| | | ); |
| | | } |
| | | |
| | | /** |
| | | * 清空PLC中的玻璃数据 |
| | | */ |
| | | private DevicePlcVO.OperationResult handleClearGlass( |
| | | DeviceConfig deviceConfig, |
| | | Map<String, Object> params, |
| | | Map<String, Object> logicParams) { |
| | | |
| | | Map<String, Object> payload = new HashMap<>(); |
| | | |
| | | int slotCount = getLogicParam(logicParams, "glassSlotCount", 6); |
| | | if (slotCount <= 0) { |
| | | slotCount = 6; |
| | | } |
| | | |
| | | List<String> slotFields = resolveGlassSlotFields(logicParams, slotCount); |
| | | for (String field : slotFields) { |
| | | payload.put(field, ""); |
| | | } |
| | | |
| | | payload.put("plcGlassCount", 0); |
| | | payload.put("plcRequest", 0); |
| | | payload.put("plcReport", 0); |
| | | |
| | | if (params != null && params.containsKey("positionValue")) { |
| | | payload.put("inPosition", params.get("positionValue")); |
| | | } else if (params != null && Boolean.TRUE.equals(params.get("clearPosition"))) { |
| | | payload.put("inPosition", 0); |
| | | } |
| | | |
| | | log.info("清空上大车PLC玻璃数据: deviceId={}, clearedSlots={}", deviceConfig.getId(), slotFields.size()); |
| | | |
| | | return devicePlcOperationService.writeFields( |
| | | deviceConfig.getId(), |
| | | payload, |
| | | "上大车-清空玻璃数据" |
| | | ); |
| | | } |
| | | |
| | | private List<String> resolveGlassSlotFields(Map<String, Object> logicParams, int fallbackCount) { |
| | | List<String> fields = new ArrayList<>(); |
| | | if (logicParams != null) { |
| | | Object slotFieldConfig = logicParams.get("glassSlotFields"); |
| | | if (slotFieldConfig instanceof List) { |
| | | List<?> configured = (List<?>) slotFieldConfig; |
| | | for (Object item : configured) { |
| | | if (item != null) { |
| | | String fieldName = String.valueOf(item).trim(); |
| | | if (!fieldName.isEmpty()) { |
| | | fields.add(fieldName); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (fields.isEmpty()) { |
| | | for (int i = 1; i <= fallbackCount; i++) { |
| | | fields.add("plcGlassId" + i); |
| | | } |
| | | } |
| | | return fields; |
| | | } |
| | | |
| | | @Override |
| | | public String validateLogicParams(DeviceConfig deviceConfig) { |
| | | Map<String, Object> logicParams = parseLogicParams(deviceConfig); |
| | |
| | | |
| | | if (result.isEmpty()) { |
| | | List<String> glassIds = (List<String>) params.get("glassIds"); |
| | | if (glassIds != null) { |
| | | if (glassIds != null && !glassIds.isEmpty()) { |
| | | // 从数据库查询玻璃尺寸 |
| | | Map<String, Integer> lengthMap = glassInfoService.getGlassLengthMap(glassIds); |
| | | for (String glassId : glassIds) { |
| | | result.add(new GlassInfo(glassId, null)); |
| | | Integer length = lengthMap.get(glassId); |
| | | result.add(new GlassInfo(glassId, length)); |
| | | } |
| | | log.debug("从数据库查询玻璃尺寸: glassIds={}, lengthMap={}", glassIds, lengthMap); |
| | | } |
| | | } |
| | | return result; |