/*
 * COMMON_PROCESS.c
 *
 *  Created on: 2020年7月30日
 *      Author: Seuls
 */
#include PLATFORM_HEADER
#include "stack/include/ember-types.h"
#include "hal/hal.h"
#include "common_process.h"
#include "uart_handler.h"
#include <00ikonke-app/general/ikk-opt-tunnel.h>
#include <00ikonke-app/general/ikk-cluster.h>
#include "./general/ikk-common-utils.h"

DEVICVE_INFO_ST g_sDevice_Info = {
		.Device_Type =Device_Type_Air_Condition,
		.Binding_Device_SrcEP = 0,
		.mcu_hardware_version = 0xff,
		.mcu_hardware_version = 0xff,
};

bool APP_PreCommandHandlerCallback(EmberAfClusterCommand* cmd)
{
	uint8_t i 		 = cmd->payloadStartIndex;
	uint8_t endpoint = cmd->apsFrame->destinationEndpoint;
	uint16_t attribute_id = BUILD_UINT16(cmd->buffer[i+ 0], cmd->buffer[i + 1]);
	uint8_t temp_data 	=0;
	uint16_t temp_16_t 	= 0;
	iKonkeAfSelfPrint("#########APP_PreCommandHandlerCallback,EP = %d,attribute_id = 0x%2x\r\n",endpoint,attribute_id);
	switch(cmd->apsFrame->clusterId)
	{
		case (ZCL_ON_OFF_CLUSTER_ID):
		{
			if(	(cmd->clusterSpecific) && (cmd->direction == (uint8_t)ZCL_DIRECTION_CLIENT_TO_SERVER) &&
				(!cmd->mfgSpecific)) {
				switch(cmd->commandId) {
					case (ZCL_OFF_COMMAND_ID):
					{
						if( (endpoint == 1)&&(sDevice_attr_last.air_condition.on_off) )
						{
							temp_data = 0;
							Control_MCU(&g_sMsgNode,endpoint,CMD_ON_OFF,temp_data);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_ON_OFF_CLUSTER_ID,  false, false, false, false);
//							if(g_sMsgNode.buffer[POLAD_INDEX] == TP_SUCCESS)
//								Write_OnOff_Attr(0,endpoint,&temp_data);
						}
						else if( (endpoint == 2)&&(sDevice_attr_last.floor_heating.on_off) )
						{
							temp_data = 0;
							Control_MCU(&g_sMsgNode,endpoint,CMD_ON_OFF,temp_data);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_ON_OFF_CLUSTER_ID,  false, false, false, false);
						}
						else if( (endpoint == 3)&&(sDevice_attr_last.fan.on_off) )
						{
							temp_data = 0;
							Control_MCU(&g_sMsgNode,endpoint,CMD_ON_OFF,temp_data);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_ON_OFF_CLUSTER_ID,  false, false, false, false);
						}
					}
					break;
					case (ZCL_ON_COMMAND_ID):
					{
						if( (endpoint == 1)&&(sDevice_attr_last.air_condition.on_off == 0) )
						{
							temp_data = 1;
							Control_MCU(&g_sMsgNode,endpoint,CMD_ON_OFF,temp_data);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_ON_OFF_CLUSTER_ID,  false, false, false, false);
//							if(g_sMsgNode.buffer[POLAD_INDEX] == TP_SUCCESS)
//								Write_OnOff_Attr(0,endpoint,&temp_data);
						}
						else if( (endpoint == 2)&&(sDevice_attr_last.floor_heating.on_off == 0) )
						{
							temp_data = 1;
							Control_MCU(&g_sMsgNode,endpoint,CMD_ON_OFF,temp_data);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_ON_OFF_CLUSTER_ID,  false, false, false, false);
						}
						else if( (endpoint == 3)&&(sDevice_attr_last.fan.on_off == 0) )
						{
							temp_data = 1;
							Control_MCU(&g_sMsgNode,endpoint,CMD_ON_OFF,temp_data);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_ON_OFF_CLUSTER_ID,  false, false, false, false);
						}
					}
					break;
					case (ZCL_TOGGLE_COMMAND_ID):
					{
						if( endpoint == 1 )
						{
							if(sDevice_attr_last.air_condition.on_off == 0)
							{
								temp_data = 1;
								Control_MCU(&g_sMsgNode,endpoint,CMD_ON_OFF,temp_data);
								kZclClusterSetPermitReportInfo(endpoint, ZCL_ON_OFF_CLUSTER_ID,  false, false, false, false);
							}
							else{
								temp_data = 0;
								Control_MCU(&g_sMsgNode,endpoint,CMD_ON_OFF,temp_data);
								kZclClusterSetPermitReportInfo(endpoint, ZCL_ON_OFF_CLUSTER_ID,  false, false, false, false);
							}
						}
						else if( endpoint == 2 )
						{
							if(sDevice_attr_last.floor_heating.on_off == 0)
							{
								temp_data = 1;
								Control_MCU(&g_sMsgNode,endpoint,CMD_ON_OFF,temp_data);
								kZclClusterSetPermitReportInfo(endpoint, ZCL_ON_OFF_CLUSTER_ID,  false, false, false, false);
							}
							else{
								temp_data = 0;
								Control_MCU(&g_sMsgNode,endpoint,CMD_ON_OFF,temp_data);
								kZclClusterSetPermitReportInfo(endpoint, ZCL_ON_OFF_CLUSTER_ID,  false, false, false, false);
							}
						}
						else if( endpoint == 3 )
						{
							if(sDevice_attr_last.fan.on_off == 0)
							{
								temp_data = 1;
								Control_MCU(&g_sMsgNode,endpoint,CMD_ON_OFF,temp_data);
								kZclClusterSetPermitReportInfo(endpoint, ZCL_ON_OFF_CLUSTER_ID,  false, false, false, false);
							}
							else{
								temp_data = 0;
								Control_MCU(&g_sMsgNode,endpoint,CMD_ON_OFF,temp_data);
								kZclClusterSetPermitReportInfo(endpoint, ZCL_ON_OFF_CLUSTER_ID,  false, false, false, false);
							}
						}
						break;
					}
					default:

						break;
				}
			}
		}
			break;
		case ZCL_THERMOSTAT_CLUSTER_ID:
		{
			if(cmd->commandId == ZCL_WRITE_ATTRIBUTES_COMMAND_ID)
			{
				switch(attribute_id)
				{
					case ZCL_OCCUPIED_HEATING_SETPOINT_ATTRIBUTE_ID:
					{
						int16_t  datavalue = U16MERG(cmd->buffer[i + 4],cmd->buffer[i + 3]);
						iKonkeAfSelfPrint("##### datavalue (%d),%d \r\n",datavalue,cmd->buffer[i + 5] );
						if((datavalue >= 1600)&&(datavalue <= 3000))
						{
							temp_16_t = datavalue /10;
							Control_MCU(&g_sMsgNode,endpoint,CMD_TARGET_TEMPER,temp_16_t);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_THERMOSTAT_CLUSTER_ID,  false, false, false, false);
//							if(g_sMsgNode.buffer[POLAD_INDEX] == TP_SUCCESS)
//								Write_TargetTemp_Attr(0,endpoint,&datavalue);
							return false;
						}
						else
						{
							emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_INVALID_VALUE);
							return true;
						}
					}
						break;
					case ZCL_SYSTEM_MODE_ATTRIBUTE_ID:
					{
						uint8_t  datavalue = cmd->buffer[i + 3];
						iKonkeAfSelfPrint("##### datavalue (%d),%d \r\n",datavalue,cmd->buffer[i + 3] );
						if( (((datavalue == 3)||(datavalue == 4)||(datavalue == 7)||(datavalue == 8))&&(endpoint == 1)) ||
							(((datavalue == 1)||(datavalue == 2))&&(endpoint == 2)))
						{
							temp_data = datavalue;
							Control_MCU(&g_sMsgNode,endpoint,CMD_SYSTEM,temp_data);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_THERMOSTAT_CLUSTER_ID,  false, false, false, false);
//							if(g_sMsgNode.buffer[POLAD_INDEX] == TP_SUCCESS)
//								Write_SystemMode_Attr(0,endpoint,&datavalue);
							return false;
						}
						else
						{
							emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_INVALID_VALUE);
							return true;
						}
					}
						break;
					case ZCL_LOCAL_TEMPERATURE_CALIBRATION_ATTRIBUTE_ID:
					{
						int8_t  datavalue = cmd->buffer[i + 3];
						iKonkeAfSelfPrint("#####Calibration datavalue (%d),%d \r\n",datavalue,cmd->buffer[i + 3] );
						if(((datavalue >= 0)&&(datavalue <= 0x5A))||((datavalue > -90)&&(datavalue <0)))
						{
							Control_MCU(&g_sMsgNode,endpoint,CMD_CALI_TEMPER,datavalue);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_THERMOSTAT_CLUSTER_ID,  false, false, false, false);
//							if(g_sMsgNode.buffer[POLAD_INDEX] == TP_SUCCESS)
//								Write_CalibrationTemp_Attr(1,endpoint,&datavalue);
							return false;
						}
						else
						{
							emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_INVALID_VALUE);
							return true;
						}
					}
						break;
					default:
						break;
				}
			}
		}
			break;
		case ZCL_FAN_CONTROL_CLUSTER_ID:
		{
			if(cmd->commandId == ZCL_WRITE_ATTRIBUTES_COMMAND_ID)
			{
				if(attribute_id == ZCL_FAN_CONTROL_FAN_MODE_ATTRIBUTE_ID )
				{
					int8_t  datavalue = cmd->buffer[i + 3];
					iKonkeAfSelfPrint("#####fan speed, datavalue (%d),%d \r\n",datavalue,cmd->buffer[i + 3] );
					if( ((datavalue > 0)&&(datavalue <4)) || (datavalue == 5))
					{
						Control_MCU(&g_sMsgNode,endpoint,CMD_WIND_SPEED,datavalue);
						kZclClusterSetPermitReportInfo(endpoint, ZCL_FAN_CONTROL_CLUSTER_ID,  false, false, false, false);
//						if(g_sMsgNode.buffer[POLAD_INDEX] == TP_SUCCESS)
//							Write_FanSpeed_Attr(0,endpoint,&datavalue);
						return false;
					}
					else
					{
						emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_INVALID_VALUE);
						return true;
					}
				}
			}
		}
			break;
		case ZCL_OPT_TUNNEL_CLUSTER_ID:
		{
			if(cmd->commandId == ZCL_WRITE_ATTRIBUTES_COMMAND_ID)
			{
				/*************************命令下发处理**************************************/
				switch(attribute_id)
				{
					case FCC0_ATTR_Child_Lock:
					{
						uint8_t  datavalue = cmd->buffer[i + 3];
						iKonkeAfSelfPrint("#####child lock, datavalue (%d),%d \r\n",datavalue,cmd->buffer[i + 3] );
						if( datavalue >= 0)
						{
							Control_MCU(&g_sMsgNode,endpoint,CMD_CHILD_LOCK,datavalue);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_OPT_TUNNEL_CLUSTER_ID,  false, false, false, false);
//							if(g_sMsgNode.buffer[POLAD_INDEX] == TP_SUCCESS)
//								Write_Child_Lock_Attr(1,endpoint,&datavalue);
							return false;
						}
						else
						{
							emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_INVALID_VALUE);
							return true;
						}

					}
						break;
					case FCC0_ATTR_System_Mode:
					{
						uint8_t  datavalue = cmd->buffer[i + 3];
						iKonkeAfSelfPrint("#####System_Mode, datavalue (%d),%d \r\n",datavalue,cmd->buffer[i + 3] );
						//新风的模式设置
						if( ( (datavalue == 2)||(datavalue == 10) )&&(endpoint == 3) )
						{
							Control_MCU(&g_sMsgNode,endpoint,CMD_SYSTEM,datavalue);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_OPT_TUNNEL_CLUSTER_ID,  false, false, false, false);
							return false;
						}
						else
						{
							emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_INVALID_VALUE);
							return true;
						}
					}
						break;
					case FCC0_ATTR_Delay_Off:
					{
						uint8_t  datavalue = cmd->buffer[i + 3];
						iKonkeAfSelfPrint("#####delay off, datavalue (%d),%d \r\n",datavalue,cmd->buffer[i + 3] );
						if( (datavalue >= 0)&&(datavalue <= 240)){
							Control_MCU(&g_sMsgNode,endpoint,CMD_DELAY_OFF,datavalue);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_OPT_TUNNEL_CLUSTER_ID,  false, false, false, false);
//							if(g_sMsgNode.buffer[POLAD_INDEX] == TP_SUCCESS)
//								Write_Delay_off_Attr(1,endpoint,&datavalue);
							return false;
						}
						else{
							emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_INVALID_VALUE);
							return true;
						}
					}
						break;
					case FCC0_ATTR_Swing_Mode:
					{
						uint8_t  datavalue = cmd->buffer[i + 3];
						iKonkeAfSelfPrint("#####Swing_Mode, datavalue (%d),%d \r\n",datavalue,cmd->buffer[i + 3] );
						if( (datavalue >= 0)&&(datavalue <3)){
							Control_MCU(&g_sMsgNode,endpoint,CMD_WIND_DIRECT,datavalue);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_OPT_TUNNEL_CLUSTER_ID,  false, false, false, false);
//							if(g_sMsgNode.buffer[POLAD_INDEX] == TP_SUCCESS)
//								Write_Wind_Direct_Attr(1,endpoint,&datavalue);
							return false;
						}
						else{
							emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_INVALID_VALUE);
							return true;
						}
					}
						break;
					case FCC0_ATTR_Screen_Saver_Time:
					{
						uint8_t  datavalue = cmd->buffer[i + 3];
						iKonkeAfSelfPrint("#####Screen_Saver_Time, datavalue (%d),%d \r\n",datavalue,cmd->buffer[i + 3] );
						if(datavalue <10){
							Control_MCU(&g_sMsgNode,endpoint,CMD_SCREEN_SAVE,datavalue);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_OPT_TUNNEL_CLUSTER_ID,  false, false, false, false);
//							if(g_sMsgNode.buffer[POLAD_INDEX] == TP_SUCCESS)
//								Write_Screen_Save_Time_Attr(1,endpoint,&datavalue);
							return false;
						}
						else{
							emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_INVALID_VALUE);
							return true;
						}
					}
						break;
					case FCC0_ATTR_Calibration_Temperature:
					{
						int8_t  datavalue = cmd->buffer[i + 3];
						iKonkeAfSelfPrint("#####Calibration datavalue (%d),%d \r\n",datavalue,cmd->buffer[i + 3] );
						if( (((datavalue >= 0)&&(datavalue <= 0x5A))||((datavalue > -90)&&(datavalue <0)))
							&&(g_sDevice_Info.Device_Type == Device_Type_Fan) )
						{
							Control_MCU(&g_sMsgNode,endpoint,CMD_CALI_TEMPER,datavalue);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_OPT_TUNNEL_CLUSTER_ID,  false, false, false, false);
//							if(g_sMsgNode.buffer[POLAD_INDEX] == TP_SUCCESS)
//								Write_CalibrationTemp_Attr(1,endpoint,&datavalue);
							return false;
						}
						else
						{
							emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_INVALID_VALUE);
							return true;
						}
					}
						break;
					case FCC0_ATTR_Filter_Clean_Time:
					{
						uint16_t  datavalue = U16MERG(cmd->buffer[i + 4],cmd->buffer[i + 3]);
						iKonkeAfSelfPrint("#####Screen_Saver_Time, datavalue (%d),%d \r\n",datavalue);
						if( (datavalue > 0)&&(datavalue <0xffff)){
							Control_MCU(&g_sMsgNode,endpoint,CMD_FILTER_CLEAN,datavalue);
							kZclClusterSetPermitReportInfo(endpoint, ZCL_OPT_TUNNEL_CLUSTER_ID,  false, false, false, false);
//							if(g_sMsgNode.buffer[POLAD_INDEX] == TP_SUCCESS)
//								Write_Filter_Clean_Time_Attr(1,endpoint,(uint8_t *)&datavalue);
							return false;
						}
						else{
							emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_INVALID_VALUE);
							return true;
						}
					}
						break;
					default:
						break;
				}
			}
		}
			break;
		default:
			break;

	}
	return false;
}

BIND_Info_Store Bind_Addr_Table = {
		.addr1 = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
		.addr2 = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
		.bind_flag1 = 0,
		.bind_flag2 = 0};
void kSetBindConfigInfo(BIND_Info_Store* info)
{
	halCommonSetToken(TOKEN_BINDIND_ADDR_TABLE, info);
}

BIND_Info_Store kGetBindConfigInfo(void)
{
	halCommonGetToken(&Bind_Addr_Table, TOKEN_BINDIND_ADDR_TABLE);
	return Bind_Addr_Table;
}

void Reset_Binding_Token(void)
{
	memset(Bind_Addr_Table.addr1,0xff,EUI64_SIZE);
	memset(Bind_Addr_Table.addr1,0xff,EUI64_SIZE);
	Bind_Addr_Table.bind_flag1 = Bind_Addr_Table.bind_flag2 = 0;
	kSetBindConfigInfo(&Bind_Addr_Table);
}
EmberEventControl ikkGetBindInfoEventControl;
bool IS_IEE_Addr_Exist(EmberEUI64 addr)
{
	EmberBindingTableEntry result;
	for(int index = 0; index < emberAfGetBindingTableSize(); ++index ) {
		EmberStatus status = emberGetBinding(index, &result);
		if( (status == EMBER_SUCCESS && result.type == EMBER_UNICAST_BINDING )
			&&(result.clusterId == 0xFCC0)
		    &&(memcmp(result.identifier, addr, EUI64_SIZE) == 0))
		{
			return true;
		}
	}
	return false;
}

void ikkGetBindInfoEventHandler(void )
{
	EmberBindingTableEntry result;
	EmberEUI64 g_Eui64GatewayAddr;
	kNwkGetGateWayEui64(g_Eui64GatewayAddr);
	emberEventControlSetInactive(ikkGetBindInfoEventControl);
	for(int index = 0; index < emberAfGetBindingTableSize(); ++index ) {
		EmberStatus status = emberGetBinding(index, &result);
		if( (status == EMBER_SUCCESS && result.type == EMBER_UNICAST_BINDING )&&result.clusterId == 0xFCC0) {
			if(memcmp(result.identifier, g_Eui64GatewayAddr, EUI64_SIZE) != 0)
			{
				iKonkeAfSelfPrint("mac=%x-%x-%x-%x\r\n",result.identifier[0],result.identifier[1],result.identifier[2],result.identifier[3]);
				if((Bind_Addr_Table.bind_flag1 == 0)&&
				   (memcmp(Bind_Addr_Table.addr2,result.identifier,EUI64_SIZE)))
				{
					memcpy(Bind_Addr_Table.addr1,result.identifier,EUI64_SIZE);
					if(result.local != g_sDevice_Info.Device_Type)
					{
						g_sDevice_Info.Binding_Device_SrcEP[0] = result.local;
						Binging_Info_Notify(1,result.local);						//bind
						Bind_Addr_Table.bind_flag1 = 1;
						iKonkeAfSelfPrint("send blind1 info,ep = %d\r\n",g_sDevice_Info.Binding_Device_SrcEP[0]);
						kSetBindConfigInfo(&Bind_Addr_Table);
					}
				}
				if((Bind_Addr_Table.bind_flag2 == 0)&&
				   (memcmp(Bind_Addr_Table.addr1,result.identifier,EUI64_SIZE)))
				{
					memcpy(Bind_Addr_Table.addr2,result.identifier,EUI64_SIZE);
					if(result.local != g_sDevice_Info.Device_Type)
					{
						g_sDevice_Info.Binding_Device_SrcEP[1] = result.local;
						Binging_Info_Notify(1,result.local);						//bind
						Bind_Addr_Table.bind_flag2 = 1;
						kSetBindConfigInfo(&Bind_Addr_Table);
						iKonkeAfSelfPrint("send blind2 info,ep = %d\r\n",g_sDevice_Info.Binding_Device_SrcEP[1]);
					}
				}
			}
			else if((!IS_IEE_Addr_Exist(Bind_Addr_Table.addr1))&&(Bind_Addr_Table.bind_flag1))
			{
				Bind_Addr_Table.bind_flag1 = 0;
				Binging_Info_Notify(0,g_sDevice_Info.Binding_Device_SrcEP[0]);     //unbind
				iKonkeAfSelfPrint("send unblind1 info,ep = %d\r\n",g_sDevice_Info.Binding_Device_SrcEP[0]);
				g_sDevice_Info.Binding_Device_SrcEP[0] = 0;
				memset(Bind_Addr_Table.addr1,0xff,EUI64_SIZE);
				kSetBindConfigInfo(&Bind_Addr_Table);
			}
			else if((!IS_IEE_Addr_Exist(Bind_Addr_Table.addr2))&&(Bind_Addr_Table.bind_flag2))
			{
				Bind_Addr_Table.bind_flag2 = 0;
				Binging_Info_Notify(0,g_sDevice_Info.Binding_Device_SrcEP[1]);     //unbind
				iKonkeAfSelfPrint("send unblind2 info,ep = %d\r\n",g_sDevice_Info.Binding_Device_SrcEP[1]);
				g_sDevice_Info.Binding_Device_SrcEP[1] = 0;
				memset(Bind_Addr_Table.addr2,0xff,EUI64_SIZE);
				kSetBindConfigInfo(&Bind_Addr_Table);
			}
		}
	}

	emberEventControlSetDelayMS(ikkGetBindInfoEventControl, 2000);
}
bool emberAfThermostatClusterSetpointRaiseLowerCallback(uint8_t mode,
                                                        int8_t amount)
{
	int16s   uiCurrent_Target_Temper = 0 ;
	uint8_t endpoint = emberAfCurrentEndpoint();
	EmberAfStatus status = emberAfReadAttribute(endpoint, ZCL_THERMOSTAT_CLUSTER_ID, ZCL_OCCUPIED_HEATING_SETPOINT_ATTRIBUTE_ID, CLUSTER_MASK_SERVER,\
									(uint8_t *)&uiCurrent_Target_Temper, sizeof(uiCurrent_Target_Temper), NULL);
	iKonkeAfSelfPrint("##Receive Adjust Temperture,temper = %d\r\n",uiCurrent_Target_Temper);

	if( status == EMBER_ZCL_STATUS_SUCCESS ) {
		switch(mode)
		{
			case 0:
				uiCurrent_Target_Temper = uiCurrent_Target_Temper + amount*10;
				break;
			case 1:
				uiCurrent_Target_Temper = uiCurrent_Target_Temper - amount*10;
				break;
			case 2:
				uiCurrent_Target_Temper = uiCurrent_Target_Temper + amount*10;
				break;
			default:
				break;
		}
	}

	uiCurrent_Target_Temper = uiCurrent_Target_Temper/10;
	emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_SUCCESS);
	//温度调节范围未16 - 30度
	if((uiCurrent_Target_Temper< 160 ) ||(uiCurrent_Target_Temper >300))
		return true;
	Control_MCU(&g_sMsgNode,endpoint,CMD_TARGET_TEMPER,uiCurrent_Target_Temper);
	uiCurrent_Target_Temper *= 10;
	if(g_sMsgNode.buffer[POLAD_INDEX] == TP_SUCCESS)						//wait test
	{
		kZclClusterSetPermitReportInfo(endpoint, ZCL_THERMOSTAT_CLUSTER_ID,  false, false, false, false);
		emberAfWriteServerAttribute(endpoint, ZCL_THERMOSTAT_CLUSTER_ID, ZCL_OCCUPIED_HEATING_SETPOINT_ATTRIBUTE_ID
				,(uint8_t*)&uiCurrent_Target_Temper,ZCL_INT16S_ATTRIBUTE_TYPE);
		iKonkeAfSelfPrint("####Set Temper Command Control Success !!\r\n");
	}
	return true;
}
uint8_t Get_Device_Endpoint(void)
{
	return g_sDevice_Info.Device_Type;
}
uint8_t  Get_Binding_Device_EP(void)
{
	for(uint8_t i = 1;i<4;i++)
	{
		if((i != Get_Device_Endpoint())&&(kZclNodeIsBindNotGatewayIeeeAddress(i,0xFCC0)) )
		{
			iKonkeAfSelfPrint("####Set Temper Command Control Success !!\r\n");
			return i;
		}
	}
	return 0;
}
void emberAfThermostatClusterServerAttributeChangedCallback(uint8_t endpoint,
                                                            EmberAfAttributeId attributeId)
{

}
void emberAfFanControlClusterServerAttributeChangedCallback(uint8_t endpoint,
                                                            EmberAfAttributeId attributeId)
{


}
EmberStatus Write_Attr(bool Report,uint8_t ep,uint16_t cluster,uint16_t attr, uint8_t* dataPtr,EmberAfAttributeType datatype)
{
	if(Report == 1)
		kZclClusterSetPermitReportInfo(ep, cluster,  true, false, true, false);
	else
		kZclClusterSetPermitReportInfo(ep, cluster,  false, false, false, false);
	EmberStatus status=emberAfWriteServerAttribute(ep, cluster, attr ,(uint8_t*)dataPtr,datatype);
	return status;
}
uint8_t Read_Zigbee_Version(void)
{
	uint8_t version = 0;
	emberAfReadAttribute(1, ZCL_BASIC_CLUSTER_ID, ZCL_APPLICATION_VERSION_ATTRIBUTE_ID, CLUSTER_MASK_SERVER,
									(uint8_t *)&version, sizeof(version), NULL);
	return version;
}
kk_err_t PrivateClusterHandler(EmberAfClusterCommand* cmd)
{
	/*begin add by maozj 20191127 add get gateway EUI64*/
	EmberNodeId sender = emberGetSender();
	// Gateway Eui64 address cache.
	EmberEUI64 eui64GatewayAddr = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
	EmberEUI64 tmpEui64 = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
	EmberStatus status = emberGetSenderEui64(eui64GatewayAddr);
	uint8_t i = cmd->payloadStartIndex;
	uint8_t dataValue = 0;
	uint16_t dataValue1 = 0;

	uint16_t attribute_id = U16MERG(cmd->buffer[i + 1], cmd->buffer[i + 0]);
	uint8_t dataType =  cmd->buffer[i + 2];
	uint8_t endpoint = cmd->apsFrame->destinationEndpoint;
	/***********************************多控处理************************************************/
	if((!cmd->clusterSpecific) && (cmd->direction == (uint8_t)ZCL_DIRECTION_SERVER_TO_CLIENT) \
			&& (!cmd->mfgSpecific))
	{
		uint8_t generalCommandFrame =  cmd->buffer[cmd->payloadStartIndex - 1];
		iKonkeAfSelfPrint("####ON OFF Cluster Report attrId(%02x) seq(%x) payloadStartIndex(%d) generalCommandFrame(0x%x)dataType(%d)epEnable(%d)Payload:\r\n", attribute_id, cmd->seqNum, cmd->payloadStartIndex, generalCommandFrame, dataType, emberAfEndpointIsEnabled(cmd->apsFrame->destinationEndpoint));
		iKonkeAfSelfPrintBuffer(cmd->buffer, cmd->bufLen, true);
		iKonkeAfSelfPrint("\r\n");
		if (generalCommandFrame == ZCL_REPORT_ATTRIBUTES_COMMAND_ID && emberAfEndpointIsEnabled(cmd->apsFrame->destinationEndpoint)){
			if (sender != 0x0000){
				#if 1
					uint8_t index = 0;
					uint8_t storedSequence = kSequenceFindByNodeidAndEp(sender, cmd->apsFrame->destinationEndpoint, &index);

					iKonkeAfSelfPrint("##### LinkageExecute normal sender_node(%02x) index(%d) localep(%d) storedseq(0x%x) senderseq(0x%x) commandId(%d) \r\n", \
							sender, index, cmd->apsFrame->destinationEndpoint, storedSequence, cmd->seqNum, cmd->commandId);
					//过滤seq,以免早的seq改变后来的seq状态
					if (cmd->seqNum <= storedSequence && storedSequence - cmd->seqNum  <= 60) {
						iKonkeAfSelfPrint("#####OnOff LinkageExecute sequence too early, sender_node(%02x) index(%d) localep(%d) storedseq(0x%x) senderseq(0x%x) command(%d)\r\n", \
								sender, index, cmd->apsFrame->destinationEndpoint, storedSequence, cmd->seqNum,  cmd->commandId);
						return true; //直接不执行write属性
					}else {
						kSequenceAdd(sender, cmd->apsFrame->destinationEndpoint, cmd->seqNum);
					}
				#endif
			}
			uint8_t data_type = ZCL_BOOLEAN_ATTRIBUTE_TYPE;
			if(emberAfGetDataSize(dataType) == 1)
			{
			    dataValue  = cmd->buffer[i + 3];
			}
			else if(emberAfGetDataSize(dataType) == 2)
			{
				dataValue1  = U16MERG(cmd->buffer[i + 4], cmd->buffer[i + 3]);
			}
			iKonkeAfSelfPrint("####attribute = (%02x) data1(%x) data2(%d)\r\n",attribute_id,dataValue,dataValue1);

			switch(attribute_id)
			{
				case FCC0_ATTR_Error_Code:
				{
					if(cmd->apsFrame->sourceEndpoint == 1)
						sDevice_attr_new.air_condition.error = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 2)
						sDevice_attr_new.floor_heating.error = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 3)
						sDevice_attr_new.fan.error = dataValue;
					data_type = ZCL_INT8U_ATTRIBUTE_TYPE;
					Control_MCU(&g_sMsgNode,cmd->apsFrame->sourceEndpoint,CMD_ERROR_CODE,dataValue);
				}
					break;
				case FCC0_ATTR_Child_Lock:
				{
					if(cmd->apsFrame->sourceEndpoint == 1)
						sDevice_attr_new.air_condition.lock_flage = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 2)
						sDevice_attr_new.floor_heating.lock_flage = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 3)
						sDevice_attr_new.fan.lock_flage = dataValue;
					data_type = ZCL_BOOLEAN_ATTRIBUTE_TYPE;
					Control_MCU(&g_sMsgNode,cmd->apsFrame->sourceEndpoint,CMD_CHILD_LOCK,dataValue);
				}
					break;
				case FCC0_ATTR_Delay_Off:
				{
					if(cmd->apsFrame->sourceEndpoint == 1)
						sDevice_attr_new.air_condition.power_off_time = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 2)
						sDevice_attr_new.floor_heating.power_off_time = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 3)
						sDevice_attr_new.fan.power_off_time = dataValue;
					data_type = ZCL_INT8U_ATTRIBUTE_TYPE;
					Control_MCU(&g_sMsgNode,cmd->apsFrame->sourceEndpoint,CMD_DELAY_OFF,dataValue);
				}
					break;
				case FCC0_ATTR_Swing_Mode:
				{
					if(cmd->apsFrame->sourceEndpoint == 1)
						sDevice_attr_new.air_condition.air_wind_direct = dataValue;
					data_type = ZCL_ENUM8_ATTRIBUTE_TYPE;
					Control_MCU(&g_sMsgNode,cmd->apsFrame->sourceEndpoint,CMD_WIND_DIRECT,dataValue);
				}
					break;
				case FCC0_ATTR_Screen_Saver_Time:
				{
					if(cmd->apsFrame->sourceEndpoint == 1)
						sDevice_attr_new.air_condition.screen_save_time = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 2)
						sDevice_attr_new.floor_heating.screen_save_time = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 3)
						sDevice_attr_new.fan.screen_save_time = dataValue;
					data_type = ZCL_ENUM8_ATTRIBUTE_TYPE;
					Control_MCU(&g_sMsgNode,cmd->apsFrame->sourceEndpoint,CMD_SCREEN_SAVE,dataValue);
				}
					break;
				case FCC0_ATTR_Filter_Clean_Time:
				{
					if(cmd->apsFrame->sourceEndpoint == 1)
						sDevice_attr_new.air_condition.filter_wash_time = dataValue1;
					else if(cmd->apsFrame->sourceEndpoint == 3)
						sDevice_attr_new.fan.filter_wash_time = dataValue1;
					data_type = ZCL_INT16U_ATTRIBUTE_TYPE;
					Control_MCU(&g_sMsgNode,cmd->apsFrame->sourceEndpoint,CMD_FILTER_CLEAN,dataValue1);
				}
					break;
				case FCC0_ATTR_System_Mode:
				{
					if(cmd->apsFrame->sourceEndpoint == 1)
						sDevice_attr_new.air_condition.system_type = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 3)
						sDevice_attr_new.fan.system_type = dataValue;
					data_type = ZCL_ENUM8_ATTRIBUTE_TYPE;
					Control_MCU(&g_sMsgNode,cmd->apsFrame->sourceEndpoint,CMD_SYSTEM,dataValue);
				}
					break;
				case FCC0_ATTR_Target_Temperature:
				{
					if(cmd->apsFrame->sourceEndpoint == 1)
						sDevice_attr_new.air_condition.set_temperature = dataValue1/10;
					else if(cmd->apsFrame->sourceEndpoint == 2)
						sDevice_attr_new.floor_heating.set_temperature = dataValue1/10;
					data_type = ZCL_INT16S_ATTRIBUTE_TYPE;
					Control_MCU(&g_sMsgNode,cmd->apsFrame->sourceEndpoint,CMD_TARGET_TEMPER,dataValue1/10);
				}
					break;
				case FCC0_ATTR_Current_Temperature:
				{
					if(cmd->apsFrame->sourceEndpoint == 1)
						sDevice_attr_new.air_condition.real_temperature = dataValue1;
					else if(cmd->apsFrame->sourceEndpoint == 2)
						sDevice_attr_new.floor_heating.real_temperature = dataValue1;
					else if(cmd->apsFrame->sourceEndpoint == 3)
						sDevice_attr_new.fan.real_temperature = dataValue1;
					data_type = ZCL_INT16S_ATTRIBUTE_TYPE;
					Control_MCU(&g_sMsgNode,cmd->apsFrame->sourceEndpoint,CMD_CURRENT_TEMPER,dataValue1);
				}
					break;
				case FCC0_ATTR_Calibration_Temperature:
				{
					if(cmd->apsFrame->sourceEndpoint == 1)
						sDevice_attr_new.air_condition.correct_temperature = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 2)
						sDevice_attr_new.floor_heating.correct_temperature = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 3)
						sDevice_attr_new.fan.correct_temperature = dataValue;
					data_type = ZCL_INT8S_ATTRIBUTE_TYPE;
					Control_MCU(&g_sMsgNode,cmd->apsFrame->sourceEndpoint,CMD_CALI_TEMPER,dataValue);
				}
					break;
				case FCC0_ATTR_Fan_Mode:
				{
					if(cmd->apsFrame->sourceEndpoint == 1)
						sDevice_attr_new.air_condition.wind_speed = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 3)
						sDevice_attr_new.fan.wind_speed = dataValue;
					data_type = ZCL_ENUM8_ATTRIBUTE_TYPE;
					Control_MCU(&g_sMsgNode,cmd->apsFrame->sourceEndpoint,CMD_WIND_SPEED,dataValue);
				}
					break;
				case FCC0_ATTR_ON_OFF:
				{
					if(cmd->apsFrame->sourceEndpoint == 1)
						sDevice_attr_new.air_condition.on_off = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 2)
						sDevice_attr_new.floor_heating.on_off = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 3)
						sDevice_attr_new.fan.on_off = dataValue;
					data_type = ZCL_BOOLEAN_ATTRIBUTE_TYPE;
					Control_MCU(&g_sMsgNode,cmd->apsFrame->sourceEndpoint,CMD_ON_OFF,dataValue);
				}
					break;
				case FCC0_ATTR_No_Disturb_Mode:
				{
					if(cmd->apsFrame->sourceEndpoint == 1)
						sDevice_attr_new.air_condition.no_disturb_mode = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 2)
						sDevice_attr_new.floor_heating.no_disturb_mode = dataValue;
					else if(cmd->apsFrame->sourceEndpoint == 3)
						sDevice_attr_new.fan.no_disturb_mode = dataValue;
					data_type = ZCL_BOOLEAN_ATTRIBUTE_TYPE;
					Control_MCU(&g_sMsgNode,cmd->apsFrame->sourceEndpoint,CMD_NO_DISTURB_MODE,dataValue);
				}
					break;
				default:
					break;
			}
			kZclClusterSetPermitReportInfo(cmd->apsFrame->sourceEndpoint, ZCL_OPT_TUNNEL_CLUSTER_ID,  false, false, false, false);
			if(emberAfGetDataSize(data_type) == 1)
				emberAfWriteServerAttribute(cmd->apsFrame->sourceEndpoint,ZCL_OPT_TUNNEL_CLUSTER_ID,attribute_id,
					(uint8_t *)&dataValue,data_type);
			else
				emberAfWriteServerAttribute(cmd->apsFrame->sourceEndpoint,ZCL_OPT_TUNNEL_CLUSTER_ID,attribute_id,
					(uint8_t *)&dataValue1,data_type);
			emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_SUCCESS);
			return KET_OK;
		}
	}
	return KET_FAILED;
}
void print_all_attr_vaule(uint8_t ep)
{
	if(ep == 1)
	{
		iKonkeAfSelfPrint("####Air Condition attribute!!!!\r\n");
		iKonkeAfSelfPrint("####error_code = %x\r\n",sDevice_attr_last.air_condition.error);
		iKonkeAfSelfPrint("####on_off = %x\r\n",sDevice_attr_last.air_condition.on_off);
		iKonkeAfSelfPrint("####system_type = %x\r\n",sDevice_attr_last.air_condition.system_type);
		iKonkeAfSelfPrint("####set_temperature = 0x%2x\r\n",sDevice_attr_last.air_condition.set_temperature);
		iKonkeAfSelfPrint("####real_temperature = 0x%2x\r\n",sDevice_attr_last.air_condition.real_temperature);
		iKonkeAfSelfPrint("####wind_speed = %x\r\n",sDevice_attr_last.air_condition.wind_speed);
		iKonkeAfSelfPrint("####power_off_time = %x\r\n",sDevice_attr_last.air_condition.power_off_time);
		iKonkeAfSelfPrint("####lock_flage = %x\r\n",sDevice_attr_last.air_condition.lock_flage);
		iKonkeAfSelfPrint("####correct_temperature = %x\r\n",sDevice_attr_last.air_condition.correct_temperature);
		iKonkeAfSelfPrint("####screen_save_time = 0x%2x\r\n",sDevice_attr_last.air_condition.screen_save_time);
		iKonkeAfSelfPrint("####air_wind_direct = %x\r\n",sDevice_attr_last.air_condition.air_wind_direct);
		iKonkeAfSelfPrint("####filter_wash_time =0x%2x\r\n",sDevice_attr_last.air_condition.filter_wash_time);
		iKonkeAfSelfPrint("####no_disturb_mode =0x%x\r\n",sDevice_attr_last.air_condition.no_disturb_mode);
	}
	else if(ep == 2)
	{
		iKonkeAfSelfPrint("####Floor_Heatig attribute!!!!\r\n");
		iKonkeAfSelfPrint("####error_code = %x\r\n",sDevice_attr_last.floor_heating.error);
		iKonkeAfSelfPrint("####on_off = %x\r\n",sDevice_attr_last.floor_heating.on_off);
		iKonkeAfSelfPrint("####system_type = %x\r\n",sDevice_attr_last.floor_heating.system_type);
		iKonkeAfSelfPrint("####set_temperature = 0x%2x\r\n",sDevice_attr_last.floor_heating.set_temperature);
		iKonkeAfSelfPrint("####real_temperature = 0x%2x\r\n",sDevice_attr_last.floor_heating.real_temperature);
		iKonkeAfSelfPrint("####power_off_time = %x\r\n",sDevice_attr_last.floor_heating.power_off_time);
		iKonkeAfSelfPrint("####lock_flage = %x\r\n",sDevice_attr_last.floor_heating.lock_flage);
		iKonkeAfSelfPrint("####correct_temperature = %x\r\n",sDevice_attr_last.floor_heating.correct_temperature);
		iKonkeAfSelfPrint("####screen_save_time = 0x%2x\r\n",sDevice_attr_last.floor_heating.screen_save_time);
		iKonkeAfSelfPrint("####no_disturb_mode =0x%x\r\n",sDevice_attr_last.floor_heating.no_disturb_mode);
	}
	else if(ep == 3)
	{
		iKonkeAfSelfPrint("####FAN attribute!!!!\r\n");
		iKonkeAfSelfPrint("####error_code = %x\r\n",sDevice_attr_last.fan.error);
		iKonkeAfSelfPrint("####on_off = %x\r\n",sDevice_attr_last.fan.on_off);
		iKonkeAfSelfPrint("####system_type = %x\r\n",sDevice_attr_last.fan.system_type);
		iKonkeAfSelfPrint("####real_temperature = 0x%2x\r\n",sDevice_attr_last.fan.real_temperature);
		iKonkeAfSelfPrint("####wind_speed = %x\r\n",sDevice_attr_last.fan.wind_speed);
		iKonkeAfSelfPrint("####power_off_time = %x\r\n",sDevice_attr_last.fan.power_off_time);
		iKonkeAfSelfPrint("####lock_flage = %x\r\n",sDevice_attr_last.fan.lock_flage);
		iKonkeAfSelfPrint("####correct_temperature = %x\r\n",sDevice_attr_last.fan.correct_temperature);
		iKonkeAfSelfPrint("####screen_save_time = 0x%2x\r\n",sDevice_attr_last.fan.screen_save_time);
		iKonkeAfSelfPrint("####filter_wash_time =0x%2x\r\n",sDevice_attr_last.fan.filter_wash_time);
		iKonkeAfSelfPrint("####no_disturb_mode =0x%x\r\n",sDevice_attr_last.fan.no_disturb_mode);
	}
}
