| | |
| | | logger.Debug("db open error ", err) |
| | | return err |
| | | } |
| | | _ = db.AutoMigrate(Device{}) |
| | | _ = db.AutoMigrate(&Device{}, &Positions{}) |
| | | |
| | | // 添加默认数据 |
| | | InitData() |
| | |
| | | package models |
| | | |
| | | import "gorm.io/gorm" |
| | | import ( |
| | | "gorm.io/gorm" |
| | | "time" |
| | | ) |
| | | |
| | | type Device struct { |
| | | Id string `gorm:"column:id;primary_key;" json:"id"` |
| | |
| | | if err == gorm.ErrRecordNotFound { |
| | | // 记录不存在,创建新记录 |
| | | db.Create(&d) |
| | | |
| | | } else { |
| | | // 其他错误,你可以进行适当的处理 |
| | | return err |
| | |
| | | db.Save(&existingDevice) |
| | | } |
| | | |
| | | // 记录位置的历史, 给人脸抓拍匹配楼层 |
| | | if existingDevice.Pos != "" { |
| | | var pos = Positions{ |
| | | DeviceId: d.Id, |
| | | Pos: d.Pos, |
| | | CreateTime: time.Now().Unix(), |
| | | } |
| | | |
| | | db.Create(&pos) |
| | | } |
| | | return nil |
| | | } |
New file |
| | |
| | | package models |
| | | |
| | | type Positions struct { |
| | | Id uint `gorm:"column:id;primary_key;auto_increment;unique;not null;"` |
| | | DeviceId string `gorm:"column:device_id;" json:"device_id"` |
| | | Pos string `gorm:"column:pos" json:"pos"` |
| | | CreateTime int64 `gorm:"column:create_time;autoCreateTime;"` |
| | | } |
| | | |
| | | func (d *Positions) TableName() string { |
| | | return "positions" |
| | | } |
| | | |
| | | func (d *Positions) FindDevicePosition(devId string, timestamp int64) error { |
| | | return db.Table(d.TableName()).Where(&d, "device_id = ? AND create_time < ?", devId, timestamp).Order("create_time desc").First(&d).Error |
| | | } |
| | |
| | | // 转发图像 |
| | | 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) |
| | | pd := c.PackPushData(deviceId, faceId, faceImageStr, face.FaceAppearTime) |
| | | if pd == nil { |
| | | return |
| | | } |
| | |
| | | return |
| | | } |
| | | |
| | | func (c CaptureRepository) PackPushData(deviceId, faceId, faceImage string) *vo.PushDataInfo { |
| | | func (c CaptureRepository) PackPushData(deviceId, faceId, faceImage, appearTime string) *vo.PushDataInfo { |
| | | var pd = new(vo.PushDataInfo) |
| | | var device models.Device |
| | | |
| | |
| | | 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 { |
| | |
| | | tr := vo.TaskResultInfo{ |
| | | Id: uuid.NewV4().String(), |
| | | CameraId: deviceId, |
| | | CameraAddr: device.Addr + device.Pos, |
| | | CameraAddr: device.Addr + devPos.Pos, |
| | | CameraName: device.Name, |
| | | PicMaxUrl: []string{""}, |
| | | PicDate: time.Now().Format("2006-01-02 15:04:05"), |