cameraService.go 4.0 KB

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