weather.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package manager
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/gogf/gf/container/gmap"
  7. "github.com/gogf/gf/encoding/gjson"
  8. "sparrow/pkg/server"
  9. "sparrow/pkg/utils"
  10. "time"
  11. weather "weather-api-sdk"
  12. )
  13. // WeatherSceneConfig 天气监控任务配置
  14. type WeatherSceneConfig struct {
  15. SceneId string `json:"scene_id"` // 任务id
  16. DecisionExpr string `json:"decision_expr"` // 条件表达式 and or
  17. Conditions []*WeatherCondition `json:"conditions"` // 条件
  18. Interval int `json:"interval"` // 检查间隔(分钟)
  19. Actions []*Action `json:"actions"` // 执行动作
  20. ticker *time.Ticker `json:"-"` // 定时器
  21. stopChan chan struct{} `json:"-"` // 停止信号通道
  22. }
  23. type WeatherCondition struct {
  24. Location string `json:"location"` // 地点
  25. FieldType int `json:"field_type"` // 字段类型 1字符串 2数值
  26. Operator int `json:"operator"` // 比较类型 数值比较 1 > 2 >= 3 = 4 <= 5 < 6 != 字符串比较 1 相等 2 不相等
  27. Field string `json:"field"` // 字段名
  28. TargetValue string `json:"target_value"` // 目标值
  29. }
  30. type WeatherSceneService struct {
  31. tasks *gmap.HashMap
  32. }
  33. func NewWeatherSceneService() *WeatherSceneService {
  34. return &WeatherSceneService{
  35. tasks: gmap.NewHashMap(true),
  36. }
  37. }
  38. func (w *WeatherSceneService) Add(config string) error {
  39. var c WeatherSceneConfig
  40. err := json.Unmarshal([]byte(config), &c)
  41. if err != nil {
  42. }
  43. if len(c.Conditions) == 0 {
  44. return errors.New("天气监控任务配置错误:判断条件不能为空")
  45. }
  46. // 初始化Ticker和停止通道
  47. c.ticker = time.NewTicker(time.Duration(c.Interval) * time.Minute)
  48. c.stopChan = make(chan struct{})
  49. // 启动监控协程
  50. go w.monitorTask(c)
  51. w.tasks.Set(c.SceneId, c)
  52. return nil
  53. }
  54. // monitorTask 监控任务:使用select监听Ticker和停止信号
  55. func (w *WeatherSceneService) monitorTask(task WeatherSceneConfig) {
  56. for {
  57. select {
  58. case <-task.ticker.C: // 定时触发
  59. result, err := w.checkWeatherCondition(task)
  60. if err != nil {
  61. server.Log.Errorf("compare weather condition error :%s", err.Error())
  62. }
  63. if result {
  64. if err = NewTaskExecutor(task.Actions).Do(); err != nil {
  65. server.Log.Errorf("weather do taskid :%s error:%s", task.SceneId, err.Error())
  66. }
  67. }
  68. case <-task.stopChan: // 收到停止信号
  69. task.ticker.Stop()
  70. return
  71. }
  72. }
  73. }
  74. func (w *WeatherSceneService) Update(config string) error {
  75. var c WeatherSceneConfig
  76. err := json.Unmarshal([]byte(config), &c)
  77. if err != nil {
  78. server.Log.Errorf("config to timerConfig error :%s", err.Error())
  79. }
  80. _ = w.Stop(c.SceneId)
  81. // 初始化Ticker和停止通道
  82. c.ticker = time.NewTicker(time.Duration(c.Interval) * time.Minute)
  83. c.stopChan = make(chan struct{})
  84. // 启动监控协程
  85. go w.monitorTask(c)
  86. w.tasks.Set(c.SceneId, c)
  87. server.Log.Debugf("UpdateWeatherScene :%s", config)
  88. return nil
  89. }
  90. func (w *WeatherSceneService) Remove(config string) error {
  91. var c WeatherSceneConfig
  92. err := json.Unmarshal([]byte(config), &c)
  93. if err != nil {
  94. server.Log.Errorf("config to timerConfig error :%s", err.Error())
  95. }
  96. w.tasks.Remove(c.SceneId)
  97. server.Log.Debugf("RemoveTimeScene :%s", c.SceneId)
  98. return nil
  99. }
  100. // Start 停止任务
  101. func (w *WeatherSceneService) Start(id string) error {
  102. if !w.tasks.Contains(id) {
  103. return errors.New("任务不存在")
  104. }
  105. task := w.tasks.Get(id)
  106. c := task.(WeatherSceneConfig)
  107. go w.monitorTask(c)
  108. return nil
  109. }
  110. // Stop 停止任务
  111. func (w *WeatherSceneService) Stop(id string) error {
  112. if !w.tasks.Contains(id) {
  113. return errors.New("任务不存在")
  114. }
  115. task := w.tasks.Get(id)
  116. c := task.(WeatherSceneConfig)
  117. c.stopChan <- struct{}{}
  118. return nil
  119. }
  120. // checkWeatherCondition 检查天气
  121. func (w *WeatherSceneService) checkWeatherCondition(config WeatherSceneConfig) (bool, error) {
  122. results := make([]bool, len(config.Conditions))
  123. for _, condition := range config.Conditions {
  124. // 获取天气数据
  125. weatherInfo, err := getWeatherInfo(condition.Location)
  126. fmt.Printf("任务 %s: 获取天气数据成功: %v\n", config.SceneId, weatherInfo)
  127. if err != nil {
  128. fmt.Printf("任务 %s: 获取天气数据失败: %v\n", config.SceneId, err)
  129. return false, err
  130. }
  131. results = append(results, utils.CheckValue(condition.TargetValue, weatherInfo[condition.Field], condition.FieldType, condition.Operator))
  132. }
  133. switch config.DecisionExpr {
  134. case "and":
  135. for _, v := range results {
  136. if !v {
  137. return false, nil
  138. }
  139. }
  140. case "or":
  141. for _, v := range results {
  142. if v {
  143. return true, nil
  144. }
  145. }
  146. }
  147. return true, nil
  148. }
  149. func getWeatherInfo(location string) (map[string]interface{}, error) {
  150. weatherInfo, err := weather.GetWeatherInfo(location, "SgqF9ZGBUj7R0dLAb")
  151. if err != nil {
  152. return nil, err
  153. }
  154. return gjson.New(weatherInfo).Map(), nil
  155. }