#ifndef _SOE_H_
#define _SOE_H_

#include "type.h"
#include <time.h>
#include "UWater_rtcc_driver.h"

#pragma pack(1)

#define  LENGTH_ONE_SOE   			9
#define  TOTAL_NUM_SOE    			100  	//事件最大记录数
#define  SOE_ALL_TYPE     			0xFFFF
#define  SOE_REMARK_NOR   			0   	//SOE备注，普通
//#define  SOE_REMARK_A_PIPE_BAD   	1   	//SOE备注，
//#define  SOE_REMARK_B_PIPE_BAD   	2   	//SOE备注，
//#define  SOE_VALVE_CLOSE   		 	1   	//SOE备注，普关
//#define  SOE_VALVE_CLOSE_LOCK    	2   	//SOE备注，权关
//#define  SOE_VALVE_CLOSE_LOCK    	2   	//SOE备注，权关
#define  SOE_NUM          			10  	//SOE事件同时可以发生10个

#define  EEP_ADDR_SOE_BASE          EEP_SOE_ADDR  //0x1000
#define  EEP_ADDR_SOE_WR_POINT      (EEP_ADDR_SOE_BASE + TOTAL_NUM_SOE*9)
#define EEP_DATA_LEN_SOE  (LENGTH_ONE_SOE*TOTAL_NUM_SOE+1)

#define MAX_NUM_READ_SOE	25			//单次读取SOE事件的最大条数	

enum SOE_TYPE{	
	SOE_START_ID0 = 0x0000,			//开始地址--无SOE
	SOE_VALVE_OPEN,					//普通开阀
	SOE_VALVE_OPEN_EMERGENCY,		//应急开阀
	SOE_VALVE_CLOSE,				//普通关阀
	SOE_VALVE_CLOSE_EMERGENCY,		//应急关阀
	SOE_VALVE_CLOSE_LOCK,			//权限关阀		
	SOE_DEVICE_RESTART,				//终端重启
	SOE_BAT_LOW_LEVEL,				//欠压
	SOE_MAGNETIC_ATTACK,			//磁干扰
	SOE_OVER_FLOW,					//过流
	SOE_BACK_FLOW,					//倒转
	SOE_MODEL_ERROE,				//计量模块故障
	SOE_TAKE_OFF_METER,				//拆表
	SOE_INSERT_SOMETHING,			//异物插入
	SOE_SWITCH_INTER_OSCI,			//切换内部晶振
	SOE_MANUAL_UPLOAD,				//手动发起上报
	SOE_SET_INIT_VALUE,				//设置底数
	SOE_17_BLE_ADV_ERR,
	SOE_18_BLE_ERR,
	SOE_19_BLE_TRIM,
	SOE_20_BLE_INIT, //蓝牙初始化
/*****沈阳规约报警事件类型**********/
	//报警
	SOE_SY_OVER_FLOW = 1001,//流量超限--
	SOE_SY_NO_WATER, //空管报警--
	SOE_SY_REVERSE_FLOW, //反流报警--
	SOE_SY_LEAK_FLOW,//漏水报警--
	SOE_SY_LOW_POWER,//低电压报警---
	SOE_SY_TEMP_OVER,//温度超范围--
	SOE_SY_SHELL_OPEN,//上盖被打开
	SOE_SY_VALVE_ERR,//阀门操作失败--
	SOE_SY_CIRCLE_WRONG,//电路故障--
	SOE_SY_TEMP_SHORT,//测温短路--
	SOE_SY_TEMP_OPEN,//测温开路--
	//事件
	SOE_SY_VALVE_OPEN=1021,//开阀--
	SOE_SY_VALVE_CLOSE,//关阀--
	SOE_SY_SYS_RESET,//系统重启--1023
	SOE_SY_FROZEN_CLEAR,//冻结数据清空----
	SOE_SY_CURRENT_CLEAR,//水表读数清0----
	//自定义报警事件
	SOE_SY_LOW_AMP = 1101,//低振幅--
	SOE_SY_GP30_COMERR,//GP30通信故障--
	SOE_SY_TOF_OVER,//TOF跳变--
	
	SOE_END_ID0 = 0xFFF0,
	//自定义事件
	SOE_START_ID1 = 0xFFF8,
	SOE_END_ID1,
};

enum{	
	SAVE_SOE_S0 = 0,
	SAVE_SOE_S1,
	SAVE_SOE_S2,
	SAVE_SOE_S3
};

typedef struct
{
    u16 soe_type[SOE_NUM];
    DateTime_noweek soe_time[SOE_NUM];
    u8 soe_data[SOE_NUM];
	u8 soe_cnt;
	u8 soe_record_cnt;
} SOE;

typedef struct
{
	 u16 soe_type;
	 DateTime_noweek soe_time;
	 u8  soe_data;
} ONE_SOE;

void SaveNewSoe(u16 soe_type,u8 soe_data);
u8 Check_One_Soe (u16 soe_type,DateTime_noweek *start_time,DateTime_noweek *end_time,ONE_SOE *soe_read);
void SoeInit(void);
void SoeDealApp(void);
u8 SaveSoeToEEIfIdle(void);
//#pragma pack()


#endif
