#include "includes.h"


void Dev_NoCard_Dis(uint32_t DevAddr);
uint8_t Dev_NoCard_InType_Get(uint32_t DevAddr, uint16_t DevInputLoop, uint16_t DevInputType);
void BLV_NoCard_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t DevOutputLoop,uint16_t start);



void BLV_Nor_Dev_NoCard_Init(uint8_t devaddr)
{
	Device_Public_Information_G BUS_Public;
	NOR_NoCard_INFO NoCard_Inf;
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&NoCard_Inf,0,sizeof(NOR_NoCard_INFO));
	
	BUS_Public.addr = devaddr;			//设备地址
	BUS_Public.type = DEV_Virtual_NoCard;		//设备类型
	BUS_Public.port = NULL;			//设备发送端口
	BUS_Public.baud = 0000;			//设备发送波特率
//	BUS_Public.retry_num = 0x03;		//设备数据重发次数
//	BUS_Public.wait_time = 0x0064;		//设备数据发送等待回复时间
//	BUS_Public.polling_cf = (uint32_t)&BLW_Touch_SwitchCycleDis;
//	BUS_Public.processing_cf = (uint32_t)&BLW_Rs485_Touch_Swi_Check;
	
	BUS_Public.DevFunInfo.Dev_Data_Process = Dev_NoCard_Dis;                  //
	BUS_Public.DevFunInfo.Dev_Input_Type_Get = Dev_NoCard_InType_Get;         //
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLV_NoCard_Control_State;         //
//	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get =      //
	
	
	NoCard_Inf.DevC5IOAddr = Find_AllDevice_List_Information(DEV_C5IO_Type, 0x00);
	
	NoCard_Inf.AllPercent = 80;       //2024-04-24
	NoCard_Inf.OnPercent = 40;        //2024-05-09
	
	NoCard_Inf.Port_State[0].Port = 8;
	NoCard_Inf.Port_State[1].Port = 9;
	NoCard_Inf.Port_State[2].Port = 10;
	NoCard_Inf.Port_State[3].Port = 11;
	NoCard_Inf.Port_State[4].Port = 12;
//	NoCard_Inf.Port_State[5].Port = 4;
	
	
	Add_Nor_Device_To_List2(&BUS_Public,(uint8_t *)&NoCard_Inf,sizeof(NOR_NoCard_INFO));
}






void BLV_Nor_Dev_NoCard_For_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len)
{
	Device_Public_Information_G BUS_Public;
	NOR_NoCard_INFO NoCard_Inf;
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&NoCard_Inf,0,sizeof(NOR_NoCard_INFO));

	BUS_Public.addr = dev_info->addr;		//设备地址
	BUS_Public.type = DEV_Virtual_NoCard;		//设备类型
	BUS_Public.port = dev_info->type;		//设备发送端口
	BUS_Public.baud = 0000;			        //设备发送波特率

	BUS_Public.DevFunInfo.Dev_Data_Process = Dev_NoCard_Dis;                  //
	BUS_Public.DevFunInfo.Dev_Input_Type_Get = Dev_NoCard_InType_Get;         //
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLV_NoCard_Control_State;         //
//	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get =      //
	
	
	
	NoCard_Inf.DevC5IOAddr = Find_AllDevice_List_Information(DEV_C5IO_Type, 0x00);
	
	NoCard_Inf.AllPercent = 80;       //2024-04-24
	NoCard_Inf.OnPercent = 40;        //2024-05-09
	
	for(uint8_t i = 0; i < 5; i++)
	{
		if(dev_info->lin[0] >= 1)
		{
			NoCard_Inf.Port_State[i].Port = dev_info->lin[i]-1;
		}
	}
	
//	NoCard_Inf.Port_State[0].Port = dev_info->lin[0]-1;
//	NoCard_Inf.Port_State[1].Port = dev_info->lin[1]-1;
//	NoCard_Inf.Port_State[2].Port = dev_info->lin[2]-1;
//	NoCard_Inf.Port_State[3].Port = dev_info->lin[3]-1;
//	NoCard_Inf.Port_State[4].Port = dev_info->lin[4]-1;
	
	
	NoCard_Inf.Port_State[5].Port = 0x03;   //默认4号端口
	NoCard_Inf.Port_State[6].Port = 0x04;   //默认5号端口
	

	Add_Nor_Device_To_List2(&BUS_Public,(uint8_t *)&NoCard_Inf,sizeof(NOR_NoCard_INFO));

}



void Dev_NoCard_Dis(uint32_t DevAddr)
{
	Device_Public_Information_G BUS_PublicNoCard;   //共用	
	NOR_NoCard_INFO NoCard_Inf;
	
	Device_Public_Information_G BUS_PublicC5IO;   //共用		
	BUS_C5IO_INFO C5IO_Info;
	
	uint8_t count = 0;
	
	uint8_t KeepFlag = 0x00;
	uint8_t i = 0;
	
	uint8_t pencent = 0;
	uint16_t allper = 0;
	
	static uint32_t temp_tick = 0;
	
	if(NULL == DevAddr)
	{
		return ;
	}
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_PublicNoCard,sizeof(Device_Public_Information_G),DevAddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&NoCard_Inf,sizeof(NOR_NoCard_INFO),DevAddr+Dev_Privately); 
	
	
	if(NULL == NoCard_Inf.DevC5IOAddr)
	{
		return ;
	}
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_PublicC5IO,sizeof(Device_Public_Information_G),NoCard_Inf.DevC5IOAddr);	//拷贝所有共有数据			
	SRAM_DMA_Read_Buff((uint8_t *)&C5IO_Info,sizeof(BUS_C5IO_INFO),NoCard_Inf.DevC5IOAddr+Dev_Privately);
	
//	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"Dev_NoCard_Dis");
	
	
	if((DevActionGlobal.People_Flag == 0x01) && (NoCard_Inf.CtrlFlag == 0x01))     //有人按按键提前结束有人无人判断统计，判定为有人
	{
		NoCard_Inf.CtrlFlag = 0x00;
		DevActionGlobal.People_Flag = 0x00;
		NoCard_Inf.CtrlTick = SysTick_1s;
		Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"Someone press the switch,have people");
		LOG_LogicInfo_DebugRecord("Someone press the switch,have people\n\r");
		KeepFlag = 0x01;
	}
	
	
	
	if(NoCard_Inf.CtrlFlag == NOCARD_OPEN)              //开始统计
	{
		for(i = 0; i < PORT_NUM_MAX; i++)
		{
//			Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"----%d NoCard Port %d  Type:%d----",i,NoCard_Inf.Port_State[i].Port,((C5IO_Info.DI_Type[NoCard_Inf.Port_State[i].Port] & 0x0F)));
			if(NoCard_Inf.Port_State[i].Port != 0x00)
			{
				switch((C5IO_Info.DI_Type[NoCard_Inf.Port_State[i].Port] & 0x0F))
				{
					case BUS_C5IO_DI_Key_Type:
						break;
					case BUS_C5IO_DI_Pir_Type:
					case BUS_C5IO_DI_Dry_Type:
						if(NoCard_Inf.Port_State[i].State != 0x01)      //处于非触发状态
						{
							if(C5IO_Info.DI_Start[NoCard_Inf.Port_State[i].Port] == 0x01)
							{
								NoCard_Inf.Port_State[i].State = 0x01;      //触发
								NoCard_Inf.Port_State[i].Tick = SysTick_1ms;
								Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"NoCard Port %d Press",NoCard_Inf.Port_State[i].Port+1);
								LOG_LogicInfo_DebugRecord("NoCard Port %d Press\n\r",NoCard_Inf.Port_State[i].Port+1);
							}
						}
						else if(NoCard_Inf.Port_State[i].State != 0x00) //处于触发状态
						{
							if(C5IO_Info.DI_Start[NoCard_Inf.Port_State[i].Port] == 0x00)
							{
								NoCard_Inf.Port_State[i].State = 0x00;      //释放
								Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"NoCard Port %d Release",NoCard_Inf.Port_State[i].Port+1);
								LOG_LogicInfo_DebugRecord("NoCard Port %d Release",NoCard_Inf.Port_State[i].Port+1);
							}
							NoCard_Inf.Port_State[i].OnTime += SysTick_1ms - NoCard_Inf.Port_State[i].Tick;   //累加触发时间
							NoCard_Inf.Port_State[i].Tick = SysTick_1ms;
	//						Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"NoCard Port %d OnTime",NoCard_Inf.Port_State[i].OnTime);
						}
						break;
					default:
						break;
				}
			
			}
		}
		
		if(SysTick_1s - temp_tick > 30)
		{
			temp_tick = SysTick_1s;
			for(i = 0; i < PORT_NUM_MAX; i++)
			{
				pencent = (((NoCard_Inf.Port_State[i].OnTime*100)/1000)/(NoCard_Inf.OffTime*60));
				allper += pencent;
				
				Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"NoCard In Statistics,Port:%d  OP:%d LP:%d PAP:%d PLP:%d" ,\
				NoCard_Inf.Port_State[i].Port+1,pencent,NoCard_Inf.OnPercent,allper,NoCard_Inf.AllPercent);
				
				LOG_LogicInfo_DebugRecord("NoCard In Statistics,Port:%d  OP:%d  LP:%d  PAP:%d  PLP:%d",\
				NoCard_Inf.Port_State[i].Port+1,pencent,NoCard_Inf.OnPercent,allper,NoCard_Inf.AllPercent);
			}
		}
		
		
		

		if(SysTick_1s - NoCard_Inf.CtrlTick > NoCard_Inf.OffTime*60)
		{
			NoCard_Inf.CtrlTick = SysTick_1s;
			
			for(i = 0; i < PORT_NUM_MAX; i++)
			{
				if((NoCard_Inf.Port_State[i].OnTime/1000 < NoCard_Inf.OffTime*60*NoCard_Inf.OnPercent/100))       // && (NoCard_Inf.Port_State[i].Count <= NoCard_Inf.OnCount)
				{
					count++;
				}
				pencent = (((NoCard_Inf.Port_State[i].OnTime*100)/1000)/(NoCard_Inf.OffTime*60));
				allper += pencent;
				
				Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"NoCard Finish,Port:%d  OP:%d LP:%d PAP:%d PLP:%d" ,\
				NoCard_Inf.Port_State[i].Port+1,pencent,NoCard_Inf.OnPercent,allper,NoCard_Inf.AllPercent);
				
				LOG_LogicInfo_DebugRecord("NoCard Finish,Port:%d  OP:%d  LP:%d  PAP:%d  PLP:%d",\
				NoCard_Inf.Port_State[i].Port+1,pencent,NoCard_Inf.OnPercent,allper,NoCard_Inf.AllPercent);
			}
			
			if(count == PORT_NUM_MAX)
			{
				if(allper < NoCard_Inf.AllPercent)
				{
					NoCard_Inf.Action = NOCARD_OPEN;
					Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"---No People---");
					LOG_LogicInfo_DebugRecord("No People");
				}
				else
				{
					Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"---Have People---");
					LOG_LogicInfo_DebugRecord("Have People");
				}
				
			}
			else
			{
				Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"---Have People---");
				LOG_LogicInfo_DebugRecord("Have People");
			}
			
			NoCard_Inf.CtrlFlag = 0x00;
		}
		KeepFlag = 0x01;
	}
	
	
	
	
	
	
	
	if(0x01 == KeepFlag)
	{
		BUS_PublicNoCard.check = 0x00;
		BUS_PublicNoCard.check = DoubleData_CheckSum((uint8_t *)&BUS_PublicNoCard, sizeof(Device_Public_Information_G), (uint8_t *)&NoCard_Inf, sizeof(NOR_NoCard_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_PublicNoCard, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&NoCard_Inf,sizeof(NOR_NoCard_INFO),DevAddr+Dev_Privately);	

		/*保存C5IO的数据*/		
		BUS_PublicC5IO.check = 0x00;
		BUS_PublicC5IO.check = DoubleData_CheckSum((uint8_t *)&BUS_PublicC5IO, sizeof(Device_Public_Information_G), (uint8_t *)&C5IO_Info, sizeof(BUS_C5IO_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_PublicC5IO, sizeof(Device_Public_Information_G),NoCard_Inf.DevC5IOAddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&C5IO_Info,sizeof(BUS_C5IO_INFO),NoCard_Inf.DevC5IOAddr+Dev_Privately);
	}
	
}



uint8_t Dev_NoCard_InType_Get(uint32_t DevAddr, uint16_t DevInputLoop, uint16_t DevInputType)
{
	Device_Public_Information_G BUS_Public;   //共用	
	NOR_NoCard_INFO NoCard_Inf;
	uint8_t Ret = CtrlInvalid;         //控制无效
	
	if(DevAddr == 0x00)	return Ret;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&NoCard_Inf,sizeof(NOR_NoCard_INFO),DevAddr+Dev_Privately);	/*拷贝设备私有信息*/
	
	switch(DevInputType)
	{
		case NOCARD_OPEN:
		case NOCARD_CLOSE:
			if(DevInputType == NoCard_Inf.Action)
			{
				NoCard_Inf.Action = NULL;
				Ret = CtrlValid;
			}
			break;
	}
	
	
	if(CtrlValid == Ret)
	{
		Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"无卡断电动作清空");

		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&NoCard_Inf, sizeof(NOR_NoCard_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&NoCard_Inf,sizeof(NOR_NoCard_INFO),DevAddr+Dev_Privately);
		
	}
	
	return Ret;
}



void BLV_NoCard_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t DevOutputLoop,uint16_t start)
{
	uint8_t temp1 = 0,temp_start = 0,temp_content = 0;
	Device_Public_Information_G BUS_Public;   //共用
	NOR_NoCard_INFO NoCard_Inf;
	
	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"BLV_NoCard_Control_State");
	
	if(devaddr == 0x00)	return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&NoCard_Inf,sizeof(NOR_NoCard_INFO),devaddr+Dev_Privately);
	
	
	
	temp_start = start&0xFF;
	temp_content = (start >> 8) & 0xFF;
	
	
	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"NoCard Ctrl:%d  %d",temp_start,temp_content);
	
	switch(temp_start)
	{
		case NOCARD_OPEN:
		case NOCARD_CLOSE:
			NoCard_Inf.CtrlFlag = temp_start;
			NoCard_Inf.CtrlTick = SysTick_1s;
			NoCard_Inf.OffTime = temp_content;
		
			NoCard_Inf.Port_State[0].OnTime = 0;
			NoCard_Inf.Port_State[1].OnTime = 0;
			NoCard_Inf.Port_State[2].OnTime = 0;
			NoCard_Inf.Port_State[3].OnTime = 0;
			NoCard_Inf.Port_State[4].OnTime = 0;
			NoCard_Inf.Port_State[5].OnTime = 0;
			NoCard_Inf.Port_State[6].OnTime = 0;
		
			NoCard_Inf.Port_State[0].State = 0;
			NoCard_Inf.Port_State[1].State = 0;
			NoCard_Inf.Port_State[2].State = 0;
			NoCard_Inf.Port_State[3].State = 0;
			NoCard_Inf.Port_State[4].State = 0;
			NoCard_Inf.Port_State[5].State = 0;
			NoCard_Inf.Port_State[6].State = 0;
			
			NoCard_Inf.Port_State[0].Tick = SysTick_1ms;
			NoCard_Inf.Port_State[1].Tick = SysTick_1ms;
			NoCard_Inf.Port_State[2].Tick = SysTick_1ms;
			NoCard_Inf.Port_State[3].Tick = SysTick_1ms;
			NoCard_Inf.Port_State[4].Tick = SysTick_1ms;
			NoCard_Inf.Port_State[5].Tick = SysTick_1ms;
			NoCard_Inf.Port_State[6].Tick = SysTick_1ms;
			
			temp1++;
			
			if(temp_start == NOCARD_OPEN)
			{
				LOG_LogicInfo_DebugRecord("Door Close\n\r");
			}
			else if(temp_start == NOCARD_CLOSE)
			{
				LOG_LogicInfo_DebugRecord("Door Open\n\r");
			}
			
			break;
		case NOCARD_ONCOUNT:
			NoCard_Inf.AllPercent = temp_content;
			temp1++;
			break;
		case NOCARD_PERCENT:
			NoCard_Inf.OnPercent = temp_content;
			temp1++;
			break;
		default:    //	其他情况直接返回
			break;
	}
	
	if(temp1 != 0x00)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&NoCard_Inf, sizeof(NOR_NoCard_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&NoCard_Inf,sizeof(NOR_NoCard_INFO),devaddr+Dev_Privately);
	}	
}



