
#ifndef __UWater_SY_STORE_APP_H
#define __UWater_SY_STORE_APP_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "sys_config.h"
#if PRODUCT_SY
#include "UWater_frame_app.h"
#include "UWater_Rtcc_driver.h"

#define STORE_SIZE_DATA_CIRCLE            16  //周期数据单条长度
#define STORE_SIZE_DATA_FROZEN            32  //冻结数据单条长度

#define EEP_DATA_LEN_CIRCLE   (STORE_SIZE_DATA_CIRCLE*24*6)//2352//周期数据存储和指针长度
	
#define EEP_ADDR_SY_STORE_CIRCLE            FRAME_SHENYANG_STORE_ADD //0x7300~0x7C00  周期数据16*24*6=2304=0x900
#define EEP_ADDR_SY_STORE_INDEX_CIRCLE     (EEP_ADDR_SY_STORE_CIRCLE + STORE_SIZE_DATA_CIRCLE*24*6) //数据存储指针_周期数据_48BYTE
#define EEP_ADDR_SY_STORE_INDEX_DAILY      (EEP_ADDR_SY_STORE_INDEX_CIRCLE + 66)////数据存储指针_日冻结数据_1BYTE
#define EEP_ADDR_SY_STORE_INDEX_MONTH      (EEP_ADDR_SY_STORE_INDEX_DAILY + 1)////数据存储指针_月冻结数据_1BYTE
#define EEP_ADDR_SY_STORE_INDEX_YEAR       (EEP_ADDR_SY_STORE_INDEX_MONTH + 1)////数据存储指针_年冻结数据_1BYTE
#define EEP_ADDR_SY_FIRST_PWON             (EEP_ADDR_SY_STORE_INDEX_YEAR + 1)
//参数存储地址
#define EEP_ADDR_SY_TLVDATA                (EEP_ADDR_SY_FIRST_PWON+1)  //104字节

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define EEP_DATA_LEN_FROZEN   (STORE_SIZE_DATA_FROZEN*31)//995//冻结数据存储和指针长度
	
#define EEP_ADDR_SY_STORE_FROZEN_DAILY      EEP_RTCC_APP_HISTORY_ADDR //0x440 日冻结数据(32*7)
#define EEP_ADDR_SY_STORE_FROZEN_MONTH     (EEP_ADDR_SY_STORE_FROZEN_DAILY + STORE_SIZE_DATA_FROZEN*7)  //月冻结数据(32*18)
#define EEP_ADDR_SY_STORE_FROZEN_YEAR      (EEP_ADDR_SY_STORE_FROZEN_MONTH + STORE_SIZE_DATA_FROZEN*18) //年冻结数据(32*6)

#define SY_FIRST_POWERON_FLAG  0x56

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

//数据存储指针参数名称
typedef enum
{
    SY_StoreCurIndex_Daily = 0,//日冻结存储指针
	SY_StoreCurIndex_Month = 1,//月冻结存储指针
	SY_StoreCurIndex_Year  = 2//月冻结存储指针
} SY_ParaTypeDef;

//周期数据结构体
typedef union 
{
	struct
	{
		u32 CurrentFlow;//正向累计流量
		u32 CurrentFlowReverse;//负向累计流量
		u32 InstantaneousFlow;//瞬时流量
		u16 Presure;//压力
		u16 Temprature;//温度
	}Data;
	u8 data_buf[STORE_SIZE_DATA_CIRCLE];
}UNION_SY_STORE_CIRCLE;

//周期数据存储索引结构体
typedef union 
{
	struct
	{
		DateTime CircleDataTime;//年月日时(分秒),分秒为0,使用DateTime格式而不是DateTime_noweek格式,已便于计算
		u8 CircleCntStart;
		u8 CircleCntEnd;
		u8 ReadStart; //读取周期数据时使用
		u8 ReadEnd;
	}Data;
	u8 data_buf[11];
}TYPE_CIRCLE_INDEX;

typedef union 
{
	TYPE_CIRCLE_INDEX CircleIndex[6];
	u8 data_buf[66];
}UNION_SY_STORE_CIRCLE_INDEX;

//数据存储指针
#define SIZE_SY_STORE_INDEX  (11*6+3+1)//70
typedef union 
{
	struct
	{
		UNION_SY_STORE_CIRCLE_INDEX Circle;//周期数据存储指针
		u8 Daily;//日冻结数据存储指针
		u8 Month;//月冻结数据存储指针
		u8 Year;//年冻结数据存储指针
		u8 firstPowerOn;//第一次上电标志
	}Data;
	u8 data_buf[SIZE_SY_STORE_INDEX];
}UNION_SY_STORE_INDEX;

//冻结数据结构体
/**
日期	3
时间	3
正向累计流量	4
负向累计流量	4
峰值流量	4
峰值流量时间	3
电池电压	6
水表状态	1
水表故障	1
阀门状态	1
预留	1
校验和	1
总字节数	32
*/
typedef union 
{
	struct
	{
		DateTime_noweek FrozenDataTime; //日期时间      6                         
		u32 CurrentFlow;//正向累计流量           4
		u32 CurrentFlowReverse;//负向累计流量    4
		u32 PeakCurrentFlow;//峰值流量	         4
		u8  PeakCurrentFlowTime[3];//时分秒      3
		u8  BatPower[6];//电池电压	             6
		u8  MeterState;//水表状态	             1
		u8  MeterFault;//水表故障	             1
		u8  ValveState;//阀门状态	             1
		u8  Reverse;   //预留                    1
		u8 CheckSum;//                           1
	}Data;
	u8 data_buf[STORE_SIZE_DATA_FROZEN];
}UNION_SY_STORE_FROZEN;

/**TLV 数据格式的参数**/
typedef enum
{
	TagWarningId = 0, //即时报警ID
	TagOverFlowCurrent = 1, //过流报警的阈值，单位0.1L/H
	TagOverFlowMinute = 2, //持续过流时间
	TagReverseFlowCurrent = 3, //反流报警的阈值，单位0.1L/H
	TagReverseFlowMinute = 4, //持续反流报警时间
	TagLeakFlowMinute = 5, //漏水报警时间
	TagPowerLow001V = 6, //电压报警阈值
	TagNoWaterMinute = 7, //空管报警时间
	TagValveState    = 8, //阀门状态
	TagUploadStartTime = 12,   //数据上报起始时间a
	TagCircleData5Minutes = 13,//周期数据时间间隔a
	TagDataFrozenTimeDailay = 14,//日冻结时间
	TagDataFrozenTimeMonth = 15,   //月冻结时间
	TagUploadRetryTimes = 16,//上报重发次数
	TagUploadRetryDelayMinite = 17,//重发延时时间
	TagUploadIntervalHour = 18,//数据上报时间间隔a
	TagServerIpPort = 21,    //服务器地址a
	TagAPN = 22,    //APN信息
	TagWorkTime = 31    //水表运行时间
	
}TypeDef_TLVTag;

typedef enum
{
	LenWarningId = 2, //即时报警ID-----------------------------2
	LenOverFlowCurrent = 4, //过流报警的阈值，单位0.1L/H-------4
	LenOverFlowMinute = 1, //持续过流时间----------------------1
	LenReverseFlowCurrent = 4, //反流报警的阈值，单位0.1L/H----4
	LenReverseFlowMinute = 1, //持续反流报警时间---------------1
	LenLeakFlowMinute = 1, //漏水报警时间----------------------1
	LenPowerLow001V = 2, //电压报警阈值------------------------2
	LenNoWaterMinute = 1, //空管报警时间-----------------------1
	LenValveState    = 7, //阀门状态---------------------------7
	LenUploadStartTime = 4,    //数据上报起始时间a-------------4
	LenCircleData5Minutes = 1, //周期数据时间间隔a-------------1
	LenDataFrozenTimeDailay = 1, //日冻结时间------------------1
	LenDataFrozenTimeMonth = 2,    //月冻结时间----------------2
	LenUploadRetryTimes = 1, //上报重发次数--------------------1
	LenUploadRetryDelayMinite = 1, //重发延时时间--------------1
	LenUploadIntervalHour = 1, //数据上报时间间隔a-------------1
	LenServerIpPort = 32,    //服务器地址a--------------------32
	LenAPN = 32,    //APN信息---------------------------------32
	LenWorkTime = 6     //水表运行时间-------------------------6
}TypeDef_TLVDataLen;

//水表参数定义
#define TLV_VALUE_LEN  104
typedef union 
{
	struct 
	{
		u8  ValueWarningId[2];//即时报警ID--------------------------2
		u8  ValueOverFlowCurrent[4];//过流报警的阈值，单位0.1L/H----4
		u8  ValueOverFlowMinute;//持续过流时间----------------------1
		u8  ValueReverseFlowCurrent[4];//反流报警的阈值，单位0.1L/H-4
		u8  ValueReverseFlowMinute;//持续反流报警时间---------------1
		u8  ValueLeakFlowMinute;//漏水报警时间----------------------1
		u8  ValuePowerLow001V[2];//电压报警阈值---------------------2
		u8  ValueNoWaterMinute;//空管报警时间-----------------------1
		u8  ValueValveState[7];//阀门状态---------------------------7
		u8  ValueUploadStartTime[4];//数据上报起始时间a-------------4
		u8  ValueCircleData5Minutes;//周期数据时间间隔a-------------1
		u8  ValueDataFrozenTimeDailay;//日冻结时间------------------1
		u8  ValueDataFrozenTimeMonth[2];//月冻结时间----------------2
		u8  ValueUploadRetryTimes;//上报重发次数--------------------1
		u8  ValueUploadRetryDelayMinite;//重发延时时间--------------1
		u8  ValueUploadIntervalHour;//数据上报时间间隔a-------------1
		u8  ValueServerIpPort[32];//服务器地址a--------------------32
		u8  ValueAPN[32];//APN信息---------------------------------32
		u8  ValueWorkTime[6];//水表运行时间-------------------------6
	}s;
	u8 data_buf[TLV_VALUE_LEN];
}Type_TLVValue;

typedef enum
{
	AddrWarningId = 0, //即时报警ID-----------------------------2
	AddrOverFlowCurrent = 2, //过流报警的阈值，单位0.1L/H-------4
	AddrOverFlowMinute = 6, //持续过流时间----------------------1
	AddrReverseFlowCurrent = 7, //反流报警的阈值，单位0.1L/H----4
	AddrReverseFlowMinute = 11,//持续反流报警时间---------------1
	AddrLeakFlowMinute = 12,//漏水报警时间----------------------1
	AddrPowerLow001V = 13,//电压报警阈值------------------------2
	AddrNoWaterMinute = 15,//空管报警时间-----------------------1
	AddrValveState    = 16,//阀门状态---------------------------7
	AddrUploadStartTime = 23,   //数据上报起始时间a-------------4
	AddrCircleData5Minutes = 27,//周期数据时间间隔a-------------1
	AddrDataFrozenTimeDailay = 28,//日冻结时间------------------1
	AddrDataFrozenTimeMonth = 29,   //月冻结时间----------------2
	AddrUploadRetryTimes = 31,//上报重发次数--------------------1
	AddrUploadRetryDelayMinite = 32,//重发延时时间--------------1
	AddrUploadIntervalHour = 33,//数据上报时间间隔a-------------1
	AddrServerIpPort = 34,    //服务器地址a--------------------32
	AddrAPN = 66,    //APN信息---------------------------------32
	AddrWorkTime = 98    //水表运行时间-------------------------6
}TypeDef_TLVDataAddr;

typedef struct 
{
	const TypeDef_TLVTag Tag;
	const TypeDef_TLVDataLen Len;
	TypeDef_TLVDataAddr ValueAddr; 
	u8 EditFlag;//是否修改,0不修改,  1修改
} TypeDef_TLVStruct; 

#define TLV_STR_BUF_LEN   19
static TypeDef_TLVStruct TlvStrBuf[TLV_STR_BUF_LEN]={
	{TagWarningId,               LenWarningId,                AddrWarningId,          0},
	{TagOverFlowCurrent,         LenOverFlowCurrent,          AddrOverFlowCurrent,    0},
	{TagOverFlowMinute,          LenOverFlowMinute,           AddrOverFlowMinute,     0},
	{TagReverseFlowCurrent,      LenReverseFlowCurrent,       AddrReverseFlowCurrent, 0},
	{TagReverseFlowMinute,       LenReverseFlowMinute,        AddrReverseFlowMinute,  0},
	{TagLeakFlowMinute,          LenLeakFlowMinute,           AddrLeakFlowMinute,     0},
	{TagPowerLow001V,            LenPowerLow001V,             AddrPowerLow001V,       0},
	{TagNoWaterMinute,           LenNoWaterMinute,            AddrNoWaterMinute,      0},
	{TagValveState,              LenValveState,               AddrValveState,         0},
	{TagUploadStartTime,         LenUploadStartTime,          AddrUploadStartTime,    0},
	{TagCircleData5Minutes,      LenCircleData5Minutes,       AddrCircleData5Minutes, 0},
	{TagDataFrozenTimeDailay,    LenDataFrozenTimeDailay,     AddrDataFrozenTimeDailay,  0},
	{TagDataFrozenTimeMonth,     LenDataFrozenTimeMonth,      AddrDataFrozenTimeMonth,   0},
	{TagUploadRetryTimes,        LenUploadRetryTimes,         AddrUploadRetryTimes,      0},
	{TagUploadRetryDelayMinite,  LenUploadRetryDelayMinite,   AddrUploadRetryDelayMinite,0},
	{TagUploadIntervalHour,      LenUploadIntervalHour,       AddrUploadIntervalHour,    0},
	{TagServerIpPort,            LenServerIpPort,             AddrServerIpPort,          0},
	{TagAPN,                     LenAPN,                      AddrAPN,                   0},
	{TagWorkTime,                LenWorkTime,                 AddrWorkTime,              0}
};
/*报警触发上报标志位
0	流量超限
1	空管报警
2	反流报警
3	漏水报警
4	低电压报警
5	温度超范围
6	上盖被打开
7	阀门动作
8-11	预留
12-15	厂家自定义
*/
typedef enum
{
	UPLOAD_OVER_FLOW = 0,//流量超限--
	UPLOAD_NO_WATER, //空管报警--
	UPLOAD_REVERSE_FLOW, //反流报警--
	UPLOAD_LEAK_FLOW,//漏水报警--
	UPLOAD_LOW_POWER,//低电压报警---
	UPLOAD_TEMP_OVER,//温度超范围--
	UPLOAD_SHELL_OPEN,//上盖被打开
	UPLOAD_VALVE_CHANGE//阀门动作--
}TypeDef_UploadWarning;
/*状态号*/
typedef enum{	
	SY_STATE_S0 = 0,
	SY_STATE_S1,
	SY_STATE_S2,
	SY_STATE_S3,
	SY_STATE_S4,
	SY_STATE_S5,
	SY_STATE_S6,
	SY_STATE_S7,
	SY_STATE_S8,
	SY_STATE_S9,
	SY_STATE_S10,
	SY_STATE_S11,
	SY_STATE_S12,
	SY_STATE_S13,
	SY_STATE_S20,
	SY_STATE_S21,
	SY_STATE_S22,
	SY_STATE_S30,
	SY_STATE_S31,
	SY_STATE_S32,
	SY_STATE_S40,
	SY_STATE_S41,
	SY_STATE_S42,
	SY_STATE_S50,
	SY_STATE_S51,
	SY_STATE_S52,
	SY_STATE_S60,
	SY_STATE_S61,
	SY_STATE_S62,
	SY_STATE_S70,
	SY_STATE_S71,
	SY_STATE_S72,
	SY_STATE_S80,
	SY_STATE_S81,
	SY_STATE_S82,
	SY_STATE_S90,
	SY_STATE_S91,
	SY_STATE_S92
}TYP_STATE_SY;




void ShenYang_Store_InitApp(void);
void ShenYang_Store_Machine(void);
void ShenYang_SOE_Machine(void);
u8 get_SY_StorePara(SY_ParaTypeDef paraType);
u8 ShenYangStore_IfSleep(void);
//int NB_SY_Frame_Upload_Warning(u8 *pBuf);

u8 getSY_StoreCircleIndex(void);
u8 getSY_CircleUploadPackets(void);
u8 getSY_ThisCirclePacket_Index(u8 current_packet);
int NB_SY_Frame_Upload_Data(u8 index, u8 *pBuf);
int NB_SY_Frame_Upload_CurrentData(u8 *pBuf);
void SY_GetCircleDateTime(DateTime *pDT, u8 index, u8 UploadOrRead);
u8 SY_GetCircleReadStartEnd(u8 index, u8 StartOrEnd);
int ShenYang_CircleDataStore_ClearUploadIndex(u8 index);
void ShenYang_TlvDataStoreMachine(void);
//计算往前几天的日期和时间,带周
DateTime getBackDateTime(u8 backDays);
//计算往前几天的日期和时间,不带周
DateTime_noweek getBackDateTimeNoWeek(u8 backDays);
u8 getSY_CircleDataPackets(u8 readDays, u8 *pIndexBuf);
u8 checkWarningUploadState(TypeDef_UploadWarning tWarningType);
u8 GetTLVData(u8 Tag, u8 Len, u8* pValue);
u8 SetTLVData(u8 Tag, u8 Len, u8* pValue);
u8 getTlvTagIndex(u8 inTag);
u8 getWaterState(void);

#endif

#ifdef __cplusplus
}
#endif

#endif /* __UWater_JIANGXISTORE_APP_H */

/***************************************************************END OF FILE****/
