zigbeeService.go 4.0 KB

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