zhangjian
2023-07-10 4bcf7f6452d6ae04f210fc5c8408ad928d6602f4
无人机转发接口反射调用
已修改3个文件
107 ■■■■■ 文件已修改
ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java 78 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java 22 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java
@@ -11,14 +11,22 @@
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;
import javax.annotation.PostConstruct;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import org.apache.commons.codec.binary.Base64;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
/**
 * 相机设备Controller
@@ -36,20 +44,27 @@
    public static final String SALT = "0123456789012345";
    //token 登陆后每次请求,在header中携带
    private String token = null;
    private String token;//登录成功返回的token
    private ObjectMapper om = new ObjectMapper();
    @Autowired
    private UavClient uavClient;
    @PostConstruct
    public void created() {
        this.uavLogin();
    }
    public void uavLogin() {
        String codedPassword = this.Encrypt(PASSWORD, SALT);
        String body = "{\"username\":\"" + USERNAME + "\",\"password\":\"" + codedPassword + "\"}";
        try {
            String res = uavClient.post("login", null, body);
            String res = uavClient.POST("login", null, body);
            Map resMap = om.readValue(res, Map.class);
            Map data = (Map) resMap.get("data");
            this.token = (String) data.get("access_token");
            if (data != null) {
                this.token = (String) data.get("access_token");
            }
        } catch (ForestNetworkException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
@@ -59,50 +74,49 @@
        }
    }
    public String getToken() {
        if (StringUtil.isEmpty(this.token)) {
            this.uavLogin();
        }
        return this.token;
    }
    @GetMapping("/")
    @ApiOperation("无人机get接口")
    public Object get(@RequestParam String url, @RequestParam String data) {
        return doUavRequest(GET, url, data);
    }
    @Nullable
    private String doUavRequest(HttpMethod method, String url, String data) {
        String res = null;
        String token = getToken();
        Method requestMethod = null;
        //获取method
        try {
            res = uavClient.get(url, token, data);
        } catch (ForestNetworkException e) {
            if (e.getStatusCode() == 401) {
            requestMethod = UavClient.class.getMethod(method.name(), String.class, String.class, String.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        //执行method
        try {
            res = (String) requestMethod.invoke(this.uavClient, url, this.token, data);
        } catch (InvocationTargetException e) {
            ForestNetworkException fe = (ForestNetworkException) e.getCause();
            if (fe.getStatusCode() == 401) {//token失效,重新登录
                this.uavLogin();
                token = getToken();
                res = uavClient.get(url, token, data);
            }
            try {//再次调用接口
                res = (String) requestMethod.invoke(this.uavClient, url, this.token, data);
            } catch (IllegalAccessException ex) {
                throw new RuntimeException(ex);
            } catch (InvocationTargetException ex) {
                throw new RuntimeException(ex);
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return res;
    }
    @PostMapping("/")
    @ApiOperation("无人机post接口")
    public Object post(@RequestParam String url, @RequestParam String data) {
        String res = null;
        String token = getToken();
        try {
            res = uavClient.post(url, token, data);
        } catch (ForestNetworkException e) {
            if (e.getStatusCode() == 401) {
                this.uavLogin();
                token = getToken();
                res = uavClient.post(url, token, data);
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return res;
        return doUavRequest(POST, url, data);
    }
    public String Encrypt(String sSrc, String sKey) {
ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java
@@ -13,7 +13,18 @@
     */
    @Override
    public void onInvokeMethod(ForestRequest request, ForestMethod method, Object[] args) {
        System.out.println("Invoke Method '" + method.getMethodName() + "' Arguments: " + args);
        System.out.println("!!!!!onInvokeMethod!!!!!! '" + method.getMethodName() + "' Arguments>>>>>>>>>: ");
    /*    for (Object arg :
                args) {
            System.out.println(arg);
        }
        String token = (String) args[1];
        if (token != null) {
            request.addHeader("x-auth-token", token);
        }*/
        System.out.println("=================");
    }
    /**
@@ -22,10 +33,11 @@
    @Override
    public boolean beforeExecute(ForestRequest request) {
        String token = (String) getAttribute(request, "token");
        System.out.println(" UavAuthLifeCircle token:"+token);
        if(token!=null){
            request.addHeader("x-auth-token", token);
        }
        System.out.println("!!!!beforeExecute!!!!");
        System.out.println("request.getMethod().getMethodName():" + request.getMethod().getMethodName());
        System.out.println("  token:" + token);
        System.out.println("=================");
        request.addHeader("x-auth-token", token);
        return true;
    }
ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java
@@ -5,8 +5,9 @@
import com.dtflys.forest.annotation.JSONBody;
import com.dtflys.forest.annotation.Post;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component
@Service
public interface UavClient {
    @Get("http://www.baidu.com")
    String test();
@@ -16,11 +17,11 @@
    @UavAuth(token = "${token}")
    @Get(IP + "${url}")
    String get(@DataVariable("url") String url, @DataVariable("token") String token, @JSONBody String body);
    String GET(@DataVariable("url") String url, @DataVariable("token") String token, @JSONBody String body);
    @UavAuth(token = "${token}")
    @Post(IP + "${url}")
    String post(@DataVariable("url") String url, @DataVariable("token") String token, @JSONBody String body);
    String POST(@DataVariable("url") String url, @DataVariable("token") String token, @JSONBody String body);
}