| | |
| | | package com.ruoyi.device.uav.service; |
| | | |
| | | import com.dtflys.forest.exceptions.ForestNetworkException; |
| | | import com.dtflys.forest.exceptions.ForestRuntimeException; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.JsonMappingException; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.ruoyi.utils.forest.UavClient; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.codec.binary.Base64; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpMethod; |
| | |
| | | import java.util.Map; |
| | | |
| | | @Service |
| | | @Slf4j(topic = "uav") |
| | | public class UavService { |
| | | public static final String USERNAME = "ardbailu";//用户名 |
| | | public static final String PASSWORD = "ardkj12345";//密码 |
| | |
| | | |
| | | //token 登陆后每次请求,在header中携带 |
| | | private String token;//登录成功返回的token |
| | | private Map uavUser;//登录的用户信息 |
| | | private ObjectMapper om = new ObjectMapper(); |
| | | @Autowired |
| | | private UavClient uavClient; |
| | |
| | | return res; |
| | | } |
| | | |
| | | public Map getUavUser() { |
| | | if (this.uavUser == null) { |
| | | this.login(); |
| | | } |
| | | return this.uavUser; |
| | | } |
| | | |
| | | public void login() { |
| | | log.debug("登录无人机外部接口"); |
| | | String codedPassword = this.Encrypt(PASSWORD, SALT); |
| | | String body = "{\"username\":\"" + USERNAME + "\",\"password\":\"" + codedPassword + "\"}"; |
| | | try { |
| | | String res = uavClient.POST("login", null, body); |
| | | Map resMap = om.readValue(res, Map.class); |
| | | Map data = (Map) resMap.get("data"); |
| | | if (data != null) { |
| | | this.token = (String) data.get("access_token"); |
| | | this.uavUser = (Map) resMap.get("data"); |
| | | if (this.uavUser != null) { |
| | | this.token = (String) this.uavUser.get("access_token"); |
| | | } |
| | | } catch (ForestNetworkException e) { |
| | | e.printStackTrace(); |
| | | } catch (ForestRuntimeException e) { |
| | | log.error("无人机连接超时" + e.getMessage()); |
| | | } catch (JsonMappingException e) { |
| | | e.printStackTrace(); |
| | | } catch (JsonProcessingException e) { |