
/**
  ******************************************************************************
  * @file    BLV_485_Dev_TempT1Fun.c
  * @author  BLW RCU  development Team
  * @version V1.0.0
  * @date    2022年03月18日
  * @brief   宝来威T1温控器       
	* @log  202205111030  SongHan  温控器接收控制命令 Rs485TempT1.TemState.on_off = Rs485TempT1.TemStateCtrl.on_off         
	*       202205121850  SongHan  温控器发送函数执行 温控器状态为未确定  当轮询温控器收到回复之后，温控器为在线
  ******************************************************************************
  */

#include "BLV_485_Dev_TempT1Fun.h"
#include "string.h"
#include "SPI_SRAM.h"
#include "usart.h"
#include "check_fun.h"
#include "DBG.h"
#include "rw_logging.h"
#include "SRAM_Variable_Address.h"
#include "BLV_NET_Function.h"
#include "rtc.h"

#include "includes.h"

/*T1温控器驱动代码*/

#define REPEATSENDTIMEMAX         0x03  //重发次数上限



void BLWOut_TempT1Ctrl(Device_Public_Information_G *BUS_Public,RS485_TEMP_INFO *Rs485Tem,uint8_t CtrlWay);
void BLWOut_tempT1Cycle(Device_Public_Information_G *BUS_Public);   

uint8_t BLWOut_Rs485_TempT1_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len);
uint8_t BLWOut_TempT1CycleCtrl(uint32_t dev_addr);
//uint8_t TemSingleJudge(uint32_t CfgDevAdd, RS485_Tem_Ctrl_ptr Rs485TemCtrl, RS485_Tem_CycleCtrl_ptr Rs485TemCycle);
//void Dev_TEMPCTRL_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t CfgDevAddOut, uint16_t DevOutputLoop, uint16_t DevOutputType);
//uint8_t Dev_TEMPCTRL_InType_Get(uint32_t CfgDevAddIn, uint16_t DevInputLoop, uint16_t DevInputType);

/*******************************************************************************
* Function Name  : BLWOut_RS485_TempT1_Data_Init 
* Description    : 添加设备到链表 - 轮询设备
* Input          : 
	type	: 添加的设备类型
	addr	: 添加的设备地址
	polling_cf		：设备的轮询数据发送回调函数
	processing_cf	：设备的数据处理回调函数
* Return         : None
*******************************************************************************/
void BLWOut_RS485_TempT1_Data_Init(Device_Public_Information_G *BUS_Public, RS485_TEMP_INFO *Rs485TempT1)
{

	BUS_Public->polling_cf = (uint32_t)&BLWOut_TempT1CycleCtrl;
	BUS_Public->processing_cf = (uint32_t)&BLWOut_Rs485_TempT1_Check;
	
	Rs485TempT1->ValveSameFlag = 0x01;      //冷热阀一致   2023-04-17
	
	Rs485TempT1->DevPort = Polling_Port;     //2024-11-05    轮训端口设备
	
}

/*******************************************************************************
* Function Name  : BLWOut_RS485_TempT1D_Data_Init 
* Description    : 添加设备到链表 - 轮询设备
* Input          : 
	type	: 添加的设备类型
	addr	: 添加的设备地址
	polling_cf		：设备的轮询数据发送回调函数
	processing_cf	：设备的数据处理回调函数
* Return         : None
*******************************************************************************/
void BLWOut_RS485_TempT1D_Data_Init(Device_Public_Information_G *BUS_Public, RS485_TEMP_INFO *Rs485TempT1)
{

	BUS_Public->polling_cf = (uint32_t)&BLWOut_TempT1CycleCtrl;
	BUS_Public->processing_cf = (uint32_t)&BLWOut_Rs485_TempT1_Check;
	
	Rs485TempT1->ValveSameFlag = 0x02;      //冷热阀不一致   2023-04-17
	
	Rs485TempT1->DevPort = Polling_Port;     //2024-11-05    轮训端口设备
	
}


/*******************************************************************************
* Function Name  : BLWOut_RS485_TempT1_Activ_Init 
* Description    : 添加设备到链表 - 主动设备
* Input          : 
	type	: 添加的设备类型
	addr	: 添加的设备地址
	polling_cf		：设备的轮询数据发送回调函数
	processing_cf	：设备的数据处理回调函数
* Return         : None
*******************************************************************************/
void BLWOut_RS485_TempT1_Activ_Init(Device_Public_Information_G *BUS_Public, RS485_TEMP_INFO *Rs485TempT1)
{

	BUS_Public->polling_cf = (uint32_t)&BLWOut_TempT1CycleCtrl;
	BUS_Public->processing_cf = (uint32_t)&BLWOut_Rs485_TempT1_Check;
	
	Rs485TempT1->ValveSameFlag = 0x02;      //冷热阀不一致   2023-04-17
	
	Rs485TempT1->DevPort = Active_Port;     //2024-11-05    主动端口设备
	
}

/*******************************************************************************
* Function Name  : BLWOut_TempT1CycleCtrl 
* Description    : BLWOut温控器控制发送函数
*******************************************************************************/
void BLWOut_TempT1Ctrl(Device_Public_Information_G *BUS_Public,RS485_TEMP_INFO *Rs485Tem,uint8_t CtrlWay)
{
	                       //      头         LEN   Type  Addr  功能  内容
	uint8_t CtrlSend[10] = {0x55, 0x55, 0xee, 0x07, 0x03, 0x01, 0x00, 0x00};
	
//	uint8_t port = SRAM_Read_Byte(dev_addr+Dev_port);  
	CtrlSend[5] = BUS_Public->addr;//SRAM_Read_Byte(dev_addr+Dev_Addr);  
	
	switch(CtrlWay)
	{
		case 0: //开关机
			CtrlSend[6] = 0x01;
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s:开关机状态:%d", __func__,Rs485Tem->TemStateCtrl.on_off);
		
			switch(Rs485Tem->TemStateCtrl.on_off)   //开关机
			{
				case TEMP_STATE_ON:        //开机
					CtrlSend[7] = 0x01;
					break;
				case TEMP_STATE_OFF:       //关机
					CtrlSend[7] = 0x00;		
					break;
			}	
		  break;
		case 1: //模式
			CtrlSend[6] = 0x02;
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s:模式状态:%d",__func__, Rs485Tem->TemStateCtrl.mode);
			switch(Rs485Tem->TemStateCtrl.mode)
			{
				case TEMP_COLD: CtrlSend[7] = 0x01; break;    //冷
				case TEMP_HOT: CtrlSend[7] = 0x02; break;		//热
				case TEMP_WIND: CtrlSend[7] = 0x03; break;    //通风
				
			}
		  break;
		case 3: //风速
			CtrlSend[6] = 0x04;
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s:风速状态:%d", __func__,Rs485Tem->TemStateCtrl.fan);
			switch(Rs485Tem->TemStateCtrl.fan)
			{
//				case 0: CtrlSend[5] = 0x03; break;   //停止
				case TEMP_LOW: CtrlSend[7] = 0x03; break;   //低速
				case TEMP_MID: CtrlSend[7] = 0x02; break;   //中速
				case TEMP_HIGH: CtrlSend[7] = 0x01; break;   //高速
				case TEMP_FANAUTO: CtrlSend[7] = 0x00; break;   //自动
			}
		  break;
		case 2: //设置温度
			CtrlSend[6] = 0x03; 
		  CtrlSend[7] = TEMTEMPCONVER(Rs485Tem->TemStateCtrl.set_t);  //
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s:温度:%d", __func__,CtrlSend[7]);			
		  break;
	}
	
	NetCRC16(&CtrlSend[3],7);
//	Rs485Tem->DevOffline = DEV_IS_LINEUNINIT;    //设备状态未确定

	/*发送数据*/
	MCU485_SendString(BUS_Public->port,CtrlSend,10);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,CtrlSend,10);
}

/*******************************************************************************
* Function Name  : BLWOut_tempCycle 
* Description    : BLWOut温控器轮询发送函数
*******************************************************************************/
void BLWOut_tempT1Cycle(Device_Public_Information_G *BUS_Public)   
{
		                    //  头             LEN   Type  Addr  CMD
	uint8_t t_send[9] = {0x55, 0x55, 0xEE, 0x06, 0x03, 0x01, 0x0A};  //轮询报文
//	uint8_t port = SRAM_Read_Byte(dev_addr+Dev_port);  

	t_send[5] = BUS_Public->addr; //  SRAM_Read_Byte(dev_addr+Dev_Addr);	

	NetCRC16(&t_send[3],6);
	
	/*发送数据*/
	MCU485_SendString(BUS_Public->port,t_send,9);
}


/*******************************************************************************
* Function Name  : BLWOut_tempT1CardCtrl 
* Description    : BLWOut温控器插卡状态同步功能发送函数
*******************************************************************************/
void BLWOut_tempT1CardCtrl(Device_Public_Information_G *BUS_Public, RS485_TEMP_INFO *Rs485Tem)   
{
		                    //  头             LEN   Type  Addr  CMD
	uint8_t t_send[10] = {0x55, 0x55, 0xEE, 0x07, 0x03, 0x01, 0x11 , 0x00 ,0x00, 0x00};  //轮询报文

	t_send[5] = BUS_Public->addr; 
	
	t_send[7] = Rs485Tem->CardEn;

	NetCRC16(&t_send[3],7);
	
	
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLWOut_tempT1CardCtrl",t_send,10);
	/*发送数据*/
	MCU485_SendString(BUS_Public->port,t_send,10);
}


/*******************************************************************************
* Function Name  : BLV_T1Temp_PortSet_Send 
* Description    : BLV T1温控端口类型设置
*******************************************************************************/
void BLV_T1Temp_PortSet_Send(Device_Public_Information_G *BUS_Public, RS485_TEMP_INFO *Rs485Tem)   
{
		                    //  头          LEN   Type  Addr  CMD
	uint8_t t_send[10] = {0x55, 0x55, 0xEE, 0x07, 0x03, 0x01, 0x12 , 0x00 ,0x00, 0x00};  //轮询报文

	t_send[5] = BUS_Public->addr; 
	
	t_send[7] = Rs485Tem->DevPort;

	NetCRC16(&t_send[3],7);
	
	
	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLV_T1Temp_PortSet_Send",t_send,10);
	/*发送数据*/
	MCU485_SendString(BUS_Public->port,t_send,10);
}


/*******************************************************************************
* Function Name  : BLWOut_TempT1CycleCtrl 
* Description    : BLWOut温控器轮询兼控制发送函数
*******************************************************************************/
uint8_t BLWOut_TempT1CycleCtrl(uint32_t dev_addr) 
{

	return TemSingleJudge(dev_addr, BLWOut_TempT1Ctrl, BLWOut_tempT1Cycle);
}	

/*BLWOut温控器接收数据处理函数*/
uint8_t BLWOut_TempT1CtrDataProc(Device_Public_Information_G* BUS_Public, uint8_t *data, RS485_TEMP_INFO *Rs485Tem)
{
	RS485_TEMP_BASIC  Rs485TemRecBuf;    //定义温控器局部变量，用于存储温控器发过来的数据
	uint8_t temp = 0;
	
//	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLWOut_TempT1CtrDataProc");
	
	switch(data[4])
	{
		case 0x01:	//开机
			Rs485TemRecBuf.TemState.on_off = TEMP_STATE_ON;
			break;
		case 0x00:	//关机
			Rs485TemRecBuf.TemState.on_off = TEMP_STATE_OFF;
			break;
	}
	
	
	switch(data[6])
	{
		case 1: 	//制冷
			Rs485TemRecBuf.TemState.mode = TEMP_COLD; 
			break;  
		case 2: 	//制热
			Rs485TemRecBuf.TemState.mode = TEMP_HOT;  
			break;  
		case 3: 	//送风
			Rs485TemRecBuf.TemState.mode = TEMP_WIND;  
			break;   
	}
	Rs485TemRecBuf.TemState.set_t = data[7];
	switch(data[10])
	{
		case 1: 
			Rs485TemRecBuf.TemState.fan	= TEMP_HIGH; 
			break; //高速
		case 2: 
			Rs485TemRecBuf.TemState.fan	= TEMP_MID; 
			break; //中速
		case 3: 
			Rs485TemRecBuf.TemState.fan	= TEMP_LOW; 
			break; //低速
		case 0: 
			Rs485TemRecBuf.TemState.fan	= TEMP_FANAUTO; 
			break; //自动
	}
	
	
	if((data[13] & 0x08))   //如果阀开||(temp & 0x10)
	{
		Rs485TemRecBuf.TemState.valve = TEMP_VALVE_OPEN;
	}
	else
	{
		Rs485TemRecBuf.TemState.valve = TEMP_VALVE_CLOSE; //阀关
	}
	
	if(0x00 != (data[13]&0x07))     //非0 就按照对面去算  
	{
		Rs485TemRecBuf.FanAutoRelay = (data[13]&0x07);  //自动风速会用到  只赋值最低三位
	}
	else
	{
		Rs485TemRecBuf.FanAutoRelay = 0x00;    //不支持自动风速
	}
	Rs485TemRecBuf.TemState.indoor_t = data[17];//SRAM_Read_Byte(data_addr + 17);           //
	
	if(data[18] < 0x0A ){						//将范围在0-9的小数数据存为小数部分，如果数据非法，则小数部分为0
		Rs485Tem->indoor_dec_temp = data[18];	
	}else{	
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"dec ERR:%d",data[18]);
		Rs485Tem->indoor_dec_temp = 0x00;
	}
	
	
	if((Rs485TemRecBuf.TemState.on_off != Rs485Tem->TemState.on_off) 
		|| (Rs485TemRecBuf.TemState.mode != Rs485Tem->TemState.mode)  
		||(Rs485TemRecBuf.TemState.fan != Rs485Tem->TemState.fan) 
		|| (Rs485TemRecBuf.TemState.valve != Rs485Tem->TemState.valve)
		||(Rs485TemRecBuf.FanAutoRelay != Rs485Tem->FanAutoRelay) 
		||(Rs485TemRecBuf.TemState.set_t != Rs485Tem->TemState.set_t)
		||(Rs485TemRecBuf.TemState.indoor_t != Rs485Tem->TemState.indoor_t)
		)//开关机 模式 温度 风速 室内温度与储存的不一样
	{
		
		Temp_Action_Set(&Rs485TemRecBuf, Rs485Tem);  //同步状态，并产生动作
	
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"温控器地址:%d 状态：", BUS_Public->addr);   //
		switch(Rs485Tem->TemState.on_off)
		{
			case TEMP_STATE_OFF:
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"关机");
				break;
			case TEMP_STATE_ON:
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"开机");
				break;
		}
		switch(Rs485Tem->TemState.mode)
		{
			case 0x01:
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"制冷");
				break;
			case 0x02:
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"制热");
				break;
			case 0x03:
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"送风");
				break;
		}
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"风速:%d", Rs485Tem->FanAutoRelay);

		switch(Rs485Tem->TemState.fan)
		{
			case 0x00:
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"自动");
				break;
			case 0x01:
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"高速");
				break;
			case 0x02:
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"中速");
				break;
			case 0x03:
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"低速");
				break;
		}
		switch(Rs485Tem->TemState.valve)
		{
			case TEMP_VALVE_OPEN:
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"阀开");
				break;
			case TEMP_VALVE_CLOSE:
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"阀关");
				break;
		}
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"设置温度:%d 室内温度:%d\r\n",Rs485Tem->TemState.set_t,Rs485TemRecBuf.TemState.indoor_t);
		
		temp++;
	}
	
//	if(Rs485TemRecBuf.TemState.indoor_t != Rs485Tem->TemState.indoor_t)
//	{
//		Rs485Tem->TemState.indoor_t = Rs485TemRecBuf.TemState.indoor_t;
//		if(Rs485Tem->TempT1Type == TEMP_TYPE_VALVE)   //温控器类型为电磁阀类型
//		{
//			Rs485Tem->TemDataChangeFlag = 0x01;
//		}
//	}
	
	if(Rs485Tem->TemState.on_off == Rs485Tem->control_start)		//本地控制状态，控制成功清楚标志位
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"清楚控制状态标志");
		Rs485Tem->control_start = 0xFF;
		
		temp++;
	}
	

//	Rs485Tem->TemState = ((Rs485Tem->on_off&0x01)<<15)+(Rs485Tem->mode<<13)+(Rs485Tem->fan<<11)+((Rs485Tem->valve&0x01)<<10)+((Rs485Tem->set_t&0x1F)<<5)+(Rs485Tem->indoor_t&0x1F);//全赋值		
	
	/*比对服务器上报状态 - 是否写入日志
		2025-10-13 修改
			1、只针对用户操作状态变化进行上报，室内温度变化，不会进行数据上报
			2、温控器操作停止后30S后，再进行数据上报 - 已经取消，改回2s
	*/
	if( (Rs485Tem->TemStateLast.on_off != Rs485Tem->TemState.on_off) 	\
		||	(Rs485Tem->TemStateLast.mode != Rs485Tem->TemState.mode) 	\
		||	(Rs485Tem->TemStateLast.fan != Rs485Tem->TemState.fan)		\
		||	(Rs485Tem->TemStateLast.valve != Rs485Tem->TemState.valve)	\
		||	(Rs485Tem->TemStateLast.set_t != Rs485Tem->TemState.set_t) \
		||	(Rs485Tem->TemStateLast.indoor_t != Rs485Tem->TemState.indoor_t) )		
	{
		Dev_Temp_State_Sync(&Rs485Tem->TemStateLast,&Rs485Tem->TemState);
		if( (Rs485Tem->TemStateLast.on_off != Rs485Tem->TemState.on_off) 	\
			||	(Rs485Tem->TemStateLast.mode != Rs485Tem->TemState.mode) 	\
			||	(Rs485Tem->TemStateLast.fan != Rs485Tem->TemState.fan)		\
			||	(Rs485Tem->TemStateLast.valve != Rs485Tem->TemState.valve)	\
			||	(Rs485Tem->TemStateLast.set_t != Rs485Tem->TemState.set_t) )		
		{
			
			
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"服务器上报:%04x",Dev_Temp_State_Data(Rs485Tem->TemState));
			
			Rs485Tem->udp_flag = 0x01;
			Rs485Tem->udp_tick = SysTick_1ms;
			temp++;
		}
	}
	
	
	
	if(Rs485Tem->udp_flag == 0x01)
	{
		if( SysTick_1ms - Rs485Tem->udp_tick >= 2000)
		{
			Rs485Tem->udp_tick = SysTick_1ms;
			Rs485Tem->udp_flag = 0x00;
			
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"写入日志:%04x",Dev_Temp_State_Data(Rs485Tem->TemStateLast));
			
			temp++;
			//写入日志
			Udp_Addtion_Roomstate(DEV_RS485_TEMP,BUS_Public->addr,0x0000,Dev_Temp_State_Data(Rs485Tem->TemStateLast));
		}
	}
	
	return temp;
}


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

/*******************************************************************************
* Function Name  : BLWOut_Rs485_Tem_Check 
* Description    : BLWOut温控器接收数据处理函数
* Input          : 
	dev_addr	: 设备信息地址
	data_addr	: 数据地址
	len			：数据长度
* Return         : 
	0x00：处理成功
	0x01：处理失败
*******************************************************************************/
uint8_t BLWOut_Rs485_TempT1_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len)
{
	uint8_t temp_1 = 0;
	uint8_t rev = 0x01;
//	uint16_t temp = 0;
	uint8_t data[RECDATALENMAX];
	uint16_t crc_val;// = SRAM_Read_Word(data_addr+len-2);
	RS485_TEMP_INFO Rs485TempT1;
	Device_Public_Information_G BUS_Public;
	
	memset(data,0x00,RECDATALENMAX);
	
	if(len > RECDATALENMAX)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"T1 Temp 接收数据长度超出范围!!\r\n");
		return rev;     //长度
	}
	SRAM_DMA_Read_Buff(data,len,data_addr);   //拷贝482数据
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);		
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485TempT1,sizeof(RS485_TEMP_INFO),dev_addr+Dev_Privately);
	
	if(len < 6) return rev;
	
	if((data[0] != 0x55) || (data[1] != 0x55) || (data[2] != 0xee) || (len != data[3] + 0x03) || (0x03 != data[4]) ||BUS_Public.addr!=data[5] )
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s Addr:%d Check Error",__func__,BUS_Public.addr);
		return rev;     
	}
	crc_val = data[len-2] + (data[len-1]<<8);
	
	if(crc_val == NetCRC16_2(&data[3],len - 5))
	{
		rev = 0x00;
		
		if(Rs485TempT1.DevOffline != DEV_IS_ONLINE)
		{
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Rs485TempT1 LogInfo_Device_Online...........");
			LOG_Device_Online_Record(DEV_RS485_TEMP,BUS_Public.addr,LogInfo_Device_Online);	//记录设备在线
		}
		BLV_Communication_Record(&Rs485TempT1.comm_record,0x02,0x01);		//记录通讯成功
		Rs485TempT1.DevSendCnt = 0x00;    //计数清0

				
		switch(data[3])    //根据长度区分数据
		{
			case 0x15://轮询回复报文	
				Rs485TempT1.DevOffline = DEV_IS_ONLINE;    //唯有轮询设备在线
				if(Rs485TempT1.DevOffline != Rs485TempT1.DevOfflineLast)	//当前在线状态不等于上一次在线状态
				{
					Rs485TempT1.DevOfflineLast = Rs485TempT1.DevOffline;	//更新上一次在线状态
					Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
				}
				temp_1 = BLWOut_TempT1CtrDataProc(&BUS_Public, &data[3], &Rs485TempT1); 
				if(temp_1 != 0x00)	//轮询回复数据有变化
				{
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"BLV Rs485 TempT1 Reply Change");
					LOG_Device_COMM_ASK_TO_Reply_Record(BUS_Public.port, BUS_Public.baud,(SysTick_1ms - Rs485TempT1.inquire_tick),data,len);
				}
				if(BUS_Public.port == Active_Port)    //2024-11-26
				{
					BLWOut_tempT1Cycle(&BUS_Public); 
				}
				break;
			case 0x07:  //控制开关机
				Rs485TempT1.DevOffline = DEV_IS_ONLINE;    //设备在线
				if(Rs485TempT1.DevOffline != Rs485TempT1.DevOfflineLast)	//当前在线状态不等于上一次在线状态
				{
					Rs485TempT1.DevOfflineLast = Rs485TempT1.DevOffline;	//更新上一次在线状态
					Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
				}
				switch(data[6])     //SRAM_Read_Byte(data_addr + 6)
				{
				case 0x01:   //开关机回复			
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"温控器:%d 收到开关机回复，清开关机控制标志", BUS_Public.addr);
//					Rs485TempT1.TemState.on_off = Rs485TempT1.TemStateCtrl.on_off;   //开关机状态同步  不能同步，保证轮询时能产生事件
					Rs485TempT1.TemStateCtrlFlag.TemOnOffCtrlVar = 0x00;    //清空开关机控制标志
					break;
				case 0x02:  //模式
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"温控器:%d 收到模式回复，清模式控制标志", BUS_Public.addr);				
//					Rs485TempT1.TemState.mode = Rs485TempT1.TemStateCtrl.mode;   //模式状态同步
					Rs485TempT1.TemStateCtrlFlag.TemModeCtrlVar = 0x00;    //清空模式控制标志
					break;
				case 0x04:  //风速
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"温控器:%d 收到风速回复，清风速控制标志", BUS_Public.addr);
//					Rs485TempT1.TemState.fan = Rs485TempT1.TemStateCtrl.fan;   //风速状态同步
					Rs485TempT1.TemStateCtrlFlag.TemFanCtrlVar = 0x00;    //清空风速控制标志
					break;
				case 0x03:  //温度
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"温控器:%d 收到温度回复，清温度设置标志", BUS_Public.addr);
//					Rs485TempT1.TemState.set_t = Rs485TempT1.TemStateCtrl.set_t;   //风速状态同步
					Rs485TempT1.TemStateCtrlFlag.TemSetTCtrlVar = 0x00;    //清空温度控制标志
					break;
				case 0x11:
					Rs485TempT1.CardFlag = 0x00;
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"温控器:%d 收到插卡状态设置回复，清设置标志", BUS_Public.addr);
					break;
				case 0x12:
					Rs485TempT1.DevPort_Flag = 0x00;
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"温控器:%d 收到设置端口类型设置回复，清设置标志", BUS_Public.addr);
					break;
				}
				
				/*日志记录*/
				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 *)&Rs485TempT1, sizeof(RS485_TEMP_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485TempT1,sizeof(RS485_TEMP_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,NetCRC16_2(&data[3],len - 5));
//		
//	}
	
	
	return rev;
}



/*******************************************************************************
* Function Name  : BLW_TEMP_Control_State 
* Description    : BLWOut温控器单个状态控制函数
* Input          : 
	devaddr	: 设备信息地址
	serial	: 
		0x00 - 开关机
		0x01 - 模式
		0x02 - 设置温度
		0x03 - 风速
	serial_start ：
				开关机		模式		风速
		0x00 -    		  自动模式	  自动风速	
		0x01 -   开机		制冷		低速
		0x02 -   关机		制热		中速
		0x03 -     			送风		高速
* Return         : 
*******************************************************************************/
//void BLW_TEMP_Control_State(uint32_t devaddr,uint8_t serial,uint8_t serial_start)
//{
//	uint16_t temp = 0;
//	uint8_t crc_val = 0,temp1 = 0;
//	if(devaddr == 0x00)	return;
//	
//	RS485_TEMP_INFO Rs485TempT1;
//	
//	SRAM_DMA_Read_Buff((uint8_t *)&Rs485TempT1,sizeof(RS485_TEMP_INFO),devaddr+Dev_Privately);
//	
//	switch(serial)
//	{
//		case 0:
//			temp1++;
//			Rs485TempT1.control_start = serial_start;
//			Rs485TempT1.info[0] = serial_start;
//			break;
//		case 1:
//			temp1++;
//			Rs485TempT1.info[1] = serial_start;
//			break;
//		case 2:
//			temp1++;
//			Rs485TempT1.info[2] = serial_start;
//			break;
//		case 3:
//			temp1++;
//			Rs485TempT1.info[3] = serial_start;
//			break;
//	}
//	
//	if(temp1 != 0x00)
//	{
//		/*将数据保存进入SRAM中*/
//		SRAM_DMA_Write_Buff((uint8_t *)&Rs485TempT1,sizeof(RS485_TEMP_INFO),devaddr+Dev_Privately);
//		
//		/*计算校验值*/
//		SRAM_Write_Byte(0x00,devaddr+Dev_Check);
//		temp = sizeof(RS485_TEMP_INFO) + Dev_Privately;
//		crc_val = Dev_CheckSum(devaddr,temp);
//		SRAM_Write_Byte(crc_val,devaddr+Dev_Check);		//校验值重新写入
//	}
//}

/*******************************************************************************
* Function Name  : BLW_TEMP_UDP_Control 
* Description    : BLWOut温控器UDP下发控制状态
* Input          : 
	devaddr	: 设备信息地址
	state	: 温控状态 - 2Byte
* Return         : 
*******************************************************************************/
//void BLW_TEMP_Group_Control_State(uint32_t devaddr,uint16_t state)
//{
//	uint16_t temp = state;
//	uint8_t crc_val = 0,temp1 = 0;
//	if(devaddr == 0x00)	return;
//	
//	RS485_TEMP_INFO Rs485TempT1;
//	RS485_TEM_BASIC set_temp;
//	
//	memset(&set_temp,0,sizeof(RS485_TEM_BASIC));
//	SRAM_DMA_Read_Buff((uint8_t *)&Rs485TempT1,sizeof(RS485_TEMP_INFO),devaddr+Dev_Privately);
//	
//	set_temp.set_t = temp & 0xFF;
//	set_temp.valve = (temp >> 8) & 0x03;
//	set_temp.fan = (temp >> 10) & 0x03;
//	set_temp.mode = (temp >> 12) & 0x03;
//	set_temp.on_off = (temp >> 14) & 0x03;
//	
//	Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"set TEMP State:%04X\r\n",temp);
//	Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"set TEMP State: 温度:%d,阀门:%d,风速:%d,模式:%d开关机:%d \r\n",set_temp.set_t,set_temp.valve,set_temp.fan,set_temp.mode,set_temp.on_off);
//	
//	if(set_temp.on_off != Rs485TempT1.on_off )		//开关机
//	{
//		temp1++;
//		Rs485TempT1.info[0] = set_temp.on_off;
//	}
//	
//	if(set_temp.mode != Rs485TempT1.mode)
//	{
//		temp1++;
//		Rs485TempT1.info[1] = set_temp.mode;
//	}
//	
//	if(set_temp.set_t != Rs485TempT1.set_t)
//	{
//		temp1++;
//		Rs485TempT1.info[2] = set_temp.set_t;
//	}
//	
//	if(set_temp.fan != Rs485TempT1.fan )
//	{
//		temp1++;
//		Rs485TempT1.info[3] = set_temp.fan;
//	}
//	
//	
//	if(temp1 != 0x00)
//	{
//		/*将数据保存进入SRAM中*/
//		SRAM_DMA_Write_Buff((uint8_t *)&Rs485TempT1,sizeof(RS485_TEMP_INFO),devaddr+Dev_Privately);
//		
//		/*计算校验值*/
//		SRAM_Write_Byte(0x00,devaddr+Dev_Check);
//		temp = sizeof(RS485_TEMP_INFO) + Dev_Privately;
//		crc_val = Dev_CheckSum(devaddr,temp);
//		SRAM_Write_Byte(crc_val,devaddr+Dev_Check);		//校验值重新写入
//	}
//}







