| | |
| | | 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 |
| | |
| | | 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) { |
| | |
| | | } |
| | | } |
| | | |
| | | 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) { |