package com.mes.interaction.impl;
|
|
import com.mes.device.entity.DeviceConfig;
|
import com.mes.interaction.BaseDeviceLogicHandler;
|
import com.mes.device.service.DevicePlcOperationService;
|
import com.mes.device.vo.DevicePlcVO;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Component;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 大理片设备逻辑处理器
|
*
|
* @author mes
|
* @since 2025-01-XX
|
*/
|
@Slf4j
|
@Component
|
public class LargeGlassLogicHandler extends BaseDeviceLogicHandler {
|
|
public LargeGlassLogicHandler(DevicePlcOperationService devicePlcOperationService) {
|
super(devicePlcOperationService);
|
}
|
|
@Override
|
public String getDeviceType() {
|
return DeviceConfig.DeviceType.LARGE_GLASS;
|
}
|
|
@Override
|
protected DevicePlcVO.OperationResult doExecute(
|
DeviceConfig deviceConfig,
|
String operation,
|
Map<String, Object> params,
|
Map<String, Object> logicParams) {
|
|
log.info("执行大理片设备操作: deviceId={}, operation={}", deviceConfig.getId(), operation);
|
|
switch (operation) {
|
case "processGlass":
|
return handleProcessGlass(deviceConfig, params, logicParams);
|
case "triggerRequest":
|
return handleTriggerRequest(deviceConfig, params, logicParams);
|
case "triggerReport":
|
return handleTriggerReport(deviceConfig, params, logicParams);
|
case "reset":
|
return handleReset(deviceConfig, params, logicParams);
|
default:
|
log.warn("不支持的操作类型: {}", operation);
|
return DevicePlcVO.OperationResult.builder()
|
.success(false)
|
.message("不支持的操作: " + operation)
|
.build();
|
}
|
}
|
|
/**
|
* 处理玻璃加工操作
|
*/
|
private DevicePlcVO.OperationResult handleProcessGlass(
|
DeviceConfig deviceConfig,
|
Map<String, Object> params,
|
Map<String, Object> logicParams) {
|
|
// 从逻辑参数中获取配置
|
Integer glassSize = getLogicParam(logicParams, "glassSize", 2000);
|
Integer processingTime = getLogicParam(logicParams, "processingTime", 5000);
|
Boolean autoProcess = getLogicParam(logicParams, "autoProcess", true);
|
|
// 从运行时参数中获取数据
|
String glassId = (String) params.get("glassId");
|
Integer processType = (Integer) params.get("processType");
|
Boolean triggerRequest = (Boolean) params.getOrDefault("triggerRequest", autoProcess);
|
|
// 构建写入数据
|
Map<String, Object> payload = new HashMap<>();
|
|
if (glassId != null) {
|
payload.put("plcGlassId", glassId);
|
}
|
|
if (processType != null) {
|
payload.put("processType", processType);
|
}
|
|
// 自动触发请求
|
if (triggerRequest != null && triggerRequest) {
|
payload.put("plcRequest", 1);
|
}
|
|
log.info("大理片玻璃加工: deviceId={}, glassId={}, processType={}",
|
deviceConfig.getId(), glassId, processType);
|
|
return devicePlcOperationService.writeFields(
|
deviceConfig.getId(),
|
payload,
|
"大理片-玻璃加工"
|
);
|
}
|
|
/**
|
* 处理触发请求操作
|
*/
|
private DevicePlcVO.OperationResult handleTriggerRequest(
|
DeviceConfig deviceConfig,
|
Map<String, Object> params,
|
Map<String, Object> logicParams) {
|
|
Map<String, Object> payload = new HashMap<>();
|
payload.put("plcRequest", 1);
|
|
log.info("大理片触发请求: deviceId={}", deviceConfig.getId());
|
return devicePlcOperationService.writeFields(
|
deviceConfig.getId(),
|
payload,
|
"大理片-触发请求"
|
);
|
}
|
|
/**
|
* 处理触发汇报操作
|
*/
|
private DevicePlcVO.OperationResult handleTriggerReport(
|
DeviceConfig deviceConfig,
|
Map<String, Object> params,
|
Map<String, Object> logicParams) {
|
|
Map<String, Object> payload = new HashMap<>();
|
payload.put("plcReport", 1);
|
|
log.info("大理片触发汇报: deviceId={}", deviceConfig.getId());
|
return devicePlcOperationService.writeFields(
|
deviceConfig.getId(),
|
payload,
|
"大理片-触发汇报"
|
);
|
}
|
|
/**
|
* 处理重置操作
|
*/
|
private DevicePlcVO.OperationResult handleReset(
|
DeviceConfig deviceConfig,
|
Map<String, Object> params,
|
Map<String, Object> logicParams) {
|
|
Map<String, Object> payload = new HashMap<>();
|
payload.put("plcRequest", 0);
|
payload.put("plcReport", 0);
|
|
log.info("大理片重置: deviceId={}", deviceConfig.getId());
|
return devicePlcOperationService.writeFields(
|
deviceConfig.getId(),
|
payload,
|
"大理片-重置"
|
);
|
}
|
|
@Override
|
public String validateLogicParams(DeviceConfig deviceConfig) {
|
Map<String, Object> logicParams = parseLogicParams(deviceConfig);
|
|
// 验证必填参数
|
Integer glassSize = getLogicParam(logicParams, "glassSize", null);
|
if (glassSize != null && glassSize <= 0) {
|
return "玻璃尺寸(glassSize)必须大于0";
|
}
|
|
Integer processingTime = getLogicParam(logicParams, "processingTime", null);
|
if (processingTime != null && processingTime < 0) {
|
return "处理时间(processingTime)不能为负数";
|
}
|
|
return null; // 验证通过
|
}
|
|
@Override
|
public String getDefaultLogicParams() {
|
Map<String, Object> defaultParams = new HashMap<>();
|
defaultParams.put("glassSize", 2000);
|
defaultParams.put("processingTime", 5000);
|
defaultParams.put("autoProcess", true);
|
defaultParams.put("maxRetryCount", 3);
|
|
try {
|
return objectMapper.writeValueAsString(defaultParams);
|
} catch (Exception e) {
|
log.error("生成默认逻辑参数失败", e);
|
return "{}";
|
}
|
}
|
}
|