Commit 4b30d68d authored by chen.weican's avatar chen.weican

【修改内容】测试定时模式的场景并调整scene相关代码

【提交人】陈伟灿
parent aa3d17c0
......@@ -117,21 +117,18 @@ int kk_area_init(void)
}
uint64_t kk_room_add(const char *name)
int kk_room_add(const char *name,const char *roomId)
{
int res = 0;
kk_area_ctx_t *ctx = _kk_area_get_ctx();
char *sqlCmd = NULL;
char roomId[32] = {0};
char *zErrMsg = 0;
uint64_t u64RoomId = 0;
const char *insertCmd = "insert into AreaInfo (name, roomId) \
values ('%s','%s');";
_kk_area_lock();
ctx->roomNum++;
u64RoomId = get_unique_id();
sprintf(roomId,"%u",u64RoomId);
HAL_GetTimeMs(roomId);
sqlCmd = sqlite3_mprintf(insertCmd,name,roomId);
res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
......@@ -143,7 +140,7 @@ uint64_t kk_room_add(const char *name)
}
sqlite3_free(sqlCmd);
_kk_area_unlock();
return u64RoomId;
return SUCCESS_RETURN;
}
int kk_room_delete(const char *roomId)
......
......@@ -281,8 +281,7 @@ static int kk_service_addRoom_handle(const char *deviceCode, cJSON *params)
if(roomInfoStr == NULL){
return FAIL_RETURN;
}
uint64_t id = kk_room_add(roomInfoStr->valuestring);
sprintf(roomId,"%u",id);
kk_room_add(roomInfoStr->valuestring,roomId);
kk_tsl_set_value(kk_tsl_set_event_output_value,node->dev_shadow,MSG_AREA_ADDROOM_NOTIFICATION_ROOMID,NULL,roomId);
return SUCCESS_RETURN;
......@@ -519,7 +518,6 @@ static int kk_service_getLockKeylist_handle(const char *deviceCode)
static int kk_service_addKey_handle(const char *deviceCode,cJSON *param)
{
char keyId[32] = {0};
uint64_t u64KeyId = 0;
int res = 0;
dm_mgr_dev_node_t *node = NULL;
if(deviceCode == NULL || param == NULL){
......@@ -543,8 +541,7 @@ static int kk_service_addKey_handle(const char *deviceCode,cJSON *param)
if(KeyEffectiveTime == NULL) return FAIL_RETURN;;
cJSON *KeyExpiryTime = cJSON_GetObjectItem(param,MSG_KEYADD_KEYEXPIRE);
if(KeyExpiryTime == NULL) return FAIL_RETURN;;
u64KeyId = get_unique_id();
sprintf(keyId,"%u",u64KeyId);
HAL_GetTimeMs(keyId);
kk_property_update_lockkeys(deviceCode,keyId,KeyType->valueint,KeyRole->valueint,IsValid->valueint,
KeyName->valuestring,KeyEffectiveTime->valueint,KeyExpiryTime->valueint);
kk_tsl_set_value(kk_tsl_set_event_output_value,node->dev_shadow,MSG_KEYADD_NOTIFICATION_KEYID,NULL,keyId);
......
......@@ -78,7 +78,7 @@ static void _kk_scene_lock(void)
static void _kk_scene_unlock(void)
{
kk_scene_ctx_t *ctx = _kk_scene_get_ctx();
if (ctx->mutex) {
HAL_MutexUnlock(ctx->mutex);
}
......@@ -210,15 +210,17 @@ static int kk_scene_timer_load(void)
kk_scene_ctx_t *ctx = _kk_scene_get_ctx();
sqlite3_stmt *stmt;
char *sceneId = NULL;
char *sqlCmd = "select * from SceneConditionInfo WHERE type = timeRange";
char *sqlCmd = "select * from SceneConditionInfo WHERE type = 'timeRange'";
sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL);
while(sqlite3_step(stmt) == SQLITE_ROW){
sceneId = sqlite3_column_text(stmt, DB_SCENECONDITION_SCENEID);
if(kk_scene_check_able(sceneId) == SUCCESS_RETURN){
int starttime = sqlite3_column_int(stmt, DB_SCENECONDITION_STARTTIME);
int endtime = sqlite3_column_int(stmt, DB_SCENECONDITION_ENDTIME);
int repeatday = sqlite3_column_int(stmt, DB_SCENECONDITION_REPEATDAY);
kk_scene_push_timer_info(starttime,endtime,repeatday);
kk_scene_push_timer_info(starttime,endtime,repeatday,sceneId);
}
}
sqlite3_finalize(stmt);
......@@ -231,6 +233,7 @@ kk_tsl_t * kk_scene_shadow(void)
}
static kk_scene_action_delay_t *p_delay_action_list = NULL;
static kk_scene_timer_list_t *p_scene_timer_list = NULL;
extern uint64_t s_start_time;
void *kk_scene_yield(void *args)
{
......@@ -241,10 +244,20 @@ void *kk_scene_yield(void *args)
kk_scene_ctx_t *ctx = _kk_scene_get_ctx();
while (1) {
current_time = HAL_GetTime();
/****系统起来15s后开始定时处理****/
if((HAL_UptimeMs() - s_start_time) <= 15000){
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
sleep(1);
continue;
}
_kk_scene_lock();
actionDelayInfo = p_delay_action_list;
while(actionDelayInfo){
while(actionDelayInfo){
INFO_PRINT("[%s][%d] current_time:%d\n",__FUNCTION__,__LINE__,current_time);
INFO_PRINT("[%s][%d] actionDelayInfo->starttime:%d\n",__FUNCTION__,__LINE__,actionDelayInfo->starttime);
if(current_time >= actionDelayInfo->starttime){
INFO_PRINT("[%s][%d]\n",__FUNCTION__,__LINE__);
kk_scene_send_action_msg(actionDelayInfo->action);
if(actionDelayInfo == p_delay_action_list){
pTemp = p_delay_action_list;
......@@ -263,9 +276,15 @@ void *kk_scene_yield(void *args)
actionDelayInfo = actionDelayInfo->next;
}
}
_kk_scene_unlock();
_kk_scene_lock();
/*******定时模式**********/
scene_timer_list = p_scene_timer_list;
while(scene_timer_list){
while(scene_timer_list){
INFO_PRINT("--------scene_timer_list->starttime:%d\n",scene_timer_list->starttime);
INFO_PRINT("--------current_time %d\n",current_time);
INFO_PRINT("--------scene_timer_list->end:%d\n",scene_timer_list->endtime);
if(current_time >= scene_timer_list->starttime && \
current_time <= scene_timer_list->endtime){
kk_scene_execute_action(scene_timer_list->sceneId);
......@@ -273,7 +292,6 @@ void *kk_scene_yield(void *args)
}
scene_timer_list = scene_timer_list->next;
}
_kk_scene_unlock();
sleep(1);
}
......@@ -286,8 +304,6 @@ int kk_scene_init(void)
int res = 0;
kk_scene_ctx_t *ctx = _kk_scene_get_ctx();
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
/* Create Mutex */
ctx->mutex = HAL_MutexCreate();
if (ctx->mutex == NULL) {
......@@ -313,6 +329,28 @@ int kk_scene_init(void)
return SUCCESS_RETURN;
}
int kk_scene_update_scene_enable(int enable,const char *sceneId)
{
char *sqlCmd = NULL;
int len =0;
int rc = 0;
char *zErrMsg = 0;
kk_scene_ctx_t *ctx = _kk_scene_get_ctx();
//_kk_subDb_lock();
sqlCmd = sqlite3_mprintf("UPDATE SceneInfo SET enable=%d WHERE sceneId= '%s'",enable,sceneId);
INFO_PRINT("kk_scene_update_scene_enable,sqlCmd:%s\n",sqlCmd);
rc = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
if( rc != SQLITE_OK ){
ERROR_PRINT("SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}else{
//INFO_PRINT("Table updata data successfully\n");
}
sqlite3_free(sqlCmd);
//_kk_subDb_unlock();
return SUCCESS_RETURN;
}
static int kk_scene_insert_scene_info(const char* name,const char* sceneType,const char* enable,const char* sceneId)
{
......@@ -605,6 +643,9 @@ void kk_scene_remove_timer_info(char *sceneId)
int kk_scene_push_timer_info(int starttime,int endtime,int repeatday,char *sceneId)
{
kk_scene_timer_list_t *ptr = NULL,*ptemp = NULL;
if(sceneId == NULL){
return INVALID_PARAMETER;
}
_kk_scene_lock();
ptemp = ptr = p_scene_timer_list;
while(ptr){
......@@ -629,8 +670,8 @@ int kk_scene_push_timer_info(int starttime,int endtime,int repeatday,char *scene
_kk_scene_unlock();
INFO_PRINT("kk_scene_push_timer_info called!!!\n");
return SUCCESS_RETURN;
}
int kk_scene_parse_scene_condition(const cJSON* str,const char *sceneId)
{
char weekflag = 0;
......@@ -760,7 +801,7 @@ int kk_scene_parse_addscene(const cJSON* args,char *sceneId,int isUpdate)
return SUCCESS_RETURN;
}
static void kk_scenec_delete(char* sceneId)
static void kk_scene_delete(char* sceneId)
{
kk_scene_remove_timer_info(sceneId);
kk_scene_delete_scene_info(sceneId);
......@@ -775,7 +816,7 @@ int kk_scene_parse_updatescene(const cJSON* arg,char *sceneId)
ERROR_PRINT("[%d]kk_scene_parse_addscene fail!!!\n",__LINE__);
return INVALID_PARAMETER;
}
kk_scenec_delete(sceneId);
kk_scene_delete(sceneId);
return kk_scene_parse_addscene(arg,sceneId,1);
}
int kk_scene_parse_deletescene(char *sceneId)
......@@ -785,7 +826,7 @@ int kk_scene_parse_deletescene(char *sceneId)
ERROR_PRINT("[%d]kk_scene_parse_deletescene fail!!!\n",__LINE__);
return INVALID_PARAMETER;
}
kk_scenec_delete(sceneId);
kk_scene_delete(sceneId);
return SUCCESS_RETURN;
}
......@@ -820,7 +861,8 @@ static int kk_scene_update_starttime(kk_scene_timer_list_t *pInfo,int starttime,
/*only support once*/
if(pInfo->repeatday == 0){
pInfo->starttime = 0;
pInfo->endtime = 0;
pInfo->endtime = 0;
kk_scene_update_scene_enable(0,pInfo->sceneId);
return SUCCESS_RETURN;
}else{
curWeek = kk_scene_date_to_week(current);
......@@ -872,11 +914,11 @@ int kk_scene_check_condition(const char *sceneId)
while(sqlite3_step(stmt) == SQLITE_ROW){
repeatday = sqlite3_column_int(stmt, DB_SCENECONDITION_REPEATDAY);
startTime = sqlite3_column_int(stmt, DB_SCENECONDITION_STARTTIME);
endTime = sqlite3_column_int(stmt, DB_SCENECONDITION_STARTTIME);
endTime = sqlite3_column_int(stmt, DB_SCENECONDITION_ENDTIME);
duration = endTime - startTime;
startTime_m = kk_scene_creat_new_starttime(startTime,current);
/********check today is one of repeatday**************/
if((repeatday > 0) && (repeatday &(curWeek<<1))){
if((repeatday > 0) && (repeatday &(1<<(curWeek-1)))){
if(current >= startTime_m && current <= (startTime_m + duration)){
res = SUCCESS_RETURN;
}
......@@ -995,8 +1037,13 @@ static void kk_scene_send_action_msg(kk_scene_action_info_t *pInfo)
if(pInfo == NULL){
return;
}
cJSON *root=cJSON_CreateObject();
cJSON *root=cJSON_CreateObject();
valueType = kk_dm_get_property_type(pInfo->deviceCode,pInfo->propertyName);
if(valueType < 0){
ERROR_PRINT("[%d]kk_scene_send_action_msg valueType < 0!!!\n",__LINE__);
return 0;
}
switch(valueType){
case KK_TSL_DATA_TYPE_INT:
case KK_TSL_DATA_TYPE_ENUM:
......@@ -1026,7 +1073,8 @@ static void kk_scene_send_action_msg(kk_scene_action_info_t *pInfo)
static int kk_scene_start_action(const char *deviceCode,const char *propertyName,const char *valueS,int delay)
{
int res = 0;
INFO_PRINT("[%s][%d]kk_scene_start_action called\n",__FUNCTION__,__LINE__);
dm_mgr_dev_node_t *node = NULL;
kk_scene_action_info_t *actionInfo = NULL;
kk_scene_ctx_t *ctx = _kk_scene_get_ctx();
......@@ -1043,16 +1091,17 @@ static int kk_scene_start_action(const char *deviceCode,const char *propertyName
if(actionInfo == NULL) {
return MEMORY_NOT_ENOUGH;
}
memset(actionInfo,0x0,sizeof(kk_scene_action_info_t));
memcpy(actionInfo->deviceCode,deviceCode,strlen(deviceCode));
memcpy(actionInfo->productCode,node->productCode,strlen(node->productCode));
memcpy(actionInfo->fatherdeviceCode,node->fatherDeviceCode,strlen(node->fatherDeviceCode));
memcpy(actionInfo->propertyName,propertyName,strlen(propertyName));
memcpy(actionInfo->propertyValue,valueS,strlen(valueS));
if(delay == 0){
INFO_PRINT("[%s][%d]\n",__FUNCTION__,__LINE__);
kk_scene_send_action_msg(actionInfo);
}else{
_kk_scene_lock();
INFO_PRINT("[%s][%d]delay:%d\n",__FUNCTION__,__LINE__,delay);
kk_scene_action_delay_t *ptr = NULL,*ptemp = NULL;
ptemp = ptr = p_delay_action_list;
while(ptr){
......@@ -1061,9 +1110,9 @@ static int kk_scene_start_action(const char *deviceCode,const char *propertyName
}
ptr = malloc(sizeof(kk_scene_action_delay_t));
if(ptr == NULL) {
_kk_scene_unlock();
return MEMORY_NOT_ENOUGH;
}
memset(ptr,0x0,sizeof(kk_scene_action_delay_t));
ptr->starttime = HAL_GetTime()+delay;
ptr->action = actionInfo;
if(p_delay_action_list == NULL){
......@@ -1071,7 +1120,6 @@ static int kk_scene_start_action(const char *deviceCode,const char *propertyName
}else{
ptemp->next = ptr;
}
_kk_scene_unlock();
}
return SUCCESS_RETURN;
......@@ -1132,7 +1180,9 @@ int kk_scene_query_trigger_info(const char *deviceCode,cJSON *param)
INFO_PRINT("[%d]kk_scene_check_condition enter!!!\n",__LINE__);
res = kk_scene_check_condition(sceneId);
if(res == SUCCESS_RETURN){
_kk_scene_lock();
res = kk_scene_execute_action(sceneId);
_kk_scene_unlock();
}
}
}
......@@ -1148,3 +1198,25 @@ int kk_scene_iftt_check(const char*deviceCode,cJSON *param)
return kk_scene_query_trigger_info(deviceCode,param);
}
int kk_scene_execute_scene(const char *sceneId)
{
int res = 0;
int sceneType = 0;
int isEnable = 0;
if(sceneId == NULL){
return INVALID_PARAMETER;
}
res = kk_scene_get_scene_info(sceneId,&sceneType,&isEnable);
if(res == SUCCESS_RETURN && sceneType == DB_SCENETYPE_SCENE && isEnable){
res = kk_scene_check_condition(sceneId);
if(res == SUCCESS_RETURN){
_kk_scene_lock();
res = kk_scene_execute_action(sceneId);
_kk_scene_unlock();
}
}
INFO_PRINT("[%d]kk_scene_execute_scene called!!!\n",__LINE__);
return res;
}
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