|
|
@@ -6,6 +6,7 @@ import com.yunfeiyun.agmp.common.exception.BizException;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
|
|
|
import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
|
|
|
import com.yunfeiyun.agmp.iotm.device.common.domin.IotDeviceBaseCtlReqVo;
|
|
|
+import com.yunfeiyun.agmp.iotm.device.common.domin.IotDeviceBaseFunReqVo;
|
|
|
import com.yunfeiyun.agmp.iotm.device.common.service.IotDeviceBaseService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.poi.ss.formula.functions.T;
|
|
|
@@ -249,6 +250,47 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public <T> T func(IotDeviceBaseFunReqVo iotDeviceBaseFunReqVo) {
|
|
|
+ // 获取将要执行的方法
|
|
|
+ String methodName = iotDeviceBaseFunReqVo.getMethodName();
|
|
|
+ // 获取类下所有的方法
|
|
|
+ Method[] methods = this.getClass().getDeclaredMethods();
|
|
|
+ Method method = null;
|
|
|
+ T param = null;
|
|
|
+ // 根据methodName通过反射获取对应方法,并对参数进行预处理
|
|
|
+ for (Method item : methods) {
|
|
|
+ // 判断方法名称与参数数量
|
|
|
+ if (!item.getName().equals(methodName) || item.getParameterCount() > 1) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ method = item;
|
|
|
+ // 获取方法的参数类型
|
|
|
+ Class<?>[] paramClass = item.getParameterTypes();
|
|
|
+ // 方法若是无参,则无需进行转化
|
|
|
+ if (paramClass.length == 1) {
|
|
|
+ param = transFuncParm(paramClass[0], iotDeviceBaseFunReqVo);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return (T) method.invoke(getProxy(method), param);
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
+ Throwable throwable = e.getTargetException();
|
|
|
+ if (throwable instanceof IotBizException) {
|
|
|
+ throw (IotBizException) throwable;
|
|
|
+ }
|
|
|
+ if (throwable instanceof BizException) {
|
|
|
+ throw (BizException) throwable;
|
|
|
+ }
|
|
|
+ log.error("", e);
|
|
|
+ throw new BizException(ErrorCode.SYSTEM_ERROR.getCode(), "执行失败,错误原因未知");
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ log.error("", e);
|
|
|
+ throw new BizException(ErrorCode.SYSTEM_ERROR.getCode(), "未实现方法");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private void setIotDeviceInParams(IotDevice iotDevice, Object param) {
|
|
|
try {
|
|
|
Method method = param.getClass().getDeclaredMethod("setIotDevice", IotDevice.class);
|
|
|
@@ -285,6 +327,33 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
|
|
|
return (T) ctlMethodParam;
|
|
|
}
|
|
|
|
|
|
+ private <T> T transFuncParm(Class ctlMethodParamClass, IotDeviceBaseFunReqVo iotDeviceBaseFunReqVo) {
|
|
|
+ Object ctlMethodParam;
|
|
|
+ Object reqParam = iotDeviceBaseFunReqVo.getParam();
|
|
|
+ if (reqParam instanceof String && !ctlMethodParamClass.equals(String.class)) {
|
|
|
+ // 如果参数类型是String,且接口入参是非String类,则将字符串参数转化为对应类型
|
|
|
+ ctlMethodParam = JSONObject.parseObject((String) reqParam, ctlMethodParamClass);
|
|
|
+ setIotDeviceInParams(iotDeviceBaseFunReqVo.getIotDevice(), ctlMethodParam);
|
|
|
+ } else if (reqParam instanceof JSONObject && !ctlMethodParamClass.equals(JSONObject.class)) {
|
|
|
+ // 如果参数类型是JSONObject,且接口入参是非JSONObject类,则将JSONObject参数转化为对应类型
|
|
|
+ ctlMethodParam = ((JSONObject) reqParam).to(ctlMethodParamClass);
|
|
|
+ setIotDeviceInParams(iotDeviceBaseFunReqVo.getIotDevice(), ctlMethodParam);
|
|
|
+ } else if (reqParam instanceof JSONObject) {
|
|
|
+ ((JSONObject) reqParam).put("IotDevice", iotDeviceBaseFunReqVo.getIotDevice());
|
|
|
+ ctlMethodParam = reqParam;
|
|
|
+ } else if (reqParam instanceof LinkedHashMap && !ctlMethodParamClass.equals(JSONObject.class)) {
|
|
|
+ ctlMethodParam = JSONObject.parseObject(JSONObject.toJSONString(reqParam), ctlMethodParamClass);
|
|
|
+ setIotDeviceInParams(iotDeviceBaseFunReqVo.getIotDevice(), ctlMethodParam);
|
|
|
+ } else if (reqParam instanceof LinkedHashMap) {
|
|
|
+ ((LinkedHashMap) reqParam).put("IotDevice", iotDeviceBaseFunReqVo.getIotDevice());
|
|
|
+ ctlMethodParam = JSONObject.parseObject(JSONObject.toJSONString(reqParam), ctlMethodParamClass);
|
|
|
+ } else {
|
|
|
+ // 无需转化,则直接赋值
|
|
|
+ ctlMethodParam = reqParam;
|
|
|
+ }
|
|
|
+ return (T) ctlMethodParam;
|
|
|
+ }
|
|
|
+
|
|
|
private Object getProxy(Method method){
|
|
|
if (AopUtils.isAopProxy(method) || AopUtils.isAopProxy(this)) {
|
|
|
return AopContext.currentProxy();
|