|
@@ -8,12 +8,15 @@ 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.IotDeviceBaseCtlReqVo;
|
|
|
import com.yunfeiyun.agmp.iotm.device.common.service.IotDeviceBaseService;
|
|
import com.yunfeiyun.agmp.iotm.device.common.service.IotDeviceBaseService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.poi.ss.formula.functions.T;
|
|
|
import org.springframework.aop.framework.AopContext;
|
|
import org.springframework.aop.framework.AopContext;
|
|
|
|
|
+import org.springframework.aop.support.AopUtils;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Method;
|
|
|
|
|
+import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -44,39 +47,15 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
|
|
|
Class<?>[] paramClass = item.getParameterTypes();
|
|
Class<?>[] paramClass = item.getParameterTypes();
|
|
|
// 方法若是无参,则无需进行转化
|
|
// 方法若是无参,则无需进行转化
|
|
|
if (paramClass.length == 1) {
|
|
if (paramClass.length == 1) {
|
|
|
- Object reqParam = iotDeviceBaseCtlReqVo.getParams();
|
|
|
|
|
- if (reqParam instanceof String && !paramClass[0].equals(String.class)) {
|
|
|
|
|
- // 如果参数类型是String,且接口入参是非String类,则将字符串参数转化为对应类型
|
|
|
|
|
- param = (T) JSONObject.parseObject((String) reqParam, paramClass[0]);
|
|
|
|
|
- setIotDeviceInParams(iotDeviceBaseCtlReqVo.getIotDevice(), param);
|
|
|
|
|
- } else if (reqParam instanceof JSONObject && !paramClass[0].equals(JSONObject.class)) {
|
|
|
|
|
- // 如果参数类型是JSONObject,且接口入参是非JSONObject类,则将JSONObject参数转化为对应类型
|
|
|
|
|
- param = (T) ((JSONObject) reqParam).to(paramClass[0]);
|
|
|
|
|
- setIotDeviceInParams(iotDeviceBaseCtlReqVo.getIotDevice(), param);
|
|
|
|
|
- } else if (reqParam instanceof JSONObject) {
|
|
|
|
|
- ((JSONObject) reqParam).put("IotDevice", iotDeviceBaseCtlReqVo.getIotDevice());
|
|
|
|
|
- param = (T) reqParam;
|
|
|
|
|
- } else {
|
|
|
|
|
- // 无需转化,则直接赋值
|
|
|
|
|
- param = (T) reqParam;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ param = transCtlParm(paramClass[0], iotDeviceBaseCtlReqVo);
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
- if ("close".equals(methodName) && null == method) {
|
|
|
|
|
- log.debug("设备未实现关闭方法,无需手动关闭,不再继续执行");
|
|
|
|
|
- return null;
|
|
|
|
|
- } else if (null == method) {
|
|
|
|
|
|
|
+ if (null == method) {
|
|
|
throw new BizException(ErrorCode.SYSTEM_ERROR.getCode(), "设备未实现操作方法");
|
|
throw new BizException(ErrorCode.SYSTEM_ERROR.getCode(), "设备未实现操作方法");
|
|
|
}
|
|
}
|
|
|
- Object current;
|
|
|
|
|
- if (method.getAnnotation(Transactional.class) != null) {
|
|
|
|
|
- current = AopContext.currentProxy();
|
|
|
|
|
- } else {
|
|
|
|
|
- current = this;
|
|
|
|
|
- }
|
|
|
|
|
- return (String) method.invoke(current, param);
|
|
|
|
|
|
|
+ return (String) method.invoke(getProxy(method), param);
|
|
|
} catch (InvocationTargetException e) {
|
|
} catch (InvocationTargetException e) {
|
|
|
Throwable throwable = e.getTargetException();
|
|
Throwable throwable = e.getTargetException();
|
|
|
if (throwable instanceof IotBizException) {
|
|
if (throwable instanceof IotBizException) {
|
|
@@ -95,14 +74,14 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public <T> T reset(String devBid) {
|
|
public <T> T reset(String devBid) {
|
|
|
- Method method = null;
|
|
|
|
|
|
|
+ Method method;
|
|
|
try {
|
|
try {
|
|
|
method = this.getClass().getDeclaredMethod("reset", String.class);
|
|
method = this.getClass().getDeclaredMethod("reset", String.class);
|
|
|
} catch (NoSuchMethodException e) {
|
|
} catch (NoSuchMethodException e) {
|
|
|
throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现简单重置方法");
|
|
throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现简单重置方法");
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
- return (T) method.invoke(AopContext.currentProxy(), devBid);
|
|
|
|
|
|
|
+ return (T) method.invoke(getProxy(method), devBid);
|
|
|
} catch (InvocationTargetException e) {
|
|
} catch (InvocationTargetException e) {
|
|
|
Throwable throwable = e.getTargetException();
|
|
Throwable throwable = e.getTargetException();
|
|
|
if (throwable instanceof IotBizException) {
|
|
if (throwable instanceof IotBizException) {
|
|
@@ -126,14 +105,14 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
|
|
|
for (int i = 0; i < listParams.length; i++) {
|
|
for (int i = 0; i < listParams.length; i++) {
|
|
|
paramClasses[i] = listParams[i].getClass();
|
|
paramClasses[i] = listParams[i].getClass();
|
|
|
}
|
|
}
|
|
|
- Method method = null;
|
|
|
|
|
|
|
+ Method method;
|
|
|
try {
|
|
try {
|
|
|
method = this.getClass().getDeclaredMethod("list", paramClasses);
|
|
method = this.getClass().getDeclaredMethod("list", paramClasses);
|
|
|
} catch (NoSuchMethodException e) {
|
|
} catch (NoSuchMethodException e) {
|
|
|
throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现详情方法");
|
|
throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现详情方法");
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
- return (List<T>) method.invoke(AopContext.currentProxy(), listParams);
|
|
|
|
|
|
|
+ return (List<T>) method.invoke(getProxy(method), listParams);
|
|
|
} catch (InvocationTargetException e) {
|
|
} catch (InvocationTargetException e) {
|
|
|
Throwable throwable = e.getTargetException();
|
|
Throwable throwable = e.getTargetException();
|
|
|
if (throwable instanceof IotBizException) {
|
|
if (throwable instanceof IotBizException) {
|
|
@@ -156,14 +135,14 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
|
|
|
for (int i = 0; i < infoParams.length; i++) {
|
|
for (int i = 0; i < infoParams.length; i++) {
|
|
|
paramClasses[i] = infoParams[i].getClass();
|
|
paramClasses[i] = infoParams[i].getClass();
|
|
|
}
|
|
}
|
|
|
- Method method = null;
|
|
|
|
|
|
|
+ Method method ;
|
|
|
try {
|
|
try {
|
|
|
method = this.getClass().getDeclaredMethod("info", paramClasses);
|
|
method = this.getClass().getDeclaredMethod("info", paramClasses);
|
|
|
} catch (NoSuchMethodException e) {
|
|
} catch (NoSuchMethodException e) {
|
|
|
throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现详情查询方法");
|
|
throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现详情查询方法");
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
- return (T) method.invoke(AopContext.currentProxy(), infoParams);
|
|
|
|
|
|
|
+ return (T) method.invoke(getProxy(method), infoParams);
|
|
|
} catch (InvocationTargetException e) {
|
|
} catch (InvocationTargetException e) {
|
|
|
Throwable throwable = e.getTargetException();
|
|
Throwable throwable = e.getTargetException();
|
|
|
if (throwable instanceof IotBizException) {
|
|
if (throwable instanceof IotBizException) {
|
|
@@ -186,14 +165,14 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
|
|
|
for (int i = 0; i < infoParams.length; i++) {
|
|
for (int i = 0; i < infoParams.length; i++) {
|
|
|
paramClasses[i] = infoParams[i].getClass();
|
|
paramClasses[i] = infoParams[i].getClass();
|
|
|
}
|
|
}
|
|
|
- Method method = null;
|
|
|
|
|
|
|
+ Method method;
|
|
|
try {
|
|
try {
|
|
|
method = this.getClass().getDeclaredMethod("edit", paramClasses);
|
|
method = this.getClass().getDeclaredMethod("edit", paramClasses);
|
|
|
} catch (NoSuchMethodException e) {
|
|
} catch (NoSuchMethodException e) {
|
|
|
throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现基础信息编辑方法");
|
|
throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现基础信息编辑方法");
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
- return (T) method.invoke(AopContext.currentProxy(), infoParams);
|
|
|
|
|
|
|
+ return (T) method.invoke(getProxy(method), infoParams);
|
|
|
} catch (InvocationTargetException e) {
|
|
} catch (InvocationTargetException e) {
|
|
|
Throwable throwable = e.getTargetException();
|
|
Throwable throwable = e.getTargetException();
|
|
|
if (throwable instanceof IotBizException) {
|
|
if (throwable instanceof IotBizException) {
|
|
@@ -223,7 +202,7 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
|
|
|
throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现配置信息编辑方法");
|
|
throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现配置信息编辑方法");
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
- return (T) method.invoke(AopContext.currentProxy(), infoParams);
|
|
|
|
|
|
|
+ return (T) method.invoke(getProxy(method), infoParams);
|
|
|
} catch (InvocationTargetException e) {
|
|
} catch (InvocationTargetException e) {
|
|
|
Throwable throwable = e.getTargetException();
|
|
Throwable throwable = e.getTargetException();
|
|
|
if (throwable instanceof IotBizException) {
|
|
if (throwable instanceof IotBizException) {
|
|
@@ -253,7 +232,7 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
|
|
|
throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现编辑方法");
|
|
throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现编辑方法");
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
- return (T) method.invoke(AopContext.currentProxy(), infoParams);
|
|
|
|
|
|
|
+ return (T) method.invoke(getProxy(method), infoParams);
|
|
|
} catch (InvocationTargetException e) {
|
|
} catch (InvocationTargetException e) {
|
|
|
Throwable throwable = e.getTargetException();
|
|
Throwable throwable = e.getTargetException();
|
|
|
if (throwable instanceof IotBizException) {
|
|
if (throwable instanceof IotBizException) {
|
|
@@ -278,4 +257,39 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
|
|
|
log.debug("通用控制接口自动写入IotDevice失败,失败原因:", e);
|
|
log.debug("通用控制接口自动写入IotDevice失败,失败原因:", e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private <T> T transCtlParm(Class ctlMethodParamClass, IotDeviceBaseCtlReqVo iotDeviceBaseCtlReqVo) {
|
|
|
|
|
+ Object ctlMethodParam;
|
|
|
|
|
+ Object reqParam = iotDeviceBaseCtlReqVo.getParams();
|
|
|
|
|
+ if (reqParam instanceof String && !ctlMethodParamClass.equals(String.class)) {
|
|
|
|
|
+ // 如果参数类型是String,且接口入参是非String类,则将字符串参数转化为对应类型
|
|
|
|
|
+ ctlMethodParam = JSONObject.parseObject((String) reqParam, ctlMethodParamClass);
|
|
|
|
|
+ setIotDeviceInParams(iotDeviceBaseCtlReqVo.getIotDevice(), ctlMethodParam);
|
|
|
|
|
+ } else if (reqParam instanceof JSONObject && !ctlMethodParamClass.equals(JSONObject.class)) {
|
|
|
|
|
+ // 如果参数类型是JSONObject,且接口入参是非JSONObject类,则将JSONObject参数转化为对应类型
|
|
|
|
|
+ ctlMethodParam = ((JSONObject) reqParam).to(ctlMethodParamClass);
|
|
|
|
|
+ setIotDeviceInParams(iotDeviceBaseCtlReqVo.getIotDevice(), ctlMethodParam);
|
|
|
|
|
+ } else if (reqParam instanceof JSONObject) {
|
|
|
|
|
+ ((JSONObject) reqParam).put("IotDevice", iotDeviceBaseCtlReqVo.getIotDevice());
|
|
|
|
|
+ ctlMethodParam = reqParam;
|
|
|
|
|
+ } else if (reqParam instanceof LinkedHashMap && !ctlMethodParamClass.equals(JSONObject.class)) {
|
|
|
|
|
+ ctlMethodParam = JSONObject.parseObject(JSONObject.toJSONString(reqParam), ctlMethodParamClass);
|
|
|
|
|
+ setIotDeviceInParams(iotDeviceBaseCtlReqVo.getIotDevice(), ctlMethodParam);
|
|
|
|
|
+ } else if (reqParam instanceof LinkedHashMap) {
|
|
|
|
|
+ ((LinkedHashMap) reqParam).put("IotDevice", iotDeviceBaseCtlReqVo.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();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|