cameraService.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/device/dao"
  5. "iot_manager_service/app/device/model"
  6. "iot_manager_service/util"
  7. "strconv"
  8. "time"
  9. )
  10. // 中间件管理服务
  11. var CameraService = new(cameraService)
  12. type cameraService struct{}
  13. func (s *cameraService) CreateOrUpdate(userId int64, tenantId int, req *model.CameraDetail) *util.Errors {
  14. device := req.CameraDevice
  15. device.TenantId = tenantId
  16. device.UpdateUser = userId
  17. device.UpdateTime = time.Now()
  18. if device.LampPoleId != 0 {
  19. lampPole, err := LampPoleService.GetOne(device.LampPoleId)
  20. if err == nil {
  21. device.LampLat = lampPole.PoleLat
  22. device.LampLng = lampPole.PoleLng
  23. device.LampPoleName = lampPole.PoleName
  24. device.LampPoleSn = lampPole.PoleSN
  25. device.LampPoleLocation = lampPole.InstallLocation
  26. device.GroupId = lampPole.GroupId
  27. } else {
  28. fmt.Printf("LampPoleService.GetOne err = %v \n", err)
  29. }
  30. }
  31. if device.ID == 0 {
  32. device.CreateTime = time.Now()
  33. device.CreateUser = userId
  34. if device.IsExistedBySN() {
  35. fmt.Printf("Create IsExistedBySN \n")
  36. return util.ParamsInvalidResponse(model.RepeatedPrompts, nil)
  37. }
  38. fmt.Printf("device = %+v \n", device)
  39. if err := device.Create(); err != nil {
  40. fmt.Printf("Create err = %s \n", err.Error())
  41. return util.FailResponse(err.Error(), nil)
  42. }
  43. return util.SuccessResponse(util.Succeeded, nil)
  44. }
  45. if err := device.Update(); err != nil {
  46. fmt.Printf("Update err = %s \n", err.Error())
  47. return util.FailResponse(err.Error(), nil)
  48. }
  49. //todo operation record
  50. return util.SuccessResponse(util.Succeeded, nil)
  51. }
  52. func (s *cameraService) Get(id int) (*model.CameraDetail, *util.Errors) {
  53. // 创建查询实例
  54. device := &dao.CameraDevice{
  55. ID: id,
  56. }
  57. err := device.GetDevice()
  58. if err != nil {
  59. return nil, util.FailResponse(err.Error(), nil)
  60. }
  61. detail := &model.CameraDetail{CameraDevice: *device}
  62. if device.CameraType == model.CameraTypeBall {
  63. detail.CameraTypeName = model.CameraTypeBallName
  64. } else if device.CameraType == model.CameraTypeGun {
  65. detail.CameraTypeName = model.CameraTypeGunName
  66. }
  67. //todo 获取运行状态
  68. // runState = forVidiconService.queryRtStatus();
  69. return detail, nil
  70. }
  71. func (s *cameraService) List(searchValue, cameraType string, current, size int) ([]model.CameraDetail, *util.Errors) {
  72. var details []model.CameraDetail
  73. device := dao.CameraDevice{}
  74. if searchValue != "" {
  75. device.DeviceSN = searchValue
  76. }
  77. if cameraType != "" {
  78. t, _ := strconv.Atoi(cameraType)
  79. device.CameraType = t
  80. } else {
  81. device.CameraType = -1
  82. }
  83. offset := (current - 1) * size
  84. limit := size
  85. devices, err := device.GetDevices(offset, limit)
  86. if err != nil {
  87. return nil, util.FailResponse(err.Error(), nil)
  88. }
  89. for _, d := range devices {
  90. details = append(details, model.CameraDetail{
  91. CameraDevice: d,
  92. RunState: "", // todo queryRtStatus
  93. })
  94. }
  95. return details, nil
  96. }
  97. func (s *cameraService) Remove(userId int64, id int) *util.Errors {
  98. // 创建查询实例
  99. device := &dao.CameraDevice{
  100. ID: id,
  101. IsDeleted: 1,
  102. UpdateUser: userId,
  103. UpdateTime: time.Now(),
  104. }
  105. //todo operation record
  106. err := device.Delete()
  107. if err != nil {
  108. return util.FailResponse(err.Error(), nil)
  109. }
  110. return nil
  111. }
  112. func (s *cameraService) GetList(tenantId int) ([]dao.CameraDevice, *util.Errors) {
  113. device := &dao.CameraDevice{
  114. TenantId: tenantId,
  115. IsDeleted: 0,
  116. }
  117. devices, err := device.GetAllDevices()
  118. var results []dao.CameraDevice
  119. // todo 不要返回多余字段,只需要id和name
  120. for _, device := range devices {
  121. results = append(results, dao.CameraDevice{ID: device.ID, DeviceName: device.DeviceName + "(" + device.DeviceSN + ")"})
  122. }
  123. if err != nil {
  124. return nil, util.FailResponse(err.Error(), nil)
  125. }
  126. return results, nil
  127. }
  128. func (s *cameraService) Enable(id, status int) *util.Errors {
  129. // 创建查询实例
  130. device := &dao.CameraDevice{
  131. ID: id,
  132. IsEnable: status,
  133. }
  134. err := device.UpdateEnable()
  135. if err != nil {
  136. return util.FailResponse(err.Error(), nil)
  137. }
  138. return nil
  139. }