WetWeather.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package main
  2. import (
  3. "cicv-data-closedloop/pjisuv_ticker"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "sync"
  9. "time"
  10. )
  11. // 这个触发器必须保证是工作状态,其他和天气有关的触发器才能生效!!!
  12. var (
  13. threshold float64 = 60.0
  14. apiKey = "f9d230f00d9ccdba49a97e043333d410"
  15. maxRetries = 5
  16. retryDelay = time.Second * 2
  17. )
  18. type Weather struct {
  19. WeatherID []int
  20. temperature float64
  21. humidity float64
  22. }
  23. // 定时任务触发器固定的
  24. func Topic() string {
  25. return pjisuv_ticker.TickerTopic
  26. }
  27. // ******* 禁止存在下划线_
  28. // 触发器标记
  29. func Label() string {
  30. return "WetWeather"
  31. }
  32. func OpenWeatherAPI(latitude float64, longitude float64) Weather {
  33. var data map[string]interface{}
  34. var weather = Weather{}
  35. for retries := 0; retries < maxRetries; retries++ {
  36. url := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&appid=%s", latitude, longitude, apiKey)
  37. resp, err := http.Get(url)
  38. if err != nil {
  39. fmt.Println("Error:", err)
  40. time.Sleep(retryDelay)
  41. continue
  42. }
  43. defer resp.Body.Close()
  44. body, err := ioutil.ReadAll(resp.Body)
  45. if err != nil {
  46. fmt.Println("Error reading response:", err)
  47. time.Sleep(retryDelay)
  48. continue
  49. }
  50. if err := json.Unmarshal(body, &data); err != nil {
  51. fmt.Println("Error parsing JSON:", err)
  52. time.Sleep(retryDelay)
  53. continue
  54. }
  55. // Successfully retrieved data
  56. break
  57. }
  58. if data == nil {
  59. fmt.Println("Failed to retrieve data after maximum retries.")
  60. return weather
  61. }
  62. mainData := data["main"].(map[string]interface{})
  63. weather.temperature = mainData["temp"].(float64) - 273.15
  64. weather.humidity = mainData["humidity"].(float64)
  65. for _, w := range data["weather"].([]interface{}) {
  66. tianqi := w.(map[string]interface{})
  67. weather.WeatherID = append(weather.WeatherID, int(tianqi["id"].(float64)))
  68. }
  69. fmt.Printf("当前位置的天气状况/温度/湿度:%v/%.2f/%.2f\n", weather.WeatherID, weather.temperature, weather.humidity)
  70. return weather
  71. }
  72. func Rule(shareVars *sync.Map) {
  73. defer func() {
  74. if r := recover(); r != nil {
  75. fmt.Println("Recovered from panic:", r)
  76. }
  77. }()
  78. // 1 使用goroutine
  79. go func(shareVars *sync.Map) {
  80. // 2 定义触发器的间隔时间
  81. ticker := time.NewTicker(time.Duration(120) * time.Second)
  82. defer ticker.Stop()
  83. // 3 运行一个无限循环
  84. for {
  85. select {
  86. // 定时器触发时执行的代码
  87. case <-ticker.C:
  88. FinalCallback(shareVars)
  89. }
  90. }
  91. }(shareVars)
  92. }
  93. func FinalCallback(shareVars *sync.Map) {
  94. OutsideWorkshopFlag, ok := shareVars.Load("OutsideWorkshopFlag")
  95. Latitude, ok1 := shareVars.Load("Latitude")
  96. Longitude, ok2 := shareVars.Load("Longitude")
  97. if ok && ok1 && ok2 && OutsideWorkshopFlag.(bool) == true {
  98. NOwWeather := OpenWeatherAPI(Latitude.(float64), Longitude.(float64))
  99. shareVars.Store("Weather", NOwWeather)
  100. if NOwWeather.WeatherID != nil {
  101. if NOwWeather.humidity > threshold {
  102. eventLabel := "WetWeather"
  103. fmt.Println(eventLabel)
  104. pjisuv_ticker.TickerChan <- pjisuv_ticker.TickInfo{FaultLabel: Label(), FaultHappenTime: pjisuv_ticker.GetNowTimeCustom()}
  105. }
  106. }
  107. }
  108. }