monitor.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package controller
  2. import (
  3. "bigdata_archives/app/e"
  4. "bigdata_archives/global"
  5. "bigdata_archives/simple_zap"
  6. "bigdata_archives/unity"
  7. "bigdata_archives/utils"
  8. "context"
  9. "encoding/json"
  10. "github.com/gin-gonic/gin"
  11. "io"
  12. "log"
  13. "os"
  14. "path/filepath"
  15. "strconv"
  16. "time"
  17. )
  18. type EventLog struct {
  19. utils.BaseModel
  20. IPAddress string `gorm:"column:ip_address" json:"ipAddress"`
  21. IPv6Address string `gorm:"column:ipv6_address" json:"ipv6Address"`
  22. PortNo int `gorm:"column:port_no" json:"portNo"`
  23. Protocol string `gorm:"column:protocol" json:"protocol"`
  24. MacAddress string `gorm:"column:mac_address" json:"macAddress"`
  25. ChannelID int `gorm:"column:channel_id" json:"channelID"`
  26. DateTime string `gorm:"column:date_time" json:"dateTime"`
  27. ActivePostCount int `gorm:"column:active_post_count" json:"activePostCount"`
  28. EventType string `gorm:"column:event_type" json:"eventType"`
  29. EventState string `gorm:"column:event_state" json:"eventState"`
  30. EventDescription string `gorm:"column:event_description" json:"eventDescription"`
  31. ShortSerialNumber string `gorm:"column:short_serial_number" json:"shortSerialNumber"`
  32. AccessControllerEvent AccessControllerEvent `gorm:"-" json:"AccessControllerEvent"`
  33. }
  34. type AccessControllerEvent struct {
  35. utils.BaseModel
  36. DeviceName string `gorm:"column:device_name" json:"deviceName"`
  37. MajorEventType int `gorm:"column:major_event_type" json:"majorEventType"`
  38. SubEventType int `gorm:"column:sub_event_type" json:"subEventType"`
  39. CardReaderKind int `gorm:"column:card_reader_kind" json:"cardReaderKind"`
  40. CardReaderNo int `gorm:"column:card_reader_no" json:"cardReaderNo"`
  41. VerifyNo int `gorm:"column:verify_no" json:"verifyNo"`
  42. SerialNo int `gorm:"column:serial_no" json:"serialNo"`
  43. CurrentVerifyMode string `gorm:"column:current_verify_mode" json:"currentVerifyMode"`
  44. FrontSerialNo int `gorm:"column:front_serial_no" json:"frontSerialNo"`
  45. AttendanceStatus string `gorm:"column:attendance_status" json:"attendanceStatus"`
  46. Label string `gorm:"column:label" json:"label"`
  47. StatusValue int `gorm:"column:status_value" json:"statusValue"`
  48. Mask string `gorm:"column:mask" json:"mask"`
  49. Helmet string `gorm:"column:helmet" json:"helmet"`
  50. PicturesNumber int `gorm:"column:pictures_number" json:"picturesNumber"`
  51. PurePwdVerifyEnable bool `gorm:"column:pure_pwd_verify_enable" json:"purePwdVerifyEnable"`
  52. FaceImagePath string `gorm:"column:face_image_path" json:"faceImagePath"`
  53. IPAddress string `gorm:"column:ip_address" json:"ipAddress"`
  54. FaceRect FaceRect `gorm:"-" json:"FaceRect"`
  55. }
  56. func (acc *AccessControllerEvent) TableName() string {
  57. return "access_controller_events"
  58. }
  59. type FaceRect struct {
  60. Height float64 `gorm:"column:height" json:"height"`
  61. Width float64 `gorm:"column:width" json:"width"`
  62. X float64 `gorm:"column:x" json:"x"`
  63. Y float64 `gorm:"column:y" json:"y"`
  64. }
  65. func Monitor(c *gin.Context) {
  66. // 解析 multipart/form-data 请求
  67. err := c.Request.ParseMultipartForm(10 << 20) // 设置最大文件大小为 10MB
  68. if err != nil {
  69. //e.ResponseWithMsg(c, e.ERROR, "读取表单失败")
  70. simple_zap.WithCtx(context.Background()).Sugar().Error(err, "读取表单失败")
  71. return
  72. }
  73. // 处理 JSON 数据
  74. jsonData, ok := c.Request.Form["event_log"]
  75. if !ok {
  76. //e.ResponseWithMsg(c, e.ERROR, "缺少 event_log 参数")
  77. simple_zap.WithCtx(context.Background()).Sugar().Error(err, "缺少 event_log 参数")
  78. return
  79. }
  80. jsonStr := jsonData[0]
  81. // 解析 JSON
  82. var eventLog EventLog
  83. err = json.Unmarshal([]byte(jsonStr), &eventLog)
  84. if err != nil {
  85. //e.ResponseWithMsg(c, e.ERROR, "json解析失败")
  86. simple_zap.WithCtx(context.Background()).Sugar().Error(err, "json解析失败")
  87. return
  88. }
  89. // 处理图片
  90. fileHeader, err := c.FormFile("Picture")
  91. if err != nil {
  92. //e.ResponseWithMsg(c, e.ERROR, "未获取文件图片")
  93. simple_zap.WithCtx(context.Background()).Sugar().Error(err, "未获取文件图片")
  94. return
  95. }
  96. open, err := fileHeader.Open()
  97. // 打开文件
  98. if err != nil {
  99. //e.ResponseWithMsg(c, e.ERROR, "打开文件失败")
  100. simple_zap.WithCtx(context.Background()).Sugar().Error(err, "打开文件失败")
  101. return
  102. }
  103. // 读取文件内容
  104. fileBytes, err := io.ReadAll(open)
  105. if err != nil {
  106. //e.ResponseWithMsg(c, e.ERROR, "读取文件失败")
  107. simple_zap.WithCtx(context.Background()).Sugar().Error(err, "读取文件失败")
  108. return
  109. }
  110. // 保存图片到本地文件
  111. err, path := savePicture(fileBytes)
  112. if err != nil {
  113. log.Println(err)
  114. //e.ResponseWithMsg(c, e.ERROR, "保存文件失败")
  115. simple_zap.WithCtx(context.Background()).Sugar().Error(err, "保存文件失败")
  116. return
  117. }
  118. var access AccessControllerEvent
  119. access = eventLog.AccessControllerEvent
  120. access.FaceImagePath = path
  121. access.IPAddress = eventLog.IPAddress
  122. tx := global.DBLink.Create(&eventLog)
  123. tx = global.DBLink.Create(&access)
  124. if tx.Error == nil && tx.RowsAffected > 0 {
  125. //e.ResponseSuccess(c, "200")
  126. return
  127. }
  128. // 返回响应
  129. //e.ResponseSuccess(c, "200")
  130. }
  131. // Getmonitor 获取门禁记录
  132. func Getmonitor(c *gin.Context) {
  133. var params unity.TimePageParams
  134. var access AccessControllerEvent
  135. err := c.ShouldBindJSON(&params)
  136. if err != nil {
  137. simple_zap.WithCtx(context.Background()).Sugar().Error(err, "参数解析失败")
  138. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  139. return
  140. }
  141. result, total, err := unity.PaginateWithTime(params, access.TableName(), &access)
  142. if err != nil {
  143. e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
  144. return
  145. }
  146. e.ResPonsePageTime(c, result, total, params)
  147. }
  148. // 保存图片到本地文件
  149. func savePicture(data []byte) (error, string) {
  150. // 指定保存图片的路径
  151. unix := time.Now().Unix()
  152. picturePath := "/image/" + strconv.FormatInt(unix, 10) + ".jpg"
  153. // 创建目录
  154. if err := os.MkdirAll(filepath.Dir(picturePath), os.ModePerm); err != nil {
  155. return err, ""
  156. }
  157. // 创建文件
  158. file, err := os.Create(picturePath)
  159. if err != nil {
  160. return err, ""
  161. }
  162. defer file.Close()
  163. // 写入图片数据
  164. _, err = file.Write(data)
  165. if err != nil {
  166. return err, ""
  167. }
  168. return nil, picturePath
  169. }