#include "type.h"
#include "UWater_uart_driver.h"
#include "UWater_DebugPrint.h"
#include "UWater_frame_app.h"
#include "UWater_eeprom_driver.h"
#include "UWater_rtcc_app.h"
#include "SOE.h"


void SaveNewSoe(u16 soe_type,u8 soe_data);

SOE soe_event;
//SOE_TIME SOE_START_TIME =  {0x00,0x01,0x01,0x00,0x00,0x00}; //2000-01-01 00:00:00
//SOE_TIME SOE_END_TIME   =  {0x38,0x01,0x19,0x03,0x14,0x07}; //2038-01-19 03:14:07
//SOE_TIME SOE_END_TIME   =  {0x99,0x12,0x31,0x23,0x59,0x59}; //2038-01-19 03:14:07
u8 save_soe_state;
DateTime rtcc_time_soe;
u8 soe_tmp_buf[20];
u8 soe_ee_msg;

/*************************************************************************************
* @fun_name:	SaveNewSoe -- 记录一条新的SOE
* @param[in] :	soe_type--类型   soe_data--数据(默认为0)
*************************************************************************************/
void SaveNewSoe(u16 soe_type,u8 soe_data)
{
	if(soe_event.soe_cnt > SOE_NUM)
	{
		soe_event.soe_cnt = 0;
	}
	soe_event.soe_cnt++;
	soe_event.soe_cnt %= SOE_NUM;
	RtccGetDateTime(&rtcc_time_soe);
	soe_event.soe_type[soe_event.soe_cnt] = soe_type;
    soe_event.soe_time[soe_event.soe_cnt].s.year   = rtcc_time_soe.s.year;
    soe_event.soe_time[soe_event.soe_cnt].s.month  = rtcc_time_soe.s.month;
    soe_event.soe_time[soe_event.soe_cnt].s.day    = rtcc_time_soe.s.day;
    soe_event.soe_time[soe_event.soe_cnt].s.hour   = rtcc_time_soe.s.hour;
    soe_event.soe_time[soe_event.soe_cnt].s.minute = rtcc_time_soe.s.minute;
    soe_event.soe_time[soe_event.soe_cnt].s.second = rtcc_time_soe.s.second;
	soe_event.soe_data[soe_event.soe_cnt] = soe_data;	
}
//
u8 SaveSoeToEEIfIdle(void)
{
	if(SAVE_SOE_S0 == save_soe_state)
	{
		return TASK_IDLE;
	}
	else
	{
		return TASK_BUSY;
	}
}
//存SOE内容到EE
void SaveSoeToEE(void)
{
	switch(save_soe_state)
	{
		case SAVE_SOE_S0:
		{			
			if(soe_event.soe_cnt != soe_event.soe_record_cnt)
			{
				soe_event.soe_record_cnt++;
				soe_event.soe_record_cnt %= SOE_NUM;
				save_soe_state = SAVE_SOE_S1;
			}			
		}
		break;
		
		case SAVE_SOE_S1:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				soe_tmp_buf[0] = soe_event.soe_type[soe_event.soe_record_cnt];
				soe_tmp_buf[1] = soe_event.soe_type[soe_event.soe_record_cnt]>>8;
				soe_tmp_buf[2] = soe_event.soe_time[soe_event.soe_record_cnt].s.year;
				soe_tmp_buf[3] = soe_event.soe_time[soe_event.soe_record_cnt].s.month;
				soe_tmp_buf[4] = soe_event.soe_time[soe_event.soe_record_cnt].s.day;
				soe_tmp_buf[5] = soe_event.soe_time[soe_event.soe_record_cnt].s.hour;
				soe_tmp_buf[6] = soe_event.soe_time[soe_event.soe_record_cnt].s.minute;
				soe_tmp_buf[7] = soe_event.soe_time[soe_event.soe_record_cnt].s.second;
				soe_tmp_buf[8] = soe_event.soe_data[soe_event.soe_record_cnt];

				if(Sys_data_object.P4.Point_SOE_write >= TOTAL_NUM_SOE) 
				{
					Sys_data_object.P4.Point_SOE_write = 0;
				}
				EepWrite(soe_tmp_buf, EEP_ADDR_SOE_BASE + (u16)Sys_data_object.P4.Point_SOE_write*LENGTH_ONE_SOE, LENGTH_ONE_SOE, &soe_ee_msg);
				save_soe_state = SAVE_SOE_S2;				
			}
		}
		break;
		
		case SAVE_SOE_S2:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				Sys_data_object.P4.Point_SOE_write++;  
				Sys_data_object.P4.Point_SOE_write %= TOTAL_NUM_SOE;
				EepWrite((u8*)(&Sys_data_object.P4.Point_SOE_write), EEP_ADDR_SOE_WR_POINT,sizeof(Sys_data_object.P4.Point_SOE_write),&soe_ee_msg);   
				save_soe_state = SAVE_SOE_S0;					
			}
		}
		break;
		
		default:
		{
			save_soe_state = SAVE_SOE_S0;				
		}
		break;		
	}
}
//
void SoeInit(void)
{
	soe_event.soe_cnt = 0;
	soe_event.soe_record_cnt = 0;
}
void SoeDealApp(void)
{
	SaveSoeToEE();
}
/*************************************************************************************
* @fun_name:	Search_Soe -- 按照筛选条件 检索SOE
* @brief   :
* @param[in] :	  soe_type--SOE类型 (0xFFFF表示不限定类型)
								start_time--起始时间(BCD前些 00年01月01日 00:00:00 表示读取最新SOE)
								end_time--结束时间(BCD填写当前时间，表示读取最新SOE)
								num--要检索几条 (0XFF-不限定条数)
								soe_read--检索到后存放指针
								检索顺序为最新发生的事件在前
* @param[out] :	u8 检索到有效的SOE条数
* @retval     :	None
* @other     :  None
*************************************************************************************/
u8 Check_One_Soe (u16 soe_type,DateTime_noweek *start_time,DateTime_noweek *end_time,ONE_SOE *soe_read)
{
	time_t start_time_Greene,end_time_Greene,soe_time_Greene;//格林威治时间：从1970年1月1日开始到现在的秒数
    start_time_Greene = get_mktime_noweek(start_time);
    end_time_Greene   = get_mktime_noweek(end_time);
	if(((soe_read->soe_type > SOE_START_ID0)&&(soe_read->soe_type < SOE_END_ID0))\
	||((soe_read->soe_type > SOE_START_ID1)&&(soe_read->soe_type < SOE_END_ID1)))
	{   //确保是有效的SOE事件
		if((soe_read->soe_type == soe_type) || (soe_type == SOE_ALL_TYPE))//是需要读取的类型
		{
			soe_time_Greene = get_mktime_noweek(&soe_read->soe_time);
			
			if((soe_time_Greene >= start_time_Greene) && (soe_time_Greene <= end_time_Greene)) //是发生在读取时间段内
			{   //又检索到一条有效的SOE
		      return TRUE;
			}
		}
	}
	return FALSE;
}


