environmentService.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 string) ([]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. detail.CreateDate = data.PostTime
  21. if data != nil {
  22. detail.EnvironmentData = *data
  23. }
  24. lampPole, _ := deviceService.LampPoleService.GetOne(d.LampPoleId)
  25. if lampPole != nil {
  26. detail.LampPoleLocation = lampPole.InstallLocation
  27. detail.LampLat = fmt.Sprintf("%f", lampPole.PoleLat)
  28. detail.LampLng = fmt.Sprintf("%f", lampPole.PoleLng)
  29. detail.RealTimeWindSpeed = common.CalculateSpeed(fmt.Sprintf("%.2f", detail.WindSpeed/100)) //风力等级
  30. }
  31. result = append(result, detail)
  32. }
  33. return result, nil
  34. }
  35. func (s *environmentService) Get(id int) (model.EnvironmentDetail, *common.Errors) {
  36. var result model.EnvironmentDetail
  37. device := deviceService.OptoSensorService.GetOne(id)
  38. if device == nil {
  39. return result, nil
  40. }
  41. detail := model.EnvironmentDetail{CreateDate: device.CreateTime, Name: device.Name, Sn: device.Sn, LampPoleLocation: device.LampPoleLocation}
  42. data := dataService.EnvironmentDataService.Get(device.ID)
  43. if data != nil {
  44. detail.EnvironmentData = *data
  45. }
  46. return detail, nil
  47. }
  48. func (s *environmentService) GetHistoryData(id int, scope string, queryType int, start,
  49. end time.Time) ([]dao.EnvironmentData, error) {
  50. isDay := 0
  51. if scope == "day" {
  52. isDay = 1
  53. }
  54. data, err := service.EnvironmentDataService.GetData(id, isDay, start, end)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return data, nil
  59. }
  60. func (s *environmentService) GetScopeData(id int, scope string, queryType int) (interface{}, error) {
  61. isDay := 0
  62. if scope == "day" {
  63. isDay = 1
  64. }
  65. data, err := service.EnvironmentDataService.GetScopeData(id, isDay, queryType)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return data, nil
  70. }