#include "sys_config.h"
#if PRODUCT_JS
#include "UWater_frame_app.h"
#include "UWater_eeprom_driver.h"
#include "UWater_rtcc_app.h"
#include "UWater_power_app.h"
#include "UWater_lcd_app.h"	
#include "UWater_gp22_app.h"
#include "sys_processor.h"
#include "UWater_DataTools_App.h"
#include "UWater_JX_Store_App.h"
#include "UWater_sys.h"
/******************************************************************************************************
com_rxbuf_wcount_copy，com_rxbuf_rcount_copy 用于记录上报时刻的com_rxbuf_wcount和 com_rxbuf_rcount;
上报完成后，会将com_rxbuf_rcount_copy赋予com_rxbuf_rcount更新。
*/
#define __no_init __attribute__((zero_init))
__no_init volatile u32 jx_com_rxbuf_wcount_copy __attribute__((section(".ARM.__at_0x20007F28")));
__no_init volatile u32 jx_com_rxbuf_rcount_copy __attribute__((section(".ARM.__at_0x20007F2C")));
__no_init volatile u16 jx_com_rxbuf_check_copy __attribute__((section(".ARM.__at_0x20007F30")));

UNION_4U8TOU32 		com_rxbuf_wcount, com_rxbuf_rcount;			          //存储数据读写指针
UNION_4U8TOU32 		com_rxbuf_wcount_copy, com_rxbuf_rcount_copy;			//存储数据读写指针上传时间冻结指针
u32 temp_rxbuf_rcount=0;
//上传时com_rxbuf_rcount_copy的变量，上传前com_rxbuf_rcount_copy=temp_rxbuf_rcount，上传后temp_rxbuf_rcount=com_rxbuf_rcount_copy
/******************************************************************************************************/
/*
old_miji_interval_flow，new_miji_interval_flow用于计算间隔流量
当new_miji_interval_flow和old_miji_interval_flow发生变化时,
记录差值new_miji_interval_flow-old_miji_interval_flow做为
*/
U32_To_Float  old_miji_interval_flow;
U32_To_Float  new_miji_interval_flow; 
U32_To_Float  temp_base_flow;
static u8 miji_interval_time;//密集采样间隔时间
/******************************************************************************************************/
static DateTime s_temp_store_Time;//用于取出当前时间
UNION_JX_STORE_PackHead u_temp_store_data;//间隔存储数据格式

u8 upload_store_buf[JX_UPLOAD_LEN];//364 上报数据缓存：8字节间隔数据x最大45条+4字节流量基数据
u8 upload_store_count=0; //记录上报的数据包实际的间隔数据条数，其余补0xFF
  
u8 s_temp_store_ee_msg; //EEPROM操作状态

static u8 jiangxi_store_first_power_flag=0;//第一次上电标志
static TYP_STATE_JX jiangxi_store_init_step=JX_STATE_S0;//上电初始化状态
static TYP_STATE_JX step_jx_store=JX_STATE_S0; //间隔流量数据存储状态
/******************************************************************************************************
JiangXiStore_InitApp
江西规约存储、读取指针上电初始化
******************************************************************************************************/
void JiangXiStore_InitApp(void)
{
	jiangxi_store_init_step = JX_STATE_S1;
	while(jiangxi_store_init_step)
	{
		switch(jiangxi_store_init_step)
		{
			case JX_STATE_S0:
			{
				break;
			}
			case JX_STATE_S1:
			{
				if (TASK_IDLE == EepIfIdle()) 
				{
					EepRead(EEP_ADDR_JX_STORE_FIRST_POWER, &jiangxi_store_first_power_flag, 1, &s_temp_store_ee_msg);
					jiangxi_store_init_step = JX_STATE_S2;
				}
				break;
			}
			case JX_STATE_S2:
			{
				if (TASK_IDLE == EepIfIdle())
				{
					if (0x55 == jiangxi_store_first_power_flag)
					{
						jiangxi_store_init_step = JX_STATE_S20;
					}
					else //第一次上电
					{
						com_rxbuf_wcount.datas = 0;
						com_rxbuf_rcount.datas = 0;
						StoreRWIndex_2_Ram();
						jiangxi_store_init_step = JX_STATE_S10;
					}
				}
				break;
			}
			//第一次上电
			case JX_STATE_S10://保存写指针
			{
				if (TASK_IDLE == EepIfIdle())
				{
					EepWrite(&com_rxbuf_wcount.data_buf[0], EEP_ADDR_JX_STORE_WRITE, 4, &s_temp_store_ee_msg);
					jiangxi_store_init_step = JX_STATE_S11;
				}
				break;
			}
			case JX_STATE_S11://保存读指针
			{
				if ((SUCCESS == EepCheckMsg(s_temp_store_ee_msg)) && (TASK_IDLE == EepIfIdle()))
				{
					EepWrite(&com_rxbuf_rcount.data_buf[0], EEP_ADDR_JX_STORE_READ, 4, &s_temp_store_ee_msg);
					jiangxi_store_init_step = JX_STATE_S12;
				}
				break;
			}
			case JX_STATE_S12://保存上电标志
			{
				if (TASK_IDLE == EepIfIdle())
				{
					jiangxi_store_first_power_flag = 0x55;
				   EepWrite(&jiangxi_store_first_power_flag, EEP_ADDR_JX_STORE_FIRST_POWER, 1, &s_temp_store_ee_msg);
				   jiangxi_store_init_step = JX_STATE_S30;
				}
				break;
			}
			//非第一次上电
			case JX_STATE_S20://读取写指针
			{
				if (TASK_IDLE == EepIfIdle())
				{
					EepRead(EEP_ADDR_JX_STORE_WRITE, com_rxbuf_wcount.data_buf, 4, &s_temp_store_ee_msg);
				   jiangxi_store_init_step = JX_STATE_S21;
				}
				break;
			}
			case JX_STATE_S21://读取读指针
			{
				if (TASK_IDLE == EepIfIdle())
				{
					EepRead(EEP_ADDR_JX_STORE_READ, com_rxbuf_rcount.data_buf, 4, &s_temp_store_ee_msg);
				   jiangxi_store_init_step = JX_STATE_S30;
				}
				break;
			}
			//等待EE操作完成
			case JX_STATE_S30:
			{
				if (TASK_IDLE == EepIfIdle())
				{
					if(jx_com_rxbuf_check_copy == getCRC_HisIndex(jx_com_rxbuf_wcount_copy, jx_com_rxbuf_rcount_copy))
					{
						com_rxbuf_wcount.datas = jx_com_rxbuf_wcount_copy;
						com_rxbuf_rcount.datas = jx_com_rxbuf_rcount_copy;
					}
				   jiangxi_store_init_step = JX_STATE_S0;
				}
				break;
			}
			default:
			{
			    jiangxi_store_init_step = JX_STATE_S0;
				break;
			}
		}
		EepMachineDriver();
	}
}
/*************************************************************************
函数名：write_com_rx_data
说  明：存储一条间隔数据流量；并存储对应的指针和按条件存储流量基数据
输  入:UNION_JX_STORE_PackHead类型。该数据类型包括间隔数据和流量基数据。
       流量基数据按每45条间隔数据对应一条基数据。
*************************************************************************/
int write_com_rx_data(UNION_JX_STORE_PackHead *dat)
{
	static TYP_STATE_JX step_store_write=JX_STATE_S0;
	switch(step_store_write)
	{
		case JX_STATE_S0://保存间隔数据
		{
			if(TASK_IDLE == EepIfIdle())
			{
				if(com_rxbuf_wcount.datas == 0xFFFFFFFF)//判断指针有效性
				{
					com_rxbuf_wcount.datas = 0;//重新初始化指针
					com_rxbuf_rcount.datas = 0;
					StoreRWIndex_2_Ram();
				}
				EepWrite(dat->Stru.StepUnion.buf, (EEP_ADDR_JX_STORE_STEP_START + (com_rxbuf_wcount.datas%COM_RXBUF_MAX)*8), 8, &s_temp_store_ee_msg);
				step_store_write = JX_STATE_S1;
			}
			break;
		}
		case JX_STATE_S1://保存基数据
		{
			if(TASK_IDLE == EepIfIdle())
			{
				if((com_rxbuf_wcount.datas % 45) == 0)
				{
					EepWrite(dat->Stru.BaseFlowData.data_buf, EEP_ADDR_JX_STORE_BASE_START + ((com_rxbuf_wcount.datas%COM_RXBUF_MAX)/45)*4, 4, &s_temp_store_ee_msg);
				}
				step_store_write = JX_STATE_S2;
			}
			break;
		}
		case JX_STATE_S2://保存写指针
		{
			if(TASK_IDLE == EepIfIdle())//ee写完成
			{
				com_rxbuf_wcount.datas ++;
				StoreRWIndex_2_Ram();
				step_store_write = JX_STATE_S0;
				return SUCCESS;//返回完成	
			}
			break;
		}
		default:
		{
			step_store_write = JX_STATE_S0;
		}
	}
	return ERROR;
}
/*************************************************************************
函数名:Copy_R_W_Count
说  明:上传时刻冻结当前存储的读、写指针
返回值:操作完成返回1，操作未完成返回0xFF
*************************************************************************/
u8 Copy_R_W_Count(void)
{
	static TYP_STATE_JX step_store_read_count_copy=JX_STATE_S0;
	switch(step_store_read_count_copy)
	{
		case JX_STATE_S0:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				if((com_rxbuf_wcount.datas == 0xFFFFFFFF)||(com_rxbuf_rcount.datas == 0xFFFFFFFF))//判断指针有效性
				{
					com_rxbuf_wcount.datas = 0;
					com_rxbuf_rcount.datas = 0;
					StoreRWIndex_2_Ram();
				    step_store_read_count_copy = JX_STATE_S1;
				}
				else
				{
					com_rxbuf_wcount_copy.datas = com_rxbuf_wcount.datas;//6cb7
					com_rxbuf_rcount_copy.datas = com_rxbuf_rcount.datas;//1f2f

					if (com_rxbuf_wcount_copy.datas >= com_rxbuf_rcount_copy.datas )
					{
						if((com_rxbuf_wcount_copy.datas - com_rxbuf_rcount_copy.datas) > COM_RXBUF_MAX)
						{
							com_rxbuf_rcount_copy.datas = (com_rxbuf_wcount_copy.datas - COM_RXBUF_MAX);//6177
						}
					}
					else
					{
						com_rxbuf_rcount_copy.datas = com_rxbuf_wcount_copy.datas;
					}
					if(com_rxbuf_wcount_copy.datas > (com_rxbuf_rcount_copy.datas + COM_RXBUF_MAX -45))//判断是否覆盖了首包数据的基数据
					{
						com_rxbuf_rcount_copy.datas = com_rxbuf_rcount_copy.datas + (45 - com_rxbuf_rcount_copy.datas%45);//舍去首包的残余条数
					}
					step_store_read_count_copy = JX_STATE_S0;//38941---38970
					return SUCCESS;
				}
			}
			break;
		}
		//数据无效,重新初始化读，写指针
		case JX_STATE_S1:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				EepWrite(com_rxbuf_wcount.data_buf, EEP_ADDR_JX_STORE_WRITE, 4, &s_temp_store_ee_msg);
				step_store_read_count_copy = JX_STATE_S2;
			}
		    break;
		}
		case JX_STATE_S2:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				EepWrite(com_rxbuf_rcount.data_buf, EEP_ADDR_JX_STORE_READ, 4, &s_temp_store_ee_msg);
				step_store_read_count_copy = JX_STATE_S0;
			}
		    break;
		}
		default:
		{
			step_store_read_count_copy = JX_STATE_S0;
			break;
		}
	}
	return ERROR;
}

/*************************************************************************
函数名:Renew_Rcount
说  明:将上报用完之后的com_rxbuf_rcount_copy更新到读指针com_rxbuf_rcount.
       目的是防止上报失败或个别包上报失败.
       成功收到上报应答时调用.
       temp_rxbuf_rcount的作用是暂时保存com_rxbuf_rcount_copy，
       如果上报成功则赋值给com_rxbuf_rcount_copy,上报不成功，则不影响com_rxbuf_rcount_copy
返回值:操作完成返回1，操作未完成返回0xFF
*************************************************************************/
u8 Renew_Rcount(void)
{
	static TYP_STATE_JX step_renew_Rcount=JX_STATE_S0;
	switch(step_renew_Rcount)
	{
		case JX_STATE_S0://保存读指针
		{
			if(TASK_IDLE == EepIfIdle())//ee写完成
			{
				com_rxbuf_rcount_copy.datas = temp_rxbuf_rcount;
				com_rxbuf_rcount.datas = com_rxbuf_rcount_copy.datas;
				EepWrite(com_rxbuf_rcount.data_buf, EEP_ADDR_JX_STORE_READ, 4, &s_temp_store_ee_msg);
				step_renew_Rcount = JX_STATE_S1;
			}
			break;
		}
		case JX_STATE_S1:
		{
			if(TASK_IDLE == EepIfIdle())//ee写完成
			{
				step_renew_Rcount = JX_STATE_S0;
				return SUCCESS;
			}
			break;
		}
		default:
		{
			step_renew_Rcount = JX_STATE_S0;
			break;
		}
	}
	return ERROR;
}


/************************************************************************
函数名:get_com_rx_num_copy
说  明:根据冻结指针计算需要上报的记录条数,
       如果超过COM_RXBUF_MAX,就取COM_RXBUF_MAX
返回值:操作完成返回1，操作未完成返回0xFF
*************************************************************************/
u16 get_com_rx_num_copy(void)
{
	if (com_rxbuf_wcount_copy.datas >= com_rxbuf_rcount_copy.datas )
	{
		if((com_rxbuf_wcount_copy.datas - com_rxbuf_rcount_copy.datas) > COM_RXBUF_MAX)
		{
			com_rxbuf_rcount_copy.datas = (com_rxbuf_wcount_copy.datas - COM_RXBUF_MAX);
		}
		return (com_rxbuf_wcount_copy.datas - com_rxbuf_rcount_copy.datas);
	}
	return 0;
}

/*************************************************************************
函数名:get_com_rx_data
说  明:获取上传数据
*************************************************************************/
int get_com_rx_data(void)
{
    static TYP_STATE_JX step_store_read=JX_STATE_S0;
    switch(step_store_read)
    {
        case JX_STATE_S0://读取存储指针
        {
            int i=0;
			for(i=0;i<JX_UPLOAD_LEN;i++)
            {
                 upload_store_buf[i] = 0xFF;//先清空发送缓存，后期就不用再补0xFF
            }
            if(get_com_rx_num_copy() > 0)
            {
				temp_rxbuf_rcount = com_rxbuf_rcount_copy.datas - com_rxbuf_rcount_copy.datas%45;
                upload_store_count = 0;
                step_store_read = JX_STATE_S1;//进入下一步读取数据
            }
            else if(get_com_rx_num_copy() == 0) //无用水记录时,回复一条当前流量,间隔流量设置一条0
            {
                RtccGetDateTime(&s_temp_store_Time);
                u_temp_store_data.Stru.StepUnion.StepData.Day = s_temp_store_Time.s.day;
                u_temp_store_data.Stru.StepUnion.StepData.Hour = s_temp_store_Time.s.hour;
                u_temp_store_data.Stru.StepUnion.StepData.Minute = s_temp_store_Time.s.minute;
                u_temp_store_data.Stru.StepUnion.StepData.StepTime = miji_interval_time;
                u_temp_store_data.Stru.StepUnion.StepData.StepFlowData.gp22_float = 0;
                RTCC_To_Float  temp_flow;

                temp_rxbuf_rcount = com_rxbuf_rcount_copy.datas;
                temp_flow.gp22_u32 = Gp30CheckDataApp(2);
                upload_store_buf[0] = u_temp_store_data.Stru.StepUnion.StepData.Day;
                upload_store_buf[1] = u_temp_store_data.Stru.StepUnion.StepData.Hour;
                upload_store_buf[2] = u_temp_store_data.Stru.StepUnion.StepData.Minute;
                upload_store_buf[3] = u_temp_store_data.Stru.StepUnion.StepData.StepTime;
				
                upload_store_buf[4] = u_temp_store_data.Stru.StepUnion.StepData.StepFlowData.buf[0];
                upload_store_buf[5] = u_temp_store_data.Stru.StepUnion.StepData.StepFlowData.buf[1];
                upload_store_buf[6] = u_temp_store_data.Stru.StepUnion.StepData.StepFlowData.buf[2];
                upload_store_buf[7] = u_temp_store_data.Stru.StepUnion.StepData.StepFlowData.buf[3];

                upload_store_buf[360] = temp_flow.buf[0];
                upload_store_buf[361] = temp_flow.buf[1];
                upload_store_buf[362] = temp_flow.buf[2];
                upload_store_buf[363] = temp_flow.buf[3];
                return SUCCESS;// 在此就返回完成
            }
            break;
        }
        case JX_STATE_S1://读取基数据
        {
            if(TASK_IDLE == EepIfIdle())//(temp_rxbuf_rcount%COM_RXBUF_MAX)/45=第2a=42条开始,导致第43、44条数据使用了新循环的‘1’‘2’‘3’...条的基数据
            {
                EepRead(EEP_ADDR_JX_STORE_BASE_START + ((temp_rxbuf_rcount%COM_RXBUF_MAX)/45)*4, &upload_store_buf[360], 4, &s_temp_store_ee_msg);
                step_store_read = JX_STATE_S2;
            }
            break;
        }
        case JX_STATE_S2://读间隔数据
        {
            if(TASK_IDLE == EepIfIdle())
            {
				//判断基数据的有效性
				temp_base_flow.buf[0] = upload_store_buf[360];
				temp_base_flow.buf[1] = upload_store_buf[361];
				temp_base_flow.buf[2] = upload_store_buf[362];
				temp_base_flow.buf[3] = upload_store_buf[363];
				if((temp_base_flow.gp22_float > 10000000)||(temp_base_flow.gp22_float < 0)||(temp_base_flow.gp22_u32 == 0Xffffffff))//数据无效
				{
					//数据无效，上报当前数据，且初始化读写指针
					com_rxbuf_wcount.datas = 0;
					com_rxbuf_rcount.datas = 0;
					com_rxbuf_wcount_copy.datas = 0;
					com_rxbuf_rcount_copy.datas = 0;
					StoreRWIndex_2_Ram();
           
					temp_rxbuf_rcount = com_rxbuf_rcount_copy.datas;
					temp_base_flow.gp22_u32 = Gp30CheckDataApp(2);
					upload_store_buf[0] = u_temp_store_data.Stru.StepUnion.StepData.Day;
					upload_store_buf[1] = u_temp_store_data.Stru.StepUnion.StepData.Hour;
					upload_store_buf[2] = u_temp_store_data.Stru.StepUnion.StepData.Minute;
					upload_store_buf[3] = u_temp_store_data.Stru.StepUnion.StepData.StepTime;

					upload_store_buf[4] = u_temp_store_data.Stru.StepUnion.StepData.StepFlowData.buf[0];
					upload_store_buf[5] = u_temp_store_data.Stru.StepUnion.StepData.StepFlowData.buf[1];
					upload_store_buf[6] = u_temp_store_data.Stru.StepUnion.StepData.StepFlowData.buf[2];
					upload_store_buf[7] = u_temp_store_data.Stru.StepUnion.StepData.StepFlowData.buf[3];

					upload_store_buf[360] = temp_base_flow.buf[0];
					upload_store_buf[361] = temp_base_flow.buf[1];
					upload_store_buf[362] = temp_base_flow.buf[2];
					upload_store_buf[363] = temp_base_flow.buf[3];
				
					step_store_read = JX_STATE_S5;
				}
				else
				{
					if(temp_rxbuf_rcount < com_rxbuf_wcount_copy.datas)
					{
						EepRead(EEP_ADDR_JX_STORE_STEP_START + (temp_rxbuf_rcount%COM_RXBUF_MAX)*8, &upload_store_buf[8*upload_store_count], 8, &s_temp_store_ee_msg);
						step_store_read = JX_STATE_S3;
					}
					else
					{
						step_store_read = JX_STATE_S4;
					}
				}
            }
            break;
        }
        case JX_STATE_S3:
        {
            if(TASK_IDLE == EepIfIdle())
            {
                temp_rxbuf_rcount++;
                upload_store_count++;
                if(upload_store_count < 45)
                {
                    step_store_read = JX_STATE_S2; 
                }
                else
                {
                     step_store_read = JX_STATE_S4;
                }
            }
            break;
        }
        case JX_STATE_S4:
        {
            if(TASK_IDLE == EepIfIdle())
            {
                step_store_read = JX_STATE_S0;
                return SUCCESS; //返回完成
            }
            break;
        }
		//数据无效,重新初始化读，写指针
		case JX_STATE_S5:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				EepWrite(com_rxbuf_wcount.data_buf, EEP_ADDR_JX_STORE_WRITE, 4, &s_temp_store_ee_msg);
				step_store_read = JX_STATE_S6;
			}
		    break;
		}
		case JX_STATE_S6:
		{
			if(TASK_IDLE == EepIfIdle())
			{
				EepWrite(com_rxbuf_rcount.data_buf, EEP_ADDR_JX_STORE_READ, 4, &s_temp_store_ee_msg);
				step_store_read = JX_STATE_S4;
			}
		    break;
		}
		
        default:
        {
            break;
        }
    }
    return ERROR;
}

/**************************************************************************
函数名：JiangXiStore_Machine
说  明：江西定制规约数据存储状态机;
      当流速大于0时，启动间隔存储.
      间隔存储时间间隔可设置，即，0-30s; 1-1分钟; 2-2分钟 ……
***************************************************************************/	
void JiangXiStore_Machine(void)
{
    switch(step_jx_store)
    {
        case JX_STATE_S0:
       {
           if(Gp30CheckDataApp(1) > 0)//判断流速
           {
               old_miji_interval_flow.gp22_u32 = Gp30CheckDataApp(2);
			   u_temp_store_data.Stru.BaseFlowData.datas = Gp30CheckDataApp(2);//基础流量暂存,要放在间隔流量变化之前
               miji_interval_time = FrameCheckParaApp(FramePara_StoreFrequency); 
               if(miji_interval_time == 0x00)				
               {
                  RtccSetTimer(RTCC_MIJI_WAIT_TIME,30);//默认30s存储间隔
               }
               else
               {
                   RtccSetTimer(RTCC_MIJI_WAIT_TIME,60*miji_interval_time);
               }
               step_jx_store = JX_STATE_S1;
           }//8900+5000+1000+2980+7000+6200+2000=11.4 3.2+2.2=5.4 5.8
           break;
       }
       case JX_STATE_S1:
       {
           if(RtccCheckTimer(RTCC_MIJI_WAIT_TIME) == 0)
           {
              //间隔流量存储格式整理：时间格式
              RtccGetDateTime(&s_temp_store_Time);
              u_temp_store_data.Stru.StepUnion.StepData.Day = s_temp_store_Time.s.day;
              u_temp_store_data.Stru.StepUnion.StepData.Hour = s_temp_store_Time.s.hour;
              u_temp_store_data.Stru.StepUnion.StepData.Minute = s_temp_store_Time.s.minute;
              u_temp_store_data.Stru.StepUnion.StepData.StepTime = miji_interval_time;
              //间隔流量存储格式整理：流量格式
              new_miji_interval_flow.gp22_u32 = Gp30CheckDataApp(2);
              u_temp_store_data.Stru.StepUnion.StepData.StepFlowData.gp22_float = (new_miji_interval_flow.gp22_float - old_miji_interval_flow.gp22_float)/100;
              step_jx_store = JX_STATE_S2;
           }
           break;
        }
        case JX_STATE_S2:
        {
            if(write_com_rx_data(&u_temp_store_data) == SUCCESS)//流量数据存储
            {
                step_jx_store = JX_STATE_S0;
            }
            break;
        }
        default:
        {
            step_jx_store = JX_STATE_S0;
            break;
        }
    }
}
/**
计算需要上报的包数
*/
u8 JiangXiStore_UploadPacks(void)
{
	u16 temp_upload_record_cnts=0;
	u8  temp_upload_total_packs=0;
	if((com_rxbuf_wcount.datas == 0xFFFFFFFF)||(com_rxbuf_rcount.datas == 0xFFFFFFFF))//判断指针有效性
	{
		com_rxbuf_wcount.datas = 0;
		com_rxbuf_rcount.datas = 0;
	}
	else
	{
		if (com_rxbuf_wcount.datas >= com_rxbuf_rcount_copy.datas )
		{
			if((com_rxbuf_wcount.datas - com_rxbuf_rcount.datas) > COM_RXBUF_MAX)
			{
				com_rxbuf_rcount.datas = (com_rxbuf_wcount.datas - COM_RXBUF_MAX);//6177
			}
		}
		if(com_rxbuf_wcount.datas > (com_rxbuf_rcount.datas + COM_RXBUF_MAX -45))//判断是否覆盖了首包数据的基数据
		{
			com_rxbuf_rcount.datas = com_rxbuf_rcount.datas + (45 - com_rxbuf_rcount.datas%45);//舍去首包的残余条数
		}
	}
	temp_upload_record_cnts = com_rxbuf_wcount.datas - com_rxbuf_rcount.datas;
	temp_upload_total_packs = (temp_upload_record_cnts/45);//计算需要上传的总包数,45条记录一包
	if(temp_upload_record_cnts % 45)
	{
		temp_upload_total_packs += 1;
	}
	if(temp_upload_total_packs>64)//最大64包
	{
		temp_upload_total_packs = 64;
	}
    return temp_upload_total_packs;
}
/**
计算读写指针的CRC校验,
*/
u16 getCRC_HisIndex(u32 data1, u32 data2)
{
    u8 tempBuf[8];

	tempBuf[0] = data1&0xFF;
	tempBuf[1] = (data1>>8)&0xFF;
	tempBuf[2] = (data1>>16)&0xFF;
	tempBuf[3] = (data1>>24)&0xFF;
	tempBuf[4] = data2&0xFF;
	tempBuf[5] = (data2>>8)&0xFF;
	tempBuf[6] = (data2>>16)&0xFF;
	tempBuf[7] = (data2>>24)&0xFF;
	return CRC16(tempBuf,8); 
}

void StoreRWIndex_2_Ram(void)
{
	jx_com_rxbuf_wcount_copy = com_rxbuf_wcount.datas;
	jx_com_rxbuf_rcount_copy = com_rxbuf_rcount.datas;
	jx_com_rxbuf_check_copy = getCRC_HisIndex(jx_com_rxbuf_wcount_copy, jx_com_rxbuf_rcount_copy);
}

/***********************************************
江西规约发送数据组帧
***********************************************/
void NB_JX_Frame_Upload_Data(u8* psend_buf, u16* psend_len, u8 totalPack, u8 curPack)
{
	u16 tmp_i =0;
	
	s16 s_temp_data=0;
	MET_STATE temp_state; //状态字

	//u16 s_g_data_len = PACK_NUM_GPRS_TOTAL;
	UNION_2U8TOU16 temp_data;
	
	*psend_buf++ = totalPack;//需要上报总报数;
	*psend_buf++ = curPack;//当前上报的包号;
	for (tmp_i = 0; tmp_i < 360; tmp_i++) //8条历史数据
	{
		*psend_buf++ = upload_store_buf[tmp_i];
	}
  
	//电压值
	temp_data.datas  =FramHexToBcd16(PowerCheckValueSys());
	*psend_buf++ = temp_data.datas_map.data0;
	*psend_buf++ = temp_data.datas_map.data1;
    //流量
	*psend_buf++ = upload_store_buf[360];
	*psend_buf++ = upload_store_buf[361];
	*psend_buf++ = upload_store_buf[362];
	*psend_buf++ = upload_store_buf[363];
    //信号强度
	NbiotGetRSRP(&s_temp_data);
	temp_data.datas = -s_temp_data;
	*psend_buf++ =temp_data.data_buf[0];
	*psend_buf++ =temp_data.data_buf[1];	
////新增附网时间 
	*psend_buf++=GetUploadConnectTime_JX(0)&0xFF; 
	*psend_buf++=(GetUploadConnectTime_JX(0)>>8)&0xFF;		 
	*psend_buf++= GetUploadConnectTime_JX(1)&0xFF; 
	*psend_buf++= (GetUploadConnectTime_JX(1)>>8)&0xFF;
//状态字
	temp_state = FrameGetByte(); 
	*psend_buf++ = temp_state.stat0;
	*psend_buf++ = temp_state.stat1;
	*psend_buf++ = temp_state.stat2;
	*psend_buf++ = 0x02; 
	*psend_len = PACK_NUM_GPRS_TOTAL;
}

u8 JiangXiStore_IfSleep(void)
{
    if((step_jx_store == JX_STATE_S0) ||(step_jx_store == JX_STATE_S1) )
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}


#endif






