#include "BLV_485_Dev_AirDetectFun.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 "Log_api.h"

/*AirDetect空气质量检测驱动代码*/

#define REPEATSENDTIMEMAX         0x03  //重发次数上限
//#define AirDetectUPLIGHT              100    //最高亮80
//#define AirDetectDOWNLIGHT            0    //最低亮20

//#define AirDetectGRADUALTIME         30   //30  即300ms
//#define   A9EXPANDTYPE           14	    //AirDetect类型

//#define AirDetectSnMin                 0x08            //最大Sn号
//#define AirDetectSnMax                 0x0F            //最大Sn号

#define AIR_QUALITY_DETECTION_SAVE_TIME		30000	//空气质量检测保存时间

/*本地函数调用开始*/
uint8_t BLW_AirDetectCycleCtrl(uint32_t dev_addr); 
uint8_t BLW_Rs485_AirDetect_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len);
//void BLW_Rs485_AirDetect_4LOOP_Ctrl(uint32_t devaddr, RS485_AirDetect_INFO* Rs485AirDetectInfo);
void BLW_AirDetect_Cycle(Device_Public_Information_G *BUS_Public, RS485_AirDetect_INFOP Rs485AirDetectInfo);

//void Dev_AirDetect_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t loop,uint16_t start);
uint16_t Dev_AirDetect_Read(uint32_t devaddr,uint16_t loop);
void save_air_detection_state(Device_Public_Information_G *bus,RS485_AirDetect_INFO *air_detection);
/*本地函数调用结束*/

/*******************************************************************************
* Function Name  : BLW_RS485_AirDetect_Data_Init 
* Description    : 添加设备到链表 - 轮询设备
* Input          : 
	type	: 添加的设备类型
	addr	: 添加的设备地址
	polling_cf		：设备的轮询数据发送回调函数
	processing_cf	：设备的数据处理回调函数
* Return         : None
*******************************************************************************/
void BLW_RS485_AirDetect_Data_Init(uint8_t devaddr)
{
	Device_Public_Information_G BUS_Public;
	RS485_AirDetect_INFO Rs485AirDetect;
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&Rs485AirDetect,0,sizeof(RS485_AirDetect_INFO));
	
	BUS_Public.addr = devaddr;		//设备地址
	BUS_Public.type = DEV_AirDetectTYPE;		//设备类型
	BUS_Public.port = Polling_Port;		//设备发送端口
	BUS_Public.baud = 9600;		//设备发送波特率
	BUS_Public.retry_num = 0x03;	//设备数据重发次数
	BUS_Public.wait_time = 0x00C8;	//设备数据发送等待回复时间 - 200ms
	BUS_Public.polling_cf = (uint32_t)&BLW_AirDetectCycleCtrl;
	BUS_Public.processing_cf = (uint32_t)&BLW_Rs485_AirDetect_Check;
	
//	BUS_Public.DevFunInfo.Dev_Output_Ctrl = Dev_AirDetect_Ctrl;
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = Dev_AirDetect_Read;   
	/*初始化AirDetect空气质量检测初始值*/
	Rs485AirDetect.AirDetectInputValidNum = AirDetectValidMax;
	Rs485AirDetect.DevOffline = Rs485AirDetect.DevOfflineLast = DEV_IS_LINEUNINIT;   //在线状态初始化
	Add_POLL_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485AirDetect,sizeof(RS485_AirDetect_INFO));
}

/*******************************************************************************
* Function Name  : BLW_RS485_AirDetect_Data_For_Logic_Init     2022-08-30
* Description    : 空气质量检测初始化信息来自逻辑文件
*******************************************************************************/
void BLW_RS485_AirDetect_For_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len)
{
	Device_Public_Information_G BUS_Public;
	RS485_AirDetect_INFO Rs485AirDetect;
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&Rs485AirDetect,0,sizeof(RS485_AirDetect_INFO));
	
	BUS_Public.addr = dev_info->addr;			//设备地址
	BUS_Public.type = dev_info->type;		//设备类型
	BUS_Public.port = Polling_Port;				//设备发送端口
	BUS_Public.baud = dev_info->baud;						//设备发送波特率
	BUS_Public.retry_num = 0x03;				//设备数据重发次数
	BUS_Public.wait_time = 0x00C8;				//设备数据发送等待回复时间 
	BUS_Public.polling_cf = (uint32_t)&BLW_AirDetectCycleCtrl;
	BUS_Public.processing_cf = (uint32_t)&BLW_Rs485_AirDetect_Check;
	BUS_Public.Protocol = 0x01;  //dev_info->version
	
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = Dev_AirDetect_Read;
		/*初始化AirDetect空气质量检测初始值*/
	Rs485AirDetect.AirDetectInputValidNum = AirDetectValidMax;
	Rs485AirDetect.DevOfflineLast = DEV_IS_ONLINE;   //在线状态初始化
	Rs485AirDetect.DevOffline = Rs485AirDetect.DevOfflineLast;
	Add_POLL_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485AirDetect,sizeof(RS485_AirDetect_INFO));
	Poll485_Info.device_num += 1;
}





/*******************************************************************************
* Function Name  : BLW_AirDetectCycleCtrl 
* Description    : BLWAirDetect空气质量检测轮询兼控制发送函数
*******************************************************************************/
uint8_t BLW_AirDetectCycleCtrl(uint32_t dev_addr) 
{
	uint16_t temp = 0;
	uint8_t crc_val = 0;
//	uint8_t i;     //用于遍历所有回路
	uint8_t Ret = RS485OCCUPYTIME;    //默认占用时间
	
	RS485_AirDetect_INFO Rs485AirDetectInfo;
	Device_Public_Information_G BUS_Public;

	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirDetectInfo,sizeof(RS485_AirDetect_INFO),dev_addr+Dev_Privately);
	SRAM_Read_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), dev_addr);  
	
//	for(i = 0; i < Rs485AirDetectInfo.AirDetectInputValidNum; i++)
//	{
//		if(Rs485AirDetectInfo.DevSendBuf_last[i] != Rs485AirDetectInfo.DevSendBuf[i])
//		{
//			Rs485AirDetectInfo.AirDetectCtrlFlag = 0x01;
//			Rs485AirDetectInfo.AirDetectCtrlCnt = REPEATSENDTIMEMAX;
//			memcpy(Rs485AirDetectInfo.DevSendBuf_last, Rs485AirDetectInfo.DevSendBuf, Rs485AirDetectInfo.AirDetectInputValidNum);   //同步这组数据

//			break;
//		} 
//	}
	
	

//	if(0x01 == Rs485AirDetectInfo.AirDetectCtrlFlag)
//	{
//		if(0x00 != Rs485AirDetectInfo.AirDetectCtrlCnt)
//		{
//			Rs485AirDetectInfo.AirDetectCtrlCnt--; 
//		}
//		else
//		{
//			Rs485AirDetectInfo.AirDetectCtrlFlag = 0x00;
//		}
//		BLW_Rs485_AirDetect_4LOOP_Ctrl(dev_addr, &Rs485AirDetectInfo);           //4路控制，状态控制
//		Ret = RS485OCCUPYTIME;
//		
//		/*通讯记录*/
//		BLV_Communication_Record(&Rs485AirDetectInfo.comm_record,0x01,0x00);
//	}

	BLW_AirDetect_Cycle(&BUS_Public, &Rs485AirDetectInfo);
	
//	if(RS485OCCUPYTIME == Ret)
	{
		/*通讯统计*/
		BLV_Communication_Record(&Rs485AirDetectInfo.comm_record,0x01,0x00);
		Rs485AirDetectInfo.inquire_tick = SysTick_1ms;
		
		if(Rs485AirDetectInfo.DevSendCnt > REPEATSENDTIMEMAX)
		{
			if(Rs485AirDetectInfo.DevOffline != DEV_IS_OFFLINE)
			{
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s DEV_OFFLINE",__func__);
				LOG_Device_Online_Record(DEV_AirDetectTYPE,SRAM_Read_Byte(dev_addr+Dev_Addr),LogInfo_Device_Offline);	//记录设备离线
			}
			Rs485AirDetectInfo.DevOffline = DEV_IS_OFFLINE;   //离线
			if(Rs485AirDetectInfo.DevOffline != Rs485AirDetectInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
			{
				Rs485AirDetectInfo.DevOfflineLast = Rs485AirDetectInfo.DevOffline;	//更新上一次在线状态
				Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_OFFLINE);	//保存设备在线状态到SRAM
			}
		}
		else
		{
			Rs485AirDetectInfo.DevSendCnt++;   //
		}
		
		/*将数据保存进入SRAM中*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485AirDetectInfo,sizeof(RS485_AirDetect_INFO),dev_addr+Dev_Privately);
		
		/*计算校验值*/
		SRAM_Write_Byte(0x00,dev_addr+Dev_Check);
		temp = sizeof(RS485_AirDetect_INFO) + Dev_Privately;
		crc_val = Dev_CheckSum(dev_addr,temp);
		SRAM_Write_Byte(crc_val,dev_addr+Dev_Check);		//校验值重新写入
		//Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"AirDetect crc_val:%02X\r\n",crc_val);
	}

	
	

	
	return Ret;
}

/*门锁轮询函数*/
#define BLWAIRDETECTCYCLELEN   0x08
void BLW_AirDetect_Cycle(Device_Public_Information_G *BUS_Public, RS485_AirDetect_INFOP Rs485AirDetectInfo)
{
	uint8_t SendData[BLWAIRDETECTCYCLELEN]; 
	SendData[0] = BUS_Public->addr;  //地址
	SendData[1] = 0x03;                //读
	SendData[2] = 0x00;             //起始地址高字节
	SendData[3] = 0x02;             //起始地址低字节
	SendData[4] = 0x00;             //寄存器个数高字节
	SendData[5] = 0x07;             //寄存器个数低字节
	
	NetCRC16(SendData,8);
	
//	SendData[2] = CheckSum_Overlook_Check(SendData, BLWAIRDETECTCYCLELEN, 0x02);  //和校验取反
	
	MCU485_SendString(BUS_Public->port,SendData,BLWAIRDETECTCYCLELEN);

}
/**
	*@name 空气质量检测接收数据处理函数
  *@para  
  *   BUS_Public     公用变量
  *   data           数据包
  *   Rs485AirDetect 私有变量
  *@ret 0表示无变化  1表示有变化
  */
uint8_t AirDetectDataProc(Device_Public_Information_G* BUS_Public, uint8_t *data, RS485_AirDetect_INFO *Rs485AirDetect)
{
	uint8_t Ret = 0x00;
	uint8_t i;
	
	for(i = 0x00; i < Rs485AirDetect->AirDetectInputValidNum; i++)    //
	{
		Rs485AirDetect->DevReadBuf[i] = (data[3+2*i]<<8)+data[3+2*i+1];  //
		switch(i)
		{
			case AirDetectCH2O:     	 	 		   //甲醛浓度       单位 ug/m^3
			case AirDetectTVOC:     					 //TVOC浓度       单位 ug/m^3
			case AirDetectPM2_5:     				   //PM2.5浓度      单位 ug/m^3
			case AirDetectPM10:     					 //PM10浓度值     单位 ug/m^3
				if(Rs485AirDetect->DevReadBuf_last[i]/20 != Rs485AirDetect->DevReadBuf[i]/20)
				{
					Ret = 0x01;
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"AirDetect :%d,参数:%04X:%04X!", i, Rs485AirDetect->DevReadBuf_last[i]/10, Rs485AirDetect->DevReadBuf[i]/10);
					Rs485AirDetect->DevReadBuf_last[i] = Rs485AirDetect->DevReadBuf[i];
				}
				break;
			case AirDetectTemperature: 			   //温度 被放大10倍存放  单位 度
				if(Rs485AirDetect->DevReadBuf_last[i]/10 != Rs485AirDetect->DevReadBuf[i]/10)
				{
					Ret = 0x01;
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"AirDetect :%d,参数:%04X:%04X!", i, Rs485AirDetect->DevReadBuf_last[i]/10, Rs485AirDetect->DevReadBuf[i]/10);				
					Rs485AirDetect->DevReadBuf_last[i] = Rs485AirDetect->DevReadBuf[i];					
				}
				break;
			case AirDetectCO2:         	        //二氧化碳浓度   单位 ppm 
			case AirDetectHumidity:    				 //湿度 被放大十倍      单位 %RH
				if(Rs485AirDetect->DevReadBuf_last[i]/50 != Rs485AirDetect->DevReadBuf[i]/50)
				{
					Ret = 0x01;
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"AirDetect :%d,参数:%04X:%04X!", i, Rs485AirDetect->DevReadBuf_last[i]/50, Rs485AirDetect->DevReadBuf[i]/50);				
					Rs485AirDetect->DevReadBuf_last[i] = Rs485AirDetect->DevReadBuf[i];					
				}
				break;
		}
	}
	return Ret;
}

#define RECDATALENMAX 19
#define IsMyData     0x00  
#define IsNotMyData  0x01
/*******************************************************************************
* Function Name  : BLW_Rs485_AirDetect_Check 
* Description    : BLWAirDetect空气质量检测接收数据处理函数
* Input          : 
	dev_addr	: 设备信息地址
	data_addr	: 数据地址
	len			：数据长度
* Return         : 
	0x00：处理成功
	0x01：处理失败
*******************************************************************************/
uint8_t BLW_Rs485_AirDetect_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len)
{
	//Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"BLW_Rs485_AirDetect_Check\r\n");
	uint8_t rev = IsNotMyData;
	uint8_t temp;  //保存命令类型和数据长度
	RS485_AirDetect_INFO Rs485AirDetect;
	uint8_t data[RECDATALENMAX];	
	Device_Public_Information_G BUS_Public;
	uint16_t crc_val;  //校验
	
//	uint8_t AirDetect_addr = SRAM_Read_Byte(dev_addr+Dev_Addr); //拿到当前设备的地址
//	uint8_t crc_val = SRAM_Read_Word(data_addr+5);        //保存校验
	
	if(len > RECDATALENMAX)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s Len Error",__func__);
		return rev;     //长度
	}		
	
	SRAM_DMA_Read_Buff(data,len,data_addr);   //拷贝485数据
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);		
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirDetect,sizeof(RS485_AirDetect_INFO),dev_addr+Dev_Privately);
	
	if((data[0] != BUS_Public.addr)|| (data[2] + 0x05 != len))	   //地址不对  长度不对
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s Check Error",__func__);
		return rev;				//校验文件头
	}
	crc_val = data[len-2] + (data[len-1]<<8);

	if(crc_val == NetCRC16_2(data, len-2))   //校验通过
	{
//		SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirDetect,sizeof(RS485_AirDetect_INFO),dev_addr+Dev_Privately);
//		temp = SRAM_Read_Byte(data_addr + 6);
	
		BLV_Communication_Record(&Rs485AirDetect.comm_record,0x02,0x01);		//记录通讯成功
		
		switch(data[1])  
		{
			case 0x03:   //读取寄存器状态回复

				rev = IsMyData;
			
				if(AirDetectDataProc(&BUS_Public, data, &Rs485AirDetect))
				{
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"AirDetect Reply Change");
					LOG_Device_COMM_ASK_TO_Reply_Record(BUS_Public.port, BUS_Public.baud,(SysTick_1ms - Rs485AirDetect.inquire_tick),data,len);
				}
				if(Rs485AirDetect.DevOffline != DEV_IS_ONLINE)
				{
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s Device_Online",__func__);
					LOG_Device_Online_Record(DEV_AirDetectTYPE,SRAM_Read_Byte(dev_addr+Dev_Addr),LogInfo_Device_Online);	//记录设备在线
				}
				save_air_detection_state(&BUS_Public, &Rs485AirDetect);
				
				Rs485AirDetect.DevSendCnt = 0x00;
				Rs485AirDetect.DevOffline = DEV_IS_ONLINE;    //设备在线
				if(Rs485AirDetect.DevOffline != Rs485AirDetect.DevOfflineLast)	//当前在线状态不等于上一次在线状态
				{
					Rs485AirDetect.DevOfflineLast = Rs485AirDetect.DevOffline;	//更新上一次在线状态
					Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
				}
//				Rs485AirDetect.AirDetectCtrlFlag = 0x00;
				
				/*日志记录*/
				break;			
		}
		
				/*将数据保存进入SRAM中*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485AirDetect,sizeof(RS485_AirDetect_INFO),dev_addr+Dev_Privately);
		
		/*计算校验值*/
		SRAM_Write_Byte(0x00,dev_addr+Dev_Check);
		temp = sizeof(RS485_AirDetect_INFO) + Dev_Privately;
		crc_val = Dev_CheckSum(dev_addr,temp);
		SRAM_Write_Byte(crc_val,dev_addr+Dev_Check);		//校验值重新写入
	}
//	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;
}



/*******************************************************************************
* Function Name  : BLW_AirDetect_Control_State 
* Description    : BLWAirDetect空气质量检测状态控制函数
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路
	start   ：状态
* Return  : 无
* attention : temp1非0才会进行保存
*******************************************************************************/
//void BLW_AirDetect_Control_State(uint32_t devaddr,uint8_t loop,uint8_t start)
//{
//	uint16_t temp = 0;
//	uint8_t crc_val = 0,temp1 = 0;
//	
//	if(devaddr == 0x00)	return;
//	
//	RS485_AirDetect_INFO Rs485AirDetect;

//	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirDetect,sizeof(RS485_AirDetect_INFO),devaddr+Dev_Privately);
//	
//	if(loop < Rs485AirDetect.AirDetectInputValidNum)  //有效回路
//	{
//		if(Rs485AirDetect.DevSendBuf[loop] != start)  //不同
//		{
//			Rs485AirDetect.DevSendBuf[loop] = start; 
//			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"DevSendBuf loop:%d,start:%d\r\n",loop,start);
//			temp1++;
//		}
//	}
//	
//	if(temp1 != 0x00)
//	{
//		/*将数据保存进入SRAM中*/
//		SRAM_DMA_Write_Buff((uint8_t *)&Rs485AirDetect,sizeof(RS485_AirDetect_INFO),devaddr+Dev_Privately);
//		
//		/*计算校验值*/
//		SRAM_Write_Byte(0x00,devaddr+Dev_Check);
//		temp = sizeof(RS485_AirDetect_INFO) + Dev_Privately;
//		crc_val = Dev_CheckSum(devaddr,temp);
//		SRAM_Write_Byte(crc_val,devaddr+Dev_Check);		//校验值重新写入
//	}
//}

/*******************************************************************************
* Function Name  : BLW_AirDetect_Read_State 
* Description    : BLWAirDetect空气质量检测状态读取函数
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路
* Return  : 无
*******************************************************************************/
uint8_t BLW_AirDetect_Read_State(uint32_t devaddr,uint8_t loop)
{
	if(devaddr == 0x00)
		return 0x00;
	
	RS485_AirDetect_INFO Rs485AirDetect;
	
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirDetect,sizeof(RS485_AirDetect_INFO),devaddr+Dev_Privately);	
	
	if(loop >= Rs485AirDetect.AirDetectInputValidNum)
	{
		return 0x00;    //超出范围 直接返回0
	}
	
	return Rs485AirDetect.DevReadBuf[loop];
	
}

/*获取AirDetect空气质量检测在线状态*/
uint8_t Get_BLV485_AirDetect_Online_Status(uint32_t devaddr)
{
	RS485_AirDetect_INFO Rs485AirDetect;
	
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirDetect,sizeof(RS485_AirDetect_INFO),devaddr+Dev_Privately);
	
	if(Rs485AirDetect.DevOffline == DEV_IS_ONLINE)
	{
		return 0x01;
	}
	return 0x02;
}


#define A9EXPANDCTRLLEN    12    //空气质量检测控制报文
/**
	* @name BLW 4路 集中空气质量检测控制函数
	* @para
	*   dev_addr     设备起始地址   
	* @return         无
  * @attention      发送循环里调用，变量已经被判断
	*/
//void BLW_Rs485_AirDetect_4LOOP_Ctrl(uint32_t dev_addr, RS485_AirDetect_INFO* Rs485AirDetectInfo)
//{
//	uint8_t port = SRAM_Read_Byte(dev_addr+Dev_port);      //通讯端口
//	uint8_t DevAddr = SRAM_Read_Byte(dev_addr+Dev_Addr);   //设备地址
//	                               //己方地址 发送类型  设备类型      收方地址    LEN          校验和   CMD  4个灯
//	uint8_t SendBuf[A9EXPANDCTRLLEN] = {0x00,   0x00,   A9EXPANDTYPE,   0x00,  A9EXPANDCTRLLEN,  0x18,  0x20, 0x00, 0x00,  0x00,  0x00, 0x00};   //
//	uint8_t i;	

//	SendBuf[1] |= BLV_SN_Get(&Rs485AirDetectInfo->AirDetectSn, AirDetectSnMin, AirDetectSnMax);
//												 
//	SendBuf[3] = DevAddr;   
////	memcpy(&SendBuf[6], &RS485RelayExpandInfo.DevRelayBuf[SwiAddr*2], 2); //空气质量检测， 下标从5开始
//	for(i = 0; i < Rs485AirDetectInfo->AirDetectInputValidNum; i++)
//	{
//		switch(Rs485AirDetectInfo->DevSendBuf[i])
//		{
//			case 0x01:
//				SendBuf[7+i/4] |= 0x02<<((i%4)*2);       //开
//				break;
//			case 0x02:
//				SendBuf[7+i/4] |= 0x01<<((i%4)*2);      //关
//				break;
//		}
//	}
//	SendBuf[5] = CheckSum_Overlook_Check(SendBuf, A9EXPANDCTRLLEN, 5);	
//		
//	MCU485_SendString(port,SendBuf,A9EXPANDCTRLLEN);
//	
//	/*日志记录*/
//	LOG_Device_COMM_Send_Control_Record2(dev_addr+Dev_port,dev_addr+Dev_baud,SendBuf,A9EXPANDCTRLLEN);	
//}


/**@name  空气质量检测状态变化得
	*@para  无
  *@ret   为1 有变化   为0  无变化
	*/
//uint8_t AirDetect_State_Change_Get(uint32_t devaddr)
//{
//	uint8_t i;
//	uint8_t Ret = 0x00;
//	uint16_t temp = 0;
//	uint8_t crc_val = 0,temp1 = 0;

//	if(devaddr == 0x00)	return 0;
//	
//	RS485_AirDetect_INFO Rs485AirDetectInfo;

//	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirDetectInfo,sizeof(RS485_AirDetect_INFO),devaddr+Dev_Privately);
//	
//	for(i = 0; i < Rs485AirDetectInfo.AirDetectInputValidNum; i ++)
//	{
//		if(Rs485AirDetectInfo.DevSendBufNext[i] != Rs485AirDetectInfo.DevSendBuf[i])   //状态变化
//		{
//			Ret = 0x01;     //有变化	
//			temp1++;
//			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"AirDetect空气质量检测状态变化 DevSendBuf loop:%d,start:%d\r\n", i, Rs485AirDetectInfo.DevSendBuf[i]);  //空气质量检测状态变化打印
//			
//			memcpy(Rs485AirDetectInfo.DevSendBufNext, Rs485AirDetectInfo.DevSendBuf, Rs485AirDetectInfo.AirDetectInputValidNum); //同步状态
//			break;
//		}
//	}
//	
//	if(temp1 != 0x00)
//	{
//		/*将数据保存进入SRAM中*/
//		SRAM_DMA_Write_Buff((uint8_t *)&Rs485AirDetectInfo,sizeof(RS485_AirDetect_INFO),devaddr+Dev_Privately);
//		
//		/*计算校验值*/
//		SRAM_Write_Byte(0x00,devaddr+Dev_Check);
//		temp = sizeof(RS485_AirDetect_INFO) + Dev_Privately;
//		crc_val = Dev_CheckSum(devaddr,temp);
//		SRAM_Write_Byte(crc_val,devaddr+Dev_Check);		//校验值重新写入
//	}
//	
//	return Ret;        
//}

	



/*******************************************************************************
* Function Name  : BLW_AirDetect_Control_State 
* Description    : BLWAirDetect空气质量检测状态控制函数
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路
	start   ：状态
* Return  : 无
* attention : temp1非0才会进行保存
*******************************************************************************/
//void Dev_AirDetect_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t loop,uint16_t start)
//{
//	uint16_t temp = 0;
//	uint8_t crc_val = 0,temp1 = 0;
//	
//	if(devaddr == 0x00)	return;
//	
//	RS485_AirDetect_INFO Rs485AirDetect;

//	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirDetect,sizeof(RS485_AirDetect_INFO),devaddr+Dev_Privately);
//	
//	if(loop < Rs485AirDetect.AirDetectInputValidNum)  //有效回路
//	{
//		if(Rs485AirDetect.DevSendBuf[loop] != start)  //不同
//		{
//			Rs485AirDetect.DevSendBuf[loop] = start; 
//			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"DevSendBuf loop:%d,start:%d\r\n",loop,start);
//			temp1++;
//		}
//	}
//	
//	if(temp1 != 0x00)
//	{
//		/*将数据保存进入SRAM中*/
//		SRAM_DMA_Write_Buff((uint8_t *)&Rs485AirDetect,sizeof(RS485_AirDetect_INFO),devaddr+Dev_Privately);
//		
//		/*计算校验值*/
//		SRAM_Write_Byte(0x00,devaddr+Dev_Check);
//		temp = sizeof(RS485_AirDetect_INFO) + Dev_Privately;
//		crc_val = Dev_CheckSum(devaddr,temp);
//		SRAM_Write_Byte(crc_val,devaddr+Dev_Check);		//校验值重新写入
//	}
//}


/*******************************************************************************
* Function Name  : BLW_AirDetect_Read_State 
* Description    : BLWAirDetect空气质量检测状态读取函数
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路
* Return  : 无
*******************************************************************************/
uint16_t Dev_AirDetect_Read(uint32_t devaddr,uint16_t loop)
{
	if(devaddr == 0x00)
		return 0x00;
	
	RS485_AirDetect_INFO Rs485AirDetect;
	
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirDetect,sizeof(RS485_AirDetect_INFO),devaddr+Dev_Privately);	
	
	if(loop >= Rs485AirDetect.AirDetectInputValidNum)
	{
		return 0x00;    //超出范围 直接返回0
	}
	
	return Rs485AirDetect.DevReadBuf[loop];
}

/*******************************************************************************
* Function Name  :  read_air_detection_state 
* Description    :  读取空气质量检测状态
* Input          :  air_quality_detection - 空气质量检测私有变量指针
					loop - 读取的回路数
* Return         :  当前回路数的值
*******************************************************************************/
uint16_t read_air_detection_state(RS485_AirDetect_INFO *air_detection,uint8_t loop)
{
	uint16_t temp = 0x00;
	if(loop >= air_detection->AirDetectInputValidNum)
		return 0;
	if(loop==0x05||loop==0x06)
	{
		temp = air_detection->DevReadBuf[loop]/10;
		if((air_detection->DevReadBuf[loop]%10)<4)
		{
			return temp;
		}
		else
		{
			return temp+1;
		}
	}
	else 
	{
		return air_detection->DevReadBuf[loop];
	}
}

/*******************************************************************************
* Function Name  :  save_air_detection_state 
* Description    :  保存空气质量检测状态
* Input          :  bus_public - 设备共用变量指针
					air_quality_detection - 空气质量检测私有变量指针
* Return         :  none
*******************************************************************************/
void save_air_detection_state(Device_Public_Information_G *bus,RS485_AirDetect_INFO *air_detection)
{
	if(SysTick_1ms >= air_detection->AirDetectCycleTime)	//如果当前时间超过空气质量显示当前时间
	{
		air_detection->AirDetectCycleTime = SysTick_1ms + AIR_QUALITY_DETECTION_SAVE_TIME;	//更新当前时间
		for(uint8_t i = 0; i < air_detection->AirDetectInputValidNum; i++)
		{
			Udp_Addtion_Roomstate(DEV_RS485_AirDetect, bus->addr, i+1, read_air_detection_state(air_detection, i));
		}
	}
}
