|  |  |  | 
|---|
|  |  |  | package com.ruoyi.device.uav.controller; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.dtflys.forest.exceptions.ForestNetworkException; | 
|---|
|  |  |  | import com.fasterxml.jackson.core.JsonProcessingException; | 
|---|
|  |  |  | import com.fasterxml.jackson.databind.JsonMappingException; | 
|---|
|  |  |  | import com.fasterxml.jackson.databind.ObjectMapper; | 
|---|
|  |  |  | import com.github.pagehelper.util.StringUtil; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.ruoyi.common.core.controller.BaseController; | 
|---|
|  |  |  | import com.ruoyi.utils.forest.UavClient; | 
|---|
|  |  |  | import com.ruoyi.common.core.domain.AjaxResult; | 
|---|
|  |  |  | import com.ruoyi.device.uav.service.UavService; | 
|---|
|  |  |  | import io.swagger.annotations.Api; | 
|---|
|  |  |  | 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.beans.factory.annotation.Value; | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | import static org.springframework.http.HttpMethod.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 相机设备Controller | 
|---|
|  |  |  | 
|---|
|  |  |  | @RequestMapping("/device/uav") | 
|---|
|  |  |  | @Api(tags = "无人机外部接口") | 
|---|
|  |  |  | public class ArdUavController extends BaseController { | 
|---|
|  |  |  | public static final String USERNAME = "ardbailu"; | 
|---|
|  |  |  | public static final String PASSWORD = "ardkj12345"; | 
|---|
|  |  |  | public static final String SALT = "0123456789012345"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //token 登陆后每次请求,在header中携带 | 
|---|
|  |  |  | private String token;//登录成功返回的token | 
|---|
|  |  |  | private ObjectMapper om = new ObjectMapper(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private UavClient uavClient; | 
|---|
|  |  |  | private UavService uavService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Value("${uav.enabled}") | 
|---|
|  |  |  | private Boolean uavEnabled; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostConstruct | 
|---|
|  |  |  | public void created() { | 
|---|
|  |  |  | this.uavLogin(); | 
|---|
|  |  |  | if(!uavEnabled){//无人机加入开关 | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | this.uavService.login(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void uavLogin() { | 
|---|
|  |  |  | 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"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (ForestNetworkException e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } catch (JsonMappingException e) { | 
|---|
|  |  |  | throw new RuntimeException(e); | 
|---|
|  |  |  | } catch (JsonProcessingException e) { | 
|---|
|  |  |  | throw new RuntimeException(e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @GetMapping("/") | 
|---|
|  |  |  | @ApiOperation("无人机get接口") | 
|---|
|  |  |  | public Object get(@RequestParam String url, @RequestParam String data) { | 
|---|
|  |  |  | return doUavRequest(GET, url, data); | 
|---|
|  |  |  | public Object get(@RequestParam String url) { | 
|---|
|  |  |  | return this.uavService.doUavRequest(GET, url, "{}"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Nullable | 
|---|
|  |  |  | private String doUavRequest(HttpMethod method, String url, String data) { | 
|---|
|  |  |  | String res = null; | 
|---|
|  |  |  | Method requestMethod = null; | 
|---|
|  |  |  | //获取method | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | 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(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 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) { | 
|---|
|  |  |  | return doUavRequest(POST, url, data); | 
|---|
|  |  |  | public Object post(@RequestBody Map<String, String> param) { | 
|---|
|  |  |  | String url = param.get("url"); | 
|---|
|  |  |  | String data = param.get("data"); | 
|---|
|  |  |  | return this.uavService.doUavRequest(POST, url, data); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public String Encrypt(String sSrc, String sKey) { | 
|---|
|  |  |  | if (sKey == null) { | 
|---|
|  |  |  | System.out.print("Key为空null"); | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 判断Key是否为16位 | 
|---|
|  |  |  | if (sKey.length() != 16) { | 
|---|
|  |  |  | System.out.print("Key长度不是16位"); | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | byte[] encrypted = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | byte[] raw = sKey.getBytes("utf-8"); | 
|---|
|  |  |  | SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); | 
|---|
|  |  |  | Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式" | 
|---|
|  |  |  | cipher.init(Cipher.ENCRYPT_MODE, skeySpec); | 
|---|
|  |  |  | encrypted = cipher.doFinal(sSrc.getBytes("utf-8")); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | @DeleteMapping("/") | 
|---|
|  |  |  | @ApiOperation("无人机delete接口") | 
|---|
|  |  |  | public Object delete(@RequestBody Map<String, String> param) { | 
|---|
|  |  |  | String url = param.get("url"); | 
|---|
|  |  |  | String data = param.get("data"); | 
|---|
|  |  |  | return this.uavService.doUavRequest(DELETE, url, data); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @GetMapping("/getToken") | 
|---|
|  |  |  | @ApiOperation("无人机delete接口") | 
|---|
|  |  |  | public Object getToken() { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return this.uavService.getToken(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostMapping("/checkPointValid") | 
|---|
|  |  |  | @ApiOperation("无人机检测航点有效性接口") | 
|---|
|  |  |  | public Object checkPointValid(@RequestBody Map<String,Map<String,Object>> param) { | 
|---|
|  |  |  | String url = "wayline/api/v1/waylines/planning/check_point_valid"; | 
|---|
|  |  |  | return this.uavService.checkPointValid(url, param); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostMapping("/checkLineValid") | 
|---|
|  |  |  | @ApiOperation("无人机检测线路有效性接口") | 
|---|
|  |  |  | public Object checkLineValid(@RequestBody Map<String,List<Map<String,String>>> param) { | 
|---|
|  |  |  | String url = "wayline/api/v1/waylines/planning/check_line_valid"; | 
|---|
|  |  |  | return this.uavService.checkLineValid(url, param); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @GetMapping("/getWorkspaceId") | 
|---|
|  |  |  | @ApiOperation("获取工作空间主键") | 
|---|
|  |  |  | public AjaxResult getWorkspaceId() { | 
|---|
|  |  |  | try{ | 
|---|
|  |  |  | String workspaceId = this.uavService.getWorkspaceId(); | 
|---|
|  |  |  | if(workspaceId != null){ | 
|---|
|  |  |  | return AjaxResult.success(workspaceId); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | return AjaxResult.success(""); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }catch(Exception e){ | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | ; | 
|---|
|  |  |  | return AjaxResult.error(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。 | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostMapping("/addWaylinesPlanning") | 
|---|
|  |  |  | @ApiOperation("新增航线") | 
|---|
|  |  |  | public Object addWaylinesPlanning(@RequestBody Map<String,Object> param) { | 
|---|
|  |  |  | String url = "wayline/api/v1/waylines/planning"; | 
|---|
|  |  |  | return this.uavService.addWaylinesPlanning(url, param); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @DeleteMapping("/deleteWaylinesPlanning") | 
|---|
|  |  |  | @ApiOperation("删除航线") | 
|---|
|  |  |  | public Object deleteWaylinesPlanning(@RequestBody Map<String,String> param) { | 
|---|
|  |  |  | String url = "wayline/api/v1/waylines/delete/"; | 
|---|
|  |  |  | return this.uavService.deleteWaylinesPlanning(url, param); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PutMapping("/updateWaylinesPlanning") | 
|---|
|  |  |  | @ApiOperation("修改航线") | 
|---|
|  |  |  | public Object updateWaylinesPlanning(@RequestBody Map<String,Object> param) { | 
|---|
|  |  |  | String url = "wayline/api/v1/waylines/update"; | 
|---|
|  |  |  | return this.uavService.updateWaylinesPlanning(url, param); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostMapping("/addAlarm") | 
|---|
|  |  |  | @ApiOperation("单点任务") | 
|---|
|  |  |  | public Object addAlarm(@RequestBody Map<String,Object> param) { | 
|---|
|  |  |  | String url = "manage/api/v1/alarms/add"; | 
|---|
|  |  |  | return this.uavService.addAlarm(url, param); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostMapping("/saveCurrentGroup") | 
|---|
|  |  |  | @ApiOperation("同步") | 
|---|
|  |  |  | public Object saveCurrentGroup(@RequestBody Map<String,Object> param) { | 
|---|
|  |  |  | String url = "wayline/api/v1/waylines/planning/save_current_group"; | 
|---|
|  |  |  | return this.uavService.saveCurrentGroup(url, param); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|