thingsboard-client-demo/src/main/java/com/tuoheng/machine/instruction/Instruction.java

95 lines
2.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.tuoheng.machine.instruction;
/**
* 指令接口
* 一个指令包含四个部分:
* a. 判断是否可以执行该指令
* b. 执行远程调用如MQTT发送
* c. 等待方法回调,并判断该方法是否成功执行
* d. 等待状态回调并判断结果是否OK
*/
public interface Instruction {
/**
* 获取指令名称
*/
String getName();
/**
* a. 判断是否可以执行该指令
*/
boolean canExecute(InstructionContext context);
/**
* b. 执行远程调用如MQTT发送
*/
void executeRemoteCall(InstructionContext context) throws Exception;
/**
* c. 获取方法回调配置(可选)
* 返回null表示不需要方法回调
*/
CallbackConfig getMethodCallbackConfig(InstructionContext context);
/**
* d. 获取状态回调配置(可选)
* 返回null表示不需要状态回调
*/
CallbackConfig getStateCallbackConfig(InstructionContext context);
/**
* 获取指令超时时间(毫秒)
*/
default long getTimeoutMs() {
return 60000; // 默认60秒
}
/**
* 指令执行完成回调(无论成功失败都会调用)
*/
default void onComplete(InstructionContext context, InstructionResult result) {
// 默认空实现
}
/**
* 获取成功后执行的下一个指令
*/
default Instruction getOnSuccessInstruction() {
return null;
}
/**
* 获取失败后执行的下一个指令
*/
default Instruction getOnFailureInstruction() {
return null;
}
/**
* 获取无论成功失败都执行的下一个指令(优先级最高)
*/
default Instruction getAlwaysNextInstruction() {
return null;
}
/**
* 根据执行结果获取下一个指令
*
* @param success 是否成功
* @return 下一个指令如果没有则返回null
*/
default Instruction getNextInstruction(boolean success) {
// 优先返回 always 配置
Instruction alwaysNext = getAlwaysNextInstruction();
if (alwaysNext != null) {
return alwaysNext;
}
// 根据成功失败返回对应的指令
if (success) {
return getOnSuccessInstruction();
} else {
return getOnFailureInstruction();
}
}
}