|
|
@ -5,6 +5,9 @@ import org.springframework.stereotype.Controller; |
|
|
|
import org.springframework.web.bind.annotation.PostMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestBody; |
|
|
|
import org.springframework.web.bind.annotation.ResponseBody; |
|
|
|
|
|
|
|
import java.lang.reflect.InvocationTargetException; |
|
|
|
import java.lang.reflect.Method; |
|
|
|
import java.util.Map; |
|
|
|
@Controller |
|
|
|
public class DiApiTask extends DiApiControllerBase { |
|
|
@ -20,7 +23,7 @@ public class DiApiTask extends DiApiControllerBase { |
|
|
|
*/ |
|
|
|
@ResponseBody |
|
|
|
@PostMapping("/api/task/append") |
|
|
|
public DiApiResponse append(@RequestBody Map<String,Object> params) { |
|
|
|
public DiApiResponse append(@RequestBody Map<String,Object> params) { |
|
|
|
DiTaskExecutor executor = this.device.getTaskManager().getExecutor(); |
|
|
|
if ( null == executor ) { |
|
|
|
return this.error("task executor is not running"); |
|
|
@ -31,6 +34,54 @@ public class DiApiTask extends DiApiControllerBase { |
|
|
|
DiTaskManager taskManager = this.device.getTaskManager(); |
|
|
|
DiTask task = taskManager.generateTask(name, taskParams); |
|
|
|
executor.appendTask(task); |
|
|
|
return this.success(); |
|
|
|
return this.success(Map.of("id", task.getUUID())); |
|
|
|
} |
|
|
|
|
|
|
|
@ResponseBody |
|
|
|
@PostMapping("/api/task/task-action-execute") |
|
|
|
public DiApiResponse taskActionExecute(@RequestBody Map<String,Object> params) { |
|
|
|
String id = (String)params.get("id"); |
|
|
|
String action = (String)params.get("action"); |
|
|
|
Object actionParams = params.get("params"); |
|
|
|
|
|
|
|
DiTaskExecutor executor = this.device.getTaskManager().getExecutor(); |
|
|
|
DiTask task = executor.getTaskByUUID(id); |
|
|
|
if ( null == task ) { |
|
|
|
return this.error("task not found"); |
|
|
|
} |
|
|
|
|
|
|
|
boolean hasParams = false; |
|
|
|
Method actionMethod = null; |
|
|
|
try { |
|
|
|
actionMethod = task.getClass().getMethod(action); |
|
|
|
} catch (NoSuchMethodException e) { |
|
|
|
try { |
|
|
|
hasParams = true; |
|
|
|
actionMethod = task.getClass().getMethod(action, Map.class); |
|
|
|
} catch (NoSuchMethodException ep) { |
|
|
|
throw new RuntimeException(ep); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Object actionResult = null; |
|
|
|
try { |
|
|
|
if ( hasParams ) { |
|
|
|
if ( actionMethod.getReturnType().equals(Void.TYPE) ) { |
|
|
|
actionMethod.invoke(task, params); |
|
|
|
} else { |
|
|
|
actionResult = actionMethod.invoke(task, params); |
|
|
|
} |
|
|
|
} else { |
|
|
|
if ( actionMethod.getReturnType().equals(Void.TYPE) ) { |
|
|
|
actionMethod.invoke(task); |
|
|
|
} else { |
|
|
|
actionResult = actionMethod.invoke(task); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (IllegalAccessException | InvocationTargetException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
|
|
|
|
return this.success(actionResult); |
|
|
|
} |
|
|
|
} |