cameraService.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 int, tenantId string, 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, int64, *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, total, err := device.GetDevices(offset, limit)
  93. if err != nil {
  94. return nil, 0, 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. NetworkState: state,
  102. EndLineTime: endTime,
  103. })
  104. }
  105. return details, total, nil
  106. }
  107. func (s *cameraService) Remove(userId int, tenantId string, id int) *common.Errors {
  108. // 创建查询实例
  109. device := &dao.CameraDevice{
  110. ID: id,
  111. IsDeleted: 1,
  112. UpdateUser: userId,
  113. UpdateTime: time.Now(),
  114. }
  115. err := device.Delete()
  116. if err != nil {
  117. return common.FailResponse(err.Error(), nil)
  118. }
  119. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  120. common.DeviceTypeCamera, common.GetDeviceObject(device.ID, device.DeviceName), common.OperationSuccess)
  121. return nil
  122. }
  123. func (s *cameraService) GetList(tenantId string) ([]dao.CameraDevice, *common.Errors) {
  124. device := &dao.CameraDevice{
  125. TenantId: tenantId,
  126. IsDeleted: 0,
  127. }
  128. devices, err := device.GetAllDevices()
  129. var results []dao.CameraDevice
  130. // todo 不要返回多余字段,只需要id和name
  131. for _, device := range devices {
  132. results = append(results, dao.CameraDevice{ID: device.ID, DeviceName: device.DeviceName + "(" + device.DeviceSN + ")"})
  133. }
  134. if err != nil {
  135. return nil, common.FailResponse(err.Error(), nil)
  136. }
  137. return results, nil
  138. }
  139. func (s *cameraService) Enable(id, status int) *common.Errors {
  140. // 创建查询实例
  141. device := &dao.CameraDevice{
  142. ID: id,
  143. IsEnable: status,
  144. }
  145. err := device.UpdateEnable()
  146. if err != nil {
  147. return common.FailResponse(err.Error(), nil)
  148. }
  149. return nil
  150. }
  151. func (s *cameraService) GetByGateway(id int) []dao.CameraDevice {
  152. // 创建查询实例
  153. device := &dao.CameraDevice{
  154. GatewayId: id,
  155. }
  156. return device.GetDevicesByGateway()
  157. }
  158. func (s *cameraService) GetByLampPole(id int) []dao.CameraDevice {
  159. // 创建查询实例
  160. device := &dao.CameraDevice{
  161. LampPoleId: id,
  162. }
  163. return device.GetDevicesByLampPole()
  164. }
  165. func (s *cameraService) GetByLampPoleGroup(id int) []dao.CameraDevice {
  166. // 创建查询实例
  167. device := &dao.CameraDevice{
  168. GroupId: id,
  169. }
  170. return device.GetDevicesByLampPoleGroup()
  171. }
  172. func (s *cameraService) DeviceCount() dao.DeviceStatus {
  173. var rsp dao.DeviceStatus
  174. d := dao.CameraDevice{}
  175. rsp.Set(d.DeviceCount1())
  176. return rsp
  177. }
  178. func (s *cameraService) ListDevices() []dao.Device {
  179. pole := dao.CameraDevice{}
  180. return pole.ListDevices1()
  181. }