| | |
| | | package com.mes.device.service.impl; |
| | | |
| | | import com.mes.device.entity.DeviceConfig; |
| | | import com.mes.interaction.DeviceLogicHandler; |
| | | import com.mes.interaction.DeviceLogicHandlerFactory; |
| | | import com.mes.device.request.DeviceGlassFeedRequest; |
| | | import com.mes.device.service.DeviceConfigService; |
| | | import com.mes.device.service.DeviceControlProfileService; |
| | | import com.mes.device.service.DeviceInteractionService; |
| | | import com.mes.device.service.DevicePlcOperationService; |
| | |
| | | |
| | | private final DeviceControlProfileService controlProfileService; |
| | | private final DevicePlcOperationService devicePlcOperationService; |
| | | private final DeviceConfigService deviceConfigService; |
| | | private final DeviceLogicHandlerFactory handlerFactory; |
| | | |
| | | /** |
| | | * 执行玻璃上料写入(兼容旧接口,保留原有逻辑) |
| | | */ |
| | | @Override |
| | | public DevicePlcVO.OperationResult feedGlass(DeviceGlassFeedRequest request) { |
| | | // 优先使用新的处理器架构 |
| | | DeviceConfig deviceConfig = deviceConfigService.getDeviceById(request.getDeviceId()); |
| | | if (deviceConfig != null) { |
| | | DeviceLogicHandler handler = handlerFactory.getHandler(deviceConfig.getDeviceType()); |
| | | if (handler != null) { |
| | | // 使用新架构执行 |
| | | Map<String, Object> params = new HashMap<>(); |
| | | params.put("glassIds", request.getGlassIds()); |
| | | params.put("positionCode", request.getPositionCode()); |
| | | params.put("positionValue", request.getPositionValue()); |
| | | params.put("triggerRequest", request.getTriggerRequest()); |
| | | return handler.execute(deviceConfig, "feedGlass", params); |
| | | } |
| | | } |
| | | |
| | | // 降级到原有逻辑(兼容旧代码) |
| | | DeviceControlProfile profile = controlProfileService.getProfile(request.getDeviceId()); |
| | | Map<String, Object> payload = buildGlassPayload(profile, request); |
| | | String opName = "玻璃上料"; |
| | |
| | | return devicePlcOperationService.writeFields(request.getDeviceId(), payload, opName); |
| | | } |
| | | |
| | | /** |
| | | * 执行设备逻辑操作(新接口,使用处理器架构) |
| | | */ |
| | | @Override |
| | | public DevicePlcVO.OperationResult executeOperation(Long deviceId, String operation, Map<String, Object> params) { |
| | | // 获取设备配置 |
| | | DeviceConfig deviceConfig = deviceConfigService.getDeviceById(deviceId); |
| | | if (deviceConfig == null) { |
| | | return DevicePlcVO.OperationResult.builder() |
| | | .success(false) |
| | | .message("设备不存在: " + deviceId) |
| | | .build(); |
| | | } |
| | | |
| | | // 获取对应的处理器 |
| | | DeviceLogicHandler handler = handlerFactory.getHandler(deviceConfig.getDeviceType()); |
| | | if (handler == null) { |
| | | return DevicePlcVO.OperationResult.builder() |
| | | .success(false) |
| | | .message("不支持的设备类型: " + deviceConfig.getDeviceType()) |
| | | .build(); |
| | | } |
| | | |
| | | // 执行操作 |
| | | return handler.execute(deviceConfig, operation, params != null ? params : new HashMap<>()); |
| | | } |
| | | |
| | | /** |
| | | * 构建玻璃上料数据(兼容旧逻辑) |
| | | */ |
| | | private Map<String, Object> buildGlassPayload(DeviceControlProfile profile, DeviceGlassFeedRequest request) { |
| | | if (CollectionUtils.isEmpty(profile.getGlassSlots())) { |
| | | throw new IllegalStateException("设备未配置玻璃槽位信息"); |