cameraService.go 5.4 KB

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