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 GlassStorageLogicHandler extends BaseDeviceLogicHandler {
|
|
public GlassStorageLogicHandler(DevicePlcOperationService devicePlcOperationService) {
|
super(devicePlcOperationService);
|
}
|
|
@Override
|
public String getDeviceType() {
|
return DeviceConfig.DeviceType.GLASS_STORAGE;
|
}
|
|
@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 "storeGlass":
|
return handleStoreGlass(deviceConfig, params, logicParams);
|
case "retrieveGlass":
|
return handleRetrieveGlass(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 handleStoreGlass(
|
DeviceConfig deviceConfig,
|
Map<String, Object> params,
|
Map<String, Object> logicParams) {
|
|
// 从逻辑参数中获取配置
|
Integer storageCapacity = getLogicParam(logicParams, "storageCapacity", 100);
|
String retrievalMode = getLogicParam(logicParams, "retrievalMode", "FIFO");
|
Boolean autoStore = getLogicParam(logicParams, "autoStore", true);
|
|
// 从运行时参数中获取数据
|
String glassId = (String) params.get("glassId");
|
Integer storagePosition = (Integer) params.get("storagePosition");
|
Boolean triggerRequest = (Boolean) params.getOrDefault("triggerRequest", autoStore);
|
|
// 构建写入数据
|
Map<String, Object> payload = new HashMap<>();
|
|
if (glassId != null) {
|
payload.put("plcGlassId", glassId);
|
}
|
|
if (storagePosition != null) {
|
payload.put("storagePosition", storagePosition);
|
}
|
|
// 自动触发请求
|
if (triggerRequest != null && triggerRequest) {
|
payload.put("plcRequest", 1);
|
}
|
|
log.info("玻璃存储: deviceId={}, glassId={}, position={}",
|
deviceConfig.getId(), glassId, storagePosition);
|
|
return devicePlcOperationService.writeFields(
|
deviceConfig.getId(),
|
payload,
|
"玻璃存储-存储玻璃"
|
);
|
}
|
|
/**
|
* 处理取货操作
|
*/
|
private DevicePlcVO.OperationResult handleRetrieveGlass(
|
DeviceConfig deviceConfig,
|
Map<String, Object> params,
|
Map<String, Object> logicParams) {
|
|
// 从逻辑参数中获取配置
|
String retrievalMode = getLogicParam(logicParams, "retrievalMode", "FIFO");
|
Boolean autoRetrieve = getLogicParam(logicParams, "autoRetrieve", true);
|
|
// 从运行时参数中获取数据
|
Integer storagePosition = (Integer) params.get("storagePosition");
|
String glassId = (String) params.get("glassId");
|
Boolean triggerRequest = (Boolean) params.getOrDefault("triggerRequest", autoRetrieve);
|
|
// 构建写入数据
|
Map<String, Object> payload = new HashMap<>();
|
|
if (storagePosition != null) {
|
payload.put("retrievePosition", storagePosition);
|
}
|
|
if (glassId != null) {
|
payload.put("retrieveGlassId", glassId);
|
}
|
|
// 自动触发请求
|
if (triggerRequest != null && triggerRequest) {
|
payload.put("plcRequest", 1);
|
}
|
|
log.info("玻璃取货: deviceId={}, position={}, glassId={}",
|
deviceConfig.getId(), storagePosition, glassId);
|
|
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 storageCapacity = getLogicParam(logicParams, "storageCapacity", null);
|
if (storageCapacity != null && storageCapacity <= 0) {
|
return "存储容量(storageCapacity)必须大于0";
|
}
|
|
String retrievalMode = getLogicParam(logicParams, "retrievalMode", null);
|
if (retrievalMode != null && !retrievalMode.matches("FIFO|LIFO|RANDOM")) {
|
return "取货模式(retrievalMode)必须是FIFO、LIFO或RANDOM";
|
}
|
|
return null; // 验证通过
|
}
|
|
@Override
|
public String getDefaultLogicParams() {
|
Map<String, Object> defaultParams = new HashMap<>();
|
defaultParams.put("storageCapacity", 100);
|
defaultParams.put("retrievalMode", "FIFO");
|
defaultParams.put("autoStore", true);
|
defaultParams.put("autoRetrieve", true);
|
defaultParams.put("maxRetryCount", 3);
|
|
try {
|
return objectMapper.writeValueAsString(defaultParams);
|
} catch (Exception e) {
|
log.error("生成默认逻辑参数失败", e);
|
return "{}";
|
}
|
}
|
}
|