camera.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package app
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/sirupsen/logrus"
  6. "gorm.io/gorm"
  7. "io/ioutil"
  8. "lcfns/global"
  9. "lcfns/isapi"
  10. "lcfns/model/app"
  11. "lcfns/model/common/request"
  12. "net/http"
  13. "net/url"
  14. )
  15. type CameraService struct {
  16. }
  17. // CreateCamera 创建Camera记录
  18. // Author [piexlmax](https://github.com/piexlmax)
  19. func (cameraService *CameraService) CreateCamera(camera *app.Camera) (err error) {
  20. err = global.Db.Create(camera).Error
  21. return err
  22. }
  23. func (cameraService *CameraService) CreateIfNotExist(info isapi.DeviceInfo, gwId uint) (err error) {
  24. var camera app.Camera
  25. err = global.Db.Select("id,sn").Model(&app.Camera{}).Where("sn = ? AND is_deleted = 0", info.SerialNumber).Scan(&camera).Debug().Error
  26. if err != nil {
  27. return err
  28. }
  29. if camera.SN != "" {
  30. //已注册至数据库,绑定至网关,更新摄像头状态
  31. var m = map[string]any{"gid": gwId, "state": 1}
  32. global.Db.Model(&app.Camera{}).Where("id = ?", camera.ID).Updates(m)
  33. return errors.New("摄像头已注册")
  34. }
  35. //注册
  36. var ca = &app.Camera{SN: info.SerialNumber, Gid: gwId, MacAddress: info.MacAddress, State: 1}
  37. err = global.Db.Create(ca).Debug().Error
  38. if err != nil {
  39. return err
  40. }
  41. logrus.Infof("摄像头注册成功:%s", info.SerialNumber)
  42. return nil
  43. }
  44. // DeleteCamera 删除Camera记录
  45. // Author [piexlmax](https://github.com/piexlmax)
  46. func (cameraService *CameraService) DeleteCamera(camera app.Camera) (err error) {
  47. err = global.Db.Transaction(func(tx *gorm.DB) error {
  48. if err := tx.Debug().Model(&app.Camera{}).Where("id = ?", camera.ID).Update("is_deleted", 1).Error; err != nil {
  49. return err
  50. }
  51. if err = tx.Debug().Delete(&camera).Error; err != nil {
  52. return err
  53. }
  54. return nil
  55. })
  56. return err
  57. }
  58. // DeleteCameraByIds 批量删除Camera记录
  59. // Author [piexlmax](https://github.com/piexlmax)
  60. func (cameraService *CameraService) DeleteCameraByIds(ids request.IdsReq, deleted_by uint) (err error) {
  61. err = global.Db.Transaction(func(tx *gorm.DB) error {
  62. if err := tx.Model(&app.Camera{}).Where("id in ?", ids.Ids).Update("is_deleted", 1).Error; err != nil {
  63. return err
  64. }
  65. if err := tx.Where("id in ?", ids.Ids).Delete(&app.Camera{}).Error; err != nil {
  66. return err
  67. }
  68. return nil
  69. })
  70. return err
  71. }
  72. // UpdateCamera 更新Camera记录
  73. // Author [piexlmax](https://github.com/piexlmax)
  74. func (cameraService *CameraService) UpdateCamera(camera app.Camera) (err error) {
  75. //err = global.Db.Debug().Omit("created_at, deleted_at, id, Gid, created_by, deleted_by, user_name, password").SaveEventPicture(&camera).Error
  76. err = global.Db.Debug().Omit("id, created_at, deleted_at, gid, name,state, sn,mac_address, deleted_by").Save(&camera).Error
  77. return err
  78. }
  79. // GetCamera 根据id获取Camera记录
  80. // Author [piexlmax](https://github.com/piexlmax)
  81. func (cameraService *CameraService) GetCamera(id uint) (camera app.Camera, err error) {
  82. err = global.Db.Where("id = ? AND is_deleted = 0", id).First(&camera).Error
  83. return
  84. }
  85. func (cameraService *CameraService) GetCameraByGid(id string) (cameras []app.Camera, err error) {
  86. err = global.Db.Where("gid = ? AND is_deleted = 0", id).Find(&cameras).Debug().Error
  87. return
  88. }
  89. func (cameraService *CameraService) GetCameraByDeviceId(devId string) (camera app.Camera, err error) {
  90. err = global.Db.Where("sn = ? AND is_deleted = 0", devId).Find(&camera).Debug().Error
  91. return
  92. }
  93. func (cameraService *CameraService) SetCameraRegisted(devId string) (err error) {
  94. //return global.Db.Table("camera").Where("device_id = ? AND deleted_by = 0", devId).Update("is_registed", 1).Debug().Error
  95. return global.Db.Table("camera").Where("sn = ? AND is_deleted = 0", devId).Update("is_registed", 1).Debug().Error
  96. }
  97. // GetRewindText 请求回话加密数据
  98. func (cameraService *CameraService) GetRewindText(streamId string, date string) (string, error) {
  99. baseUrl := global.Config.Foreign.SecurityRewindUrl
  100. api := baseUrl + "/list/%v/%v"
  101. url2 := fmt.Sprintf(api, streamId, url.QueryEscape(date))
  102. return getRewindApi(url2)
  103. }
  104. // getRewindApi 公用请求接口
  105. func getRewindApi(url string) (string, error) {
  106. method := "GET"
  107. client := &http.Client{}
  108. req, err := http.NewRequest(method, url, nil)
  109. if err != nil {
  110. return "", nil
  111. }
  112. res, err := client.Do(req)
  113. if err != nil {
  114. return "", nil
  115. }
  116. defer res.Body.Close()
  117. body, err := ioutil.ReadAll(res.Body)
  118. if err != nil {
  119. return "", nil
  120. }
  121. return string(body), err
  122. }