RobotMapController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. package com.ruoyi.web.controller.robot;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONArray;
  4. import com.alibaba.fastjson2.JSONObject;
  5. import com.ruoyi.common.core.domain.AjaxResult;
  6. import com.ruoyi.common.core.redis.RedisCache;
  7. import com.ruoyi.mqtt.service.MqttSendService;
  8. import com.ruoyi.mqtt.util.MqttTopicUtil;
  9. import io.swagger.annotations.Api;
  10. import io.swagger.annotations.ApiOperation;
  11. import io.swagger.annotations.ApiParam;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.http.*;
  15. import org.springframework.util.LinkedMultiValueMap;
  16. import org.springframework.util.MultiValueMap;
  17. import org.springframework.web.bind.annotation.*;
  18. import org.springframework.web.client.RestTemplate;
  19. import java.util.*;
  20. import java.util.concurrent.TimeUnit;
  21. /**
  22. * 地图管理Controller
  23. * 提供地图列表、缩略图、路网数据、点云等API
  24. * 数据源:LD导航系统后端(http://192.168.0.102:8086)
  25. */
  26. @Api(tags = "地图管理")
  27. @RestController
  28. @RequestMapping("/robot/map")
  29. public class RobotMapController {
  30. @Value("${ld-nav.url:http://192.168.0.102:8086}")
  31. private String ldNavUrl;
  32. @Autowired
  33. private MqttSendService mqttSendService;
  34. @Autowired
  35. private RedisCache redisCache;
  36. private final RestTemplate restTemplate = new RestTemplate();
  37. // ==================== 导入导出API ====================
  38. @ApiOperation("压缩导出地图")
  39. @PostMapping("/export/compress")
  40. public AjaxResult compressMapExport(@RequestBody Map<String, Object> params) {
  41. try {
  42. String url = ldNavUrl + "/v1/map/export/compress";
  43. HttpHeaders headers = new HttpHeaders();
  44. headers.setContentType(MediaType.APPLICATION_JSON);
  45. HttpEntity<Map<String, Object>> request = new HttpEntity<>(params, headers);
  46. ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
  47. if (response.getStatusCode() == HttpStatus.OK) {
  48. return AjaxResult.success("压缩成功", JSON.parse(response.getBody()));
  49. }
  50. return AjaxResult.error("压缩失败");
  51. } catch (Exception e) {
  52. return AjaxResult.error("压缩异常: " + e.getMessage());
  53. }
  54. }
  55. @ApiOperation("下载地图导出包")
  56. @GetMapping("/export")
  57. public ResponseEntity<byte[]> downloadMapExport(
  58. @ApiParam("地图名称") @RequestParam("map") String mapName
  59. ) {
  60. try {
  61. String url = ldNavUrl + "/v1/map/export?map=" + mapName;
  62. ResponseEntity<byte[]> response = restTemplate.getForEntity(url, byte[].class);
  63. HttpHeaders headers = new HttpHeaders();
  64. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  65. headers.setContentDisposition(ContentDisposition.builder("attachment")
  66. .filename(mapName + ".zip")
  67. .build());
  68. return new ResponseEntity<>(response.getBody(), headers, HttpStatus.OK);
  69. } catch (Exception e) {
  70. return ResponseEntity.notFound().build();
  71. }
  72. }
  73. @ApiOperation("导入地图")
  74. @PostMapping("/import")
  75. public AjaxResult importMap(
  76. @ApiParam("地图文件") @RequestParam("file") org.springframework.web.multipart.MultipartFile file
  77. ) {
  78. try {
  79. String url = ldNavUrl + "/v1/map/import";
  80. MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
  81. body.add("filedata", file.getResource());
  82. HttpHeaders headers = new HttpHeaders();
  83. headers.setContentType(MediaType.MULTIPART_FORM_DATA);
  84. HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
  85. ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
  86. if (response.getStatusCode() == HttpStatus.OK) {
  87. return AjaxResult.success("导入成功", JSON.parse(response.getBody()));
  88. }
  89. return AjaxResult.error("导入失败");
  90. } catch (Exception e) {
  91. return AjaxResult.error("导入异常: " + e.getMessage());
  92. }
  93. }
  94. // ==================== 路网数据API ====================
  95. @ApiOperation("获取路网GeoJSON数据")
  96. @GetMapping("/roadmap/geojson")
  97. public AjaxResult getRoadMapGeoJson(
  98. @ApiParam("地图名称") @RequestParam("map") String mapName
  99. ) {
  100. try {
  101. String cacheKey = "ld:roadmap:" + mapName;
  102. Object cached = redisCache.getCacheObject(cacheKey);
  103. if (cached != null) {
  104. return AjaxResult.success("获取成功(缓存)", cached);
  105. }
  106. String url = ldNavUrl + "/v1/roadmap/geojson?map=" + mapName;
  107. ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
  108. if (response.getStatusCode() == HttpStatus.OK) {
  109. JSONObject geojson = JSON.parseObject(response.getBody());
  110. // 缓存10分钟
  111. redisCache.setCacheObject(cacheKey, geojson, 10, TimeUnit.MINUTES);
  112. return AjaxResult.success("获取成功", geojson);
  113. }
  114. return AjaxResult.error("获取路网数据失败");
  115. } catch (Exception e) {
  116. return AjaxResult.error("获取路网数据异常: " + e.getMessage());
  117. }
  118. }
  119. @ApiOperation("保存路网GeoJSON数据")
  120. @PostMapping("/roadmap/geojson")
  121. public AjaxResult saveRoadMapGeoJson(@RequestBody JSONObject geoJsonData) {
  122. try {
  123. String mapName = geoJsonData.getString("map");
  124. if (mapName == null) {
  125. return AjaxResult.error("缺少地图名称");
  126. }
  127. String url = ldNavUrl + "/v1/roadmap/geojson";
  128. HttpHeaders headers = new HttpHeaders();
  129. headers.setContentType(MediaType.APPLICATION_JSON);
  130. HttpEntity<JSONObject> request = new HttpEntity<>(geoJsonData, headers);
  131. ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
  132. if (response.getStatusCode() == HttpStatus.OK) {
  133. // 清除路网缓存
  134. redisCache.deleteObject("ld:roadmap:" + mapName);
  135. return AjaxResult.success("保存成功");
  136. }
  137. return AjaxResult.error("保存失败");
  138. } catch (Exception e) {
  139. return AjaxResult.error("保存异常: " + e.getMessage());
  140. }
  141. }
  142. // ==================== 任务路线API ====================
  143. @ApiOperation("获取任务路线列表")
  144. @GetMapping("/path/list")
  145. public AjaxResult listPath(
  146. @ApiParam("地图名称") @RequestParam("map") String mapName
  147. ) {
  148. try {
  149. String url = ldNavUrl + "/v1/path/list?map=" + mapName;
  150. ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
  151. if (response.getStatusCode() == HttpStatus.OK) {
  152. return AjaxResult.success("获取成功", JSON.parse(response.getBody()));
  153. }
  154. return AjaxResult.error("获取路线列表失败");
  155. } catch (Exception e) {
  156. return AjaxResult.error("获取路线列表异常: " + e.getMessage());
  157. }
  158. }
  159. @ApiOperation("获取任务路线详情")
  160. @GetMapping("/path")
  161. public AjaxResult getPath(
  162. @ApiParam("地图名称") @RequestParam("map") String mapName,
  163. @ApiParam("路线名称") @RequestParam("path") String pathName
  164. ) {
  165. try {
  166. String url = ldNavUrl + "/v1/path?map=" + mapName + "&path=" + pathName;
  167. ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
  168. if (response.getStatusCode() == HttpStatus.OK) {
  169. return AjaxResult.success("获取成功", JSON.parse(response.getBody()));
  170. }
  171. return AjaxResult.error("获取路线详情失败");
  172. } catch (Exception e) {
  173. return AjaxResult.error("获取路线详情异常: " + e.getMessage());
  174. }
  175. }
  176. @ApiOperation("添加任务路线")
  177. @PostMapping("/path")
  178. public AjaxResult addPath(@RequestBody Map<String, Object> params) {
  179. try {
  180. String url = ldNavUrl + "/v1/path";
  181. HttpHeaders headers = new HttpHeaders();
  182. headers.setContentType(MediaType.APPLICATION_JSON);
  183. HttpEntity<Map<String, Object>> request = new HttpEntity<>(params, headers);
  184. ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
  185. if (response.getStatusCode() == HttpStatus.OK) {
  186. return AjaxResult.success("添加成功");
  187. }
  188. return AjaxResult.error("添加失败");
  189. } catch (Exception e) {
  190. return AjaxResult.error("添加异常: " + e.getMessage());
  191. }
  192. }
  193. @ApiOperation("删除任务路线")
  194. @DeleteMapping("/path")
  195. public AjaxResult deletePath(@RequestBody Map<String, String> params) {
  196. try {
  197. String map = params.get("map");
  198. String path = params.get("path");
  199. String url = ldNavUrl + "/v1/path?map=" + map + "&path=" + path;
  200. HttpHeaders headers = new HttpHeaders();
  201. HttpEntity<String> request = new HttpEntity<>(headers);
  202. ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.DELETE, request, String.class);
  203. if (response.getStatusCode() == HttpStatus.OK) {
  204. return AjaxResult.success("删除成功");
  205. }
  206. return AjaxResult.error("删除失败");
  207. } catch (Exception e) {
  208. return AjaxResult.error("删除异常: " + e.getMessage());
  209. }
  210. }
  211. // ==================== 标定API ====================
  212. @ApiOperation("获取标定历史")
  213. @GetMapping("/calibration/history")
  214. public AjaxResult getCalibrationHistory(
  215. @ApiParam("地图名称") @RequestParam("map") String mapName
  216. ) {
  217. try {
  218. String url = ldNavUrl + "/v1/map/file/" + mapName + "/shapefile/calibration.json";
  219. ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
  220. if (response.getStatusCode() == HttpStatus.OK) {
  221. return AjaxResult.success("获取成功", JSON.parse(response.getBody()));
  222. }
  223. return AjaxResult.error("获取标定历史失败");
  224. } catch (Exception e) {
  225. return AjaxResult.error("获取标定历史异常: " + e.getMessage());
  226. }
  227. }
  228. // ==================== VSLAM API ====================
  229. @ApiOperation("获取VSLAM统计信息")
  230. @GetMapping("/vslam/statistics")
  231. public AjaxResult getVSlamStatistics(
  232. @ApiParam("地图名称") @RequestParam("map") String mapName
  233. ) {
  234. try {
  235. String url = ldNavUrl + "/v1/vslam/statistics?map=" + mapName;
  236. ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
  237. if (response.getStatusCode() == HttpStatus.OK) {
  238. return AjaxResult.success("获取成功", JSON.parse(response.getBody()));
  239. }
  240. return AjaxResult.error("获取VSLAM统计信息失败");
  241. } catch (Exception e) {
  242. return AjaxResult.error("获取VSLAM统计信息异常: " + e.getMessage());
  243. }
  244. }
  245. @ApiOperation("获取关键帧点云")
  246. @GetMapping("/vslam/keyframe/cloud")
  247. public ResponseEntity<byte[]> getKeyframeCloud(
  248. @ApiParam("地图名称") @RequestParam("map") String mapName,
  249. @ApiParam("关键帧索引") @RequestParam("idx") int idx
  250. ) {
  251. try {
  252. String url = ldNavUrl + "/v1/vslam/keyframe/cloud?map=" + mapName + "&idx=" + idx;
  253. ResponseEntity<byte[]> response = restTemplate.getForEntity(url, byte[].class);
  254. return ResponseEntity.ok()
  255. .headers(headers -> headers.setContentType(MediaType.APPLICATION_OCTET_STREAM))
  256. .body(response.getBody());
  257. } catch (Exception e) {
  258. return ResponseEntity.notFound().build();
  259. }
  260. }
  261. @ApiOperation("获取关键帧变换矩阵")
  262. @GetMapping("/vslam/keyframe/trans")
  263. public ResponseEntity<byte[]> getKeyframeTrans(
  264. @ApiParam("地图名称") @RequestParam("map") String mapName,
  265. @ApiParam("关键帧索引") @RequestParam("idx") int idx
  266. ) {
  267. try {
  268. String url = ldNavUrl + "/v1/vslam/keyframe/trans?map=" + mapName + "&idx=" + idx;
  269. ResponseEntity<byte[]> response = restTemplate.getForEntity(url, byte[].class);
  270. return ResponseEntity.ok()
  271. .headers(headers -> headers.setContentType(MediaType.APPLICATION_OCTET_STREAM))
  272. .body(response.getBody());
  273. } catch (Exception e) {
  274. return ResponseEntity.notFound().build();
  275. }
  276. }
  277. @ApiOperation("获取闭环详情")
  278. @GetMapping("/vslam/closure/details")
  279. public AjaxResult getClosureDetails(
  280. @ApiParam("地图名称") @RequestParam("map") String mapName,
  281. @ApiParam("闭环索引") @RequestParam("idx") int idx
  282. ) {
  283. try {
  284. String url = ldNavUrl + "/v1/vslam/closure/details?map=" + mapName + "&idx=" + idx;
  285. ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
  286. if (response.getStatusCode() == HttpStatus.OK) {
  287. return AjaxResult.success("获取成功", JSON.parse(response.getBody()));
  288. }
  289. return AjaxResult.error("获取闭环详情失败");
  290. } catch (Exception e) {
  291. return AjaxResult.error("获取闭环详情异常: " + e.getMessage());
  292. }
  293. }
  294. // ==================== 传感器API ====================
  295. @ApiOperation("获取点云数据")
  296. @GetMapping("/sensor/pointcloud")
  297. public ResponseEntity<byte[]> getPointcloud() {
  298. try {
  299. String url = ldNavUrl + "/v1/sensor/pointcloud/3d";
  300. ResponseEntity<byte[]> response = restTemplate.getForEntity(url, byte[].class);
  301. return ResponseEntity.ok()
  302. .headers(headers -> headers.setContentType(MediaType.APPLICATION_OCTET_STREAM))
  303. .body(response.getBody());
  304. } catch (Exception e) {
  305. return ResponseEntity.notFound().build();
  306. }
  307. }
  308. // ==================== 地图瓦片API ====================
  309. @ApiOperation("获取地图配置信息")
  310. @GetMapping("/tilemap/details")
  311. public AjaxResult getTilemapDetails(
  312. @ApiParam("地图名称") @RequestParam("map") String mapName
  313. ) {
  314. try {
  315. String url = ldNavUrl + "/v1/tilemap/details?map=" + mapName;
  316. ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
  317. if (response.getStatusCode() == HttpStatus.OK) {
  318. return AjaxResult.success("获取成功", JSON.parse(response.getBody()));
  319. }
  320. return AjaxResult.error("获取地图配置失败");
  321. } catch (Exception e) {
  322. return AjaxResult.error("获取地图配置异常: " + e.getMessage());
  323. }
  324. }
  325. // ==================== 设备状态API ====================
  326. @ApiOperation("获取机器人实时位姿(从Redis缓存)")
  327. @GetMapping("/robot/pose")
  328. public AjaxResult getRobotPose(
  329. @ApiParam("设备ID") @RequestParam(required = false) String deviceId
  330. ) {
  331. try {
  332. String cacheKey = "ld:pose:" + (deviceId != null ? deviceId : "default");
  333. Object cached = redisCache.getCacheObject(cacheKey);
  334. if (cached != null) {
  335. return AjaxResult.success("获取成功(实时)", cached);
  336. }
  337. return AjaxResult.error("未获取到位姿数据");
  338. } catch (Exception e) {
  339. return AjaxResult.error("获取位姿异常: " + e.getMessage());
  340. }
  341. }
  342. @ApiOperation("获取任务实时信息(从Redis缓存)")
  343. @GetMapping("/robot/task")
  344. public AjaxResult getTaskRealtimeInfo(
  345. @ApiParam("设备ID") @RequestParam(required = false) String deviceId
  346. ) {
  347. try {
  348. String cacheKey = "ld:task:realtime:" + (deviceId != null ? deviceId : "default");
  349. Object cached = redisCache.getCacheObject(cacheKey);
  350. if (cached != null) {
  351. return AjaxResult.success("获取成功(实时)", cached);
  352. }
  353. return AjaxResult.error("未获取到任务数据");
  354. } catch (Exception e) {
  355. return AjaxResult.error("获取任务信息异常: " + e.getMessage());
  356. }
  357. }
  358. }