|
|
@@ -25,13 +25,20 @@ type WeatherSceneConfig struct {
|
|
|
}
|
|
|
|
|
|
type WeatherCondition struct {
|
|
|
+ ConditionId string `json:"condition_id"` // 条件id
|
|
|
Location string `json:"location"` // 地点
|
|
|
FieldType int `json:"field_type"` // 字段类型 1字符串 2数值
|
|
|
Operator int `json:"operator"` // 比较类型 数值比较 1 > 2 >= 3 = 4 <= 5 < 6 != 字符串比较 1 相等 2 不相等
|
|
|
- Field string `json:"field"` // 字段名
|
|
|
+ FieldName string `json:"field_name"` // 字段名
|
|
|
+ Field string `json:"field"` // 字段
|
|
|
TargetValue string `json:"target_value"` // 目标值
|
|
|
}
|
|
|
|
|
|
+type CheckResult struct {
|
|
|
+ Result bool
|
|
|
+ ConditionId []string
|
|
|
+}
|
|
|
+
|
|
|
type WeatherSceneService struct {
|
|
|
tasks *gmap.HashMap
|
|
|
}
|
|
|
@@ -69,10 +76,12 @@ func (w *WeatherSceneService) monitorTask(task WeatherSceneConfig) {
|
|
|
if err != nil {
|
|
|
server.Log.Errorf("compare weather condition error :%s", err.Error())
|
|
|
}
|
|
|
- if result {
|
|
|
- if err = NewTaskExecutor(task.Actions).Do(task.SceneId); err != nil {
|
|
|
+ if result.Result {
|
|
|
+ taskExecutor := NewTaskExecutor(task.Actions)
|
|
|
+ if err := taskExecutor.Do(task.SceneId); err != nil {
|
|
|
server.Log.Errorf("weather do taskid :%s error:%s", task.SceneId, err.Error())
|
|
|
}
|
|
|
+ err = taskExecutor.saveHis(task.SceneId, result.ConditionId, task.Actions[0])
|
|
|
}
|
|
|
case <-task.stopChan: // 收到停止信号
|
|
|
task.ticker.Stop()
|
|
|
@@ -135,39 +144,48 @@ func (w *WeatherSceneService) Stop(id string) error {
|
|
|
}
|
|
|
|
|
|
// checkWeatherCondition 检查天气
|
|
|
-func (w *WeatherSceneService) checkWeatherCondition(config WeatherSceneConfig) (bool, error) {
|
|
|
+func (w *WeatherSceneService) checkWeatherCondition(config WeatherSceneConfig) (CheckResult, error) {
|
|
|
results := make([]bool, len(config.Conditions))
|
|
|
+ var checkResult CheckResult
|
|
|
+ var err error
|
|
|
for _, condition := range config.Conditions {
|
|
|
// 获取天气数据
|
|
|
weatherInfo, err := getWeatherInfo(condition.Location)
|
|
|
fmt.Printf("任务 %s: 获取天气数据成功: %v\n", config.SceneId, weatherInfo)
|
|
|
if err != nil {
|
|
|
fmt.Printf("任务 %s: 获取天气数据失败: %v\n", config.SceneId, err)
|
|
|
- return false, err
|
|
|
+ return checkResult, err
|
|
|
+ }
|
|
|
+ result := utils.CheckValue(condition.TargetValue, weatherInfo[condition.Field], condition.FieldType, condition.Operator)
|
|
|
+ if result {
|
|
|
+ checkResult.ConditionId = append(checkResult.ConditionId, condition.ConditionId)
|
|
|
}
|
|
|
- results = append(results, utils.CheckValue(condition.TargetValue, weatherInfo[condition.Field], condition.FieldType, condition.Operator))
|
|
|
+ results = append(results, result)
|
|
|
+
|
|
|
}
|
|
|
if len(results) == 0 {
|
|
|
- return false, nil
|
|
|
+ return checkResult, nil
|
|
|
}
|
|
|
switch config.DecisionExpr {
|
|
|
case "and":
|
|
|
for _, v := range results {
|
|
|
if !v {
|
|
|
- return false, nil
|
|
|
+ checkResult.Result = false
|
|
|
}
|
|
|
}
|
|
|
- return true, nil
|
|
|
+ checkResult.Result = true
|
|
|
+
|
|
|
case "or":
|
|
|
for _, v := range results {
|
|
|
if v {
|
|
|
- return true, nil
|
|
|
+ checkResult.Result = true
|
|
|
}
|
|
|
}
|
|
|
- return false, nil
|
|
|
+ checkResult.Result = false
|
|
|
default:
|
|
|
- return false, errors.New("无效的判断逻辑")
|
|
|
+ err = errors.New("无效的判断逻辑")
|
|
|
}
|
|
|
+ return checkResult, err
|
|
|
}
|
|
|
|
|
|
func getWeatherInfo(location string) (map[string]interface{}, error) {
|