package app import ( "errors" "fmt" "github.com/sirupsen/logrus" "gorm.io/gorm" "io/ioutil" "lcfns/global" "lcfns/isapi" "lcfns/model/app" "lcfns/model/common/request" "net/http" "net/url" ) type CameraService struct { } // CreateCamera 创建Camera记录 // Author [piexlmax](https://github.com/piexlmax) func (cameraService *CameraService) CreateCamera(camera *app.Camera) (err error) { err = global.Db.Create(camera).Error return err } func (cameraService *CameraService) CreateIfNotExist(info isapi.DeviceInfo, gwId uint) (err error) { var camera app.Camera err = global.Db.Select("id,sn").Model(&app.Camera{}).Where("sn = ? AND is_deleted = 0", info.SerialNumber).Scan(&camera).Debug().Error if err != nil { return err } if camera.SN != "" { //已注册至数据库,绑定至网关,更新摄像头状态 var m = map[string]any{"gid": gwId, "state": 1} global.Db.Model(&app.Camera{}).Where("id = ?", camera.ID).Updates(m) return errors.New("摄像头已注册") } //注册 var ca = &app.Camera{SN: info.SerialNumber, Gid: gwId, MacAddress: info.MacAddress, State: 1} err = global.Db.Create(ca).Debug().Error if err != nil { return err } logrus.Infof("摄像头注册成功:%s", info.SerialNumber) return nil } // DeleteCamera 删除Camera记录 // Author [piexlmax](https://github.com/piexlmax) func (cameraService *CameraService) DeleteCamera(camera app.Camera) (err error) { err = global.Db.Transaction(func(tx *gorm.DB) error { if err := tx.Debug().Model(&app.Camera{}).Where("id = ?", camera.ID).Update("is_deleted", 1).Error; err != nil { return err } if err = tx.Debug().Delete(&camera).Error; err != nil { return err } return nil }) return err } // DeleteCameraByIds 批量删除Camera记录 // Author [piexlmax](https://github.com/piexlmax) func (cameraService *CameraService) DeleteCameraByIds(ids request.IdsReq, deleted_by uint) (err error) { err = global.Db.Transaction(func(tx *gorm.DB) error { if err := tx.Model(&app.Camera{}).Where("id in ?", ids.Ids).Update("is_deleted", 1).Error; err != nil { return err } if err := tx.Where("id in ?", ids.Ids).Delete(&app.Camera{}).Error; err != nil { return err } return nil }) return err } // UpdateCamera 更新Camera记录 // Author [piexlmax](https://github.com/piexlmax) func (cameraService *CameraService) UpdateCamera(camera app.Camera) (err error) { //err = global.Db.Debug().Omit("created_at, deleted_at, id, Gid, created_by, deleted_by, user_name, password").SaveEventPicture(&camera).Error err = global.Db.Debug().Omit("id, created_at, deleted_at, gid, name,state, sn,mac_address, deleted_by").Save(&camera).Error return err } // GetCamera 根据id获取Camera记录 // Author [piexlmax](https://github.com/piexlmax) func (cameraService *CameraService) GetCamera(id uint) (camera app.Camera, err error) { err = global.Db.Where("id = ? AND is_deleted = 0", id).First(&camera).Error return } func (cameraService *CameraService) GetCameraByGid(id string) (cameras []app.Camera, err error) { err = global.Db.Where("gid = ? AND is_deleted = 0", id).Find(&cameras).Debug().Error return } func (cameraService *CameraService) GetCameraByDeviceId(devId string) (camera app.Camera, err error) { err = global.Db.Where("sn = ? AND is_deleted = 0", devId).Find(&camera).Debug().Error return } func (cameraService *CameraService) SetCameraRegisted(devId string) (err error) { //return global.Db.Table("camera").Where("device_id = ? AND deleted_by = 0", devId).Update("is_registed", 1).Debug().Error return global.Db.Table("camera").Where("sn = ? AND is_deleted = 0", devId).Update("is_registed", 1).Debug().Error } // GetRewindText 请求回话加密数据 func (cameraService *CameraService) GetRewindText(streamId string, date string) (string, error) { baseUrl := global.Config.Foreign.SecurityRewindUrl api := baseUrl + "/list/%v/%v" url2 := fmt.Sprintf(api, streamId, url.QueryEscape(date)) return getRewindApi(url2) } // getRewindApi 公用请求接口 func getRewindApi(url string) (string, error) { method := "GET" client := &http.Client{} req, err := http.NewRequest(method, url, nil) if err != nil { return "", nil } res, err := client.Do(req) if err != nil { return "", nil } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) if err != nil { return "", nil } return string(body), err }