#include <stdio.h>
#include "kk_tsl_api.h"
#include "kk_dm_mng.h"
#include "sqlite3.h"
#include "kk_log.h"
#include "kk_area_handle.h"
#include "kk_hal.h"
#include "id.h"

extern sqlite3 *g_kk_pDb;

static kk_dev_list_t *s_device_list = NULL;
static kk_floor_list_t *s_floor_list = NULL;
typedef struct {
	void *mutex;
	int  roomNum;
	sqlite3 *pDb;	
} kk_area_ctx_t;



static kk_area_ctx_t s_kk_area_ctx = {NULL,0,NULL};

static kk_area_ctx_t *_kk_area_get_ctx(void)
{
	return &s_kk_area_ctx;
}
static void _kk_area_lock(void)
{
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	if (ctx->mutex) {
		HAL_MutexLock(ctx->mutex);
	}
}

static void _kk_area_unlock(void)
{
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	
	if (ctx->mutex) {
		HAL_MutexUnlock(ctx->mutex);
	}
}
static int _kk_area_db_init(void)
{
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	char *pcErr;

	//eUtils_LockLock(&sLock);
	_kk_area_lock();
	ctx->pDb = g_kk_pDb;

	INFO_PRINT("area db Database opened\n");

	const char *pAreaTable = "CREATE TABLE IF NOT EXISTS AreaInfo( \
		idx INTEGER PRIMARY KEY, \
		name varchar(256), \
		roomId varchar(256), \
		armingstate INTEGER, \
		floorId varchar(256), \
		floorName varchar(256))";

	if (sqlite3_exec(ctx->pDb, pAreaTable, NULL, NULL, &pcErr) != SQLITE_OK)
	{
		ERROR_PRINT("Error creating table (%s)\n", pcErr);
		sqlite3_free(pcErr);
		//eUtils_LockUnlock(&sLock);
		_kk_area_unlock();
		return FAIL_RETURN;
	}
	const char *pFloorTable = "CREATE TABLE IF NOT EXISTS floorInfo( \
		idx INTEGER PRIMARY KEY, \
		name varchar(256), \
		floorId varchar(256))";

	if (sqlite3_exec(ctx->pDb, pFloorTable, NULL, NULL, &pcErr) != SQLITE_OK)
	{
		ERROR_PRINT("Error creating table (%s)\n", pcErr);
		sqlite3_free(pcErr);
		//eUtils_LockUnlock(&sLock);
		_kk_area_unlock();
		return FAIL_RETURN;
	}
	const char *pAreaDevTable = "CREATE TABLE IF NOT EXISTS AreaDevInfo( \
		idx INTEGER PRIMARY KEY, \
		roomId varchar(256), \
		roomName varchar(256), \
		deviceCode varchar(33), \
		epNum varchar(10), \
		devName varchar(256))";

	if (sqlite3_exec(ctx->pDb, pAreaDevTable, NULL, NULL, &pcErr) != SQLITE_OK)
	{
		ERROR_PRINT("Error creating table (%s)\n", pcErr);
		sqlite3_free(pcErr);
		//eUtils_LockUnlock(&sLock);
		_kk_area_unlock();
		return FAIL_RETURN;
	}

	_kk_area_unlock();
	return SUCCESS_RETURN;
}
int kk_area_init(void)
{
	int res = 0;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	set_workid(100);
	/* Create Mutex */
	ctx->mutex = HAL_MutexCreate();
	if (ctx->mutex == NULL) {
		return FAIL_RETURN;
	}
	res = _kk_area_db_init();
	if(res != SUCCESS_RETURN){
		ERROR_PRINT("[%s][%d]kk_area_init FAIL!!!\n",__FUNCTION__,__LINE__);
	}
	kk_floor_add("默认","0");//创建默认楼层
	return SUCCESS_RETURN;
}
static int kk_check_room_exist(const char *roomId)
{
	int isExist = 0;
	sqlite3_stmt *stmt;
	char *proomId = NULL;

	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	const char *searchCmd = "select * from AreaInfo;";
	_kk_area_lock();
	
	sqlite3_prepare_v2(ctx->pDb, searchCmd, strlen(searchCmd), &stmt, NULL);

	while(sqlite3_step(stmt) == SQLITE_ROW){
		proomId = (char*)sqlite3_column_text(stmt, DB_ROOM_ID);

		if(!strcmp(proomId,roomId))
		{
			isExist = 1;
			break;
		}
	}

	sqlite3_finalize(stmt);
	_kk_area_unlock();
	return isExist;
}

static 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 *zErrMsg = 0;

	const char *insertCmd = "insert into AreaInfo (name, roomId,armingstate,floorId,floorName) \
								values ('%s','%s','%d','%s','%s');";						
	_kk_area_lock();
	ctx->roomNum++;

	sqlCmd = sqlite3_mprintf(insertCmd,name,roomId,-1,"0","默认");	
	res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
	if( res != SQLITE_OK ){
		ERROR_PRINT("SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	}else{
		//INFO_PRINT("sub device insert data successfully\n");
	}
	sqlite3_free(sqlCmd);
	_kk_area_unlock();
	return SUCCESS_RETURN;
}

static int _kk_room_update(const char *name,const char *roomId)
{
	int res = 0;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	char *sqlCmd = NULL;
	char *zErrMsg = 0;

	sqlCmd = sqlite3_mprintf("UPDATE AreaInfo SET name='%s'  WHERE roomId= '%s'",name,roomId);	

	_kk_area_lock();

	res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
	if( res != SQLITE_OK ){
		ERROR_PRINT("SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	}else{
		//INFO_PRINT("sub device insert data successfully\n");
	}
	sqlite3_free(sqlCmd);
	_kk_area_unlock();
	return SUCCESS_RETURN;
}
static int _kk_room_dev_update(const char *name,const char *roomId)
{
	int res = 0;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	char *sqlCmd = NULL;
	char *zErrMsg = 0;

	sqlCmd = sqlite3_mprintf("UPDATE AreaDevInfo SET roomName='%s'  WHERE roomId= '%s'",name,roomId);	

	_kk_area_lock();
	
	res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
	if( res != SQLITE_OK ){
		ERROR_PRINT("SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	}else{
		//INFO_PRINT("sub device insert data successfully\n");
	}
	sqlite3_free(sqlCmd);
	_kk_area_unlock();
	return SUCCESS_RETURN;
}
int kk_room_add(const char *name,const char *roomId)
{
	if(kk_check_room_exist(roomId)){
		printf("name=%s,room id=%s\n",name,roomId);
		_kk_room_dev_update(name,roomId);
		_kk_room_update(name,roomId);
		return 0;
	}else{
		return _kk_room_add(name,roomId);
	}

}
int kk_get_floorname_by_id(const char *floorid,char *floorname,int len)
{
	int isExist = 0;
	sqlite3_stmt *stmt;
	char *pFloorname = NULL;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	char *sqlCmd = NULL;
	int tlen = 0;
	if(floorid == NULL){
		return -1;
	}
	_kk_area_lock();
	sqlCmd = sqlite3_mprintf("select * from floorInfo WHERE floorId= '%s'",floorid);
	sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL);
	//INFO_PRINT("total_column = %d\n", sqlite3_column_count(stmt));
	while(sqlite3_step(stmt) == SQLITE_ROW){
		pFloorname = (char*)sqlite3_column_text(stmt, DB_FLOOR_NAME);
		if(pFloorname != NULL && strlen(pFloorname) > 0){
			tlen = strlen(pFloorname) > len?len:strlen(pFloorname);
			memcpy(floorname,pFloorname,strlen(pFloorname));
		}else{
			memcpy(floorname,"",len);
		}				
	}
	sqlite3_finalize(stmt);
	sqlite3_free(sqlCmd);
	_kk_area_unlock();
	return 0;
}
int kk_room_update_armingstate(int state,const char *roomid)
{
	char *sqlCmd = NULL;
	int rc = 0;
	char *zErrMsg = 0;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
 
	_kk_area_lock();
	sqlCmd = sqlite3_mprintf("UPDATE AreaInfo SET armingstate=%d  WHERE roomId= '%s'",state,roomid);	
	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_area_unlock();
	return SUCCESS_RETURN;
}
static int kk_check_floor_exist(const char *floorId)
{
	int isExist = 0;
	sqlite3_stmt *stmt;
	char *pfloorId = NULL;

	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	const char *searchCmd = "select * from floorInfo;";
	_kk_area_lock();
	sqlite3_prepare_v2(ctx->pDb, searchCmd, strlen(searchCmd), &stmt, NULL);
	while(sqlite3_step(stmt) == SQLITE_ROW){
		pfloorId = (char*)sqlite3_column_text(stmt, DB_FLOOR_ID);
		if(!strcmp(pfloorId,floorId))
		{
			isExist = 1;
			break;
		}
	}
	sqlite3_finalize(stmt);
	_kk_area_unlock();
	return isExist;
}
int kk_floor_add(const char *name,const char *floorId)
{
	int res = 0;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	char *sqlCmd = NULL;
	char *zErrMsg = 0;
	const char *insertCmd = NULL;
	if(kk_check_floor_exist(floorId)){
		insertCmd = "UPDATE floorInfo SET name='%s' WHERE floorId= '%s'";
	}else{
		insertCmd = "insert into floorInfo (name, floorId) \
									values ('%s','%s');";
	}					
	_kk_area_lock();

	sqlCmd = sqlite3_mprintf(insertCmd,name,floorId);	
	res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
	if( res != SQLITE_OK ){
		ERROR_PRINT("SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	}else{
		//INFO_PRINT("sub device insert data successfully\n");
	}
	sqlite3_free(sqlCmd);
	_kk_area_unlock();
	return SUCCESS_RETURN;
}
int kk_room_set_floor_info(const char*floorId,const char* floorName,const char *roomid)
{
	char *sqlCmd = NULL;
	int rc = 0;
	char *zErrMsg = 0;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	if(strcmp(floorName,"") == 0){
		char floorNameBuf[128] = {0};
		kk_get_floorname_by_id(floorId,floorNameBuf,sizeof(floorNameBuf));
		sqlCmd = sqlite3_mprintf("UPDATE AreaInfo SET floorId='%s',floorName='%s' WHERE roomId= '%s'",floorId,floorNameBuf,roomid);
	}else{
		sqlCmd = sqlite3_mprintf("UPDATE AreaInfo SET floorId='%s',floorName='%s' WHERE roomId= '%s'",floorId,floorName,roomid);
	}
	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);
	return SUCCESS_RETURN;
}
int kk_set_floor_to_default(const char*floorId)
{
	char *sqlCmd = NULL;
	int rc = 0;
	char *zErrMsg = 0;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
 
	_kk_area_lock();
	sqlCmd = sqlite3_mprintf("UPDATE AreaInfo SET floorId='%s',floorName='%s' WHERE floorId= '%s'","0","默认",floorId);	
	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_area_unlock();
	return SUCCESS_RETURN;
}
int kk_room_delete(const char *roomId)
{
	int res = 0;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	char *sqlCmd = NULL;
	char *zErrMsg = 0;
	const char *deleteCmd = "delete from AreaInfo where roomId = '%s';";
	kk_room_dev_remove_by_roomid(roomId);
	_kk_area_lock();
	sqlCmd = sqlite3_mprintf(deleteCmd,roomId);	

	INFO_PRINT("Table delete data sqlCmd:%s\n",sqlCmd);
	res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
	if( res != SQLITE_OK ){
		ERROR_PRINT("SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	}else{
		//INFO_PRINT("Table delete data successfully\n");
		ctx->roomNum--;
	}
	sqlite3_free(sqlCmd);
	
	_kk_area_unlock();
	return SUCCESS_RETURN;
}
int kk_floor_delete_all(void)
{
	int res = 0;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	char *sqlCmd = NULL;
	char *zErrMsg = 0;
	const char *deleteCmd = "delete from floorInfo where floorId != '%s';";
	_kk_area_lock();
	sqlCmd = sqlite3_mprintf(deleteCmd,"1");	

	INFO_PRINT("Table delete data sqlCmd:%s\n",sqlCmd);
	res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
	if( res != SQLITE_OK ){
		ERROR_PRINT("SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	}else{
		//INFO_PRINT("Table delete data successfully\n");
	}
	sqlite3_free(sqlCmd);
	
	_kk_area_unlock();
	return SUCCESS_RETURN;
}
int kk_floor_delete_by_id(char *floorId)
{
	int res = 0;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	char *sqlCmd = NULL;
	char *zErrMsg = 0;
	const char *deleteCmd = "delete from floorInfo where floorId = '%s';";
	_kk_area_lock();
	sqlCmd = sqlite3_mprintf(deleteCmd,floorId);	

	INFO_PRINT("Table delete data sqlCmd:%s\n",sqlCmd);
	res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
	if( res != SQLITE_OK ){
		ERROR_PRINT("SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	}else{
		//INFO_PRINT("Table delete data successfully\n");
	}
	sqlite3_free(sqlCmd);
	
	_kk_area_unlock();
	return SUCCESS_RETURN;
}
static int _kk_check_dev_exist(const char* deviceCode,const char *epNum)
{
	int isExist = 0;
	sqlite3_stmt *stmt;
	char *pDeviceCode = NULL;
	char *pEpNum = NULL;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	const char *searchCmd = "select * from AreaDevInfo;";
	_kk_area_lock();
	
	sqlite3_prepare_v2(ctx->pDb, searchCmd, strlen(searchCmd), &stmt, NULL);
	//INFO_PRINT("total_column = %d\n", sqlite3_column_count(stmt));
	while(sqlite3_step(stmt) == SQLITE_ROW){
		pDeviceCode = (char*)sqlite3_column_text(stmt, DB_DEV_DEVICECODE);
		pEpNum = (char*)sqlite3_column_text(stmt, DB_DEV_EPNUM);
		if(!strcmp(deviceCode,pDeviceCode) && !strcmp(pEpNum,epNum))
		{
			isExist = 1;
			INFO_PRINT("_kk_check_dev_exist\n");
			break;
		}
	}
	//INFO_PRINT("\n");
	sqlite3_finalize(stmt);
	_kk_area_unlock();
	return isExist;
}

int kk_room_dev_add(const char *roomId,const char *roomName,const char *deviceCode,const char *epNum,const char *devName)
{
	int res = 0;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	char *sqlCmd = NULL;
	char *zErrMsg = 0;


	if(_kk_check_dev_exist(deviceCode,epNum)){
		sqlCmd = sqlite3_mprintf("UPDATE AreaDevInfo SET roomId='%s', roomName='%s', devName='%s'\
					WHERE deviceCode= '%s' and epNum = '%s'",roomId,roomName,devName,deviceCode,epNum);	
	}
	else{
		const char *insertCmd = "insert into AreaDevInfo (roomId, roomName, deviceCode, epNum, devName) \
							values ('%s','%s','%s','%s','%s');";	
		sqlCmd = sqlite3_mprintf(insertCmd,roomId,roomName,deviceCode,epNum,devName);
	}
								
	_kk_area_lock();
	res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
	if( res != SQLITE_OK ){
		ERROR_PRINT("SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	}else{
		//INFO_PRINT("sub device insert data successfully\n");
	}
	sqlite3_free(sqlCmd);
	_kk_area_unlock();
	return SUCCESS_RETURN;
}
int kk_room_reset_armingstate(void)
{
	char *sqlCmd = NULL;
	int rc = 0;
	char *zErrMsg = 0;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();

	_kk_area_lock();
	sqlCmd = sqlite3_mprintf("UPDATE AreaInfo SET armingstate=%d ",0);	
	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_area_unlock();
	return SUCCESS_RETURN;
}
int kk_get_room_armingstate(const char* roomId)
{
	sqlite3_stmt *stmt;
	int state = 0;
	char *sqlCmd = NULL;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	const char *searchCmd = "select * from AreaInfo where roomId = '%s';";
	_kk_area_lock();
	sqlCmd = sqlite3_mprintf(searchCmd,roomId);
	sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL);
	while(sqlite3_step(stmt) == SQLITE_ROW){
		state = sqlite3_column_int(stmt, DB_ROOM_ARMING);
	}
	sqlite3_finalize(stmt);
	sqlite3_free(sqlCmd);
	_kk_area_unlock();
	return state;
}
int kk_get_roomId_by_deviceCode(const char* deviceCode,const char *epNum,char *roomId,int size)
{
	int isGet = 0;
	sqlite3_stmt *stmt;
	char *pDeviceCode = NULL;
	char *pEpNum = NULL;
	char *pRoomId = NULL;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	const char *searchCmd = "select * from AreaDevInfo;";
	_kk_area_lock();
	sqlite3_prepare_v2(ctx->pDb, searchCmd, strlen(searchCmd), &stmt, NULL);

	while(sqlite3_step(stmt) == SQLITE_ROW){
		pDeviceCode = (char*)sqlite3_column_text(stmt, DB_DEV_DEVICECODE);
		pEpNum = (char*)sqlite3_column_text(stmt, DB_DEV_EPNUM);
		if(!strcmp(deviceCode,pDeviceCode) && !strcmp(pEpNum,epNum))
		{
			pRoomId = (char*)sqlite3_column_text(stmt, DB_DEV_ROOM_ID);
			memset(roomId,0,size);
			snprintf(roomId,size,"%s",pRoomId);
			isGet = 1;
		}
	}
	//INFO_PRINT("\n");
	sqlite3_finalize(stmt);
	_kk_area_unlock();
	return isGet;
}
int kk_get_device_name(const char* deviceCode,const char *epNum,char *devName,int size)
{
	int isGet = 0;
	sqlite3_stmt *stmt;
	char *pDeviceCode = NULL;
	char *pEpNum = NULL;
	char *pDevName = NULL;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	const char *searchCmd = "select * from AreaDevInfo;";
	_kk_area_lock();
	
	sqlite3_prepare_v2(ctx->pDb, searchCmd, strlen(searchCmd), &stmt, NULL);

	while(sqlite3_step(stmt) == SQLITE_ROW){
		pDeviceCode = (char*)sqlite3_column_text(stmt, DB_DEV_DEVICECODE);
		pEpNum = (char*)sqlite3_column_text(stmt, DB_DEV_EPNUM);
		if(!strcmp(deviceCode,pDeviceCode) && !strcmp(pEpNum,epNum))
		{
			pDevName = (char*)sqlite3_column_text(stmt, DB_DEV_DEV_NAME);
			memset(devName,0,size);
			snprintf(devName,size,"%s",pDevName);
			isGet = 1;
		}
	}
	//INFO_PRINT("\n");
	sqlite3_finalize(stmt);
	_kk_area_unlock();
	return isGet;
}
int kk_get_device_roomInfo(const char* deviceCode,int epNum,char *roomName,char *roomId)
{
	int isGet = 0;
	char *sqlCmd = NULL;
	char *proomName = NULL;
	char *proomId = NULL;
	sqlite3_stmt *stmt;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	const char *searchCmd = "select * from AreaDevInfo where deviceCode = '%s' and epNum = '%d';";
	_kk_area_lock();
	sqlCmd = sqlite3_mprintf(searchCmd,deviceCode,epNum);
	sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL);

	while(sqlite3_step(stmt) == SQLITE_ROW){
		proomName = (char*)sqlite3_column_text(stmt, DB_DEV_ROOM_NAME);
		proomId = (char*)sqlite3_column_text(stmt, DB_DEV_ROOM_ID);
		if(strlen(proomName) < 256){
			memcpy(roomName,proomName,strlen(proomName));
		}else{
			memcpy(roomName,proomName,255);
		}
		memcpy(roomId,proomId,strlen(proomId));
		isGet = 1;
	}
	//INFO_PRINT("\n");
	sqlite3_free(sqlCmd);
	sqlite3_finalize(stmt);
	_kk_area_unlock();
	return isGet;
}
int kk_room_dev_remove_by_roomid(const char *roomid)
{
	int res = 0;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	char *sqlCmd = NULL;
	char *zErrMsg = 0;
	const char *deleteCmd = "delete from AreaDevInfo where roomId = '%s';";

	_kk_area_lock();
	sqlCmd = sqlite3_mprintf(deleteCmd,roomid);	
	res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
	if( res != SQLITE_OK ){
		ERROR_PRINT("SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	}else{
		//INFO_PRINT("sub device insert data successfully\n");
	}
	sqlite3_free(sqlCmd);
	_kk_area_unlock();
	return SUCCESS_RETURN;
}
int kk_room_dev_remove(const char *deviceCode,const char *epNum)
{
	int res = 0;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	char *sqlCmd = NULL;
	char *zErrMsg = 0;
	const char *insertCmd = NULL;

	if(epNum == NULL){
		insertCmd = "delete from AreaDevInfo where deviceCode = '%s';";
	}
	else{
		insertCmd = "delete from AreaDevInfo where deviceCode = '%s' and epNum = '%s';";
	}

	_kk_area_lock();
	sqlCmd = sqlite3_mprintf(insertCmd,deviceCode,epNum);	
	res = sqlite3_exec(ctx->pDb, sqlCmd, NULL, NULL, &zErrMsg);
	if( res != SQLITE_OK ){
		ERROR_PRINT("SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	}else{
		//INFO_PRINT("sub device insert data successfully\n");
	}
	sqlite3_free(sqlCmd);
	_kk_area_unlock();
	return SUCCESS_RETURN;
}

int kk_room_device_list_add(const char *deviceCode)
{
	//int len;
	kk_dev_list_t *ptr,*pre;
	
	ptr = pre = s_device_list;
	if(deviceCode == NULL){
		return FAIL_RETURN;
	}
	
	while(ptr!=NULL){
		pre = ptr;
		if(strcmp(ptr->deviceCode,deviceCode) == 0){
			return SUCCESS_RETURN;
		}
		ptr = ptr->next;
	}
	ptr = (kk_dev_list_t*)malloc(sizeof(kk_dev_list_t));
	memset(ptr,0,sizeof(kk_dev_list_t));
	memcpy(ptr->deviceCode,deviceCode,strlen(deviceCode));

	if(s_device_list==NULL){
		s_device_list = ptr;
	}else{
		pre->next = ptr;
	}

	return SUCCESS_RETURN;
}
static int kk_room_floor_list_add(char *floorId,char *floorName)
{
	//int len;
	kk_floor_list_t *ptr,*pre;
	
	ptr = pre = s_floor_list;
	if(floorId == NULL ||floorName == NULL){
		return FAIL_RETURN;
	}
	while(ptr!=NULL){
		pre = ptr;
		if(strcmp(ptr->floorId,floorId) == 0){
			return SUCCESS_RETURN;
		}
		ptr = ptr->next;
	}
	ptr = (kk_floor_list_t*)malloc(sizeof(kk_floor_list_t));
	memset(ptr,0,sizeof(kk_floor_list_t));
	ptr->floorId = (char*)malloc(strlen(floorId)+1);
	memset(ptr->floorId,0x0,strlen(floorId)+1);
	strncpy(ptr->floorId,floorId,strlen(floorId));
	ptr->floorName = (char*)malloc(strlen(floorName)+1);
	memset(ptr->floorName,0x0,strlen(floorName)+1);
	strncpy(ptr->floorName,floorName,strlen(floorName));
	if(s_floor_list==NULL){
		s_floor_list = ptr;
	}else{
		pre->next = ptr;
	}
	return SUCCESS_RETURN;
}
void kk_free_floor_list(void)
{
	kk_floor_list_t *ptr = s_floor_list;
	kk_floor_list_t *ptmp = NULL;
	while(ptr != NULL){
		ptmp = ptr->next;
		free(ptr->floorId);
		ptr->floorId = NULL;
		free(ptr->floorName);
		ptr->floorName = NULL;		
		free(ptr);
		ptr = NULL;
		ptr = ptmp;
	}
	s_floor_list = NULL;
}

cJSON * kk_get_roomids_by_floorId(const char* floorId)
{
	sqlite3_stmt *stmt;
	char *pRoomId = NULL;
	int count = 0;
	char *sqlCmd = NULL;
	cJSON *roomIdsAry = cJSON_CreateArray();
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	const char *searchCmd = "select * from AreaInfo where floorId = '%s';";

	sqlCmd = sqlite3_mprintf(searchCmd,floorId);
	sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL);
	while(sqlite3_step(stmt) == SQLITE_ROW){
		cJSON *roomJson =cJSON_CreateObject();
		pRoomId = (char*)sqlite3_column_text(stmt, DB_ROOM_ID);
		cJSON_AddStringToObject(roomJson, "roomId", pRoomId);
		cJSON_AddItemToArray(roomIdsAry,roomJson);
	}
	sqlite3_finalize(stmt);
	sqlite3_free(sqlCmd);
	return roomIdsAry;
}
kk_floor_list_t *kk_get_floorIds_ary(void)
{
	sqlite3_stmt *stmt;
	char *pfloorId = NULL;
	char *pfloorName = NULL;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	const char *searchCmd = "select * from floorInfo;";
	_kk_area_lock();
	
	sqlite3_prepare_v2(ctx->pDb, searchCmd, strlen(searchCmd), &stmt, NULL);

	while(sqlite3_step(stmt) == SQLITE_ROW){
		pfloorId = (char*)sqlite3_column_text(stmt, DB_FLOOR_ID);
		pfloorName = (char*)sqlite3_column_text(stmt, DB_FLOOR_NAME);
		printf("------------->pfloorId:%s,pfloorName:%s\n",pfloorId,pfloorName);
		kk_room_floor_list_add(pfloorId,pfloorName);
	}
	sqlite3_finalize(stmt);
	_kk_area_unlock();
	return s_floor_list;
}
cJSON *kk_get_roomIds_ary(void)
{
	int isExist = 0;
	sqlite3_stmt *stmt;
	char *proomId = NULL;
	int armingstate= 0;
	cJSON *roomIdsAry = cJSON_CreateArray();

	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	const char *searchCmd = "select * from AreaInfo;";
	_kk_area_lock();
	
	sqlite3_prepare_v2(ctx->pDb, searchCmd, strlen(searchCmd), &stmt, NULL);

	while(sqlite3_step(stmt) == SQLITE_ROW){
		armingstate = (int)sqlite3_column_int(stmt, DB_ROOM_ARMING);
		if(armingstate!=0){
			proomId = (char*)sqlite3_column_text(stmt, DB_ROOM_ID);
			cJSON_AddItemToArray(roomIdsAry,cJSON_CreateNumber(atoi(proomId)));
		}
		
	}
	sqlite3_finalize(stmt);
	_kk_area_unlock();
	return roomIdsAry;
}
cJSON *kk_get_roomIds_ary_by_floorid(char *floorid)
{
	int isExist = 0;
	sqlite3_stmt *stmt;
	char *proomId = NULL;
	int armingstate= 0;
	cJSON *roomIdsAry = cJSON_CreateArray();

	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	const char *searchCmd = "select * from AreaInfo whre;";

	sqlite3_prepare_v2(ctx->pDb, searchCmd, strlen(searchCmd), &stmt, NULL);

	while(sqlite3_step(stmt) == SQLITE_ROW){
		armingstate = (int)sqlite3_column_int(stmt, DB_ROOM_ARMING);
		if(armingstate!=0){
			proomId = (char*)sqlite3_column_text(stmt, DB_ROOM_ID);
			cJSON_AddItemToArray(roomIdsAry,cJSON_CreateNumber(atoi(proomId)));
		}
		
	}
	sqlite3_finalize(stmt);
	return roomIdsAry;
}

kk_dev_list_t* kk_get_room_deviceCode(const char* roomId)
{
	sqlite3_stmt *stmt;
	char *sqlCmd = NULL;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	const char *searchCmd = "select * from AreaDevInfo where epNum = 1;";	
	_kk_area_lock();
	sqlCmd = sqlite3_mprintf(searchCmd,roomId);	
	sqlite3_prepare_v2(ctx->pDb, sqlCmd, strlen(sqlCmd), &stmt, NULL);
	//INFO_PRINT("total_column = %d\n", sqlite3_column_count(stmt));
	while(sqlite3_step(stmt) == SQLITE_ROW){
		if(strcmp((char*)sqlite3_column_text(stmt, DB_DEV_ROOM_ID),roomId) == 0){
			kk_room_device_list_add((char*)sqlite3_column_text(stmt, DB_DEV_DEVICECODE));
		}
	}
	sqlite3_finalize(stmt);
	_kk_area_unlock();
	return s_device_list;
}
void kk_free_room_dev_list(void)
{
	kk_dev_list_t *ptr = s_device_list;
	kk_dev_list_t *ptmp = NULL;
	while(ptr != NULL){
		ptmp = ptr->next;
		free(ptr);
		ptr = NULL;
		ptr = ptmp;
	}
	s_device_list = NULL;
}

static int kk_check_floorId(int nodeId)
{
	int isExist = 0;
	sqlite3_stmt *stmt;
	char *pnode = 0;
	kk_area_ctx_t *ctx = _kk_area_get_ctx();
	const char *searchCmd = "select * from floorInfo;";	
	_kk_area_lock();
	sqlite3_prepare_v2(ctx->pDb, searchCmd, strlen(searchCmd), &stmt, NULL);
	while(sqlite3_step(stmt) == SQLITE_ROW){
		pnode = (char*)sqlite3_column_text(stmt, DB_FLOOR_ID);
		if( atoi(pnode)==nodeId)
		{
			isExist = 1;
			break;
		}
	}
	sqlite3_finalize(stmt);
	_kk_area_unlock();
	return isExist;
}
int kk_creater_floorid(char *floorId)
{
	static int next = 100;

	while(kk_check_floorId(next)){
		++next;
	}
	sprintf(floorId,"%d",next);

	return 0;
	
}
