zhangzengfei
2024-06-03 7eb122e20a75ed800cc96b1565522951286e4081
repository/captureRepo.go
@@ -3,6 +3,7 @@
import (
   "encoding/base64"
   "encoding/json"
   "gat1400Exchange/pkg"
   "time"
   "gat1400Exchange/client"
@@ -11,8 +12,6 @@
   "gat1400Exchange/pkg/logger"
   "gat1400Exchange/util"
   "gat1400Exchange/vo"
   uuid "github.com/satori/go.uuid"
)
type CaptureRepository struct {
@@ -35,7 +34,6 @@
         logger.Warn("SubImageInfoObject is nil")
         continue
      }
      var deviceId = face.DeviceID
      var faceId = face.FaceID
      var bgImageStr, imageType string
@@ -98,6 +96,12 @@
            pd.CameraFloor = face.OtherFeature
         }
         // 尝试从faceId提取楼层
         if pd.CameraFloor == "" && config.ClientConf.AddFloorToFaceId {
            pd.CameraFloor, _ = pkg.ParseFloorFromId(face.FaceID)
         }
         //logger.Debug("device %s, CameraFloor:%s", deviceId, pd.CameraFloor)
         payload, err := json.Marshal(pd)
         if err != nil {
            logger.Warn("Marshal error, %s", err.Error())
@@ -117,58 +121,89 @@
   return
}
func (c CaptureRepository) PackPushData(deviceId, faceId, faceImage, appearTime string) *vo.PushDataInfo {
   var pd = new(vo.PushDataInfo)
   var device models.Device
func (c CaptureRepository) PersonForward(personList []vo.PersonObject) {
   var err error
   if err := device.FindById(deviceId); err != nil {
      logger.Warn("Can't find device in database, device:%s, %s", deviceId, err.Error())
      return pd
   if personList == nil || len(personList) == 0 {
      logger.Warn("FaceList is nil")
      return
   }
   // 匹配楼层
   faceAppearTime, err := time.ParseInLocation("20060102150405", appearTime, time.Local)
   if err != nil {
      logger.Warn("Parse face appear time error, %s", err.Error())
      faceAppearTime = time.Now()
   for _, v := range personList {
      if v.SubImageList.SubImageInfoObject == nil {
         logger.Warn("SubImageInfoObject is nil")
         continue
      }
      var deviceId = v.DeviceID
      var targetId = v.PersonID
      var bgImageStr string
      var bgImageBytes, faceImageBytes []byte = nil, nil
      // 获取大图, 目前海康的小图分辨率太低
      for _, image := range v.SubImageList.SubImageInfoObject {
         if image.Type != "14" {
            continue
         }
         if len(image.Data) > 0 {
            if len(image.Data) > len(bgImageStr) {
               bgImageStr = image.Data
            }
         } else if image.StoragePath != "" {
            imgData, err := util.ImageDownload(image.StoragePath, nil)
            if err != nil {
               logger.Warn("Image download failure, %s", err.Error())
            } else {
               bgImageBytes = imgData
               bgImageStr = "///"
            }
         }
      }
      if bgImageBytes == nil {
         bgImageBytes, err = base64.StdEncoding.DecodeString(bgImageStr)
         if err != nil {
            logger.Warn("Decode Image Base64 String failure, %s", err.Error())
            continue
         }
      }
      // 转发图像
      logger.Debug("Prepare forward person image, deviceId:%s, image len:%d, server:%s", deviceId, len(bgImageBytes), config.ForwardConf.SyncServer)
      if deviceId != "" && bgImageStr != "" && config.ForwardConf.SyncServer != "" {
         pd := c.PackPushDataV2(deviceId, targetId, v.PersonAppearTime, bgImageBytes, faceImageBytes)
         if pd == nil {
            return
         }
         // 处理梯控填写的楼层信息 暂时使用oherFeature字段
         if v.BehaviorDescription != "" {
            pd.CameraFloor = v.BehaviorDescription
         }
         // 尝试从faceId提取楼层
         if pd.CameraFloor == "" && config.ClientConf.AddFloorToFaceId {
            pd.CameraFloor, _ = pkg.ParseFloorFromId(v.PersonID)
         }
         //logger.Debug("device %s, CameraFloor:%s", deviceId, pd.CameraFloor)
         payload, err := json.Marshal(pd)
         if err != nil {
            logger.Warn("Marshal error, %s", err.Error())
            return
         }
         if !util.SendData(payload, config.ForwardConf.SyncServer) {
            cacheItem, _ := json.Marshal(pd)
            c.CacheData(cacheItem, "basic")
            logger.Warn("The data forwarding failed, adding to local cache.")
         } else {
            logger.Debug("The data forwarding successful. deviceId:%s", deviceId)
         }
      }
   }
   // 电梯停止的时间
   devStopTime := time.Now().Format("2006-01-02 15:04:05")
   var devPos models.Positions
   _ = devPos.FindDevicePosition(deviceId, faceAppearTime.Unix()+5) // 加5秒电梯关门的时间
   if devPos.TimeString != "" {
      devStopTime = devPos.TimeString
   }
   imageBytes, err := base64.StdEncoding.DecodeString(faceImage)
   if err != nil {
      logger.Warn("Decode Image Base64 String failure, %s", err.Error())
      return pd
   }
   pd.PicMaxImages = append(pd.PicMaxImages, imageBytes)
   tr := vo.TaskResultInfo{
      Id:            uuid.NewV4().String(),
      CameraId:      deviceId,
      CameraAddr:    device.Addr + devPos.Pos,
      CameraName:    device.Name,
      PicMaxUrl:     []string{""},
      PicDate:       faceAppearTime.Format("2006-01-02 15:04:05"),
      LikeDate:      devStopTime,
      AnalyServerId: deviceId,
      DataSource:    "camera",
      TargetInfo:    []vo.TargetInfo{{TargetId: faceId}},
   }
   pd.SourceData = vo.ESInfo{
      TaskResultInfo: tr,
      Version:        "3.3",
      UpdateTime:     time.Now().Format("2006-01-02 15:04:05"),
   }
   return pd
   return
}
func (c CaptureRepository) PackPushDataV2(deviceId, faceId, appearTime string, bgImgBytes, faceImgBytes []byte) *vo.PushDataInfoV2 {
@@ -182,19 +217,9 @@
   }
   if config.ServeConf.Role == "server" {
      var device models.Device
      if err := device.FindById(deviceId); err != nil {
         logger.Warn("Can't find device in database, device:%s, %s", deviceId, err.Error())
         return pd
      }
      // 匹配楼层
      var devPos models.Positions
      _ = devPos.FindDevicePosition(deviceId, faceAppearTime.Unix()+5) // 加5秒电梯关门的时间
      if devPos.Pos == "" {
         devPos.Pos = device.Floor
      }
      floor = devPos.Pos
   }
@@ -225,7 +250,7 @@
   cacheItem.Save()
}
func (c CaptureRepository) VIIDMsgForward(msg *vo.RequestFaceList) {
func (c CaptureRepository) VIIDFaceMsgForward(msg *vo.RequestFaceList) {
   faceInfo := msg.FaceListObject.FaceObject[0]
   // 匹配楼层
   faceAppearTime, err := time.ParseInLocation("20060102150405", faceInfo.FaceAppearTime, time.Local)
@@ -240,14 +265,49 @@
      devPos.Pos = "1F"
   }
   for idx, _ := range msg.FaceListObject.FaceObject {
   for idx, face := range msg.FaceListObject.FaceObject {
      msg.FaceListObject.FaceObject[idx].OtherFeature = devPos.Pos
      if config.ClientConf.AddFloorToFaceId {
         msg.FaceListObject.FaceObject[idx].FaceID = pkg.GenerateFaceIdContainFloor(face.FaceID, devPos.Pos)
      }
   }
   b, _ := json.Marshal(msg)
   if client.FaceCapture(b) != vo.StatusSuccess {
      cacheItem, _ := json.Marshal(msg)
      c.CacheData(cacheItem, "1400")
      c.CacheData(cacheItem, "1400-face")
      logger.Warn("The data forwarding failed, adding to local cache.")
   }
   return
}
func (c CaptureRepository) VIIDPersonMsgForward(msg *vo.RequestPersonList) {
   person := msg.PersonListObject.PersonObject[0]
   // 匹配楼层
   appearTime, err := time.ParseInLocation("20060102150405", person.PersonAppearTime, time.Local)
   if err != nil {
      logger.Warn("Parse face appear time error, %s", err.Error())
      appearTime = time.Now()
   }
   var devPos models.Positions
   _ = devPos.FindPositionByTime(appearTime.Unix() + 5) // 加5秒电梯关门的时间
   if devPos.Pos == "" {
      devPos.Pos = "1F"
   }
   for idx, v := range msg.PersonListObject.PersonObject {
      msg.PersonListObject.PersonObject[idx].BehaviorDescription = devPos.Pos
      if config.ClientConf.AddFloorToFaceId {
         msg.PersonListObject.PersonObject[idx].PersonID = pkg.GenerateFaceIdContainFloor(v.PersonID, devPos.Pos)
      }
   }
   b, _ := json.Marshal(msg)
   if client.PersonCapture(b) != vo.StatusSuccess {
      cacheItem, _ := json.Marshal(msg)
      c.CacheData(cacheItem, "1400-person")
      logger.Warn("The data forwarding failed, adding to local cache.")
   }