record.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package api
  2. import (
  3. "AIT/common"
  4. "AIT/exception/errmsg"
  5. "AIT/global"
  6. "AIT/models"
  7. "AIT/response"
  8. "AIT/service"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/gin-gonic/gin"
  12. "github.com/gorilla/websocket"
  13. "path/filepath"
  14. )
  15. type RecordService struct {
  16. service.RecordService
  17. }
  18. func GetRecord() *RecordService {
  19. return &RecordService{}
  20. }
  21. // SaveRecord
  22. // @Summary 新增测试
  23. // @Description 新增测试
  24. // @Tags 测试记录
  25. // @Param file formData file true "文件"
  26. // @Param userName formData string true "用户名"
  27. // @Param fileType formData string true "文件类型"
  28. // @Param positiveDir formData string false "方向"
  29. // @Param negativaDir formData string false "方向"
  30. // @Param taskType formData string true "car_classification :车辆识别 ; car_direction:方向; class_direc =car_classification+car_direction;"
  31. // @Accept json
  32. // @Produce json
  33. // @Success 200 {string} string "ok"
  34. // @Router /v1/record/create [post]
  35. // @Security ApiKeyAuth
  36. func (f *RecordService) SaveRecord(c *gin.Context) {
  37. file, err := c.FormFile("file")
  38. if err != nil {
  39. fmt.Printf(errmsg.FileUploadError+"%s", err.Error())
  40. response.Failed(errmsg.FileUploadError, c)
  41. return
  42. }
  43. message := make(map[string]string)
  44. userName := c.PostForm("userName")
  45. if len(userName) <= 0 {
  46. response.Failed(errmsg.ParamInvalid+",用户名为空", c)
  47. return
  48. }
  49. fileType := c.PostForm("fileType")
  50. positiveDir := c.PostForm("positiveDir")
  51. negativaDir := c.PostForm("negativaDir")
  52. taskType := c.PostForm("taskType")
  53. message["msg_type"] = fileType
  54. message["Sender"] = "server"
  55. message["Recipient"] = "client"
  56. message["positive_dir"] = positiveDir
  57. message["negativa_dir"] = negativaDir
  58. message["task_type"] = taskType
  59. if (taskType == "car_direction" || taskType == "class_direc") && (len(positiveDir) == 0 || len(negativaDir) == 0) {
  60. response.Failed(errmsg.ParamInvalid+",方向为空", c)
  61. return
  62. }
  63. //task_type 类型:car_classification :车辆识别 car_direction方向 class_direc =car_classification+car_direction
  64. //positive_dir
  65. //negativa_dir
  66. //添加记录
  67. var record models.Record
  68. //文件格式限制
  69. fileName := file.Filename
  70. record.FileName = fileName
  71. fileExtendName := common.CheckFile(fileName)
  72. if len(fileExtendName) <= 0 {
  73. response.Failed(errmsg.FileExtendNameError, c)
  74. return
  75. }
  76. //fmt.Printf("fileName====%s\n", fileName)
  77. fileName = common.GenerateUUID() + "." + fileExtendName
  78. fileLocation := global.Config.Upload
  79. //保存文件
  80. err = c.SaveUploadedFile(file, fileLocation.SavePath+fileName)
  81. if err != nil {
  82. return
  83. }
  84. //获取文件绝对路径
  85. fileAbsolutePath, err := filepath.Abs(fileLocation.SavePath + fileName)
  86. if err != nil {
  87. return
  88. }
  89. message["content"] = fileAbsolutePath
  90. fileURL := fileLocation.AccessUrl + fileName
  91. record.FilePath = fileAbsolutePath
  92. record.CreateTime = common.NowTime()
  93. record.UserName = userName
  94. record.FileType = fileType
  95. record.Id = common.GenerateUUID()
  96. record.FileUrl = fileURL
  97. record.TaskType = taskType
  98. record.PositiveDir = positiveDir
  99. record.NegativaDir = negativaDir
  100. message["recordId"] = record.Id
  101. //invoke AI
  102. ws, _, err := websocket.DefaultDialer.Dial(global.Config.Websocket.WSUrl, nil)
  103. if err != nil {
  104. fmt.Println("Websocket client init error" + err.Error())
  105. return
  106. }
  107. //defer func(ws *websocket.Conn) {
  108. // err := ws.Close()
  109. // if err != nil {
  110. // fmt.Printf("socket关闭异常:%s", err.Error())
  111. // response.Failed(errmsg.SocketCloseError, c)
  112. // }
  113. //}(ws)
  114. //发送消息
  115. bytes, err := json.Marshal(message)
  116. if err != nil {
  117. fmt.Printf("json异常:%s", err.Error())
  118. response.Failed(errmsg.JSONParseError, c)
  119. return
  120. }
  121. err = ws.WriteMessage(websocket.TextMessage, bytes)
  122. if err != nil {
  123. fmt.Printf("json异常:%s", err.Error())
  124. response.Failed(errmsg.SendMessageError, c)
  125. return
  126. }
  127. if count := f.CreateRecord(record); count <= 0 {
  128. response.Failed(errmsg.RecordSaveError, c)
  129. } else {
  130. response.Success(errmsg.RecordSaveSuccess, record.Id, c)
  131. }
  132. }
  133. // RecordList
  134. // @Summary 记录列表查询
  135. // @Description 记录列表查询
  136. // @Tags 测试记录
  137. // @Param r body string true "role:角色;userName:用户名;pageNum:页数;pageSize:每页记录数"
  138. // @Accept json
  139. // @Produce json
  140. // @Success 200 {string} string "ok"
  141. // @Router /v1/record/find [post]
  142. func (f *RecordService) RecordList(c *gin.Context) {
  143. var param models.RecordListParam
  144. if err := c.ShouldBindJSON(&param); err != nil {
  145. fmt.Printf(errmsg.ParamInvalid+"%s", err.Error())
  146. response.Failed(errmsg.ParamInvalid, c)
  147. return
  148. }
  149. recordList, rows := f.GetRecordList(param)
  150. response.SuccessPage(errmsg.FindSuccess, recordList, rows, c)
  151. }
  152. // GetRecordById
  153. // @Summary 记录详情查询
  154. // @Description 记录详情查询
  155. // @Tags 测试记录
  156. // @Param r path string true "测试记录Id"
  157. // @Accept json
  158. // @Produce json
  159. // @Success 200 {string} string "ok"
  160. // @Router /v1/record/:recordId/find [get]
  161. func (f *RecordService) GetRecordById(c *gin.Context) {
  162. recordId := c.Param("recordId")
  163. record, count := f.FindById(recordId)
  164. if count > 0 {
  165. response.Success(errmsg.FindSuccess, record, c)
  166. } else {
  167. response.Failed(errmsg.RecordNotFoundError, c)
  168. }
  169. }