gatewayService.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. "time"
  8. )
  9. // 中间件管理服务
  10. var GatewayService = new(gatewayService)
  11. type gatewayService struct{}
  12. func (s *gatewayService) Get(id int) (*model.GatewayDetail, *util.Errors) {
  13. // 创建查询实例
  14. device := &dao.Gateway{
  15. ID: id,
  16. }
  17. err := device.GetDevice()
  18. if err != nil {
  19. return nil, util.FailResponse(err.Error(), nil)
  20. }
  21. detail := &model.GatewayDetail{Gateway: *device}
  22. relation, e := GatewayRelationService.Get(id)
  23. if e == nil {
  24. detail.CountLampPole = relation.Total
  25. } else {
  26. fmt.Printf("GatewayRelationService.Get err = %v", err)
  27. }
  28. //todo 获取网关状态
  29. // vo.setEndLineTime(time);
  30. // vo.setNetworkState(online);
  31. // vo.setRunState(online);
  32. return detail, nil
  33. }
  34. func (s *gatewayService) CreateOrUpdate(userId int64, tenantId int, req *model.GatewayDetail) *util.Errors {
  35. device := req.Gateway
  36. device.TenantId = tenantId
  37. device.UpdateUser = userId
  38. device.UpdateTime = time.Now()
  39. if device.LampPoleId != 0 {
  40. lampPole, err := LampPoleService.GetOne(device.LampPoleId)
  41. if err == nil {
  42. device.LampLat = lampPole.PoleLat
  43. device.LampLng = lampPole.PoleLng
  44. device.LampPoleName = lampPole.PoleName
  45. device.LampPoleSn = lampPole.PoleSN
  46. device.LampPoleLocation = lampPole.InstallLocation
  47. } else {
  48. fmt.Printf("LampPoleService.GetOne err = %v \n", err)
  49. }
  50. }
  51. if device.ID == 0 {
  52. device.CreateTime = time.Now()
  53. device.CreateUser = userId
  54. if device.IsExistedBySN() {
  55. fmt.Printf("Create IsExistedBySN \n")
  56. return util.ParamsInvalidResponse(model.RepeatedPrompts, nil)
  57. }
  58. fmt.Printf("device = %+v \n", device)
  59. if err := device.Create(); err != nil {
  60. fmt.Printf("Create err = %s \n", err.Error())
  61. return util.FailResponse(err.Error(), nil)
  62. }
  63. return util.SuccessResponse(util.Succeeded, nil)
  64. }
  65. if err := device.Update(); err != nil {
  66. fmt.Printf("Update err = %s \n", err.Error())
  67. return util.FailResponse(err.Error(), nil)
  68. }
  69. //todo operation record
  70. return util.SuccessResponse(util.Succeeded, nil)
  71. }
  72. func (s *gatewayService) List(searchValue string, current, size int) ([]model.GatewayDetail, *util.Errors) {
  73. var details []model.GatewayDetail
  74. device := dao.Gateway{}
  75. if searchValue != "" {
  76. device.GatewaySN = searchValue
  77. }
  78. offset := (current - 1) * size
  79. limit := size
  80. devices, err := device.GetDevices(offset, limit)
  81. for _, d := range devices {
  82. detail := model.GatewayDetail{Gateway: d}
  83. relation, _ := GatewayRelationService.Get(d.ID)
  84. if relation != nil {
  85. detail.CountLampPole = relation.Total
  86. }
  87. details = append(details, detail)
  88. }
  89. if err != nil {
  90. return nil, util.FailResponse(err.Error(), nil)
  91. }
  92. return details, nil
  93. }
  94. func (s *gatewayService) Remove(userId int64, id int) *util.Errors {
  95. // 创建查询实例
  96. device := &dao.Gateway{
  97. ID: id,
  98. IsDeleted: 1,
  99. UpdateUser: userId,
  100. UpdateTime: time.Now(),
  101. }
  102. if relation, _ := GatewayRelationService.Get(id); relation != nil && relation.Total > 0 {
  103. return util.OperationInvalidResponse(model.GatewayHasRelation, nil)
  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 *gatewayService) GetList(tenantId int) ([]*dao.Gateway, *util.Errors) {
  113. var devices []*dao.Gateway
  114. err := util.Redis.Get(getGatewayListRedisKey(tenantId)).Scan(&devices)
  115. if err == nil {
  116. return devices, nil
  117. }
  118. device := &dao.Gateway{
  119. TenantId: tenantId,
  120. IsDeleted: 0,
  121. }
  122. devices, err = device.GetAllDevices()
  123. for _, device := range devices {
  124. device.GatewayName = device.GatewayName + "(" + device.GatewaySN + ")"
  125. }
  126. if err != nil {
  127. return nil, util.FailResponse(err.Error(), nil)
  128. }
  129. _ = util.Redis.Set(getGatewayListRedisKey(tenantId), devices, 0).Err()
  130. return devices, nil
  131. }
  132. func getGatewayListRedisKey(tenantId int) string {
  133. return fmt.Sprintf(model.GatewayList, tenantId)
  134. }
  135. func (s *gatewayService) GetRelevanceDetail(id int) (*model.GatewayDetail, *util.Errors) {
  136. // 创建查询实例
  137. device := &dao.Gateway{
  138. ID: id,
  139. }
  140. err := device.GetDevice()
  141. if err != nil {
  142. return nil, util.FailResponse(err.Error(), nil)
  143. }
  144. //todo get gateway ipBroadcast lightcontroller... list
  145. return &model.GatewayDetail{
  146. Gateway: *device,
  147. AlarmTerminalList: nil,
  148. CameraList: nil,
  149. CaptureUnitList: nil,
  150. InfoBoardList: nil,
  151. IpBroadcastList: nil,
  152. LightControlList: nil,
  153. OptoSensorList: nil,
  154. ZigbeeList: nil,
  155. }, nil
  156. }
  157. func (s *gatewayService) GetOne(id int) (*dao.Gateway, *util.Errors) {
  158. // 创建查询实例
  159. device := &dao.Gateway{
  160. ID: id,
  161. }
  162. err := device.GetDevice()
  163. if err != nil {
  164. return nil, util.FailResponse(err.Error(), nil)
  165. }
  166. return device, nil
  167. }