gatewayService.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/app/system/service"
  7. "iot_manager_service/util/cache"
  8. "iot_manager_service/util/common"
  9. "time"
  10. )
  11. // 中间件管理服务
  12. var GatewayService = new(gatewayService)
  13. type gatewayService struct{}
  14. func (s *gatewayService) Get(id int) (*model.GatewayDetail, *common.Errors) {
  15. // 创建查询实例
  16. device := &dao.Gateway{
  17. ID: id,
  18. }
  19. err := device.GetDevice()
  20. if err != nil {
  21. return nil, common.FailResponse(err.Error(), nil)
  22. }
  23. detail := &model.GatewayDetail{Gateway: *device}
  24. relation, e := GatewayRelationService.Get(id)
  25. if e == nil {
  26. detail.CountLampPole = relation.Total
  27. } else {
  28. fmt.Printf("GatewayRelationService.Get err = %v", err)
  29. }
  30. endTime, state := cache.GetDeviceState(device.GatewaySn)
  31. detail.RunState = state
  32. detail.EndLineTime = endTime
  33. detail.NetworkState = state
  34. return detail, nil
  35. }
  36. func (s *gatewayService) CreateOrUpdate(userId int64, tenantId int, req *model.GatewayDetail) *common.Errors {
  37. device := req.Gateway
  38. device.TenantId = tenantId
  39. device.UpdateUser = userId
  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 = userId
  56. if device.IsExistedBySN() {
  57. fmt.Printf("Create IsExistedBySN \n")
  58. return common.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 common.FailResponse(err.Error(), nil)
  64. }
  65. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  66. common.DeviceTypeGateway, common.GetDeviceObject(device.ID, device.GatewayName), common.OperationSuccess)
  67. return common.SuccessResponse(common.Succeeded, nil)
  68. }
  69. if err := device.Update(); err != nil {
  70. fmt.Printf("Update err = %s \n", err.Error())
  71. return common.FailResponse(err.Error(), nil)
  72. }
  73. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  74. common.DeviceTypeGateway, common.GetDeviceObject(device.ID, device.GatewayName), common.OperationSuccess)
  75. return common.SuccessResponse(common.Succeeded, nil)
  76. }
  77. func (s *gatewayService) List(searchValue string, current, size int) ([]model.GatewayDetail, *common.Errors) {
  78. var details []model.GatewayDetail
  79. device := dao.Gateway{}
  80. if searchValue != "" {
  81. device.GatewaySn = searchValue
  82. }
  83. offset := (current - 1) * size
  84. limit := size
  85. devices, err := device.GetDevices(offset, limit)
  86. if err != nil {
  87. return nil, common.FailResponse(err.Error(), nil)
  88. }
  89. for _, d := range devices {
  90. detail := model.GatewayDetail{Gateway: d}
  91. relation, _ := GatewayRelationService.Get(d.ID)
  92. if relation != nil {
  93. detail.CountLampPole = relation.Total
  94. }
  95. endTime, state := cache.GetDeviceState(d.GatewaySn)
  96. detail.RunState = state
  97. detail.EndLineTime = endTime
  98. detail.NetworkState = state
  99. details = append(details, detail)
  100. }
  101. return details, nil
  102. }
  103. func (s *gatewayService) Remove(userId int64, tenantId int, id int) *common.Errors {
  104. // 创建查询实例
  105. device := &dao.Gateway{
  106. ID: id,
  107. IsDeleted: 1,
  108. UpdateUser: userId,
  109. UpdateTime: time.Now(),
  110. }
  111. if relation, _ := GatewayRelationService.Get(id); relation != nil && relation.Total > 0 {
  112. return common.OperationInvalidResponse(model.GatewayHasRelation, nil)
  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.DeviceTypeGateway, common.GetDeviceObject(device.ID, device.GatewayName), common.OperationSuccess)
  120. return nil
  121. }
  122. func (s *gatewayService) GetList(tenantId int) ([]*dao.Gateway, *common.Errors) {
  123. var devices []*dao.Gateway
  124. err := cache.Redis.Get(getGatewayListRedisKey(tenantId)).Scan(&devices)
  125. if err == nil {
  126. return devices, nil
  127. }
  128. device := &dao.Gateway{
  129. TenantId: tenantId,
  130. IsDeleted: 0,
  131. }
  132. devices, err = device.GetAllDevices()
  133. for _, device := range devices {
  134. device.GatewayName = device.GatewayName + "(" + device.GatewaySn + ")"
  135. }
  136. if err != nil {
  137. return nil, common.FailResponse(err.Error(), nil)
  138. }
  139. _ = cache.Redis.Set(getGatewayListRedisKey(tenantId), devices, 0).Err()
  140. return devices, nil
  141. }
  142. func getGatewayListRedisKey(tenantId int) string {
  143. return fmt.Sprintf(model.GatewayList, tenantId)
  144. }
  145. func (s *gatewayService) GetRelevanceDetail(id int) (*model.GatewayDetail, *common.Errors) {
  146. // 创建查询实例
  147. device := &dao.Gateway{
  148. ID: id,
  149. }
  150. err := device.GetDevice()
  151. if err != nil {
  152. return nil, common.FailResponse(err.Error(), nil)
  153. }
  154. return &model.GatewayDetail{
  155. Gateway: *device,
  156. AlarmTerminalList: AlarmTerminalService.GetByGateway(device.ID),
  157. CameraList: CameraService.GetByGateway(device.ID),
  158. CaptureUnitList: CaptureUintService.GetCaptureByGateway(device.ID),
  159. InfoBoardList: InfoBoardService.GetByGateway(device.ID),
  160. IpBroadcastList: IpBroadcastService.GetByGateway(device.ID),
  161. LightControlList: LightControlService.GetByGateway(device.ID),
  162. OptoSensorList: OptoSensorService.GetByGateway(device.ID),
  163. ZigbeeList: ZigbeeService.GetByGateway(device.ID),
  164. }, nil
  165. }
  166. func (s *gatewayService) GetOne(id int) (*dao.Gateway, *common.Errors) {
  167. // 创建查询实例
  168. device := &dao.Gateway{
  169. ID: id,
  170. }
  171. err := device.GetDevice()
  172. if err != nil {
  173. return nil, common.FailResponse(err.Error(), nil)
  174. }
  175. return device, nil
  176. }
  177. func (s *gatewayService) GetByLampPole(id int) []dao.Gateway {
  178. // 创建查询实例
  179. device := &dao.Gateway{
  180. LampPoleId: id,
  181. }
  182. return device.GetDevicesByLampPole()
  183. }