123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- package main
- import (
- "awesomeProject/entity"
- "awesomeProject/pjisuv_msgs"
- "encoding/json"
- "fmt"
- "github.com/bluenviron/goroslib/v2"
- "io/ioutil"
- "net/http"
- "os"
- "os/signal"
- "time"
- )
- type Weather struct {
- WeatherID []int
- temperature float64
- humidity float64
- }
- var (
- param entity.PjisuvParam
- //weatherID = []int{}
- apiKey = "f9d230f00d9ccdba49a97e043333d410"
- maxRetries = 5
- retryDelay = time.Second * 2
- data map[string]interface{}
- )
- func OpenWeatherAPI() Weather {
- var weather = Weather{}
- latitude := param.LatitudeOfCicvLocation
- longitude := param.LongitudeOfCicvLocation
- for retries := 0; retries < maxRetries; retries++ {
- url := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&appid=%s", latitude, longitude, apiKey)
- resp, err := http.Get(url)
- if err != nil {
- fmt.Println("Error:", err)
- time.Sleep(retryDelay)
- continue
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println("Error reading response:", err)
- time.Sleep(retryDelay)
- continue
- }
- if err := json.Unmarshal(body, &data); err != nil {
- fmt.Println("Error parsing JSON:", err)
- time.Sleep(retryDelay)
- continue
- }
- // Successfully retrieved data
- break
- }
- if data == nil {
- fmt.Println("Failed to retrieve data after maximum retries.")
- return weather
- }
- mainData := data["main"].(map[string]interface{})
- weather.temperature = mainData["temp"].(float64) - 273.15
- weather.humidity = mainData["humidity"].(float64)
- for _, w := range data["weather"].([]interface{}) {
- tianqi := w.(map[string]interface{})
- weather.WeatherID = append(weather.WeatherID, int(tianqi["id"].(float64)))
- }
- fmt.Printf("当前位置的天气状况/温度/湿度:%v/%.2f/%.2f\n", weather.WeatherID, weather.temperature, weather.humidity)
- return weather
- }
- func FinalCallback() {
- NOwWeather := OpenWeatherAPI()
- if NOwWeather.WeatherID != nil {
- for _, weatherid := range NOwWeather.WeatherID {
- if weatherid == 701 || weatherid == 711 || weatherid == 741 || weatherid == 721 {
- eventLabel := "Foggy"
- fmt.Println(eventLabel)
- break
- }
- }
- }
- }
- func main() {
- ticker := time.NewTicker(80 * time.Second)
- defer ticker.Stop()
- go listener()
- for {
- select {
- case <-ticker.C:
- FinalCallback()
- }
- }
- }
- func CallbackCicvLocation(data *pjisuv_msgs.PerceptionLocalization) {
- param.LatitudeOfCicvLocation = data.Latitude
- param.LongitudeOfCicvLocation = data.Longitude
- }
- func listener() {
- // create a node and connect to the master
- n, err := goroslib.NewNode(goroslib.NodeConf{
- Name: "goroslib_sub",
- MasterAddress: "127.0.0.1:11311",
- })
- if err != nil {
- panic(err)
- }
- defer n.Close()
- // create a subscriber
- subCicvLocation, err := goroslib.NewSubscriber(goroslib.SubscriberConf{
- Node: n,
- Topic: "/cicv_location",
- Callback: CallbackCicvLocation,
- })
- if err != nil {
- panic(err)
- }
- defer subCicvLocation.Close()
- // wait for CTRL-C
- c := make(chan os.Signal, 1)
- signal.Notify(c, os.Interrupt)
- <-c
- }
|