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