GatewayService.go 4.8 KB

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