Commit 04a9b5c5 authored by chen.weican's avatar chen.weican

【修改内容】增加场景且和或的逻辑处理

【提交人】陈伟灿
parent 2aa54bfb
......@@ -923,7 +923,7 @@ static void _iotx_linkkit_event_callback(iotx_dm_event_types_t type, char *data)
if(identifierStr != NULL){
int value_ = 0;
dm_mgr_dev_node_t *node = NULL;
kk_property_db_get_value(deviceCode->valuestring,identifierStr->valuestring,4, &value_);
kk_property_db_get_value(deviceCode->valuestring,identifierStr->valuestring, &value_);
value_ = !value_;
res = dm_mgr_get_device_by_devicecode(deviceCode->valuestring, &node);
if (res != SUCCESS_RETURN) {
......
......@@ -16,6 +16,8 @@
#include "sqlite3.h"
#include "kk_log.h"
#include "kk_scene_handle.h"
#include "kk_scene_db.h"
/*************************************************************
全局变量定义
......@@ -70,7 +72,8 @@ int kk_scene_db_init(void)
propertyName varchar(255), \
compareType varchar(255), \
compareValue varchar(255), \
sceneId varchar(255))";
sceneId varchar(255), \
isAnd INTEGER)";
if (sqlite3_exec(ctx->pDb, pSceneTriggerTable, NULL, NULL, &pcErr) != SQLITE_OK)
......@@ -220,34 +223,57 @@ int kk_scene_insert_scene_info(const char* name,const char* sceneType,const char
return SUCCESS_RETURN;
}
#if 0
static int kk_scene_check_trigger_exist(const char* type,const char* deviceCode,const char* epNum,const char* propertyName,
/************************************************************
*功能描述: 检查场景触发是否已经存在
*输入参数: deviceCode:设备deviceCode
epNum: 设备路数
propertyName:属性名称
compareType:比对符号
compareValue:value值
sceneId:场景Id
*输出参数: 无
*返 回 值: 0:成功;其他:失败
*其他说明:
*************************************************************/
static int kk_scene_check_trigger_exist(const char* deviceCode,int epNum,const char* propertyName,
const char* compareType,const char* compareValue,const char* sceneId)
{
int res = 0;
kk_scene_ctx_t *ctx = _kk_scene_get_ctx();
char *sqlCmd = NULL;
sqlite3_stmt *stmt;
int dbepNum = 0;
char *dbcompareValue =NULL;
char *dbcompareType =NULL;
char *dbsceneId =NULL;
const char *selectCmd = "select * from SceneTriggerInfo WHERE deviceCode = '%s' and propertyName = '%s';";
_kk_scene_lock();
sqlCmd = sqlite3_mprintf(selectCmd,,deviceCode,propertyName);
sqlCmd = sqlite3_mprintf(selectCmd,deviceCode,propertyName);
sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL);
while(sqlite3_step(stmt) == SQLITE_ROW){
*sceneType = sqlite3_column_int(stmt, DB_SCENEINFO_SCENETYPE);
*enable = sqlite3_column_int(stmt, DB_SCENEINFO_ENABLE);
res = SUCCESS_RETURN;
dbepNum = sqlite3_column_int(stmt, DB_SCENETRIGGER_EPNUM);
dbcompareType = sqlite3_column_text(stmt, DB_SCENETRIGGER_COMPARETYPE);
dbcompareValue = sqlite3_column_text(stmt, DB_SCENETRIGGER_COMPAREVALUE);
dbsceneId = sqlite3_column_text(stmt, DB_SCENETRIGGER_SCENEID);
if((epNum == dbepNum) && (!strcmp(dbcompareType,compareType)) && \
(!strcmp(compareValue,compareValue)) && (!strcmp(dbsceneId,sceneId))){
res = 1;
INFO_PRINT("kk_scene_check_trigger_exist!!!\n");
break;
}
}
sqlite3_finalize(stmt);
sqlite3_free(sqlCmd);
_kk_scene_unlock();
return SUCCESS_RETURN;
return res;
}
#endif
/************************************************************
*功能描述: 添加场景触发信息到数据库
*输入参数: type:类型
......@@ -262,19 +288,22 @@ static int kk_scene_check_trigger_exist(const char* type,const char* deviceCode,
*其他说明:
*************************************************************/
int kk_scene_insert_scene_trigger(const char* type,const char* deviceCode,const char* epNum,const char* propertyName,
const char* compareType,const char* compareValue,const char* sceneId)
int kk_scene_insert_scene_trigger(const char* type,const char* deviceCode,int epNum,const char* propertyName,
const char* compareType,const char* compareValue,const char* sceneId,int isAnd)
{
int res = 0;
kk_scene_ctx_t *ctx = _kk_scene_get_ctx();
char *sqlCmd = NULL;
char *zErrMsg = 0;
const char *insertCmd = "insert into SceneTriggerInfo (type, deviceCode,epNum,propertyName,compareType,compareValue,sceneId) \
values ('%s','%s','%d','%s','%s','%s','%s');";
/*此触发条件已经存在*/
if(kk_scene_check_trigger_exist(deviceCode,epNum,propertyName,compareType,compareValue,sceneId)){
return SUCCESS_RETURN;
}
const char *insertCmd = "insert into SceneTriggerInfo (type, deviceCode,epNum,propertyName,compareType,compareValue,sceneId,isAnd) \
values ('%s','%s','%d','%s','%s','%s','%s','%d');";
_kk_scene_lock();
sqlCmd = sqlite3_mprintf(insertCmd,type,deviceCode,epNum,propertyName,compareType,compareValue,sceneId);
sqlCmd = sqlite3_mprintf(insertCmd,type,deviceCode,epNum,propertyName,compareType,compareValue,sceneId,isAnd);
res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
if( res != SQLITE_OK ){
ERROR_PRINT("SQL error: %s\n", zErrMsg);
......
......@@ -24,7 +24,7 @@ typedef enum{
DB_SCENETRIGGER_COMPARETYPE,
DB_SCENETRIGGER_COMPAREVALUE,
DB_SCENETRIGGER_SCENEID,
//DB_SCENETRIGGER_TRIGGERFLAG,
DB_SCENETRIGGER_ISAND,
};
typedef enum{
......
......@@ -35,6 +35,9 @@ static kk_scene_timer_list_t *p_scene_timer_list = NULL;
extern uint64_t s_start_time;
static kk_scene_action_t *p_kk_scene_action = NULL;
static kk_scene_ctx_t s_kk_scene_ctx = {NULL};
int kk_scene_check_trigger_condition(const char *sceneId);
static int kk_scene_embed_find(const char *sceneId);
/*************************************************************
函数实现
......@@ -259,7 +262,11 @@ void *kk_scene_yield(void *args)
INFO_PRINT("scene_timer_list->starttime:%d\n",scene_timer_list->starttime);
INFO_PRINT("current_time %d\n",current_time);
if(scene_timer_list->starttime != 0 && current_time >= scene_timer_list->starttime){
kk_scene_execute_action(scene_timer_list->sceneId);
if(kk_scene_check_trigger_condition(scene_timer_list->sceneId) == 0){
if(kk_scene_embed_find(scene_timer_list->sceneId) == 0){
kk_scene_execute_action(scene_timer_list->sceneId);
}
}
kk_scene_update_starttime(scene_timer_list,current_time);
}
scene_timer_list = scene_timer_list->next;
......@@ -558,12 +565,13 @@ int kk_scene_action_add(const char *gwdeviceCode,const char *sceneId,kk_scene_ac
*输入参数:type:触发类型;
item:触发内容的CJSON字串;
sceneId:场景Id
isAnd:是否是且操作
*输出参数:无
*返 回 值: 0:成功;其他:失败
*其他说明:
*************************************************************/
static int kk_scene_parse_trigger_detail(const char *type,const cJSON *item,const char *sceneId)
static int kk_scene_parse_trigger_detail(const char *type,const cJSON *item,const char *sceneId,int isAnd)
{
int res = FAIL_RETURN;
if(type == NULL || item == NULL || sceneId == NULL){
......@@ -579,8 +587,8 @@ static int kk_scene_parse_trigger_detail(const char *type,const cJSON *item,cons
if(compareType == NULL) return FAIL_RETURN;
cJSON *compareValue = cJSON_GetObjectItem(item,MSG_SCENE_COMPAREVALUE);
if(compareValue == NULL) return FAIL_RETURN;
res = kk_scene_insert_scene_trigger(type,deviceCode->valuestring,deviceCode->valueint,
propertyName->valuestring,compareType->valuestring,compareValue->valuestring,sceneId);
res = kk_scene_insert_scene_trigger(type,deviceCode->valuestring,epNum->valueint,
propertyName->valuestring,compareType->valuestring,compareValue->valuestring,sceneId,isAnd);
if(res != SUCCESS_RETURN){
ERROR_PRINT("kk_scene_parse_scene_trigger fail!!!\n");
}
......@@ -612,7 +620,7 @@ int kk_scene_parse_scene_trigger(const cJSON* str,const char *sceneId)
cJSON *type = cJSON_GetObjectItem(item,MSG_SCENE_TYPE);
if(type == NULL) return FAIL_RETURN;
if(!strcmp("trigger/thing/property",type->valuestring)){
res = kk_scene_parse_trigger_detail(type->valuestring,item,sceneId);
res = kk_scene_parse_trigger_detail(type->valuestring,item,sceneId,0);
if(res != SUCCESS_RETURN){
ERROR_PRINT("kk_scene_parse_scene_trigger fail!!!\n");
return res;
......@@ -786,8 +794,8 @@ int kk_scene_parse_scene_condition(const cJSON* str,const char *sceneId)
return res;
}
}
else if(!strcmp("condition/property",type->valuestring)){
kk_scene_parse_trigger_detail(type->valuestring,item,sceneId);
else if(!strcmp("trigger/thing/property",type->valuestring)){
kk_scene_parse_trigger_detail(type->valuestring,item,sceneId,1);
}
else{
INFO_PRINT("kk_scene_parse_scene_condition wrong type!!!\n");
......@@ -1083,7 +1091,7 @@ static int kk_scene_update_starttime(kk_scene_timer_list_t *pInfo,time_t current
/*only support once*/
if(pInfo->repeatday == 0){
pInfo->starttime = 0;
kk_scene_update_scene_enable(0,pInfo->sceneId);
//kk_scene_update_scene_enable(0,pInfo->sceneId);
return SUCCESS_RETURN;
}else{
curWeek = kk_scene_date_to_week(current);
......@@ -1200,7 +1208,7 @@ int kk_scene_check_trigger_condition(const char *sceneId)
dm_mgr_dev_node_t *node = NULL;
kk_scene_ctx_t *ctx = _kk_scene_get_ctx();
sqlCmd = sqlite3_mprintf("select * from SceneTriggerInfo WHERE sceneId = '%s' and type = '%s'",sceneId,"condition/property");
sqlCmd = sqlite3_mprintf("select * from SceneTriggerInfo WHERE sceneId = '%s' and isAnd = '%d'",sceneId,1);
sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL);
while(sqlite3_step(stmt) == SQLITE_ROW){
conditionFlag = 1;
......@@ -1460,7 +1468,15 @@ static void kk_scene_send_action_msg(kk_scene_action_info_t *pInfo)
case KK_TSL_DATA_TYPE_INT:
case KK_TSL_DATA_TYPE_ENUM:
case KK_TSL_DATA_TYPE_BOOL:
ivalue = atoi(pInfo->propertyValue);
/*pInfo->propertyValue 如果为‘10’,代表需要取反操作*/
if(strstr(pInfo->propertyName,"PowerSwitch") != NULL && \
!strcmp(pInfo->propertyValue,"10")){
kk_property_db_get_value(pInfo->deviceCode,pInfo->propertyName, &ivalue);
ivalue = !ivalue;
}
else{
ivalue = atoi(pInfo->propertyValue);
}
cJSON_AddNumberToObject(root,pInfo->propertyName,ivalue);
break;
case KK_TSL_DATA_TYPE_FLOAT:
......@@ -1711,6 +1727,7 @@ int kk_scene_query_trigger_info(const char *deviceCode,cJSON *param)
char *compareType = NULL;
char *compareValue = NULL;
int propertyValueType = 0;
int isAnd = 0;
int sceneType = 0,isEnable = 0;
sqlite3_stmt *stmt;
kk_scene_ctx_t *ctx = _kk_scene_get_ctx();
......@@ -1723,8 +1740,9 @@ int kk_scene_query_trigger_info(const char *deviceCode,cJSON *param)
identifier = sqlite3_column_text(stmt,DB_SCENETRIGGER_PROPERTYNAME);
compareType = sqlite3_column_text(stmt,DB_SCENETRIGGER_COMPARETYPE);
compareValue = sqlite3_column_text(stmt,DB_SCENETRIGGER_COMPAREVALUE);
isAnd = sqlite3_column_int(stmt,DB_SCENETRIGGER_ISAND);
res = kk_scene_get_scene_info(sceneId,&sceneType,&isEnable);
if(res == SUCCESS_RETURN && sceneType == DB_SCENETYPE_IFTT && isEnable){
if(res == SUCCESS_RETURN && isEnable && isAnd == 0){
cJSON *item = cJSON_GetObjectItem(param,identifier);
if(item != NULL){
propertyValueType = kk_dm_get_property_type(deviceCode,identifier);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment