浏览代码

更新场景执行返回

liuxiulin 2 天之前
父节点
当前提交
3de784fc49

+ 21 - 12
services/scene-service/internal/service/manager/device_status.go

@@ -24,10 +24,12 @@ type DeviceSceneConfig struct {
 
 // DeviceCondition 设备场景配置
 type DeviceCondition struct {
+	ConditionId string `json:"condition_id"`  // 条件id
 	Key         string `json:"key"`           // redis key
 	DeviceType  string `json:"device_type"`   // 设备类型
 	DeviceId    string `json:"device_id"`     // 设备id
 	SubDeviceId string `json:"sub_device_id"` // 子设备id
+	FieldName   string `json:"field_name"`    // 字段名称
 	FieldType   int    `json:"field_type"`    // 字段类型 1字符串 2数值
 	Field       string `json:"field"`         // 字段名
 	TargetValue string `json:"target_value"`  // 值
@@ -124,11 +126,11 @@ func (d *DeviceSceneService) monitorTask(config DeviceSceneConfig) {
 			if err != nil {
 				server.Log.Errorf("compare weather condition error :%s", err.Error())
 			}
-			if result {
-				if err = NewTaskExecutor(config.Actions).Do(config.SceneId); err != nil {
-					server.Log.Errorf("weather do taskid :%s error:%s", config.SceneId, err.Error())
-				}
+			taskExecutor := NewTaskExecutor(config.Actions)
+			if err = taskExecutor.Do(config.SceneId); err != nil {
+				server.Log.Errorf("weather do taskid :%s error:%s", config.SceneId, err.Error())
 			}
+			err = taskExecutor.saveHis(config.SceneId, result.ConditionId, config.Actions[0])
 		case <-config.stopChan: // 收到停止信号
 			config.ticker.Stop()
 			return
@@ -136,8 +138,10 @@ func (d *DeviceSceneService) monitorTask(config DeviceSceneConfig) {
 	}
 }
 
-func (d *DeviceSceneService) checkDeviceCondition(config DeviceSceneConfig) (bool, error) {
+func (d *DeviceSceneService) checkDeviceCondition(config DeviceSceneConfig) (CheckResult, error) {
 	var results []bool
+	var checkResult CheckResult
+	var err error
 	for _, v := range config.Conditions {
 
 		var args rpcs.ArgsGetStatus
@@ -146,29 +150,34 @@ func (d *DeviceSceneService) checkDeviceCondition(config DeviceSceneConfig) (boo
 		err := server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceStatusByKey", args, &reply)
 		if err != nil {
 			server.Log.Errorf("设备状态数据获取失败:%v", err)
-			return false, err
+			return checkResult, err
 		}
 		j := gjson.New(reply.Status)
 		// 判断是否满足条件并填入到result
-		fmt.Printf("判断条件:target_value:%s,value:%s,type:%d,operator:%d\n", v.TargetValue, j.Get(v.Field), v.FieldType, v.Operator)
 		result := utils.CheckValue(v.TargetValue, j.Get(v.Field), v.FieldType, v.Operator)
+		if result {
+			checkResult.ConditionId = append(checkResult.ConditionId, v.ConditionId)
+		}
 		results = append(results, result)
-		fmt.Printf("判断结果:%v\n", result)
+
 	}
 	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
 			}
 		}
+	default:
+		err = errors.New("无效的判断逻辑")
 	}
-	return false, nil
+
+	return checkResult, err
 }

+ 2 - 5
services/scene-service/internal/service/manager/executer.go

@@ -56,10 +56,6 @@ func (a *TaskExecutor) Do(id string) error {
 		if err := a.doTask(action); err != nil {
 			return err
 		}
-		err := a.saveHis(id, action)
-		if err != nil {
-			return err
-		}
 	}
 
 	return nil
@@ -112,13 +108,14 @@ func getAccessRPCHost(deviceid string) (string, error) {
 	return reply.AccessRPCHost, nil
 }
 
-func (a *TaskExecutor) saveHis(id string, action *Action) error {
+func (a *TaskExecutor) saveHis(id string, conditionId []string, action *Action) error {
 
 	client := utils.NewHttpClient()
 	url := "http://127.0.0.1:8199/iot/v1/scene_history"
 	body := make(map[string]interface{})
 	body["scene_id"] = id
 	body["time"] = time.Now()
+	body["condition_id"] = conditionId
 	_, err := client.Post(url, "application/json", gjson.New(body))
 	if err != nil {
 		server.Log.Errorf("sync his error:%s", err.Error())

+ 30 - 12
services/scene-service/internal/service/manager/weather.go

@@ -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) {