package api import ( "AIT/common" "AIT/exception/errmsg" "AIT/global" "AIT/models" "AIT/response" "AIT/service" "encoding/json" "fmt" "github.com/gin-gonic/gin" "github.com/gorilla/websocket" "path/filepath" ) type RecordService struct { service.RecordService } func GetRecord() *RecordService { return &RecordService{} } // SaveRecord // @Summary 新增测试 // @Description 新增测试 // @Tags 测试记录 // @Param file formData file true "文件" // @Param userName formData string true "用户名" // @Param fileType formData string true "文件类型" // @Param positiveDir formData string false "方向" // @Param negativaDir formData string false "方向" // @Param taskType formData string true "car_classification :车辆识别 ; car_direction:方向; class_direc =car_classification+car_direction;" // @Accept json // @Produce json // @Success 200 {string} string "ok" // @Router /v1/record/create [post] // @Security ApiKeyAuth func (f *RecordService) SaveRecord(c *gin.Context) { file, err := c.FormFile("file") if err != nil { fmt.Printf(errmsg.FileUploadError+"%s", err.Error()) response.Failed(errmsg.FileUploadError, c) return } message := make(map[string]string) userName := c.PostForm("userName") if len(userName) <= 0 { response.Failed(errmsg.ParamInvalid+",用户名为空", c) return } fileType := c.PostForm("fileType") positiveDir := c.PostForm("positiveDir") negativaDir := c.PostForm("negativaDir") taskType := c.PostForm("taskType") message["msg_type"] = fileType message["Sender"] = "server" message["Recipient"] = "client" message["positive_dir"] = positiveDir message["negativa_dir"] = negativaDir message["task_type"] = taskType if (taskType == "car_direction" || taskType == "class_direc") && (len(positiveDir) == 0 || len(negativaDir) == 0) { response.Failed(errmsg.ParamInvalid+",方向为空", c) return } //task_type 类型:car_classification :车辆识别 car_direction方向 class_direc =car_classification+car_direction //positive_dir //negativa_dir //添加记录 var record models.Record //文件格式限制 fileName := file.Filename record.FileName = fileName fileExtendName := common.CheckFile(fileName) if len(fileExtendName) <= 0 { response.Failed(errmsg.FileExtendNameError, c) return } //fmt.Printf("fileName====%s\n", fileName) fileName = common.GenerateUUID() + "." + fileExtendName fileLocation := global.Config.Upload //保存文件 err = c.SaveUploadedFile(file, fileLocation.SavePath+fileName) if err != nil { return } //获取文件绝对路径 fileAbsolutePath, err := filepath.Abs(fileLocation.SavePath + fileName) if err != nil { return } message["content"] = fileAbsolutePath fileURL := fileLocation.AccessUrl + fileName record.FilePath = fileAbsolutePath record.CreateTime = common.NowTime() record.UserName = userName record.FileType = fileType record.Id = common.GenerateUUID() record.FileUrl = fileURL record.TaskType = taskType record.PositiveDir = positiveDir record.NegativaDir = negativaDir message["recordId"] = record.Id //invoke AI ws, _, err := websocket.DefaultDialer.Dial(global.Config.Websocket.WSUrl, nil) if err != nil { fmt.Println("Websocket client init error" + err.Error()) return } //defer func(ws *websocket.Conn) { // err := ws.Close() // if err != nil { // fmt.Printf("socket关闭异常:%s", err.Error()) // response.Failed(errmsg.SocketCloseError, c) // } //}(ws) //发送消息 bytes, err := json.Marshal(message) if err != nil { fmt.Printf("json异常:%s", err.Error()) response.Failed(errmsg.JSONParseError, c) return } err = ws.WriteMessage(websocket.TextMessage, bytes) if err != nil { fmt.Printf("json异常:%s", err.Error()) response.Failed(errmsg.SendMessageError, c) return } if count := f.CreateRecord(record); count <= 0 { response.Failed(errmsg.RecordSaveError, c) } else { response.Success(errmsg.RecordSaveSuccess, record.Id, c) } } // RecordList // @Summary 记录列表查询 // @Description 记录列表查询 // @Tags 测试记录 // @Param r body string true "role:角色;userName:用户名;pageNum:页数;pageSize:每页记录数" // @Accept json // @Produce json // @Success 200 {string} string "ok" // @Router /v1/record/find [post] func (f *RecordService) RecordList(c *gin.Context) { var param models.RecordListParam if err := c.ShouldBindJSON(¶m); err != nil { fmt.Printf(errmsg.ParamInvalid+"%s", err.Error()) response.Failed(errmsg.ParamInvalid, c) return } recordList, rows := f.GetRecordList(param) response.SuccessPage(errmsg.FindSuccess, recordList, rows, c) } // GetRecordById // @Summary 记录详情查询 // @Description 记录详情查询 // @Tags 测试记录 // @Param r path string true "测试记录Id" // @Accept json // @Produce json // @Success 200 {string} string "ok" // @Router /v1/record/:recordId/find [get] func (f *RecordService) GetRecordById(c *gin.Context) { recordId := c.Param("recordId") record, count := f.FindById(recordId) if count > 0 { response.Success(errmsg.FindSuccess, record, c) } else { response.Failed(errmsg.RecordNotFoundError, c) } }