package repository import ( "encoding/base64" "encoding/json" "time" "gat1400Exchange/config" "gat1400Exchange/models" "gat1400Exchange/pkg/logger" "gat1400Exchange/util" "gat1400Exchange/vo" uuid "github.com/satori/go.uuid" ) type CaptureRepository struct { } func NewCaptureRepository() CaptureRepository { return CaptureRepository{} } func (c CaptureRepository) FaceForward(faceList []vo.FaceObject) { if faceList == nil || len(faceList) == 0 { logger.Warn("FaceList is nil") return } for _, face := range faceList { if face.SubImageList.SubImageInfoObject == nil { logger.Warn("SubImageInfoObject is nil") continue } var deviceId = face.DeviceID var faceId = face.FaceID var faceImageStr string // 获取大图, 目前海康的小图分辨率太低 for _, image := range face.SubImageList.SubImageInfoObject { if len(image.Data) > len(faceImageStr) { faceImageStr = image.Data } } // 转发图像 logger.Debug("Prepare forward image, deviceId:%s, image len:%d, server:%s", deviceId, len(faceImageStr), config.ForwardConf.SyncServer) if deviceId != "" && faceImageStr != "" && config.ForwardConf.SyncServer != "" { pd := c.PackPushDataV2(deviceId, faceId, faceImageStr, face.FaceAppearTime) if pd == nil { return } 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) logger.Warn("The data forwarding failed, adding to local cache.") } else { logger.Debug("The data forwarding successful. deviceId:%s", deviceId) } } } return } func (c CaptureRepository) PackPushDataV2(deviceId, faceId, faceImage, appearTime string) *vo.PushDataInfoV2 { var pd = new(vo.PushDataInfoV2) 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 } // 匹配楼层 faceAppearTime, err := time.ParseInLocation("20060102150405", appearTime, time.Local) if err != nil { logger.Warn("Parse face appear time error, %s", err.Error()) faceAppearTime = time.Now() } var devPos models.Positions _ = devPos.FindDevicePosition(deviceId, faceAppearTime.Unix()+5) // 加5秒电梯关门的时间 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) pd.PicId = faceId pd.PicDate = faceAppearTime.Format("2006-01-02 15:04:05") pd.DataSource = "camera" pd.CameraFloor = devPos.Pos pd.CameraId = deviceId return pd } func (c CaptureRepository) PackPushData(deviceId, faceId, faceImage, appearTime string) *vo.PushDataInfo { var pd = new(vo.PushDataInfo) 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 } // 匹配楼层 faceAppearTime, err := time.ParseInLocation("20060102150405", appearTime, time.Local) if err != nil { logger.Warn("Parse face appear time error, %s", err.Error()) faceAppearTime = time.Now() } // 电梯停止的时间 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 } func (c CaptureRepository) CacheData(payload []byte) { var cacheItem = models.Cache{ Data: string(payload), CreateTime: time.Now().Unix(), Retry: 0, } cacheItem.Save() }