weather.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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/rpcs"
  9. "sparrow/pkg/server"
  10. "sparrow/pkg/utils"
  11. "time"
  12. weather "weather-api-sdk"
  13. )
  14. // WeatherSceneConfig 天气监控任务配置
  15. type WeatherSceneConfig struct {
  16. SceneId string `json:"scene_id"` // 任务id
  17. DecisionExpr string `json:"decision_expr"` // 条件表达式 and or
  18. Conditions []*WeatherCondition `json:"conditions"` // 条件
  19. Interval int `json:"interval"` // 检查间隔(分钟)
  20. Actions []*Action `json:"actions"` // 执行动作
  21. ticker *time.Ticker `json:"-"` // 定时器
  22. stopChan chan struct{} `json:"-"` // 停止信号通道
  23. }
  24. type WeatherCondition struct {
  25. ConditionId string `json:"condition_id"` // 条件id
  26. Location string `json:"location"` // 地点
  27. FieldType int `json:"field_type"` // 字段类型 1字符串 2数值
  28. Operator int `json:"operator"` // 比较类型 数值比较 1 > 2 >= 3 = 4 <= 5 < 6 != 字符串比较 1 相等 2 不相等
  29. FieldName string `json:"field_name"` // 字段名
  30. Field string `json:"field"` // 字段
  31. TargetValue string `json:"target_value"` // 目标值
  32. }
  33. type CheckResult struct {
  34. Result bool
  35. ConditionId []string
  36. }
  37. type WeatherSceneService struct {
  38. tasks *gmap.HashMap
  39. }
  40. func NewWeatherSceneService() *WeatherSceneService {
  41. return &WeatherSceneService{
  42. tasks: gmap.NewHashMap(true),
  43. }
  44. }
  45. func (w *WeatherSceneService) Add(config string) error {
  46. var c WeatherSceneConfig
  47. err := json.Unmarshal([]byte(config), &c)
  48. if err != nil {
  49. }
  50. if len(c.Conditions) == 0 {
  51. return errors.New("天气监控任务配置错误:判断条件不能为空")
  52. }
  53. // 初始化Ticker和停止通道
  54. c.ticker = time.NewTicker(600 * time.Second)
  55. c.stopChan = make(chan struct{})
  56. // 启动监控协程
  57. go w.monitorTask(c)
  58. w.tasks.Set(c.SceneId, c)
  59. return nil
  60. }
  61. // monitorTask 监控任务:使用select监听Ticker和停止信号
  62. func (w *WeatherSceneService) monitorTask(task WeatherSceneConfig) {
  63. for {
  64. select {
  65. case <-task.ticker.C: // 定时触发
  66. result, err := w.checkWeatherCondition(task)
  67. if err != nil {
  68. server.Log.Errorf("compare weather condition error :%s", err.Error())
  69. }
  70. if result.Result {
  71. taskExecutor := NewTaskExecutor(task.Actions)
  72. if err = taskExecutor.Do(task.SceneId); err != nil {
  73. server.Log.Errorf("weather do taskid :%s error:%s", task.SceneId, err.Error())
  74. }
  75. err = taskExecutor.saveHis(task.SceneId, result.ConditionId)
  76. }
  77. case <-task.stopChan: // 收到停止信号
  78. task.ticker.Stop()
  79. return
  80. }
  81. }
  82. }
  83. func (w *WeatherSceneService) Update(config string) error {
  84. var c WeatherSceneConfig
  85. err := json.Unmarshal([]byte(config), &c)
  86. if err != nil {
  87. server.Log.Errorf("config to timerConfig error :%s", err.Error())
  88. }
  89. _ = w.Stop(c.SceneId)
  90. // 初始化Ticker和停止通道
  91. c.ticker = time.NewTicker(600 * time.Second)
  92. c.stopChan = make(chan struct{})
  93. // 启动监控协程
  94. go w.monitorTask(c)
  95. w.tasks.Set(c.SceneId, c)
  96. server.Log.Debugf("UpdateWeatherScene :%s", config)
  97. return nil
  98. }
  99. func (w *WeatherSceneService) Remove(id string) error {
  100. scene := w.tasks.Get(id)
  101. if scene == nil {
  102. return nil
  103. }
  104. c := scene.(WeatherSceneConfig)
  105. c.stopChan <- struct{}{}
  106. w.tasks.Remove(c.SceneId)
  107. server.Log.Debugf("RemoveTimeScene :%s", c.SceneId)
  108. return nil
  109. }
  110. // Start 停止任务
  111. func (w *WeatherSceneService) Start(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. go w.monitorTask(c)
  118. return nil
  119. }
  120. // Stop 停止任务
  121. func (w *WeatherSceneService) Stop(id string) error {
  122. if !w.tasks.Contains(id) {
  123. return errors.New("任务不存在")
  124. }
  125. task := w.tasks.Get(id)
  126. c := task.(WeatherSceneConfig)
  127. c.stopChan <- struct{}{}
  128. return nil
  129. }
  130. // checkWeatherCondition 检查天气
  131. func (w *WeatherSceneService) checkWeatherCondition(config WeatherSceneConfig) (CheckResult, error) {
  132. results := make([]bool, len(config.Conditions))
  133. var checkResult CheckResult
  134. var err error
  135. for _, condition := range config.Conditions {
  136. // 获取天气数据
  137. weatherInfo, err := getWeatherInfo(condition.Location)
  138. fmt.Printf("任务 %s: 获取天气数据成功: %v\n", config.SceneId, weatherInfo)
  139. if err != nil {
  140. fmt.Printf("任务 %s: 获取天气数据失败: %v\n", config.SceneId, err)
  141. return checkResult, err
  142. }
  143. result := utils.CheckValue(condition.TargetValue, weatherInfo[condition.Field], condition.FieldType, condition.Operator)
  144. if result {
  145. checkResult.ConditionId = append(checkResult.ConditionId, condition.ConditionId)
  146. }
  147. results = append(results, result)
  148. }
  149. if len(results) == 0 {
  150. return checkResult, nil
  151. }
  152. switch config.DecisionExpr {
  153. case "and":
  154. for _, v := range results {
  155. if !v {
  156. checkResult.Result = false
  157. }
  158. }
  159. checkResult.Result = true
  160. case "or":
  161. for _, v := range results {
  162. if v {
  163. checkResult.Result = true
  164. }
  165. }
  166. checkResult.Result = false
  167. default:
  168. err = errors.New("无效的判断逻辑")
  169. }
  170. return checkResult, err
  171. }
  172. func getWeatherInfo(location string) (map[string]interface{}, error) {
  173. // redis获取天气数据
  174. args := rpcs.ArgsGetWeather{Location: location}
  175. reply := rpcs.ReplayWeatherInfo{}
  176. err := server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetWeatherInfo", args, &reply)
  177. if err != nil {
  178. server.Log.Errorf("天气状态获取失败:%v", err)
  179. return nil, err
  180. }
  181. if reply.Info != "" {
  182. info := gjson.New(reply.Info)
  183. // 判断数据是否过期
  184. if !info.GetTime("last_update").Before(time.Now().Add(-time.Minute * 15)) {
  185. return info.Map(), nil
  186. }
  187. }
  188. weatherInfo, err := weather.GetWeatherInfo(location, "SgqF9ZGBUj7R0dLAb")
  189. if err != nil {
  190. return nil, err
  191. }
  192. j := gjson.New(weatherInfo)
  193. err = redisSaveWeatherInfo(location, j.MustToJsonString())
  194. return j.Map(), nil
  195. }
  196. func redisSaveWeatherInfo(location string, info string) error {
  197. args := rpcs.ArgsSetWeather{
  198. Location: location,
  199. Info: info,
  200. }
  201. reply := rpcs.ReplySetStatus{}
  202. err := server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.SetWeatherInfo", args, &reply)
  203. if err != nil {
  204. server.Log.Errorf("天气状态获取失败:%v", err)
  205. return err
  206. }
  207. return nil
  208. }