#include "sys_config.h"
#if PRODUCT_SS
#include "sys_processor.h"
#include "UWater_eeprom_driver.h"
#include "UWater_valve_driver.h"
#include "UWater_Valve_app.h"
#include "UWater_rtcc_app.h"
#include "UWater_power_app.h"
#include "UWater_gp22_app.h"
#include "UWater_pt_app.h" 
#include "UWater_DataTools_App.h"
#include "UWater_QZ_Store_App.h"
#include "nb_upload_monitor.h"
#include "UWater_frame_func_ReplyNB.h"
#include "UWater_frame_func_ReplyShenShui.h"

static u8 s_eep_complete_ShenShui=0;
static u8 task_complete_ShenShui = PTL_PACK_BUSY;
static u8 g_frame_temp_buf_ShenShui[UP_BUF_SIZE];	//数据部分缓冲区
static u16 g_frame_temp_len_ShenShui=0;
static DateTime shenshui_rtcc_time;

static STRUCT_FRAME_IES frame_struct_ShenShui; //规约信息 
static u8 *new_message_data_rev_ShenShui; 
static u8 up_deal_stat_ShenShui = UP_DEAL_S0;//规约处理状态机
/*******主动上传数据相关变量*****************************************/
static u16 upload_serial_num_ShenShui=0;
static STRUCT_FRAME_IES upload_struct_ShenShui; //规约信息 
static u8 upload_frame_stat_ShenShui=0; //主动上报组帧状态
static u8 upload_task_complete_ShenShui = PTL_PACK_FAIL;  
static u16 upload_temp_len_ShenShui=0;//主动上报数据长度	
static UNION_SIGNAL signal_val;

static u8 upload_frame_retry_times=0;
static u8 temp_upload_step=0;
//static u8 temp_Supplemental_send_NO;
static u8 temp_store_msg=0;

//积成规约上报缓存
static STRU_UPLOAD_FRAME upload_frame;//长帧上报数据
static STRU_UPLOAD_FRAME store_frame;//长帧存储数据,和上报帧缓存独立,防止上报期间存储数据
static STRU_SHORT_UPLOAD_FRAME short_upload_frame;//短帧数据
//新补报规约
static u8 supplement_data_frame_state = SUPPLEMENT_DATA_FRAME_S0;
static u8 supplement_cnt = 0;
static u8 supplement_buf[300];

//补报数据,间隔流量存储
static TYP_STATE_SS step_save_supplt=SS_STATE_S0; 
static RTCC_To_Float old_jiange_total_flow;
static RTCC_To_Float new_jiange_total_flow;
static RTCC_UNION_2U8TOU16 jiange_data;
//补报标志存储
static TYP_STATE_SS step_clear_flag=SS_STATE_S0; 
static u32 temp_supplement_bit;
//主动上传帧------------------------判断的应答控制字，返回PTL_PACK_FAIL
//深水和江水上传应答处理统一
static void frame_func_reply_NB_AckUpload(void)
{
	g_frame_temp_len_ShenShui =0;
	u8 sernum0 = upload_serial_num_ShenShui&0xFF;
	u8 sernum1 = (upload_serial_num_ShenShui>>8)&0xFF;
	if(*(new_message_data_rev_ShenShui + 6) == 1)//上报数据格式异常
	{
		if(frame_struct_ShenShui.serial_num0 == sernum0)//江西水务产线生产测试返回格式异常,应判断过
		{
			if(frame_struct_ShenShui.serial_num1 == sernum1)
			{
				NB_SS_Control_SetPara(NB_SS_FormatError, 1);//置起上报格式异常标志
				FrameSetMsgApp(MsgNbUplaod_AckOk);//放宽限制,防止异常数据导致一直补报
			}
		}
	}
	else
	{
		if(frame_struct_ShenShui.serial_num0 == sernum0)
		{
			if(frame_struct_ShenShui.serial_num1 == sernum1)
			{
				FrameSetMsgApp(MsgNbUplaod_AckOk);
			}
		}
		NB_SS_Control_SetPara(NB_SS_FormatError, 0);//清除上报格式异常标志
	}
	task_complete_ShenShui = PTL_PACK_FAIL;//这是上报应答帧,返回错误,表计不应答
}
//短帧上报应答
static void frame_func_reply_NB_AckUploadShort(void)
{
	frame_func_reply_NB_AckUpload();
}
//新补报规约应答
static void frame_func_reply_NB_AckNewSupplement(void)
{
	frame_func_reply_NB_AckUpload();
}
//登录帧------------------------判断的应答控制字，返回PTL_PACK_FAIL
static void frame_func_reply_NB_AckLogin(void)
{			
	FrameSetMsgApp(MsgNbUplaod_AckLoginOk);
}

static void ies_ack_upload_valve_frame(void)
{    
    g_frame_temp_len_ShenShui =0;
#if defined(VALVE)
	u8 sernum0 = upload_serial_num_ShenShui&0xFF;
	u8 sernum1 = (upload_serial_num_ShenShui>>8)&0xFF;
    u8 rcv_state = 0;
	rcv_state = *(new_message_data_rev_ShenShui + 6);
	if(*(new_message_data_rev_ShenShui + 6) == 1)//上报数据格式异常
	{
		NB_SS_Control_SetPara(NB_SS_FormatError, 1);//置起上报格式异常标志
	}
	else
	{
		if(frame_struct_ShenShui.serial_num0 == sernum0)
		{
			if(frame_struct_ShenShui.serial_num1 == sernum1)
			{
				FrameSetMsgApp(MsgNbUplaod_AckOk);
			}
		}
		NB_SS_Control_SetPara(NB_SS_FormatError, 0);//清除上报格式异常标志
	}
    if(0x00 == (rcv_state & 0x06))	//阀控内容-开阀
    {
        ValveDealAppStart(VALVE_STATE_OPEN);
    }
    else if(0x02 == (rcv_state & 0x06))	//阀控内容-普关
    {
        ValveDealAppStart(VALVE_STATE_CLOSE);
    }
    else if(0x04 == (rcv_state & 0x06))	//阀控内容-权关
    {
        ValveDealAppStart(VALVE_STATE_CLOSE_LOCK);
    }
    else if(0x06 == (rcv_state & 0x06))	//阀控内容-应急关
    {
        ValveDealAppStart(VALVE_STATE_CLOSE_EMERGENCY);
    }
#endif
	task_complete_ShenShui = PTL_PACK_FAIL;//这是上报应答帧,返回错误,表计不应答
}

//读历史数据
static void frame_func_reply_NB_ReadHistoryData(void)
{
    u8 temp = 0;
    u8 temp_read_index = 0;
    u8 temp_read_code = *new_message_data_rev_ShenShui;   

    switch(up_deal_stat_ShenShui)
    {
        case UP_DEAL_S0:
        {
			if(TASK_IDLE == EepIfIdle())//读取历史数据
			{
				g_frame_temp_len_ShenShui =0;
				temp_read_index = NB_SS_Control_GetPara(NB_HistroyReadIndex);
				if (temp_read_index >= temp_read_code)
				{
					temp = temp_read_index - temp_read_code;
				}
				else
				{
					temp = 32 - (temp_read_code - temp_read_index);
				}
				EepRead((FRAME_NBIot_HISTORY_READ_ADD +(temp*PACK_NUM_GPRS)), g_frame_temp_buf_ShenShui, 155, &s_eep_complete_ShenShui);		
				up_deal_stat_ShenShui  = UP_DEAL_S1;
			}
        }
        break;

        case UP_DEAL_S1:
        {
            if(TASK_IDLE == EepIfIdle())//读取历史数据完成
            {
				g_frame_temp_len_ShenShui = PACK_NUM_GPRS;  
                task_complete_ShenShui = PTL_PACK_SUCCESS;                
                up_deal_stat_ShenShui  = UP_DEAL_S0;
            }
        }
        break;
      
        default:
        {
            up_deal_stat_ShenShui  = UP_DEAL_S0;
            task_complete_ShenShui = PTL_PACK_FAIL;
        }
        break;
    }    	 
}

//清除失败次数
static void frame_func_s3_NB_ClearFailTimes(void)
{   
	if(TASK_IDLE == EepIfIdle())
	{
		g_frame_temp_len_ShenShui = 0;
		NB_SS_Control_SetPara(NB_SS_FormatError, 0);
		NB_SS_Control_SetPara(NB_UploadFailTimes, 0);
		task_complete_ShenShui = PTL_PACK_SUCCESS;
	}
}
/************************************************************************************************************/
//解深水规约
u8 crack_frame_ShenShui(u16 frame_lenth,u8 *frame_buffer)
{
	u8 temp_res = crack_frame_IES_Base(frame_lenth, frame_buffer, &frame_struct_ShenShui);
	if(temp_res == SUCCESS_IES)
	{		
		new_message_data_rev_ShenShui = frame_buffer + frame_struct_ShenShui.head_site + 16;								
		return  SUCCESS_IES;
	}
	return ERROR_IES;
}

//深水规约处理
u8 up_deal_code_ShenShui(void)
{
    task_complete_ShenShui = PTL_PACK_BUSY;
    switch(frame_struct_ShenShui.ctrl)
    {
		case SS_ACK_UPLOAD_CTRL:
		{
			frame_func_reply_NB_AckUpload();
		}
		break;
		case SHORT_ACK_UPLOAD_CTRL:
		{
			frame_func_reply_NB_AckUploadShort();
		}
		break;
		case SS_ACK_SIGN_IN_CTRL:
		{
			frame_func_reply_NB_AckLogin();
		}
		break;
		case ACK_UPLOAD_WITH_VAVLE_CTRL://携带控阀指令的应答帧
		{
			ies_ack_upload_valve_frame();
		}
		break;
		case SUPP_ACK_UPLOAD_CTRL://新补报规约应答
		{
			frame_func_reply_NB_AckNewSupplement();
		}
		break;
		case SS_CLEAR_FAIL_TIMES_CTRL:
		{
			frame_func_s3_NB_ClearFailTimes();
			frame_struct_ShenShui.ack_ctrl = SS_ACK_CLEAR_FAIL_TIMES_CTRL;
		}
		break;
		case SS_READ_HIS_CTRL:
		{
			switch(frame_struct_ShenShui.DI1)
			{
				case SS_READ_HIS_CTRL_ID1://深水规约读历史数据
				{
					frame_func_reply_NB_ReadHistoryData();
				}
				break;
				default:
				{
					up_deal_stat_ShenShui  = UP_DEAL_S0;
					task_complete_ShenShui = PTL_PACK_FAIL; 
				}
			}
			frame_struct_ShenShui.ack_ctrl = SS_ACK_READ_HIS_CTRL;
		}
		break;
        default:
        {     
			up_deal_stat_ShenShui  = UP_DEAL_S0;
			task_complete_ShenShui = PTL_PACK_FAIL;           
        }
        break;
    }
    return task_complete_ShenShui;
}

void Frame_ResetDealState_ShenShui(void)
{
	up_deal_stat_ShenShui = UP_DEAL_S0;//基线扩展规约处理状态复位
}
//应答数据组帧
void Frame_ack_ShenShui(u8* p_send_buf, u16* p_send_len)
{
	STRUCT_FRAME_IES temp_struct;
	
	temp_struct.ack_ctrl = frame_struct_ShenShui.ack_ctrl;
	temp_struct.DI0 = frame_struct_ShenShui.DI0;
	temp_struct.DI1 = frame_struct_ShenShui.DI1;
	temp_struct.sta = frame_struct_ShenShui.sta;
	temp_struct.serial_num0 = frame_struct_ShenShui.serial_num0;
	temp_struct.serial_num1 = frame_struct_ShenShui.serial_num1;

	Frame_ack_IesBase(temp_struct,g_frame_temp_buf_ShenShui, g_frame_temp_len_ShenShui, p_send_buf, p_send_len);
}
/****************************************************************以下主动上报数据的处理*********************************************/
static void setStoreMsg(SS_StoreMsgType msg, boolean_t bFlag)
{
    if(TRUE == bFlag)
    {
        temp_store_msg |= (1<<msg);
    }
    else
    {
        temp_store_msg &= (~(1<<msg));
    }    
}

static boolean_t getStoreMsg(SS_StoreMsgType msg)
{
	if(temp_store_msg & (1<<msg))
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

//更新数据包中的当前流量和信息状态
static void Upload_RenewCurrentData(STRU_UPLOAD_FRAME* temp_frame)
{
	MET_STATE temp_state; //状态字
	u8 temp1 = 0;
	Fram_To_Float tmp_float;
	u8* hight_flow; 
	DateTime tempRtcc_time;

	tmp_float.gp22_u32  = Gp30CheckDataApp(2);
	temp_frame->data.CurrentFlow.datas = (u32)(tmp_float.gp22_float*10);//当前累计流量
	temp_frame->data.CurrentFlowForward.datas = temp_frame->data.CurrentFlow.datas;//当前累计正向流量
	tmp_float.gp22_u32 = Gp30CheckDataApp(3);//当前累积逆向流量
	temp_frame->data.CurrentFlowReverse.datas = (u32)(tmp_float.gp22_float*10);
	//获取:日最高流量(4字节),日最高流量持续时间(2字节),日最高流量开始时间(8字节,预留)	
	RtccCheck_Hight_Flow(&hight_flow);			
	for(temp1 = 0; temp1 <14 ;temp1++)
	{
		temp_frame->data.DailyHighestFlowAndTime[temp1] = *(hight_flow+temp1);
	}
	//获取:rsrp(2B),电池电压(2B),信噪比(2B),无线信号覆盖等级ecl(2B),CSQ(1B),电压阈值(1B)
	temp_frame->data.UploadInfo = UploadGetInfo_Base();
	//获取间隔流量:2B(间隔流量)X2(次/h)X24h=96B
	/*
	上报时通过UPLOAD_DATA_READ_HIS获取，0点存储时通过UPLOAD_DATA_STEP_FLOW获取
	*/
	//上报时间
	RtccGetDateTime(&tempRtcc_time);//上报时间
	temp_frame->data.UploadTime[0] = tempRtcc_time.s.second;
	temp_frame->data.UploadTime[1] = tempRtcc_time.s.minute;
	temp_frame->data.UploadTime[2] = tempRtcc_time.s.hour;
	temp_frame->data.UploadTime[3] = tempRtcc_time.s.day;
	temp_frame->data.UploadTime[4] = tempRtcc_time.s.month;
	temp_frame->data.UploadTime[5] = tempRtcc_time.s.year; 
	temp_frame->data.UploadTime[6] = 0x20;
	//状态字
	temp_state = FrameGetByte();
	temp_frame->data.StateBytes[0] = temp_state.stat0;
	temp_frame->data.StateBytes[1] = temp_state.stat1;
	temp_frame->data.StateBytes[2] = temp_state.stat2;
	temp_frame->data.StateBytes[3] = temp_state.stat3;
	temp_frame->data.StateBytes[4] = temp_state.stat4;
	//累计活跃时间
	temp_frame->data.DailyActiveTime.datas = GetEventUploadType();//上报类型
}
//短帧上报数据整理
static void Upload_RenewShortFrameData(void)
{
	DateTime tempRtcc_time;
	//流量
	short_upload_frame.data.null_buf.datas = 0; 
	short_upload_frame.data.CurrentFlow_short.datas = upload_frame.data.CurrentFlow.datas;//当前累计流量
	//信号\电池相关
	upload_frame.data.UploadInfo = UploadGetInfo_Base();
	short_upload_frame.data.RSRP_val = (u8)(upload_frame.data.UploadInfo.metinfo.RSRP_val/10);
	short_upload_frame.data.bat_voltage = PowerCheckValueSys();
	short_upload_frame.data.mode_snr = upload_frame.data.UploadInfo.metinfo.snr_val;
	short_upload_frame.data.mode_ecl = upload_frame.data.UploadInfo.metinfo.ecl_val;
	//上报时间
	RtccGetDateTime(&tempRtcc_time);
	short_upload_frame.data.UploadTime[0] = tempRtcc_time.s.minute;
	short_upload_frame.data.UploadTime[1] = tempRtcc_time.s.hour;
	short_upload_frame.data.UploadTime[2] = tempRtcc_time.s.day;
	short_upload_frame.data.UploadTime[3] = tempRtcc_time.s.month;
	short_upload_frame.data.UploadTime[4] = tempRtcc_time.s.year;
	//状态字
	short_upload_frame.data.StateBytes[0] = upload_frame.data.StateBytes[0];
	short_upload_frame.data.StateBytes[1] = upload_frame.data.StateBytes[1];
	short_upload_frame.data.StateBytes[2] = upload_frame.data.StateBytes[2];
	short_upload_frame.data.StateBytes[3] = 0x02;
	short_upload_frame.data.StateBytes[3] |= getUploadControlPara(UP_BAT_STATE);//剩余电池电量
	//软件版本
	short_upload_frame.data.soft_ver.datas_map.data1 =(FrameCheckParaApp(FramePara_ProgVersion)>>16)&0xFF;
	short_upload_frame.data.soft_ver.datas_map.data0 =(FrameCheckParaApp(FramePara_ProgVersion)>>24)&0xFF;

	short_upload_frame.data.temp_value.datas = (u16)TempCheckValueApp();
}

//启动补报数据组帧
void StartSupplementFrame(void)
{	
	supplement_data_frame_state = SUPPLEMENT_DATA_FRAME_S1;
}

u8 SupplementFrameMachineIfIdle(void)
{
	if(SUPPLEMENT_DATA_FRAME_S0 == supplement_data_frame_state)
	{
		return TASK_IDLE;
	}
	else
	{
		return TASK_BUSY;
	}
}

 //新补报数据组帧
void SupplementFrameMachine(void)  
{ 
    switch(supplement_data_frame_state)
    {
        case SUPPLEMENT_DATA_FRAME_S0:
		break;
  
		case(SUPPLEMENT_DATA_FRAME_S1):
        {    
		   supplement_cnt = 0;
		   temp_supplement_bit = Sys_data_object.P1.Supplemental_send_flag.datas;
		   supplement_data_frame_state = SUPPLEMENT_DATA_FRAME_S2;
		}		
		break;
  
	    case(SUPPLEMENT_DATA_FRAME_S2):
		{  
			if(EepIfIdle())
			{   
				//根据补报标志计算补报指针,日期从新往老排列
				Sys_data_object.P1.histroy_r = 0xFF;
				for (int i = Sys_data_object.P1.histroy_w; i >0; i--)
				{
					if (Sys_data_object.P1.Supplemental_send_flag.datas &(0x00000001<<i)) 
					{
						Sys_data_object.P1.histroy_r = i;
						break;
					}
				}
				if(Sys_data_object.P1.histroy_r == 0xFF)
				{
					for (int i = 31; i >(Sys_data_object.P1.histroy_w-1); i--)
					{
						if (Sys_data_object.P1.Supplemental_send_flag.datas &(0x00000001<<i)) 
						{
							Sys_data_object.P1.histroy_r = i;
							break;
						}
					}
				}
				if(supplement_cnt < SUPPLEMENT_DATA_MAX_NUM)
				{
					EepRead(FRAME_NBIot_HISTORY_READ_ADD+(Sys_data_object.P1.histroy_r*PACK_NUM_GPRS), &upload_frame.buf[0], PACK_NUM_GPRS, &s_eep_complete_ShenShui);
					supplement_data_frame_state = SUPPLEMENT_DATA_FRAME_S3;   
				}
				else
				{
					supplement_buf[0] = supplement_cnt;
					supplement_data_frame_state = SUPPLEMENT_DATA_FRAME_S0;
				} 
			}
		}	
		break;
  
		case(SUPPLEMENT_DATA_FRAME_S3):
		{  
			if(EepIfIdle())
			{ 
				supplement_buf[1+supplement_cnt*12+0] = upload_frame.data.UploadTime[3];
                supplement_buf[1+supplement_cnt*12+1] = upload_frame.data.UploadTime[4];
                supplement_buf[1+supplement_cnt*12+2] = upload_frame.data.UploadTime[5];
                supplement_buf[1+supplement_cnt*12+3] = upload_frame.data.StateBytes[1];
                supplement_buf[1+supplement_cnt*12+4] = upload_frame.data.CurrentFlow.datas_map.data0;
                supplement_buf[1+supplement_cnt*12+5] = upload_frame.data.CurrentFlow.datas_map.data1;
                supplement_buf[1+supplement_cnt*12+6] = upload_frame.data.CurrentFlow.datas_map.data2;
                supplement_buf[1+supplement_cnt*12+7] = upload_frame.data.CurrentFlow.datas_map.data3;
                supplement_buf[1+supplement_cnt*12+8] = 0; //大表预留4字节
                supplement_buf[1+supplement_cnt*12+9] = 0;
                supplement_buf[1+supplement_cnt*12+10] = 0;
                supplement_buf[1+supplement_cnt*12+11] = 0;
				supplement_cnt++;
				supplement_data_frame_state = SUPPLEMENT_DATA_FRAME_S2;    
			}
		}
		break;
  
        default:
        {
            supplement_data_frame_state = SUPPLEMENT_DATA_FRAME_S0;
        }
        break;
    }
}

void GetUploadDataResetState_SS(void)
{
	upload_frame_stat_ShenShui =UP_DEAL_S0;
	upload_task_complete_ShenShui = PTL_PACK_BUSY;
}
u8 GetUploadDataCheckIdle_SS(void)
{
	if(UP_DEAL_S0 == upload_frame_stat_ShenShui)
    {
        return TASK_IDLE;
    }
    else
    {
        return TASK_BUSY;
    }
}
u8 GetUploadDataCheckTask_SS(void)
{
	return upload_task_complete_ShenShui;
}

void GetUploadDataMachine_SS(u8 data_step, u8* p_send_buf, u16* p_send_len)
{
	switch(upload_frame_stat_ShenShui)
	{
		case UP_DEAL_S0:
		{	
			if(data_step == UPLOAD_FRAME_REQUEST_NEW)//请求新数据包
			{
				temp_upload_step = 0;
				upload_frame_retry_times = 0;//重发次数清0
				//计算要读取的上报数据存储位置
				if(Sys_data_object.P1.histroy_w >= PERIOD_DATA_NUM)
				{
					Sys_data_object.P1.histroy_w = 0;
				}
				if(0 == Sys_data_object.P1.histroy_w)
				{
					Sys_data_object.P1.histroy_r = PERIOD_DATA_NUM - 1;
				}
				else
				{
					Sys_data_object.P1.histroy_r = Sys_data_object.P1.histroy_w-1;
				}
				setStoreMsg(SS_StoreMsg4,TRUE);//更新当前包数据
				upload_frame_stat_ShenShui = UP_DEAL_S1;
			}
			else if(data_step == UPLOAD_FRAME_UPLOAD_OK)//发送成功
			{
				if(temp_upload_step == UP_DEAL_S1)//当前包发送成功
				{
					SystemClearUploadAgain();//重传清0
					setStoreMsg(SS_StoreMsg2, TRUE);//当前包发送成功,清除补报位置(当天失败的情况)
					upload_frame_retry_times = 0;
					 if(Sys_data_object.P1.func_cfg_bytes[7]==0x55)//需要补报
					 {
						if(((FrameCheckParaApp(FramePara_FuncCfg8_11)>>8)&0xFF) == 0xAA)//新补报规约
						{
							upload_frame_stat_ShenShui = UP_DEAL_S30;//新补报流程
						}
						else
						{
							upload_frame_stat_ShenShui = UP_DEAL_S10;//获取补报数据
						}
					 }
					 else//不需要补报
					 {
						NB_SS_Control_SetPara(NB_UploadFailTimes, 0);//清除失败标志
						upload_task_complete_ShenShui = PTL_PACK_FAIL;// 失败,上报结束 
						upload_frame_stat_ShenShui = UP_DEAL_S0;
					 }
				}
				else if(temp_upload_step == UP_DEAL_S10)//补报发送成功
				{
					setStoreMsg(SS_StoreMsg2, TRUE);//清除补报位置
					upload_frame_retry_times = 0;
					upload_frame_stat_ShenShui = UP_DEAL_S10;//获取补报数据
				}
				else if(temp_upload_step == UP_DEAL_S30)//新补报规约
				{
					upload_frame_retry_times = 0;
					upload_frame_stat_ShenShui = UP_DEAL_S33;
				}
			}
			else if(data_step == UPLOAD_FRAME_UPLOAD_RETRY)//请求重新发送
			{
				upload_frame_stat_ShenShui = UP_DEAL_S21;
				/*山西订单无应答修改( 改为ACK判断,不再使用下面两句)
				upload_task_complete_ShenShui = PTL_PACK_FAIL;
				upload_frame_stat_ShenShui = UP_DEAL_S0;*/
			}
			else if(data_step == UPLOAD_FRAME_ONLINE_FAIL) //联网失败
			{
				//计算要读取的上报数据存储位置(用于补报记录) 
				if(Sys_data_object.P1.histroy_w >= PERIOD_DATA_NUM)
				{
					Sys_data_object.P1.histroy_w = 0;
				}
				if(0 == Sys_data_object.P1.histroy_w)
				{
					Sys_data_object.P1.histroy_r = PERIOD_DATA_NUM - 1;
				}
				else
				{
					Sys_data_object.P1.histroy_r = Sys_data_object.P1.histroy_w-1;
				}
				if(	(UPLOAD_TYPE_TOUCH_KEY == GetEventUploadType()) || 
				(UPLOAD_TYPE_HAND_KEY == GetEventUploadType()))//按键上报
				{
					setStoreMsg(SS_StoreMsg1, TRUE);//标记补报位置							
				}		
				else if(UPLOAD_TYPE_FIX_TIME == GetEventUploadType())//定时上传
				{
					setStoreMsg(SS_StoreMsg1, TRUE);//标记补报位置
					SystemAddUploadAgain();//重传增加
				}
				else if(UPLOAD_TYPE_RETRY_AGAIN == GetEventUploadType())//重传
				{
					SystemAddUploadAgain();//重传增加
				}
				upload_task_complete_ShenShui = PTL_PACK_FAIL;
				upload_frame_stat_ShenShui = UP_DEAL_S0;
			}
			else 
			{
				upload_task_complete_ShenShui = PTL_PACK_FAIL;
				upload_frame_stat_ShenShui = UP_DEAL_S0;
			}
		}
		break;
		case UP_DEAL_S1://获取最新周期数据
		{
			if(step_save_supplt == SS_STATE_S0)//数据更新完成
			{
				if(EepIfIdle())
				{
					EepRead(FRAME_NBIot_HISTORY_READ_ADD+(Sys_data_object.P1.histroy_r*PACK_NUM_GPRS), &upload_frame.buf[0], PACK_NUM_GPRS, &s_eep_complete_ShenShui);
					temp_upload_step = UP_DEAL_S1;//记录当前状态
					upload_frame_stat_ShenShui = UP_DEAL_S2;
				}
			}
		}
		break;
		case UP_DEAL_S2:
		{
			if(EepIfIdle())
			{
				Date g_date_miji;
				u8* miji_buf;
				//当天数据包可上传密集采集数据
				if(RtccCheckMsgApp(RtccMsg_MiJiCaiJi))//查询密集采集是否在周期内且已采集完成
				{
					//密集采样开始时间
					upload_frame.data.MiJiCaiJiStartTime[0]= Sys_data_object.P1.report_start_time[6]; 
					upload_frame.data.MiJiCaiJiStartTime[1]= Sys_data_object.P1.report_start_time[7];
					upload_frame.data.MiJiCaiJiStartTime[2]= Sys_data_object.P1.report_start_time[8];
					upload_frame.data.MiJiCaiJiStartTime[3]= g_date_miji.s.day;
					upload_frame.data.MiJiCaiJiStartTime[4]= g_date_miji.s.month;
					upload_frame.data.MiJiCaiJiStartTime[5]= g_date_miji.s.year;
					upload_frame.data.MiJiCaiJiStartTime[6]= 0x20; 
					RtccGetDate(&g_date_miji); //周期上传时间作为密集采样开始时间   
					Gp30CheckMiJiDataApp(&miji_buf);
					for(int tmp_j = 0; tmp_j <192;tmp_j++)
					{
						upload_frame.data.MiJiCaiJiFlow[tmp_j] = *(miji_buf  + tmp_j);
					}
					upload_temp_len_ShenShui = PACK_NUM_GPRS_TOTAL;
				}
				else
				{
					upload_temp_len_ShenShui = PACK_NUM_GPRS;//无密集上报数据长度
				}
				upload_frame_stat_ShenShui = UP_DEAL_S20;
			}
		}
		break;
		case UP_DEAL_S10://获取补报数据
		{
			if(step_clear_flag == SS_STATE_S0)
			{
				if(EepIfIdle())
				{
					s_eep_complete_ShenShui = 0; 

					//根据补报标志计算补报指针,日期从新往老排列
					Sys_data_object.P1.histroy_r = 0xFF;
					for (int i = Sys_data_object.P1.histroy_w; i >=0; i--)
					{
						if(Sys_data_object.P1.Supplemental_send_flag.datas &(0x00000001<<i)) 
						{
							Sys_data_object.P1.histroy_r = i;
							break;
						}
					}
					if(Sys_data_object.P1.histroy_r == 0xFF)
					{
						for(int i = 31; i >(Sys_data_object.P1.histroy_w-1); i--)
						{
							if (Sys_data_object.P1.Supplemental_send_flag.datas &(0x00000001<<i)) 
							{
								Sys_data_object.P1.histroy_r = i;
								break;
							}
						}
					}
				
					if(Sys_data_object.P1.histroy_r == 0xFF)//不需补报
					{
						upload_task_complete_ShenShui = PTL_PACK_FAIL;// 失败
						upload_frame_stat_ShenShui = UP_DEAL_S0;
					} 
					else //需要补报
					{
						EepRead(FRAME_NBIot_HISTORY_READ_ADD+(Sys_data_object.P1.histroy_r*PACK_NUM_GPRS), &upload_frame.buf[0], PACK_NUM_GPRS, &s_eep_complete_ShenShui);
						upload_temp_len_ShenShui = PACK_NUM_GPRS;
						temp_upload_step = UP_DEAL_S10;//记录当前状态
						upload_frame_stat_ShenShui = UP_DEAL_S20;
					} 
				} 	
			}
		}
		break;
		
		case UP_DEAL_S20://组上传帧
		{
			if(EepIfIdle())
			{
				if((FrameCheckParaApp(FramePara_FuncCfg8_11)&0xFF) == 0xAA)//短帧上报
				{
					upload_struct_ShenShui.ack_ctrl = SHORT_UPLOAD_CTRL;
					upload_struct_ShenShui.DI0 = SHORT_UPLOAD_CTRL_ID0;
					upload_struct_ShenShui.DI1 = SHORT_UPLOAD_CTRL_ID1;
					upload_struct_ShenShui.sta = 0x03;//加密主动上传
					upload_serial_num_ShenShui++;
					upload_struct_ShenShui.serial_num0 = upload_serial_num_ShenShui&0xFF;
					upload_struct_ShenShui.serial_num1 = (upload_serial_num_ShenShui>>8)&0xFF; 
					upload_temp_len_ShenShui = STRU_DATA_LEN_IES_SHORT;
					Upload_RenewShortFrameData();//组短帧数据
					Frame_ack_IesBase(upload_struct_ShenShui, short_upload_frame.buf, upload_temp_len_ShenShui, p_send_buf, p_send_len);
				}
				else
				{
					upload_struct_ShenShui.ack_ctrl = SS_UPLOAD_CTRL;
					upload_struct_ShenShui.DI0 = SS_UPLOAD_CTRL_ID0;
					upload_struct_ShenShui.DI1 = SS_UPLOAD_CTRL_ID1;
					upload_struct_ShenShui.sta = 0x03;//加密主动上传
					upload_serial_num_ShenShui++;
					upload_struct_ShenShui.serial_num0 = upload_serial_num_ShenShui&0xFF;
					upload_struct_ShenShui.serial_num1 = (upload_serial_num_ShenShui>>8)&0xFF; 
					Frame_ack_IesBase(upload_struct_ShenShui, upload_frame.buf, upload_temp_len_ShenShui, p_send_buf, p_send_len);
				}
				upload_task_complete_ShenShui = PTL_PACK_SUCCESS;// 成功
				upload_frame_stat_ShenShui = UP_DEAL_S0;
			}
		}
		break;

		case UP_DEAL_S21://重试
		{
			if(upload_frame_retry_times < 1) //改为重发1次
			{
				upload_frame_retry_times++;
				if(temp_upload_step == UP_DEAL_S30)//新补报规约
				{
					upload_frame_stat_ShenShui = UP_DEAL_S32;
				}
				else
				{
					upload_frame_stat_ShenShui = UP_DEAL_S20;
				}
			} 
			else//重新发送失败3次的处理
			{
				if(temp_upload_step == UP_DEAL_S1)//当前包发送失败,判断是否补报
				{
					if(	(UPLOAD_TYPE_TOUCH_KEY == GetEventUploadType()) || 
					(UPLOAD_TYPE_HAND_KEY == GetEventUploadType()))//非阀门关闭上报或者重传
					{
						setStoreMsg(SS_StoreMsg1, TRUE);//temp_store_msg |= 0x02;//当前包进位置,且记录需要补报							
					}		
					else if(UPLOAD_TYPE_FIX_TIME == GetEventUploadType())//定时上传
					{
						setStoreMsg(SS_StoreMsg1, TRUE);//temp_store_msg |= 0x02;//当前包进位置,且记录需要补报
						SystemAddUploadAgain();//重传增加
					}
					else if(UPLOAD_TYPE_RETRY_AGAIN == GetEventUploadType())//重传
					{
						SystemAddUploadAgain();//重传增加
					}
				}
				upload_task_complete_ShenShui = PTL_PACK_FAIL;
				upload_frame_stat_ShenShui = UP_DEAL_S0;
			}
		}
		break;
		
		case UP_DEAL_S30://新补报规约,组帧初始化
		{
			if(EepIfIdle())
			{
				temp_upload_step = UP_DEAL_S30;
				StartSupplementFrame();
				upload_frame_stat_ShenShui = UP_DEAL_S31;
			}
		}
		break;
		case UP_DEAL_S31://等待读取补报数据完成
		{
			SupplementFrameMachine();
			if(TASK_IDLE == SupplementFrameMachineIfIdle())
			{
				if(supplement_cnt)//不为0，说明有补报的日冻结
				{
					upload_frame_stat_ShenShui = UP_DEAL_S32;
				}
				else
				{
					upload_task_complete_ShenShui = PTL_PACK_FAIL;
					upload_frame_stat_ShenShui = UP_DEAL_S0;
				}
			}	
		}
		break;
		case UP_DEAL_S32://组帧
		{
			if(EepIfIdle())
			{		 
				upload_struct_ShenShui.ack_ctrl = SUPP_UPLOAD_CTRL;
				upload_struct_ShenShui.DI0 = SUPP_UPLOAD_CTRL_ID0;
				upload_struct_ShenShui.DI1 = SUPP_UPLOAD_CTRL_ID1;
				upload_struct_ShenShui.sta = 0x03;//加密主动上传
				upload_serial_num_ShenShui++;
				upload_struct_ShenShui.serial_num0 = upload_serial_num_ShenShui&0xFF;
				upload_struct_ShenShui.serial_num1 = (upload_serial_num_ShenShui>>8)&0xFF; 
				upload_temp_len_ShenShui = supplement_cnt*12 + 1;
				Frame_ack_IesBase(upload_struct_ShenShui, supplement_buf, upload_temp_len_ShenShui, p_send_buf, p_send_len);
				upload_task_complete_ShenShui = PTL_PACK_SUCCESS;
				upload_frame_stat_ShenShui = UP_DEAL_S0;
			}
		}
		break;
		case UP_DEAL_S33://更新补报标志
		{
			if(EepIfIdle())
			{		
				Sys_data_object.P1.Supplemental_send_flag.datas = temp_supplement_bit;
				EepWrite(&Sys_data_object.P1.Supplemental_send_flag.datas_map.data0, FRAME_NBIot_BUBAO_FLAG, 6, &s_eep_complete_ShenShui);
				upload_frame_stat_ShenShui = UP_DEAL_S30;//继续获取补报数据
			}
		}
		break;
		
		default:
		{
			upload_task_complete_ShenShui = PTL_PACK_FAIL;
			upload_frame_stat_ShenShui = UP_DEAL_S0;
		}
		break;
	}
}
/*************************************************规约数据存储部分****************************/
//计算往前几天的日期和时间,不带周
static 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;
}
//补报标志存储
static void SS_Store_SuppleFlagMachine(void)
{
	switch(step_clear_flag)
	{
		case SS_STATE_S0:
		{
			if(getStoreMsg(SS_StoreMsg1) == TRUE)//保存补报标志
			{
				setStoreMsg(SS_StoreMsg1,FALSE);
				if((Sys_data_object.P1.func_cfg_bytes[0]>0)&&NbiotIfOnline())//厂区上报时间,如果有信号值,则不报故障
				{
					NB_SS_Control_SetPara(NB_UploadFailTimes, 0);
					step_clear_flag = SS_STATE_S0;
				}
				else
				{
					Sys_data_object.P1.Supplemental_send_flag.datas |=(0x00001<<Sys_data_object.P1.histroy_r);
					step_clear_flag = SS_STATE_S1;
				}
			}
			else if(getStoreMsg(SS_StoreMsg2) == TRUE)//清当前补报标志
			{
				setStoreMsg(SS_StoreMsg2,FALSE);
				Sys_data_object.P1.Supplemental_send_flag.datas &=(~(0x0001<<(Sys_data_object.P1.histroy_r)));
				step_clear_flag = SS_STATE_S1;
			}
			else if(getStoreMsg(SS_StoreMsg5) == TRUE)//清所有补报标志
			{
				setStoreMsg(SS_StoreMsg5,FALSE);
				Sys_data_object.P1.Supplemental_send_flag.datas = 0;
				step_clear_flag = SS_STATE_S1;
			}
		}
		break;
		case SS_STATE_S1:
		{
			if(EepIfIdle())
			{		
				EepWrite(&Sys_data_object.P1.Supplemental_send_flag.datas_map.data0, FRAME_NBIot_BUBAO_FLAG, 6, &s_eep_complete_ShenShui);
				step_clear_flag = SS_STATE_WAIT;
			}
		}
		break;
		case SS_STATE_WAIT:
		{
			if(EepIfIdle())
			{		
				step_clear_flag = SS_STATE_S0;
			}
		}
		break;

		default:
		{
			step_clear_flag = SS_STATE_S0;
		}
		break;
	}
}

//周期数据存储
static void SS_Store_CircleDataMachine(void)
{
	switch (step_save_supplt)//数据存储
	{
		case SS_STATE_S0://存储消息类型判断
		{
			if (RtccCheckMsgApp(RtccMsg_OtherProtl))
			{
				RtccClearMsgApp(RtccMsg_OtherProtl);
				RtccGetDateTime(&shenshui_rtcc_time);
				if((shenshui_rtcc_time.s.minute==0x00)||(shenshui_rtcc_time.s.minute==0x30))
				{
					FrameSetMsgApp(MsgSS_JianGeStore);//半小时间隔流量存储消息
				}
				if ((shenshui_rtcc_time.s.hour == 0)&&(shenshui_rtcc_time.s.minute==0x00))//冻结时间到，零点
				{
					RtccSetMsgApp(RtccMsg_DataSave);//0点更新冻结数据
					FrameSetMsgApp(MsgSS_HisIndexStore);//0点更新冻结数据指针
				}
			}	
			/**间隔流量和冻结数据存储：先0点存储冻结数据; 然后最后存储指针增加; 最后开始新的一轮间隔流量存储,期间判断写指针的有效性;*/
			if(RtccCheckMsgApp(RtccMsg_DataSave))//0点保存冻结数据
			{
				RtccClearMsgApp(RtccMsg_DataSave);
				step_save_supplt = SS_STATE_S10;
			}
			else if(FrameCheckMsgApp(MsgSS_HisIndexStore))//0点更新冻结数据指针
			{
				FrameClearMsgApp(MsgSS_HisIndexStore);
				step_save_supplt = SS_STATE_S2;
			}
			else if(FrameCheckMsgApp(MsgSS_JianGeStore)) //半小时间隔流量存储
			{
				FrameClearMsgApp(MsgSS_JianGeStore);
				step_save_supplt = SS_STATE_S20;
			}
			else if(getStoreMsg(SS_StoreMsg4) == TRUE)//更新当前数据
			{
				setStoreMsg(SS_StoreMsg4,FALSE);
				step_save_supplt = SS_STATE_S30;
			}
			else if(FrameCheckMsgApp(Msg_ClearHisOther))//清表数据
			{
				FrameClearMsgApp(Msg_ClearHisOther);
				step_save_supplt = SS_STATE_S40;
			} 
			break;
		}
		/*0点更新冻结数据*/
		case SS_STATE_S10:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				//(只)更新表计数据和表计状态
				Upload_RenewCurrentData(&store_frame);
			    EepWrite(store_frame.buf, (FRAME_NBIot_HISTORY_READ_ADD+(Sys_data_object.P1.histroy_w*PACK_NUM_GPRS)), CURRENT_DATA_INFO_LEN, &s_eep_complete_ShenShui);
				step_save_supplt = SS_STATE_S11;
			}
		    break;
		}
		case SS_STATE_S11://保存上报时间 
		{
			if(TASK_IDLE == EepIfIdle())
			{
			    EepWrite(&store_frame.data.UploadTime[0], (FRAME_NBIot_HISTORY_READ_ADD+(Sys_data_object.P1.histroy_w*PACK_NUM_GPRS)+UPLOAD_TIME_ADDR), 7, &s_eep_complete_ShenShui);
				step_save_supplt = SS_STATE_WAIT;
			}
		    break;
		}
		/*0点更新冻结数据指针*/
		case SS_STATE_S2:
		{
			if(TASK_IDLE == EepIfIdle())
			{			
				Sys_data_object.P1.histroy_w++;
				if(Sys_data_object.P1.histroy_w>=PERIOD_DATA_NUM) 
				{
					Sys_data_object.P1.histroy_w=0; 
				}				
				Sys_data_object.P1.Supplemental_send_flag.datas &=(~(0x0001<<(Sys_data_object.P1.histroy_w)));//清该位置补报标志
				EepWrite(&Sys_data_object.P1.Supplemental_send_flag.datas_map.data0, FRAME_NBIot_BUBAO_FLAG, 6, &s_eep_complete_ShenShui);
				step_save_supplt = SS_STATE_WAIT;
			}	
			break;					
		}
		/*间隔流量存储*/
		case SS_STATE_S20://半小时间隔流量时间判断:防止首次上电,日期变更
		{
			if(EepIfIdle())
			{		
				if(	(shenshui_rtcc_time.s.year  == store_frame.data.StepFlowStartTime[5])&&
					(shenshui_rtcc_time.s.month == store_frame.data.StepFlowStartTime[4])&&
					(shenshui_rtcc_time.s.day   == store_frame.data.StepFlowStartTime[3]))
				{
					step_save_supplt = SS_STATE_S22;
				}
				else
				{	
					EepRead(FRAME_NBIot_HISTORY_READ_ADD+(Sys_data_object.P1.histroy_w*PACK_NUM_GPRS)+STEP_FLOW_TIME_ADDR, &store_frame.data.StepFlowStartTime[0], 7, &s_eep_complete_ShenShui);
					step_save_supplt = SS_STATE_S21;
				}
			}
		}
		break;
		case SS_STATE_S21:
		{
			if(EepIfIdle())
			{		
				if(	(shenshui_rtcc_time.s.year  == store_frame.data.StepFlowStartTime[5])&&
					(shenshui_rtcc_time.s.month == store_frame.data.StepFlowStartTime[4])&&
					(shenshui_rtcc_time.s.day   == store_frame.data.StepFlowStartTime[3]))
				{
					step_save_supplt = SS_STATE_S22;
				}
				else//间隔流量无效,进行当前区域初始化
				{
					//获取表计数据和表计状态
					Upload_RenewCurrentData(&store_frame);
					//上报信号参数清除
					store_frame.data.UploadInfo.metinfo.ecl_val =0;
					store_frame.data.UploadInfo.metinfo.RSRP_val =0;
					store_frame.data.UploadInfo.metinfo.single_val =0;
					store_frame.data.UploadInfo.metinfo.snr_val =0;
					//保存间隔流量起始时间
					store_frame.data.StepFlowStartTime[0]= 0x00;
					store_frame.data.StepFlowStartTime[1]= 0x00;
					store_frame.data.StepFlowStartTime[2]= 0x00;
					store_frame.data.StepFlowStartTime[3]= shenshui_rtcc_time.s.day;
					store_frame.data.StepFlowStartTime[4]= shenshui_rtcc_time.s.month;
					store_frame.data.StepFlowStartTime[5]= shenshui_rtcc_time.s.year;        
					store_frame.data.StepFlowStartTime[6]= 0x20; 
					//间隔流量清除:2B(间隔流量)X2(次/h)X24h=96B
					for(int i = 0; i <96 ;i++)//间隔流量清除
					{
						store_frame.data.StepFlow[i] = 0;
					}
					if(Sys_data_object.P1.histroy_w>=PERIOD_DATA_NUM) 
					{
						Sys_data_object.P1.histroy_w=0; 
					}
					EepWrite(store_frame.buf, (FRAME_NBIot_HISTORY_READ_ADD+(Sys_data_object.P1.histroy_w*PACK_NUM_GPRS)), PACK_NUM_GPRS, &s_eep_complete_ShenShui);
				}
			}
		}
		break;
		case SS_STATE_S22://存储某半点间隔流量
		{
			if(EepIfIdle())
			{		
				u16 temp_step_flow_addr=0;
				//计算间隔流量
				new_jiange_total_flow.gp22_u32 = Gp30CheckDataApp(2);
				jiange_data.datas = (u16)((new_jiange_total_flow.gp22_float - old_jiange_total_flow.gp22_float)*10);
				old_jiange_total_flow.gp22_u32 = new_jiange_total_flow.gp22_u32;
			    //计算本时刻间隔流量存储位置
				temp_step_flow_addr = FRAME_NBIot_HISTORY_READ_ADD+(Sys_data_object.P1.histroy_w*PACK_NUM_GPRS)+STEP_FLOW_START_ADDR + 
				                      BCD2HEX(shenshui_rtcc_time.s.hour)*4 + (BCD2HEX(shenshui_rtcc_time.s.minute)/30)*2;
				EepWrite(&jiange_data.datas_map.data0, temp_step_flow_addr, 2, &s_eep_complete_ShenShui);
				step_save_supplt = SS_STATE_WAIT;
				//RtccAdjustDateTime(1,1800-10);//往后校时半小时//debug-----------------
			}
		}
		break;
		/*上报前更新并保存要读取的上报数据*/
		case SS_STATE_S30:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				EepRead(FRAME_NBIot_HISTORY_READ_ADD+(Sys_data_object.P1.histroy_r*PACK_NUM_GPRS), &store_frame.buf[0], PACK_NUM_GPRS, &s_eep_complete_ShenShui);
				step_save_supplt = SS_STATE_S31;
			}
		}
		break;
		case SS_STATE_S31://更新并保存上报时间
		{
			if(TASK_IDLE == EepIfIdle())
			{
				DateTime_noweek tempTime;
				tempTime = getBackDateTimeNoWeek(1);
				RtccGetDateTime(&shenshui_rtcc_time);
				//间隔流量有效性判断
				if(	(tempTime.s.year  != store_frame.data.StepFlowStartTime[5])||
					(tempTime.s.month != store_frame.data.StepFlowStartTime[4])||
					(tempTime.s.day   != store_frame.data.StepFlowStartTime[3]))
				{
					//保存间隔流量起始时间
					store_frame.data.StepFlowStartTime[0]= 0x00;
					store_frame.data.StepFlowStartTime[1]= 0x00;
					store_frame.data.StepFlowStartTime[2]= 0x00;
					store_frame.data.StepFlowStartTime[3]= shenshui_rtcc_time.s.day;//修改为当前时间,防止误判
					store_frame.data.StepFlowStartTime[4]= shenshui_rtcc_time.s.month;
					store_frame.data.StepFlowStartTime[5]= shenshui_rtcc_time.s.year;        
					store_frame.data.StepFlowStartTime[6]= 0x20; 
					//间隔流量清除:2B(间隔流量)X2(次/h)X24h=96B
					for(int i = 0; i <96 ;i++)//间隔流量清除
					{
						store_frame.data.StepFlow[i] = 0;
					}
				}
				Upload_RenewCurrentData(&store_frame);
			    EepWrite(store_frame.buf, (FRAME_NBIot_HISTORY_READ_ADD+ Sys_data_object.P1.histroy_r*PACK_NUM_GPRS), PACK_NUM_GPRS, &s_eep_complete_ShenShui);
				step_save_supplt = SS_STATE_WAIT;
			}
		}
		break;
		/*清表数据初始化读写指针*/
		case SS_STATE_S40:
		{
			if(TASK_IDLE == EepIfIdle())
			{			
				//间隔流量初始化
				new_jiange_total_flow.gp22_u32 = Gp30CheckDataApp(2);
				old_jiange_total_flow.gp22_u32 = new_jiange_total_flow.gp22_u32;
				for(int i=0;i<7;i++)
				{
					store_frame.data.StepFlowStartTime[i]= 0x00;
				}
				Sys_data_object.P1.histroy_w = 0;
				Sys_data_object.P1.histroy_r = 0;
				Sys_data_object.P1.Supplemental_send_flag.datas = 0;//补报也清0		
				EepWrite(&Sys_data_object.P1.Supplemental_send_flag.datas_map.data0, FRAME_NBIot_BUBAO_FLAG, 6, &s_eep_complete_ShenShui);
				step_save_supplt = SS_STATE_WAIT;
			}	
			break;					
		}
		case SS_STATE_WAIT:
		{
            if(TASK_IDLE == EepIfIdle())
            {
                 step_save_supplt = SS_STATE_S0;
            }
            break;
		}
        default:
        {
            break;		
        }			
    }
}
/*******************************************************************************
 * @fun_name:NB_SS_Control_GetPara
 * @brief   :    
 * @param[in] :读取上报控制相关参数
 * @param[out]: None
 * @retval    : None
 * @other     :
 ******************************************************************************/
u8 NB_SS_Control_GetPara(NB_SS_ControlParaTypeDef paraType)
{
    switch(paraType)
	{
	    case NB_HistroyReadIndex://历史数据的读指针
		{
			return Sys_data_object.P1.histroy_w;
		}
		case NB_UploadFailTimes://失败次数
		{
			u8 temp_fail_times=0;
			if(Sys_data_object.P3.ProductUpoadFailFlag)
			{
				temp_fail_times = Sys_data_object.P3.ProductUpoadFailFlag;
			}
			else
			{
				for(int i = 0; i < 32; i++)
				{
					if(((Sys_data_object.P1.Supplemental_send_flag.datas  >> i) & 0x00000001) == 0x00000001)
					{
						temp_fail_times += 1;
					}
				}
			}
			return temp_fail_times;
		}
		case NB_SS_FormatError://上报数据格式错误
		{
			return signal_val.SignalStruct.format_error;
		}
		default:
			break;
	}
	return 0;
}
/*******************************************************************************
 * @fun_name:NB_SS_Control_SetPara
 * @param[in] :设置上报控制相关参数
 ******************************************************************************/
void NB_SS_Control_SetPara(NB_SS_ControlParaTypeDef paraType, u8 data)
{
    switch(paraType)
	{
	    case NB_HistroyReadIndex://历史数据的读指针
		{
			Sys_data_object.P1.histroy_w = data;
			break;
		}
		case NB_UploadFailTimes://失败次数
		{
			SystemClearUploadAgain();
			Sys_data_object.P1.Supplemental_send_flag.datas = data;
			Sys_data_object.P3.ProductUpoadFailFlag = 0;
			setStoreMsg(SS_StoreMsg5,TRUE);//temp_store_msg |= 0x20;//保存EEPROM
			break;
		}
		case NB_SS_FormatError://上报数据格式错误
		{
			signal_val.SignalStruct.format_error = data;
			break;
		}
		default:
			break;
	}
}
//规约数据存储部分初始化
void DataStoreInit(void)
{
	Quanzhou_Store_InitApp();
	if(Sys_data_object.P1.histroy_w>=PERIOD_DATA_NUM) 
	{
		Sys_data_object.P1.histroy_w=0; 
	}
	if(Sys_data_object.P1.histroy_r>=PERIOD_DATA_NUM) 
	{
		Sys_data_object.P1.histroy_r=0; 
	}
	old_jiange_total_flow.gp22_u32  = Gp30CheckDataApp(GP30_APP_TOTAL_FLOW);//间隔流量计算初始化
}
//规约数据存储状态机
void DataStoreMachine(void)
{
	SS_Store_SuppleFlagMachine();//补报标志存储
	SS_Store_CircleDataMachine();//周期数据存储
	Quanzhou_Store_Machine();//冻结数据存储
}
//规约数据存储休眠判断
u8 DataStoreIfIdle(void)
{
    if((step_save_supplt == SS_STATE_S0)&&(step_clear_flag == SS_STATE_S0)&&(QuanzhouStore_IfSleep()==TRUE))
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

#endif

