#include "sys_config.h"
#if PRODUCT_SY
#include "UWater_frame_app.h"
#include "UWater_eeprom_driver.h"
#include "UWater_rtcc_driver.h"
#include "UWater_rtcc_app.h"
#include "UWater_lcd_app.h"	
#include "UWater_gp22_app.h"
#include "UWater_DataTools_App.h"
#include "UWater_pt_app.h"
#include "UWater_valve_driver.h"
#include "UWater_valve_app.h"
#include "SOE.h"
#include "UWater_power_app.h"
#include "UWater_SY_Store_App.h" 
#include "sys_processor.h"
/**********公用变量************************************************************/
DateTime uTempRtccTime_BCD; 
TYP_STATE_SY step_ShenYangStoreInit=SY_STATE_S0; //EE存储参数初始化状态机
TYP_STATE_SY step_ShenYangStoreMachine=SY_STATE_S0;//沈阳规约数据存储主状态机
UNION_SY_STORE_INDEX  uStoreIndex_SY;//存储索引
u16 uTemp_ee_addr=0; 
u8 uTempMsg_EE=0;
u8 s_sy_eep_complete=0;
static u8 s_sy_clear_history_buf[256];
DateTime sy_protl_rtcc_time;
/**********周期数据存储相关变量***********************************************/
UNION_SY_STORE_CIRCLE temp_Circle_data;//周期数据存储缓存
u8 uStoreCircleIndex_SY=0; //周期数据存储地址索引
TYP_STATE_SY step_CircleDataStore=SY_STATE_S0;//周期数据存储状态机
u32 uUploadIndexTimeBuf[6];//周期数据指针时间排序
u8 uUploadIndexBuf[6];//周期数据指针排序
/**********冻结信息存储相关变量***********************************************/
UNION_SY_STORE_FROZEN temp_frozen_data;//冻结数据存储缓存
u8 *pTempBuf_DailyHighestFlow;//日最高流量指针
TYP_STATE_SY step_FrozenDataStore=SY_STATE_S0;//冻结数据存储状态机
TYP_STATE_SY step_FrozenDataClear = SY_STATE_S0;//冻结数据和周期数据清除
u8 uClearDataLen=0;
/**********报警信息存储相关变量***********************************************/
TYP_STATE_SY step_ShenYangSOEMachine=SY_STATE_S0;
u16 uSOE_Data_New=0;
u16 uSOE_Data_Old=0;
u8 uSOE_Valve_New=0;
u8 uSOE_Valve_Old=0;
u8 uSOE_Power_New=0;
u8 uSOE_Power_Old=0;
float uSOE_Temprature_New=0;
u8 uSOE_Temprature_ShortNew=0;
u8 uSOE_Temprature_ShortOld=0;
u8 uSOE_Temprature_OpenNew=0;
u8 uSOE_Temprature_OpenOld=0;
u32 uSOE_Update_Flag=0xFFFFFFFF;
u32 uSOE_Update_Flag_Old=0xFFFFFFFF;

u8 uWaterState=0;
DateTime ValveActiveDateTime;
u8 Valve_Active=0;
/**********TLV数据存储相关变量***********************************************/
TYP_STATE_SY step_TlvDataStore=SY_STATE_S0;
u8 temp_tlv_data[32];
Type_TLVValue TlvValue;
u8 tlv_store_cnt=0;
/**********周期数据上报***********************************************/
int SY_ThisPack_Count=0;
TYP_STATE_SY SY_CircleUploadStep=SY_STATE_S0;//上报状态机
/********************************************************************************
函数名：ShenYang_Store_InitApp
说  明：沈阳数据存储初始化
********************************************************************************/	
void ShenYang_Store_InitApp(void) 
{
	TYP_STATE_SY tmp_init_stat;
	
    while (tmp_init_stat)
    {
      switch (tmp_init_stat)
      {
        case SY_STATE_S0: 
        {
			if(TASK_IDLE == EepIfIdle())
            {
                tmp_init_stat = SY_STATE_S0;
            }
            break;
        }
        case SY_STATE_S1: 
        {
			if (TASK_IDLE == EepIfIdle())
            {
				EepRead(EEP_ADDR_SY_STORE_INDEX_CIRCLE, &uStoreIndex_SY.data_buf[0], SIZE_SY_STORE_INDEX, &uTempMsg_EE);
                tmp_init_stat = SY_STATE_S2;
            }
            break;
        }
		case SY_STATE_S2://非第一次上电需要读取参数
		{
			if (TASK_IDLE == EepIfIdle())
            {
				EepRead(EEP_ADDR_SY_TLVDATA,&TlvValue.data_buf[0], TLV_VALUE_LEN, &s_sy_eep_complete);
				tmp_init_stat  =SY_STATE_S3;				
			}
			break;
		}
		
		case SY_STATE_S3://判断第一次上电标志
		{
			if (TASK_IDLE == EepIfIdle())
            {
				if(uStoreIndex_SY.Data.firstPowerOn != SY_FIRST_POWERON_FLAG) 
				{
					tmp_init_stat  =SY_STATE_S10;		
				}	
				else
				{
					tmp_init_stat  =SY_STATE_S0;	
				}				
			}
			break;
		}
		//第一次上电流程
        case SY_STATE_S10:
        {
            if (TASK_IDLE == EepIfIdle())
            {
				for(int i=0;i<SIZE_SY_STORE_INDEX;i++)
				{
					uStoreIndex_SY.data_buf[i] = 0;
				}
				uStoreIndex_SY.Data.firstPowerOn = SY_FIRST_POWERON_FLAG;
                EepWrite(&uStoreIndex_SY.data_buf[0], EEP_ADDR_SY_STORE_INDEX_CIRCLE, SIZE_SY_STORE_INDEX, &uTempMsg_EE);
                tmp_init_stat = SY_STATE_S11;
            }
            break;
        }
		
		case SY_STATE_S11:  //第一次上电赋值参数
		{
			if(TASK_IDLE == EepIfIdle())
			{
				int i;
				int len=0;
				UNION_4U8TOU32 tmp_datas_buf;
				TlvValue.s.ValueWarningId[0]=0;//即时报警ID--------------------------2
				TlvValue.s.ValueWarningId[1]=0;
				tmp_datas_buf.datas = (MAX_FLOW_1_02/65536.00)*10;//单位0.1L/h
				TlvValue.s.ValueOverFlowCurrent[0]=tmp_datas_buf.data_buf[0];//过流报警的阈值，单位0.1L/H----4
				TlvValue.s.ValueOverFlowCurrent[1]=tmp_datas_buf.data_buf[1];
				TlvValue.s.ValueOverFlowCurrent[2]=tmp_datas_buf.data_buf[2];
				TlvValue.s.ValueOverFlowCurrent[3]=tmp_datas_buf.data_buf[3];
				//
				TlvValue.s.ValueOverFlowMinute=10;//持续过流时间----------------------1				
				TlvValue.s.ValueReverseFlowCurrent[0]=0;//反流报警的阈值，单位0.1L/H-4
				TlvValue.s.ValueReverseFlowCurrent[1]=0;
				TlvValue.s.ValueReverseFlowCurrent[2]=0;
				TlvValue.s.ValueReverseFlowCurrent[3]=0;			
				TlvValue.s.ValueReverseFlowMinute=10;//持续反流报警时间---------------1				
				TlvValue.s.ValueLeakFlowMinute=10;//漏水报警时间----------------------1				
				TlvValue.s.ValuePowerLow001V[0]=0x36;//电压报警阈值---------------------2
				TlvValue.s.ValuePowerLow001V[1]=0x01;				
				TlvValue.s.ValueNoWaterMinute=10;//空管报警时间-----------------------1				
				TlvValue.s.ValueValveState[0]=0;//阀门状态---------------------------7预留
				TlvValue.s.ValueValveState[1]=0;
				TlvValue.s.ValueValveState[2]=0;
				TlvValue.s.ValueValveState[3]=0;
				TlvValue.s.ValueValveState[4]=0;
				TlvValue.s.ValueValveState[5]=0;
				TlvValue.s.ValueValveState[6]=0;		
				tmp_datas_buf.datas = FrameCheckParaApp(FramePara_ReportFreq);				
				TlvValue.s.ValueUploadStartTime[0]=0;//数据上报起始时间a-------------4
				TlvValue.s.ValueUploadStartTime[1]=BCD2HEX(tmp_datas_buf.data_buf[2]);//小时
				TlvValue.s.ValueUploadStartTime[2]=BCD2HEX(tmp_datas_buf.data_buf[3]);//分钟
				TlvValue.s.ValueUploadStartTime[3]=0;				
				TlvValue.s.ValueCircleData5Minutes=6;//周期数据时间间隔a-------------1				
				TlvValue.s.ValueDataFrozenTimeDailay=0;//日冻结时间------------------1				
				TlvValue.s.ValueDataFrozenTimeMonth[0]=1;//月冻结时间----------------2
				TlvValue.s.ValueDataFrozenTimeMonth[1]=0;				
				TlvValue.s.ValueUploadRetryTimes=Sys_data_object.P1.report_retry_times;//上报重发次数--------------------1				
				TlvValue.s.ValueUploadRetryDelayMinite=20;//重发延时时间--------------1			
				TlvValue.s.ValueUploadIntervalHour=12;//数据上报时间间隔a-------------1		
				//IP  223.100.022.125      3031
				Sys_data_object.P2.server_ip[0] = 223;
				Sys_data_object.P2.server_ip[1] = 100;
				Sys_data_object.P2.server_ip[2] = 22;
				Sys_data_object.P2.server_ip[3] = 125;
				Sys_data_object.P2.server_port.datas =3031;

				for (i = 0; i < 4; i++)
				{
					if (Sys_data_object.P2.server_ip[i] < 10)
					{
						TlvValue.s.ValueServerIpPort[len++] = Sys_data_object.P2.server_ip[i] + 0x30;
					}
					else if (Sys_data_object.P2.server_ip[i] < 100)
					{
						TlvValue.s.ValueServerIpPort[len++] = (Sys_data_object.P2.server_ip[i] / 10) + 0x30;
						TlvValue.s.ValueServerIpPort[len++] = (Sys_data_object.P2.server_ip[i] % 10) + 0x30;
					}
					else
					{
						TlvValue.s.ValueServerIpPort[len++] = (Sys_data_object.P2.server_ip[i] / 100) + 0x30;
						TlvValue.s.ValueServerIpPort[len++] = (Sys_data_object.P2.server_ip[i] / 10) % 10 + 0x30;
						TlvValue.s.ValueServerIpPort[len++] = (Sys_data_object.P2.server_ip[i] % 10) + 0x30;
					}
					TlvValue.s.ValueServerIpPort[len++] = '.';
				}
				len--; //消除多余的点号
				TlvValue.s.ValueServerIpPort[len++] = ':';
				TlvValue.s.ValueServerIpPort[len++] = ((Sys_data_object.P2.server_port.datas) / 10000) + 0x30;
				TlvValue.s.ValueServerIpPort[len++] = (((Sys_data_object.P2.server_port.datas) % 10000) / 1000) + 0x30;
				TlvValue.s.ValueServerIpPort[len++] = (((Sys_data_object.P2.server_port.datas) % 1000) / 100) + 0x30;
				TlvValue.s.ValueServerIpPort[len++] = (((Sys_data_object.P2.server_port.datas) % 100) / 10) + 0x30;
				TlvValue.s.ValueServerIpPort[len++] = ((Sys_data_object.P2.server_port.datas) % 10) + 0x30;
				TlvValue.s.ValueServerIpPort[len++] = 0;
				//APN
				for(int i = 0; i < Sys_data_object.P1.apn[1]; i++)
				{
					TlvValue.s.ValueAPN[i] = Sys_data_object.P1.apn[2 + i];
				}		
				TlvValue.s.ValueWorkTime[0]=0;//水表运行时间-------------------------6
				
				EepWrite(&TlvValue.data_buf[0], EEP_ADDR_SY_TLVDATA, TLV_VALUE_LEN, &s_sy_eep_complete);
				tmp_init_stat  = SY_STATE_S12;
			}
			break;
		}
		case SY_STATE_S12:
        {
            if (TASK_IDLE == EepIfIdle())
            {
                EepWrite(&Sys_data_object.P2.server_ip[0], EEP_UPDATE_UDP_IP, 6, &s_sy_eep_complete);//保存到UDP IP
                tmp_init_stat = SY_STATE_S13;				
            }
            break;			
        }
		case SY_STATE_S13:
        {
            if (TASK_IDLE == EepIfIdle())
            {
                tmp_init_stat = SY_STATE_S0;				
            }
            break;			
        }
		default:
		{
			tmp_init_stat  =SY_STATE_S0;
		    break;
		}
      }
      EepMachineDriver();
    }
	//存储时间合法性判断
	for(int i=0;i<6;i++)
	{
		if(Rtcc_Check_DateTime(&uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime) == ERROR)
		{
			uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.year  = 0x19;
			uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.month = 0x01;
			uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.day   = 0x01;
			uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.weekday=0x01;
			uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.hour  = 0x01;
			uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.minute= 0x01;
			uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.second= 0x01;
			uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleCntStart = 0;
			uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleCntEnd =0;
			uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.ReadStart =0;
			uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.ReadEnd =0;
		}
	}
	step_ShenYangSOEMachine=SY_STATE_S0;
	uSOE_Data_New=0;
	uSOE_Data_Old=0;
	uSOE_Valve_New=0;
	uSOE_Valve_Old=0;
	uSOE_Power_New=0;
	uSOE_Power_Old=0;
	uSOE_Temprature_New=0;
	uSOE_Temprature_ShortNew=0;
	uSOE_Temprature_ShortOld=0;
	uSOE_Temprature_OpenNew=0;
	uSOE_Temprature_OpenOld=0;
	uSOE_Update_Flag=0xFFFFFFFF;
}
/********************************************************************************
函数名：SortMinToMax
说  明：大小排序
********************************************************************************/
void SortMinToMax(u32 *data, u8 data_len)
{
    u8 i, j, len;
    u32 *p;
    u32 temp = 0;

    p = data;
    len = data_len;

    for (j = 0; j < len - 1; j++)
    {
        for (i = 0; i < len - 1 - j; i++)
        {
            if (p[i] > p[i + 1])
            {
                temp = p[i];
                p[i] = p[i + 1];
                p[i + 1] = temp;
            }
        }
    }
}
/********************************************************************************
函数名：getSY_StoreCircleIndex
说  明：获取周期数据存储位置
********************************************************************************/
u8 getSY_StoreCircleIndex(void)
{
	u8 i=0;
	u8 new_index=0;
	u8 temp_hour=0;
	//获取当前时间
	RtccGetDateTime(&uTempRtccTime_BCD);
	//检测上半天或下半天 
	if(uTempRtccTime_BCD.s.hour>=0x12)
	{
		temp_hour = 0x12;
	}
	else
	{
		temp_hour = 0x00;
	}
	for(i=0;i<6;i++)
	{
		if((uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.year == uTempRtccTime_BCD.s.year)
		&&(uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.month == uTempRtccTime_BCD.s.month)
		&&(uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.day == uTempRtccTime_BCD.s.day)
		&&(uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.hour == temp_hour))
		{
			new_index = (BCD2HEX(uTempRtccTime_BCD.s.hour) % 12)*2+BCD2HEX(uTempRtccTime_BCD.s.minute)/30;//根据时间计算当前应该存储的位置
			//合法性判断,指针错误则初始话start和end
			if((uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleCntEnd > 24)||
			   (uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleCntEnd < uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleCntStart)||
			   (uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleCntEnd != new_index))//存储位置和当前时间不对应,则重新初始该位置
			{
				uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.year  = uTempRtccTime_BCD.s.year;
				uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.month = uTempRtccTime_BCD.s.month;
				uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.day   = uTempRtccTime_BCD.s.day;
				uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.hour  = temp_hour;
				uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.minute= 0x00;
				uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime.s.second= 0x00;
				uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleCntStart = new_index;
				uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleCntEnd = uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleCntStart;
				uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.ReadStart = uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleCntStart;
				uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.ReadEnd = uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleCntStart;
			}
			return i;
		}
	}
	//如果没有查询到当天对应日期，则按照已存储时间顺序递增，新递增到的位置存储数量清0
	//在重新上电的情况下，表计时间小于存储的时间,不会覆盖已存储时间
	//出现这种情况的原因可能有:断过电RTC时间重置,校时往前或往后超过半天
	for(new_index=0; new_index<6; new_index++)//使用无效的指针
	{
		if((uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleCntEnd > 24)||
			(uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleCntStart > uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleCntEnd)||
			(Rtcc_Check_DateTime(&uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleDataTime) == ERROR)||
			(RtccCalculateDiffTime(&uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleDataTime, &uTempRtccTime_BCD)>0))
		{
			uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleDataTime.s.year  = uTempRtccTime_BCD.s.year;
			uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleDataTime.s.month = uTempRtccTime_BCD.s.month;
			uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleDataTime.s.day   = uTempRtccTime_BCD.s.day;
			uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleDataTime.s.hour  = temp_hour;
			uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleDataTime.s.minute= 0x00;
			uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleDataTime.s.second= 0x00;
			uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleCntStart = (BCD2HEX(uTempRtccTime_BCD.s.hour) % 12)*2+BCD2HEX(uTempRtccTime_BCD.s.minute)/30;
			uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleCntEnd = uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleCntStart;
			uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.ReadStart = uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleCntStart;
			uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.ReadEnd = uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleCntStart;
			return new_index;
		}
		else
		{
			uUploadIndexTimeBuf[new_index] = get_mktime(&uStoreIndex_SY.Data.Circle.CircleIndex[new_index].Data.CircleDataTime);
		}
	}
	SortMinToMax(uUploadIndexTimeBuf, new_index);//时间按大小排序
	for(int index=0;index<6;index++)
	{
		if(uUploadIndexTimeBuf[0] == get_mktime(&uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime))
		{
			uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.year  = uTempRtccTime_BCD.s.year;
			uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.month = uTempRtccTime_BCD.s.month;
			uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.day   = uTempRtccTime_BCD.s.day;
			uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.hour  = temp_hour;
			uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.minute= 0x00;
			uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.second= 0x00;
			uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntStart = (BCD2HEX(uTempRtccTime_BCD.s.hour) % 12)*2+BCD2HEX(uTempRtccTime_BCD.s.minute)/30;
			uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntEnd = uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntStart;
			uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.ReadStart = uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntStart;
			uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.ReadEnd = uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntStart;
			return index;
		}
	}
	return 0;
}
/********************************************************************************
函数名：ShenYang_CircleDataStore_proc
说  明：周期数据存储
累计流量单位为L/0.001M3，4字节无符号整数。
瞬时流量单位为0.01L/H，4字节无符号整数。
电压单位为0.01V，2字节无符号整数。
压力单位为10mbar/0.01kg/1KPa，2字节无符号整数。
温度单位为0.01℃，2字节有符号整数。

正向累计流量	4
负向累计流量	4
瞬时流量	4
压力	2
温度	2
********************************************************************************/
static int ShenYang_CircleDataStore_proc(void)
{
	switch(step_CircleDataStore)
	{
		case SY_STATE_S0:
		{
            SY_U32_To_Float  temp_float_data;
			//获取当前流量
            temp_float_data.gp22_u32 =  Gp30CheckDataApp(2);
            temp_Circle_data.Data.CurrentFlow  = (u32)(temp_float_data.gp22_float*10);//(注意测试最大最小值)
			//负向累计
			temp_float_data.gp22_u32 =  Gp30CheckDataApp(3);
            temp_Circle_data.Data.CurrentFlowReverse = (u32)(temp_float_data.gp22_float*10);
			//瞬时流量
			temp_float_data.gp22_float = Gp30CheckDataApp(1); //瞬时流量 瞬时流量单位为0.01L/H，4字节无符号整数。
			temp_Circle_data.Data.InstantaneousFlow = (u32)(temp_float_data.gp22_float / (float)65536.00)*100; 		
			//压力
			temp_Circle_data.Data.Presure = 0;
			//温度
			temp_float_data.gp22_float = TempCheckValueApp(); 
			if(temp_float_data.gp22_float<0)
			{
				temp_Circle_data.Data.Temprature = (~(u16)(-temp_float_data.gp22_float*100))|0x8000;
			}
			else
			{
				temp_Circle_data.Data.Temprature = ((u16)(temp_float_data.gp22_float*100));
			}
			step_CircleDataStore = SY_STATE_S1;
			break;
		}
		case SY_STATE_S1://存储数据
		{
			if(TASK_IDLE == EepIfIdle())
			{
				uStoreCircleIndex_SY = getSY_StoreCircleIndex();
				uTemp_ee_addr = EEP_ADDR_SY_STORE_CIRCLE + //周期数据基地址
				                (uStoreCircleIndex_SY*STORE_SIZE_DATA_CIRCLE*24)+  //每半天偏移地址长度
				                 uStoreIndex_SY.Data.Circle.CircleIndex[uStoreCircleIndex_SY].Data.CircleCntEnd*STORE_SIZE_DATA_CIRCLE; //当前半天偏移位置
				EepWrite(temp_Circle_data.data_buf, uTemp_ee_addr, STORE_SIZE_DATA_CIRCLE, &uTempMsg_EE);
				step_CircleDataStore = SY_STATE_S2;
			}  
			break;
		}
		case SY_STATE_S2: //存储指针
		{
			if(TASK_IDLE == EepIfIdle())
			{
				uStoreIndex_SY.Data.Circle.CircleIndex[uStoreCircleIndex_SY].Data.CircleCntEnd++;
				if(uStoreIndex_SY.Data.Circle.CircleIndex[uStoreCircleIndex_SY].Data.CircleCntEnd>=24)
				{
					uStoreIndex_SY.Data.Circle.CircleIndex[uStoreCircleIndex_SY].Data.CircleCntEnd = 24;
				}
				uStoreIndex_SY.Data.Circle.CircleIndex[uStoreCircleIndex_SY].Data.ReadEnd = uStoreIndex_SY.Data.Circle.CircleIndex[uStoreCircleIndex_SY].Data.CircleCntEnd;
				/*当CircleCntEnd++到24时，禁止归0，而是在下次存储调用getSY_StoreCircleIndex的时候重新切换存储块*/
				EepWrite(&uStoreIndex_SY.Data.Circle.CircleIndex[uStoreCircleIndex_SY].data_buf[0], EEP_ADDR_SY_STORE_INDEX_CIRCLE+uStoreCircleIndex_SY*11, 11, &uTempMsg_EE);
				step_CircleDataStore = SY_STATE_S3;
			}  
			break;
		}
		case SY_STATE_S3:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				step_CircleDataStore = SY_STATE_S0;
				return 1;
			}  
			break;
		}
		default:
		{
			step_CircleDataStore = SY_STATE_S0;
			break;
		}
	}
	return 0;
}

//清除已经上报成功的索引;保留时间,ReadStart和ReadEnd以备读取周期数据使用
int ShenYang_CircleDataStore_ClearUploadIndex(u8 index)
{
	if(TASK_IDLE == EepIfIdle())
	{
		uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntStart=uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntEnd;
		EepWrite(&uStoreIndex_SY.Data.Circle.CircleIndex[index].data_buf[7], EEP_ADDR_SY_STORE_INDEX_CIRCLE+index*11+7, 2, &uTempMsg_EE);
		return 1;
	}  
	return 0;
}

/********************************************************************************
函数名：ShenYang_FrozenDataStore_proc
说  明：冻结数据存储,
        0点存储日冻结数据\
		半小时的间隔流量\
		日最高流速\
		日最高水温\
		日最低水温\
        报警状态字\
		时间年月日

日期	3
时间	3
正向累计流量	4
负向累计流量	4
峰值流量	4
峰值流量时间	3
电池电压	6
水表状态	1
水表故障	1
阀门状态	1
预留	1
校验和	1
总字节数	32
********************************************************************************/
static int ShenYang_FrozenDataStore_proc(TYP_STATE_SY StoreType)
{
	switch(step_FrozenDataStore)
	{
		case SY_STATE_S0:
		{
			u8 temp_index = 0;
            SY_U32_To_Float  temp_float_data;
			RTCC_HISTORY_DATA  temp_his_data;
			RtccGetDateTime(&uTempRtccTime_BCD);
			//冻结日期时间
			temp_frozen_data.Data.FrozenDataTime.s.year   = uTempRtccTime_BCD.s.year;
			temp_frozen_data.Data.FrozenDataTime.s.month  = uTempRtccTime_BCD.s.month;
			temp_frozen_data.Data.FrozenDataTime.s.day    = uTempRtccTime_BCD.s.day;
			temp_frozen_data.Data.FrozenDataTime.s.hour   = uTempRtccTime_BCD.s.hour;
			temp_frozen_data.Data.FrozenDataTime.s.minute = uTempRtccTime_BCD.s.minute;
			temp_frozen_data.Data.FrozenDataTime.s.second = uTempRtccTime_BCD.s.second;
			//获取当前流量
            temp_float_data.gp22_u32 =  Gp30CheckDataApp(2);
            temp_frozen_data.Data.CurrentFlow = (u32)(temp_float_data.gp22_float*10);//(注意测试最大最小值)
            //负向累计
			temp_float_data.gp22_u32 =  Gp30CheckDataApp(3);
            temp_frozen_data.Data.CurrentFlowReverse = (u32)(temp_float_data.gp22_float*10);

			//获取日最高流速
			RtccCheck_Hight_Flow(&pTempBuf_DailyHighestFlow);			
			for(temp_index = 0; temp_index <14 ;temp_index++)
			{
				temp_his_data.byte[temp_index] = *(pTempBuf_DailyHighestFlow+temp_index);
			}		
			temp_frozen_data.Data.PeakCurrentFlow = temp_his_data.s.most_high_flow;//(temp_his_data.s.most_high_flow/10)*60;//原存储格式386~38.6L/min
			temp_frozen_data.Data.PeakCurrentFlowTime[0] = temp_his_data.byte[8];//最高流速起始时间:hour
			temp_frozen_data.Data.PeakCurrentFlowTime[1] = temp_his_data.byte[7];//最高流速起始时间:minute
			temp_frozen_data.Data.PeakCurrentFlowTime[2] = temp_his_data.byte[6];//最高流速起始时间:second
			//电池电压
			for(temp_index = 0; temp_index <6 ;temp_index++)
			{
				temp_frozen_data.Data.BatPower[temp_index] = 0;
			}//选一组电池电压-----------------			
			temp_frozen_data.Data.BatPower[0] = PowerCheckValueSys()&0xFF;
			temp_frozen_data.Data.BatPower[1] = (PowerCheckValueSys()>>8)&0xFF;
			
			//水表状态
			temp_frozen_data.Data.MeterState = 0;//0为正常，按实计自己定义有用状态
			//水表故障
			temp_frozen_data.Data.MeterFault = 0;//0为正常，按实计自己定义有用故障信息
			//阀门状态
			temp_frozen_data.Data.ValveState = 0;
			if(Sys_data_object.P1.ies_valve_paras.valve_err)
			{
				temp_frozen_data.Data.ValveState = 0xFF;
			}	
			else
			{
			#if defined(VALVE)
				if(VALVE_STATE_CLOSE == ValveGetStateApp()) 
				{
					temp_frozen_data.Data.ValveState = 0x99;
				}
				else if(VALVE_STATE_OPEN == ValveGetStateApp())
				{
					temp_frozen_data.Data.ValveState = 0x55;
				}
			#endif
			}
			//预留字节
			temp_frozen_data.Data.Reverse =0x99;
			//校验和
			temp_frozen_data.Data.CheckSum = 0;
			for(temp_index = 0; temp_index<(STORE_SIZE_DATA_FROZEN-1); temp_index++)
			{
				temp_frozen_data.Data.CheckSum += temp_frozen_data.data_buf[temp_index];
			}
			
			step_FrozenDataStore = StoreType;
			break;
		}
		case SY_STATE_S10://存储数据:日冻结
		{
			if(TASK_IDLE == EepIfIdle())
			{
				if(uStoreIndex_SY.Data.Daily>=7)
				{
					uStoreIndex_SY.Data.Daily = 0;
				}
				EepWrite(temp_frozen_data.data_buf, EEP_ADDR_SY_STORE_FROZEN_DAILY + uStoreIndex_SY.Data.Daily*32, 32, &uTempMsg_EE);
				step_FrozenDataStore = SY_STATE_S11;
			}  
			break;
		}
		case SY_STATE_S11: //存储指针:日冻结指针
		{
			if(TASK_IDLE == EepIfIdle())
			{
				uStoreIndex_SY.Data.Daily++;//下次要存的位置
				EepWrite(&uStoreIndex_SY.Data.Daily, EEP_ADDR_SY_STORE_INDEX_DAILY, 1, &uTempMsg_EE);
				step_FrozenDataStore = SY_STATE_S1;
			}  
			break;
		}
		case SY_STATE_S20://存储数据:月冻结
		{
			if(TASK_IDLE == EepIfIdle())
			{
				if(uStoreIndex_SY.Data.Month >=18)
				{
					uStoreIndex_SY.Data.Month = 0;
				}
				EepWrite(temp_frozen_data.data_buf, EEP_ADDR_SY_STORE_FROZEN_MONTH + uStoreIndex_SY.Data.Month*32, 32, &uTempMsg_EE);
				step_FrozenDataStore = SY_STATE_S21;
			}  
			break;
		}
		case SY_STATE_S21: //存储指针:月冻结指针
		{
			if(TASK_IDLE == EepIfIdle())
			{
				uStoreIndex_SY.Data.Month++;//下次要存的位置
				EepWrite(&uStoreIndex_SY.Data.Month, EEP_ADDR_SY_STORE_INDEX_MONTH, 1, &uTempMsg_EE);
				step_FrozenDataStore = SY_STATE_S1;
			}  
			break;
		}
		case SY_STATE_S30://存储数据:年冻结
		{
			if(TASK_IDLE == EepIfIdle())
			{
				if(uStoreIndex_SY.Data.Year>=6)
				{
					uStoreIndex_SY.Data.Year = 0;
				}
				EepWrite(temp_frozen_data.data_buf, EEP_ADDR_SY_STORE_FROZEN_YEAR + uStoreIndex_SY.Data.Year*32, 32, &uTempMsg_EE);
				step_FrozenDataStore = SY_STATE_S31;
			}  
			break;
		}
		case SY_STATE_S31: //存储指针:年冻结指针
		{
			if(TASK_IDLE == EepIfIdle())
			{
				uStoreIndex_SY.Data.Year++;//下次要存的位置
				EepWrite(&uStoreIndex_SY.Data.Year, EEP_ADDR_SY_STORE_INDEX_YEAR, 1, &uTempMsg_EE);
				step_FrozenDataStore = SY_STATE_S1;
			}  
			break;
		}
		case SY_STATE_S1:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				step_FrozenDataStore = SY_STATE_S0;
				return SY_STATE_S1;
			}  
			break;
		}
		default:
		{
			step_FrozenDataStore = SY_STATE_S0;
			break;
		}
	}
	return 0;
}
//冻结数据和周期数据的清除
static int ShenYang_StoreDataClear_proc(TYP_STATE_SY StoreType)
{
	switch(step_FrozenDataClear)
	{
		case SY_STATE_S0:
		{
			uClearDataLen =0;
			step_FrozenDataClear = StoreType;
			break;
		}
		case SY_STATE_S10://清除冻结数据
		{
			if(TASK_IDLE == EepIfIdle())
			{
				memset(s_sy_clear_history_buf,0,sizeof(s_sy_clear_history_buf));
				EepWrite(s_sy_clear_history_buf, EEP_ADDR_SY_STORE_FROZEN_DAILY + uClearDataLen*CLEAR_HIS_BUF_LEN, CLEAR_HIS_BUF_LEN, &uTempMsg_EE);
				step_FrozenDataClear = SY_STATE_S11;
			}
			break;
		}
		case SY_STATE_S11:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				uClearDataLen++;
				if((uClearDataLen+1)*CLEAR_HIS_BUF_LEN < EEP_DATA_LEN_FROZEN)
				{
					step_FrozenDataClear = SY_STATE_S10;
				}
				else
				{
					memset(s_sy_clear_history_buf,0,sizeof(s_sy_clear_history_buf));
					EepWrite(s_sy_clear_history_buf, EEP_ADDR_SY_STORE_FROZEN_DAILY + uClearDataLen*CLEAR_HIS_BUF_LEN, (EEP_DATA_LEN_FROZEN%CLEAR_HIS_BUF_LEN), &uTempMsg_EE);
					uStoreIndex_SY.Data.Daily = 0;
					uStoreIndex_SY.Data.Month = 0;
					uStoreIndex_SY.Data.Year = 0;
					step_FrozenDataClear = SY_STATE_S12;
				}
			}
			break;
		}
		case SY_STATE_S12://清除冻结数据存储指针
		{
			if(TASK_IDLE == EepIfIdle())
			{
				memset(s_sy_clear_history_buf,0,sizeof(s_sy_clear_history_buf));
				EepWrite(s_sy_clear_history_buf, EEP_ADDR_SY_STORE_INDEX_DAILY, 3, &uTempMsg_EE);
				step_FrozenDataClear = SY_STATE_S0;
				return 1;
			}
			break;
		}
		
		case SY_STATE_S20://清除周期数据
		{
			if(TASK_IDLE == EepIfIdle())
			{
				memset(s_sy_clear_history_buf,0,sizeof(s_sy_clear_history_buf));
				EepWrite(s_sy_clear_history_buf, EEP_ADDR_SY_STORE_CIRCLE + uClearDataLen*CLEAR_HIS_BUF_LEN, CLEAR_HIS_BUF_LEN, &uTempMsg_EE);
				step_FrozenDataClear = SY_STATE_S21;
			}
			break;
		}
		case SY_STATE_S21:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				uClearDataLen++;
				if((uClearDataLen+1)*CLEAR_HIS_BUF_LEN < EEP_DATA_LEN_CIRCLE)
				{
					step_FrozenDataClear = SY_STATE_S20;
				}
				else
				{
					memset(s_sy_clear_history_buf,0,sizeof(s_sy_clear_history_buf));
					EepWrite(s_sy_clear_history_buf, EEP_ADDR_SY_STORE_CIRCLE + uClearDataLen*CLEAR_HIS_BUF_LEN, (EEP_DATA_LEN_CIRCLE%CLEAR_HIS_BUF_LEN), &uTempMsg_EE);
					for(int index=0; index<6; index++)
					{
						uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.year    = 0x00;
						uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.month   = 0x00;
						uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.day     = 0x00;
						uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.hour    = 0x00;
						uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.minute  = 0x00;
						uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.second  = 0x00;
						uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.weekday = 0x00;
						uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntStart = 0;
						uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntEnd = 0;
						//清除读取索引
						uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.ReadStart = 0;
						uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.ReadEnd = 0;
					}
					step_FrozenDataClear = SY_STATE_S22;
				}
			}  
			break;
		}
		case SY_STATE_S22://清除周期数据存储指针
		{
			if(TASK_IDLE == EepIfIdle())
			{
				memset(s_sy_clear_history_buf,0,sizeof(s_sy_clear_history_buf));
				EepWrite(s_sy_clear_history_buf, EEP_ADDR_SY_STORE_INDEX_CIRCLE, 66, &uTempMsg_EE);
				step_FrozenDataClear = SY_STATE_S0;
				return 1;
			}
			break;
		}
		
		case SY_STATE_S30://清除报警事件
		{
			if(TASK_IDLE == EepIfIdle())
			{
				memset(s_sy_clear_history_buf,0,sizeof(s_sy_clear_history_buf));
				EepWrite(s_sy_clear_history_buf, EEP_ADDR_SOE_BASE + uClearDataLen*CLEAR_HIS_BUF_LEN, CLEAR_HIS_BUF_LEN, &uTempMsg_EE);
				step_FrozenDataClear = SY_STATE_S31;
			}  
			break;
		}
		case SY_STATE_S31:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				uClearDataLen++;
				if((uClearDataLen+1)*CLEAR_HIS_BUF_LEN < EEP_DATA_LEN_SOE)
				{
					step_FrozenDataClear = SY_STATE_S30;
				}
				else
				{
					memset(s_sy_clear_history_buf,0,sizeof(s_sy_clear_history_buf));
					EepWrite(s_sy_clear_history_buf, EEP_ADDR_SOE_BASE + uClearDataLen*CLEAR_HIS_BUF_LEN, (EEP_DATA_LEN_SOE%CLEAR_HIS_BUF_LEN), &uTempMsg_EE);
					step_FrozenDataClear = SY_STATE_S0;
					return 1;
				}
			}  
			break;
		}
		default:
		{
			step_FrozenDataClear = SY_STATE_S0;
			break;
		}
	}
	return 0;
}

/********************************************************************************
ShenYang_Store_Machine
说  明：沈阳数据存储状态机
********************************************************************************/
void ShenYang_Store_Machine(void)
{
	switch(step_ShenYangStoreMachine)
	{
		case SY_STATE_S0://数据存储消息判断
		{
			if (RtccCheckMsgApp(RtccMsg_OtherProtl))
			{
				RtccClearMsgApp(RtccMsg_OtherProtl);
				FrameSetMsgApp(MsgShenYang_WarningCheck);
				RtccGetDateTime(&sy_protl_rtcc_time);
				if((sy_protl_rtcc_time.s.hour==0)&&(sy_protl_rtcc_time.s.minute==1))//每月1号0点1分存储月冻结数据
                {
                    FrameSetMsgApp(MsgShenYang_DailySave);//沈阳日冻结数据存储
					if(sy_protl_rtcc_time.s.day == 1)
					{
						FrameSetMsgApp(MsgShenYang_MonthSave);//沈阳月冻结数据存储
						if(sy_protl_rtcc_time.s.month == 1)
						{
							FrameSetMsgApp(MsgShenYang_YearSave);//沈阳年冻结数据存储
						}
					}
                }
				if((sy_protl_rtcc_time.s.minute==0x00)||(sy_protl_rtcc_time.s.minute==0x30))
				{
					FrameSetMsgApp(MsgShenYang_CircleSave);//周期数据存储
				}
				if (sy_protl_rtcc_time.s.hour == 0)//零点
				{
					FrameSetMsgApp(MsgShenYang_WarningStoreReset);//重置报警存储标志
				}
			}
			
			if(FrameCheckMsgApp(MsgShenYang_DailySave))//日冻结存储消息
			{
				FrameClearMsgApp(MsgShenYang_DailySave);	//清消息
				step_ShenYangStoreMachine = SY_STATE_S1;
			}
			else if(FrameCheckMsgApp(MsgShenYang_MonthSave))//月冻结存储消息
			{
				FrameClearMsgApp(MsgShenYang_MonthSave);	
				step_ShenYangStoreMachine = SY_STATE_S2;
			}
			else if(FrameCheckMsgApp(MsgShenYang_YearSave))//年冻结存储消息
			{
				FrameClearMsgApp(MsgShenYang_YearSave);	
				step_ShenYangStoreMachine = SY_STATE_S3;
			}
			else if(FrameCheckMsgApp(MsgShenYang_CircleSave))//周期数据存储消息
			{
				FrameClearMsgApp(MsgShenYang_CircleSave);	
				step_ShenYangStoreMachine = SY_STATE_S4;
			}			
			else if(FrameCheckMsgApp(MsgShenYang_CircleClear))//周期数据清除
			{
				FrameClearMsgApp(MsgShenYang_CircleClear);	
				step_ShenYangStoreMachine = SY_STATE_S6;
			}
			else if(FrameCheckMsgApp(MsgShenYang_WarningClear))//报警数据清除
			{
				uSOE_Update_Flag=0xFFFFFFFF;  //清除标志，当天重复的报警可以再次记录
				FrameClearMsgApp(MsgShenYang_WarningClear);	
				step_ShenYangStoreMachine = SY_STATE_S7;
			}
			else if(FrameCheckMsgApp(MsgShenYang_FrozenClear))//冻结数据清除
			{
				FrameClearMsgApp(MsgShenYang_FrozenClear);
				SaveNewSoe(SOE_SY_FROZEN_CLEAR,SOE_REMARK_NOR);//冻结数据清空
				step_ShenYangStoreMachine = SY_STATE_S5;
			}
			else if(FrameCheckMsgApp(MsgShenYang_FactorySettingAndReset))//恢复出厂设置
			{
				FrameClearMsgApp(MsgShenYang_FactorySettingAndReset);	
				step_ShenYangStoreMachine = SY_STATE_S8;
			}
			else if(FrameCheckMsgApp(MsgShenYang_SysReset))//系统重启
			{
				FrameClearMsgApp(MsgShenYang_SysReset);	
				step_ShenYangStoreMachine = SY_STATE_S8;
			}
			break;
		}
		case SY_STATE_S1://日冻结数据存储
		{
			if(ShenYang_FrozenDataStore_proc(SY_STATE_S10) == 1)
			{
				step_ShenYangStoreMachine = SY_STATE_S0;
			}
			break;
		}
		case SY_STATE_S2://月冻结数据存储
		{
			if(ShenYang_FrozenDataStore_proc(SY_STATE_S20) == 1)
			{
				step_ShenYangStoreMachine = SY_STATE_S0;
			}
			break;
		}
		case SY_STATE_S3://年冻结数据存储
		{
			if(ShenYang_FrozenDataStore_proc(SY_STATE_S30) == 1)
			{
				step_ShenYangStoreMachine = SY_STATE_S0;
			}
			break;
		}
		case SY_STATE_S4://周期数据存储
		{
			if(ShenYang_CircleDataStore_proc() == 1)
			{
				//RtccAdjustDateTime(1,1800-5);//往后校时半小时,测试使用
				step_ShenYangStoreMachine = SY_STATE_S0;
			}
			break;
		}
		case SY_STATE_S5:
		{
			if(ShenYang_StoreDataClear_proc(SY_STATE_S10) == 1)//清冻结数据
			{
				step_ShenYangStoreMachine = SY_STATE_S0;
			}
			break;
		}
		case SY_STATE_S6: 
		{
			if(ShenYang_StoreDataClear_proc(SY_STATE_S20) == 1)//清周期数据
			{
				step_ShenYangStoreMachine = SY_STATE_S0;
			}
			break;
		}
		case SY_STATE_S7:
		{
			if(ShenYang_StoreDataClear_proc(SY_STATE_S30) == 1)//清报警数据
			{
				step_ShenYangStoreMachine = SY_STATE_S0;
			}
			break;
		}
		case SY_STATE_S8://暂不使用恢复出厂设置
		{
			if (TASK_IDLE == EepIfIdle())
            {
				NVIC_SystemReset(); 
				step_ShenYangStoreMachine = SY_STATE_S0;
            }
			break;
		}
		default:
		{
			step_ShenYangStoreMachine = SY_STATE_S0;
			break;
		}
	}
}

//计算上报总包数
u8 getSY_CircleUploadPackets(void)
{
	u8 temp_count=0;
	u32 temp_time=0;
	
	for(int i=0;i<6;i++)
	{
		uUploadIndexTimeBuf[i] = 0;
	}
	for(int index=0; index<6; index++)
	{
		if(uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntEnd <= 24) //合法性判断
		{
			if(uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntStart < uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntEnd)
			{
				uUploadIndexTimeBuf[temp_count] = get_mktime(&uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime);
				temp_count++;
			}
		}
	}
	SortMinToMax(uUploadIndexTimeBuf, temp_count);//时间按大小排序
	for(int j=0; j<temp_count; j++)//根据时间大小排序索引
	{
		for(int index=0;index<6;index++)
		{
			temp_time = get_mktime(&uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime);
			if(temp_time == uUploadIndexTimeBuf[j])
			{
				uUploadIndexBuf[j] = index;
			}
		}
	}
	return temp_count;
}
//获取当前要上报数据包索引
u8 getSY_ThisCirclePacket_Index(u8 current_packet)
{
	if(current_packet < 6)
	{
		return uUploadIndexBuf[current_packet];
	}
	return 0;
}
//获取当前要上报数据包数据
int NB_SY_Frame_Upload_Data(u8 index, u8 *pBuf)  
{
	switch(SY_CircleUploadStep)
	{
		case SY_STATE_S0://计算当前包位置,获取当前要上报数据
		{
			if(TASK_IDLE == EepIfIdle())
			{	
				SY_ThisPack_Count = uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntEnd-
									uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntStart;
				EepRead(EEP_ADDR_SY_STORE_CIRCLE + index*16*24 + uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntStart*16, pBuf, 16*SY_ThisPack_Count, &uTempMsg_EE);
				SY_CircleUploadStep = SY_STATE_S1;
			}
		}
		break;
		case SY_STATE_S1:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				SY_CircleUploadStep =SY_STATE_S0;
				return SY_ThisPack_Count;
			}
		}
		break;
		default:
		{
			SY_CircleUploadStep = SY_STATE_S0;
		}
		break;
	}
	return 0;
}

//获取最近几天的存储总包数和包号
u8 getSY_CircleDataPackets(u8 readDays, u8 *pIndexBuf)
{
	u8 temp_packet_cnt=0;//总包数,局部变量
	DateTime start_time;
    DateTime end_time;
	
	time_t sec_start,sec_end,sec_circle;//临时变量,时间转成秒数
	
	start_time = getBackDateTime(readDays);//获取起始时间
	RtccGetDateTime(&end_time);//获取当前时间,结束时间
	sec_start = get_mktime(&start_time);
	sec_end   = get_mktime(&end_time);

	for(int i=0;i<6;i++)
	{
		sec_circle = get_mktime(&uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.CircleDataTime);
		if((uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.ReadEnd<=24)&&
		(uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.ReadEnd>uStoreIndex_SY.Data.Circle.CircleIndex[i].Data.ReadStart))//判断存储指针是否有效
		{
			if((sec_circle >= sec_start) && (sec_circle <= sec_end)) //判断时间是否有效
			{
				*pIndexBuf++ = i;
				temp_packet_cnt++;
			}
		}
	}
	return temp_packet_cnt;
}
/*******************************************************************************
 * @fun_name:	NB_SY_Frame_Upload_CurrentData
 * @brief   : 上报当前表数据
 * @param[in] :	None
 * @param[out]: None
 * @retval    : None
 * @other     : 联调测试平台接收到的数据需要非加密，平台应答数据时间返回的是HEX格式
 ******************************************************************************/
int NB_SY_Frame_Upload_CurrentData(u8 *pBuf) 
{
	UNION_4U8TOU32 tmp_datas_buf;
	 SY_U32_To_Float  temp_float_data;
	
	*pBuf++ = 1; //总包数 1字节
	*pBuf++ = 1; //当前包序号 1字节
	RtccGetDateTime(&uTempRtccTime_BCD);
	//起始日期 3字节
	*pBuf++ = BCD2HEX(uTempRtccTime_BCD.s.year);
	*pBuf++ = BCD2HEX(uTempRtccTime_BCD.s.month);
	*pBuf++ = BCD2HEX(uTempRtccTime_BCD.s.day);
	//起始时间 3字节
	*pBuf++ = BCD2HEX(uTempRtccTime_BCD.s.hour); 
	*pBuf++ = BCD2HEX(uTempRtccTime_BCD.s.minute);
	*pBuf++ = BCD2HEX(uTempRtccTime_BCD.s.second);
	*pBuf++ = 6;  //6:表示30分钟（5x6）时间间隔1字节
	*pBuf++ = 1;  //数据条数1字节

	temp_float_data.gp22_u32  = Gp30CheckDataApp(2);  //正向累计流量  累计流量单位为L/0.001M3，4字节无符号整数。
	tmp_datas_buf.datas = (u32)(temp_float_data.gp22_float*10); 
	*pBuf++ = tmp_datas_buf.data_buf[0];//小数位  
	*pBuf++ = tmp_datas_buf.data_buf[1];
	*pBuf++ = tmp_datas_buf.data_buf[2];
	*pBuf++ = tmp_datas_buf.data_buf[3];//小数位

	temp_float_data.gp22_u32  = Gp30CheckDataApp(3);  //反向累计流量
	tmp_datas_buf.datas = (u32)(temp_float_data.gp22_float*10); 
	*pBuf++ = tmp_datas_buf.data_buf[0];//小数位
	*pBuf++ = tmp_datas_buf.data_buf[1];
	*pBuf++ = tmp_datas_buf.data_buf[2];
	*pBuf++ = tmp_datas_buf.data_buf[3];//小数位

	temp_float_data.gp22_float = Gp30CheckDataApp(GP30_APP_INS_FLOW); //瞬时流量 瞬时流量单位为0.01L/H，4字节无符号整数。
	tmp_datas_buf.datas = (u32)(temp_float_data.gp22_float / (float)65536.00)*100; 
	*pBuf++ = tmp_datas_buf.data_buf[0];
	*pBuf++ = tmp_datas_buf.data_buf[1];
	*pBuf++ = tmp_datas_buf.data_buf[2];//小数位
	*pBuf++ = tmp_datas_buf.data_buf[3];

	//tmp_datas_buf_yali.datas = 210;  //测试大表返回压力  压力单位为10mbar/0.01kg/1KPa，2字节无符号整数。
	*pBuf++ = 0; //压力
	*pBuf++ = 0;

	//温度 温度单位为0.01℃，2字节有符号整数
	float tmep = TempCheckValueApp();      //
	//tmep = -12.03;  //测试
	if(tmep <0)
	{				
		tmp_datas_buf.datas = (u16)(tmep*(-100));	
		tmp_datas_buf.datas &= 0x0FFF;
		tmp_datas_buf.datas = ~tmp_datas_buf.datas;
		tmp_datas_buf.datas |= 0x8000;		
	}
	else
	{
		tmp_datas_buf.datas = (u16)(tmep*(100));	
	}
	*pBuf++ = tmp_datas_buf.datas_map.data0; //温度
	*pBuf++ = tmp_datas_buf.datas_map.data1;

	return 26;
}

/********************************************************************************
函数名：SY_GetCircleDateTime
说  明：获取数据包的起始时间
********************************************************************************/
void SY_GetCircleDateTime(DateTime *pDT, u8 index, u8 UploadOrRead)
{
	u8 temp_hour=0;
	u8 temp_minute=0;
	u32 temp_second=0;
	
	pDT->s.year  = uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.year;
	pDT->s.month = uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.month;
	pDT->s.day   = uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.day;

	pDT->s.hour   = uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.hour; 
	pDT->s.minute = uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.minute;
	pDT->s.second = uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleDataTime.s.second;
	
	if(UploadOrRead == 0)//上报周期数据使用
	{
		temp_second = uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.CircleCntStart*30*60;
	}
	else//读取周期数据使用
	{
		temp_second = uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.ReadStart*30*60;
	}
	temp_hour = (temp_second/3600)%12;
	temp_minute = (temp_second/60)%60;
	pDT->s.hour = HEX2BCD(BCD2HEX(pDT->s.hour)+temp_hour);
	pDT->s.minute = HEX2BCD(BCD2HEX(pDT->s.minute)+temp_minute);
}
//根据包号获取存储指针的起始和结束位置
u8 SY_GetCircleReadStartEnd(u8 index, u8 StartOrEnd)
{
	if(StartOrEnd == 0)
	{
		return uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.ReadStart;
	}
	else
	{
		return uStoreIndex_SY.Data.Circle.CircleIndex[index].Data.ReadEnd;
	}
}
/********************************************************************************
函数名：get_SY_StorePara
说  明：获取沈阳数据存储当前指针
********************************************************************************/
u8 get_SY_StorePara(SY_ParaTypeDef paraType)
{
    switch(paraType)
	{
	    case SY_StoreCurIndex_Daily://日冻结存储指针
		{
			return uStoreIndex_SY.Data.Daily;
		}
		case SY_StoreCurIndex_Month://月冻结存储指针
		{
			return uStoreIndex_SY.Data.Month;
		}
		case SY_StoreCurIndex_Year://年冻结存储指针
		{
			return uStoreIndex_SY.Data.Year;
		}
		default:
			break;
	}
	return 0;
}

/****************************************************************
Function:    u16 Gp30CheckErrDataApp (void)
Description:  查询错误信息
Input:
Return:
Others:
Bit0:当前流量>Q4或者<Q1    GP30_low_high_flow
Bit1:水温超出温度等级范围  GP30_out_temp
Bit2:倒转           GP30_reverse_flow
Bit3:无水           GP30_no_water
Bit4:GP30异常		GP30_err
Bit5:低流量报警     GP30_low_flow
Bit6:换能器接受信号低（低振幅）	GP30_low_amp;
Bit7:GP30通信异常  GP30_communication_err
Bit8:TOF overrun   GP30_TOF_overrun
Bit9:水流方向    water_reverse
*****************************************************************/
u8 sy_rtc_time_overflow=0;//过流报警时间
u8 sy_rtc_time_reverseflow=0;//反流报警时间
u8 sy_rtc_time_leakflow=0;//漏水报警时间
u8 sy_rtc_time_nowater=0;//空管报警时间
//时间延迟置位
u16 Gp30CheckErrDataApp_ReWrite(void)
{
	u16 temp_state=0;
	u8 temp_data=0;
	temp_state = Gp30CheckErrDataApp();
	//流量超限
	if(temp_state&0x0001)//gp30_event_flag.low_high_flow<<0
	{
		temp_state &=~0x0001;//先清除
		sy_rtc_time_overflow += 1;//增加1分钟
		if(GetTLVData(TagOverFlowMinute, LenOverFlowMinute, &temp_data))
		{
			if(sy_rtc_time_overflow>temp_data)
			{
				temp_state |=0x0001;//满足条件再置起
			}
		}
	}
	else
	{
		sy_rtc_time_overflow = 0;
	}
	//反流报警
	if(temp_state&0x0004)//gp30_event_flag.low_high_flow<<0
	{
		temp_state &=~0x0004;
		sy_rtc_time_reverseflow += 1;
		if(GetTLVData(TagReverseFlowMinute, LenReverseFlowMinute, &temp_data))
		{
			if(sy_rtc_time_reverseflow>temp_data)
			{
				temp_state |=0x0004;
			}
		}
	}
	else
	{
		sy_rtc_time_reverseflow = 0;
	}
	//漏水报警
	if(temp_state&0x0020)//gp30_event_flag.low_high_flow<<0
	{
		temp_state &=~0x0020;
		sy_rtc_time_leakflow += 1;
		if(GetTLVData(TagLeakFlowMinute, LenLeakFlowMinute, &temp_data))
		{
			if(sy_rtc_time_leakflow>temp_data)
			{
				temp_state |=0x0020;
			}
		}
	}
	else
	{
		sy_rtc_time_leakflow = 0;
	}
	//空管报警
	if(temp_state&0x0008)//gp30_event_flag.low_high_flow<<0
	{
		temp_state &=~0x0008;
		sy_rtc_time_nowater += 1;
		if(GetTLVData(TagNoWaterMinute, LenNoWaterMinute, &temp_data))
		{
			if(sy_rtc_time_nowater>temp_data)
			{
				temp_state |=0x0008;
			}
		}
	}
	else
	{
		sy_rtc_time_nowater = 0;
	}
	return temp_state;
}
u8 PowerCheckStatusSys_ReWrite(void) 
{
	UNION_2U8TOU16 temp_data;
	u16 temp_power;
	if(GetTLVData(TagPowerLow001V, LenPowerLow001V, &temp_data.data_buf[0]))
	{
		temp_power = PowerCheckValueSys();
		if(temp_power < temp_data.datas)//数据转一下
		{
			return LOW_POWER;
		}
	}
	return NORMAL_POWER;
}

void ShenYang_SOE_Machine(void)
{
	switch(step_ShenYangSOEMachine)
	{
		case SY_STATE_S0:
		{
			if(FrameCheckMsgApp(MsgShenYang_WarningCheck))
			{
				FrameClearMsgApp(MsgShenYang_WarningCheck);
				step_ShenYangSOEMachine =SY_STATE_S1;
			}
			if(FrameCheckMsgApp(MsgShenYang_WarningStoreReset))
			{
				FrameClearMsgApp(MsgShenYang_WarningStoreReset);
				uSOE_Data_Old = 0;
				uSOE_Temprature_ShortOld = 0;
				uSOE_Temprature_OpenOld = 0;
				uSOE_Update_Flag = 0xFFFFFFFF;
				uWaterState =0;
			}
		}
		break;
		case SY_STATE_S1://报警事件消息判断
		{
			uSOE_Data_New = Gp30CheckErrDataApp_ReWrite();
			#if defined(VALVE)
			uSOE_Valve_New = ValveGetStateApp();
			#endif
			uSOE_Power_New = PowerCheckStatusSys_ReWrite();
			uSOE_Temprature_New = TempCheckValueApp();
			if(uSOE_Data_Old != uSOE_Data_New)
			{
				uSOE_Data_Old = uSOE_Data_New;
				if((uSOE_Data_New&0x0001)&&(uSOE_Update_Flag&0x00000001))//gp30_event_flag.low_high_flow<<0
				{
					uSOE_Update_Flag &=~0x00000001;
					SaveNewSoe(SOE_SY_OVER_FLOW,SOE_REMARK_NOR);//流量超限
					if(checkWarningUploadState(UPLOAD_OVER_FLOW))
					{
						FrameSetMsgApp(MsgShenYang_NbUploadWarning);
					}
					FrameSetMsgApp(MsgShenYang_NbUploadWarningWithCircle);
					uWaterState = SOE_SY_OVER_FLOW -1000;
				}
				if((uSOE_Data_New&0x0002)&&(uSOE_Update_Flag&0x00000002))//(gp30_event_flag.out_temp<<1)
				{
					uSOE_Update_Flag &=~0x00000002;
					SaveNewSoe(SOE_SY_TEMP_OVER,SOE_REMARK_NOR);//温度超限
					if(checkWarningUploadState(UPLOAD_TEMP_OVER))
					{
						FrameSetMsgApp(MsgShenYang_NbUploadWarning);
					}
					FrameSetMsgApp(MsgShenYang_NbUploadWarningWithCircle);
				}
				if((uSOE_Data_New&0x0004)&&(uSOE_Update_Flag&0x00000004))//(gp30_event_flag.reverse_flow<<2)
				{
					uSOE_Update_Flag &=~0x00000004;
					SaveNewSoe(SOE_SY_REVERSE_FLOW,SOE_REMARK_NOR);//反流报警
					if(checkWarningUploadState(UPLOAD_REVERSE_FLOW))
					{
						FrameSetMsgApp(MsgShenYang_NbUploadWarning);
					}
					FrameSetMsgApp(MsgShenYang_NbUploadWarningWithCircle);
					uWaterState = SOE_SY_REVERSE_FLOW -1000;
				}
				if((uSOE_Data_New&0x0008)&&(uSOE_Update_Flag&0x00000008))//(gp30_event_flag.no_water<<3)
				{
					uSOE_Update_Flag &=~0x00000008;
					SaveNewSoe(SOE_SY_NO_WATER,SOE_REMARK_NOR);//空管报警
					if(checkWarningUploadState(UPLOAD_NO_WATER))
					{
						FrameSetMsgApp(MsgShenYang_NbUploadWarning);
					}
					FrameSetMsgApp(MsgShenYang_NbUploadWarningWithCircle);
				}
				if((uSOE_Data_New&0x0010)&&(uSOE_Update_Flag&0x00000010))//(gp30_event_flag.gp30_err<<4) 
				{
					uSOE_Update_Flag &=~0x00000010;
					SaveNewSoe(SOE_SY_CIRCLE_WRONG,SOE_REMARK_NOR);//电路故障
					FrameSetMsgApp(MsgShenYang_NbUploadWarningWithCircle);
					uWaterState = SOE_SY_CIRCLE_WRONG -1000;
				}
				if((uSOE_Data_New&0x0020)&&(uSOE_Update_Flag&0x00000020))//(gp30_event_flag.low_flow<<5)
				{
					uSOE_Update_Flag &=~0x00000020;
					SaveNewSoe(SOE_SY_LEAK_FLOW,SOE_REMARK_NOR);//漏水报警
					if(checkWarningUploadState(UPLOAD_LEAK_FLOW))
					{
						FrameSetMsgApp(MsgShenYang_NbUploadWarning);
					}
					FrameSetMsgApp(MsgShenYang_NbUploadWarningWithCircle);
				}
				if((uSOE_Data_New&0x0040)&&(uSOE_Update_Flag&0x00000040))//(gp30_event_flag.low_amp<<6) 
				{
					uSOE_Update_Flag &=~0x00000040;
					SaveNewSoe(SOE_SY_LOW_AMP,SOE_REMARK_NOR);//低振幅
				}
				if((uSOE_Data_New&0x0080)&&(uSOE_Update_Flag&0x00000080))//(gp30_event_flag.communication_err<<7)
				{
					uSOE_Update_Flag &=~0x00000080;
					SaveNewSoe(SOE_SY_GP30_COMERR,SOE_REMARK_NOR);//GP30通信故障
					FrameSetMsgApp(MsgShenYang_NbUploadWarningWithCircle);
					uWaterState = SOE_SY_GP30_COMERR -1000;
				}
				if((uSOE_Data_New&0x0001)&&(uSOE_Update_Flag&0x00000100))//(gp30_event_flag.tof_overrun<<8)
				{
					uSOE_Update_Flag &=~0x00000100;
					SaveNewSoe(SOE_SY_TOF_OVER,SOE_REMARK_NOR);//TOF跳变
				}
				if((uSOE_Data_New&0x0100)&&(uSOE_Update_Flag&0x00000200))//(gp30_event_flag.water_direction<<9)
				{
					uSOE_Update_Flag &=~0x00000200;
					SaveNewSoe(SOE_SY_REVERSE_FLOW,SOE_REMARK_NOR);//反流报警
					/*if(checkWarningUploadState(UPLOAD_REVERSE_FLOW))
					{
						FrameSetMsgApp(MsgShenYang_NbUploadWarning);
					}*/
					uWaterState = SOE_SY_REVERSE_FLOW -1000;
				}
			}
			if(uSOE_Valve_Old != uSOE_Valve_New)
			{
				u8 tem_tlv_data[7];
				uSOE_Valve_Old = uSOE_Valve_New;
				RtccGetDateTime(&uTempRtccTime_BCD);//阀门动作时间

				if(Sys_data_object.P1.ies_valve_paras.valve_err)
				{
					SaveNewSoe(SOE_SY_VALVE_ERR,SOE_REMARK_NOR);//阀门操作失败
					tem_tlv_data[0] = 0xFF;
				}	
				else
				{
					if(uSOE_Valve_New == VALVE_STATE_OPEN)
					{
						SaveNewSoe(SOE_SY_VALVE_OPEN,SOE_REMARK_NOR);//开阀
						tem_tlv_data[0] = 0x55;
					}
					else if(uSOE_Valve_New == VALVE_STATE_CLOSE)
					{
						SaveNewSoe(SOE_SY_VALVE_CLOSE,SOE_REMARK_NOR);//关阀
						tem_tlv_data[0] = 0x99;
					}
				}
			
				if(checkWarningUploadState(UPLOAD_VALVE_CHANGE))
				{
					FrameSetMsgApp(MsgShenYang_NbUploadWarning);
				}
				FrameSetMsgApp(MsgShenYang_NbUploadWarningWithCircle);
				tem_tlv_data[1] = uTempRtccTime_BCD.s.year;
				tem_tlv_data[2] = uTempRtccTime_BCD.s.month;
				tem_tlv_data[3] = uTempRtccTime_BCD.s.day;
				tem_tlv_data[4] = uTempRtccTime_BCD.s.hour;
				tem_tlv_data[5] = uTempRtccTime_BCD.s.minute;
				tem_tlv_data[6] = uTempRtccTime_BCD.s.second;
				SetTLVData(TagValveState, LenValveState, tem_tlv_data);//保存到TLV缓存
			}
			if(uSOE_Power_Old != uSOE_Power_New)
			{
				uSOE_Power_Old = uSOE_Power_New;
				if((LOW_POWER == uSOE_Power_New)&&(uSOE_Update_Flag&0x00000400))
				{
					uSOE_Update_Flag &=~0x00000400;
					SaveNewSoe(SOE_SY_LOW_POWER,SOE_REMARK_NOR);//低电压报警
					if(checkWarningUploadState(UPLOAD_LOW_POWER))
					{
						FrameSetMsgApp(MsgShenYang_NbUploadWarning);
					}
					FrameSetMsgApp(MsgShenYang_NbUploadWarningWithCircle);
				}
			}
			if(uSOE_Temprature_New == -1)
			{
				uSOE_Temprature_OpenNew = 1;
			}
			else if(uSOE_Temprature_New == 36)
			{
				uSOE_Temprature_ShortNew = 1;
			}
			step_ShenYangSOEMachine = SY_STATE_S2;
		}
		break;
		case SY_STATE_S2://其他触发事件的处理
		{
			//获取当前时间
			RtccGetDateTime(&uTempRtccTime_BCD);
			//需要存eeprom
			if( (uTempRtccTime_BCD.s.year == ValveActiveDateTime.s.year)&&
				(uTempRtccTime_BCD.s.month == ValveActiveDateTime.s.month)&&
				(uTempRtccTime_BCD.s.day == ValveActiveDateTime.s.day)&&
				(uTempRtccTime_BCD.s.hour == ValveActiveDateTime.s.hour)&&
				(uTempRtccTime_BCD.s.minute == ValveActiveDateTime.s.minute))
			{
				if (Valve_Active == 0x55) //开阀门
				{
					FrameSetMsgApp(ValveOpenMsg);
				}
				else if (Valve_Active == 0x99) //关阀门
				{
					FrameSetMsgApp(ValveCloseMsg);
				}
				else if(Valve_Active == 0xAA)//半开
				{
					FrameSetMsgApp(ValveHalfMsg);
				}
			}
			step_ShenYangSOEMachine = SY_STATE_S0;
		}
		break;
		default:
		{
			step_ShenYangSOEMachine = SY_STATE_S0;
			break;
		}
	}
}

u8 getWaterState(void)
{
	return uWaterState;
}

//TLV数据
u8 getTlvTagIndex(u8 inTag) 
{
    int index=0;
    for(index=0;index<TLV_STR_BUF_LEN;index++)
    {
        if(TlvStrBuf[index].Tag == inTag)
        {
            return index;
        }
    }
	return 0xFF;
}

u8 SetTLVData(u8 Tag, u8 Len, u8* pValue)
{
	int index = getTlvTagIndex(Tag);
	int i=0;
	u8 temp_addr=0;
	if(index != 0xFF)
	{
		if(Len == TlvStrBuf[index].Len)//长度判断
		{
			temp_addr = TlvStrBuf[index].ValueAddr;
			for(i=0; i<TlvStrBuf[index].Len ;i++)
			{
				TlvValue.data_buf[temp_addr++] = *pValue++;
			}
			TlvStrBuf[index].EditFlag = 1;
			if(tlv_store_cnt<=TLV_STR_BUF_LEN)
			{
				tlv_store_cnt++;
			}
			return SUCCESS_IES;
		}
	}
	return ERROR_IES;
}

u8 GetTLVData(u8 Tag, u8 Len, u8* pValue)
{
	int index = getTlvTagIndex(Tag);
	int i=0;
	u8 temp_addr=0;
	if(TagWorkTime == TlvStrBuf[index].Tag)//更新表计运行时间
	{
		RtccGetDateTime(&uTempRtccTime_BCD);
		TlvValue.s.ValueWorkTime[0] = uTempRtccTime_BCD.s.year;
		TlvValue.s.ValueWorkTime[1] = uTempRtccTime_BCD.s.month;
		TlvValue.s.ValueWorkTime[2] = uTempRtccTime_BCD.s.day;
		TlvValue.s.ValueWorkTime[3] = uTempRtccTime_BCD.s.hour;
		TlvValue.s.ValueWorkTime[4] = uTempRtccTime_BCD.s.minute;
		TlvValue.s.ValueWorkTime[5] = uTempRtccTime_BCD.s.second;
	}
	if(index != 0xFF)
	{
		if(Len == TlvStrBuf[index].Len)//长度判断
		{
			temp_addr = TlvStrBuf[index].ValueAddr;
			for(i=0; i<TlvStrBuf[index].Len ;i++)
			{
				*pValue++ = TlvValue.data_buf[temp_addr++];
			}
			return SUCCESS_IES;
		}
	}
	return ERROR_IES;
}
/**
位（bit）	含义
0	流量超限
1	空管报警
2	反流报警
3	漏水报警
4	低电压报警
5	温度超范围
6	上盖被打开
7	阀门动作
8-11	预留
12-15	厂家自定义
*/
u8 checkWarningUploadState(TypeDef_UploadWarning tWarningType)
{
	UNION_2U8TOU16 temp_warning_id;
	if(GetTLVData(TagWarningId, LenWarningId, &temp_warning_id.data_buf[0]))
	{
		if((temp_warning_id.datas>>tWarningType)&0x0001)
		{
			return 1;
		}
	}
	return 0;
}

void ShenYang_TlvDataStoreMachine(void)
{
	static u8 index_edit=0xFF;
	switch(step_TlvDataStore)
	{
		case SY_STATE_S0:
		{	
			if(FrameCheckMsgApp(MsgNbUploadFreqChange))
			{
				FrameClearMsgApp(MsgNbUploadFreqChange);
				temp_tlv_data[0]=0;
				temp_tlv_data[1]=Sys_data_object.P1.Time_upload_2006.s.hour;//小时
				temp_tlv_data[2]=Sys_data_object.P1.Time_upload_2006.s.minute;//分钟
				temp_tlv_data[3]=0;
				SetTLVData(TagUploadStartTime, LenUploadStartTime, temp_tlv_data);
			}
			if(FrameCheckMsgApp(MsgShenYang_UdpIpPortChange))
			{
				int len=0;
				FrameClearMsgApp(MsgShenYang_UdpIpPortChange);
				//IP
				for (int i = 0; i < 4; i++)
				{
					if (Sys_data_object.P2.server_ip[i] < 10)
					{
						temp_tlv_data[len++] = Sys_data_object.P2.server_ip[i] + 0x30;
					}
					else if (Sys_data_object.P2.server_ip[i] < 100)
					{
						temp_tlv_data[len++] = (Sys_data_object.P2.server_ip[i] / 10) + 0x30;
						temp_tlv_data[len++] = (Sys_data_object.P2.server_ip[i] % 10) + 0x30;
					}
					else
					{
						temp_tlv_data[len++] = (Sys_data_object.P2.server_ip[i] / 100) + 0x30;
						temp_tlv_data[len++] = (Sys_data_object.P2.server_ip[i] / 10) % 10 + 0x30;
						temp_tlv_data[len++] = (Sys_data_object.P2.server_ip[i] % 10) + 0x30;
					}
					temp_tlv_data[len++] = '.';
				}
				len--; //消除多余的点号
				temp_tlv_data[len++] = ':';
				temp_tlv_data[len++] = ((Sys_data_object.P2.server_port.datas) / 10000) + 0x30;
				temp_tlv_data[len++] = (((Sys_data_object.P2.server_port.datas) % 10000) / 1000) + 0x30;
				temp_tlv_data[len++] = (((Sys_data_object.P2.server_port.datas) % 1000) / 100) + 0x30;
				temp_tlv_data[len++] = (((Sys_data_object.P2.server_port.datas) % 100) / 10) + 0x30;
				temp_tlv_data[len++] = ((Sys_data_object.P2.server_port.datas) % 10) + 0x30;
				temp_tlv_data[len++] = 0;
				SetTLVData(TagServerIpPort, LenServerIpPort, temp_tlv_data);
			}
			if(tlv_store_cnt>0)
			{
				step_TlvDataStore = SY_STATE_S1;
			}
		}
		break;
		case SY_STATE_S1://
		{
			if(TASK_IDLE == EepIfIdle())
			{
				for(int index=0; index<TLV_STR_BUF_LEN; index++)
				{
					if(TlvStrBuf[index].EditFlag == 1)
					{
						TlvStrBuf[index].EditFlag = 0;
						index_edit = index;//缓存被编辑的标号
						EepWrite(&TlvValue.data_buf[TlvStrBuf[index].ValueAddr], (EEP_ADDR_SY_TLVDATA+TlvStrBuf[index].ValueAddr), TlvStrBuf[index].Len, &s_sy_eep_complete);
						break;
					} 
				}
				tlv_store_cnt--;//无论是否找到,都得减掉
				step_TlvDataStore  = SY_STATE_S2;
			}
		}
		break;
		case SY_STATE_S2://沈阳规约转存积成规约
		{
			if(TASK_IDLE == EepIfIdle())
			{
				GetTLVData(TlvStrBuf[index_edit].Tag, TlvStrBuf[index_edit].Len, temp_tlv_data);//获取tlv数据
				if(TlvStrBuf[index_edit].Tag == TagValveState)//判断是否为阀控设置
				{
					if((temp_tlv_data[1]==0)&&(temp_tlv_data[2]==0)&&(temp_tlv_data[3]==0)&&
					   (temp_tlv_data[4]==0)&&(temp_tlv_data[5]==0)&&(temp_tlv_data[6]==0))
					{
						if (temp_tlv_data[0] == 0x55) //开阀门
						{
							FrameSetMsgApp(ValveOpenMsg);
						}
						else if (temp_tlv_data[0] == 0x99) //关阀门
						{
							FrameSetMsgApp(ValveCloseMsg);
						}
						else if(temp_tlv_data[0] == 0xAA)//半开
						{
							FrameSetMsgApp(ValveHalfMsg);
						}
					}
					else
					{
						uTempRtccTime_BCD.s.year  = temp_tlv_data[1];
						uTempRtccTime_BCD.s.month = temp_tlv_data[2];
						uTempRtccTime_BCD.s.day   = temp_tlv_data[3];
						uTempRtccTime_BCD.s.hour  = temp_tlv_data[4];
						uTempRtccTime_BCD.s.minute= temp_tlv_data[5];
						uTempRtccTime_BCD.s.second= temp_tlv_data[6]; 
						if(Rtcc_Check_DateTime(&uTempRtccTime_BCD) == SUCCESS)//判断时间是否合法
						{ 
							Valve_Active = temp_tlv_data[0];
							ValveActiveDateTime.s.year   = uTempRtccTime_BCD.s.year;
							ValveActiveDateTime.s.month  = uTempRtccTime_BCD.s.month;
							ValveActiveDateTime.s.day    = uTempRtccTime_BCD.s.day;
							ValveActiveDateTime.s.hour   = uTempRtccTime_BCD.s.hour;
							ValveActiveDateTime.s.minute = uTempRtccTime_BCD.s.minute;
							ValveActiveDateTime.s.second = 0; 
							for(int i=0;i<7;i++)//修改为阀门无动作
							{
								TlvValue.s.ValueValveState[i] = 0;
							}
							EepWrite(&TlvValue.data_buf[TlvStrBuf[index_edit].ValueAddr], (EEP_ADDR_SY_TLVDATA+TlvStrBuf[index_edit].ValueAddr), TlvStrBuf[index_edit].Len, &s_sy_eep_complete);
						}
					}
				}
				if(TlvStrBuf[index_edit].Tag == TagUploadRetryTimes)//设置重发次数---转存积成规约
				{
					Sys_data_object.P1.report_retry_times = temp_tlv_data[0];
					EepWrite(&Sys_data_object.P1.report_retry_times, EEP_RETRY_TIMES_ADDR, 1, &s_sy_eep_complete);//转存积成规约
					FrameSetMsgApp(MsgNbRetryTimesChange);
				}
				if(TlvStrBuf[index_edit].Tag == TagUploadRetryTimes)//重发延时时间---转存积成规约
				{
					Sys_data_object.P1.Upload_again_interval_time.datas = temp_tlv_data[0]*60;
					EepWrite(&Sys_data_object.P1.Upload_again_interval_time.datas_map.data0, EEP_FRAME_NB_PARA_ADDR, 2, &s_sy_eep_complete);
					Frame_Para_Init();	
				}
				if(TlvStrBuf[index_edit].Tag == TagUploadRetryTimes)//设置服务器IP地址---转存积成规约
				{
					
				}
				if(TlvStrBuf[index_edit].Tag == TagUploadRetryTimes)//设置APN---转存积成规约
				{
					
				}
				step_TlvDataStore  = SY_STATE_S0;
			}	
		}
		break;
		default:
		{
			step_TlvDataStore = SY_STATE_S0;
			break;
		}
	}
}

//计算往前几天的日期和时间,带周
DateTime getBackDateTime(u8 backDays)
{
	DateTime cdt;
	struct tm tmdt;
	struct tm *ldt;
	time_t ctt;

	RtccGetDateTime(&cdt);
	tmdt.tm_year = (cdt.s.year >> 4) * 10 + (cdt.s.year & 0x0F) + 100;
	tmdt.tm_mon = (cdt.s.month >> 4) * 10 + (cdt.s.month & 0x0F) - 1;
	tmdt.tm_mday = (cdt.s.day >> 4) * 10 + (cdt.s.day & 0x0F);
	tmdt.tm_hour = (cdt.s.hour >> 4) * 10 + (cdt.s.hour & 0x0F);
	tmdt.tm_min = (cdt.s.minute >> 4) * 10 + (cdt.s.minute & 0x0F);
	tmdt.tm_sec = (cdt.s.second >> 4) * 10 + (cdt.s.second & 0x0F);
	tmdt.tm_isdst = 0;
	ctt = mktime(&tmdt);

    //时间往前
	ctt -= backDays*24*3600;

	ldt = localtime(&ctt);

	cdt.s.year = ((*ldt).tm_year - 100) / 10 * 16 + ((*ldt).tm_year - 100) % 10;
	cdt.s.month = ((*ldt).tm_mon + 1) / 10 * 16 + ((*ldt).tm_mon + 1) % 10;
	cdt.s.day = ((*ldt).tm_mday) / 10 * 16 + ((*ldt).tm_mday) % 10;
	cdt.s.weekday = ((*ldt).tm_wday) / 10 * 16 + ((*ldt).tm_wday) % 10;
	cdt.s.hour = ((*ldt).tm_hour) / 10 * 16 + ((*ldt).tm_hour) % 10;
	cdt.s.minute = ((*ldt).tm_min) / 10 * 16 + ((*ldt).tm_min) % 10;
	cdt.s.second = ((*ldt).tm_sec) / 10 * 16 + ((*ldt).tm_sec) % 10;
	return cdt;
}
//计算往前几天的日期和时间,不带周
DateTime_noweek getBackDateTimeNoWeek(u8 backDays)
{
	DateTime cdt;
	DateTime_noweek  cdt_noweek;
	struct tm tmdt;
	struct tm *ldt;
	time_t ctt;

	RtccGetDateTime(&cdt);//获取当前时间
	tmdt.tm_year = (cdt.s.year >> 4) * 10 + (cdt.s.year & 0x0F) + 100;
	tmdt.tm_mon = (cdt.s.month >> 4) * 10 + (cdt.s.month & 0x0F) - 1;
	tmdt.tm_mday = (cdt.s.day >> 4) * 10 + (cdt.s.day & 0x0F);
	tmdt.tm_hour = (cdt.s.hour >> 4) * 10 + (cdt.s.hour & 0x0F);
	tmdt.tm_min = (cdt.s.minute >> 4) * 10 + (cdt.s.minute & 0x0F);
	tmdt.tm_sec = (cdt.s.second >> 4) * 10 + (cdt.s.second & 0x0F);
	tmdt.tm_isdst = 0;
	ctt = mktime(&tmdt);
    //时间往前
	ctt -= backDays*24*3600;


	ldt = localtime(&ctt);

	cdt_noweek.s.year = ((*ldt).tm_year - 100) / 10 * 16 + ((*ldt).tm_year - 100) % 10;
	cdt_noweek.s.month = ((*ldt).tm_mon + 1) / 10 * 16 + ((*ldt).tm_mon + 1) % 10;
	cdt_noweek.s.day = ((*ldt).tm_mday) / 10 * 16 + ((*ldt).tm_mday) % 10;
	cdt_noweek.s.hour = ((*ldt).tm_hour) / 10 * 16 + ((*ldt).tm_hour) % 10;
	cdt_noweek.s.minute = ((*ldt).tm_min) / 10 * 16 + ((*ldt).tm_min) % 10;
	cdt_noweek.s.second = ((*ldt).tm_sec) / 10 * 16 + ((*ldt).tm_sec) % 10;
	return cdt_noweek;
}

/********************************************************************************
函数名：ShenYangStore_IfSleep
说  明：数据存储状态机空闲判断
********************************************************************************/
u8 ShenYangStore_IfSleep(void)
{
    if((step_ShenYangStoreMachine == SY_STATE_S0)&&(step_ShenYangSOEMachine == SY_STATE_S0)&&(step_TlvDataStore ==SY_STATE_S0))
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}
		
#endif



