92 lines
2.1 KiB
Java
92 lines
2.1 KiB
Java
|
|
package com.tuoheng.machine.instruction;
|
|||
|
|
|
|||
|
|
import lombok.AllArgsConstructor;
|
|||
|
|
import lombok.Builder;
|
|||
|
|
import lombok.Data;
|
|||
|
|
import lombok.NoArgsConstructor;
|
|||
|
|
|
|||
|
|
import java.util.function.Predicate;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 回调配置(用于方法回调和状态回调)
|
|||
|
|
*/
|
|||
|
|
@Data
|
|||
|
|
@Builder
|
|||
|
|
@NoArgsConstructor
|
|||
|
|
@AllArgsConstructor
|
|||
|
|
public class CallbackConfig {
|
|||
|
|
/**
|
|||
|
|
* 监听的MQTT主题
|
|||
|
|
*/
|
|||
|
|
private String topic;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 字段路径(支持嵌套,如 "data.status")
|
|||
|
|
*/
|
|||
|
|
private String fieldPath;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 期望的字段值
|
|||
|
|
*/
|
|||
|
|
private Object expectedValue;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 自定义判断逻辑(如果设置,则优先使用此逻辑)
|
|||
|
|
*/
|
|||
|
|
private Predicate<Object> customPredicate;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 是否可以被短路(跳过此回调)
|
|||
|
|
*/
|
|||
|
|
private boolean canShortCircuit;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 超时时间(毫秒)
|
|||
|
|
*/
|
|||
|
|
private long timeoutMs = 30000;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 判断消息是否匹配
|
|||
|
|
*/
|
|||
|
|
public boolean matches(Object messageBody) {
|
|||
|
|
if (customPredicate != null) {
|
|||
|
|
return customPredicate.test(messageBody);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Object fieldValue = extractFieldValue(messageBody, fieldPath);
|
|||
|
|
return expectedValue == null || expectedValue.equals(fieldValue);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 从消息体中提取字段值
|
|||
|
|
*/
|
|||
|
|
private Object extractFieldValue(Object messageBody, String path) {
|
|||
|
|
if (messageBody == null || path == null) {
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
String[] parts = path.split("\\.");
|
|||
|
|
Object current = messageBody;
|
|||
|
|
|
|||
|
|
for (String part : parts) {
|
|||
|
|
if (current == null) {
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (current instanceof java.util.Map) {
|
|||
|
|
current = ((java.util.Map<?, ?>) current).get(part);
|
|||
|
|
} else {
|
|||
|
|
try {
|
|||
|
|
java.lang.reflect.Field field = current.getClass().getDeclaredField(part);
|
|||
|
|
field.setAccessible(true);
|
|||
|
|
current = field.get(current);
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return current;
|
|||
|
|
}
|
|||
|
|
}
|