Browse Source

优化公共接口

liuyaowen 1 year atrás
parent
commit
e60d5995f2

+ 1 - 0
src/main/java/com/yunfeiyun/agmp/iotm/device/common/domin/IotDeviceBaseListReqVo.java

@@ -8,4 +8,5 @@ import lombok.Data;
 public class IotDeviceBaseListReqVo extends IotDevice {
     private String landId;
     private String blockId;
+    private String devTypeCode;
 }

+ 51 - 37
src/main/java/com/yunfeiyun/agmp/iotm/device/common/service/impl/IotDeviceBaseServiceImpl.java

@@ -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.service.IotDeviceBaseService;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.poi.ss.formula.functions.T;
 import org.springframework.aop.framework.AopContext;
+import org.springframework.aop.support.AopUtils;
 import org.springframework.stereotype.Component;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.LinkedHashMap;
 import java.util.List;
 
 /**
@@ -44,39 +47,15 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
             Class<?>[] paramClass = item.getParameterTypes();
             // 方法若是无参,则无需进行转化
             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;
         }
         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(), "设备未实现操作方法");
             }
-            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) {
             Throwable throwable = e.getTargetException();
             if (throwable instanceof IotBizException) {
@@ -95,14 +74,14 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
 
     @Override
     public <T> T reset(String devBid) {
-        Method method = null;
+        Method method;
         try {
             method = this.getClass().getDeclaredMethod("reset", String.class);
         } catch (NoSuchMethodException e) {
             throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现简单重置方法");
         }
         try {
-            return (T) method.invoke(AopContext.currentProxy(), devBid);
+            return (T) method.invoke(getProxy(method), devBid);
         } catch (InvocationTargetException e) {
             Throwable throwable = e.getTargetException();
             if (throwable instanceof IotBizException) {
@@ -126,14 +105,14 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
         for (int i = 0; i < listParams.length; i++) {
             paramClasses[i] = listParams[i].getClass();
         }
-        Method method = null;
+        Method method;
         try {
             method = this.getClass().getDeclaredMethod("list", paramClasses);
         } catch (NoSuchMethodException e) {
             throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现详情方法");
         }
         try {
-            return (List<T>) method.invoke(AopContext.currentProxy(), listParams);
+            return (List<T>) method.invoke(getProxy(method), listParams);
         } catch (InvocationTargetException e) {
             Throwable throwable = e.getTargetException();
             if (throwable instanceof IotBizException) {
@@ -156,14 +135,14 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
         for (int i = 0; i < infoParams.length; i++) {
             paramClasses[i] = infoParams[i].getClass();
         }
-        Method method = null;
+        Method method ;
         try {
             method = this.getClass().getDeclaredMethod("info", paramClasses);
         } catch (NoSuchMethodException e) {
             throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现详情查询方法");
         }
         try {
-            return (T) method.invoke(AopContext.currentProxy(), infoParams);
+            return (T) method.invoke(getProxy(method), infoParams);
         } catch (InvocationTargetException e) {
             Throwable throwable = e.getTargetException();
             if (throwable instanceof IotBizException) {
@@ -186,14 +165,14 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
         for (int i = 0; i < infoParams.length; i++) {
             paramClasses[i] = infoParams[i].getClass();
         }
-        Method method = null;
+        Method method;
         try {
             method = this.getClass().getDeclaredMethod("edit", paramClasses);
         } catch (NoSuchMethodException e) {
             throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现基础信息编辑方法");
         }
         try {
-            return (T) method.invoke(AopContext.currentProxy(), infoParams);
+            return (T) method.invoke(getProxy(method), infoParams);
         } catch (InvocationTargetException e) {
             Throwable throwable = e.getTargetException();
             if (throwable instanceof IotBizException) {
@@ -223,7 +202,7 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
             throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现配置信息编辑方法");
         }
         try {
-            return (T) method.invoke(AopContext.currentProxy(), infoParams);
+            return (T) method.invoke(getProxy(method), infoParams);
         } catch (InvocationTargetException e) {
             Throwable throwable = e.getTargetException();
             if (throwable instanceof IotBizException) {
@@ -253,7 +232,7 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
             throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(), "设备未实现编辑方法");
         }
         try {
-            return (T) method.invoke(AopContext.currentProxy(), infoParams);
+            return (T) method.invoke(getProxy(method), infoParams);
         } catch (InvocationTargetException e) {
             Throwable throwable = e.getTargetException();
             if (throwable instanceof IotBizException) {
@@ -278,4 +257,39 @@ public abstract class IotDeviceBaseServiceImpl implements IotDeviceBaseService {
             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;
+        }
+    }
 }

+ 5 - 2
src/main/java/com/yunfeiyun/agmp/iotm/device/common/service/impl/IotDeviceCommonServiceImpl.java

@@ -5,6 +5,7 @@ import com.yunfeiyun.agmp.common.constant.ErrorCode;
 import com.yunfeiyun.agmp.common.exception.BizException;
 import com.yunfeiyun.agmp.common.utils.StringUtils;
 import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
+import com.yunfeiyun.agmp.iot.common.domain.TosDevicetype;
 import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
 import com.yunfeiyun.agmp.iot.common.service.TypeCacheService;
 import com.yunfeiyun.agmp.iotm.device.common.domin.IotDeviceBaseCtlReqVo;
@@ -28,10 +29,12 @@ public class IotDeviceCommonServiceImpl implements IotDeviceCommonService {
     private IotDeviceMapper iotDeviceMapper;
     @Override
     public <T> List<T> list(IotDeviceBaseListReqVo iotDeviceBaseListReqVo) {
-        if(StringUtils.isEmpty(iotDeviceBaseListReqVo.getDevtypeBid())){
+        if(StringUtils.isEmpty(iotDeviceBaseListReqVo.getDevTypeCode())){
             throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"设备类型为空");
         }
-        IotDeviceBaseService iotDeviceBaseService = iotDeviceBaseServiceMap.get(typeCacheService.getServiceNameByDevTypeBid(iotDeviceBaseListReqVo.getDevtypeBid()));
+        TosDevicetype tosDevicetype = typeCacheService.getCacheObjectByDevTypeCode(iotDeviceBaseListReqVo.getDevTypeCode());
+        IotDeviceBaseService iotDeviceBaseService = iotDeviceBaseServiceMap.get(typeCacheService.getServiceNameByDevTypeBid(tosDevicetype.getDevtypeBid()));
+        iotDeviceBaseListReqVo.setDevtypeBid(tosDevicetype.getDevtypeBid());
         return iotDeviceBaseService.list(iotDeviceBaseListReqVo);
     }
 

+ 2 - 5
src/main/java/com/yunfeiyun/agmp/iotm/web/controller/IotDeviceCommonController.java

@@ -35,16 +35,13 @@ public class IotDeviceCommonController extends BaseController {
      * */
     @PreAuthorize("@ss.hasPermi('iot:device:common:ctl')")
     @PostMapping("/ctl")
-    public AjaxResult ctl(IotDeviceBaseCtlReqVo iotDeviceBaseCtlReqVo){
+    public AjaxResult ctl(@RequestBody IotDeviceBaseCtlReqVo iotDeviceBaseCtlReqVo){
         return success(iotDeviceCommonService.ctl(iotDeviceBaseCtlReqVo));
     }
 
     /**设备详情公共接口*/
-    /**
-     * 设备控制公共接口
-     * */
     @PreAuthorize("@ss.hasPermi('iot:device:common:info')")
-    @PostMapping("/info/{devBid}")
+    @GetMapping("/info/{devBid}")
     public AjaxResult info(@PathVariable("devBid") String devBid){
         return success(iotDeviceCommonService.info(devBid));
     }