/*
 * File:   __GP22LoRa_RTCC_APP_H
 * Author:
 *
 * Created on：2015-5-18
 */

#ifndef __UWater_RTCC_APP_H
#define	__UWater_RTCC_APP_H

#include "type.h" 
#include "UWater_rtcc_driver.h"
#include "UWater_frame_app.h"

/************************************RTCC应用层的状态信息************************************************/
//#define HEATING_STATE                               1//采暖季状态信息 BIT0
#define RTCC_ERROR_STATE                            2//RTCC异常 BIT1

#define RTCC_APP_SWITCH_ALRM_S0     0
#define RTCC_APP_SWITCH_ALRM_S1     1
#define RTCC_APP_SWITCH_ALRM_S2     2
#define RTCC_APP_SWITCH_ALRM_S3     3
#define RTCC_APP_SWITCH_ALRM_S4     4

/************************************RTCC应用层的消息信息************************************************/
//#define GP22_SAMPLE_MSG                             1//GP22采样消息 BIT0

#define RTCC_FIRST_FLAG                             0x88
#define RTCC_TIME_ERROR_HOUR                        ((long)100*3600)
#define VALVE_CL_UPLOAD_TIMES                       24//上传次数
//#define EE_FIRST_USE_ADDR                                   0xB8
//
//#define EE_ERROR_HISTORY_ADDR                               (EE_FIRST_USE_ADDR+sizeof(RTCC_EE_DATA))
//#define EE_DAY_HISTORY_ADDR                                 (EE_ERROR_HISTORY_ADDR+sizeof(RTCC_HISTORY_DATA))
//#define EE_MONTH_HISTORY_ADDR                               (EE_DAY_HISTORY_ADDR+14*31*sizeof(RTCC_HISTORY_DATA))
//#define EE_OTHER_ADDR                                       (EE_MONTH_HISTORY_ADDR+24*sizeof(RTCC_HISTORY_DATA))

//#define EE_FIRST_USE_ADDR                                   0x100  //累计工作时间+上电标志字，，EEPROM区域：0x100-0x10F  共32字节

//#define EE_MONTH_HISTORY_ADDR                               0x120  //EEPROM区域： 0x120-0x30F 共496字节
//#define EE_DAY_HISTORY_ADDR                                 0x310  //EEPROM区域： 0x310-0x78F 共1152字节
//#define EE_END_HISTORY_ADDR                                 0x790  //78F+1
////#define EE_OTHER_ADDR                                       (EE_END_HISTORY_ADDR - EE_MONTH_HISTORY_ADDR)  //1648
//#define EE_OTHER_ADDR                                       (EE_END_HISTORY_ADDR - EE_MONTH_HISTORY_ADDR + 206)  //1648 2015-6-16 连同事件记录一起清
//#define FRAME_NBIot_ADDR_BASE                               (EE_OTHER_ADDR + 130)

#define EE_FIRST_USE_ADDR               EEP_RTCC_APP_USE_ADDR//0x180  //累计工作时间+上电标志字，，EEPROM区域

//#define EE_MONTH_HISTORY_ADDR                               0x200  //EEPROM区域： EEPROM: 0x200-0x43F 576字节(预留)
//#define EE_DAY_HISTORY_ADDR             EEP_RTCC_APP_HISTORY_ADDR  //0x440  //EEPROM区域： 计划26字节*62天 = 1612   EEPROM区域： 0x440-0xC3F 共2048字节

																    //实际存储31天数据，前31天，循环存储，不带时间戳 26字节*31天 = 806字节
//#define EE_END_HISTORY_ADDR                                 0x766   //0x440 - 0x765 

//#define EE_OTHER_ADDR                                       (EE_END_HISTORY_ADDR - EE_MONTH_HISTORY_ADDR + 18)  //(1382 +18) / 200 = 7,18字节凑个数
//#define EE_OTHER_ADDR                                     (EE_END_HISTORY_ADDR - EE_MONTH_HISTORY_ADDR + 206)  //1648 2015-6-16 连同事件记录一起清
#define CLEAR_HIS_BUF_LEN     256//清历史数据缓存长度


#pragma pack(1)

typedef union {

    struct {
        u32 most_high_flow;
        u16 most_high_flow_total_time;
        u32 most_high_flow_begin_time1;
		u32 most_high_flow_begin_time2;
    }s;
    u8 byte[14];
} RTCC_HISTORY_DATA;

typedef union                  //u32与数组
{
    u32   rtcc_u32;
    u8    rtcc_u8[4];
}To_u32_Rtcc;

 typedef union            //强制转化为浮点变量
{
    u32   gp22_u32;
    float gp22_float;
    u8 buf[4];
} RTCC_To_Float;

typedef union 
{
	struct 
	{
			u8 data0;
			u8 data1;
	} datas_map;
	u16 datas;
} RTCC_UNION_2U8TOU16;

#define MINUTE_TO_SECOND  60
#define HOUR_TO_SECOND    3600
#define DAY_TO_SECOND     86400

typedef enum
{
    RtccMsg_Search_Card =0,
	RtccMsg_MiJiCaiJi=1,//密集采集时间段内（开始年月日-结束年月日），4h采集完成MSG
	RtccMsg_DataSave=2,//置每日零点时间到（周期上报数据存储）msg		
    RtccMsg_UploadInTime = 3, //置定时上传时间到MSG
	RtccMsg_DiftofCalibration = 4,//置定时校准MSG
	RtccMsg_Transducer_Check = 5,//置定时检测表计换能器是否异常的MSG
	RtccMsg_ValveActive = 6,//定时活动阀门MSG
	RtccMsg_OtherProtl=7//抛给其他规约的分钟消息
} Rtcc_MsgTypeDef;


u8 RtccCheckMsgApp(Rtcc_MsgTypeDef msg_bit);
void RtccClearMsgApp(Rtcc_MsgTypeDef clear_bit);
void RtccSetMsgApp(Rtcc_MsgTypeDef set_bit);
	
void RtccInitApp(void);
void RtccMachineApp(void);
void RtccResetWorkHourApp(void);
u32 RtccGetWorkHourApp(void);

void RtccStartSampleApp(u8 span);
void RtccStopSampleApp(void);

u8 RtccGetStateApp(void);

u8 RtccIfSleepApp(void);
u16 RtccDayHisAddrApp(void);
u32 RtccJSDataApp(void);

u8 RtccIsError(void);
void RtccClearError(void);
//u8 RtccCheckStateApp();
void Gp30CheckMiJiDataApp(u8 **addr);
void Gp30CheckJianGeDataApp(u8 **addr);
void RtccCheck_MAX_MIN_Temprature(u16 **addr);
//void RtccStartSampleApp(u8 span);
//void RtccStopSampleApp();
u8 Rtcc_Check_Upload(void);
void RtccAppCheckMiJi(DateTime alarm_time);
void RtccAppCheckMostFloat(DateTime alarm_time);

#endif	/* __UWater_RTCC_APP_H */
