environmentService.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/data/dao"
  5. "iot_manager_service/app/data/service"
  6. dataService "iot_manager_service/app/data/service"
  7. deviceService "iot_manager_service/app/device/service"
  8. "iot_manager_service/app/operation/model"
  9. "iot_manager_service/util/common"
  10. "time"
  11. )
  12. var EnvironmentService = new(environmentService)
  13. type environmentService struct{}
  14. func (s *environmentService) EnvironmentList(tenantId int) ([]model.EnvironmentDetail, *common.Errors) {
  15. var result []model.EnvironmentDetail
  16. devices := deviceService.OptoSensorService.GetByTenant(tenantId)
  17. for _, d := range devices {
  18. detail := model.EnvironmentDetail{CreateDate: d.CreateTime, Name: d.Name, Sn: d.Sn}
  19. data := dataService.EnvironmentDataService.Get(d.ID)
  20. if data != nil {
  21. detail.EnvironmentData = *data
  22. }
  23. lampPole, _ := deviceService.LampPoleService.GetOne(d.LampPoleId)
  24. if lampPole != nil {
  25. detail.LampPoleLocation = lampPole.InstallLocation
  26. detail.LampLat = fmt.Sprintf("%f", lampPole.PoleLat)
  27. detail.LampLng = fmt.Sprintf("%f", lampPole.PoleLng)
  28. }
  29. result = append(result, detail)
  30. }
  31. return result, nil
  32. }
  33. func (s *environmentService) Get(id int) (model.EnvironmentDetail, *common.Errors) {
  34. var result model.EnvironmentDetail
  35. device := deviceService.OptoSensorService.GetOne(id)
  36. if device == nil {
  37. return result, nil
  38. }
  39. detail := model.EnvironmentDetail{CreateDate: device.CreateTime, Name: device.Name, Sn: device.Sn}
  40. data := dataService.EnvironmentDataService.Get(device.ID)
  41. if data != nil {
  42. detail.EnvironmentData = *data
  43. }
  44. return result, nil
  45. }
  46. func (s *environmentService) GetHistoryData(id int, scope string, queryType int, start,
  47. end time.Time) ([]dao.EnvironmentData, error) {
  48. isDay := 0
  49. if scope == "day" {
  50. isDay = 1
  51. }
  52. data, err := service.EnvironmentDataService.GetData(id, isDay, start, end)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return data, nil
  57. }