gatewayService.go 6.5 KB

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