Commit aa50f0d6 authored by 陈伟灿's avatar 陈伟灿

Merge branch 'cwc' into 'master'

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

See merge request chenweican/k-sdk!69
parents 2aa54bfb 04a9b5c5
...@@ -923,7 +923,7 @@ static void _iotx_linkkit_event_callback(iotx_dm_event_types_t type, char *data) ...@@ -923,7 +923,7 @@ static void _iotx_linkkit_event_callback(iotx_dm_event_types_t type, char *data)
if(identifierStr != NULL){ if(identifierStr != NULL){
int value_ = 0; int value_ = 0;
dm_mgr_dev_node_t *node = NULL; 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_; value_ = !value_;
res = dm_mgr_get_device_by_devicecode(deviceCode->valuestring, &node); res = dm_mgr_get_device_by_devicecode(deviceCode->valuestring, &node);
if (res != SUCCESS_RETURN) { if (res != SUCCESS_RETURN) {
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
#include "sqlite3.h" #include "sqlite3.h"
#include "kk_log.h" #include "kk_log.h"
#include "kk_scene_handle.h" #include "kk_scene_handle.h"
#include "kk_scene_db.h"
/************************************************************* /*************************************************************
全局变量定义 全局变量定义
...@@ -70,7 +72,8 @@ int kk_scene_db_init(void) ...@@ -70,7 +72,8 @@ int kk_scene_db_init(void)
propertyName varchar(255), \ propertyName varchar(255), \
compareType varchar(255), \ compareType varchar(255), \
compareValue varchar(255), \ compareValue varchar(255), \
sceneId varchar(255))"; sceneId varchar(255), \
isAnd INTEGER)";
if (sqlite3_exec(ctx->pDb, pSceneTriggerTable, NULL, NULL, &pcErr) != SQLITE_OK) 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 ...@@ -220,34 +223,57 @@ int kk_scene_insert_scene_info(const char* name,const char* sceneType,const char
return SUCCESS_RETURN; 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) const char* compareType,const char* compareValue,const char* sceneId)
{ {
int res = 0; int res = 0;
kk_scene_ctx_t *ctx = _kk_scene_get_ctx(); kk_scene_ctx_t *ctx = _kk_scene_get_ctx();
char *sqlCmd = NULL; char *sqlCmd = NULL;
sqlite3_stmt *stmt; 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';"; const char *selectCmd = "select * from SceneTriggerInfo WHERE deviceCode = '%s' and propertyName = '%s';";
_kk_scene_lock(); _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); sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL);
while(sqlite3_step(stmt) == SQLITE_ROW){ while(sqlite3_step(stmt) == SQLITE_ROW){
*sceneType = sqlite3_column_int(stmt, DB_SCENEINFO_SCENETYPE); dbepNum = sqlite3_column_int(stmt, DB_SCENETRIGGER_EPNUM);
*enable = sqlite3_column_int(stmt, DB_SCENEINFO_ENABLE); dbcompareType = sqlite3_column_text(stmt, DB_SCENETRIGGER_COMPARETYPE);
res = SUCCESS_RETURN; 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_finalize(stmt);
sqlite3_free(sqlCmd); sqlite3_free(sqlCmd);
_kk_scene_unlock(); _kk_scene_unlock();
return SUCCESS_RETURN; return res;
} }
#endif
/************************************************************ /************************************************************
*功能描述: 添加场景触发信息到数据库 *功能描述: 添加场景触发信息到数据库
*输入参数: type:类型 *输入参数: type:类型
...@@ -262,19 +288,22 @@ static int kk_scene_check_trigger_exist(const char* type,const char* deviceCode, ...@@ -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, 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) const char* compareType,const char* compareValue,const char* sceneId,int isAnd)
{ {
int res = 0; int res = 0;
kk_scene_ctx_t *ctx = _kk_scene_get_ctx(); kk_scene_ctx_t *ctx = _kk_scene_get_ctx();
char *sqlCmd = NULL; char *sqlCmd = NULL;
char *zErrMsg = 0; char *zErrMsg = 0;
/*此触发条件已经存在*/
const char *insertCmd = "insert into SceneTriggerInfo (type, deviceCode,epNum,propertyName,compareType,compareValue,sceneId) \ if(kk_scene_check_trigger_exist(deviceCode,epNum,propertyName,compareType,compareValue,sceneId)){
values ('%s','%s','%d','%s','%s','%s','%s');"; 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(); _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); res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
if( res != SQLITE_OK ){ if( res != SQLITE_OK ){
ERROR_PRINT("SQL error: %s\n", zErrMsg); ERROR_PRINT("SQL error: %s\n", zErrMsg);
......
...@@ -24,7 +24,7 @@ typedef enum{ ...@@ -24,7 +24,7 @@ typedef enum{
DB_SCENETRIGGER_COMPARETYPE, DB_SCENETRIGGER_COMPARETYPE,
DB_SCENETRIGGER_COMPAREVALUE, DB_SCENETRIGGER_COMPAREVALUE,
DB_SCENETRIGGER_SCENEID, DB_SCENETRIGGER_SCENEID,
//DB_SCENETRIGGER_TRIGGERFLAG, DB_SCENETRIGGER_ISAND,
}; };
typedef enum{ typedef enum{
......
...@@ -35,6 +35,9 @@ static kk_scene_timer_list_t *p_scene_timer_list = NULL; ...@@ -35,6 +35,9 @@ static kk_scene_timer_list_t *p_scene_timer_list = NULL;
extern uint64_t s_start_time; extern uint64_t s_start_time;
static kk_scene_action_t *p_kk_scene_action = NULL; static kk_scene_action_t *p_kk_scene_action = NULL;
static kk_scene_ctx_t s_kk_scene_ctx = {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) ...@@ -259,7 +262,11 @@ void *kk_scene_yield(void *args)
INFO_PRINT("scene_timer_list->starttime:%d\n",scene_timer_list->starttime); INFO_PRINT("scene_timer_list->starttime:%d\n",scene_timer_list->starttime);
INFO_PRINT("current_time %d\n",current_time); INFO_PRINT("current_time %d\n",current_time);
if(scene_timer_list->starttime != 0 && current_time >= scene_timer_list->starttime){ 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); kk_scene_update_starttime(scene_timer_list,current_time);
} }
scene_timer_list = scene_timer_list->next; 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 ...@@ -558,12 +565,13 @@ int kk_scene_action_add(const char *gwdeviceCode,const char *sceneId,kk_scene_ac
*输入参数:type:触发类型; *输入参数:type:触发类型;
item:触发内容的CJSON字串; item:触发内容的CJSON字串;
sceneId:场景Id sceneId:场景Id
isAnd:是否是且操作
*输出参数:无 *输出参数:无
*返 回 值: 0:成功;其他:失败 *返 回 值: 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; int res = FAIL_RETURN;
if(type == NULL || item == NULL || sceneId == NULL){ 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 ...@@ -579,8 +587,8 @@ static int kk_scene_parse_trigger_detail(const char *type,const cJSON *item,cons
if(compareType == NULL) return FAIL_RETURN; if(compareType == NULL) return FAIL_RETURN;
cJSON *compareValue = cJSON_GetObjectItem(item,MSG_SCENE_COMPAREVALUE); cJSON *compareValue = cJSON_GetObjectItem(item,MSG_SCENE_COMPAREVALUE);
if(compareValue == NULL) return FAIL_RETURN; if(compareValue == NULL) return FAIL_RETURN;
res = kk_scene_insert_scene_trigger(type,deviceCode->valuestring,deviceCode->valueint, res = kk_scene_insert_scene_trigger(type,deviceCode->valuestring,epNum->valueint,
propertyName->valuestring,compareType->valuestring,compareValue->valuestring,sceneId); propertyName->valuestring,compareType->valuestring,compareValue->valuestring,sceneId,isAnd);
if(res != SUCCESS_RETURN){ if(res != SUCCESS_RETURN){
ERROR_PRINT("kk_scene_parse_scene_trigger fail!!!\n"); 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) ...@@ -612,7 +620,7 @@ int kk_scene_parse_scene_trigger(const cJSON* str,const char *sceneId)
cJSON *type = cJSON_GetObjectItem(item,MSG_SCENE_TYPE); cJSON *type = cJSON_GetObjectItem(item,MSG_SCENE_TYPE);
if(type == NULL) return FAIL_RETURN; if(type == NULL) return FAIL_RETURN;
if(!strcmp("trigger/thing/property",type->valuestring)){ 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){ if(res != SUCCESS_RETURN){
ERROR_PRINT("kk_scene_parse_scene_trigger fail!!!\n"); ERROR_PRINT("kk_scene_parse_scene_trigger fail!!!\n");
return res; return res;
...@@ -786,8 +794,8 @@ int kk_scene_parse_scene_condition(const cJSON* str,const char *sceneId) ...@@ -786,8 +794,8 @@ int kk_scene_parse_scene_condition(const cJSON* str,const char *sceneId)
return res; return res;
} }
} }
else if(!strcmp("condition/property",type->valuestring)){ else if(!strcmp("trigger/thing/property",type->valuestring)){
kk_scene_parse_trigger_detail(type->valuestring,item,sceneId); kk_scene_parse_trigger_detail(type->valuestring,item,sceneId,1);
} }
else{ else{
INFO_PRINT("kk_scene_parse_scene_condition wrong type!!!\n"); 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 ...@@ -1083,7 +1091,7 @@ static int kk_scene_update_starttime(kk_scene_timer_list_t *pInfo,time_t current
/*only support once*/ /*only support once*/
if(pInfo->repeatday == 0){ if(pInfo->repeatday == 0){
pInfo->starttime = 0; pInfo->starttime = 0;
kk_scene_update_scene_enable(0,pInfo->sceneId); //kk_scene_update_scene_enable(0,pInfo->sceneId);
return SUCCESS_RETURN; return SUCCESS_RETURN;
}else{ }else{
curWeek = kk_scene_date_to_week(current); curWeek = kk_scene_date_to_week(current);
...@@ -1200,7 +1208,7 @@ int kk_scene_check_trigger_condition(const char *sceneId) ...@@ -1200,7 +1208,7 @@ int kk_scene_check_trigger_condition(const char *sceneId)
dm_mgr_dev_node_t *node = NULL; dm_mgr_dev_node_t *node = NULL;
kk_scene_ctx_t *ctx = _kk_scene_get_ctx(); 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); sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL);
while(sqlite3_step(stmt) == SQLITE_ROW){ while(sqlite3_step(stmt) == SQLITE_ROW){
conditionFlag = 1; conditionFlag = 1;
...@@ -1460,7 +1468,15 @@ static void kk_scene_send_action_msg(kk_scene_action_info_t *pInfo) ...@@ -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_INT:
case KK_TSL_DATA_TYPE_ENUM: case KK_TSL_DATA_TYPE_ENUM:
case KK_TSL_DATA_TYPE_BOOL: 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); cJSON_AddNumberToObject(root,pInfo->propertyName,ivalue);
break; break;
case KK_TSL_DATA_TYPE_FLOAT: case KK_TSL_DATA_TYPE_FLOAT:
...@@ -1711,6 +1727,7 @@ int kk_scene_query_trigger_info(const char *deviceCode,cJSON *param) ...@@ -1711,6 +1727,7 @@ int kk_scene_query_trigger_info(const char *deviceCode,cJSON *param)
char *compareType = NULL; char *compareType = NULL;
char *compareValue = NULL; char *compareValue = NULL;
int propertyValueType = 0; int propertyValueType = 0;
int isAnd = 0;
int sceneType = 0,isEnable = 0; int sceneType = 0,isEnable = 0;
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
kk_scene_ctx_t *ctx = _kk_scene_get_ctx(); kk_scene_ctx_t *ctx = _kk_scene_get_ctx();
...@@ -1723,8 +1740,9 @@ int kk_scene_query_trigger_info(const char *deviceCode,cJSON *param) ...@@ -1723,8 +1740,9 @@ int kk_scene_query_trigger_info(const char *deviceCode,cJSON *param)
identifier = sqlite3_column_text(stmt,DB_SCENETRIGGER_PROPERTYNAME); identifier = sqlite3_column_text(stmt,DB_SCENETRIGGER_PROPERTYNAME);
compareType = sqlite3_column_text(stmt,DB_SCENETRIGGER_COMPARETYPE); compareType = sqlite3_column_text(stmt,DB_SCENETRIGGER_COMPARETYPE);
compareValue = sqlite3_column_text(stmt,DB_SCENETRIGGER_COMPAREVALUE); 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); 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); cJSON *item = cJSON_GetObjectItem(param,identifier);
if(item != NULL){ if(item != NULL){
propertyValueType = kk_dm_get_property_type(deviceCode,identifier); 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