#include "sys_config.h"
#if PRODUCT_GS
#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 "UWater_frame_func_ReplyNB.h"
#include "UWater_frame_func_ReplyGuangShui.h"

static u8 s_eep_complete_GuangShui=0;
static u8 task_complete_GuangShui = PTL_PACK_BUSY;
static u8 g_frame_temp_buf_GuangShui[UP_BUF_SIZE];	//数据部分缓冲区
static u16 g_frame_temp_len_GuangShui=0;
static DateTime GuangShui_rtcc_time;

static STRUCT_FRAME_IES frame_struct_GuangShui; //规约信息 
static u8 *new_message_data_rev_GuangShui; 
static u8 up_deal_stat_GuangShui = UP_DEAL_S0;//规约处理状态机

static STRUCT_FRAME_GS  frame_struct_JM; //广水规约信息
/*******主动上传数据相关变量*****************************************/
static STRUCT_FRAME_IES upload_struct_GuangShui; //规约信息 
static u8 upload_frame_stat_GuangShui=0; //主动上报组帧状态
static u8 upload_task_complete_GuangShui = PTL_PACK_FAIL;  
static u16 upload_temp_len_GuangShui=0;//主动上报数据长度	
static UNION_SIGNAL signal_val;
static u8 *new_message_data_rev_jm;  //接收数据指针
static DateTime rtcc_time_gs;
static u8 upload_packet_count=0; //上传数据包数
static u8 gs_upload_fail_times=0;//上报失败次数
static u8 upload_frame_retry_times=0; //数据重发次数
static u8 temp_upload_step=0; //数据上报状态
static u8 temp_store_msg=0; //数据存储消息

//积成规约上报缓存
static STRU_UPLOAD_FRAME upload_frame;//长帧上报数据
static STRU_UPLOAD_FRAME store_frame;//长帧存储数据,和上报帧缓存独立,防止上报期间存储数据
//广水规约上报缓存
static STRU_UP_FRAME_GS upload_frame_gs; 

//补报数据,间隔流量存储
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 u8 ls_save= FALSE;
static u8 ls_up= FALSE;
static u8 ls_save_cnt=0;
static STRU_UP_FRAME_GS ls_save_buf[6];//最长24小时,5分钟间隔存储
static RTCC_To_Float old_gs_total_flow;
static RTCC_To_Float new_gs_total_flow;
static u8 ls_upload_index=0;
static u8 ls_upload_flag=0;

//历史数据存储相关变量 
static UNION_GS_STORE_INDEX  his_save_para;
static u16 save_his_interval_cnt=0;
static TYP_STATE_SS his_save_step=SS_STATE_S0; 
static RTCC_To_Float his_save_flow_value;
static u16 his_save_addr = EEP_ADDR_HIS_DATA;
static u8 his_save_flow_buf[5];
//历史数据读取相关变量
static u16 his_read_start_count=0;
static u16 his_read_end_count=0;
static u8  his_read_count;
static u16 his_read_addr = EEP_ADDR_HIS_DATA;
static u8  his_read_flow_buf[5];
static u8  his_read_check;
static DateTime          his_read_start_time;
static UNION_2U8TOU16    his_read_period;
static RTCC_To_Float     his_read_new_flow;
static RTCC_To_Float     his_read_old_flow;
static SS_UNION_4U8TOU32 his_read_tempData;
static u8 his_read_tempBuf[6];
static long temp_diff_time;
//设置主动上报参数
static UNION_GS_UPLOAD_PARA linshi_jm; //临时存储和上报参数
static DateTime rtcc_linshi_jm; //记录设置临时上报的开始时间
static u8 linshi_data_eff=0; //记录临时存储和上报的标志

//数据格式转换
static void  u32HEXtoBCDData(u32 uiHEXdata,u8*ucBCDdata )
{
	u8 ucHEXdata[10] = {0};
	//2字节十六进制数转BCD码，2字节十六进制数最大值为0xffff=65535，所以转换出来的BCD码需要用3个字节装载
	ucHEXdata[1] = uiHEXdata%100;
	ucBCDdata[0] = (ucHEXdata[1]/10)*0x10 + (ucHEXdata[1]%10);
	uiHEXdata = uiHEXdata/100;
	ucHEXdata[2] = uiHEXdata%100;
	ucBCDdata[1] = (ucHEXdata[2]/10)*0x10 + (ucHEXdata[2]%10);
	uiHEXdata = uiHEXdata/100;
	ucHEXdata[3] = uiHEXdata%100;
	ucBCDdata[2] = (ucHEXdata[3]/10)*0x10 + (ucHEXdata[3]%10);

	uiHEXdata = uiHEXdata/100;
	ucHEXdata[4] = uiHEXdata%100;
	ucBCDdata[3] = (ucHEXdata[4]/10)*0x10 + (ucHEXdata[4]%10);		
	uiHEXdata = uiHEXdata/100;
	ucHEXdata[5] = uiHEXdata%100;
	ucBCDdata[4] = (ucHEXdata[5]/10)*0x10 + (ucHEXdata[5]%10);		
	uiHEXdata = uiHEXdata/100;
	ucHEXdata[6] = uiHEXdata%100;
	ucBCDdata[5] = (ucHEXdata[6]/10)*0x10 + (ucHEXdata[6]%10);				
}

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

    switch(up_deal_stat_GuangShui)
    {
        case UP_DEAL_S0:
        {
			if(TASK_IDLE == EepIfIdle())//读取历史数据
			{
				g_frame_temp_len_GuangShui =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((EEP_ADDR_CIRCLE_DATA +(temp*PACK_NUM_GPRS)), g_frame_temp_buf_GuangShui, 155, &s_eep_complete_GuangShui);		
				up_deal_stat_GuangShui  = UP_DEAL_S1;
			}
        }
        break;

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

//清除失败次数
static void frame_func_s3_NB_ClearFailTimes(void)
{   
	static u8 temp_clear_buf[4];
	if(TASK_IDLE == EepIfIdle())
	{
		g_frame_temp_len_GuangShui = 0;
		NB_SS_Control_SetPara(NB_SS_FormatError, 0);
		NB_SS_Control_SetPara(NB_UploadFailTimes, 0);
		temp_clear_buf[0]=0;
		temp_clear_buf[1]=0;
		temp_clear_buf[2]=0;
		temp_clear_buf[3]=0;
		EepWrite(temp_clear_buf, FRAME_NBIot_BUBAO_FLAG, 4, &s_eep_complete_GuangShui);
		task_complete_GuangShui = PTL_PACK_SUCCESS;
	}
}
/*************************************广水规约*******************************************************/
/**
函数名:gs_ack_upload_frame
说明:主动上报数据的应答
功能码 01（主动上报数据指令）
下行数据段格式:
平台当前时间 6 字节 BCD 码： YYMMDDHHNNSS 2020-01-11 12:15:30 200111121530
*/

void gs_ack_upload_frame(void)
{
	DateTime rtcc_time_rcvjm;
	g_frame_temp_len_GuangShui = 0;
	
	RtccGetDateTime(&rtcc_time_gs);	//获取当前时间
	rtcc_time_rcvjm.s.second = *(new_message_data_rev_jm + 5);	//second
	rtcc_time_rcvjm.s.minute = *(new_message_data_rev_jm + 4);	//MIN
	rtcc_time_rcvjm.s.hour = *(new_message_data_rev_jm + 3);		//Hour
	rtcc_time_rcvjm.s.day = *(new_message_data_rev_jm + 2);		//day
	rtcc_time_rcvjm.s.month = *(new_message_data_rev_jm + 1);	//mon
	rtcc_time_rcvjm.s.year  = *(new_message_data_rev_jm );	//year
	long diff_time = 0;
	diff_time = RtccCalculateDiffTime(&rtcc_time_rcvjm, &rtcc_time_gs);
	if(diff_time < 0)
	{
		diff_time = 0 - diff_time;
	}
	if(diff_time < HOUR_TO_SECOND)	//1h以内的应答判断为有效的
	{
		FrameSetMsgApp(MsgNbUplaod_AckOk);
	}	
	
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= rtcc_time_gs.s.year;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= rtcc_time_gs.s.month;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= rtcc_time_gs.s.day;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= rtcc_time_gs.s.hour;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= rtcc_time_gs.s.minute;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= rtcc_time_gs.s.second;
	
	up_deal_stat_GuangShui  = UP_DEAL_S0;
	task_complete_GuangShui = PTL_PACK_FAIL;
}


/**
函数名:gs_read_history_frame
说明:向设备查询某一时间节点的历史数据或 实时数据
下行数据格式: 
    指令标识:2字节高; 字节在前,设备应原码返回该值
    数据起始时间:6字节BCD, 200111121530 (当超始时间全部为0时,返回设备内存实时数据，并且上报数据段中的时间为设备当前时间)
    时间段个数:1字节
    时间间隔:2字节,高字节在前
上行数据格式:
    指令标识:指令执行失败则 MID 后全部字节置 FF、并且不用上发数据类型数据 段
    数据起始时间:
    时间段个数:
    时间间隔:
    数据类型1数据,数据类型2数据... ...:
*/
static void gs_read_history_frame(void)
{
	switch(up_deal_stat_GuangShui)
	{
		case UP_DEAL_S0:
		{
			g_frame_temp_len_GuangShui =11;//表数据开始位置
			RtccGetDateTime(&rtcc_time_gs);
			//数据起始时间
			his_read_start_time.s.year=new_message_data_rev_jm[2];
			his_read_start_time.s.month=new_message_data_rev_jm[3];
			his_read_start_time.s.day=new_message_data_rev_jm[4];
			his_read_start_time.s.hour=new_message_data_rev_jm[5]; 
			his_read_start_time.s.minute=new_message_data_rev_jm[6]; 
			his_read_start_time.s.second=new_message_data_rev_jm[7]; 			
			//时间段个数
			his_read_count=new_message_data_rev_jm[8]; 
			//时间间隔
			his_read_period.data_buf[0]=new_message_data_rev_jm[10];        
			his_read_period.data_buf[1]=new_message_data_rev_jm[9];
			
			//判断时间间隔是否有效
			if(his_read_period.datas != his_save_para.Data.Period.datas)
			{
				//返回异常
				up_deal_stat_GuangShui = UP_DEAL_S4;
			}
			else
			{
				//判断请求起始时间是否有效
				temp_diff_time = RtccCalculateDiffTime(&rtcc_time_gs, &his_read_start_time);
				if((temp_diff_time < (his_save_para.Data.Index_w.datas*his_save_para.Data.Period.datas*60))&&(temp_diff_time<(GS_HIS_STORE_MAX*his_save_para.Data.Period.datas*60)))
				{
					//判断请求时间段是否有效
					if((his_read_count*his_save_para.Data.Period.datas*60)<temp_diff_time)
					{
						his_read_end_count = 0;
						his_read_start_count = his_save_para.Data.Index_w.datas - (temp_diff_time/60)/his_save_para.Data.Period.datas;
						up_deal_stat_GuangShui = UP_DEAL_S1;
					}
					else//返回异常
					{
						up_deal_stat_GuangShui = UP_DEAL_S4;
					}
				}
				else//返回异常
				{
					up_deal_stat_GuangShui = UP_DEAL_S4;
				}
			}
		}
		break;
		case UP_DEAL_S1://数据读取
		{
			if(TASK_IDLE == EepIfIdle())
			{
				his_read_addr = EEP_ADDR_HIS_DATA+(((his_read_start_count+his_read_end_count)%GS_HIS_STORE_MAX)*5);
				EepRead(his_read_addr, &his_read_flow_buf[0], 5, &s_eep_complete_GuangShui);
				up_deal_stat_GuangShui = UP_DEAL_S2;
			}
		}
		break;
		case UP_DEAL_S2://数据处理:EE_BUF->FLOW_BUF->FLOW_FLOAT->FLOW_U8BUF->BCD
		{
			if(TASK_IDLE == EepIfIdle())
			{
				his_read_check = his_read_flow_buf[0]+his_read_flow_buf[1]+his_read_flow_buf[2]+his_read_flow_buf[3];
				if(his_read_flow_buf[4] == his_read_check)
				{
					his_read_new_flow.buf[0] = his_read_flow_buf[0];
					his_read_new_flow.buf[1] = his_read_flow_buf[1];
					his_read_new_flow.buf[2] = his_read_flow_buf[2];
					his_read_new_flow.buf[3] = his_read_flow_buf[3];
					if(his_read_end_count==0)
					{
						his_read_tempData.datas = (u32)(his_read_new_flow.gp22_float*10);
						u32HEXtoBCDData(his_read_tempData.datas, &his_read_tempBuf[0]);
						g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= 0x20;
						g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= his_read_tempBuf[5];
						g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= his_read_tempBuf[4];
						g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= his_read_tempBuf[3];
						g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= his_read_tempBuf[2];
						g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= his_read_tempBuf[1];
						g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= his_read_tempBuf[0];
						//更新老数据
						his_read_old_flow.gp22_float = his_read_new_flow.gp22_float;
					}
					else
					{
						his_read_tempData.datas = (u16)((his_read_new_flow.gp22_float - his_read_old_flow.gp22_float)*10);
						u32HEXtoBCDData(his_read_tempData.datas, &his_read_tempBuf[0]);
						g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= his_read_tempBuf[2];
						g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= his_read_tempBuf[1];
						g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= his_read_tempBuf[0];
						//更新老数据
						his_read_old_flow.gp22_float = his_read_new_flow.gp22_float;
					}
					if((his_read_end_count < (his_read_count-1))&&(his_read_end_count<47))//继续读下一条
					{
						his_read_end_count++;
						up_deal_stat_GuangShui = UP_DEAL_S1;
					}
					else//读取完成
					{
						up_deal_stat_GuangShui  = UP_DEAL_S3;
					}
				}
				else//错误返回
				{
					if(his_read_end_count == 0)//返回异常
					{
						up_deal_stat_GuangShui = UP_DEAL_S4;
					}
					else
					{
						up_deal_stat_GuangShui = UP_DEAL_S3;
					}
				}
			}
		}
		break;
		case UP_DEAL_S3:
		{
			//MID
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI0;
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI1;
			//应答:数据起始时间
			g_frame_temp_buf_GuangShui[2]=his_read_start_time.s.year;
			g_frame_temp_buf_GuangShui[3]=his_read_start_time.s.month;
			g_frame_temp_buf_GuangShui[4]=his_read_start_time.s.day;
			g_frame_temp_buf_GuangShui[5]=his_read_start_time.s.hour;
			g_frame_temp_buf_GuangShui[6]=his_read_start_time.s.minute;
			g_frame_temp_buf_GuangShui[7]=his_read_start_time.s.second;
			//应答:时间段个数
			g_frame_temp_buf_GuangShui[8]=his_read_end_count;//实际读取的个数
			//应答:时间间隔
			g_frame_temp_buf_GuangShui[9]=his_read_period.data_buf[1];
			g_frame_temp_buf_GuangShui[10]=his_read_period.data_buf[0];
			
			up_deal_stat_GuangShui  = UP_DEAL_S0;
			task_complete_GuangShui = PTL_PACK_SUCCESS;
		}
		break;
		case UP_DEAL_S4://异常返回
		{
			//指令执行失败则 MID 后全部字节置 FF、并且不用上发数据类型数据段
			//MID
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI0;
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI1;
			//应答:数据起始时间
			g_frame_temp_buf_GuangShui[2]=0xFF;
			g_frame_temp_buf_GuangShui[3]=0xFF;
			g_frame_temp_buf_GuangShui[4]=0xFF;
			g_frame_temp_buf_GuangShui[5]=0xFF;
			g_frame_temp_buf_GuangShui[6]=0xFF;
			g_frame_temp_buf_GuangShui[7]=0xFF;
			//应答:时间段个数
			g_frame_temp_buf_GuangShui[8]=0xFF;
			//应答:时间间隔
			g_frame_temp_buf_GuangShui[9]=0xFF;
			g_frame_temp_buf_GuangShui[10]=0xFF;
			
			up_deal_stat_GuangShui  = UP_DEAL_S0;
			task_complete_GuangShui = PTL_PACK_SUCCESS;
		}
		break;
		default:
		{
			up_deal_stat_GuangShui = UP_DEAL_S0;
		}
		break;
	}
}
//设置终端时钟
void jm_set_time()
{
	switch(up_deal_stat_GuangShui)
	{
		case UP_DEAL_S0:
		{
			g_frame_temp_len_GuangShui = 0;
			rtcc_time_gs.s.year  = *(new_message_data_rev_jm + 2);
			rtcc_time_gs.s.month = *(new_message_data_rev_jm + 3);
			rtcc_time_gs.s.day   = *(new_message_data_rev_jm + 4);
			rtcc_time_gs.s.hour  = *(new_message_data_rev_jm + 5);
			rtcc_time_gs.s.minute= *(new_message_data_rev_jm + 6);
			rtcc_time_gs.s.second= *(new_message_data_rev_jm + 7);
			if(SUCCESS_IES == Rtcc_Check_DateTime(&rtcc_time_gs))//日期合法
			{
				RtccSetDateTime(&rtcc_time_gs);
				up_deal_stat_GuangShui  = UP_DEAL_S1;
			}
			else
			{
				up_deal_stat_GuangShui  = UP_DEAL_S2;
			}
		}
		break;
		case UP_DEAL_S1:
		{
			RtccGetDateTime(&rtcc_time_gs);//获取当前时间	
			//MID
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI0;
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI1;
			//time
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = rtcc_time_gs.s.year;
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = rtcc_time_gs.s.month; 
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = rtcc_time_gs.s.day;
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = rtcc_time_gs.s.hour;
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = rtcc_time_gs.s.minute;					
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = rtcc_time_gs.s.second;
			up_deal_stat_GuangShui  = UP_DEAL_S0;
			task_complete_GuangShui = PTL_PACK_SUCCESS;
		}
		break;
		case UP_DEAL_S2:
		{
			//MID
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI0;
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI1;
			//time
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0xFF;
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0xFF; 
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0xFF;
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0xFF;
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0xFF;					
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0xFF;
			up_deal_stat_GuangShui  = UP_DEAL_S0;
			task_complete_GuangShui = PTL_PACK_SUCCESS;
		}
		break;
		default:
		{
			up_deal_stat_GuangShui  = UP_DEAL_S0;
			task_complete_GuangShui = PTL_PACK_FAIL;
		}
		break;
	}
}
//设置历史数据存盘间隔
void jm_set_save_interval()
{
	if (TASK_IDLE == EepIfIdle()) 
	{
		g_frame_temp_len_GuangShui =0;
		//MID
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI0;
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI1;
		//间隔
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = *(new_message_data_rev_jm + 2);
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = *(new_message_data_rev_jm + 3);
		

		his_save_para.Data.Period.data_buf[1]=*(new_message_data_rev_jm + 2);     
		his_save_para.Data.Period.data_buf[0]=*(new_message_data_rev_jm + 3);
		
		EepWrite(&his_save_para.data_buf[0], EEP_ADDR_HIS_INDEX, 6, &s_eep_complete_GuangShui);	
		
		up_deal_stat_GuangShui  = UP_DEAL_S0;
		task_complete_GuangShui = PTL_PACK_SUCCESS;	
	}
}
//设置主动上报参数
void jm_up_set() 
{
	if (TASK_IDLE == EepIfIdle())
	{
		g_frame_temp_len_GuangShui =0;
		//MID
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI0;
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI1;
		//开始时刻
		linshi_jm.Data.start_time_h = *(new_message_data_rev_jm+2);          
		linshi_jm.Data.start_time_m=*(new_message_data_rev_jm+3);
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = *(new_message_data_rev_jm + 2);
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = *(new_message_data_rev_jm + 3);
		//上发间隔
		linshi_jm.Data.up_interval.data_buf[1]=*(new_message_data_rev_jm+4);  ////上发间隔
		linshi_jm.Data.up_interval.data_buf[0]=*(new_message_data_rev_jm+5);			
		if(linshi_jm.Data.up_interval.datas <=60) //临时上报间隔不低于1小时
		{
			linshi_jm.Data.up_interval.datas = 60;
		}
		else if(linshi_jm.Data.up_interval.datas >= 60*24) //临时上报间隔不大于24
		{
			linshi_jm.Data.up_interval.datas =60*24;				
		}	
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = linshi_jm.Data.up_interval.data_buf[1];
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = linshi_jm.Data.up_interval.data_buf[0];
		//数据间隔
		linshi_jm.Data.data_interval.data_buf[1]=*(new_message_data_rev_jm+6);
		linshi_jm.Data.data_interval.data_buf[0]=*(new_message_data_rev_jm+7);
		if(linshi_jm.Data.data_interval.datas<=5) //临时存储间隔不低于5分钟
		{
			linshi_jm.Data.data_interval.datas=5;
		}
		else if(linshi_jm.Data.data_interval.datas>=60*12)
		{
			linshi_jm.Data.data_interval.datas=60*12;
		}	
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = linshi_jm.Data.data_interval.data_buf[1];
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = linshi_jm.Data.data_interval.data_buf[0];
		//离散值
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0;
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0;
		//生效时长
		linshi_jm.Data.effective_time.data_buf[1]=*(new_message_data_rev_jm+10); 
		linshi_jm.Data.effective_time.data_buf[0]=*(new_message_data_rev_jm+11);
		if(linshi_jm.Data.effective_time.datas >=24*60)
		{
			linshi_jm.Data.effective_time.datas =24*60;
		}
		if(linshi_jm.Data.effective_time.datas==0)
		{
			linshi_jm.Data.effective_time.datas=24*60;		
		}
		RtccGetDateTime(&rtcc_linshi_jm);				//生效时间直接赋值给当前时间变量。
		rtcc_linshi_jm.s.hour = linshi_jm.Data.start_time_h;
		rtcc_linshi_jm.s.minute = linshi_jm.Data.start_time_m;
		
		EepWrite(&linshi_jm.data_buf[0], EEP_ADDR_TEMP_STORE_UPLOAD, 8, &s_eep_complete_GuangShui);	 
		
		linshi_data_eff=1;//启动临时存储和上报标志
		ls_save_cnt=0;//上报条数清0
		ls_upload_index=0;//临时上报指针
		up_deal_stat_GuangShui  = UP_DEAL_S0;
		task_complete_GuangShui = PTL_PACK_SUCCESS;	
	}			
}

//读取临时数据存储和上报间隔
void jm_Up_read() 
{
    g_frame_temp_len_GuangShui =0;
	//MID
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI0;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI1;
    g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= linshi_jm.Data.start_time_h;
    g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= linshi_jm.Data.start_time_m;
    g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= linshi_jm.Data.up_interval.data_buf[1];
    g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= linshi_jm.Data.up_interval.data_buf[0];
    g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= linshi_jm.Data.data_interval.data_buf[1];
    g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= linshi_jm.Data.data_interval.data_buf[0];
    g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= 0;
    g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++]= 0;
	up_deal_stat_GuangShui  = UP_DEAL_S0;
	task_complete_GuangShui = PTL_PACK_SUCCESS;	
}

//判断临时存储间隔和临时上报间隔
u8 get_lset_judg(u8*save,u8*up)  //判断是否执行临时设置
{
	long 	diff_time;
	static u32 time_min;	
	
	RtccGetDateTime(&rtcc_time_gs);
	diff_time = RtccCalculateDiffTime(&rtcc_time_gs,&rtcc_linshi_jm)/(60); //计算时间差	
	if(diff_time>24*60)
	{
		linshi_data_eff=0;
	}
	if((diff_time <linshi_jm.Data.effective_time.datas)&&linshi_data_eff&&(diff_time>0))
	{   
		time_min= ((rtcc_time_gs.s.hour >> 4) * 10 + (rtcc_time_gs.s.hour & 0x0F))*60;
		time_min+= (rtcc_time_gs.s.minute >> 4) * 10 + (rtcc_time_gs.s.minute & 0x0F);
		if(linshi_jm.Data.up_interval.datas<24*60)
		{
			if((time_min%linshi_jm.Data.up_interval.datas)==0)
			{
				*up=TRUE;
			}
			else
			{
				*up=FALSE;
			}
		}
		else
		{
			*up=FALSE;
		}			
		if(time_min%linshi_jm.Data.data_interval.datas==0)
		{
			*save=TRUE;
		}
		else
		{
			*save=FALSE;
		}	
		return TRUE;
	}
	else 
	{		  
		*save=FALSE;
		*up=FALSE;
		return FALSE;
	}
}
//设置数据中心地址
void jm_set_center_add()
{
	static u16 add_len;
	u16 num_ip=0;
	u8 *mack[5];
	if(TASK_IDLE == EepIfIdle())//存储数据到EEPROM
	{
		g_frame_temp_len_GuangShui =0;
		//MID
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI0;
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI1;
		
		add_len=frame_struct_JM.data_len.to_uint16-15-2; //下发的数据总长度去除mid
		for(u8 l=2; l<add_len; l++)
		{
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = *(new_message_data_rev_jm+l);
			if(((char)(*(new_message_data_rev_jm+l))==':'))
			{
				*(new_message_data_rev_jm+l)='.';
			}
			if(((char)(*(new_message_data_rev_jm+l))=='.')&&(num_ip<=3))
			{
				mack[0]=new_message_data_rev_jm+1;
				mack[++num_ip]=(new_message_data_rev_jm+l);
				if(num_ip==4)
				{
					for(u8 n=0; n<4; n++)
					{
						if(mack[n+1]-mack[n]==2)
						{
							Sys_data_object.P1.server_ip[n]=*(mack[n]+1)-0x30;
						}
						else if(mack[n+1]-mack[n]==3)
						{
							Sys_data_object.P1.server_ip[n]=(*(mack[n]+1)-0x30)*10+(*(mack[n]+2)-0x30);
						}
						else if(mack[n+1]-mack[n]==4)
						{
							Sys_data_object.P1.server_ip[n]=(*(mack[n]+1)-0x30)*100+(*(mack[n]+2)-0x30)*10+(*(mack[n]+3)-0x30);
						}
					}
					if(new_message_data_rev_jm+2+add_len-mack[4]==3)	    //port	为1位  多0位
					{
						Sys_data_object.P1.server_port.datas=*(mack[4]+1)-0x30;
					}
					else if(new_message_data_rev_jm+2+add_len-mack[4]==4)	//port	为两位
					{
						Sys_data_object.P1.server_port.datas=(*(mack[4]+1)-0x30)*10+*(mack[4]+2)-0x30;
					}
					else if(new_message_data_rev_jm+2+add_len-mack[4]==5)	//port	为3位
					{
						Sys_data_object.P1.server_port.datas=(*(mack[4]+1)-0x30)*100+(*(mack[4]+2)-0x30)*10+(*(mack[4]+3)-0x30);
					}
					else if(new_message_data_rev_jm+2+add_len-mack[4]==6)	//port	为4位
					{
						Sys_data_object.P1.server_port.datas=(*(mack[4]+1)-0x30)*1000+(*(mack[4]+2)-0x30)*100+(*(mack[4]+3)-0x30)*10+(*(mack[4]+4)-0x30);
					}
					else if(new_message_data_rev_jm+2+add_len-mack[4]==7)	//port	为5位
					{
						Sys_data_object.P1.server_port.datas=(*(mack[4]+1)-0x30)*10000
						+(*(mack[4]+2)-0x30)*1000+(*(mack[4]+3)-0x30)*100+(*(mack[4]+4)-0x30)*10+(*(mack[4]+5)-0x30);
					}
				}
			}
		}
		EepWrite(&Sys_data_object.P1.server_ip[0], FRAME_NBIot_IP, 6, &s_eep_complete_GuangShui);
		up_deal_stat_GuangShui  = UP_DEAL_S0;
		task_complete_GuangShui = PTL_PACK_SUCCESS;
	}
}
//设置工作模式,只给回帧,不实际应用
void jm_set_mode()   
{
	g_frame_temp_len_GuangShui =0;
	//MID
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI0;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI1;
	//工作模式和生效时长
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0x00;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0x00;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0x00;
	up_deal_stat_GuangShui  = UP_DEAL_S0;
	task_complete_GuangShui = PTL_PACK_SUCCESS;
}
//设置设备地址,(设置设备地址指令用于非 NB 水表应用场景，NB 水表表码通常不允许修 改，所以 NB 水表不需实现该指令)
void jm_set_add_frame()  //回帧
{
	g_frame_temp_len_GuangShui =0;
	//MID
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI0;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI1;
	//设备地址
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0xFF;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0xFF;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0xFF;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0xFF;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0xFF;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0xFF;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0xFF;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0xFF;
	up_deal_stat_GuangShui  = UP_DEAL_S0;
	task_complete_GuangShui = PTL_PACK_SUCCESS;
}
//设置加密标志,无效
void jm_encrypted_data_frame()
{
    g_frame_temp_len_GuangShui =0;
	//MID
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI0;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI1;
	//加密标志和密钥
	for(int i=0;i<17;i++)
	{
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = *(new_message_data_rev_jm+2+i);
	}
	up_deal_stat_GuangShui  = UP_DEAL_S0;
	task_complete_GuangShui = PTL_PACK_SUCCESS;
}
//查询历史数据存盘间隔
void jm_read_interval_frame()
{
	g_frame_temp_len_GuangShui =0;
	//MID
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI0;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI1;
	//间隔
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = his_save_para.Data.Period.data_buf[1];
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = his_save_para.Data.Period.data_buf[0];

	up_deal_stat_GuangShui  = UP_DEAL_S0;
	task_complete_GuangShui = PTL_PACK_SUCCESS;
}
//查询数据中心地址
void jm_read_add_frame()
{
	g_frame_temp_len_GuangShui =0;
	//MID
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI0;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = frame_struct_GuangShui.DI1;
	for (int i = 0; i < 4; i++)
	{
		if (Sys_data_object.P1.server_ip[i] < 10)
		{
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = Sys_data_object.P1.server_ip[i] + 0x30;
		}
		else if (Sys_data_object.P1.server_ip[i] < 100)
		{
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = (Sys_data_object.P1.server_ip[i] / 10) + 0x30;
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = (Sys_data_object.P1.server_ip[i] % 10) + 0x30;
		}
		else
		{
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = (Sys_data_object.P1.server_ip[i] / 100) + 0x30;
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = (Sys_data_object.P1.server_ip[i] / 10) % 10 + 0x30;
			g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = (Sys_data_object.P1.server_ip[i] % 10) + 0x30;
		}
		g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = '.';
	}
	g_frame_temp_len_GuangShui--;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = ':';
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = (Sys_data_object.P1.server_port.datas / 10000) + 0x30;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = (Sys_data_object.P1.server_port.datas % 10000) / 1000 + 0x30;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = (Sys_data_object.P1.server_port.datas % 1000) / 100 + 0x30;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = (Sys_data_object.P1.server_port.datas % 100) / 10 + 0x30;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = (Sys_data_object.P1.server_port.datas % 10) + 0x30;
	g_frame_temp_buf_GuangShui[g_frame_temp_len_GuangShui++] = 0x0;
	
	up_deal_stat_GuangShui  = UP_DEAL_S0;
	task_complete_GuangShui = PTL_PACK_SUCCESS;
}
/****************************************************************************************************/
//解广水规约
u8 crack_frame_GS(u16 frame_lenth,u8 *frame_buffer)
{
    u16 i;
    u16 j;
    u16 endloc = 0;
    u16 framelen = 0;
    if (frame_lenth > FRAME_LEN_MAX) //检查待解帧长度是否大于数据缓冲区长度
    {
        return (ERROR_IES);
    }
    if (frame_lenth < FRAME_LEN_MIN) //检查待解帧长度是否小于最小协议帧长度
    {
        return (ERROR_IES);
    }
    for (i = 0; i < frame_lenth; i++)
    {
        if (FRAME_HEAD_JM == *frame_buffer) //找到帧头
        {
            frame_struct_JM.locn = i;									//记住帧头在Receive_buf位置
            frame_struct_JM.len.to_uint8[1] = *(frame_buffer + 1); 	//数据区长度
            frame_struct_JM.len.to_uint8[0] = *(frame_buffer + 2);

            framelen = frame_struct_JM.len.to_uint16+2; 				//帧长度 （现在下发的不包含头尾）
            if (frame_struct_JM.len.to_uint16 > (frame_lenth - i))		//防止串口通讯规约恰巧绕过防护后造成数组越界复位
            {
                return (ERROR_IES);
            }
            endloc = framelen - 1;
						
            if ((FRAME_END_JM == frame_buffer[endloc]) && (endloc <= frame_lenth)) 		//结束符正确，且帧长有效
            {
                frame_struct_JM.crc = 0;
                for (j = 0; j < (framelen - 2); j++)
                {
                    frame_struct_JM.crc = (u8)(frame_struct_JM.crc + frame_buffer[j]); //判校验和
                }
                if (frame_struct_JM.crc == frame_buffer[endloc - 1])   //j校验通过
                {
                    frame_struct_JM.frame_ctrl = *(frame_buffer + 13);  				//获取功能码
                    if((*(frame_buffer + 3) & 0x40)	)//加密   在帧标识中，需要判断此位
                    {
                    }
                    else
                    {
                        frame_struct_JM.data_len.to_uint8[1] = *(frame_buffer + 1);		//总数据域长度
                        frame_struct_JM.data_len.to_uint8[0] = *(frame_buffer + 2);
                       /* for (j = 0; j < (frame_struct_JM.data_len.to_uint16-15); j++)//存入新缓冲区
                        {
                            jm_rcv_buf[j] = *(frame_buffer + 15 + j);
                        }*/
                        new_message_data_rev_jm = frame_buffer + frame_struct_JM.locn + 15;	
						
						frame_struct_GuangShui.ctrl = frame_struct_JM.frame_ctrl;
						frame_struct_GuangShui.DI0 = *(new_message_data_rev_jm + 0);//MID
						frame_struct_GuangShui.DI1 = *(new_message_data_rev_jm + 1);//MID
                    }
                    return (SUCCESS_IES);
                }
            }
        }
        frame_buffer++;
    }
    return (ERROR_IES);
}
//解深水规约
u8 crack_frame_GuangShui(u16 frame_lenth,u8 *frame_buffer)
{
	u8 temp_res = crack_frame_IES_Base(frame_lenth, frame_buffer, &frame_struct_GuangShui);
	if(temp_res == SUCCESS_IES)
	{		
		new_message_data_rev_GuangShui = frame_buffer + frame_struct_GuangShui.head_site + 16;								
		return  SUCCESS_IES;
	}
	else if(crack_frame_GS(frame_lenth, frame_buffer) == SUCCESS_IES)//广水规约
	{
		return  SUCCESS_IES;
	}
	return ERROR_IES;
}

//深水规约处理
u8 up_deal_code_GuangShui(void)
{
    task_complete_GuangShui = PTL_PACK_BUSY;
    switch(frame_struct_GuangShui.ctrl)
    {
		case SS_CLEAR_FAIL_TIMES_CTRL:
		{
			frame_func_s3_NB_ClearFailTimes();
			frame_struct_GuangShui.ack_ctrl = SS_ACK_CLEAR_FAIL_TIMES_CTRL;
		}
		break;
		case SS_READ_HIS_CTRL:
		{
			switch(frame_struct_GuangShui.DI1)
			{
				case SS_READ_HIS_CTRL_ID1://深水规约读历史数据
				{
					frame_func_reply_NB_ReadHistoryData();
					frame_struct_GuangShui.ack_ctrl = SS_ACK_READ_HIS_CTRL;
				}
				break;
				default:
				{
					up_deal_stat_GuangShui  = UP_DEAL_S0;
					task_complete_GuangShui = PTL_PACK_FAIL; 
				}
			}
		}
		break; 
		//广水规约
		case READ_MET_CTRL_01: 	//回复处理
		{					
			gs_ack_upload_frame();
			frame_struct_GuangShui.ack_ctrl = READ_MET_CTRL_01;
		}	
		break;	
		
		case READ_MET_CTRL_03: 	//报警回复处理
		{
			gs_ack_upload_frame();
			frame_struct_GuangShui.ack_ctrl = READ_MET_CTRL_03;
		}	
		break;
		
		case READ_MET_CTRL_02: 	//数据查询
		{
			gs_read_history_frame();
			frame_struct_GuangShui.ack_ctrl = READ_MET_CTRL_02;
		}
		break;

		case READ_MET_CTRL_04:  //设置时钟
		{
			jm_set_time();
			frame_struct_GuangShui.ack_ctrl = READ_MET_CTRL_04;
		}
		break;

		case READ_MET_CTRL_05: 	//设置历史存盘间隔
		{
			jm_set_save_interval();
			frame_struct_GuangShui.ack_ctrl = READ_MET_CTRL_05;
		}
		break;

		case READ_MET_CTRL_06:  //设置上报参数
		{
			jm_up_set();
			frame_struct_GuangShui.ack_ctrl = READ_MET_CTRL_06;
		}
		break;

		case READ_MET_CTRL_07:		//设置数据中心地址
		{
			jm_set_center_add();
			frame_struct_GuangShui.ack_ctrl = READ_MET_CTRL_07;
		}
		break;

		case READ_MET_CTRL_08:	//工作模式
		{
			jm_set_mode();
			frame_struct_GuangShui.ack_ctrl = READ_MET_CTRL_08;
		}
		break;

		case READ_MET_CTRL_09:			//设置设备地址
		{
			jm_set_add_frame();
			frame_struct_GuangShui.ack_ctrl = READ_MET_CTRL_09;
		}
		break;

		case READ_MET_CTRL_010:		//设置加密标志
		{
			jm_encrypted_data_frame();
			frame_struct_GuangShui.ack_ctrl = READ_MET_CTRL_010;
		}
		break;

		case READ_MET_CTRL_011:		//查询历史存盘间隔
		{
			jm_read_interval_frame();
			frame_struct_GuangShui.ack_ctrl = READ_MET_CTRL_011;
		}
		break;
		
		case READ_MET_CTRL_012:		//查询主动上报参数
		{
			jm_Up_read();
			frame_struct_GuangShui.ack_ctrl = READ_MET_CTRL_012;
		}
		break;
		
		case READ_MET_CTRL_013:		//查询数据中心地址
		{
			jm_read_add_frame();
			frame_struct_GuangShui.ack_ctrl = READ_MET_CTRL_013;
		}
		break;
        default:
        {     
			up_deal_stat_GuangShui  = UP_DEAL_S0;
			task_complete_GuangShui = PTL_PACK_FAIL;           
        }
        break;
    }
    return task_complete_GuangShui;
}

void Frame_ResetDealState_GuangShui(void)
{
	up_deal_stat_GuangShui = UP_DEAL_S0;//基线扩展规约处理状态复位
}

void Frame_ack_GS(STRUCT_FRAME_IES temp_struct, u8* p_temp_buf, u16 p_temp_len, u8* p_send_buf, u16* p_send_len)
{
    int i = 0;
	int temp_pack_len;
	UNION_2U8TOU16 temp_data;
	MEASURE_UPLOAD_INFO  temp_UploadInfo;
	u8 temp_buf1[24];
	u8 temp_buf2[24];
	u32 temp_cell_id=0;
	
	temp_pack_len = 0;
    p_send_buf[temp_pack_len++] = FRAME_HEAD_JM;//帧起始符（69H）
    p_send_buf[temp_pack_len++] = ((48+p_temp_len)>>8);//帧长度 高字节在前
    p_send_buf[temp_pack_len++] = (48+p_temp_len)&0xFF;
    p_send_buf[temp_pack_len++] = FRAME_MARK_JM;//帧标识 是否加密 1bit、是否分帧 2bit、帧序号 4bit、 是否包含运行参数段 1bit
	p_send_buf[temp_pack_len++] = FRAME_TYPE_JM;//协议版本  主版本 4bit、子版本 4bit
	//设备地址 8Byte
	p_send_buf[temp_pack_len++] = 0x79;
	for (i = 0; i < 7; i++)
	{
		p_send_buf[temp_pack_len++] = Sys_data_object.P1.met_addr[i];
	}
    p_send_buf[temp_pack_len++] = temp_struct.ack_ctrl; //功能码
	p_send_buf[temp_pack_len++] = FRAME_HEAD_JM;//帧确认符（69H）
	/////////////////////////运行参数///////////////////////////////
	//设备标识,休眠模式，小口径分体水表
	p_send_buf[temp_pack_len++] =0x03;
	//设备电压 02H E8H = 744 * 0.01 =7.44V
	temp_data.datas = PowerCheckValueSys();
	p_send_buf[temp_pack_len++] = temp_data.data_buf[1];
	p_send_buf[temp_pack_len++] = temp_data.data_buf[0];
	//信号强度 CSQ
	temp_UploadInfo = UploadGetInfo_Base();
	p_send_buf[temp_pack_len++] = temp_UploadInfo.metinfo.single_val;
	//接收功率（RSRP）由 2 字节组成，高字节在前，低字节在后
	temp_UploadInfo.metinfo.RSRP_val /=10;
	temp_UploadInfo.metinfo.RSRP_val =~temp_UploadInfo.metinfo.RSRP_val+1;
	p_send_buf[temp_pack_len++] = (temp_UploadInfo.metinfo.RSRP_val)>>8;
	p_send_buf[temp_pack_len++] = (temp_UploadInfo.metinfo.RSRP_val);
	//信噪比 由 2 字节组成，高字节在前，低字节在后
	if(temp_UploadInfo.metinfo.snr_val>=0x8000)
	{
		temp_UploadInfo.metinfo.snr_val -= 0x8000;
		temp_UploadInfo.metinfo.snr_val /= 10;
		temp_UploadInfo.metinfo.snr_val = ~temp_UploadInfo.metinfo.snr_val+1;
	}
	else
	{
		temp_UploadInfo.metinfo.snr_val /= 10;
	}
	p_send_buf[temp_pack_len++] = temp_UploadInfo.metinfo.snr_val>>8;
	p_send_buf[temp_pack_len++] = temp_UploadInfo.metinfo.snr_val;
	//覆盖等级
	p_send_buf[temp_pack_len++] = temp_UploadInfo.metinfo.ecl_val;
	//小区 ID  4Byte
	NbiotGetCell_ID(temp_buf1);
	for(i=0;i<CELL_ID_SIZE;i++)
	{
	    if(temp_buf1[i] != 0)
		{
			temp_cell_id <<= 4;
			if(temp_buf1[i] <= '9')
			{
			    temp_cell_id |= temp_buf1[i]-'0';
			}
			else if((temp_buf1[i] <= 'f')&&(temp_buf1[i] >= 'a'))
			{
			    temp_cell_id |= temp_buf1[i]-'a'+10;
			}
			else if((temp_buf1[i] <= 'F')&&(temp_buf1[i] >= 'A'))
			{
			    temp_cell_id |= temp_buf1[i]-'A'+10;
			}
		}
	}
	p_send_buf[temp_pack_len++] = temp_cell_id>>24;
	p_send_buf[temp_pack_len++] = temp_cell_id>>16;
	p_send_buf[temp_pack_len++] = temp_cell_id>>8;
	p_send_buf[temp_pack_len++] = temp_cell_id>>0;
	//IMEI 标识  8Byte BCD
	NbiotGetIMEI((char*)temp_buf1); 
	temp_buf2[0] = temp_buf1[0] - 0x30;
	for(i=1;i<8;i++)
	{
		temp_buf2[i]  =  (temp_buf1[2*i]-0x30)&0x0F;
		temp_buf2[i] |= ((temp_buf1[2*i-1]-0x30)<<4)&0xF0;          
	}
	for(i=0;i<8;i++)
	{
	    p_send_buf[temp_pack_len++] = temp_buf2[i];
	}
	//SIM 标识 10Byte BCD
	NbiotGetICCID((char*)temp_buf1); 
	for(i=0;i<10;i++)
	{
	    p_send_buf[temp_pack_len++] = ((temp_buf1[i*2]-0x30)<<4)|(temp_buf1[2*i+1]-0x30);
	}
	/////////////////////////数据段  ///////////////////////////////
	for(i=0;i<p_temp_len;i++)
	{
	    p_send_buf[temp_pack_len++] = p_temp_buf[i];
	}
	//校验和
	p_send_buf[temp_pack_len] = 0;//校验和清0
	for(i = 0; i < temp_pack_len; i++) //这里需要看下从0开始的///////
    {
        p_send_buf[temp_pack_len] += p_send_buf[i];
    }
    temp_pack_len++;
    p_send_buf[temp_pack_len++] = FRAME_END_JM;	//帧尾
	*p_send_len = temp_pack_len;
}
//应答数据组帧
void Frame_ack_GuangShui(u8* p_send_buf, u16* p_send_len)
{
	STRUCT_FRAME_IES temp_struct;
	
	temp_struct.ack_ctrl = frame_struct_GuangShui.ack_ctrl;
	temp_struct.DI0 = frame_struct_GuangShui.DI0;
	temp_struct.DI1 = frame_struct_GuangShui.DI1;
	switch(temp_struct.ack_ctrl)
	{
		case SS_ACK_CLEAR_FAIL_TIMES_CTRL:
		case SS_ACK_READ_HIS_CTRL:
		{
			temp_struct.sta = frame_struct_GuangShui.sta;
			temp_struct.serial_num0 = frame_struct_GuangShui.serial_num0;
			temp_struct.serial_num1 = frame_struct_GuangShui.serial_num1;

			Frame_ack_IesBase(temp_struct,g_frame_temp_buf_GuangShui, g_frame_temp_len_GuangShui, p_send_buf, p_send_len);
		}
		break;
		default://广水规约
		{
			Frame_ack_GS(temp_struct,g_frame_temp_buf_GuangShui, g_frame_temp_len_GuangShui, 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 UploadData_GS(void)
{
	SS_UNION_4U8TOU32 tempData;
	u8 temp_buf[6];
	//数据起始时间
	upload_frame_gs.data.Datetime[0] = upload_frame.data.StepFlowStartTime[5];
	upload_frame_gs.data.Datetime[1] = upload_frame.data.StepFlowStartTime[4];
	upload_frame_gs.data.Datetime[2] = upload_frame.data.StepFlowStartTime[3];
	upload_frame_gs.data.Datetime[3] = upload_frame.data.StepFlowStartTime[2];
	upload_frame_gs.data.Datetime[4] = upload_frame.data.StepFlowStartTime[1];
	upload_frame_gs.data.Datetime[5] = upload_frame.data.StepFlowStartTime[0];
	//时间段个数
	upload_frame_gs.data.StepCnt = 48;
	//时间间隔
	upload_frame_gs.data.Step[0] =0;
	upload_frame_gs.data.Step[1] =30;//30分钟
	//数据段代码
	upload_frame_gs.data.DataCode = 0x20;
	//表头行度（单位升 L），6 字节 12 位 BCD 码，无小数部分
	u32HEXtoBCDData(upload_frame.data.CurrentFlow.datas, &temp_buf[0]);
	upload_frame_gs.data.StepFlow1[0] = temp_buf[5];
	upload_frame_gs.data.StepFlow1[1] = temp_buf[4];
	upload_frame_gs.data.StepFlow1[2] = temp_buf[3];
	upload_frame_gs.data.StepFlow1[3] = temp_buf[2];
	upload_frame_gs.data.StepFlow1[4] = temp_buf[1];
	upload_frame_gs.data.StepFlow1[5] = temp_buf[0];
	//其他时间段数据
	for(int i=0; i<47; i++) 
	{
		tempData.data_buf[0] = upload_frame.data.StepFlow[i*2];
		tempData.data_buf[1] = upload_frame.data.StepFlow[i*2+1];
		tempData.data_buf[2] = 0;
		tempData.data_buf[3] = 0;						
		u32HEXtoBCDData(tempData.datas, &temp_buf[0]);
		upload_frame_gs.data.StepFlowN[i][0] = temp_buf[2];
		upload_frame_gs.data.StepFlowN[i][1] = temp_buf[1];
		upload_frame_gs.data.StepFlowN[i][2] = temp_buf[0];
	}
}
 
static void UploadData_GS_Current(void)
{
	U32_To_Float  temp_float_data;
	SS_UNION_4U8TOU32 tempData;
	DateTime tempRtcc_time;
	u8 temp_buf[6];
	
	RtccGetDateTime(&tempRtcc_time);//上报时间
	//数据起始时间
	upload_frame_gs.data.Datetime[0] = tempRtcc_time.s.year;
	upload_frame_gs.data.Datetime[1] = tempRtcc_time.s.month;
	upload_frame_gs.data.Datetime[2] = tempRtcc_time.s.day;
	upload_frame_gs.data.Datetime[3] = tempRtcc_time.s.hour;
	upload_frame_gs.data.Datetime[4] = tempRtcc_time.s.minute;
	upload_frame_gs.data.Datetime[5] = tempRtcc_time.s.second;
	//时间段个数
	upload_frame_gs.data.StepCnt = 1;
	//时间间隔
	upload_frame_gs.data.Step[0] =0;
	upload_frame_gs.data.Step[1] =0;
	//数据段代码
	upload_frame_gs.data.DataCode = 0x20;
	//表头行度（单位升 L），6 字节 12 位 BCD 码，无小数部分
	temp_float_data.gp22_u32 =  Gp30CheckDataApp(2);
	tempData.datas  = (u32)(temp_float_data.gp22_float*10);
	u32HEXtoBCDData(tempData.datas, &temp_buf[0]);
	upload_frame_gs.data.StepFlow1[0] = temp_buf[5];
	upload_frame_gs.data.StepFlow1[1] = temp_buf[4];
	upload_frame_gs.data.StepFlow1[2] = temp_buf[3];
	upload_frame_gs.data.StepFlow1[3] = temp_buf[2];
	upload_frame_gs.data.StepFlow1[4] = temp_buf[1];
	upload_frame_gs.data.StepFlow1[5] = temp_buf[0];
}
//临时周期上报数据存储
void GS_TempSaveProc(void)
{
	u8 temp_buf[6];
	SS_UNION_4U8TOU32 tempData;
	u8 temp_index=(ls_save_cnt/48);
	if(temp_index < 6)
	{
		if((ls_save_cnt%48)==0)//新的周期开始
		{
			for(int i=0;i<STRU_DATA_LEN_GS;i++)
			{
				ls_save_buf[temp_index].buf[i] =0;
			}
			new_gs_total_flow.gp22_u32 = Gp30CheckDataApp(2);
			old_gs_total_flow.gp22_u32 = new_gs_total_flow.gp22_u32;
			//保存数据起始时间
			ls_save_buf[temp_index].data.Datetime[0] = GuangShui_rtcc_time.s.year;
			ls_save_buf[temp_index].data.Datetime[1] = GuangShui_rtcc_time.s.month;
			ls_save_buf[temp_index].data.Datetime[2] = GuangShui_rtcc_time.s.day;
			ls_save_buf[temp_index].data.Datetime[3] = GuangShui_rtcc_time.s.hour;
			ls_save_buf[temp_index].data.Datetime[4] = GuangShui_rtcc_time.s.minute;
			ls_save_buf[temp_index].data.Datetime[5] = GuangShui_rtcc_time.s.second;
			//时间段个数清0
			ls_save_buf[temp_index].data.StepCnt = 0;
			//时间间隔
			ls_save_buf[temp_index].data.Step[0] =linshi_jm.Data.data_interval.data_buf[1];
			ls_save_buf[temp_index].data.Step[1] =linshi_jm.Data.data_interval.data_buf[0];
			//数据段代码
			ls_save_buf[temp_index].data.DataCode = 0x20;
			//表头行度（单位升 L），6 字节 12 位 BCD 码，无小数部分
			tempData.datas  = (u32)(new_gs_total_flow.gp22_float*10);
			u32HEXtoBCDData(tempData.datas, &temp_buf[0]);
			ls_save_buf[temp_index].data.StepFlow1[0] = temp_buf[5];
			ls_save_buf[temp_index].data.StepFlow1[1] = temp_buf[4];
			ls_save_buf[temp_index].data.StepFlow1[2] = temp_buf[3];
			ls_save_buf[temp_index].data.StepFlow1[3] = temp_buf[2];
			ls_save_buf[temp_index].data.StepFlow1[4] = temp_buf[1];
			ls_save_buf[temp_index].data.StepFlow1[5] = temp_buf[0];
			ls_save_buf[temp_index].data.StepCnt++;
		}
		else
		{
			new_gs_total_flow.gp22_u32 = Gp30CheckDataApp(2);
			jiange_data.datas = (u16)((new_gs_total_flow.gp22_float - old_gs_total_flow.gp22_float)*10);
			old_gs_total_flow.gp22_u32 = new_gs_total_flow.gp22_u32;
			
			tempData.data_buf[0] = jiange_data.datas_map.data0;
			tempData.data_buf[1] = jiange_data.datas_map.data1;
			tempData.data_buf[2] = 0;
			tempData.data_buf[3] = 0;						
			u32HEXtoBCDData(tempData.datas, &temp_buf[0]);
			ls_save_buf[temp_index].data.StepFlowN[ls_save_buf[temp_index].data.StepCnt-1][0] = temp_buf[2];
			ls_save_buf[temp_index].data.StepFlowN[ls_save_buf[temp_index].data.StepCnt-1][1] = temp_buf[1];
			ls_save_buf[temp_index].data.StepFlowN[ls_save_buf[temp_index].data.StepCnt-1][2] = temp_buf[0];
			ls_save_buf[temp_index].data.StepCnt++;
		}
		ls_save_cnt++;
	}
}


int GetUploadDataCount(void)
{
	if (Sys_data_object.P1.histroy_w >= Sys_data_object.P1.histroy_r)
		return (Sys_data_object.P1.histroy_w - Sys_data_object.P1.histroy_r);
	else 
		return (Sys_data_object.P1.histroy_w + PERIOD_DATA_NUM - Sys_data_object.P1.histroy_r);
}

void GetUploadDataResetState_GS(void)
{
	upload_frame_stat_GuangShui =UP_DEAL_S0;
	upload_task_complete_GuangShui = PTL_PACK_BUSY;
}
u8 GetUploadDataCheckIdle_GS(void)
{
	if(UP_DEAL_S0 == upload_frame_stat_GuangShui)
    {
        return TASK_IDLE;
    }
    else
    {
        return TASK_BUSY;
    }
}
u8 GetUploadDataCheckTask_GS(void)
{
	return upload_task_complete_GuangShui;
}

void GetUploadDataMachine_GS(u8 data_step, u8* p_send_buf, u16* p_send_len)
{
	switch(upload_frame_stat_GuangShui)
	{
		case UP_DEAL_S0:
		{
			if(data_step == UPLOAD_FRAME_REQUEST_NEW)//请求新数据包
			{
				temp_upload_step = 0;
				upload_frame_retry_times = 0;//重发次数清0
				upload_frame_stat_GuangShui = UP_DEAL_S1;
			}
			else if(data_step == UPLOAD_FRAME_UPLOAD_OK)//发送成功
			{
				gs_upload_fail_times = 0;
				upload_frame_retry_times = 0;//重发次数清0
				if(temp_upload_step == UP_DEAL_S10)//周期历史数据
				{
					Sys_data_object.P1.histroy_r++;
					if(Sys_data_object.P1.histroy_r >= PERIOD_DATA_NUM)
					{
						Sys_data_object.P1.histroy_r = 0;
					}
					setStoreMsg(SS_StoreMsg3, TRUE);//保存读写指针
					upload_frame_stat_GuangShui = UP_DEAL_S1;//继续上报下一条
				}
				else if(temp_upload_step == UP_DEAL_S11)//当前数据上报成功,结束
				{
					upload_task_complete_GuangShui = PTL_PACK_FAIL;
					upload_frame_stat_GuangShui = UP_DEAL_S0;
				}
				else if(temp_upload_step == UP_DEAL_S30)//临时上报
				{
					if(ls_upload_index < ls_save_cnt/48)
					{
						ls_upload_index++;
						upload_frame_stat_GuangShui = UP_DEAL_S30;
					}
					else
					{
						upload_task_complete_GuangShui = PTL_PACK_FAIL;
						upload_frame_stat_GuangShui = UP_DEAL_S0;
					}
				}
			}
			else if(data_step == UPLOAD_FRAME_UPLOAD_RETRY)//请求重新发送
			{
				upload_frame_stat_GuangShui = UP_DEAL_S21;
			}
			else if(data_step == UPLOAD_FRAME_ONLINE_FAIL) //联网失败
			{
				if(ls_upload_flag)//临时上报不重试
				{
					ls_upload_flag = 0;//清除临时上报标志
				}
				else
				{
					if(UPLOAD_TYPE_FIX_TIME == GetEventUploadType())//定时上传
					{
						setStoreMsg(SS_StoreMsg1, TRUE);//标记补报位置
						SystemAddUploadAgain();//重传增加
					}
					else if(UPLOAD_TYPE_RETRY_AGAIN == GetEventUploadType())//重传
					{
						SystemAddUploadAgain();//重传增加
					}
				}	
				gs_upload_fail_times++;//失败次数增加				
				upload_task_complete_GuangShui = PTL_PACK_FAIL;
				upload_frame_stat_GuangShui = UP_DEAL_S0;
			}
			else 
			{
				ls_upload_flag = 0;//清除临时上报标志
				upload_task_complete_GuangShui = PTL_PACK_FAIL;
				upload_frame_stat_GuangShui = UP_DEAL_S0;
			}
		}
		break;
		case UP_DEAL_S1:
		{
			if(ls_upload_flag)//临时上报
			{
				upload_frame_stat_GuangShui = UP_DEAL_S30;
			}
			else
			{
				if(EepIfIdle())
				{
					upload_packet_count = GetUploadDataCount();
					if(upload_packet_count > 0)
					{
						EepRead(EEP_ADDR_CIRCLE_DATA+(Sys_data_object.P1.histroy_r*PACK_NUM_GPRS), &upload_frame.buf[0], PACK_NUM_GPRS, &s_eep_complete_GuangShui);
						upload_frame_stat_GuangShui = UP_DEAL_S10;
					}
					else//没有记录,上报一条当前数据
					{
						upload_frame_stat_GuangShui = UP_DEAL_S11;
					}
				}
			}
		}
		break;
		case UP_DEAL_S10://转广水协议上报帧
		{
			if(EepIfIdle())
			{
				temp_upload_step = UP_DEAL_S10;
				UploadData_GS();
				upload_temp_len_GuangShui = STRU_DATA_LEN_GS;//上报数据长度
				upload_frame_stat_GuangShui = UP_DEAL_S20;
			}
		}
		break;
		case UP_DEAL_S11://当前数据组帧
		{
			if(EepIfIdle())
			{
				temp_upload_step = UP_DEAL_S11;
				UploadData_GS_Current();
				upload_temp_len_GuangShui = STRU_DATA_LEN_GS_CURRENT;
				upload_frame_stat_GuangShui = UP_DEAL_S20;
			}
		}
		break;
		case UP_DEAL_S20://组上传帧
		{
			if(EepIfIdle())
			{
				upload_struct_GuangShui.ack_ctrl = 0x01;
				Frame_ack_GS(upload_struct_GuangShui, upload_frame_gs.buf, upload_temp_len_GuangShui, p_send_buf, p_send_len);
				upload_task_complete_GuangShui = PTL_PACK_SUCCESS;// 成功
				upload_frame_stat_GuangShui = UP_DEAL_S0;
			}
		}
		break;
		case UP_DEAL_S21://重试
		{
			if(upload_frame_retry_times < 3) 
			{
				upload_frame_retry_times++;
				upload_frame_stat_GuangShui = UP_DEAL_S20;
			} 
			else//重新发送失败3次的处理
			{
				if(temp_upload_step == UP_DEAL_S1)//当前包发送失败,判断是否补报
				{
					if(UPLOAD_TYPE_FIX_TIME == GetEventUploadType())//定时上传
					{
						SystemAddUploadAgain();//重传增加
						gs_upload_fail_times++;//失败次数增加
					}
					else if(UPLOAD_TYPE_RETRY_AGAIN == GetEventUploadType())//重传
					{
						SystemAddUploadAgain();//重传增加
					}
				}
				upload_task_complete_GuangShui = PTL_PACK_FAIL;
				upload_frame_stat_GuangShui = UP_DEAL_S0;
			}
		}
		break;
		case UP_DEAL_S30://临时上报组帧
		{
			temp_upload_step = UP_DEAL_S30;
			if(ls_upload_index <= ls_save_cnt/48)
			{
				for(int i=0;i<STRU_DATA_LEN_GS;i++)
				{
					upload_frame_gs.buf[i] = ls_save_buf[ls_upload_index].buf[i];
				}
				upload_temp_len_GuangShui = STRU_DATA_LEN_GS;//无密集上报数据长度
				upload_frame_stat_GuangShui = UP_DEAL_S20;
			}
			else
			{
				ls_upload_flag = 0;//清除临时上报标志
				upload_task_complete_GuangShui = PTL_PACK_FAIL;
				upload_frame_stat_GuangShui = UP_DEAL_S0;
			}
		}
		break;
		default:
		{
			ls_upload_flag = 0;//清除临时上报标志
			upload_task_complete_GuangShui = PTL_PACK_FAIL;
			upload_frame_stat_GuangShui = UP_DEAL_S0;
		}
		break;
	}
}
/*************************************************规约数据存储部分****************************/
//补报标志存储
static void SS_Store_SuppleFlagMachine(void)
{
	switch(step_clear_flag)
	{
		case SS_STATE_S0:
		{
			if(getStoreMsg(SS_StoreMsg1) == TRUE)//保存补报标志
			{
				setStoreMsg(SS_StoreMsg1,FALSE);
				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_GuangShui);
				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);
				setStoreMsg(SS_StoreMsg6,TRUE);//历史数据存储消息
				RtccGetDateTime(&GuangShui_rtcc_time);
				if(get_lset_judg(&ls_save,&ls_up))
				{
					if(ls_save)
					{
						setStoreMsg(SS_StoreMsg7,TRUE);//临时间隔流量存储消息
					}
					if(ls_up)
					{
						ls_up=FALSE;
						FrameSetMsgApp(MsgShenYang_NbUploadWarning);
						ls_upload_flag = 1;
					}
				}
				if((GuangShui_rtcc_time.s.minute==0x00)||(GuangShui_rtcc_time.s.minute==0x30))
				{
					FrameSetMsgApp(MsgSS_JianGeStore);//半小时间隔流量存储消息
				}
				if ((GuangShui_rtcc_time.s.hour == 0)&&(GuangShui_rtcc_time.s.minute==0x00))//冻结时间到，零点
				{
					//间隔流量清除:2B(间隔流量)X2(次/h)X24h=96B
					for(int i = 0; i <96 ;i++)//间隔流量清除
					{
						store_frame.data.StepFlow[i] = 0;
					}
					//间隔流量初始化
					new_jiange_total_flow.gp22_u32 = Gp30CheckDataApp(2);
					old_jiange_total_flow.gp22_u32 = new_jiange_total_flow.gp22_u32;
					//存储消息
					RtccSetMsgApp(RtccMsg_DataSave);//0点更新冻结数据
					FrameSetMsgApp(MsgSS_HisIndexStore);//0点更新冻结数据指针
				}
			}
			/**间隔流量和冻结数据存储：先0点存储冻结数据; 然后最后存储指针增加; 最后开始新的一轮间隔流量存储,期间判断写指针的有效性;*/

			if(FrameCheckMsgApp(MsgSS_HisIndexStore))//0点更新冻结数据指针
			{
				FrameClearMsgApp(MsgSS_HisIndexStore);
				step_save_supplt = SS_STATE_S2;
			}
			else if(RtccCheckMsgApp(RtccMsg_DataSave))//0点保存冻结数据
			{
				RtccClearMsgApp(RtccMsg_DataSave);
				step_save_supplt = SS_STATE_S10;
			}
			else if(FrameCheckMsgApp(MsgSS_JianGeStore)) //半小时间隔流量存储
			{
				FrameClearMsgApp(MsgSS_JianGeStore);
				step_save_supplt = SS_STATE_S20;
			}
			
			else if(FrameCheckMsgApp(Msg_ClearHisOther))//清表数据
			{
				FrameClearMsgApp(Msg_ClearHisOther);
				step_save_supplt = SS_STATE_S40;
			} 
			else if(getStoreMsg(SS_StoreMsg3) == TRUE)//清所有补报标志
			{
				setStoreMsg(SS_StoreMsg3,FALSE);
				step_save_supplt = SS_STATE_S3;
			}
			else if(getStoreMsg(SS_StoreMsg7) == TRUE)//临时间隔流量存储
			{
				setStoreMsg(SS_StoreMsg7,FALSE);
				step_save_supplt = SS_STATE_S30;
			}
			break;
		}
		/*0点更新冻结数据*/
		case SS_STATE_S10:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				//(只)更新表计数据和表计状态
				Upload_RenewCurrentData(&store_frame);
			    EepWrite(store_frame.buf, (EEP_ADDR_CIRCLE_DATA+(Sys_data_object.P1.histroy_w*PACK_NUM_GPRS)), CURRENT_DATA_INFO_LEN, &s_eep_complete_GuangShui);
				step_save_supplt = SS_STATE_S11;
			}
		    break;
		}
		case SS_STATE_S11://保存上报时间 
		{
			if(TASK_IDLE == EepIfIdle())
			{
			    EepWrite(&store_frame.data.UploadTime[0], (EEP_ADDR_CIRCLE_DATA+(Sys_data_object.P1.histroy_w*PACK_NUM_GPRS)+UPLOAD_TIME_ADDR), 7, &s_eep_complete_GuangShui);
				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_GuangShui);
				step_save_supplt = SS_STATE_WAIT;
			}	
			break;					
		}
		case SS_STATE_S3://保存读指针
		{
			if(TASK_IDLE == EepIfIdle())
			{			
				EepWrite(&Sys_data_object.P1.histroy_r, (FRAME_NBIot_BUBAO_FLAG+4), 2, &s_eep_complete_GuangShui);
				step_save_supplt = SS_STATE_WAIT;
			}	
			break;					
		}
		/*间隔流量存储*/
		case SS_STATE_S20://半小时间隔流量时间判断:防止首次上电,日期变更
		{
			if(EepIfIdle())
			{		
				if(	(GuangShui_rtcc_time.s.year  == store_frame.data.StepFlowStartTime[5])&&
					(GuangShui_rtcc_time.s.month == store_frame.data.StepFlowStartTime[4])&&
					(GuangShui_rtcc_time.s.day   == store_frame.data.StepFlowStartTime[3]))
				{
					step_save_supplt = SS_STATE_S22;
				}
				else
				{	
					EepRead(EEP_ADDR_CIRCLE_DATA+(Sys_data_object.P1.histroy_w*PACK_NUM_GPRS)+STEP_FLOW_TIME_ADDR, &store_frame.data.StepFlowStartTime[0], 7, &s_eep_complete_GuangShui);
					step_save_supplt = SS_STATE_S21;
				}
			}
		}
		break;
		case SS_STATE_S21:
		{
			if(EepIfIdle())
			{		
				if(	(GuangShui_rtcc_time.s.year  == store_frame.data.StepFlowStartTime[5])&&
					(GuangShui_rtcc_time.s.month == store_frame.data.StepFlowStartTime[4])&&
					(GuangShui_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]= GuangShui_rtcc_time.s.day;
					store_frame.data.StepFlowStartTime[4]= GuangShui_rtcc_time.s.month;
					store_frame.data.StepFlowStartTime[5]= GuangShui_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, (EEP_ADDR_CIRCLE_DATA+(Sys_data_object.P1.histroy_w*PACK_NUM_GPRS)), PACK_NUM_GPRS, &s_eep_complete_GuangShui);
				}
			}
		}
		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 = EEP_ADDR_CIRCLE_DATA+(Sys_data_object.P1.histroy_w*PACK_NUM_GPRS)+STEP_FLOW_START_ADDR + 
				                      BCD2HEX(GuangShui_rtcc_time.s.hour)*4 + (BCD2HEX(GuangShui_rtcc_time.s.minute)/30)*2;
				EepWrite(&jiange_data.datas_map.data0, temp_step_flow_addr, 2, &s_eep_complete_GuangShui);
				step_save_supplt = SS_STATE_WAIT;
			}
		}
		break;
		//临时间隔流量存储
		case SS_STATE_S30:
		{
			GS_TempSaveProc();
			step_save_supplt = SS_STATE_S0;
		}
		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_GuangShui);
				step_save_supplt = SS_STATE_WAIT;
			}	
			break;					
		}
		case SS_STATE_WAIT:
		{
            if(TASK_IDLE == EepIfIdle())
            {
                 step_save_supplt = SS_STATE_S0;
            }
            break;
		}
        default:
        {
            break;		
        }			
    }
}

//历史数据存储
static void SS_Store_HisDataMachine(void)
{
	switch(his_save_step) 
	{
		case SS_STATE_S0:
		{
			if(getStoreMsg(SS_StoreMsg6) == TRUE)//历史数据存储每分钟判断
			{
				setStoreMsg(SS_StoreMsg6,FALSE);
				save_his_interval_cnt++;
			}
			if(save_his_interval_cnt > his_save_para.Data.Period.datas)
			{
				save_his_interval_cnt = 0;
				his_save_step = SS_STATE_S1;
			}
		}
		break;
		case SS_STATE_S1:
		{
			if(TASK_IDLE == EepIfIdle())
            {
                his_save_flow_value.gp22_u32 = Gp30CheckDataApp(2);
				his_save_flow_buf[0] = his_save_flow_value.buf[0];
				his_save_flow_buf[1] = his_save_flow_value.buf[1];
				his_save_flow_buf[2] = his_save_flow_value.buf[2];
				his_save_flow_buf[3] = his_save_flow_value.buf[3];
				his_save_flow_buf[4] = his_save_flow_buf[0]+his_save_flow_buf[1]+his_save_flow_buf[2]+his_save_flow_buf[3];
				his_save_addr = EEP_ADDR_HIS_DATA+((his_save_para.Data.Index_w.datas%GS_HIS_STORE_MAX)*5);//计算本时刻流量存储位置
				EepWrite(&his_save_flow_buf[0], his_save_addr, 5, &s_eep_complete_GuangShui);
				his_save_step = SS_STATE_S2;
            }
		}
		break;
		case SS_STATE_S2://保存存储指针
		{
			if(TASK_IDLE == EepIfIdle())
            {
				his_save_para.Data.Index_w.datas++;
				EepWrite(&his_save_para.Data.Index_w.data_buf[0], EEP_ADDR_HIS_INDEX, 4, &s_eep_complete_GuangShui);
				his_save_step = SS_STATE_S0;
            }
		}
		break;
		default:
		{
			his_save_step = SS_STATE_S0;
		}
		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
			{
				temp_fail_times = gs_upload_fail_times;
			}
			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;
			gs_upload_fail_times =0;
			setStoreMsg(SS_StoreMsg5,TRUE);//保存EEPROM
			break;
		}
		case NB_SS_FormatError://上报数据格式错误
		{
			signal_val.SignalStruct.format_error = data;
			break;
		}
		default:
			break;
	}
}

void GuangShui_Store_InitApp(void)
{
    TYP_STATE_QZ tmp_init_stat = QZ_STATE_S0;
	if(Sys_data_object.P3.FirstPowerOnFlag == FIRST_POWER_ON_FLAG)
	{
		tmp_init_stat = QZ_STATE_S10;
	}
	else
	{
		tmp_init_stat = QZ_STATE_S20;
	}
    while (tmp_init_stat)
    {
      switch (tmp_init_stat)
      {
        case QZ_STATE_S0:
        {
			if (TASK_IDLE == EepIfIdle())
            {
                tmp_init_stat = QZ_STATE_S0;
            }
            break;
        }
        case QZ_STATE_S10://第一次上电流程,历史数据存储参数
        {
            if(TASK_IDLE == EepIfIdle())
            {
				his_save_para.Data.Period.datas = 1440;//默认1天间隔存储
				his_save_para.Data.Index_w.datas = 0;
				EepWrite(&his_save_para.data_buf[0], EEP_ADDR_HIS_INDEX, 6, &s_eep_complete_GuangShui);//历史数据存储
                tmp_init_stat = QZ_STATE_S11;
            }
            break;
        }
		case QZ_STATE_S11://第一次上电流程,临时数据存储和上报参数
        {
            if(TASK_IDLE == EepIfIdle())
            {
				linshi_jm.Data.up_interval.datas = 240;//4小时上报
				linshi_jm.Data.data_interval.datas = 5;//5分钟存储间隔
				EepWrite(&his_save_para.data_buf[0], EEP_ADDR_HIS_INDEX, 6, &s_eep_complete_GuangShui);//历史数据存储
                tmp_init_stat = QZ_STATE_S1;
            }
            break;
        }
		case QZ_STATE_S20://非第一次上电,读取历史数据存储参数
        {
            if (TASK_IDLE == EepIfIdle())
            {
                EepRead(EEP_ADDR_HIS_INDEX, &his_save_para.data_buf[0], 6, &s_eep_complete_GuangShui);
                tmp_init_stat = QZ_STATE_S21;				
            }
            break;			
        }
		case QZ_STATE_S21://非第一次上电,读取临时数据存储和上报参数
		{
			if (TASK_IDLE == EepIfIdle())
            {
                EepRead(EEP_ADDR_TEMP_STORE_UPLOAD, &linshi_jm.data_buf[0], 8, &s_eep_complete_GuangShui);
                tmp_init_stat = QZ_STATE_S1;				
            }	
			break;
		}
		case QZ_STATE_S1:
		{
			if (TASK_IDLE == EepIfIdle())
            {
                tmp_init_stat = QZ_STATE_S0;				
            }
			break;
		}
		default:
		{
			tmp_init_stat  =QZ_STATE_S0;
		    break;
		}
      }
      EepMachineDriver();
    }
	if(his_save_para.Data.Index_w.datas>0x80000000)
	{
		his_save_para.Data.Index_w.datas =0;
	}
}
/****************************数据存储部分公用接口*********************************************/
//规约数据存储部分初始化
void DataStoreInit(void)
{
	Quanzhou_Store_InitApp();
	GuangShui_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();//冻结数据存储
	SS_Store_HisDataMachine();//历史数据存储
}
//规约数据存储休眠判断
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

