gatewayService.go 6.1 KB

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