Commit e55be185 authored by 尹佳钦's avatar 尹佳钦

群组新增与网关的交互

parent a7365000
......@@ -28,7 +28,7 @@
#include "kk_hal.h"
#include "kk_history_db.h"
#include "kk_group_db.h"
#include "kk_group_handle.h"
#define IOTX_LINKKIT_KEY_ID "id"
#define IOTX_LINKKIT_KEY_CODE "code"
......@@ -2003,379 +2003,6 @@ static int _kk_cancel_warning_handle(char *deviceCode){
/************************************************************
*功能描述:添加群组REPLY
*************************************************************/
static int kk_service_addGroup_reply(cJSON *root,cJSON *msgId,const char *groupId)
{
if(root == NULL || msgId == NULL || groupId == NULL){
return INVALID_PARAMETER;
}
cJSON *deviceCode = cJSON_GetObjectItem(root, MSG_DEVICE_CODE_STR);
if(deviceCode == NULL){
return FAIL_RETURN;
}
cJSON *productCode = cJSON_GetObjectItem(root, MSG_PRODUCT_CODE_STR);
if(productCode == NULL){
return FAIL_RETURN;
}
cJSON *info = cJSON_CreateObject();
cJSON_AddStringToObject(info, MSG_TYPE_STR, KK_THING_SERVICE_ADDGROUP_REPLY);
cJSON_AddStringToObject(info, MSG_DEVICE_CODE_STR, deviceCode->valuestring);
cJSON_AddStringToObject(info, MSG_PRODUCT_CODE_STR, productCode->valuestring);
char *infff=cJSON_Print(info);
cJSON *payload = cJSON_CreateObject();
cJSON_AddStringToObject(payload, "desc", "success");
cJSON_AddStringToObject(payload, "version", "1.0");
cJSON_AddStringToObject(payload, "code", "0");
cJSON_AddStringToObject(payload, "msgId", msgId->valuestring);
cJSON_AddStringToObject(payload, "method", "thing.service.addGroup_reply");
cJSON *paramInfo = cJSON_CreateObject();
cJSON_AddStringToObject(paramInfo, "groupId", groupId);
cJSON_AddItemToObject(payload, "params", paramInfo);
char *payload11=cJSON_Print(payload);
kk_sendData2app(infff,payload11,0);
free(payload11);
free(infff);
cJSON_Delete(payload);
cJSON_Delete(info);
return SUCCESS_RETURN;
}
/************************************************************
*功能描述:添加群组
*************************************************************/
static int kk_service_addGroup_handle(cJSON *root,cJSON *param,cJSON *msgId)
{
cJSON *groupName = NULL;
cJSON *ccuRoomId = NULL;
cJSON *didList = NULL;
cJSON *didListItem = NULL,*epNum = NULL,*deviceCode = NULL;
int i,didListCnt = 0;
char groupId[64] = {0};
HAL_GetTime_s(groupId);
groupName = cJSON_GetObjectItem(param, "groupName");
ccuRoomId = cJSON_GetObjectItem(param, "ccuRoomId");
didList = cJSON_GetObjectItem(param, "didList");
didListCnt = cJSON_GetArraySize(didList);
for(i=0;i<didListCnt;i++){
didListItem = cJSON_GetArrayItem(didList,i);
epNum = cJSON_GetObjectItem(didListItem, "epNum");
deviceCode = cJSON_GetObjectItem(didListItem, "deviceCode");
char *ptr = strrchr(deviceCode->valuestring,'_');
char *deviceCodeStr;
if(ptr==NULL){
deviceCodeStr = deviceCode->valuestring;
}else{
deviceCodeStr=ptr+1;
}
INFO_PRINT("epNum = %s,deviceCode = %s\n",epNum->valuestring,deviceCodeStr);
char parent[64];
memset(parent,0,sizeof(parent));
if(kk_subDev_find_fatherDeviceCode(deviceCodeStr,parent)==SUCCESS_RETURN){
INFO_PRINT("parent1 = %s\n",parent);
kk_group_db_insert(groupId,groupName->valuestring,ccuRoomId->valuestring,epNum->valuestring,deviceCodeStr,parent);
}else{
INFO_PRINT("not find parent.\n");
}
}
return kk_service_addGroup_reply(root,msgId,groupId);
}
/************************************************************
*功能描述:更新群组REPLY
*************************************************************/
static int kk_service_updateGroup_reply(cJSON *root,cJSON *msgId)
{
if(root == NULL || msgId == NULL){
return INVALID_PARAMETER;
}
cJSON *deviceCode = cJSON_GetObjectItem(root, MSG_DEVICE_CODE_STR);
if(deviceCode == NULL){
return FAIL_RETURN;
}
cJSON *productCode = cJSON_GetObjectItem(root, MSG_PRODUCT_CODE_STR);
if(productCode == NULL){
return FAIL_RETURN;
}
cJSON *info = cJSON_CreateObject();
cJSON_AddStringToObject(info, MSG_TYPE_STR, KK_THING_SERVICE_UPDATEGROUP_REPLY);
cJSON_AddStringToObject(info, MSG_DEVICE_CODE_STR, deviceCode->valuestring);
cJSON_AddStringToObject(info, MSG_PRODUCT_CODE_STR, productCode->valuestring);
char *infff=cJSON_Print(info);
cJSON *payload = cJSON_CreateObject();
cJSON_AddStringToObject(payload, "desc", "success");
cJSON_AddStringToObject(payload, "version", "1.0");
cJSON_AddStringToObject(payload, "code", "0");
cJSON_AddStringToObject(payload, "msgId", msgId->valuestring);
cJSON_AddStringToObject(payload, "method", "thing.service.updateGroup_reply");
char *payload11=cJSON_Print(payload);
kk_sendData2app(infff,payload11,0);
free(payload11);
free(infff);
cJSON_Delete(payload);
cJSON_Delete(info);
return SUCCESS_RETURN;
}
/************************************************************
*功能描述:更新群组
*************************************************************/
static int kk_service_updateGroup_handle(cJSON *root,cJSON *param,cJSON *msgId)
{
cJSON *groupName = NULL;
cJSON *ccuRoomId = NULL;
cJSON *didList = NULL;
cJSON *didListItem = NULL,*epNum = NULL,*deviceCode = NULL;
int i,didListCnt = 0;
cJSON *groupId = NULL;
groupId = cJSON_GetObjectItem(param, "groupId");
groupName = cJSON_GetObjectItem(param, "groupName");
ccuRoomId = cJSON_GetObjectItem(param, "ccuRoomId");
didList = cJSON_GetObjectItem(param, "didList");
didListCnt = cJSON_GetArraySize(didList);
kk_group_db_delete(groupId->valuestring);
for(i=0;i<didListCnt;i++){
didListItem = cJSON_GetArrayItem(didList,i);
epNum = cJSON_GetObjectItem(didListItem, "epNum");
deviceCode = cJSON_GetObjectItem(didListItem, "deviceCode");
char *ptr = strrchr(deviceCode->valuestring,'_');
char *deviceCodeStr;
if(ptr==NULL){
deviceCodeStr = deviceCode->valuestring;
}else{
deviceCodeStr=ptr+1;
}
char parent[64];
memset(parent,0,sizeof(parent));
if(kk_subDev_find_fatherDeviceCode(deviceCodeStr,parent)==SUCCESS_RETURN){
INFO_PRINT("parent = %s\n",parent);
kk_group_db_insert(groupId->valuestring,groupName->valuestring,ccuRoomId->valuestring,epNum->valuestring,deviceCodeStr,parent);
}else{
INFO_PRINT("not find parent.\n");
}
}
return kk_service_updateGroup_reply(root,msgId);
}
/************************************************************
*功能描述:删除群组REPLY
*************************************************************/
static int kk_service_deleteGroup_reply(cJSON *root,cJSON *msgId)
{
if(root == NULL || msgId == NULL){
return INVALID_PARAMETER;
}
cJSON *deviceCode = cJSON_GetObjectItem(root, MSG_DEVICE_CODE_STR);
if(deviceCode == NULL){
return FAIL_RETURN;
}
cJSON *productCode = cJSON_GetObjectItem(root, MSG_PRODUCT_CODE_STR);
if(productCode == NULL){
return FAIL_RETURN;
}
cJSON *info = cJSON_CreateObject();
cJSON_AddStringToObject(info, MSG_TYPE_STR, KK_THING_SERVICE_DELETEGROUP_REPLY);
cJSON_AddStringToObject(info, MSG_DEVICE_CODE_STR, deviceCode->valuestring);
cJSON_AddStringToObject(info, MSG_PRODUCT_CODE_STR, productCode->valuestring);
char *infff=cJSON_Print(info);
cJSON *payload = cJSON_CreateObject();
cJSON_AddStringToObject(payload, "desc", "success");
cJSON_AddStringToObject(payload, "version", "1.0");
cJSON_AddStringToObject(payload, "code", "0");
cJSON_AddStringToObject(payload, "msgId", msgId->valuestring);
cJSON_AddStringToObject(payload, "method", "thing.service.deleteGroup_reply");
char *payload11=cJSON_Print(payload);
kk_sendData2app(infff,payload11,0);
free(payload11);
free(infff);
cJSON_Delete(payload);
cJSON_Delete(info);
return SUCCESS_RETURN;
}
/************************************************************
*功能描述:删除群组
*************************************************************/
static int kk_service_deleteGroup_handle(cJSON *root,cJSON *param,cJSON *msgId)
{
cJSON *groupId = NULL;
groupId = cJSON_GetObjectItem(param, "groupId");
kk_group_db_delete(groupId->valuestring);
return kk_service_deleteGroup_reply(root,msgId);
}
/************************************************************
*功能描述:添加群组
*************************************************************/
static int kk_service_queryGroup_reply(cJSON *info_root,cJSON *msgId)
{
if(info_root == NULL || msgId == NULL){
return INVALID_PARAMETER;
}
cJSON *deviceCode = cJSON_GetObjectItem(info_root, MSG_DEVICE_CODE_STR);
if(deviceCode == NULL){
return FAIL_RETURN;
}
cJSON *productCode = cJSON_GetObjectItem(info_root, MSG_PRODUCT_CODE_STR);
if(productCode == NULL){
return FAIL_RETURN;
}
cJSON *info = cJSON_CreateObject();
cJSON_AddStringToObject(info, MSG_TYPE_STR, KK_THING_SERVICE_QUERYGROUP_REPLY);
cJSON_AddStringToObject(info, MSG_DEVICE_CODE_STR, deviceCode->valuestring);
cJSON_AddStringToObject(info, MSG_PRODUCT_CODE_STR, productCode->valuestring);
char *infff=cJSON_Print(info);
cJSON *payload = cJSON_CreateObject();
cJSON_AddStringToObject(payload, "desc", "success");
cJSON_AddStringToObject(payload, "version", "1.0");
cJSON_AddStringToObject(payload, "code", "0");
cJSON_AddStringToObject(payload, "msgId", msgId->valuestring);
cJSON_AddStringToObject(payload, "method", "thing.service.queryGroup_reply");
cJSON *params = kk_group_db_query(0);
cJSON_AddItemToObject(payload, "params", params);
char *payload11=cJSON_Print(payload);
kk_sendData2app(infff,payload11,0);
free(payload11);
free(infff);
cJSON_Delete(payload);
cJSON_Delete(info);
return SUCCESS_RETURN;
}
static int kk_service_queryGroup_handle(cJSON *root,cJSON *msgId)
{
return kk_service_queryGroup_reply(root,msgId);
}
/************************************************************
*功能描述:执行群组REPLY
*************************************************************/
static int kk_service_executeGroup_reply(cJSON *root,cJSON *msgId)
{
if(root == NULL || msgId == NULL){
return INVALID_PARAMETER;
}
cJSON *deviceCode = cJSON_GetObjectItem(root, MSG_DEVICE_CODE_STR);
if(deviceCode == NULL){
return FAIL_RETURN;
}
cJSON *productCode = cJSON_GetObjectItem(root, MSG_PRODUCT_CODE_STR);
if(productCode == NULL){
return FAIL_RETURN;
}
cJSON *info = cJSON_CreateObject();
cJSON_AddStringToObject(info, MSG_TYPE_STR, KK_THING_SERVICE_EXCUTEGROUP_REPLY);
cJSON_AddStringToObject(info, MSG_DEVICE_CODE_STR, deviceCode->valuestring);
cJSON_AddStringToObject(info, MSG_PRODUCT_CODE_STR, productCode->valuestring);
char *infff=cJSON_Print(info);
cJSON *payload = cJSON_CreateObject();
cJSON_AddStringToObject(payload, "desc", "success");
cJSON_AddStringToObject(payload, "version", "1.0");
cJSON_AddStringToObject(payload, "code", "0");
cJSON_AddStringToObject(payload, "msgId", msgId->valuestring);
cJSON_AddStringToObject(payload, "method", "thing.service.excuteGroup_reply");
char *payload11=cJSON_Print(payload);
kk_sendData2app(infff,payload11,0);
free(payload11);
free(infff);
cJSON_Delete(payload);
cJSON_Delete(info);
return SUCCESS_RETURN;
}
static int kk_service_executeGroup_send_to_gw(const char *groupId,cJSON *actions,const char *productCode,const char *deviceCode,char *fatherDeviceCode)
{
INFO_PRINT("kk_service_executeGroup_send_to_gw \n");
cJSON *root = cJSON_CreateObject();
cJSON *info = cJSON_CreateObject();
cJSON *payload = cJSON_CreateObject();
cJSON_AddStringToObject(info,"msgtype","/thing/service/excuteGroup");
cJSON_AddStringToObject(info,"productCode",productCode);
cJSON_AddStringToObject(info,"deviceCode",deviceCode);
char msgId[MSG_MAX_LEN] = {0};
iotx_report_id(msgId);
cJSON_AddStringToObject(payload,"msgId",msgId);
cJSON_AddStringToObject(payload,"version","1.0");
char tm[33] = {0};
snprintf(tm,sizeof(tm),"%d",time(NULL));
cJSON_AddStringToObject(payload,"time",tm);
cJSON_AddStringToObject(payload,"method","thing.service.excuteGroup");
cJSON *params = cJSON_CreateObject();
cJSON_AddStringToObject(params,"groupId",groupId);
cJSON_AddItemToObject(params,"actions",actions);
cJSON_AddItemToObject(payload,"params",params);
cJSON_AddItemToObject(root,"info",info);
cJSON_AddItemToObject(root,"payload",payload);
char *data = cJSON_Print(root);
kk_sendData2gw(data,strlen(data), fatherDeviceCode);
cJSON_Delete(root);
free(data);
return 0;
}
/************************************************************
*功能描述:执行群组
*************************************************************/
static int kk_service_executeGroup_handle(cJSON *info_root,cJSON *param,cJSON *msgId)
{
cJSON *groupId = NULL;
cJSON *controlType = NULL;
cJSON *propertyValue = NULL;
INFO_PRINT("kk_service_executeGroup_handle \n");
groupId = cJSON_GetObjectItem(param, "groupId");
controlType = cJSON_GetObjectItem(param, "controlType");
propertyValue = cJSON_GetObjectItem(param, "propertyValue");
int type;
if(controlType->type==cJSON_String){
type = atoi(controlType->valuestring);
}else{
type = controlType->valueint;
}
char parent[64];
memset(parent,0,sizeof(parent));
cJSON *actions = kk_group_device_db_exe_act(groupId->valuestring,type,propertyValue->valueint,parent);
dm_mgr_dev_node_t *node = NULL;
if(strlen(parent)!=0&&dm_mgr_get_device_by_devicecode(parent, &node)==SUCCESS_RETURN){
kk_service_executeGroup_send_to_gw(groupId->valuestring,actions,node->productCode,node->deviceCode,parent);
}
return kk_service_executeGroup_reply(info_root,msgId);
}
......
......@@ -45,6 +45,7 @@ int kk_get_screenDev_info(ScreenDevInfo_t *devInfo,char*serialId);
int kk_subDev_getDeviceCodes_byProductType(DeviceCode_t *list,char *type);
int kk_indoor_air_action_add(char *propertyValue,char* deviceCode,int delay,char*gwDeviceCode,char *sceneId,char *type,int epnum);
int kk_subDev_update_fatherDeviceCode(char *fatherDeviceCode,const char *deviceCode);
int kk_subDev_find_fatherDeviceCode(const char *deviceCode,char *fatherDeviceCode);
enum{
DB_SUB_IDX = 0,
......
#include <stdio.h>
#include "kk_tsl_api.h"
#include "sqlite3.h"
#include "kk_log.h"
#include "kk_hal.h"
#include "kk_group_db.h"
#include "kk_dm_mng.h"
#include "cJSON.h"
/*************************************************************
全局变量定义
*************************************************************/
extern sqlite3 *g_kk_pDb;
/*************************************************************
函数实现
*************************************************************/
typedef struct {
void *mutex;
sqlite3 *pDb;
} kk_group_ctx_t;
static kk_group_ctx_t s_kk_group_ctx = {NULL};
static kk_group_ctx_t *_kk_group_get_ctx(void)
{
return &s_kk_group_ctx;
}
static void _kk_group_lock(void)
{
kk_group_ctx_t *ctx = _kk_group_get_ctx();
if (ctx->mutex) {
HAL_MutexLock(ctx->mutex);
}
}
static void _kk_group_unlock(void)
{
kk_group_ctx_t *ctx = _kk_group_get_ctx();
if (ctx->mutex) {
HAL_MutexUnlock(ctx->mutex);
}
}
static int kk_group_device_db_init(void)
{
kk_group_ctx_t *ctx = _kk_group_get_ctx();
char *pcErr;
ctx->pDb = g_kk_pDb;
INFO_PRINT("kk_group_db_init db Database opened\n");
/* Create Mutex */
ctx->mutex = HAL_MutexCreate();
if (ctx->mutex == NULL) {
return FAIL_RETURN;
}
_kk_group_lock();
const char *pPanelBindMotor = "CREATE TABLE IF NOT EXISTS GroupDevice( \
groupId varchar(255), \
epNum varchar(255), \
deviceCode varchar(255), \
parentDeviceCode varchar(255))";
if (sqlite3_exec(ctx->pDb, pPanelBindMotor, NULL, NULL, &pcErr) != SQLITE_OK)
{
ERROR_PRINT("Error creating table (%s)\n", pcErr);
sqlite3_free(pcErr);
//eUtils_LockUnlock(&sLock);
_kk_group_unlock();
return FAIL_RETURN;
}
_kk_group_unlock();
return SUCCESS_RETURN;
}
static int kk_group_info_db_init(void)
{
kk_group_ctx_t *ctx = _kk_group_get_ctx();
char *pcErr;
ctx->pDb = g_kk_pDb;
/* Create Mutex */
ctx->mutex = HAL_MutexCreate();
if (ctx->mutex == NULL) {
return FAIL_RETURN;
}
_kk_group_lock();
const char *pPanelBindMotor = "CREATE TABLE IF NOT EXISTS GroupInfo( \
groupId varchar(255), \
groupName varchar(255), \
ccuRoomId varchar(255))";
if (sqlite3_exec(ctx->pDb, pPanelBindMotor, NULL, NULL, &pcErr) != SQLITE_OK)
{
ERROR_PRINT("Error creating table (%s)\n", pcErr);
sqlite3_free(pcErr);
//eUtils_LockUnlock(&sLock);
_kk_group_unlock();
return FAIL_RETURN;
}
_kk_group_unlock();
return SUCCESS_RETURN;
}
int kk_group_db_init(void)
{
INFO_PRINT("kk_group_db_init db Database opened\n");
kk_group_info_db_init();
kk_group_device_db_init();
}
static int kk_group_info_db_check(const char* groupId)
{
int isExist = 0;
sqlite3_stmt *stmt;
const unsigned char *groupIdStr = NULL;
kk_group_ctx_t *ctx = _kk_group_get_ctx();
const char *searchCmd = "select * from GroupInfo;";
_kk_group_lock();
sqlite3_prepare_v2(ctx->pDb, searchCmd, strlen(searchCmd), &stmt, NULL);
while(sqlite3_step(stmt) == SQLITE_ROW){
groupIdStr = sqlite3_column_text(stmt, GROUP_DEVICE_DB_ID);
if(!strcmp(groupId,(const char *)groupIdStr))
{
isExist = 1;
break;
}
}
sqlite3_finalize(stmt);
_kk_group_unlock();
return isExist;
}
static int kk_group_device_db_check(const char* groupId,const char* epNum,const char* deviceCode)
{
int isExist = 0;
sqlite3_stmt *stmt;
const unsigned char *groupIdStr = NULL;
const unsigned char *epNumStr = NULL;
const unsigned char *deviceCodeStr = NULL;
kk_group_ctx_t *ctx = _kk_group_get_ctx();
const char *searchCmd = "select * from GroupDevice;";
_kk_group_lock();
sqlite3_prepare_v2(ctx->pDb, searchCmd, strlen(searchCmd), &stmt, NULL);
while(sqlite3_step(stmt) == SQLITE_ROW){
groupIdStr = sqlite3_column_text(stmt, GROUP_DEVICE_DB_ID);
epNumStr = sqlite3_column_text(stmt, GROUP_DEVICE_DB_EPNUM);
deviceCodeStr = sqlite3_column_text(stmt, GROUP_DEVICE_DB_DEVICECODE);
if(!strcmp(groupId,(const char *)groupIdStr) &&
!strcmp(epNum,(const char *)epNumStr) &&
!strcmp(deviceCode,(const char *)deviceCodeStr))
{
isExist = 1;
break;
}
}
sqlite3_finalize(stmt);
_kk_group_unlock();
return isExist;
}
static int kk_group_info_db_insert(const char* groupId,const char* groupName,const char* ccuRoomId)
{
int res = 0;
kk_group_ctx_t *ctx = _kk_group_get_ctx();
char *sqlCmd = NULL;
char *zErrMsg = 0;
sqlite3_stmt *stmt;
time_t rtime = 0;
int count = 0;
const char *insertCmd = "insert into GroupInfo (groupId, groupName,ccuRoomId) \
values ('%s','%s','%s');";
if(kk_group_info_db_check(groupId)){
return SUCCESS_RETURN;
}
INFO_PRINT("[GroupInfo INSERT] groupId:%s,groupName:%s,ccuRoomId:%s\n",groupId,groupName,ccuRoomId);
_kk_group_lock();
sqlCmd = sqlite3_mprintf(insertCmd,groupId,groupName,ccuRoomId);
res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
if( res != SQLITE_OK ){
ERROR_PRINT("SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
sqlite3_free(sqlCmd);
_kk_group_unlock();
return FAIL_RETURN;
}
sqlite3_free(sqlCmd);
_kk_group_unlock();
return SUCCESS_RETURN;
}
static int kk_group_device_db_insert(const char* groupId,const char* epNum,const char* deviceCode,const char* parent)
{
int res = 0;
kk_group_ctx_t *ctx = _kk_group_get_ctx();
char *sqlCmd = NULL;
char *zErrMsg = 0;
sqlite3_stmt *stmt;
time_t rtime = 0;
int count = 0;
const char *insertCmd = "insert into GroupDevice (groupId,epNum,deviceCode,parentDeviceCode) \
values ('%s','%s','%s','%s');";
if(kk_group_device_db_check(groupId,epNum,deviceCode)){
INFO_PRINT("already exist.--->groupId:%s,epNum:%s,deviceCode:%s,parent:%s\n",groupId,epNum,deviceCode,parent);
return SUCCESS_RETURN;
}
INFO_PRINT("[GroupDevice INSERT] groupId:%s,epNum:%s,deviceCode:%s,parent:%s\n",groupId,epNum,deviceCode,parent);
_kk_group_lock();
sqlCmd = sqlite3_mprintf(insertCmd,groupId,epNum,deviceCode,parent);
res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
if( res != SQLITE_OK ){
ERROR_PRINT("SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
sqlite3_free(sqlCmd);
_kk_group_unlock();
return FAIL_RETURN;
}
sqlite3_free(sqlCmd);
_kk_group_unlock();
return SUCCESS_RETURN;
}
int kk_group_db_insert(const char* groupId,const char* groupName,const char* ccuRoomId,const char* epNum,const char* deviceCode,const char* parent)
{
kk_group_info_db_insert(groupId,groupName,ccuRoomId);
return kk_group_device_db_insert(groupId,epNum,deviceCode,parent);
}
static int kk_group_info_db_delete(const char* groupId)
{
const char *deleteCmd = "delete from GroupInfo where groupId = '%s' ;";
char *sqlCmd = NULL;
int rc = 0;
char *zErrMsg = 0;
kk_group_ctx_t *ctx = _kk_group_get_ctx();
_kk_group_lock();
sqlCmd = sqlite3_mprintf(deleteCmd,groupId);
INFO_PRINT("Table delete data 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 delete data successfully\n");
}
sqlite3_free(sqlCmd);
_kk_group_unlock();
return SUCCESS_RETURN;
}
static int kk_group_device_db_delete(const char* groupId)
{
const char *deleteCmd = "delete from GroupDevice where groupId = '%s' ;";
char *sqlCmd = NULL;
int rc = 0;
char *zErrMsg = 0;
kk_group_ctx_t *ctx = _kk_group_get_ctx();
_kk_group_lock();
sqlCmd = sqlite3_mprintf(deleteCmd,groupId);
INFO_PRINT("Table delete data 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 delete data successfully\n");
}
sqlite3_free(sqlCmd);
_kk_group_unlock();
return SUCCESS_RETURN;
}
int kk_group_db_delete(const char* groupId)
{
kk_group_info_db_delete(groupId);
kk_group_device_db_delete(groupId);
return SUCCESS_RETURN;
}
static cJSON *kk_group_device_db_query(const char *groupId,int isSync)
{
sqlite3_stmt *stmt;
char *sqlCmd = NULL;
const unsigned char *epnum = NULL;
const unsigned char *deviceCode = NULL;
kk_group_ctx_t *ctx = _kk_group_get_ctx();
cJSON *didListAry = cJSON_CreateArray();
cJSON *didListItem = NULL;
const char *searchCmd = "select * from GroupDevice where groupId = '%s'; ";
sqlCmd = sqlite3_mprintf(searchCmd,groupId);
INFO_PRINT("Table searchCmd:%s\n",sqlCmd);
sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL);
while(sqlite3_step(stmt) == SQLITE_ROW){
epnum = sqlite3_column_text(stmt, GROUP_DEVICE_DB_EPNUM);
deviceCode = sqlite3_column_text(stmt, GROUP_DEVICE_DB_DEVICECODE);
INFO_PRINT("groupId=%s,epnum=%s,deviceCode=%s\n",groupId,epnum,deviceCode);
didListItem = cJSON_CreateObject();
if(isSync!=0){
cJSON_AddNumberToObject(didListItem,"epNum",atoi(epnum));
}else{
cJSON_AddStringToObject(didListItem,"epNum",epnum);
}
cJSON_AddStringToObject(didListItem,"deviceCode",deviceCode);
cJSON_AddItemToArray(didListAry, didListItem);
}
sqlite3_finalize(stmt);
return didListAry;
}
cJSON *kk_group_db_query(int isSync)
{
sqlite3_stmt *stmt;
char *sqlCmd = NULL;
const unsigned char *groupId = NULL;
const unsigned char *groupName = NULL;
const unsigned char *ccuRoomId = NULL;
cJSON *groupListAry = cJSON_CreateArray();
cJSON *groupListItem = NULL;
cJSON *didListAry = NULL;
kk_group_ctx_t *ctx = _kk_group_get_ctx();
const char *searchCmd = "select * from GroupInfo";
_kk_group_lock();
sqlCmd = sqlite3_mprintf(searchCmd);
INFO_PRINT("Table searchCmd:%s\n",sqlCmd);
sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL);
while(sqlite3_step(stmt) == SQLITE_ROW){
groupId = sqlite3_column_text(stmt, GROUP_INFO_DB_ID);
groupName = sqlite3_column_text(stmt, GROUP_INFO_DB_NAME);
ccuRoomId = sqlite3_column_text(stmt, GROUP_INFO_DB_CCUROOMID);
INFO_PRINT("groupId=%s,groupName=%s,ccuRoomId=%s\n",groupId,groupName,ccuRoomId);
didListAry = kk_group_device_db_query(groupId,isSync);
groupListItem = cJSON_CreateObject();
cJSON_AddStringToObject(groupListItem,"groupId",groupId);
cJSON_AddStringToObject(groupListItem,"groupName",groupName);
if(isSync!=0){
cJSON_AddStringToObject(groupListItem,"roomId",ccuRoomId);
}else{
cJSON_AddStringToObject(groupListItem,"ccuRoomId",ccuRoomId);
}
cJSON_AddItemToObject(groupListItem,"didList",didListAry);
cJSON_AddItemToArray(groupListAry, groupListItem);
}
sqlite3_finalize(stmt);
_kk_group_unlock();
return groupListAry;
}
cJSON *kk_group_device_db_exe_act(const char *groupId,int controlType,int propertyValue,char *parent)
{
sqlite3_stmt *stmt;
char *sqlCmd = NULL;
const unsigned char *epnum = NULL;
const unsigned char *deviceCode = NULL;
kk_group_ctx_t *ctx = _kk_group_get_ctx();
cJSON *actionsAry = cJSON_CreateArray();
cJSON *actionItem = NULL;
const char *searchCmd = "select * from GroupDevice where groupId = '%s'; ";
sqlCmd = sqlite3_mprintf(searchCmd,groupId);
INFO_PRINT("Table searchCmd:%s\n",sqlCmd);
sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL);
while(sqlite3_step(stmt) == SQLITE_ROW){
epnum = sqlite3_column_text(stmt, GROUP_DEVICE_DB_EPNUM);
deviceCode = sqlite3_column_text(stmt, GROUP_DEVICE_DB_DEVICECODE);
INFO_PRINT("groupId=%s,epnum=%s,deviceCode=%s\n",groupId,epnum,deviceCode);
if(strlen(parent)==0){
const char *fatherDeviceCode = sqlite3_column_text(stmt, GROUP_DEVICE_DB_PARENT_DEVICECODE);
sprintf(parent,"%s",fatherDeviceCode);
}
actionItem = cJSON_CreateObject();
cJSON_AddStringToObject(actionItem,"type","action/thing/cluster");
cJSON_AddStringToObject(actionItem,"epNum",epnum);
cJSON_AddStringToObject(actionItem,"deviceCode",deviceCode);
char buf[64];
memset(buf,0,sizeof(buf));
if(controlType==1){
snprintf(buf,sizeof(buf),"PowerSwitch_%s",epnum);
}else if(controlType==2){
snprintf(buf,sizeof(buf),"Brightness_%s",epnum);
}else if(controlType==3){
snprintf(buf,sizeof(buf),"ColorTemperature_%s",epnum);
}
cJSON_AddStringToObject(actionItem,"propertyName",buf);
cJSON_AddNumberToObject(actionItem,"propertyValue",propertyValue);
cJSON_AddItemToArray(actionsAry, actionItem);
}
sqlite3_finalize(stmt);
return actionsAry;
}
int kk_group_device_db_find_parent(const char *groupId,char *parent)
{
sqlite3_stmt *stmt;
char *sqlCmd = NULL;
kk_group_ctx_t *ctx = _kk_group_get_ctx();
const char *searchCmd = "select * from GroupDevice where groupId = '%s'; ";
sqlCmd = sqlite3_mprintf(searchCmd,groupId);
INFO_PRINT("Table searchCmd:%s\n",sqlCmd);
sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL);
while(sqlite3_step(stmt) == SQLITE_ROW){
const char *fatherDeviceCode = sqlite3_column_text(stmt, GROUP_DEVICE_DB_PARENT_DEVICECODE);
sprintf(parent,"%s",fatherDeviceCode);
break;
}
sqlite3_finalize(stmt);
return 0;
}
#ifndef __KK_GROUP_DB_H__
#define __KK_GROUP_DB_H__
#include "cJSON.h"
enum{
GROUP_INFO_DB_ID = 0,
GROUP_INFO_DB_NAME,
GROUP_INFO_DB_CCUROOMID,
};
enum{
GROUP_DEVICE_DB_ID = 0,
GROUP_DEVICE_DB_EPNUM,
GROUP_DEVICE_DB_DEVICECODE,
GROUP_DEVICE_DB_PARENT_DEVICECODE,
};
int kk_group_db_init(void);
int kk_group_db_insert(const char* groupId,const char* groupName,const char* ccuRoomId,const char* epNum,const char* deviceCode,const char* parent);
int kk_group_db_delete(const char* groupId);
cJSON *kk_group_db_query(int isSync);
cJSON *kk_group_device_db_exe_act(const char *groupId,int controlType,int propertyValue,char *parent);
int kk_group_device_db_find_parent(const char *groupId,char *parent);
#endif
#include <stdio.h>
#include <pthread.h>
#include "kk_group_db.h"
#include "kk_group_handle.h"
#include "kk_sub_db.h"
#include "kk_tsl_load.h"
#include "kk_tsl_parse.h"
#include "kk_dm_api.h"
#include "kk_dm_msg.h"
#include "kk_property_db.h"
#include "kk_hal.h"
#include "kk_log.h"
#include "kk_linkkit.h"
#include <time.h>
extern void kk_sendData2gw(void* data, int len, char* chalMark);
/************************************************************
*功能描述:向网关发送群组相关的消息
*************************************************************/
static int kk_service_group_msg_send_to_gw(const char *type,cJSON *params,const char *productCode,const char *deviceCode,char *fatherDeviceCode)
{
char buff[128];
cJSON *root = cJSON_CreateObject();
cJSON *info = cJSON_CreateObject();
cJSON *payload = cJSON_CreateObject();
memset(buff,0,sizeof(buff));
sprintf(buff,"/thing/service/%s",type);
cJSON_AddStringToObject(info,"msgtype",buff);
cJSON_AddStringToObject(info,"productCode",productCode);
cJSON_AddStringToObject(info,"deviceCode",deviceCode);
char msgId[MSG_MAX_LEN] = {0};
iotx_report_id(msgId);
cJSON_AddStringToObject(payload,"msgId",msgId);
cJSON_AddStringToObject(payload,"version","1.0");
char tm[33] = {0};
snprintf(tm,sizeof(tm),"%d",time(NULL));
cJSON_AddStringToObject(payload,"time",tm);
memset(buff,0,sizeof(buff));
sprintf(buff,"thing.service.%s",type);
cJSON_AddStringToObject(payload,"method",buff);
cJSON_AddItemToObject(payload,"params",params);
cJSON_AddItemToObject(root,"info",info);
cJSON_AddItemToObject(root,"payload",payload);
char *data = cJSON_Print(root);
kk_sendData2gw(data,strlen(data), fatherDeviceCode);
cJSON_Delete(root);
free(data);
return 0;
}
/************************************************************
*功能描述:向云端发送添加群组REPLY
*************************************************************/
static int kk_service_addGroup_reply(cJSON *root,cJSON *msgId,const char *groupId)
{
if(root == NULL || msgId == NULL || groupId == NULL){
return INVALID_PARAMETER;
}
cJSON *deviceCode = cJSON_GetObjectItem(root, MSG_DEVICE_CODE_STR);
if(deviceCode == NULL){
return FAIL_RETURN;
}
cJSON *productCode = cJSON_GetObjectItem(root, MSG_PRODUCT_CODE_STR);
if(productCode == NULL){
return FAIL_RETURN;
}
cJSON *info = cJSON_CreateObject();
cJSON_AddStringToObject(info, MSG_TYPE_STR, KK_THING_SERVICE_ADDGROUP_REPLY);
cJSON_AddStringToObject(info, MSG_DEVICE_CODE_STR, deviceCode->valuestring);
cJSON_AddStringToObject(info, MSG_PRODUCT_CODE_STR, productCode->valuestring);
char *infff=cJSON_Print(info);
cJSON *payload = cJSON_CreateObject();
cJSON_AddStringToObject(payload, "desc", "success");
cJSON_AddStringToObject(payload, "version", "1.0");
cJSON_AddStringToObject(payload, "code", "0");
cJSON_AddStringToObject(payload, "msgId", msgId->valuestring);
cJSON_AddStringToObject(payload, "method", "thing.service.addGroup_reply");
cJSON *paramInfo = cJSON_CreateObject();
cJSON_AddStringToObject(paramInfo, "groupId", groupId);
cJSON_AddItemToObject(payload, "params", paramInfo);
char *payload11=cJSON_Print(payload);
kk_sendData2app(infff,payload11,0);
free(payload11);
free(infff);
cJSON_Delete(payload);
cJSON_Delete(info);
return SUCCESS_RETURN;
}
/************************************************************
*功能描述:向网关发送添加群组消息
*************************************************************/
static int kk_service_addGroup_send_to_gw(cJSON *param,const char *groupId)
{
cJSON *groupName = cJSON_GetObjectItem(param, "groupName");
cJSON *didList = cJSON_GetObjectItem(param, "didList");
int didListCnt = cJSON_GetArraySize(didList);
int i,pFlag=0;
char parent[64];
for(i=0;i<didListCnt;i++){
cJSON *didListItem = cJSON_GetArrayItem(didList,i);
cJSON *epNum = cJSON_GetObjectItem(didListItem, "epNum");
cJSON *deviceCode = cJSON_GetObjectItem(didListItem, "deviceCode");
char *ptr = strrchr(deviceCode->valuestring,'_');
char *deviceCodeStr;
if(ptr==NULL){
deviceCodeStr = deviceCode->valuestring;
}else{
deviceCodeStr=ptr+1;
}
if(pFlag==0){
memset(parent,0,sizeof(parent));
if(kk_subDev_find_fatherDeviceCode(deviceCodeStr,parent)==SUCCESS_RETURN){
pFlag = 1;
break;
}
}
}
cJSON *params = cJSON_CreateObject();
cJSON_AddStringToObject(params,"groupId",groupId);
cJSON_AddStringToObject(params,"groupName",groupName->valuestring);
char *didlistStr = cJSON_Print(didList);
cJSON *didlistAry = cJSON_Parse(didlistStr);
free(didlistStr);
cJSON_AddItemToObject(params,"didList",didlistAry);
dm_mgr_dev_node_t *node = NULL;
if(strlen(parent)!=0&&dm_mgr_get_device_by_devicecode(parent, &node)==SUCCESS_RETURN){
return kk_service_group_msg_send_to_gw("addGroup",params,node->productCode,node->deviceCode,parent);
}
return -1;
}
/************************************************************
*功能描述:向云端发送更新群组REPLY
*************************************************************/
static int kk_service_updateGroup_reply(cJSON *root,cJSON *msgId)
{
if(root == NULL || msgId == NULL){
return INVALID_PARAMETER;
}
cJSON *deviceCode = cJSON_GetObjectItem(root, MSG_DEVICE_CODE_STR);
if(deviceCode == NULL){
return FAIL_RETURN;
}
cJSON *productCode = cJSON_GetObjectItem(root, MSG_PRODUCT_CODE_STR);
if(productCode == NULL){
return FAIL_RETURN;
}
cJSON *info = cJSON_CreateObject();
cJSON_AddStringToObject(info, MSG_TYPE_STR, KK_THING_SERVICE_UPDATEGROUP_REPLY);
cJSON_AddStringToObject(info, MSG_DEVICE_CODE_STR, deviceCode->valuestring);
cJSON_AddStringToObject(info, MSG_PRODUCT_CODE_STR, productCode->valuestring);
char *infff=cJSON_Print(info);
cJSON *payload = cJSON_CreateObject();
cJSON_AddStringToObject(payload, "desc", "success");
cJSON_AddStringToObject(payload, "version", "1.0");
cJSON_AddStringToObject(payload, "code", "0");
cJSON_AddStringToObject(payload, "msgId", msgId->valuestring);
cJSON_AddStringToObject(payload, "method", "thing.service.updateGroup_reply");
char *payload11=cJSON_Print(payload);
kk_sendData2app(infff,payload11,0);
free(payload11);
free(infff);
cJSON_Delete(payload);
cJSON_Delete(info);
return SUCCESS_RETURN;
}
/************************************************************
*功能描述:向网关发送更新群组消息
*************************************************************/
static int kk_service_updateGroup_send_to_gw(cJSON *param,const char *groupId)
{
cJSON *groupName = cJSON_GetObjectItem(param, "groupName");
cJSON *didList = cJSON_GetObjectItem(param, "didList");
int didListCnt = cJSON_GetArraySize(didList);
int i,pFlag=0;
char parent[64];
for(i=0;i<didListCnt;i++){
cJSON *didListItem = cJSON_GetArrayItem(didList,i);
cJSON *epNum = cJSON_GetObjectItem(didListItem, "epNum");
cJSON *deviceCode = cJSON_GetObjectItem(didListItem, "deviceCode");
char *ptr = strrchr(deviceCode->valuestring,'_');
char *deviceCodeStr;
if(ptr==NULL){
deviceCodeStr = deviceCode->valuestring;
}else{
deviceCodeStr=ptr+1;
}
if(pFlag==0){
memset(parent,0,sizeof(parent));
if(kk_subDev_find_fatherDeviceCode(deviceCodeStr,parent)==SUCCESS_RETURN){
pFlag = 1;
break;
}
}
}
cJSON *params = cJSON_CreateObject();
cJSON_AddStringToObject(params,"groupId",groupId);
cJSON_AddStringToObject(params,"groupName",groupName->valuestring);
char *didlistStr = cJSON_Print(didList);
cJSON *didlistAry = cJSON_Parse(didlistStr);
free(didlistStr);
cJSON_AddItemToObject(params,"didList",didlistAry);
dm_mgr_dev_node_t *node = NULL;
if(strlen(parent)!=0&&dm_mgr_get_device_by_devicecode(parent, &node)==SUCCESS_RETURN){
return kk_service_group_msg_send_to_gw("updateGroup",params,node->productCode,node->deviceCode,parent);
}
return -1;
}
/************************************************************
*功能描述:向云端发送删除群组REPLY
*************************************************************/
static int kk_service_deleteGroup_reply(cJSON *root,cJSON *msgId)
{
if(root == NULL || msgId == NULL){
return INVALID_PARAMETER;
}
cJSON *deviceCode = cJSON_GetObjectItem(root, MSG_DEVICE_CODE_STR);
if(deviceCode == NULL){
return FAIL_RETURN;
}
cJSON *productCode = cJSON_GetObjectItem(root, MSG_PRODUCT_CODE_STR);
if(productCode == NULL){
return FAIL_RETURN;
}
cJSON *info = cJSON_CreateObject();
cJSON_AddStringToObject(info, MSG_TYPE_STR, KK_THING_SERVICE_DELETEGROUP_REPLY);
cJSON_AddStringToObject(info, MSG_DEVICE_CODE_STR, deviceCode->valuestring);
cJSON_AddStringToObject(info, MSG_PRODUCT_CODE_STR, productCode->valuestring);
char *infff=cJSON_Print(info);
cJSON *payload = cJSON_CreateObject();
cJSON_AddStringToObject(payload, "desc", "success");
cJSON_AddStringToObject(payload, "version", "1.0");
cJSON_AddStringToObject(payload, "code", "0");
cJSON_AddStringToObject(payload, "msgId", msgId->valuestring);
cJSON_AddStringToObject(payload, "method", "thing.service.deleteGroup_reply");
char *payload11=cJSON_Print(payload);
kk_sendData2app(infff,payload11,0);
free(payload11);
free(infff);
cJSON_Delete(payload);
cJSON_Delete(info);
return SUCCESS_RETURN;
}
/************************************************************
*功能描述:向网关发送删除群组消息
*************************************************************/
static int kk_service_deleteGroup_send_to_gw(cJSON *param,const char *groupId)
{
char parent[64] ={0};
kk_group_device_db_find_parent(groupId,parent);
cJSON *params = cJSON_CreateObject();
cJSON_AddStringToObject(params,"groupId",groupId);
dm_mgr_dev_node_t *node = NULL;
if(strlen(parent)!=0&&dm_mgr_get_device_by_devicecode(parent, &node)==SUCCESS_RETURN){
return kk_service_group_msg_send_to_gw("deleteGroup",params,node->productCode,node->deviceCode,parent);
}
return -1;
}
/************************************************************
*功能描述:向云端发送查询群组REPLY
*************************************************************/
static int kk_service_queryGroup_reply(cJSON *info_root,cJSON *msgId)
{
if(info_root == NULL || msgId == NULL){
return INVALID_PARAMETER;
}
cJSON *deviceCode = cJSON_GetObjectItem(info_root, MSG_DEVICE_CODE_STR);
if(deviceCode == NULL){
return FAIL_RETURN;
}
cJSON *productCode = cJSON_GetObjectItem(info_root, MSG_PRODUCT_CODE_STR);
if(productCode == NULL){
return FAIL_RETURN;
}
cJSON *info = cJSON_CreateObject();
cJSON_AddStringToObject(info, MSG_TYPE_STR, KK_THING_SERVICE_QUERYGROUP_REPLY);
cJSON_AddStringToObject(info, MSG_DEVICE_CODE_STR, deviceCode->valuestring);
cJSON_AddStringToObject(info, MSG_PRODUCT_CODE_STR, productCode->valuestring);
char *infff=cJSON_Print(info);
cJSON *payload = cJSON_CreateObject();
cJSON_AddStringToObject(payload, "desc", "success");
cJSON_AddStringToObject(payload, "version", "1.0");
cJSON_AddStringToObject(payload, "code", "0");
cJSON_AddStringToObject(payload, "msgId", msgId->valuestring);
cJSON_AddStringToObject(payload, "method", "thing.service.queryGroup_reply");
cJSON *params = kk_group_db_query(0);
cJSON_AddItemToObject(payload, "params", params);
char *payload11=cJSON_Print(payload);
kk_sendData2app(infff,payload11,0);
free(payload11);
free(infff);
cJSON_Delete(payload);
cJSON_Delete(info);
return SUCCESS_RETURN;
}
/************************************************************
*功能描述:向云端发送执行群组REPLY
*************************************************************/
static int kk_service_executeGroup_reply(cJSON *root,cJSON *msgId)
{
if(root == NULL || msgId == NULL){
return INVALID_PARAMETER;
}
cJSON *deviceCode = cJSON_GetObjectItem(root, MSG_DEVICE_CODE_STR);
if(deviceCode == NULL){
return FAIL_RETURN;
}
cJSON *productCode = cJSON_GetObjectItem(root, MSG_PRODUCT_CODE_STR);
if(productCode == NULL){
return FAIL_RETURN;
}
cJSON *info = cJSON_CreateObject();
cJSON_AddStringToObject(info, MSG_TYPE_STR, KK_THING_SERVICE_EXCUTEGROUP_REPLY);
cJSON_AddStringToObject(info, MSG_DEVICE_CODE_STR, deviceCode->valuestring);
cJSON_AddStringToObject(info, MSG_PRODUCT_CODE_STR, productCode->valuestring);
char *infff=cJSON_Print(info);
cJSON *payload = cJSON_CreateObject();
cJSON_AddStringToObject(payload, "desc", "success");
cJSON_AddStringToObject(payload, "version", "1.0");
cJSON_AddStringToObject(payload, "code", "0");
cJSON_AddStringToObject(payload, "msgId", msgId->valuestring);
cJSON_AddStringToObject(payload, "method", "thing.service.excuteGroup_reply");
char *payload11=cJSON_Print(payload);
kk_sendData2app(infff,payload11,0);
free(payload11);
free(infff);
cJSON_Delete(payload);
cJSON_Delete(info);
return SUCCESS_RETURN;
}
/******************************************************************************/
/* 函 数 名: kk_service_addGroup_handle */
/* 描 述: 添加group处理 */
/* 返 回 值: 返回'0'表示成功,其它返回值表示出错号 */
/******************************************************************************/
int kk_service_addGroup_handle(cJSON *root,cJSON *param,cJSON *msgId)
{
cJSON *groupName = NULL;
cJSON *ccuRoomId = NULL;
cJSON *didList = NULL;
cJSON *didListItem = NULL,*epNum = NULL,*deviceCode = NULL;
int i,didListCnt = 0;
char groupId[64] = {0};
HAL_GetTime_s(groupId);
groupName = cJSON_GetObjectItem(param, "groupName");
ccuRoomId = cJSON_GetObjectItem(param, "ccuRoomId");
didList = cJSON_GetObjectItem(param, "didList");
didListCnt = cJSON_GetArraySize(didList);
for(i=0;i<didListCnt;i++){
didListItem = cJSON_GetArrayItem(didList,i);
epNum = cJSON_GetObjectItem(didListItem, "epNum");
deviceCode = cJSON_GetObjectItem(didListItem, "deviceCode");
char *ptr = strrchr(deviceCode->valuestring,'_');
char *deviceCodeStr;
if(ptr==NULL){
deviceCodeStr = deviceCode->valuestring;
}else{
deviceCodeStr=ptr+1;
}
INFO_PRINT("epNum = %s,deviceCode = %s\n",epNum->valuestring,deviceCodeStr);
char parent[64];
memset(parent,0,sizeof(parent));
if(kk_subDev_find_fatherDeviceCode(deviceCodeStr,parent)==SUCCESS_RETURN){
INFO_PRINT("parent1 = %s\n",parent);
kk_group_db_insert(groupId,groupName->valuestring,ccuRoomId->valuestring,epNum->valuestring,deviceCodeStr,parent);
}else{
INFO_PRINT("not find parent.\n");
}
}
kk_service_addGroup_send_to_gw(param,groupId);
return kk_service_addGroup_reply(root,msgId,groupId);
}
/******************************************************************************/
/* 函 数 名: kk_service_updateGroup_handle */
/* 描 述: 更新group处理 */
/* 返 回 值: 返回'0'表示成功,其它返回值表示出错号 */
/******************************************************************************/
int kk_service_updateGroup_handle(cJSON *root,cJSON *param,cJSON *msgId)
{
cJSON *groupName = NULL;
cJSON *ccuRoomId = NULL;
cJSON *didList = NULL;
cJSON *didListItem = NULL,*epNum = NULL,*deviceCode = NULL;
int i,didListCnt = 0;
cJSON *groupId = NULL;
groupId = cJSON_GetObjectItem(param, "groupId");
groupName = cJSON_GetObjectItem(param, "groupName");
ccuRoomId = cJSON_GetObjectItem(param, "ccuRoomId");
didList = cJSON_GetObjectItem(param, "didList");
didListCnt = cJSON_GetArraySize(didList);
kk_group_db_delete(groupId->valuestring);
for(i=0;i<didListCnt;i++){
didListItem = cJSON_GetArrayItem(didList,i);
epNum = cJSON_GetObjectItem(didListItem, "epNum");
deviceCode = cJSON_GetObjectItem(didListItem, "deviceCode");
char *ptr = strrchr(deviceCode->valuestring,'_');
char *deviceCodeStr;
if(ptr==NULL){
deviceCodeStr = deviceCode->valuestring;
}else{
deviceCodeStr=ptr+1;
}
char parent[64];
memset(parent,0,sizeof(parent));
if(kk_subDev_find_fatherDeviceCode(deviceCodeStr,parent)==SUCCESS_RETURN){
INFO_PRINT("parent = %s\n",parent);
kk_group_db_insert(groupId->valuestring,groupName->valuestring,ccuRoomId->valuestring,epNum->valuestring,deviceCodeStr,parent);
}else{
INFO_PRINT("not find parent.\n");
}
}
kk_service_updateGroup_send_to_gw(param,groupId->valuestring);
return kk_service_updateGroup_reply(root,msgId);
}
/******************************************************************************/
/* 函 数 名: kk_service_deleteGroup_handle */
/* 描 述: 删除group处理 */
/* 返 回 值: 返回'0'表示成功,其它返回值表示出错号 */
/******************************************************************************/
int kk_service_deleteGroup_handle(cJSON *root,cJSON *param,cJSON *msgId)
{
cJSON *groupId = NULL;
groupId = cJSON_GetObjectItem(param, "groupId");
kk_service_deleteGroup_send_to_gw(param,groupId->valuestring);
kk_group_db_delete(groupId->valuestring);
return kk_service_deleteGroup_reply(root,msgId);
}
/******************************************************************************/
/* 函 数 名: kk_service_queryGroup_handle */
/* 描 述: 查询group处理 */
/* 返 回 值: 返回'0'表示成功,其它返回值表示出错号 */
/******************************************************************************/
int kk_service_queryGroup_handle(cJSON *root,cJSON *msgId)
{
return kk_service_queryGroup_reply(root,msgId);
}
/******************************************************************************/
/* 函 数 名: kk_service_executeGroup_handle */
/* 描 述: 执行group处理 */
/* 返 回 值: 返回'0'表示成功,其它返回值表示出错号 */
/******************************************************************************/
int kk_service_executeGroup_handle(cJSON *info_root,cJSON *param,cJSON *msgId)
{
cJSON *groupId = NULL;
cJSON *controlType = NULL;
cJSON *propertyValue = NULL;
INFO_PRINT("kk_service_executeGroup_handle \n");
groupId = cJSON_GetObjectItem(param, "groupId");
controlType = cJSON_GetObjectItem(param, "controlType");
propertyValue = cJSON_GetObjectItem(param, "propertyValue");
int type;
if(controlType->type==cJSON_String){
type = atoi(controlType->valuestring);
}else{
type = controlType->valueint;
}
char parent[64];
memset(parent,0,sizeof(parent));
cJSON *actions = kk_group_device_db_exe_act(groupId->valuestring,type,propertyValue->valueint,parent);
cJSON *params = cJSON_CreateObject();
cJSON_AddStringToObject(params,"groupId",groupId->valuestring);
cJSON_AddItemToObject(params,"actions",actions);
dm_mgr_dev_node_t *node = NULL;
if(strlen(parent)!=0&&dm_mgr_get_device_by_devicecode(parent, &node)==SUCCESS_RETURN){
return kk_service_group_msg_send_to_gw("excuteGroup",params,node->productCode,node->deviceCode,parent);
}
return kk_service_executeGroup_reply(info_root,msgId);
}
#ifndef __KK_GROUP_HANDLE_H
#define __KK_GROUP_HANDLE_H
#include "kk_tsl_common.h"
#include "sqlite3.h"
#include "kk_log.h"
#include "kk_scene_db.h"
#include "cJSON.h"
#include "kk_tsl_api.h"
#include "kk_dm_mng.h"
int kk_service_addGroup_handle(cJSON *root,cJSON *param,cJSON *msgId);
int kk_service_updateGroup_handle(cJSON *root,cJSON *param,cJSON *msgId);
int kk_service_deleteGroup_handle(cJSON *root,cJSON *param,cJSON *msgId);
int kk_service_queryGroup_handle(cJSON *root,cJSON *msgId);
int kk_service_executeGroup_handle(cJSON *info_root,cJSON *param,cJSON *msgId);
#endif
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