123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- package simulation_service
- import (
- "archive/zip"
- "context"
- "encoding/json"
- "fmt"
- "github.com/cloudwego/hertz/pkg/app"
- "github.com/cloudwego/hertz/pkg/protocol/consts"
- uuid "github.com/satori/go.uuid"
- "io"
- "net/http"
- "os"
- "path/filepath"
- "pji_desktop_http/biz/dal/mysql"
- "pji_desktop_http/biz/model"
- "pji_desktop_http/common/config"
- "pji_desktop_http/common/config/c_log"
- "pji_desktop_http/common/entity"
- "pji_desktop_http/common/util"
- )
- // DownloadSimulationZipFile 根据请求id从oss拉取并打包仿真测试所需要的文件
- // @router /simulation/download/zipfile [GET]
- func DownloadSimulationZipFile(ctx context.Context, c *app.RequestContext) {
- id := c.Query("id")
- fmt.Println(id)
- // 根据id生成用于地图更新的压缩包
- filePath, tmpDir, err := generateSimulationZipById(ctx, id)
- if err != nil {
- c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
- }
- fmt.Println("filePath", filePath)
- // 检查文件是否存在
- if _, err := os.Stat(filePath); os.IsNotExist(err) {
- c.JSON(http.StatusNotFound, map[string]string{"error": "File not found"})
- return
- }
- // 打开文件
- f, err := os.Open(filePath)
- if err != nil {
- c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to open file"})
- return
- }
- defer f.Close()
- // 设置响应头
- c.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filepath.Base(filePath)))
- c.Response.Header.Set("Content-Type", "binary/octet-stream")
- // 将文件流式传输回客户端
- data, err := io.ReadAll(f)
- if err != nil {
- panic(err)
- }
- if _, err := c.Write(data); err != nil {
- c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- defer os.RemoveAll(tmpDir)
- }
- // 根据id生成用于仿真测试的压缩包
- func generateSimulationZipById(ctx context.Context, id string) (file string, tmpDir string, err error) {
- // 根据id获取对应的oss文件列表
- allFileList, err := util.GetExactedMapFileById(id)
- //fmt.Println("Filtered Strings:", fileList)
- if err != nil {
- return
- }
- // 创建临时文件夹
- tmpDir, err = os.MkdirTemp("", "temp-download-*")
- fmt.Println("tmpDir:", tmpDir)
- if err != nil {
- fmt.Println("Error creating temporary directory:", err)
- return "", "", err
- }
- c_log.GlobalLogger.Info("创建下载-临时文件夹:", tmpDir)
- // 创建根文件夹(文件打包的根目录)
- baseDir := filepath.Join(tmpDir, "data")
- if err := os.Mkdir(baseDir, 0755); err != nil {
- fmt.Println("Error creating subdirectory:", err)
- return "", "", err
- }
- c_log.GlobalLogger.Info("创建文件打包根目录:", baseDir)
- // 从oss下载data.zip, map.pgm, map.yaml文件到根目录
- // 过滤特定后缀的文件列表
- simulationFileList := util.FilterBySuffixes(allFileList, config.SimulationFiltersuffixes...)
- fmt.Println("simulationFileList", simulationFileList)
- // 从oss下载文件到 根目录
- for _, file := range simulationFileList {
- err = config.OssBucket.GetObjectToFile(file, filepath.Join(baseDir, filepath.Base(file)))
- if err != nil {
- fmt.Println("Error downloading file - data.zip, map.pgm, map.yaml, map.bag:", err)
- return "", "", err
- }
- }
- c_log.GlobalLogger.Info("下载data.zip, map.pgm, map.yaml, map.bag文件到根目录 - 成功")
- // 下载world文件&map.stl文件
- // 查询状态
- world, err := mysql.QueryWorld(ctx, id)
- if err != nil || world == nil { // 记录不存在
- c_log.GlobalLogger.Info("world记录不存在 - 跳过")
- } else { // 记录存在
- // 下载world文件
- worldURL := world.WorldURL
- err = config.OssBucket.GetObjectToFile(worldURL, filepath.Join(baseDir, filepath.Base(worldURL)))
- if err != nil {
- fmt.Println("Error downloading world file:", err)
- return "", "", err
- }
- c_log.GlobalLogger.Info("下载world文件到根目录 - 成功")
- // 下载map.stl文件
- stlURL := world.StlURL
- if stlURL != nil && *stlURL != "" {
- err = config.OssBucket.GetObjectToFile(*stlURL, filepath.Join(baseDir, filepath.Base(*stlURL)))
- if err != nil {
- fmt.Println("Error downloading stl file:", err)
- return "", "", err
- }
- c_log.GlobalLogger.Info("下载map.stl文件到根目录 - 成功")
- } else {
- c_log.GlobalLogger.Info("map.stl文件不存在 - 跳过")
- }
- }
- // 下载原始bag
- rosField, err := util.GetRosFileById(id)
- if err != nil {
- fmt.Println("Error querying origin map's bag file:", err)
- return "", "", err
- }
- err = config.OssBucket.GetObjectToFile(rosField, filepath.Join(baseDir, "origin_map.bag"))
- if err != nil {
- fmt.Println("Error downloading origin map's bag file:", err)
- return "", "", err
- }
- c_log.GlobalLogger.Info("下载origin_map.bag文件到根目录 - 成功")
- // 创建压缩文件
- zipPath := filepath.Join(tmpDir, "simulationFile-"+id+".zip")
- zipFile, err := os.Create(zipPath)
- if err != nil {
- fmt.Println("Error creating ZIP file:", err)
- return "", "", err
- }
- defer zipFile.Close()
- zipWriter := zip.NewWriter(zipFile)
- defer zipWriter.Close()
- // 压缩文件夹
- if err := util.AddDirToZip(baseDir, zipWriter); err != nil {
- fmt.Println("Error adding directory to ZIP:", err)
- return "", "", err
- }
- fmt.Println("ZIP file created successfully.")
- c_log.GlobalLogger.Info("创建压缩文件 - 成功")
- return zipPath, tmpDir, nil
- }
- // CheckDataFileStatus 检查data目录是否存在
- // @router /simulation/check/file/data/status [GET]
- func CheckDataFileStatus(ctx context.Context, c *app.RequestContext) {
- sceneID := c.Query("id")
- fmt.Println("id", sceneID)
- // 根据id获取对应的oss文件列表
- allFileList, err := util.GetExactedMapFileById(sceneID)
- if err != nil {
- return
- }
- // 过滤特定后缀的文件列表(data.zip)
- simulationFileList := util.FilterBySuffixes(allFileList, config.DataFiltersuffixes...)
- if len(simulationFileList) == 0 {
- c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "data目录不存在"})
- return
- }
- c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "data目录存在"})
- }
- // UploadPdfFile 将result.pdf文件上传到oss
- // @router /simulation/upload/pdf [GET]
- func UploadPdfFile(ctx context.Context, c *app.RequestContext) {
- equipmentNo := c.Query("equipmentNo")
- fmt.Println("equipmentNo", equipmentNo)
- sceneNo := c.Query("sceneNo")
- fmt.Println("sceneNo", sceneNo)
- timeStamp := c.Query("timeStamp")
- fmt.Println("timeStamp", timeStamp)
- round := c.Query("round")
- fmt.Println("round", round)
- header, err := c.FormFile("file")
- if err != nil {
- c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
- return
- }
- fileName := header.Filename
- fmt.Println("filename", fileName)
- ossObjectKey := config.SimulationOssBasePrefix + "/" + equipmentNo + "/" + sceneNo + "/" + timeStamp + "/" + round + "/" + fileName
- fmt.Println("ossObjectKey", ossObjectKey)
- f, _ := header.Open()
- defer f.Close()
- config.OssMutex.Lock()
- err = config.OssBucket.PutObject(ossObjectKey, f)
- config.OssMutex.Unlock()
- if err != nil {
- c_log.GlobalLogger.Error("程序异常退出。上传文件", fileName, "->", ossObjectKey, "出错:", err)
- c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "上传文件失败", Details: ""})
- return
- }
- c_log.GlobalLogger.Info("上传文件", fileName, "->", ossObjectKey, "成功。")
- c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传文件成功", Details: ossObjectKey})
- }
- // UploadBagFile 将test-${i}.bag文件上传到oss
- // @router /simulation/upload/bag [GET]
- func UploadBagFile(ctx context.Context, c *app.RequestContext) {
- equipmentNo := c.Query("equipmentNo")
- fmt.Println("equipmentNo", equipmentNo)
- sceneNo := c.Query("sceneNo")
- fmt.Println("sceneNo", sceneNo)
- timeStamp := c.Query("timeStamp")
- fmt.Println("timeStamp", timeStamp)
- round := c.Query("round")
- fmt.Println("round", round)
- header, err := c.FormFile("file")
- if err != nil {
- c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
- return
- }
- fileName := header.Filename
- fmt.Println("filename", fileName)
- ossObjectKey := config.SimulationOssBasePrefix + "/" + equipmentNo + "/" + sceneNo + "/" + timeStamp + "/" + round + "/" + fileName
- fmt.Println("ossObjectKey", ossObjectKey)
- f, _ := header.Open()
- defer f.Close()
- config.OssMutex.Lock()
- err = config.OssBucket.PutObject(ossObjectKey, f)
- config.OssMutex.Unlock()
- if err != nil {
- c_log.GlobalLogger.Error("程序异常退出。上传文件", fileName, "->", ossObjectKey, "出错:", err)
- c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "上传文件失败", Details: ""})
- return
- }
- c_log.GlobalLogger.Info("上传文件", fileName, "->", ossObjectKey, "成功。")
- c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传文件成功", Details: ossObjectKey})
- }
- // UploadPgmFile 将map.pgm文件上传到oss
- // @router /simulation/upload/pgm [GET]
- func UploadPgmFile(ctx context.Context, c *app.RequestContext) {
- equipmentNo := c.Query("equipmentNo")
- fmt.Println("equipmentNo", equipmentNo)
- sceneNo := c.Query("sceneNo")
- fmt.Println("sceneNo", sceneNo)
- header, err := c.FormFile("file")
- if err != nil {
- c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
- return
- }
- fileName := header.Filename
- fmt.Println("filename", fileName)
- ossObjectKey := config.SimulationOssBasePrefix + "/" + equipmentNo + "/" + sceneNo + "/" + fileName
- fmt.Println("ossObjectKey", ossObjectKey)
- f, _ := header.Open()
- defer f.Close()
- config.OssMutex.Lock()
- err = config.OssBucket.PutObject(ossObjectKey, f)
- config.OssMutex.Unlock()
- if err != nil {
- c_log.GlobalLogger.Error("程序异常退出。上传文件", fileName, "->", ossObjectKey, "出错:", err)
- c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "上传文件失败", Details: ""})
- return
- }
- c_log.GlobalLogger.Info("上传文件", fileName, "->", ossObjectKey, "成功。")
- c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传文件成功", Details: ossObjectKey})
- }
- // AddSimulationRecord 添加仿真测试记录
- // @router /simulation/add/record [GET]
- func AddSimulationRecord(ctx context.Context, c *app.RequestContext) {
- var records []*model.SimulationTestRecord
- err := c.BindAndValidate(&records)
- for _, record := range records {
- record.ID = uuid.NewV1().String()
- }
- fmt.Println("records", records)
- if err != nil {
- c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
- return
- }
- err = mysql.AddSimulationTestRecords(ctx, records)
- if err != nil {
- c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "仿真测试记录添加失败"})
- return
- }
- c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "仿真测试记录添加成功"})
- }
- // QueryEvalRecord 查询算法评价记录(根据场景id及时间戳)
- // @router /simulation/query/eval/record [GET]
- func QueryEvalRecord(ctx context.Context, c *app.RequestContext) {
- sceneId := c.Query("sceneId")
- fmt.Println("sceneId", sceneId)
- testTime := c.Query("testTime")
- fmt.Println("testTime", testTime)
- records, err := mysql.QuerySimulationTestRecords(ctx, sceneId, testTime)
- if err != nil {
- c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "仿真测试记录查询失败", Details: ""})
- return
- }
- output, err := json.Marshal(records)
- if err != nil {
- c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "仿真测试记录查询失败", Details: ""})
- return
- }
- c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "仿真测试记录查询成功", Details: string(output)})
- }
- // DownloadFileByKeys 给定object key数组从oss下载(打包)文件
- // @router /simulation/download/oss/key [POST]
- func DownloadFileByKeys(ctx context.Context, c *app.RequestContext) {
- sceneId := c.Query("sceneId")
- fmt.Println("sceneId", sceneId)
- typeName := c.Query("typeName")
- fmt.Println("typeName", typeName)
- var req []string
- err := c.BindAndValidate(&req)
- if err != nil {
- c.String(consts.StatusBadRequest, err.Error())
- return
- }
- fmt.Println("req", req)
- if len(req) == 0 {
- c.JSON(consts.StatusBadRequest, entity.HttpResult{Status: false, Code: "", Message: "请求数据为空。"})
- } else if len(req) == 1 { // 只有一条数据则直接下载文件
- objectKey := req[0]
- // 从OSS下载文件
- reader, err := config.OssBucket.GetObject(objectKey)
- if err != nil {
- c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- defer reader.Close()
- // 设置响应头
- c.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filepath.Base(objectKey)))
- c.Response.Header.Set("Content-Type", "binary/octet-stream")
- // 将文件流式传输回客户端
- data, err := io.ReadAll(reader)
- if err != nil {
- panic(err)
- }
- if _, err := c.Write(data); err != nil {
- c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- } else { // 多条数据先打包后下载文件
- fileName := sceneId + "_" + typeName + ".zip"
- filePath, tmpDir, err := generateZipByKey(ctx, req, fileName)
- if err != nil {
- c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
- }
- fmt.Println("filePath", filePath)
- // 检查文件是否存在
- if _, err := os.Stat(filePath); os.IsNotExist(err) {
- c.JSON(http.StatusNotFound, map[string]string{"error": "File not found"})
- return
- }
- // 打开文件
- f, err := os.Open(filePath)
- if err != nil {
- c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to open file"})
- return
- }
- defer f.Close()
- // 设置响应头
- c.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filepath.Base(filePath)))
- c.Response.Header.Set("Content-Type", "binary/octet-stream")
- // 将文件流式传输回客户端
- data, err := io.ReadAll(f)
- if err != nil {
- panic(err)
- }
- if _, err := c.Write(data); err != nil {
- c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
- return
- }
- defer os.RemoveAll(tmpDir)
- }
- }
- // 根据object keys生成压缩包
- func generateZipByKey(ctx context.Context, keys []string, fileName string) (file string, tmpDir string, err error) {
- // 创建临时文件夹
- tmpDir, err = os.MkdirTemp("", "temp-download-*")
- fmt.Println("tmpDir:", tmpDir)
- if err != nil {
- fmt.Println("Error creating temporary directory:", err)
- return "", "", err
- }
- c_log.GlobalLogger.Info("创建下载-临时文件夹:", tmpDir)
- // 创建根文件夹(文件打包的根目录)
- baseDir := filepath.Join(tmpDir, "data")
- if err := os.Mkdir(baseDir, 0755); err != nil {
- fmt.Println("Error creating subdirectory:", err)
- return "", "", err
- }
- c_log.GlobalLogger.Info("创建文件打包根目录:", baseDir)
- // 从oss下载文件到根目录
- for _, file := range keys {
- err = config.OssBucket.GetObjectToFile(file, filepath.Join(baseDir, filepath.Base(file)))
- if err != nil {
- fmt.Println("Error downloading file:", file, err)
- return "", "", err
- }
- }
- c_log.GlobalLogger.Info("下载oss文件到根目录 - 成功")
- // 创建压缩文件
- zipPath := filepath.Join(tmpDir, fileName)
- zipFile, err := os.Create(zipPath)
- if err != nil {
- fmt.Println("Error creating ZIP file:", err)
- return "", "", err
- }
- defer zipFile.Close()
- zipWriter := zip.NewWriter(zipFile)
- defer zipWriter.Close()
- // 压缩文件夹
- if err := util.AddDirToZip(baseDir, zipWriter); err != nil {
- fmt.Println("Error adding directory to ZIP:", err)
- return "", "", err
- }
- fmt.Println("ZIP file created successfully.")
- c_log.GlobalLogger.Info("创建压缩文件 - 成功")
- return zipPath, tmpDir, nil
- }
|