#include "UWater_frame_app.h"
#include "UWater_eeprom_driver.h"
#include "UWater_rtcc_app.h"
#include "UWater_power_app.h"
#include "UWater_rtcc_driver.h"
#include "UWater_gp22_app.h"
#include "UWater_Nb_SS_DataManage.h"
#include "UWater_DataTools_App.h"
#include "UWater_key_app.h"
#include "UWater_pt_app.h"
#include "UWater_DebugPrint.h"
#include "UWater_ir_app.h"
#include "UWater_Valve_app.h"
#include <string.h>
#include "nb_uart_driver_bc25.h"	
#include "nb_driver_bc25.h"
#include "UWater_JX_Store_App.h"

STRU_UPLOAD_FRAME upload_frame; //coap上报数据帧
//获取上报信号值,电池电压
static void UploadGetInfo(void)
{
	s16 s_temp_data=0;
	
	NbiotGetSNR(&s_temp_data);
	if(s_temp_data<0)
	{
	    upload_frame.data.UploadInfo.metinfo.snr_val =(-s_temp_data);
		upload_frame.data.UploadInfo.metinfo.snr_val |= 0x8000;
	}
	else
	{
	    upload_frame.data.UploadInfo.metinfo.snr_val = s_temp_data;
	}
	//
	NbiotGetECL(&s_temp_data);
	if(s_temp_data<0)
	{
		upload_frame.data.UploadInfo.metinfo.ecl_val |= (-s_temp_data)&0x8000;
	}
	else
	{
	    upload_frame.data.UploadInfo.metinfo.ecl_val = s_temp_data;
	}
	//
	NbiotGetRSRP(&s_temp_data);
	upload_frame.data.UploadInfo.metinfo.RSRP_val = -s_temp_data;
	NbiotGetCSQ(&upload_frame.data.UploadInfo.metinfo.single_val); 
	upload_frame.data.UploadInfo.metinfo.single_val = (upload_frame.data.UploadInfo.metinfo.single_val / 10)*16 + (upload_frame.data.UploadInfo.metinfo.single_val%10);
	//获取电压值
	upload_frame.data.UploadInfo.metinfo.bat_val.u16_data =HexToBcd16(PowerCheckValueSys());															
	upload_frame.data.UploadInfo.metinfo.bat_threshold_val = 0x03;//欠压阈值3v
}
//更新数据包中的当前流量和信息状态
void Upload_RenewCurrentData(void)
{
	MET_STATE temp_state; //状态字
	u8 temp1 = 0;

	tmp_float.gp22_u32  = Gp30CheckDataApp(2);
	upload_frame.data.CurrentFlow.datas = (u32)(tmp_float.gp22_float*10);//当前累计流量
	upload_frame.data.CurrentFlowForward.datas = upload_frame.data.CurrentFlow.datas;//当前累计正向流量
	tmp_float.gp22_u32 = Gp30CheckDataApp(3);//当前累积逆向流量
	upload_frame.data.CurrentFlowReverse.datas = (u32)(tmp_float.gp22_float*10);
	//获取:日最高流量(4字节),日最高流量持续时间(2字节),日最高流量开始时间(8字节,预留)	
	RtccCheck_Hight_Flow(&hight_flow);			
	for(temp1 = 0; temp1 <14 ;temp1++)
	{
		upload_frame.data.DailyHighestFlowAndTime[temp1] = *(hight_flow+temp1);
	}
	//获取:rsrp(2B),电池电压(2B),信噪比(2B),无线信号覆盖等级ecl(2B),CSQ(1B),电压阈值(1B)
	UploadGetInfo();//UploadGetReportInfo(upload_frame.data.UploadInfo.byte);
	//获取间隔流量:2B(间隔流量)X2(次/h)X24h=96B
	/*
	上报时通过UPLOAD_DATA_READ_HIS获取，0点存储时通过UPLOAD_DATA_STEP_FLOW获取
	*/
	//上报时间
	RtccGetDateTime(&Rtcc_time);//上报时间
	upload_frame.data.UploadTime[0] = Rtcc_time.s.second;
	upload_frame.data.UploadTime[1] = Rtcc_time.s.minute;
	upload_frame.data.UploadTime[2] = Rtcc_time.s.hour;
	upload_frame.data.UploadTime[3] = Rtcc_time.s.day;
	upload_frame.data.UploadTime[4] = Rtcc_time.s.month;
	upload_frame.data.UploadTime[5] = Rtcc_time.s.year; 
	upload_frame.data.UploadTime[6] = 0x20;
	//间隔流量起始时间	
	upload_frame.data.StepFlowStartTime[0]= 0x00;
	upload_frame.data.StepFlowStartTime[1]= 0x00;
	upload_frame.data.StepFlowStartTime[2]= 0x00;
	upload_frame.data.StepFlowStartTime[3]= g_date_jiange.s.day ;
	upload_frame.data.StepFlowStartTime[4]= g_date_jiange.s.month;
	upload_frame.data.StepFlowStartTime[5]= g_date_jiange.s.year;        
	upload_frame.data.StepFlowStartTime[6]= 0x20; 
	//状态字
	temp_state = FrameGetByte();
	upload_frame.data.StateBytes[0] = temp_state.stat0;
	upload_frame.data.StateBytes[1] = temp_state.stat1;
	upload_frame.data.StateBytes[2] = temp_state.stat2;
	upload_frame.data.StateBytes[3] = temp_state.stat3;
	upload_frame.data.StateBytes[4] = temp_state.stat4;
	//累计活跃时间
	upload_frame.data.DailyActiveTime.datas = g_activity_times;
}







