package repository
|
|
import (
|
"bytes"
|
"encoding/base64"
|
"encoding/json"
|
"net/http"
|
"time"
|
|
"gat1400Exchange/config"
|
"gat1400Exchange/models"
|
"gat1400Exchange/pkg/logger"
|
"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("准备转发,deviceId:%s, image len:%d, server:%s", deviceId, len(faceImageStr), config.ForwardConf.SyncServer)
|
if deviceId != "" && faceImageStr != "" && config.ForwardConf.SyncServer != "" {
|
pd := c.PackPushData(deviceId, faceId, faceImageStr, face.FaceAppearTime)
|
if pd == nil {
|
return
|
}
|
|
rsp, err := c.SendData(pd, config.ForwardConf.SyncServer)
|
if err != nil {
|
logger.Warn("数据转发失败:%s", err.Error())
|
} else {
|
logger.Debug("数据转发成功,id:%s", rsp)
|
}
|
}
|
}
|
|
return
|
}
|
|
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
|
}
|
|
// 匹配楼层
|
aTime, err := time.ParseInLocation("20060102150405", appearTime, time.Local)
|
if err != nil {
|
logger.Warn("Parse face appear time error,%s", err.Error())
|
aTime = time.Now()
|
}
|
|
var devPos models.Positions
|
_ = devPos.FindDevicePosition(deviceId, aTime.Unix())
|
|
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: time.Now().Format("2006-01-02 15:04:05"),
|
LikeDate: time.Now().Format("2006-01-02 15:04:05"),
|
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) SendData(pushDataInfo *vo.PushDataInfo, url string) (id string, err error) {
|
id = ""
|
payload, err := json.Marshal(pushDataInfo)
|
if err != nil {
|
return id, err
|
}
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
|
if err != nil {
|
return id, err
|
}
|
|
client := &http.Client{}
|
resp, err := client.Do(req)
|
if err != nil {
|
return id, err
|
}
|
defer resp.Body.Close()
|
|
var responseData map[string]interface{}
|
err = json.NewDecoder(resp.Body).Decode(&responseData)
|
if err != nil {
|
return id, err
|
}
|
|
id = responseData["data"].(string)
|
// 处理响应
|
// 这里可以根据实际需求进行处理,例如读取响应内容或检查状态码等
|
|
return id, nil
|
|
}
|