|
|
@ -1,9 +1,7 @@ |
|
|
|
package a8k.hardware.controler; |
|
|
|
import a8k.appbean.ecode.AppRet; |
|
|
|
import a8k.service.db.entity.HardwareServiceSetting; |
|
|
|
import a8k.utils.HardwareService; |
|
|
|
import a8k.utils.HardwareServiceParam; |
|
|
|
import a8k.utils.HardwareServiceParams; |
|
|
|
import a8k.utils.*; |
|
|
|
import com.iflytop.uf.UfApplication; |
|
|
|
import com.iflytop.uf.util.UfClassHelper; |
|
|
|
import org.springframework.stereotype.Controller; |
|
|
@ -11,6 +9,7 @@ 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.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
@ -99,5 +98,115 @@ public class Controler { |
|
|
|
return AppRet.success(); |
|
|
|
} |
|
|
|
|
|
|
|
@PostMapping("/api/service-config/service-action-list") |
|
|
|
@ResponseBody |
|
|
|
public AppRet<Object> serviceActionList( @RequestBody Map<String, Object> params ) throws Exception { |
|
|
|
String serviceKey = (String)params.get("serviceKey"); |
|
|
|
Class<?> serviceClass = null; |
|
|
|
var classes = UfClassHelper.getAllClassesInPackage("a8k"); |
|
|
|
for (var clazz : classes) { |
|
|
|
var hardwareServiceAnnotation = clazz.getAnnotation(HardwareService.class); |
|
|
|
if (null == hardwareServiceAnnotation || !clazz.getSimpleName().equals(serviceKey)) { |
|
|
|
continue ; |
|
|
|
} |
|
|
|
serviceClass = clazz; |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
List<Map<String,Object>> actions = new ArrayList<>(); |
|
|
|
if ( null == serviceClass ) { |
|
|
|
return AppRet.success(actions); |
|
|
|
} |
|
|
|
|
|
|
|
var methodList = serviceClass.getMethods(); |
|
|
|
for ( var method : methodList ) { |
|
|
|
var actionAnnotation = method.getAnnotation(HardwareServiceAction.class); |
|
|
|
if ( null == actionAnnotation ) { |
|
|
|
continue ; |
|
|
|
} |
|
|
|
var action = new HashMap<String,Object>(); |
|
|
|
action.put("key", method.getName()); |
|
|
|
action.put("name", actionAnnotation.name()); |
|
|
|
action.put("group", actionAnnotation.group()); |
|
|
|
|
|
|
|
var actionParams = new ArrayList<Map<String,Object>>(); |
|
|
|
var actionParamList = method.getParameters(); |
|
|
|
for ( var param : actionParamList ) { |
|
|
|
var actionParam = new HashMap<String, Object>(); |
|
|
|
actionParam.put("key", param.getName()); |
|
|
|
actionParam.put("type", param.getType().getName()); |
|
|
|
if ( param.getType().isEnum() ) { |
|
|
|
actionParam.put("type", "Enum"); |
|
|
|
actionParam.put("typeEnum", param.getType().getName()); |
|
|
|
var paramOptions = new ArrayList<Map<String,Object>>(); |
|
|
|
var values = param.getType().getEnumConstants(); |
|
|
|
for ( var value : values ) { |
|
|
|
var str = value.toString(); |
|
|
|
var paramOption = new HashMap<String, Object>(); |
|
|
|
paramOption.put("name", str); |
|
|
|
paramOption.put("value", str); |
|
|
|
paramOptions.add(paramOption); |
|
|
|
} |
|
|
|
actionParam.put("options", paramOptions); |
|
|
|
} |
|
|
|
actionParam.put("name", param.getName()); |
|
|
|
var annotation = param.getAnnotation(HardwareServiceActionParam.class); |
|
|
|
if ( null != annotation ) { |
|
|
|
actionParam.put("name", annotation.name()); |
|
|
|
} |
|
|
|
actionParams.add(actionParam); |
|
|
|
} |
|
|
|
action.put("params", actionParams); |
|
|
|
actions.add(action); |
|
|
|
} |
|
|
|
return AppRet.success(actions); |
|
|
|
} |
|
|
|
|
|
|
|
@PostMapping("/api/service-config/service-action-exec") |
|
|
|
@ResponseBody |
|
|
|
public AppRet<Object> serviceActionExecute( @RequestBody Map<String, Object> params ) throws Exception { |
|
|
|
String serviceKey = (String)params.get("serviceKey"); |
|
|
|
Class<?> serviceClass = null; |
|
|
|
var classes = UfClassHelper.getAllClassesInPackage("a8k"); |
|
|
|
for (var clazz : classes) { |
|
|
|
var hardwareServiceAnnotation = clazz.getAnnotation(HardwareService.class); |
|
|
|
if (null == hardwareServiceAnnotation || !clazz.getSimpleName().equals(serviceKey)) { |
|
|
|
continue ; |
|
|
|
} |
|
|
|
serviceClass = clazz; |
|
|
|
break; |
|
|
|
} |
|
|
|
if ( null == serviceClass ) { |
|
|
|
return AppRet.success(); |
|
|
|
} |
|
|
|
|
|
|
|
var actionName = (String)params.get("action"); |
|
|
|
var actionParams = (List<Object>)params.get("params"); |
|
|
|
var actionParamTypes = (List<String>)params.get("paramTypes"); |
|
|
|
Class<?>[] parameterTypes = new Class[actionParams.size()]; |
|
|
|
for(int i = 0; i < actionParams.size(); ++i) { |
|
|
|
var name = actionParamTypes.get(i); |
|
|
|
parameterTypes[i] = Class.forName(name); |
|
|
|
} |
|
|
|
Method method = serviceClass.getMethod(actionName, parameterTypes); |
|
|
|
|
|
|
|
var parameters = method.getParameters(); |
|
|
|
for ( int i=0; i<parameters.length; i++ ) { |
|
|
|
var parameter = parameters[i]; |
|
|
|
if ( parameter.getType().isEnum() ) { |
|
|
|
var methodValueOf = parameter.getType().getMethod("valueOf", String.class); |
|
|
|
var value = methodValueOf.invoke(null, actionParams.get(i)); |
|
|
|
actionParams.set(i, value); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var service = UfApplication.getContext().getBean(serviceClass); |
|
|
|
Object actionResult = null; |
|
|
|
if ( method.getReturnType().equals(Void.TYPE) ) { |
|
|
|
method.invoke(service, actionParams.toArray()); |
|
|
|
} else { |
|
|
|
actionResult = method.invoke(service, actionParams.toArray()); |
|
|
|
} |
|
|
|
return AppRet.success(actionResult); |
|
|
|
} |
|
|
|
} |