Foggy.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package main
  2. import (
  3. "awesomeProject/entity"
  4. "awesomeProject/pjisuv_msgs"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/bluenviron/goroslib/v2"
  8. "io/ioutil"
  9. "net/http"
  10. "os"
  11. "os/signal"
  12. "time"
  13. )
  14. type Weather struct {
  15. WeatherID []int
  16. temperature float64
  17. humidity float64
  18. }
  19. var (
  20. param entity.PjisuvParam
  21. //weatherID = []int{}
  22. apiKey = "f9d230f00d9ccdba49a97e043333d410"
  23. maxRetries = 5
  24. retryDelay = time.Second * 2
  25. data map[string]interface{}
  26. )
  27. func OpenWeatherAPI() Weather {
  28. var weather = Weather{}
  29. latitude := param.LatitudeOfCicvLocation
  30. longitude := param.LongitudeOfCicvLocation
  31. for retries := 0; retries < maxRetries; retries++ {
  32. url := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&appid=%s", latitude, longitude, apiKey)
  33. resp, err := http.Get(url)
  34. if err != nil {
  35. fmt.Println("Error:", err)
  36. time.Sleep(retryDelay)
  37. continue
  38. }
  39. defer resp.Body.Close()
  40. body, err := ioutil.ReadAll(resp.Body)
  41. if err != nil {
  42. fmt.Println("Error reading response:", err)
  43. time.Sleep(retryDelay)
  44. continue
  45. }
  46. if err := json.Unmarshal(body, &data); err != nil {
  47. fmt.Println("Error parsing JSON:", err)
  48. time.Sleep(retryDelay)
  49. continue
  50. }
  51. // Successfully retrieved data
  52. break
  53. }
  54. if data == nil {
  55. fmt.Println("Failed to retrieve data after maximum retries.")
  56. return weather
  57. }
  58. mainData := data["main"].(map[string]interface{})
  59. weather.temperature = mainData["temp"].(float64) - 273.15
  60. weather.humidity = mainData["humidity"].(float64)
  61. for _, w := range data["weather"].([]interface{}) {
  62. tianqi := w.(map[string]interface{})
  63. weather.WeatherID = append(weather.WeatherID, int(tianqi["id"].(float64)))
  64. }
  65. fmt.Printf("当前位置的天气状况/温度/湿度:%v/%.2f/%.2f\n", weather.WeatherID, weather.temperature, weather.humidity)
  66. return weather
  67. }
  68. func FinalCallback() {
  69. NOwWeather := OpenWeatherAPI()
  70. if NOwWeather.WeatherID != nil {
  71. for _, weatherid := range NOwWeather.WeatherID {
  72. if weatherid == 701 || weatherid == 711 || weatherid == 741 || weatherid == 721 {
  73. eventLabel := "Foggy"
  74. fmt.Println(eventLabel)
  75. break
  76. }
  77. }
  78. }
  79. }
  80. func main() {
  81. ticker := time.NewTicker(80 * time.Second)
  82. defer ticker.Stop()
  83. go listener()
  84. for {
  85. select {
  86. case <-ticker.C:
  87. FinalCallback()
  88. }
  89. }
  90. }
  91. func CallbackCicvLocation(data *pjisuv_msgs.PerceptionLocalization) {
  92. param.LatitudeOfCicvLocation = data.Latitude
  93. param.LongitudeOfCicvLocation = data.Longitude
  94. }
  95. func listener() {
  96. // create a node and connect to the master
  97. n, err := goroslib.NewNode(goroslib.NodeConf{
  98. Name: "goroslib_sub",
  99. MasterAddress: "127.0.0.1:11311",
  100. })
  101. if err != nil {
  102. panic(err)
  103. }
  104. defer n.Close()
  105. // create a subscriber
  106. subCicvLocation, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
  107. Node: n,
  108. Topic: "/cicv_location",
  109. Callback: CallbackCicvLocation,
  110. })
  111. if err != nil {
  112. panic(err)
  113. }
  114. defer subCicvLocation.Close()
  115. // wait for CTRL-C
  116. c := make(chan os.Signal, 1)
  117. signal.Notify(c, os.Interrupt)
  118. <-c
  119. }