gatewayService.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 string, 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, int64, *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, total, err := device.GetDevices(offset, limit)
  87. if err != nil {
  88. return nil, 0, common.FailResponse(err.Error(), nil)
  89. }
  90. for _, d := range devices {
  91. detail := model.GatewayDetail{Gateway: d}
  92. relation, _ := GatewayRelationService.Get(d.ID)
  93. //fmt.Printf("d.ID = %v", d.ID)
  94. //查数据
  95. relation2 := dao.GatewayRelation{
  96. ID: d.ID,
  97. }
  98. relation2.Update() //临时 处理
  99. if relation != nil {
  100. detail.CountLampPole = relation.Total
  101. }
  102. endTime, state := cache.GetDeviceState(d.GatewaySn)
  103. detail.RunState = state
  104. detail.EndLineTime = endTime
  105. detail.NetworkState = state
  106. details = append(details, detail)
  107. }
  108. return details, total, nil
  109. }
  110. func (s *gatewayService) Remove(userId int64, tenantId string, id int) *common.Errors {
  111. // 创建查询实例
  112. device := &dao.Gateway{
  113. ID: id,
  114. IsDeleted: 1,
  115. UpdateUser: userId,
  116. UpdateTime: time.Now(),
  117. }
  118. if relation, _ := GatewayRelationService.Get(id); relation != nil && relation.Total > 0 {
  119. return common.OperationInvalidResponse(model.GatewayHasRelation, nil)
  120. }
  121. err := device.Delete()
  122. if err != nil {
  123. return common.FailResponse(err.Error(), nil)
  124. }
  125. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  126. common.DeviceTypeGateway, common.GetDeviceObject(device.ID, device.GatewayName), common.OperationSuccess)
  127. return nil
  128. }
  129. func (s *gatewayService) GetList(tenantId string, ) ([]*dao.Gateway, *common.Errors) {
  130. var devices []*dao.Gateway
  131. err := cache.Redis.Get(getGatewayListRedisKey(tenantId)).Scan(&devices)
  132. if err == nil {
  133. return devices, nil
  134. }
  135. device := &dao.Gateway{
  136. TenantId: tenantId,
  137. IsDeleted: 0,
  138. }
  139. devices, err = device.GetAllDevices()
  140. for _, device := range devices {
  141. device.GatewayName = device.GatewayName + "(" + device.GatewaySn + ")"
  142. }
  143. if err != nil {
  144. return nil, common.FailResponse(err.Error(), nil)
  145. }
  146. _ = cache.Redis.Set(getGatewayListRedisKey(tenantId), devices, 0).Err()
  147. return devices, nil
  148. }
  149. func getGatewayListRedisKey(tenantId string, ) string {
  150. return fmt.Sprintf(model.GatewayList, tenantId)
  151. }
  152. func (s *gatewayService) GetRelevanceDetail(id int) (*model.GatewayDetail, *common.Errors) {
  153. // 创建查询实例
  154. device := &dao.Gateway{
  155. ID: id,
  156. }
  157. err := device.GetDevice()
  158. if err != nil {
  159. return nil, common.FailResponse(err.Error(), nil)
  160. }
  161. return &model.GatewayDetail{
  162. Gateway: *device,
  163. AlarmTerminalList: AlarmTerminalService.GetByGateway(device.ID),
  164. CameraList: CameraService.GetByGateway(device.ID),
  165. CaptureUnitList: CaptureUintService.GetCaptureByGateway(device.ID),
  166. InfoBoardList: InfoBoardService.GetByGateway(device.ID),
  167. IpBroadcastList: IpBroadcastService.GetByGateway(device.ID),
  168. LightControlList: LightControlService.GetByGateway(device.ID),
  169. OptoSensorList: OptoSensorService.GetByGateway(device.ID),
  170. ZigbeeList: ZigbeeService.GetByGateway(device.ID),
  171. }, nil
  172. }
  173. func (s *gatewayService) GetOne(id int) (*dao.Gateway, *common.Errors) {
  174. // 创建查询实例
  175. device := &dao.Gateway{
  176. ID: id,
  177. }
  178. err := device.GetDevice()
  179. if err != nil {
  180. return nil, common.FailResponse(err.Error(), nil)
  181. }
  182. return device, nil
  183. }
  184. func (s *gatewayService) GetByLampPole(id int) []dao.Gateway {
  185. // 创建查询实例
  186. device := &dao.Gateway{
  187. LampPoleId: id,
  188. }
  189. return device.GetDevicesByLampPole()
  190. }