simulation_service.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. package simulation_service
  2. import (
  3. "archive/zip"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/cloudwego/hertz/pkg/app"
  8. "github.com/cloudwego/hertz/pkg/protocol/consts"
  9. uuid "github.com/satori/go.uuid"
  10. "io"
  11. "net/http"
  12. "os"
  13. "path/filepath"
  14. "pji_desktop_http/biz/dal/mysql"
  15. "pji_desktop_http/biz/model"
  16. "pji_desktop_http/common/config"
  17. "pji_desktop_http/common/config/c_log"
  18. "pji_desktop_http/common/entity"
  19. "pji_desktop_http/common/util"
  20. )
  21. // DownloadSimulationZipFile 根据请求id从oss拉取并打包仿真测试所需要的文件
  22. // @router /simulation/download/zipfile [GET]
  23. func DownloadSimulationZipFile(ctx context.Context, c *app.RequestContext) {
  24. id := c.Query("id")
  25. fmt.Println(id)
  26. // 根据id生成用于地图更新的压缩包
  27. filePath, tmpDir, err := generateSimulationZipById(ctx, id)
  28. if err != nil {
  29. c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
  30. }
  31. fmt.Println("filePath", filePath)
  32. // 检查文件是否存在
  33. if _, err := os.Stat(filePath); os.IsNotExist(err) {
  34. c.JSON(http.StatusNotFound, map[string]string{"error": "File not found"})
  35. return
  36. }
  37. // 打开文件
  38. f, err := os.Open(filePath)
  39. if err != nil {
  40. c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to open file"})
  41. return
  42. }
  43. defer f.Close()
  44. // 设置响应头
  45. c.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filepath.Base(filePath)))
  46. c.Response.Header.Set("Content-Type", "binary/octet-stream")
  47. // 将文件流式传输回客户端
  48. data, err := io.ReadAll(f)
  49. if err != nil {
  50. panic(err)
  51. }
  52. if _, err := c.Write(data); err != nil {
  53. c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
  54. return
  55. }
  56. defer os.RemoveAll(tmpDir)
  57. }
  58. // 根据id生成用于仿真测试的压缩包
  59. func generateSimulationZipById(ctx context.Context, id string) (file string, tmpDir string, err error) {
  60. // 根据id获取对应的oss文件列表
  61. allFileList, err := util.GetExactedMapFileById(id)
  62. //fmt.Println("Filtered Strings:", fileList)
  63. if err != nil {
  64. return
  65. }
  66. // 创建临时文件夹
  67. tmpDir, err = os.MkdirTemp("", "temp-download-*")
  68. fmt.Println("tmpDir:", tmpDir)
  69. if err != nil {
  70. fmt.Println("Error creating temporary directory:", err)
  71. return "", "", err
  72. }
  73. c_log.GlobalLogger.Info("创建下载-临时文件夹:", tmpDir)
  74. // 创建根文件夹(文件打包的根目录)
  75. baseDir := filepath.Join(tmpDir, "data")
  76. if err := os.Mkdir(baseDir, 0755); err != nil {
  77. fmt.Println("Error creating subdirectory:", err)
  78. return "", "", err
  79. }
  80. c_log.GlobalLogger.Info("创建文件打包根目录:", baseDir)
  81. // 从oss下载data.zip, map.pgm, map.yaml文件到根目录
  82. // 过滤特定后缀的文件列表
  83. simulationFileList := util.FilterBySuffixes(allFileList, config.SimulationFiltersuffixes...)
  84. fmt.Println("simulationFileList", simulationFileList)
  85. // 从oss下载文件到 根目录
  86. for _, file := range simulationFileList {
  87. err = config.OssBucket.GetObjectToFile(file, filepath.Join(baseDir, filepath.Base(file)))
  88. if err != nil {
  89. fmt.Println("Error downloading file - data.zip, map.pgm, map.yaml, map.bag:", err)
  90. return "", "", err
  91. }
  92. }
  93. c_log.GlobalLogger.Info("下载data.zip, map.pgm, map.yaml, map.bag文件到根目录 - 成功")
  94. // 下载world文件&map.stl文件
  95. // 查询状态
  96. world, err := mysql.QueryWorld(ctx, id)
  97. if err != nil || world == nil { // 记录不存在
  98. c_log.GlobalLogger.Info("world记录不存在 - 跳过")
  99. } else { // 记录存在
  100. // 下载world文件
  101. worldURL := world.WorldURL
  102. err = config.OssBucket.GetObjectToFile(worldURL, filepath.Join(baseDir, filepath.Base(worldURL)))
  103. if err != nil {
  104. fmt.Println("Error downloading world file:", err)
  105. return "", "", err
  106. }
  107. c_log.GlobalLogger.Info("下载world文件到根目录 - 成功")
  108. // 下载map.stl文件
  109. stlURL := world.StlURL
  110. if stlURL != nil && *stlURL != "" {
  111. err = config.OssBucket.GetObjectToFile(*stlURL, filepath.Join(baseDir, filepath.Base(*stlURL)))
  112. if err != nil {
  113. fmt.Println("Error downloading stl file:", err)
  114. return "", "", err
  115. }
  116. c_log.GlobalLogger.Info("下载map.stl文件到根目录 - 成功")
  117. } else {
  118. c_log.GlobalLogger.Info("map.stl文件不存在 - 跳过")
  119. }
  120. }
  121. // 下载原始bag
  122. rosField, err := util.GetRosFileById(id)
  123. if err != nil {
  124. fmt.Println("Error querying origin map's bag file:", err)
  125. return "", "", err
  126. }
  127. err = config.OssBucket.GetObjectToFile(rosField, filepath.Join(baseDir, "origin_map.bag"))
  128. if err != nil {
  129. fmt.Println("Error downloading origin map's bag file:", err)
  130. return "", "", err
  131. }
  132. c_log.GlobalLogger.Info("下载origin_map.bag文件到根目录 - 成功")
  133. // 创建压缩文件
  134. zipPath := filepath.Join(tmpDir, "simulationFile-"+id+".zip")
  135. zipFile, err := os.Create(zipPath)
  136. if err != nil {
  137. fmt.Println("Error creating ZIP file:", err)
  138. return "", "", err
  139. }
  140. defer zipFile.Close()
  141. zipWriter := zip.NewWriter(zipFile)
  142. defer zipWriter.Close()
  143. // 压缩文件夹
  144. if err := util.AddDirToZip(baseDir, zipWriter); err != nil {
  145. fmt.Println("Error adding directory to ZIP:", err)
  146. return "", "", err
  147. }
  148. fmt.Println("ZIP file created successfully.")
  149. c_log.GlobalLogger.Info("创建压缩文件 - 成功")
  150. return zipPath, tmpDir, nil
  151. }
  152. // CheckDataFileStatus 检查data目录是否存在
  153. // @router /simulation/check/file/data/status [GET]
  154. func CheckDataFileStatus(ctx context.Context, c *app.RequestContext) {
  155. sceneID := c.Query("id")
  156. fmt.Println("id", sceneID)
  157. // 根据id获取对应的oss文件列表
  158. allFileList, err := util.GetExactedMapFileById(sceneID)
  159. if err != nil {
  160. return
  161. }
  162. // 过滤特定后缀的文件列表(data.zip)
  163. simulationFileList := util.FilterBySuffixes(allFileList, config.DataFiltersuffixes...)
  164. if len(simulationFileList) == 0 {
  165. c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "data目录不存在"})
  166. return
  167. }
  168. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "data目录存在"})
  169. }
  170. // UploadPdfFile 将result.pdf文件上传到oss
  171. // @router /simulation/upload/pdf [GET]
  172. func UploadPdfFile(ctx context.Context, c *app.RequestContext) {
  173. equipmentNo := c.Query("equipmentNo")
  174. fmt.Println("equipmentNo", equipmentNo)
  175. sceneNo := c.Query("sceneNo")
  176. fmt.Println("sceneNo", sceneNo)
  177. timeStamp := c.Query("timeStamp")
  178. fmt.Println("timeStamp", timeStamp)
  179. round := c.Query("round")
  180. fmt.Println("round", round)
  181. header, err := c.FormFile("file")
  182. if err != nil {
  183. c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
  184. return
  185. }
  186. fileName := header.Filename
  187. fmt.Println("filename", fileName)
  188. ossObjectKey := config.SimulationOssBasePrefix + "/" + equipmentNo + "/" + sceneNo + "/" + timeStamp + "/" + round + "/" + fileName
  189. fmt.Println("ossObjectKey", ossObjectKey)
  190. f, _ := header.Open()
  191. defer f.Close()
  192. config.OssMutex.Lock()
  193. err = config.OssBucket.PutObject(ossObjectKey, f)
  194. config.OssMutex.Unlock()
  195. if err != nil {
  196. c_log.GlobalLogger.Error("程序异常退出。上传文件", fileName, "->", ossObjectKey, "出错:", err)
  197. c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "上传文件失败", Details: ""})
  198. return
  199. }
  200. c_log.GlobalLogger.Info("上传文件", fileName, "->", ossObjectKey, "成功。")
  201. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传文件成功", Details: ossObjectKey})
  202. }
  203. // UploadBagFile 将test-${i}.bag文件上传到oss
  204. // @router /simulation/upload/bag [GET]
  205. func UploadBagFile(ctx context.Context, c *app.RequestContext) {
  206. equipmentNo := c.Query("equipmentNo")
  207. fmt.Println("equipmentNo", equipmentNo)
  208. sceneNo := c.Query("sceneNo")
  209. fmt.Println("sceneNo", sceneNo)
  210. timeStamp := c.Query("timeStamp")
  211. fmt.Println("timeStamp", timeStamp)
  212. round := c.Query("round")
  213. fmt.Println("round", round)
  214. header, err := c.FormFile("file")
  215. if err != nil {
  216. c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
  217. return
  218. }
  219. fileName := header.Filename
  220. fmt.Println("filename", fileName)
  221. ossObjectKey := config.SimulationOssBasePrefix + "/" + equipmentNo + "/" + sceneNo + "/" + timeStamp + "/" + round + "/" + fileName
  222. fmt.Println("ossObjectKey", ossObjectKey)
  223. f, _ := header.Open()
  224. defer f.Close()
  225. config.OssMutex.Lock()
  226. err = config.OssBucket.PutObject(ossObjectKey, f)
  227. config.OssMutex.Unlock()
  228. if err != nil {
  229. c_log.GlobalLogger.Error("程序异常退出。上传文件", fileName, "->", ossObjectKey, "出错:", err)
  230. c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "上传文件失败", Details: ""})
  231. return
  232. }
  233. c_log.GlobalLogger.Info("上传文件", fileName, "->", ossObjectKey, "成功。")
  234. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传文件成功", Details: ossObjectKey})
  235. }
  236. // UploadPgmFile 将map.pgm文件上传到oss
  237. // @router /simulation/upload/pgm [GET]
  238. func UploadPgmFile(ctx context.Context, c *app.RequestContext) {
  239. equipmentNo := c.Query("equipmentNo")
  240. fmt.Println("equipmentNo", equipmentNo)
  241. sceneNo := c.Query("sceneNo")
  242. fmt.Println("sceneNo", sceneNo)
  243. header, err := c.FormFile("file")
  244. if err != nil {
  245. c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
  246. return
  247. }
  248. fileName := header.Filename
  249. fmt.Println("filename", fileName)
  250. ossObjectKey := config.SimulationOssBasePrefix + "/" + equipmentNo + "/" + sceneNo + "/" + fileName
  251. fmt.Println("ossObjectKey", ossObjectKey)
  252. f, _ := header.Open()
  253. defer f.Close()
  254. config.OssMutex.Lock()
  255. err = config.OssBucket.PutObject(ossObjectKey, f)
  256. config.OssMutex.Unlock()
  257. if err != nil {
  258. c_log.GlobalLogger.Error("程序异常退出。上传文件", fileName, "->", ossObjectKey, "出错:", err)
  259. c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "上传文件失败", Details: ""})
  260. return
  261. }
  262. c_log.GlobalLogger.Info("上传文件", fileName, "->", ossObjectKey, "成功。")
  263. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传文件成功", Details: ossObjectKey})
  264. }
  265. // AddSimulationRecord 添加仿真测试记录
  266. // @router /simulation/add/record [GET]
  267. func AddSimulationRecord(ctx context.Context, c *app.RequestContext) {
  268. var records []*model.SimulationTestRecord
  269. err := c.BindAndValidate(&records)
  270. for _, record := range records {
  271. record.ID = uuid.NewV1().String()
  272. }
  273. fmt.Println("records", records)
  274. if err != nil {
  275. c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
  276. return
  277. }
  278. err = mysql.AddSimulationTestRecords(ctx, records)
  279. if err != nil {
  280. c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "仿真测试记录添加失败"})
  281. return
  282. }
  283. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "仿真测试记录添加成功"})
  284. }
  285. // QueryEvalRecord 查询算法评价记录(根据场景id及时间戳)
  286. // @router /simulation/query/eval/record [GET]
  287. func QueryEvalRecord(ctx context.Context, c *app.RequestContext) {
  288. sceneId := c.Query("sceneId")
  289. fmt.Println("sceneId", sceneId)
  290. testTime := c.Query("testTime")
  291. fmt.Println("testTime", testTime)
  292. records, err := mysql.QuerySimulationTestRecords(ctx, sceneId, testTime)
  293. if err != nil {
  294. c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "仿真测试记录查询失败", Details: ""})
  295. return
  296. }
  297. output, err := json.Marshal(records)
  298. if err != nil {
  299. c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "仿真测试记录查询失败", Details: ""})
  300. return
  301. }
  302. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "仿真测试记录查询成功", Details: string(output)})
  303. }
  304. // DownloadFileByKeys 给定object key数组从oss下载(打包)文件
  305. // @router /simulation/download/oss/key [POST]
  306. func DownloadFileByKeys(ctx context.Context, c *app.RequestContext) {
  307. sceneId := c.Query("sceneId")
  308. fmt.Println("sceneId", sceneId)
  309. typeName := c.Query("typeName")
  310. fmt.Println("typeName", typeName)
  311. var req []string
  312. err := c.BindAndValidate(&req)
  313. if err != nil {
  314. c.String(consts.StatusBadRequest, err.Error())
  315. return
  316. }
  317. fmt.Println("req", req)
  318. if len(req) == 0 {
  319. c.JSON(consts.StatusBadRequest, entity.HttpResult{Status: false, Code: "", Message: "请求数据为空。"})
  320. } else if len(req) == 1 { // 只有一条数据则直接下载文件
  321. objectKey := req[0]
  322. // 从OSS下载文件
  323. reader, err := config.OssBucket.GetObject(objectKey)
  324. if err != nil {
  325. c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
  326. return
  327. }
  328. defer reader.Close()
  329. // 设置响应头
  330. c.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filepath.Base(objectKey)))
  331. c.Response.Header.Set("Content-Type", "binary/octet-stream")
  332. // 将文件流式传输回客户端
  333. data, err := io.ReadAll(reader)
  334. if err != nil {
  335. panic(err)
  336. }
  337. if _, err := c.Write(data); err != nil {
  338. c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
  339. return
  340. }
  341. } else { // 多条数据先打包后下载文件
  342. fileName := sceneId + "_" + typeName + ".zip"
  343. filePath, tmpDir, err := generateZipByKey(ctx, req, fileName)
  344. if err != nil {
  345. c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
  346. }
  347. fmt.Println("filePath", filePath)
  348. // 检查文件是否存在
  349. if _, err := os.Stat(filePath); os.IsNotExist(err) {
  350. c.JSON(http.StatusNotFound, map[string]string{"error": "File not found"})
  351. return
  352. }
  353. // 打开文件
  354. f, err := os.Open(filePath)
  355. if err != nil {
  356. c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to open file"})
  357. return
  358. }
  359. defer f.Close()
  360. // 设置响应头
  361. c.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filepath.Base(filePath)))
  362. c.Response.Header.Set("Content-Type", "binary/octet-stream")
  363. // 将文件流式传输回客户端
  364. data, err := io.ReadAll(f)
  365. if err != nil {
  366. panic(err)
  367. }
  368. if _, err := c.Write(data); err != nil {
  369. c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
  370. return
  371. }
  372. defer os.RemoveAll(tmpDir)
  373. }
  374. }
  375. // 根据object keys生成压缩包
  376. func generateZipByKey(ctx context.Context, keys []string, fileName string) (file string, tmpDir string, err error) {
  377. // 创建临时文件夹
  378. tmpDir, err = os.MkdirTemp("", "temp-download-*")
  379. fmt.Println("tmpDir:", tmpDir)
  380. if err != nil {
  381. fmt.Println("Error creating temporary directory:", err)
  382. return "", "", err
  383. }
  384. c_log.GlobalLogger.Info("创建下载-临时文件夹:", tmpDir)
  385. // 创建根文件夹(文件打包的根目录)
  386. baseDir := filepath.Join(tmpDir, "data")
  387. if err := os.Mkdir(baseDir, 0755); err != nil {
  388. fmt.Println("Error creating subdirectory:", err)
  389. return "", "", err
  390. }
  391. c_log.GlobalLogger.Info("创建文件打包根目录:", baseDir)
  392. // 从oss下载文件到根目录
  393. for _, file := range keys {
  394. err = config.OssBucket.GetObjectToFile(file, filepath.Join(baseDir, filepath.Base(file)))
  395. if err != nil {
  396. fmt.Println("Error downloading file:", file, err)
  397. return "", "", err
  398. }
  399. }
  400. c_log.GlobalLogger.Info("下载oss文件到根目录 - 成功")
  401. // 创建压缩文件
  402. zipPath := filepath.Join(tmpDir, fileName)
  403. zipFile, err := os.Create(zipPath)
  404. if err != nil {
  405. fmt.Println("Error creating ZIP file:", err)
  406. return "", "", err
  407. }
  408. defer zipFile.Close()
  409. zipWriter := zip.NewWriter(zipFile)
  410. defer zipWriter.Close()
  411. // 压缩文件夹
  412. if err := util.AddDirToZip(baseDir, zipWriter); err != nil {
  413. fmt.Println("Error adding directory to ZIP:", err)
  414. return "", "", err
  415. }
  416. fmt.Println("ZIP file created successfully.")
  417. c_log.GlobalLogger.Info("创建压缩文件 - 成功")
  418. return zipPath, tmpDir, nil
  419. }