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;
|
import com.mes.device.vo.DeviceControlProfile;
|
import com.mes.device.vo.DevicePlcVO;
|
import lombok.RequiredArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 设备交互逻辑实现
|
*/
|
@Slf4j
|
@Service
|
@RequiredArgsConstructor
|
public class DeviceInteractionServiceImpl implements DeviceInteractionService {
|
|
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 = "玻璃上料";
|
if (request.getPositionCode() != null) {
|
opName = opName + "(" + request.getPositionCode() + ")";
|
}
|
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("设备未配置玻璃槽位信息");
|
}
|
List<String> glassIds = request.getGlassIds();
|
Map<String, Object> payload = new HashMap<>();
|
|
// 写入玻璃ID
|
for (int i = 0; i < profile.getGlassSlots().size(); i++) {
|
DeviceControlProfile.GlassSlot slot = profile.getGlassSlots().get(i);
|
String value = (glassIds != null && i < glassIds.size()) ? glassIds.get(i) : "";
|
payload.put(slot.getField(), value);
|
}
|
|
// 写入玻璃数量
|
if (profile.getGlassCountField() != null) {
|
int count = glassIds != null ? glassIds.size() : 0;
|
payload.put(profile.getGlassCountField(), count);
|
}
|
|
// 写入位置
|
if (profile.getPositionField() != null) {
|
Integer positionValue = request.getPositionValue();
|
if (positionValue == null && request.getPositionCode() != null
|
&& profile.getPositionMappings() != null) {
|
positionValue = profile.getPositionMappings().get(request.getPositionCode());
|
}
|
if (positionValue != null) {
|
payload.put(profile.getPositionField(), positionValue);
|
}
|
}
|
|
// 自动触发请求字
|
boolean trigger = request.getTriggerRequest() != null ? request.getTriggerRequest()
|
: Boolean.TRUE.equals(profile.getAutoRequest());
|
if (trigger && profile.getRequestField() != null) {
|
payload.put(profile.getRequestField(), 1);
|
}
|
|
return payload;
|
}
|
}
|