/******************/
/**中弘自定义协议**/
/******************/
#include "air_condition.h"
#include "key.h"
#include "ZH_protocol.h"
#include "display_common.h"
#include "BL55072A.h"
/************************************************************************************************
时间:2019.9.16
作者:吴明军
功能:把接收到的品牌编号转化成opearoteID，productID数据赋予中间变量
参数: 传递opearoteID，productID协议的第三位，此位表示品牌编号
返回值:无
*****************************************************************************************************/
extern Lcd_Status_Info_t  LCDShowTemp_state_t;
u32 use_oprearoteID = 0xff, use_productID = 0xff;

#define SIMULATOR     				0 			//模拟器
#define DAIKIN            			1			//品牌 :大金
#define HITACHI          			2			//品牌 :日立
#define TOSHIBA         			3			//东芝
#define MITSUBISHI    				4			//三菱重工
#define MITSUBISHI_ELECTRIC   		5			//三菱电机
#define GREE                       	6			//格力
#define MIDEA_TRADEMARK        		7			//美的
#define DAIKIN_MX                   8			//大金MX
#define HITACHI_US                  9			//日立US
#define DAIKIN_NEW_WIND       		10			//大金新风
#define HITACHI_NEW_WIND     		11			//日立新风
#define PANASONIC_NEW_WIND 			12			//松下新风
#define GREE_A_PAD                  13			//格力A 型风管
#define YORK                        14			//约克
#define MCQUARY                     15			//麦克斯韦尔
#define GREE_HOTEL_PAD          	16			//格力酒店风管
#define TRANE                       17			//特灵一拖一
#define HAIER                       18			//海尔一拖一
#define MIDEA_COOL_WIND        		19			//美的酷风
#define GREE_C1_PAD                 20			//格力C1风管
#define GREE_NEW_WIND_AREFACTION 	21			//格力新风除湿
void transfer_opearoteID_productID(u8 brand ,u8 type)
{
	Oled_Display_Info.Air_Device.air_product_id  = brand;
	Oled_Display_Info.Air_Device.air_cond_type   = type;
}
extern KEY_OPERATE  sKey_Operation[3];
static void air_report_process(void)
{
    /*读取oprearoteID,productID*/
	if(ZH_protocol_i->poll_check_receive[SWITCH] == 0xAA)//带此标志的专为读取oprearoteID,productID，故之后不再次解读
	{
		transfer_opearoteID_productID(ZH_protocol_i->poll_check_receive[MODE],ZH_protocol_i->poll_check_receive[TEMPERATURE]);//此命令中此处存放的是品牌
	}
	else
	{
		/* 模式状态改变 */
		if(ZH_protocol_i->poll_check_receive[MODE] != 0xFF)
		{
				if((ZH_protocol_i->poll_check_receive[MODE] == HEATINF && Oled_Display_Info.Air_Device.system_type != SYSTEM_MODE_HEATING) ||
					(ZH_protocol_i->poll_check_receive[MODE] == REFRIGERATION && Oled_Display_Info.Air_Device.system_type != SYSTEM_MODE_COOLING) ||
					(ZH_protocol_i->poll_check_receive[MODE] == SUPPLY_AIR && Oled_Display_Info.Air_Device.system_type != SYSTEM_MODE_AIR) ||
					(ZH_protocol_i->poll_check_receive[MODE] == XERANSIS && Oled_Display_Info.Air_Device.system_type != SYSTEM_MODE_DRY))
				{
					Oled_Display_Info.Air_Device.system_type = air_condition_mode2Standard_mode(ZH_protocol_i->poll_check_receive[MODE]);
				
					if (Oled_Display_Info.Air_Device.on_off)   //开机状态
					{
						Displayt_System_Mode(Oled_Display_Info.Air_Device.system_type);
						state_change_report(OPCODE_GENERAL_REPORT_STATUS,1);	//状态改变通知					
					}	
				}
		}
			
		/* 风速状态改变 */
		if(ZH_protocol_i->poll_check_receive[WIND_SPEED] != 0xFF)
		{
			if((ZH_protocol_i->poll_check_receive[WIND_SPEED] == AUTOMATIC && Oled_Display_Info.Air_Device.wind_speed != WIND_SPEED_AUTO) ||
				(ZH_protocol_i->poll_check_receive[WIND_SPEED] == LOW_SPEED && Oled_Display_Info.Air_Device.wind_speed != WIND_SPEED_LOW) ||
				(ZH_protocol_i->poll_check_receive[WIND_SPEED] == INTERMEDIATE_SPEED && Oled_Display_Info.Air_Device.wind_speed != WIND_SPEED_MID) ||
				(ZH_protocol_i->poll_check_receive[WIND_SPEED] == HIGH_SPEED && Oled_Display_Info.Air_Device.wind_speed != WIND_SPEED_HIGH))
			{
				Oled_Display_Info.Air_Device.wind_speed = air_condition_fan_speed2Standard_fan_speed(ZH_protocol_i->poll_check_receive[WIND_SPEED]);
				if (Oled_Display_Info.Air_Device.on_off)   //开机状态
				{
					state_change_report(OPCODE_GENERAL_REPORT_STATUS,1);	//状态改变通知	
				}					
			}
		}

		/* 温度状态改变 */
		if( (ZH_protocol_i->poll_check_receive[TEMPERATURE] !=  (Oled_Display_Info.Air_Device.set_temperature/10) ) && 
			(ZH_protocol_i->poll_check_receive[TEMPERATURE] != 0xFF))
		{
			Oled_Display_Info.Air_Device.set_temperature = 10*ZH_protocol_i->poll_check_receive[TEMPERATURE];
			
			if (Oled_Display_Info.Air_Device.on_off)   //开机状态
			{
				display_set_temp(0,Oled_Display_Info.Air_Device.set_temperature,0);
				state_change_report(OPCODE_GENERAL_REPORT_STATUS,1);// 上报状态
			}
			
		}

		/* 开关状态改变 */
		if((ZH_protocol_i->poll_check_receive[SWITCH] != Oled_Display_Info.Air_Device.on_off) && (ZH_protocol_i->poll_check_receive[SWITCH] != 0xFF))
		{
			Oled_Display_Info.Air_Device.on_off = ZH_protocol_i->poll_check_receive[SWITCH];
			//printf("on off =%d\r\n",Oled_Display_Info.Air_Device.on_off);
			if(Oled_Display_Info.Air_Device.on_off)
				Control_OnOff_handler(AIR_CONDITION,1);
			else
			{
				BL55072A_Clear();
				Control_OnOff_handler(AIR_CONDITION,0);
			}
			state_change_report(OPCODE_GENERAL_REPORT_STATUS,1);	//状态改变通知	
		}
		#if  (DEV_TYPE == 0) &&(DEVICE_TYPE_AIR_CONDITION ==1)
		if(ZH_protocol_i->poll_check_receive[STATE] &0x10)
		{
			Oled_Display_Info.Air_Device.air_master_slavery_mode = 1;
			if(!IsLcdEnterScreenSave()&&(Oled_Display_Info.Air_Device.power_off_time ==0)&&(Oled_Display_Info.Air_Device.on_off))
			{
				LCDShowTemp_state_t.LCD_place = H_CHAR_DISPLAY;	
				LCDShowTemp_state_t.LCD_state = ON;	
				LcdShowCTR(LCDShowTemp_state_t);
			}
		}
			
		#endif
			
	}
}
void ZH_msg_deal(void)
{
	static int i = BUF_COUNT;
	
	for(i = 0; i < BUF_COUNT; i++)
	{
		if(ZH_protocol_i->buf_handle_flag[i] == 1)
		{
			if(ZH_protocol_i->receive_buf[i][ZH_protocol_i->ops_type] == ZH_protocol_i->send_buf[ZH_protocol_i->ops_type])
			{
					ZH_protocol_i->send_times = 0;
			}			
			
			air_report_process();
			
			ZH_protocol_i->buf_handle_flag[i] = 0;
		}
	}
}
/****************************中宏协议相关接口***********************************************/
static void ZH_send_onoff_interface(u8 onoff)
{
		ZH_protocol_i->send(SWITCH, onoff);
		
		ZH_protocol_i->send_times = 2;
		
		task_comps[9].task_switch = SWITCH_ON;
}
static void ZH_send_set_temper_interface(u8 temp)
{
		ZH_protocol_i->send(TEMPERATURE, temp);
		
		ZH_protocol_i->send_times = 2;
		
		task_comps[9].task_switch = SWITCH_ON;
}
u8   air_condition_mode2Standard_mode(uint8_t mode)
{
	u8 standard_mode = 0;
	switch(mode)
	{
		case HEATINF:
			standard_mode = SYSTEM_MODE_HEATING;
			break;
		case REFRIGERATION:
			standard_mode = SYSTEM_MODE_COOLING;
			break;
		case XERANSIS:
			standard_mode = SYSTEM_MODE_DRY;
			break;
		case SUPPLY_AIR:
			standard_mode = SYSTEM_MODE_AIR;
			break;
	}

	return standard_mode;
}
u8   Standard_mode2Air_condition_mode(uint8_t mode)
{
	u8 standard_mode = 0;
	if(mode == SYSTEM_MODE_COOLING)
	{
		standard_mode = REFRIGERATION;
	}
	else if(mode == SYSTEM_MODE_HEATING)
	{
		standard_mode = HEATINF;
	}
	else if(mode == SYSTEM_MODE_DRY)
	{
		standard_mode = XERANSIS;
	}
	else if(mode == SYSTEM_MODE_AIR)
	{
		standard_mode = SUPPLY_AIR;
	}

	return standard_mode;
}
u8   air_condition_fan_speed2Standard_fan_speed(uint8_t fan_speed)
{
	u8 standard_fan_speed = 0;
	switch(fan_speed)
	{
		case AUTOMATIC:
			standard_fan_speed = WIND_SPEED_AUTO;
			break;
		case LOW_SPEED:
		case INTERMEDIATE_SPEED:
		case HIGH_SPEED:
			standard_fan_speed = fan_speed;
			break;
	}
	return standard_fan_speed;
}
u8   Standard_fan_speed2Air_condition(uint8_t fan_speed)
{
	u8 standard_fan_speed = 0;
	switch(fan_speed)
	{
		case WIND_SPEED_AUTO:
			standard_fan_speed = AUTOMATIC;
			break;
		case LOW_SPEED:
		case INTERMEDIATE_SPEED:
		case HIGH_SPEED:
			standard_fan_speed = fan_speed;
			break;
	}
	return standard_fan_speed;
}
static void ZH_send_mode_interface(uint8_t state)
{
		u8 work_mode = 0;
	
		if(state == SYSTEM_MODE_COOLING)
		{
			work_mode = REFRIGERATION;
		}
		else if(state == SYSTEM_MODE_HEATING)
		{
			work_mode = HEATINF;
		}
		else if(state == SYSTEM_MODE_DRY)
		{
			work_mode = XERANSIS;
		}
		else if(state == SYSTEM_MODE_AIR)
		{
			work_mode = SUPPLY_AIR;
		}
		
		ZH_protocol_i->send(MODE, work_mode);
		
		ZH_protocol_i->send_times = 2;
		
		task_comps[9].task_switch = SWITCH_ON;
}

static void ZH_send_wind_speed_interface(uint8_t state)
{
		u8 wind_speed = 0;
	
		if(state == WIND_SPEED_LOW)
		{
			wind_speed = LOW_SPEED;
		}
		else if(state == WIND_SPEED_MID)
		{
			wind_speed = INTERMEDIATE_SPEED;
		}
		else if(state == WIND_SPEED_HIGH)
		{
			wind_speed = HIGH_SPEED;
		}
		else if(state == WIND_SPEED_AUTO)
		{
			wind_speed = AUTOMATIC;
		}
		
		ZH_protocol_i->send(WIND_SPEED, wind_speed);
		
		ZH_protocol_i->send_times = 2;
		
		task_comps[9].task_switch = SWITCH_ON;
}
static void ZH_send_wind_direct_interface(uint8_t wind_direct)
{	
		if(wind_direct == WIND_DIRECT_DOWN)
		{
			ZH_protocol_i->send_buf[WIND_DIRECTION]  = ANGLE_30;
		}
		else if(wind_direct == WIND_DIRECT_MIDDLE)
		{
			ZH_protocol_i->send_buf[WIND_DIRECTION]  = ANGLE_45;
		}
		else if(wind_direct == WIND_DIRECT_UP)
		{
			ZH_protocol_i->send_buf[WIND_DIRECTION]  = ANGLE_60;
		}
		
		ZH_protocol_i->send(WIND_DIRECTION, wind_direct);
		
		ZH_protocol_i->send_times = 2;
		
		task_comps[9].task_switch = SWITCH_ON;
}

void Control_AIR_CONDITION_By_RS485(u8 cmd_type,u16 data)
{
	u8 temp = 0;
	switch(cmd_type)
	{
		case AIR_CMD_ONOFF:
		{
			temp =  (u8)data;
			ZH_send_onoff_interface(temp);
		}
			break;
		case AIR_CMD_SYSTEM_MODE:
		{
			temp =  (u8)data;
			ZH_send_mode_interface(temp);
		}
			break;
		case AIR_CMD_FAN_SPEED:
		{
			temp =  (u8)data;
			ZH_send_wind_speed_interface(temp);
		}
			break;
		case AIR_CMD_FAN_DIRECT:
		{
			temp =  (u8)data;
			ZH_send_wind_direct_interface(temp);
		}
			break;
		case AIR_CMD_SET_TEMPER:
		{
			temp = (u8)(data/10);
			ZH_send_set_temper_interface(temp);
			break;
		}
	}
}
/*******************************中宏协议相关接口********************************************/
