camera.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package app
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/sirupsen/logrus"
  6. "gorm.io/gorm"
  7. "io/ioutil"
  8. "lc-fangdaosha/global"
  9. "lc-fangdaosha/isapi"
  10. "lc-fangdaosha/model/app"
  11. appReq "lc-fangdaosha/model/app/request"
  12. "lc-fangdaosha/model/common/request"
  13. "net/http"
  14. "net/url"
  15. )
  16. type CameraService struct {
  17. }
  18. // CreateCamera 创建摄像机记录
  19. // Author [piexlmax](https://github.com/piexlmax)
  20. func (cameraService *CameraService) CreateCamera(camera *app.Camera) (err error) {
  21. err = global.Db.Create(camera).Error
  22. return err
  23. }
  24. // 判断摄像机是否已注册
  25. func (cameraService *CameraService) CreateIfNotExist(info isapi.DeviceInfo, gwId uint) (err error) {
  26. var camera app.Camera
  27. err = global.Db.Select("id,sn").Model(&app.Camera{}).Where("sn = ? AND is_deleted = 0", info.SerialNumber).Scan(&camera).Debug().Error
  28. if err != nil {
  29. return err
  30. }
  31. fmt.Println(camera)
  32. if camera.SN != "" {
  33. //已注册至数据库,绑定至网关
  34. global.Db.Model(&app.Camera{}).Where("id = ?", camera.ID).Update("gid", gwId)
  35. return errors.New("摄像头已注册")
  36. }
  37. //注册
  38. var ca = &app.Camera{SN: info.SerialNumber, Gid: gwId, MacAddress: info.MacAddress}
  39. err = global.Db.Create(ca).Debug().Error
  40. if err != nil {
  41. return err
  42. }
  43. logrus.Infof("摄像头注册成功:%s", info.SerialNumber)
  44. return nil
  45. }
  46. // DeleteCamera 删除摄像机记录
  47. // Author [piexlmax](https://github.com/piexlmax)
  48. func (cameraService *CameraService) DeleteCamera(camera app.Camera) (err error) {
  49. err = global.Db.Transaction(func(tx *gorm.DB) error {
  50. if err := tx.Debug().Model(&app.Camera{}).Where("id = ?", camera.ID).Update("is_deleted", 1).Error; err != nil {
  51. return err
  52. }
  53. if err = tx.Debug().Delete(&camera).Error; err != nil {
  54. return err
  55. }
  56. return nil
  57. })
  58. return err
  59. }
  60. // DeleteCameraByIds 批量删除摄像机记录
  61. // Author [piexlmax](https://github.com/piexlmax)
  62. func (cameraService *CameraService) DeleteCameraByIds(ids request.IdsReq) (err error) {
  63. err = global.Db.Transaction(func(tx *gorm.DB) error {
  64. if err := tx.Model(&app.Camera{}).Where("id in ?", ids.Ids).Update("is_deleted", 1).Error; err != nil {
  65. return err
  66. }
  67. if err := tx.Where("id in ?", ids.Ids).Delete(&app.Camera{}).Error; err != nil {
  68. return err
  69. }
  70. return nil
  71. })
  72. return err
  73. }
  74. // UpdateCamera 更新摄像机记录
  75. // Author [piexlmax](https://github.com/piexlmax)
  76. func (cameraService *CameraService) UpdateCamera(camera app.Camera) (err error) {
  77. fmt.Println("camera", camera)
  78. err = global.Db.Debug().Omit("id, created_at, deleted_at, gid, name, sn,mac_address, deleted_by").Save(&camera).Error
  79. return err
  80. }
  81. // GetCamera 根据id获取摄像机记录
  82. // Author [piexlmax](https://github.com/piexlmax)
  83. func (cameraService *CameraService) GetCamera(id uint) (camera app.Camera, err error) {
  84. err = global.Db.Where("id = ? AND is_deleted = 0", id).First(&camera).Error
  85. return
  86. }
  87. // GetCameraByGid 根据网关id获取摄像机
  88. func (cameraService *CameraService) GetCameraByGid(id string) (cameras []app.Camera, err error) {
  89. err = global.Db.Where("gid = ? AND is_deleted = 0", id).Find(&cameras).Debug().Error
  90. return
  91. }
  92. // GetCameraByDeviceId 根据设备id获取摄像机
  93. func (cameraService *CameraService) GetCameraByDeviceId(devId string) (camera app.Camera, err error) {
  94. err = global.Db.Where("sn = ? AND is_deleted = 0", devId).Find(&camera).Debug().Error
  95. return
  96. }
  97. // 获取摄像机的注册id
  98. func (cameraService *CameraService) SetCameraRegisted(devId string) (err error) {
  99. return global.Db.Table("camera").Where("sn = ? AND is_deleted = 0", devId).Update("is_registed", 1).Debug().Error
  100. }
  101. // GetCameraInfoList 分页获取摄像机记录
  102. // Author [piexlmax](https://github.com/piexlmax)
  103. func (cameraService *CameraService) GetCameraInfoList(info appReq.CameraSearch) (list []app.Camera, total int64, err error) {
  104. limit := info.PageSize
  105. offset := info.PageSize * (info.Page - 1)
  106. // 创建db
  107. db := global.Db.Model(&app.Camera{})
  108. var cameras []app.Camera
  109. // 如果有条件搜索 下方会自动创建搜索语句
  110. if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
  111. db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
  112. }
  113. err = db.Count(&total).Error
  114. if err != nil {
  115. return
  116. }
  117. if limit != 0 {
  118. db = db.Limit(limit).Offset(offset)
  119. }
  120. err = db.Find(&cameras).Error
  121. return cameras, total, err
  122. }
  123. // GetRewindText 请求回话加密数据
  124. func (cameraService *CameraService) GetRewindText(stearmId string, date string) (string, error) {
  125. baseurl := global.Config.Foreign.SecurityRewindUrl
  126. api := baseurl + "/list/%v/%v"
  127. url2 := fmt.Sprintf(api, stearmId, url.QueryEscape(date))
  128. return getRewindApi(url2)
  129. }
  130. // getRewindApi 公用请求接口
  131. func getRewindApi(url string) (string, error) {
  132. method := "GET"
  133. client := &http.Client{}
  134. req, err := http.NewRequest(method, url, nil)
  135. if err != nil {
  136. return "", nil
  137. }
  138. res, err := client.Do(req)
  139. if err != nil {
  140. return "", nil
  141. }
  142. defer res.Body.Close()
  143. body, err := ioutil.ReadAll(res.Body)
  144. if err != nil {
  145. return "", nil
  146. }
  147. return string(body), err
  148. }