#include "includes.h"


uint8_t BLVP1_Rotary_SwitchCycleDis(uint32_t DevAdd);  
uint8_t BLVP1_Rs485_Rotary_Swi_Check(uint32_t DevAdd ,uint32_t Data_addr, uint16_t DataLen); 

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

#define Dev_RotarySwitch_RecvData_Len_Max	0x18

/*******************************************************************************
* Function Name  : BLVP1_485_Dev_Rotary_Switch_Init 
* Description    : BLVP1旋钮开关初始化函数
* Return         : NULL
*******************************************************************************/
void BLVP1_485_Dev_Rotary_Switch_Init(Device_Public_Information_G *BUS_Public, RS485_SWI_INFO *Rs485SwiInfo)
{
	BUS_Public->polling_cf = (uint32_t)&BLVP1_Rotary_SwitchCycleDis;
	BUS_Public->processing_cf = (uint32_t)&BLVP1_Rs485_Rotary_Swi_Check;
	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"增加BLVP1旋钮开关 地址: %d",BUS_Public->addr);										
	
}



/*******************************************************************************
* Function Name  : BLVP1_Rotary_Rs485_Swi_Pro 
* Description    : BLVP1旋钮开关485接收处理函数
* Return         : NULL
*******************************************************************************/
void BLVP1_Rotary_Rs485_Swi_Pro(Device_Public_Information_G* BUS_Public, uint8_t *data, RS485_SWI_INFO  *Switch_Info)
{
	uint8_t i;

	if(Switch_Info->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(&Switch_Info->comm_record,0x02,0x01);		//记录通讯成功
	Switch_Info->DevSendCnt = 0x00;             		//收到回复就清计数
	Switch_Info->DevOffline = DEV_IS_ONLINE;    		//设备在线
	if(Switch_Info->DevOffline != Switch_Info->DevOfflineLast)	//当前在线状态不等于上一次在线状态
	{
		Switch_Info->DevOfflineLast = Switch_Info->DevOffline;	//更新上一次在线状态
		Write_Device_Fault_State(BUS_Public->type,BUS_Public->addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
	}

	
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Rotary switch Input Num:%02X", Switch_Info->SwtInputValidNum);
	switch(data[1])   
	{
		case 0x09:      //2024-1-25
			if(data[2] == 0x05)
			{
				i = data[2] - 1;
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"switch%d Dev%02X KeyRotary Coord:%d",BUS_Public->addr, i, BUS_Public->DevCoord);
				Switch_Info->DevReadBuf[i] = RotaryLight;          //地址从1开始
				DevActionGlobal.Devi = BUS_Public->DevCoord;     //直接跳到指定的下标去处理
//				if(data[3] > 0)
				{
					Switch_Info->RotaryValue[0] = data[3];
					DevActionGlobal.DimGlobalValue = Switch_Info->RotaryValue[0];
				}
			}
			break;
		default:
			break;
	}
}


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

/*******************************************************************************
* Function Name  : BLVP1_Rs485_Rotary_Swi_Check 
* Description    : BLVP1旋钮开关485接收处理函数
* Return         : 
	0x01：数据处理失败
	0x00：数据处理成功
*******************************************************************************/
uint8_t BLVP1_Rs485_Rotary_Swi_Check(uint32_t DevAdd ,uint32_t Data_addr, uint16_t DataLen) 
{
//	uint8_t Crc_buf = 0;
	uint8_t ret = 0x01;
	uint8_t data[RECDATALENMAX];	
//	uint32_t SwiDevAdd;    //开关
	Device_Public_Information_G BUS_Public;   //共用	
	RS485_SWI_INFO Rs485SwiInfo;       //开关局部变量
	
	if(DataLen > RECDATALENMAX)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s Len:%d Err",__func__,DataLen);
		return ret;     //长度
	}
	
	SRAM_DMA_Read_Buff(data,DataLen,Data_addr);   //拷贝482数据
		
	
	if((DataLen < 0x05)||((data[1] != 0x09)&&(data[1] != 0x0A))||(data[DataLen - 1] != SOR_CRC(data, DataLen-1)))   //匹配开关地址数据  
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"不是旋钮开关的数据，DevAdd:%d,len:%d",BUS_Public.addr,DataLen);
		return ret;    //不是就直接返回
	}
	
//	if(data[0] == SRAM_Read_Byte(DevAdd+Dev_Addr))   //地址刚好匹配上BUS_Public.addr
//	{
//		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"旋钮开关地址匹配上 DevAdd:%d,len:%d\r\n",data[0],DataLen);
//		SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAdd);		
//	}
//	else       //地址没有匹配上
	{
		DevAdd = Find_AllDevice_List_Information2(Active_Port, 0x25, data[0]);   //地址重新赋值
		if(NULL != DevAdd)
		{
			SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAdd);		
		}
		else
		{
			return ret;
		}
	}
	 

	SRAM_DMA_Read_Buff((uint8_t *)&Rs485SwiInfo,sizeof(RS485_SWI_INFO),DevAdd+Dev_Privately);
	
	switch(data[1])
	{
		case 0x09:
			ret = 0x00;
			BLVP1_Rotary_Rs485_Swi_Pro(&BUS_Public, data, &Rs485SwiInfo);
			break;
		case 0x0A:
			ret = 0x00;
			Rs485SwiInfo.DevSendCnt = 0x00;
			Rs485SwiInfo.RotaryValue_Flag = 0x00;
			break;
	}
	
	/*日志记录*/
	LOG_Device_COMM_Control_Reply_Record(BUS_Public.port,BUS_Public.baud,data,DataLen);
	
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485SwiInfo, sizeof(RS485_SWI_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAdd);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&Rs485SwiInfo,sizeof(RS485_SWI_INFO),DevAdd+Dev_Privately);
		
	return ret;
}



void BLVP1_Rotary_Switch_ctrl(Device_Public_Information_G *BUS_Public, RS485_SWI_INFO *Rs485SwiInfo)
{
	uint8_t SendLen = 0x05;							
	uint8_t BLW_swt_buf[SendLen]; 
	
	memset(BLW_swt_buf,0,sizeof(BLW_swt_buf));

	BLW_swt_buf[0] = BUS_Public->addr;
	BLW_swt_buf[1] = 0x0A;   //写
	BLW_swt_buf[2] = 0x05;   //回路
	BLW_swt_buf[3] = DevActionGlobal.DimGlobalValue;   //全局亮度
	

	BLW_swt_buf[SendLen-1] = SOR_CRC(BLW_swt_buf,SendLen-1);
	/*发送函数*/
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s",__func__);
	MCU485_SendString(BUS_Public->port,BLW_swt_buf,SendLen);
	
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,BLW_swt_buf,SendLen);
}

/*******************************************************************************
* Function Name  : BLVP1_Rotary_SwitchCycleDis 
* Description    : BLVP1旋钮开关485数据发送处理函数，包括重发

* Return         : 
	0x01：有数据需要发送
	0x00：没有数据需要发送
*******************************************************************************/
uint8_t BLVP1_Rotary_SwitchCycleDis(uint32_t DevAdd)  
{
	Device_Public_Information_G BUS_Public;
	RS485_SWI_INFO Rs485SwiInfo;       //开关局部变量

//	uint8_t i;
	uint8_t Ret = RS485OCCUPYNOTIME;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAdd);			
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485SwiInfo,sizeof(RS485_SWI_INFO),DevAdd+Dev_Privately);/*拷贝设备私有信息*/
	
	
	

	if(Rs485SwiInfo.RotaryValue[0] != DevActionGlobal.DimGlobalValue)       //2025-07-10,YYW,标记，亮度与全局亮度比对
	{
		Rs485SwiInfo.RotaryValue[0] = DevActionGlobal.DimGlobalValue;
		Rs485SwiInfo.RotaryValue_Flag = 0x01;
	}
	
	
	if(0x01 == Rs485SwiInfo.RotaryValue_Flag)
	{
		BLVP1_Rotary_Switch_ctrl(&BUS_Public, &Rs485SwiInfo);
		Ret = RS485OCCUPYTIME;
	}
		
	if(Ret == RS485OCCUPYTIME)
	{
		if(Rs485SwiInfo.DevSendCnt > REPEATSENDTIMEMAX)
		{
			if(Rs485SwiInfo.DevOffline != DEV_IS_OFFLINE)
			{
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Dev_RotarySwitch LogInfo_Device_Offline...........");
				LOG_Device_Online_Record(Dev_Rs458_RotaryCtrl, BUS_Public.addr,LogInfo_Device_Offline);	//记录设备离线
			}

			Rs485SwiInfo.DevOffline = DEV_IS_OFFLINE;   //判为离线
			if(Rs485SwiInfo.DevOffline != Rs485SwiInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
			{
				Rs485SwiInfo.DevOfflineLast = Rs485SwiInfo.DevOffline;	//更新上一次在线状态
				Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_OFFLINE);	//保存设备在线状态到SRAM
			}
			
			Rs485SwiInfo.RotaryValue_Flag = 0x00; 
		}
		else
		{
			Rs485SwiInfo.DevSendCnt++;   //发送次数累计
		}
		
		/*通讯统计记录*/
		BLV_Communication_Record(&Rs485SwiInfo.comm_record,0x01,0x00);
		
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485SwiInfo, sizeof(RS485_SWI_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAdd);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485SwiInfo,sizeof(RS485_SWI_INFO),DevAdd+Dev_Privately);
	}
	
		
	return Ret;
}

