


/**
  ******************************************************************************
  * @file    BLV_485_Dev_IrSendFun.c
  * @author  BLW RCU  development Team
  * @version V1.1.1
  * @date    2021-09-07
  * @brief   RS485红外转发发送，接收处理函数
	* @attention 主机强电继电器
	*									输入检测：  无
	*                 输入类型：  无	
	*                 执行函数：  有	
	*            			执行方式：
	*															空调控制 
	*															电视控制
	*															  
  ******************************************************************************
  */
  

#include "BLV_485_Dev_IrSendFun.h"
#include "BLV_485_Dev_ShangJingTianIrSendFun.h"
#include "check_fun.h"
#include "string.h"
#include "SPI_SRAM.h"
#include "usart.h"
#include "DBG.h"
#include "Log_api.h"
#include "includes.h"

/*涉及到的外设头文件开始*/
#include "BLV_485_Dev_TempFun.h"
#include "BLV_485_Dev_C7TFun.h"

/*涉及到的外设头文件结束*/



/*函数指针宏定义*/
typedef void (*DevFunP)(Device_Public_Information_G *BUS_Public, RS485_IR_INFO *RS485IRInfo);	//传递共用数据指针 私有数据指针

/*设备驱动宏定义开始*/
#define RS485_DEV_PRO_FUN_01    DevExistJudgge(Dev_485_IrSend_Polling_Flag, RS485_IrSend_Polling_Init) 
#define RS485_DEV_PRO_FUN_02    DevExistJudgge(Dev_485_IrSend_Active_Flag, RS485_IrSend_Active_Init) 
#define RS485_DEV_PRO_FUN_03    DevExistJudgge(Dev_485_SJTIrSend_Polling_Flag, RS485_SJTIrSend_Polling_Init) 
#define RS485_DEV_PRO_FUN_04    DevExistJudgge(Dev_485_SJTIrSend_Active_Flag, RS485_SJTIrSend_Active_Init) 
#define RS485_DEV_PRO_FUN_05    ((DevFunP)NULL)      
#define RS485_DEV_PRO_FUN_06    ((DevFunP)NULL)          
/*设备驱动宏定义结束*/




#define IRSENDCTRLTIMEOUT        500    //控制超时时间
#define IRSENDASKTIMEOUT         10     //控制回复超时时间

#define IRCtrlSendMax             0x03        //电视控制发送次数  

#define Dev_IRSend_RecvData_Len_Max		0x18	//红外接收数据最长24Byte

/*本地函数调用*/
uint8_t app_rs485IrDis(uint32_t dev_addr); 
uint8_t App_Rs485_Ir_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len);    //
void Ir_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr,uint32_t devaddr,uint16_t DevOutputLoop,uint16_t state); //uint32_t DevaddrIn, uint32_t devaddr,uint16_t state

void IR_TVNo_Ctrl(uint32_t dev_addr, RS485_IR_INFO *RS485IRInfo);
void IR_TV_Ctrl(uint32_t dev_addr, RS485_IR_INFO *RS485IRInfo);
void IR_Air_Ctrl(uint32_t dev_addr, RS485_IR_INFO *RS485IRInfo);



void RS485_IrSend_Polling_Init(Device_Public_Information_G *BUS_Public, RS485_IR_INFO *RS485IRInfo)
{
	BUS_Public->polling_cf = (uint32_t)&app_rs485IrDis;
	BUS_Public->processing_cf = (uint32_t)&App_Rs485_Ir_Check;
}

void RS485_IrSend_Active_Init(Device_Public_Information_G *BUS_Public, RS485_IR_INFO *RS485IRInfo)
{
	BUS_Public->polling_cf = (uint32_t)&app_rs485IrDis;
	BUS_Public->processing_cf = (uint32_t)&App_Rs485_Ir_Check;
}



/*******************************************************************************
* Function Name  : RS485_IrSend_Data_Init 
* Description    : 添加设备到链表 - 轮询设备
* Input          : 
	type	: 添加的设备类型
	addr	: 添加的设备地址
	polling_cf		：设备的轮询数据发送回调函数
	processing_cf	：设备的数据处理回调函数
* Return         : None
*******************************************************************************/
void RS485_IrSend_Data_Init(uint8_t devaddr)
{
	Device_Public_Information_G BUS_Public;
	RS485_IR_INFO RS485IRInfo;       //红外局部变量
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&RS485IRInfo,0,sizeof(RS485_IR_INFO));	
	
	BUS_Public.addr = devaddr;			//设备地址
	BUS_Public.type = DEV_IRTYPE;		//设备类型
	BUS_Public.port = Polling_Port;		//设备发送端口
	BUS_Public.baud = 9600;				//设备发送波特率
	BUS_Public.retry_num = 0x05;	    //设备数据重发次数
	BUS_Public.wait_time = 0x0190;	    //设备数据发送等待回复时间 - 200ms
	BUS_Public.polling_cf = (uint32_t)&app_rs485IrDis;
	BUS_Public.processing_cf = (uint32_t)&App_Rs485_Ir_Check;	
	
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = Ir_Control_State;
	/*初始化红外转发初始值*/
	RS485IRInfo.DevOfflineLast = DEV_IS_LINEUNINIT;
	RS485IRInfo.DevOffline = DEV_IS_LINEUNINIT;
	
	Add_POLL_Device_To_List2(&BUS_Public,(uint8_t *)&RS485IRInfo,sizeof(RS485_IR_INFO));

}

/*******************************************************************************
* Function Name  : RS485_IrSend_Data_For_Logic_Init 
* Description    : 红外转发初始化信息来自逻辑文件
*******************************************************************************/
void RS485_IrSend_Data_For_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len)
{
	Device_Public_Information_G BUS_Public;
	RS485_IR_INFO RS485IRInfo;       //红外局部变量
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&RS485IRInfo,0,sizeof(RS485_IR_INFO));	
	
	BUS_Public.addr = dev_info->addr;			//设备地址
	BUS_Public.type = dev_info->type;		    //设备类型
	BUS_Public.port = dev_info->port;			//设备发送端口
	BUS_Public.baud = dev_info->baud;			//设备发送波特率
	BUS_Public.retry_num = 0x05;				//设备数据重发次数
	BUS_Public.wait_time = 0x0190;				//设备数据发送等待回复时间 - 200ms
	BUS_Public.Protocol = dev_info->version;
	
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = Ir_Control_State;
	/*初始化红外转发初始值*/
	RS485IRInfo.DevOfflineLast = DEV_IS_ONLINE;
	RS485IRInfo.DevOffline = DEV_IS_ONLINE;
	
	switch(BUS_Public.Protocol)
	{
		case	 ENUM_RS485_DEV_PRO_01: if(NULL!=RS485_DEV_PRO_FUN_01) RS485_DEV_PRO_FUN_01(&BUS_Public, &RS485IRInfo);break; //        
		case	 ENUM_RS485_DEV_PRO_02: if(NULL!=RS485_DEV_PRO_FUN_02) RS485_DEV_PRO_FUN_02(&BUS_Public, &RS485IRInfo);break; //          
		case	 ENUM_RS485_DEV_PRO_03: if(NULL!=RS485_DEV_PRO_FUN_03) RS485_DEV_PRO_FUN_03(&BUS_Public, &RS485IRInfo);break;                                 //3
		case	 ENUM_RS485_DEV_PRO_04: if(NULL!=RS485_DEV_PRO_FUN_04) RS485_DEV_PRO_FUN_04(&BUS_Public, &RS485IRInfo);break;
	}

	switch(dev_info->port)
	{
		case Active_Port:   //主动端口
			BUS_Public.port = Active_Port;		//设备发送端口
			Add_ACT_Device_To_List2(&BUS_Public,(uint8_t *)&RS485IRInfo,sizeof(RS485_IR_INFO));
			Act485_Info.device_num += 1;
			break;  
		case Polling_Port:   //轮询端口
			BUS_Public.port = Polling_Port;		//设备发送端口
			Add_POLL_Device_To_List2(&BUS_Public,(uint8_t *)&RS485IRInfo,sizeof(RS485_IR_INFO));   //将填充后的数据添加到链表
			Poll485_Info.device_num += 1;	
			break;
		case Bus_port:    //总线端口
			break;
	}
}


/**
  * @name  			 红外转发轮询控制函数
  * @param  
  *    dev_addr 设备485红外转发起始地址
* @retval      0x00:数据发送成功，0xF0:无数据发送
  * @attention   不管几个空调，485发送位置值占用一个
	*              遍历3个空调是否存在，再判断红外控制标志位是否置一
  */
uint8_t app_rs485IrDis(uint32_t dev_addr)
{
	uint8_t i;
	uint8_t Ret = RS485OCCUPYNOTIME;
	Device_Public_Information_G BUS_Public;	
	RS485_IR_INFO RS485IRInfo;       //红外局部变量

	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);		
	SRAM_DMA_Read_Buff((uint8_t *)&RS485IRInfo,sizeof(RS485_IR_INFO),dev_addr+Dev_Privately);
	
	for(i = 0; i < 4; i++)
	{
		if(RS485IRInfo.AirInfoLast[i] != RS485IRInfo.AirInfo[i])
		{
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"空调参数变化");
			RS485IRInfo.IrCtrlAirFlag = 0x01;
			RS485IRInfo.IrCtrlAirCnt = IRCtrlSendMax;

			if((0x00 == i)&&(0x01 == RS485IRInfo.AirInfo[i])) //由关到开
			{
				RS485IRInfo.AirOpenFlag = 0x00;//置标志位 当接收到红外转发的回复后，还有一次控制的机会   //20250811 关闭此功能测试 李世杰
			}
			memcpy(RS485IRInfo.AirInfoLast, RS485IRInfo.AirInfo, 4);
			break;
		}
	}

	if(0x01 == RS485IRInfo.IrCtrlAirFlag)
	{
		if(0x00 != RS485IRInfo.IrCtrlAirCnt)
		{
			RS485IRInfo.IrCtrlAirCnt--; 
		}
		else
		{
			RS485IRInfo.AirOpenFlag = 0x00;   //本次控制结束，就清特殊标志
			RS485IRInfo.IrCtrlAirFlag = 0x00;
		}		
		IR_Air_Ctrl(dev_addr, &RS485IRInfo);
		
		/*通讯统计记录*/
		BLV_Communication_Record(&RS485IRInfo.comm_record,0x01,0x00);
		Ret = RS485OCCUPYTIME;
	}
	else if(0x01 == RS485IRInfo.IrCtrlTvFlag)
	{
		if(0x00 != RS485IRInfo.IrCtrlTvCnt)
		{
			RS485IRInfo.IrCtrlTvCnt--; 
		}
		else
		{
			RS485IRInfo.IrCtrlTvFlag = 0x00;
		}
		IR_TV_Ctrl(dev_addr, &RS485IRInfo);
		
		/*通讯统计记录*/
		BLV_Communication_Record(&RS485IRInfo.comm_record,0x01,0x00);
		Ret = RS485OCCUPYTIME;
	}
	else if(0x02 == RS485IRInfo.IrCtrlTvFlag)
	{
		if(0x00 != RS485IRInfo.IrCtrlTvCnt)
		{
			RS485IRInfo.IrCtrlTvCnt--; 
		}
		else
		{
			RS485IRInfo.IrCtrlTvFlag = 0x00;
		}
		IR_TVNo_Ctrl(dev_addr, &RS485IRInfo);
		
		/*通讯统计记录*/
		BLV_Communication_Record(&RS485IRInfo.comm_record,0x01,0x00);
		Ret = RS485OCCUPYTIME;
	}
	
	if(RS485OCCUPYTIME == Ret)
	{
		if(RS485IRInfo.DevSendCnt > IRCtrlSendMax)
		{
			if(RS485IRInfo.DevOffline != DEV_IS_OFFLINE)
			{
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Dev_IRSEND LogInfo_Device_Offline...........");
				LOG_Device_Online_Record(DEV_IRTYPE,BUS_Public.addr,LogInfo_Device_Offline);	//记录设备离线SRAM_Read_Byte(dev_addr+Dev_Addr)
			}
			RS485IRInfo.DevOffline = DEV_IS_OFFLINE;   //离线
			if(RS485IRInfo.DevOffline != RS485IRInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
			{
				RS485IRInfo.DevOfflineLast = RS485IRInfo.DevOffline;	//更新上一次在线状态
				Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_OFFLINE);	//保存设备在线状态到SRAM
			}
		}
		else
		{
			RS485IRInfo.DevSendCnt++;   //
		}
		
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&RS485IRInfo, sizeof(RS485_IR_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&RS485IRInfo,sizeof(RS485_IR_INFO),dev_addr+Dev_Privately);
			
	}
	
	
	
	return Ret;
}

/*红外控制电视频道*/
void IR_TVNo_Ctrl(uint32_t dev_addr,RS485_IR_INFO *RS485IRInfo)
{                       //头              len   红外  频道	地址  内容
	uint8_t IrSend[13] = {0x55, 0x55, 0xee, 0x07, 0x04, 0x04, 0x01, 0x01};
	uint8_t port = SRAM_Read_Byte(dev_addr+Dev_port);  

//	RS485IRInfo = CfgDevAdd->DevDataStruct;
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s",__func__);
	
	IrSend[6] = SRAM_Read_Byte(dev_addr+Dev_Addr);          //地址
	IrSend[7] = RS485IRInfo->IrCtrlTvData;  //内容
	NetCRC16(&IrSend[3],7);	
	MCU485_SendString(port,IrSend,10);
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record2(dev_addr+Dev_port,dev_addr+Dev_baud,IrSend,10);	
}

/*红外控制电视*/
void IR_TV_Ctrl(uint32_t dev_addr,RS485_IR_INFO *RS485IRInfo)
{                       //头              len   红外  TV    地址  内容
	uint8_t IrSend[13] = {0x55, 0x55, 0xee, 0x07, 0x04, 0x02, 0x01, 0x01};
	uint8_t port = SRAM_Read_Byte(dev_addr+Dev_port);  
	
//	Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"IR_TV_Ctrl!!\r\n");
	
	IrSend[6] = SRAM_Read_Byte(dev_addr+Dev_Addr);          //地址
	IrSend[7] = RS485IRInfo->IrCtrlTvData;  //内容
	NetCRC16(&IrSend[3],7);	
	
		/*发送数据*/
	MCU485_SendString(port,IrSend,10);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record2(dev_addr+Dev_port,dev_addr+Dev_baud,IrSend,10);
}

/*红外控制空调*/
void IR_Air_Ctrl(uint32_t dev_addr, RS485_IR_INFO *RS485IRInfo)
{                       //头              len   红外  TV    地址  温度   开关机  风速  模式
	uint8_t IrSend[13] = {0x55, 0x55, 0xee, 0x0A, 0x04, 0x01, 0x01, 0x00,   0x00,  0x00, 0x00};
	uint8_t port = SRAM_Read_Byte(dev_addr+Dev_port);  

	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s port:%d fan:%d",__func__, port,RS485IRInfo->AirInfo[3]);
	
	IrSend[6] = SRAM_Read_Byte(dev_addr+Dev_Addr);          //地址
	
	IrSend[7] = RS485IRInfo->AirInfo[2];         //温度
	IrSend[8] = RS485IRInfo->AirInfo[0];         //开关机
	IrSend[9] = RS485IRInfo->AirInfo[3];         //风速
	IrSend[10] = RS485IRInfo->AirInfo[1];        //模式
	
//	IrSend[7] = RS485IRInfo->IrCtrlTvData;  //内容
	NetCRC16(&IrSend[3],10);	
	
		/*发送数据*/
	MCU485_SendString(port,IrSend,13);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record2(dev_addr+Dev_port,dev_addr+Dev_baud,IrSend,13);
}


#define RECDATALENMAX   24   //接收数据最大长度


/**
  * @name  			 红外转发接收处理函数
  * @param  
  *    dev_addr     设备信息地址
  *    data_addr    数据地址
  *    len          数据长度
  * @retval  
	*    0x00:处理成功
	*    0x01:处理失败
  * @attention   不管几个空调，485发送位置值占用一个
	*              遍历3个空调是否存在，再判断红外控制标志位是否置一
  */
uint8_t App_Rs485_Ir_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len)    //
{
	Device_Public_Information_G BUS_Public;
	RS485_IR_INFO RS485IRInfo;       //红外局部变量
	uint8_t data[RECDATALENMAX];    
	uint8_t rev = 0x01;   //默认失败
//	uint8_t Ir_addr = SRAM_Read_Byte(dev_addr+Dev_Addr);
	uint16_t crc_val; // = SRAM_Read_Word(data_addr+len-2);
	uint16_t temp = 0x00;
	
//	uint16_t deal_len = len;
	if(len > RECDATALENMAX)
	{
//		Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"Dev_IRSend Check_Len Fail! \r\n");
		return 0x01;
	}
	memset(data,0x00,RECDATALENMAX);
	
	SRAM_DMA_Read_Buff(data,len,data_addr);   //拷贝482数据	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);		
	
	if(len < 0x07) return 0x01;
	
	if( (data[0] != 0x55) || (data[1] != 0x55) || (data[2] != 0xEE)	)	
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"校验文件头不对!!");
		return rev;				//校验文件头
	}

	if((data[3] + 0x03) != len)	
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"数据长度不对 %d - %d!!",len,temp);
		return rev;					//校验数据长度
	}

	if(data[4] != 0x04)	
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"类型不对:%d!!",temp);
		return rev;				//校验类型
	}

	if(data[6] != BUS_Public.addr)	
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"红外转发地址不对:%d!!",temp);
		return rev;			//校验温控器地址
	}	
	crc_val = data[len-2] + (data[len-1]<<8);
	
	if(crc_val == NetCRC16_2(&data[3],len - 5))            //
	{
		rev = 0x00;        //
		SRAM_DMA_Read_Buff((uint8_t *)&RS485IRInfo,sizeof(RS485_IR_INFO),dev_addr+Dev_Privately);
		
		if(RS485IRInfo.DevOffline == DEV_IS_OFFLINE)
		{
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Dev_TouchSwitch LogInfo_Device_Online...........");
			LOG_Device_Online_Record(DEV_SWI_TYPE,BUS_Public.addr,LogInfo_Device_Online);	//记录设备在线
		}
		
		BLV_Communication_Record(&RS485IRInfo.comm_record,0x02,0x01);		//记录通讯成功
		RS485IRInfo.DevSendCnt = 0x00;
		RS485IRInfo.DevOffline = DEV_IS_ONLINE;    //设备在线
		if(RS485IRInfo.DevOffline != RS485IRInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
		{
			RS485IRInfo.DevOfflineLast = RS485IRInfo.DevOffline;	//更新上一次在线状态
			Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
		}
		switch(data[5])	//读取红外设备类型
		{
			case 0x02:    //电视控制
			case 0x04:    //频道控制
				RS485IRInfo.IrCtrlTvFlag = 0x00;
				/*日志记录*/
				LOG_Device_COMM_Control_Reply_Record(BUS_Public.port, BUS_Public.baud,data,len);
			  break;
			case 0x01:
				if(0x01 == RS485IRInfo.AirOpenFlag)   //
				{
					RS485IRInfo.AirOpenFlag = 0x00;
					//RS485IRInfo.IrCtrlAirFlag = 0x01;         //以当前的空调数据参数加多一次控制
				}
				else
				{
					RS485IRInfo.IrCtrlAirFlag = 0x00;
				}
				/*日志记录*/
				LOG_Device_COMM_Control_Reply_Record(BUS_Public.port, BUS_Public.baud,data,len);
				break;
		}	

		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&RS485IRInfo, sizeof(RS485_IR_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&RS485IRInfo,sizeof(RS485_IR_INFO),dev_addr+Dev_Privately);
							
				
	}
//	else {
//		Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"SRAM_CRC16 Fail ,crc_val:%02X SRAM_CRC16:%02X!!\r\n",crc_val,SRAM_CRC16(data_addr,len - 2));
//		
//	}
	
	return rev;
}


/*获取红外转发在线状态*/
uint8_t Get_BLV485_IrSend_Online_Status(uint32_t devaddr)
{
	RS485_IR_INFO RS485IRInfo;
	SRAM_DMA_Read_Buff((uint8_t *)&RS485IRInfo,sizeof(RS485_IR_INFO),devaddr+Dev_Privately);
	
	if(RS485IRInfo.DevOffline == DEV_IS_ONLINE)
	{
		return 0x01;
	}
	return 0x02;
}





/*******************************************************************************
* Function Name  : Ir_Control_State 
* Description    : 红外转发状态控制函数
* Input          : 
	DevaddrIn  触发设备
	devaddr	: 设备信息地址
	state:
	
* Return         : 
*******************************************************************************/
void Ir_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr,uint32_t devaddr,uint16_t DevOutputLoop,uint16_t state) //uint32_t DevaddrIn, uint32_t devaddr,uint16_t state
{
	uint8_t TVOnOffState;   //执行方式
	uint8_t TVCont;         //执行内容
	Device_Public_Information_G BUS_Public;	
	RS485_IR_INFO RS485IRInfo;
//	uint16_t crc_val = 0x00;
//	uint16_t temp = 0x00;
	
	if(devaddr == 0x00)	return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);		
	SRAM_DMA_Read_Buff((uint8_t *)&RS485IRInfo,sizeof(RS485_IR_INFO),devaddr+Dev_Privately);
	
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"RS485IRInfo.AirInfo start:", RS485IRInfo.AirInfo, 4);
	
	if((NULL != CfgDevAddIn) && (DEV_RS485_TEMP == SRAM_Read_Byte(CfgDevAddIn+Dev_Type)))//温控器触发
	{
		RS485_TEMP_INFO Rs485Tem;
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"温控器控制红外转发");
		SRAM_DMA_Read_Buff((uint8_t *)&Rs485Tem,sizeof(RS485_TEMP_INFO),CfgDevAddIn+Dev_Privately);
		
		switch(Rs485Tem.TemState.on_off)
		{
			case 0x01:
				RS485IRInfo.AirInfo[0] = 0x01;   //1开
				break;
			case 0x00:
				RS485IRInfo.AirInfo[0] = 0x00;   //0关
				break;
		}
		
		switch(Rs485Tem.TemState.mode)
		{
			case TEMP_COLD:RS485IRInfo.AirInfo[1] = 0; break;      //制冷
			case TEMP_HOT:RS485IRInfo.AirInfo[1] = 1; break;      //制热
			case TEMP_WIND:RS485IRInfo.AirInfo[1] = 2; break;      //送风
		}
//		RS485IRInfo.AirInfo[2] = Rs485Tem.TemState.set_t;
		if(Rs485Tem.TemState.set_t==0)
		{
			Rs485Tem.TemState.set_t=31;
			RS485IRInfo.AirInfo[2] = Rs485Tem.TemState.set_t+1;
		}
		else
		RS485IRInfo.AirInfo[2] = Rs485Tem.TemState.set_t;
		
		if(TEMP_FANAUTO == Rs485Tem.TemState.fan)   //风速自动
		{
			RS485IRInfo.AirInfo[3] = 2;   //中速
		}
		else         //其他情况
		{
			RS485IRInfo.AirInfo[3] = Rs485Tem.TemState.fan;  //风速
		}
	}
	else if(NULL != state) //其他设备触发，或者触发设备为空
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"电视触发");
		RS485IRInfo.IrCtrlTvFlag = 0x01;  //电视控制标志  每次操作都会控制电视，比如开关机 音量加减 频道加减 上下左右
		RS485IRInfo.IrCtrlTvCnt = IRCtrlSendMax;
		
		TVOnOffState = (state)&0x00ff;      //保存开关机   1开 2关
		TVCont = ((state>>8)&0x00ff);
		
//		if(TVCont == IR_NOT_EXIST)           //不存在
		{
	//		RS485IRInfo.MusicBackSendCnt = MusicBackSendMax;		
			switch(TVOnOffState)
			{
				case 0x00:  //执行方式不存在
					if(IR_NOT_EXIST != TVCont)RS485IRInfo.IrCtrlTvData = TVCont; //执行内容赋值					
					break;
				case 0x01:  //开机 
				case 0x02:  //关机
					if(IR_NOT_EXIST == TVCont)
					{
						RS485IRInfo.IrTvState = TVOnOffState;  //保存开关机
						RS485IRInfo.IrCtrlTvData = IR_Power;                 //开机和关机发送的内容是一样的
					}
					else
					{
						RS485IRInfo.IrCtrlTvData = TVCont; //执行内容赋值
					}
					break;
				case 0x04:  //频道号下发
					
					RS485IRInfo.IrCtrlTvFlag = 0x02;
					RS485IRInfo.IrCtrlTvData = TVCont; //执行内容赋值
					break;
				default:    //
					break;
			}
		}
//		else   //存在
//		{
//			RS485IRInfo.IrCtrlTvData = TVCont; //执行内容赋值
//		}
	}
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"RS485IRInfo.AirInfo end:", RS485IRInfo.AirInfo, 4);
	
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&RS485IRInfo, sizeof(RS485_IR_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&RS485IRInfo,sizeof(RS485_IR_INFO),devaddr+Dev_Privately);
				
	
	
}

/*******************************************************************************
* Function Name  : Ir_Control_State2 
* Description    : 红外转发状态控制函数,旧版温控器调用
* Input          : 
	DevaddrIn  触发设备
	devaddr	: 设备信息地址
	state:
	
* Return         : 
*******************************************************************************/
//void Ir_Control_State2(uint32_t CfgDevAddIn, uint16_t DevInputAddr,uint32_t devaddr,uint16_t DevOutputLoop,uint16_t state) //uint32_t DevaddrIn, uint32_t devaddr,uint16_t state
//{
//	uint8_t TVOnOffState;
//	RS485_IR_INFO RS485IRInfo;
//	uint16_t crc_val = 0x00;
//	uint16_t temp = 0x00;
//	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"RS485IRInfo.AirInfo start:\r\n", RS485IRInfo.AirInfo, 4);
//	
//	if(devaddr == 0x00)	return;
//	
//	SRAM_DMA_Read_Buff((uint8_t *)&RS485IRInfo,sizeof(RS485_IR_INFO),devaddr+Dev_Privately);
//	
//	if((NULL != CfgDevAddIn) && (DEV_RS485_TEMP == SRAM_Read_Byte(CfgDevAddIn+Dev_Type)))//温控器触发
//	{
//		RS485_TEM_INFO Rs485Tem;
//		Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"温控器触发\r\n");
//		SRAM_DMA_Read_Buff((uint8_t *)&Rs485Tem,sizeof(RS485_TEM_INFO),CfgDevAddIn+Dev_Privately);
//		
//		switch(Rs485Tem.on_off)
//		{
//			case 0x01:
//				RS485IRInfo.AirInfo[0] = 0x01;   //1开
//				break;
//			case 0x02:
//				RS485IRInfo.AirInfo[0] = 0x00;   //0关
//				break;
//		}
//		
//		switch(Rs485Tem.mode)
//		{
//			case TEMP_COLD:RS485IRInfo.AirInfo[1] = 0; break;      //制冷
//			case TEMP_HOT:RS485IRInfo.AirInfo[1] = 1; break;      //制热
//			case TEMP_WIND:RS485IRInfo.AirInfo[1] = 2; break;      //送风
//		}
//		RS485IRInfo.AirInfo[2] = Rs485Tem.set_t;
//		
//		if(TEMP_FANAUTO == Rs485Tem.fan)   //风速自动
//		{
//			RS485IRInfo.AirInfo[3] = 2;   //中速
//		}
//		else         //其他情况
//		{
//			RS485IRInfo.AirInfo[3] = Rs485Tem.fan;  //风速
//		}
//	}
//	else if(NULL != state) //其他设备触发，或者触发设备为空
//	{
//		Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"电视触发\r\n");
//		RS485IRInfo.IrCtrlTvFlag = 0x01;  //电视控制标志  每次操作都会控制电视，比如开关机 音量加减 频道加减 上下左右
//		RS485IRInfo.IrCtrlTvCnt = IRCtrlSendMax;

//		if(((state>>8)&0x00ff) == IR_NOT_EXIST)           //不存在
//		{
//	//		RS485IRInfo.MusicBackSendCnt = MusicBackSendMax;		
//			TVOnOffState = (state)&0x00ff;      //保存开关机   1开 2关
//			switch(TVOnOffState)
//			{
//				case 0x01:  //开机 
//				case 0x02:  //关机
//					RS485IRInfo.IrTvState = TVOnOffState;  //保存开关机
//					RS485IRInfo.IrCtrlTvData = IR_Power;                 //开机和关机发送的内容是一样的
//				break;
//				default:    //
//					break;
//			}
//		}
//		else   //存在
//		{
//			RS485IRInfo.IrCtrlTvData = (state>>8)&0x00ff; //执行内容赋值
//		}
//	}
//	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"RS485IRInfo.AirInfo end:\r\n", RS485IRInfo.AirInfo, 4);
//	/*将数据保存进入SRAM中*/
//	SRAM_DMA_Write_Buff((uint8_t *)&RS485IRInfo,sizeof(RS485_IR_INFO),devaddr+Dev_Privately);
//	
//	/*计算校验值*/
//	SRAM_Write_Byte(0x00,devaddr+Dev_Check);
//	temp = sizeof(RS485_IR_INFO) + Dev_Privately;
//	crc_val = Dev_CheckSum(devaddr,temp);
//	SRAM_Write_Byte(crc_val,devaddr+Dev_Check);		//校验值重新写入	
//	
//	
//}




