/**
  ******************************************************************************
  * @file    BLV_Nor_VirtualTimeFun.c
  * @author  BLW RCU  development Team
  * @version V1.0.0
  * @date    2024年08月02日
  * @brief   时间设备  暂定白天黑夜功能
  ******************************************************************************
  */
  
#include "includes.h"




/*本地函数申明开始*/
void Dev_VirtualTime_Dis(uint32_t DevAddr);

uint16_t VirtualTime_Loop_State(uint32_t devaddr, uint16_t DevOutputLoop);
void BLW_VirtualTime_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t DevOutputLoop,uint16_t start);
uint8_t Dev_VirtualTime_InType_Get(uint32_t DevAddr, uint16_t DevInputLoop, uint16_t DevInputType);
/*本地函数申明结束*/

/*软时间新增到普通设备链表*/
void BLV_Nor_Dev_VirtualTime_Init(uint8_t devaddr)
{
	Device_Public_Information_G BUS_Public;
	VIRTUALTIME_STRUCT VirtualTime;       
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&VirtualTime,0,sizeof(VIRTUALTIME_STRUCT));
	
	BUS_Public.addr = devaddr;			      //设备地址
	BUS_Public.type = DEV_Virtual_Time;		  //设备类型
	BUS_Public.port = NULL;			          //设备发送端口
	BUS_Public.baud = 0x00;			          //设备发送波特率
	
	
	VirtualTime.DayStart = DEFAULT_DSTART;
	VirtualTime.DayEnd = DEFAULT_DEND;
	
	DevActionGlobal.DevActionU64Cond.TimeState = TIME_DAY;
	
	BUS_Public.DevFunInfo.Dev_Data_Process = Dev_VirtualTime_Dis;                 
	BUS_Public.DevFunInfo.Dev_Input_Type_Get = Dev_VirtualTime_InType_Get;        
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLW_VirtualTime_Control_State;        
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = VirtualTime_Loop_State;     
	
	Add_Nor_Device_To_List2(&BUS_Public,(uint8_t *)&VirtualTime,sizeof(VIRTUALTIME_STRUCT));
}


/*软时间新增到普通设备链表*/
void BLV_Nor_Dev_VirtualTime_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len)
{
	Device_Public_Information_G BUS_Public;
	VIRTUALTIME_STRUCT VirtualTime;
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&VirtualTime,0,sizeof(VIRTUALTIME_STRUCT));
	
	BUS_Public.addr = dev_info->addr;		//设备地址
	BUS_Public.type = dev_info->type;		//设备类型
	BUS_Public.port = NULL;	                //设备发送端口
	BUS_Public.baud = 0000;			        //设备发送波特率
	BUS_Public.retry_num = 0x03;		    //设备数据重发次数
	BUS_Public.wait_time = 0x0064;		    //设备数据发送等待回复时间
	
	VirtualTime.DayStart = DEFAULT_DSTART;
	VirtualTime.DayEnd = DEFAULT_DEND;
	
	DevActionGlobal.DevActionU64Cond.TimeState = TIME_DAY;
	
	BUS_Public.DevFunInfo.Dev_Data_Process = Dev_VirtualTime_Dis;                 
	BUS_Public.DevFunInfo.Dev_Input_Type_Get = Dev_VirtualTime_InType_Get;        
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLW_VirtualTime_Control_State;        
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = VirtualTime_Loop_State;     
	
	
	Add_Nor_Device_To_List2(&BUS_Public,(uint8_t *)&VirtualTime,sizeof(VIRTUALTIME_STRUCT));
}



/**
  * @name  指定软时间回路状态得到
  * @param  
  *    devaddr             当前设备的地址
  *    DevOutputLoop       需要查找的虚拟时间回路 从0开始
  * @retval 
  *    ret                 回路状态返回    1 开   2关
  * @brief

  */
uint16_t VirtualTime_Loop_State(uint32_t devaddr, uint16_t DevOutputLoop)
{	
	VIRTUALTIME_STRUCT VirtualTime;

	if(devaddr == 0x00)
		return 0x00;
	
	SRAM_DMA_Read_Buff((uint8_t *)&VirtualTime,sizeof(VIRTUALTIME_STRUCT),devaddr+Dev_Privately);	


	
	return 0x00;
}

/*******************************************************************************
* Function Name  : BLW_VirtualTime_Control_State 
* Description    : 虚拟时间设备控制
* Input          : 
	devaddr	: 设备信息地址
    loop    ：回路    回路都是从0
	start   ：状态    0x01开  0x02关
* Return  : 无
* attention : temp1非0才会进行保存
*******************************************************************************/
void BLW_VirtualTime_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t DevOutputLoop,uint16_t start)
{
	uint8_t temp1 = 0;
	Device_Public_Information_G BUS_Public;   //共用
	VIRTUALTIME_STRUCT VirtualTime;
	
	if(devaddr == 0x00)	return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&VirtualTime,sizeof(VIRTUALTIME_STRUCT),devaddr+Dev_Privately);

	
	
	
	
	if(temp1 != 0x00)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&VirtualTime, sizeof(VIRTUALTIME_STRUCT));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&VirtualTime,sizeof(VIRTUALTIME_STRUCT),devaddr+Dev_Privately);
		
	}	
}


/**
*@name 软时间输入检测函数
*@para
*  DevAddr      设备地址
*  DevInputLoop 回路输入地址
*  DevInputType 输入类型
*/
uint8_t Dev_VirtualTime_InType_Get(uint32_t DevAddr, uint16_t DevInputLoop, uint16_t DevInputType)
{
	Device_Public_Information_G BUS_Public;   //共用	
	VIRTUALTIME_STRUCT VirtualTime;
	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 *)&VirtualTime,sizeof(VIRTUALTIME_STRUCT),DevAddr+Dev_Privately);	/*拷贝设备私有信息*/
	
	switch(DevInputType)
	{
		default:
			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 *)&VirtualTime, sizeof(VIRTUALTIME_STRUCT));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&VirtualTime,sizeof(VIRTUALTIME_STRUCT),DevAddr+Dev_Privately);
		
	}
	
	return Ret;
	
}

/**
*@name 软时间扫描函数
*@para
*  DevAddr      设备地址
*@brief 拿到软定时器的状态或者RTC时间，并转换为动作
*/

void Dev_VirtualTime_Dis(uint32_t DevAddr)
{
	Device_Public_Information_G BUS_Public;   //共用	
	VIRTUALTIME_STRUCT VirtualTime;
	uint8_t KeepFlag = 0x00;
	
	uint32_t rcu_unix = 0;
	uint32_t daystart_unix = 0;
	uint32_t dayend_unix = 0;
	S_RTC daystart_rtc = {0};
	S_RTC dayend_rtc = {0};
	
	if(NULL == DevAddr)
	{
		return ;
	}
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&VirtualTime,sizeof(VIRTUALTIME_STRUCT),DevAddr+Dev_Privately); 
	
	if((VirtualTime.DayStart != DevActionGlobal.DayStart) || (VirtualTime.DayEnd != DevActionGlobal.DayEnd))
	{
		VirtualTime.DayStart = DevActionGlobal.DayStart;
		VirtualTime.DayEnd = DevActionGlobal.DayEnd;
		Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"白天时间范围发生变化  起始时间:%d  结束时间:%d",VirtualTime.DayStart,VirtualTime.DayEnd);
		KeepFlag = 0x01;
	}
	
	
	if(((DevActionGlobal.TimeSyncFlag & 0x01) == 0x01) || (g_time_info.time_select == 0x02))               //同步了网络时间或者使用硬件RTC时间
	{
		daystart_rtc.year = RTC_Raw_Data.year;
		daystart_rtc.month = RTC_Raw_Data.month;
		daystart_rtc.day = RTC_Raw_Data.day;
		daystart_rtc.hour = DEV_Conversion_To_HEX(VirtualTime.DayStart);
		
		dayend_rtc.year = RTC_Raw_Data.year;
		dayend_rtc.month = RTC_Raw_Data.month;
		dayend_rtc.day = RTC_Raw_Data.day;
		dayend_rtc.hour = DEV_Conversion_To_HEX(VirtualTime.DayEnd);
		
		rcu_unix = RTC_Conversion_To_Unix(&RTC_Raw_Data);
		daystart_unix = RTC_Conversion_To_Unix(&daystart_rtc);
		dayend_unix = RTC_Conversion_To_Unix(&dayend_rtc);
		
		if((rcu_unix >= daystart_unix) && (rcu_unix <= dayend_unix))
		{
			DevActionGlobal.DevActionU64Cond.TimeState = TIME_DAY;     //当前处于白天
//			Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"当前处于白天  起始时间:%d  结束时间:%d",VirtualTime.DayStart,VirtualTime.DayEnd);
		}
		else
		{
			DevActionGlobal.DevActionU64Cond.TimeState = TIME_NIGHT;     //当前处于黑夜
//			Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"当前处于黑夜  起始时间:%d  结束时间:%d",VirtualTime.DayStart,VirtualTime.DayEnd);
		}
	
		KeepFlag = 0x01;
	}
	else
	{
		DevActionGlobal.DevActionU64Cond.TimeState = TIME_DAY;     //当前处于白天
	}
	

	
	
	if(0x01 == KeepFlag)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&VirtualTime, sizeof(VIRTUALTIME_STRUCT));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&VirtualTime,sizeof(VIRTUALTIME_STRUCT),DevAddr+Dev_Privately);
	}
}


/*软时间新增到普通设备链表*/
void BLV_Nor_Dev_VirtualTime_Default_Init(void)
{
	Device_Public_Information_G BUS_Public;
	VIRTUALTIME_STRUCT VirtualTime;
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&VirtualTime,0,sizeof(VIRTUALTIME_STRUCT));
	
	BUS_Public.addr = 0x01;		            //设备地址
	BUS_Public.type = DEV_Virtual_Time;		//设备类型
	BUS_Public.port = NULL;	                //设备发送端口
	BUS_Public.baud = 0000;			        //设备发送波特率
//	BUS_Public.retry_num = 0x03;		    //设备数据重发次数
//	BUS_Public.wait_time = 0x0064;		    //设备数据发送等待回复时间
	
	VirtualTime.DayStart = DEFAULT_DSTART;
	VirtualTime.DayEnd = DEFAULT_DEND;
	
	DevActionGlobal.DevActionU64Cond.TimeState = TIME_DAY;
	
	BUS_Public.DevFunInfo.Dev_Data_Process = Dev_VirtualTime_Dis;                 
	BUS_Public.DevFunInfo.Dev_Input_Type_Get = Dev_VirtualTime_InType_Get;        
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLW_VirtualTime_Control_State;        
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = VirtualTime_Loop_State;     
	
	
	Add_Nor_Device_To_List2(&BUS_Public,(uint8_t *)&VirtualTime,sizeof(VIRTUALTIME_STRUCT));
}


