zigbeeService.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package service
  2. import (
  3. "iot_manager_service/app/device/dao"
  4. "iot_manager_service/app/device/model"
  5. "iot_manager_service/app/system/service"
  6. "iot_manager_service/util/cache"
  7. "iot_manager_service/util/common"
  8. "iot_manager_service/util/logger"
  9. "time"
  10. )
  11. var ZigbeeService = new(zigbeeService)
  12. type zigbeeService struct{}
  13. func (s *zigbeeService) Get(id int) (*model.ZigbeeDetail, *common.Errors) {
  14. // 创建查询实例
  15. device := &dao.Zigbee{
  16. ID: id,
  17. }
  18. err := device.GetDevice()
  19. if err != nil {
  20. return nil, common.FailResponse(err.Error(), nil)
  21. }
  22. endTime, state := cache.GetDeviceState(device.Sn)
  23. return &model.ZigbeeDetail{
  24. Zigbee: *device,
  25. RunState: state,
  26. NetworkState: state,
  27. EndLineTime: endTime,
  28. }, nil
  29. }
  30. func (s *zigbeeService) CreateOrUpdate(userId int, tenantId string, req dao.Zigbee) *common.Errors {
  31. // 创建查询实例
  32. device := req
  33. device.TenantId = tenantId
  34. device.UpdateUser = userId
  35. device.UpdateTime = time.Now()
  36. if req.ID == 0 {
  37. device.CreateTime = time.Now()
  38. device.CreateUser = userId
  39. if device.IsExistedBySN() {
  40. logger.Logger.Errorf("Create GetDeviceID err \n")
  41. return common.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  42. }
  43. if err := device.Create(); err != nil {
  44. logger.Logger.Errorf("Create err = %s \n", err.Error())
  45. return common.FailResponse(err.Error(), nil)
  46. }
  47. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  48. common.DeviceTypeZigbee, common.GetDeviceObject(device.ID, device.Name), common.OperationSuccess)
  49. return common.SuccessResponse(common.Succeeded, nil)
  50. }
  51. if device.IsExistedByNameAndCode() {
  52. return common.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  53. }
  54. if err := device.Update(); err != nil {
  55. logger.Logger.Errorf("Update err = %s \n", err.Error())
  56. return common.FailResponse(err.Error(), nil)
  57. }
  58. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  59. common.DeviceTypeZigbee, common.GetDeviceObject(device.ID, device.Name), common.OperationSuccess)
  60. return common.SuccessResponse(common.Succeeded, nil)
  61. }
  62. func (s *zigbeeService) List(searchValue string, current, size int) ([]model.ZigbeeDetail, int64, *common.Errors) {
  63. device := dao.Zigbee{}
  64. if searchValue != "" {
  65. device.Sn = searchValue
  66. device.Name = searchValue
  67. }
  68. offset := (current - 1) * size
  69. limit := size
  70. devices, total, err := device.GetDevices(offset, limit)
  71. if err != nil {
  72. return nil, 0, common.FailResponse(err.Error(), nil)
  73. }
  74. var details []model.ZigbeeDetail
  75. for _, d := range devices {
  76. endTime, state := cache.GetDeviceState(d.Sn)
  77. details = append(details, model.ZigbeeDetail{
  78. Zigbee: d,
  79. RunState: state,
  80. NetworkState: state,
  81. EndLineTime: endTime,
  82. })
  83. }
  84. return details, total, nil
  85. }
  86. func (s *zigbeeService) Remove(userId int, tenantId string, id int) *common.Errors {
  87. // 创建查询实例
  88. device := &dao.Zigbee{
  89. ID: id,
  90. IsDeleted: 1,
  91. UpdateUser: userId,
  92. UpdateTime: time.Now(),
  93. }
  94. err := device.Delete()
  95. if err != nil {
  96. return common.FailResponse(err.Error(), nil)
  97. }
  98. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  99. common.DeviceTypeZigbee, common.GetDeviceObject(device.ID, device.Name), common.OperationSuccess)
  100. return nil
  101. }
  102. func (s *zigbeeService) GetList(tenantId string) ([]dao.Zigbee, *common.Errors) {
  103. device := &dao.Zigbee{
  104. TenantId: tenantId,
  105. IsDeleted: 0,
  106. }
  107. devices, err := device.GetAllDevices()
  108. if err != nil {
  109. return nil, common.FailResponse(err.Error(), nil)
  110. }
  111. return devices, nil
  112. }
  113. func (s *zigbeeService) GetByGateway(id int) []dao.Zigbee {
  114. // 创建查询实例
  115. device := &dao.Zigbee{
  116. GatewayId: id,
  117. }
  118. return device.GetDevicesByGateway()
  119. }
  120. func (s *zigbeeService) GetByLampPole(id int) []dao.Zigbee {
  121. // 创建查询实例
  122. device := &dao.Zigbee{
  123. LampPoleId: id,
  124. }
  125. return device.GetDevicesByLampPole()
  126. }
  127. func (s *zigbeeService) DeviceCount() dao.DeviceStatus {
  128. var rsp dao.DeviceStatus
  129. d := dao.Zigbee{}
  130. rsp.Set(d.DeviceCount1())
  131. return rsp
  132. }
  133. func (s *zigbeeService) ListDevices() []dao.Device {
  134. d := dao.Zigbee{}
  135. return d.ListDevices1()
  136. }