#include "kk_msg_report.h"


static void kk_msg_report(cJSON *data,char *msgtype,char *method,EmberEUI64 mac)
{
	static uint16_t msgid;
	char msgIdString[10]= {0};
	char macString[19] = {0};
	sub_dev_node_t *node = NULL;
	int res = 0;
	
	rpc_eui64ToString(mac,macString);	
	res = kk_sub_tsl_get_device_by_mac(macString, &node);
	if (res != SUCCESS_RETURN) {
		printf("[%s][%d]kk_rpc_send_message error!!!\n",__FUNCTION__,__LINE__);
		return; 
	}	

	cJSON *info = rpc_cJSON_CreateObject();
	if(info != NULL){
		rpc_cJSON_AddStringToObject(info, "msgType",msgtype);
		rpc_cJSON_AddStringToObject(info, "productCode",node->productCode);
		rpc_cJSON_AddStringToObject(info, "deviceCode",macString);	
	}
	cJSON *payload = rpc_cJSON_CreateObject();
	if(payload != NULL){
		sprintf(msgIdString,"%d",++msgid);
		rpc_cJSON_AddStringToObject(payload, "msgId",msgIdString);
		rpc_cJSON_AddStringToObject(payload, "version", KK_IPC_VERSION);
		rpc_cJSON_AddStringToObject(payload, "method",method);
		rpc_cJSON_AddItemToObject(payload, "params", data);		
	}
	cJSON *root = rpc_cJSON_CreateObject();
	if(root != NULL){
		rpc_cJSON_AddItemToObject(root, "info", info);	
		rpc_cJSON_AddItemToObject(root, "payload", payload);	
	}

	char* p = rpc_cJSON_Print(root);
	emberAfAppPrintln("send json:\n%s\n",p);
	free(p);
	jrpc_send_msg(root);	
	rpc_cJSON_Delete(root);	
}

static void kk_msg_report_joined(cJSON *data,EmberEUI64 mac)
{
	kk_msg_report(data,KK_REPORT_DEVICE_JOINED_TYPE,KK_REPORT_DEVICE_JOINED_METHOD,mac);
}
static void kk_msg_report_leave(cJSON *data,EmberEUI64 mac)
{
	kk_msg_report(data,KK_REPORT_DEVICE_LEFT_TYPE,KK_REPORT_DEVICE_LEAVE_METHOD,mac);
}
void kk_msg_report_property(cJSON *data,EmberEUI64 mac)
{
	kk_msg_report(data,KK_REPORT_ATTRIBUTE_TYPE,KK_REPORT_ATTRIBUTE_METHOD,mac);
}

void kk_msg_report_dev_joined(EmberEUI64 mac,const char* productCode)
{
	cJSON* devicesJson;
	EmberEUI64 localEui64  = {0};
	char macString[KK_EUI64_STRING_LENGTH] = {0};

	UTIL_LOG_WARNING("report tsl product code:%s\n",productCode);
	emberAfDebugPrint("mac:");
	emberAfPrintBigEndianEui64(mac);
	emberAfDebugPrintln("");

	devicesJson = rpc_cJSON_CreateObject();
	rpc_eui64ToString(mac,macString);
	rpc_cJSON_AddStringToObject(devicesJson, "productCode",productCode);
	rpc_cJSON_AddStringToObject(devicesJson, "deviceCode",macString);
	rpc_cJSON_AddStringToObject(devicesJson, "mac",macString);

	kk_sub_tsl_add(mac,productCode);
	emberAfGetEui64(localEui64);
	kk_msg_report_joined(devicesJson,localEui64);
}

void kk_msg_report_dev_leave(EmberEUI64 mac)
{
	int res = 0;
	cJSON* devicesJson;
	sub_dev_node_t *node = NULL;
	EmberEUI64 localEui64  = {0};
	char macString[KK_EUI64_STRING_LENGTH];
	
	rpc_eui64ToString(mac,macString);	
	res = kk_sub_tsl_get_device_by_mac(macString, &node);
	if (res != SUCCESS_RETURN) {
		emberAfDebugPrintln("[%s][%d]kk msg send error!!!\n",__FUNCTION__,__LINE__);
		return; 
	}
	devicesJson = rpc_cJSON_CreateObject();
	rpc_cJSON_AddStringToObject(devicesJson, "productCode",node->productCode);
	rpc_cJSON_AddStringToObject(devicesJson, "deviceCode",macString);
	emberAfGetEui64(localEui64);
	kk_msg_report_leave(devicesJson,localEui64);
	kk_sub_tsl_delete(mac);
	kk_device_table_delete(mac);
	kk_device_db_delete(mac);
}


int kk_sendData2CCU(char* data, int len)
{
	if (strcmp(GW2CCU_PROTOCOL,"tcp") == 0 ){
		kk_tcp_client_send(data, len);
	}else{
		kk_ipc_send(IPC_PLAT2MID, data, len);
	}
}

