
#ifndef  _HISTORY_RCD_H
#define _HISTORY_RCD_H


#define HIS_FILE_VER	"v1.0"

#define EOF_LEN			1 //0x0A“文件结束”（EOF）标志 的长度
#define R_N_LEN			2 //"\r\n" 的长度


#define MSG_SOE_RCD_LEN			strlen("第0001条SOE：0x0014,1,2018-07-26 10:29:31.109\r\n")
#define	MSG_CO_RCD_LEN			strlen("第0001条遥控操作记录：0x6001,选择    ,合,2018-07-26 11:43:48.420\r\n")
#define MSG_EXV_RCD_LEN 		strlen("0x4001,00000.000,2017-03-14 15:53:34.162,")
#define MSG_FIX_RCD_LEN			strlen("0x4001,00000.000,")
#define MSG_FLOWREV_RCD_LEN		strlen("0x6409,00000.000,2017-03-14 15:53:34.162,")
#define MSG_FRZ_RCD_LEN			strlen("0x6409,00000.000,")


//soe记录条数n（两个字节）?
#define MSG_SOE_FILE_HEAD_LEN		strlen("文件头：soe.msg,v1.0\r\nD30XXXXZN000201703100002,1024,02\r\n")
#define MSG_CO_FILE_HEAD_LEN		strlen("文件头：co.msg,v1.0\r\nD30XXXXZN000201703100002,1024,02\r\n")
#define MSG_EXV_FILE_HEAD_LEN		strlen("文件头：exvXXXXXXXX.msg,v1.0\r\nD30XXXXZN000201703100002,170507,02\r\n")
#define MSG_FIX_FILE_HEAD_LEN		strlen("文件头：fixptXXXXXXXX.msg,v1.0\r\nD30XXXXZN000201703100002,170507,1024,02\r\n")
#define MSG_FRZ_FILE_HEAD_LEN		strlen("文件头：frzXXXXXXXX.msg,v1.0\r\nD30XXXXZN000201703100002,170507,1024,02\r\n")
#define MSG_FLOWREV_FILE_HEAD_LEN	strlen("文件头：flowrev.msg,v1.0\r\nD30XXXXZN000201703100002,170507,02\r\n")
#define MSG_LOG_FILE_HEAD_LEN 		strlen("文件头：D30XXXXZN000201703100002,1024\r\n")

#define MSG_FIX_PART_ID_OFFSET		strlen("文件头：fixptXXXXXXXX.msg,v1.0\r\nD30XXXXZN000201703100002,170507,")
#define MSG_FRZ_PART_ID_OFFSET		strlen("文件头：frzXXXXXXXX.msg,v1.0\r\nD30XXXXZN000201703100002,170507,")



#define SOE_RCD_NUM_OFFSET		strlen("文件头：soe.msg,v1.0\r\nD30XXXXZN000201703100002,")
#define CO_RCD_NUM_OFFSET		strlen("文件头：co.msg,v1.0\r\nD30XXXXZN000201703100002,")
#define	MSG_LOG_RCD_NUM_OFFSET		strlen("文件头：D30XXXXZN000201703100002,")


#define SOE_RCD_NUM			1024
#define CO_RCD_NUM			32
#define LOG_RCD_NUM			1024		
#define FIX_RCD_NUM			96
#define FRZ_RCD_NUM			96
#define FLOWREV_RCD_NUM   	256


#define EXV_FILE_NUM_MAX		31
#define FIXPT_FILE_NUM_MAX		31
#define FRZ_FILE_NUM_MAX		31


#define FLOWREV_FRONT_PART_NUM		256		//文件前面保存下的节点数



#define XML_SOE_FILE_HEAD_LEN		strlen("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<DataFile>\r\n <Header fileType=\"SOE\" fileVer=\"1.00\" devName=\"D301108SQ001202003047793\" />\r\n <DataRec num=\"1024\">\r\n  </DataRec>\r\n</DataFile>\r\n")//38+2+10+2+63+2+1+20+2+1+10+2+1+11+2
#define XML_CO_FILE_HEAD_LEN		strlen("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<DataFile>\r\n <Header fileType=\"CO\" fileVer=\"1.00\" devName=\"D301108SQ001202003047793\" />\r\n <DataRec num=\"32\">\r\n  </DataRec>\r\n</DataFile>\r\n")
#define XML_EXV_FILE_HEAD_LEN		strlen("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<DataFile>\r\n <Header fileType=\"EXV\" fileVer=\"1.00\" devName=\"D301108SQ001202003047793\" />\r\n <DataAttr num=\"1024\">\r\n </DataAttr>\r\n <DataRec num=\"1024\">\r\n  </DataRec>\r\n</DataFile>\r\n")
#define XML_FLOWREV_FILE_HEAD_LEN	strlen("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<DataFile>\r\n <Header fileType=\"FLOWREV\" fileVer=\"1.00\" devName=\"D301108SQ001202003047793\" />\r\n <DataRec num=\"256\">\r\n  </DataRec>\r\n</DataFile>\r\n")
#define XML_LOG_FILE_HEAD_LEN		strlen("<?xml version=\"1.0\" encoding=\"UTF-8\"?><DataFile>\r\n <Header fileType=\"Ulog\" fileVer=\"1.00\" devID=\"D301108SQ001202003047793\"/> <DataRec num=\"0325\">\r\n  </DataRec>\r\n</DataFile>\r\n")

#define XML_SOE_RCD_LEN			strlen("  <DI ioa=\"10000\" tm=\"160813_180000_011\" val=\"0\" />\r\n")
#define	XML_CO_RCD_LEN			strlen("  <DI ioa=\"24578\" tm=\"160813_180000_011\" cmd=\"select\" val=\"0\" />\r\n")
#define XML_EXV_RCD_LEN 		strlen("  <DI ioa=\"16385\" type=\"float\" unit=\"MVAr\" />\r\n  <DI max=\"000.000\" max_tm=\"160813_114321\" min=\"000.000\" min_tm=\"160813_114321\" />\r\n")
#define XML_FLOWREV_RCD_LEN		strlen("  <DI ioa=\"25728\" tm=\"200509_153647_000\" val=\"0000.04\" />")
#define XML_LOG_RCD_LEN			strlen("  <DI logType=\"2\" tm=\"000101_000153_576\" txt=\"Device is powered on,crc:code=0x9871,rsc=0x156a,brd_res=0xc65b,equ_cfg=0x6d68,rectable=0x0518\" val=\"1\"/>")



#define XML_FIX_DATAREC_LEN		strlen(" <DataRec sect=\"96\" tm=\"160813_001500\">\r\n </DataRec>\r\n")
#define XML_FIX_SECT_LEN		strlen("  <DI val=\"000000.000\" />\r\n")
#define XML_FIX_ATTR_LEN		strlen("  <DI ioa=\"16385\" type=\"float\" unit=\"MVAr\" />\r\n")

#define XML_FIX_FILE_FIX_LEN	strlen("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<DataFile>\r\n <Header fileType=\"FIXPT\" fileVer=\"1.00\" devName=\"XXXXXXXXXXXX\" />\r\n <DataAttr dataNum=\"1024\" sectNum=\"096\" interval=\"15min\">\r\n </DataAttr>\r\n</DataFile>\r\n")
#define XML_FIX_ATTR_LEN		strlen("  <DI ioa=\"16385\" type=\"float\" unit=\"MVAr\" />\r\n")




#define XML_FRZ_ATTR_LEN			strlen("  <DI ioa=\"25609\" type=\"float\" unit=\"kVarh\" />")
#define XML_FRZ_SECT_LEN			strlen("  <DI val=\"000000.000\" />\r\n")
#define XML_FRZ_MIN_DATAREC_LEN		strlen(" <DataRec sect=\"96\" tm=\"160813_001500\">\r\n </DataRec>\r\n")
#define XML_FRZ_DAY_DATAREC_LEN		strlen(" <DataRec type=\"DayFrz\" tm=\"160813_180000\">\r\n </DataRec>\r\n")

#define XML_FRZ_FILE_FIX_LEN		strlen("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<DataFile>\r\n <Header fileType=\"FRZ\" fileVer=\"1.00\" devName=\"XXXXXXXXXXXX\" />\r\n <DataAttr type=\"FixFrz\" dataNum=\"1024\" sectNum=\"096\" interval=\"15min\">\r\n </DataAttr>\r\n <DataAttr type=\"DayFrz\" dataNum=\"1024\"> </DataAttr></DataFile>\r\n")



enum
{
	HS_FILE_SOE	=	0,		//SOE记录文件
	HS_FILE_CO,					//遥控记录文件
	HS_FILE_FIXPT,				//定点遥测记录文件
	HS_FILE_EXV,				//极值记录文件
	HS_FILE_FRZ,
	HS_FILE_FLOWREV,
	HS_FILE_LOG,
	HS_FILE_NUM
};



struct exv_rcd_data
{
	u16 cp;						//信息对象地址
	float elem_val;				//信息元素值(归一化值)
	struct rtc_time_t 	time_mark;	//时标
};

#define EXV_NUM_MAX	UI_NUM + SW_AC_NUM_ALL*SWITCH_NUM_MAX
struct exv_rcd_t
{
	struct rtc_time_t 	time_bk;		//文件日期
	
	u32 max_num;					//最大值信息对象数目n
	struct exv_rcd_data max_data[EXV_NUM_MAX];
	float max_value_bk[EXV_NUM_MAX];

	u32 min_num;						//最小值信息对象数目n
	struct exv_rcd_data min_data[EXV_NUM_MAX];
	float min_value_bk[EXV_NUM_MAX];

	u8 ac_num;
	
	bool save_flag;
	bool occur_flag;
};


#define FIXPT_RCD_NUM	UI_NUM + SW_AC_NUM_ALL*SWITCH_NUM_MAX
struct fixpt_rcd_data
{
	u16 cp;
	float elem_val;				//信息元素值(浮点数值)
};

struct fixpt_rcd_t
{
	struct  rtc_time_t 	time_bk;	//定点时刻
	u8 info_num;				//定点信息对象数目
	struct fixpt_rcd_data data[FIXPT_RCD_NUM];

};


/****************************日冻结电能量记录文件****************************/
struct frz_rcd_data
{
	u16 cp;
	float elem_val;				//信息元素值(浮点)
};

struct frz_rcd_t
{
	struct  rtc_time_t 	tm;	//定点时刻
	u8 min_frz_num;		//十五分钟冻结信息体个数
	u8 day_frz_num;		//日冻结信息体个数
	struct frz_rcd_data min_data[SW_DD_NUM_ALL];
	struct frz_rcd_data day_data[SW_DD_NUM_ALL];
};



/****************************功率反向电能量冻结值文件****************************/
#define FLOW_RCD_NUM	8
struct flowrev_rcd_data
{
	u16 cp;
	float elem_val;				//信息元素值(浮点)
	struct  rtc_time_t rev_time;	//冻结时刻
	
};

struct flowrev_rcd_t
{
	u8 info_num;
	struct  rtc_time_t time_bk;
	struct flowrev_rcd_data data[FLOW_RCD_NUM];
};



/************************************日志记录文件******************************/

enum 
{
	TYPE_LOG_NULL		=	0,
	TYPE_DEV_RESTART,
	TYPE_CHNL_LINK,
	TYPE_COMM_ERR,
	TYPE_HW_ERR,
	TYPE_SW_ERR,
	TYPE_MAIN_POW,
	TYPE_STANDBY_POW,
	TYPE_CTL_LOOP,
	TYPE_SW_YX,
	TYPE_DEV_PARM,
	TYPE_SW_UPDATE,
	LOG_TYPE_NUM
};


#define LINK_CH_NUM	12
typedef struct LOG_FLAG
{
	
	bool com_err[COMM_CHANNEL_NUM];
	bool mdygd;
	bool sdygd;
	bool dchh;	
	bool dcyc;
	
}LOG_FLAG;

struct log_struct
{
	u8 id;
	u8 val;
	struct rtc_time_t time;
	char  des_str[128];
};
	


#define LOG_NUM	16

struct log_poll 
{
	volatile u8 head;
	volatile u8 tail;
	struct log_struct log[LOG_NUM];
};



struct history_rcd_file
{
	struct rtc_time_t create_time;	//文件创建时间
	int  node_num;		//节点数
	int rcd_num;
	u32 len;				//文件大小	
	char name[64];		//文件名
	struct file * 	handle;
	int	offset;
	int num_pos;	
}; 

struct rcd_file_info
{
	u32 len;				//一条记录的长度
	u32 rcd_num;		//记录行总数
	u16 part_id;			//节点索引
	u16 end_index;	
	u8 file_num;			//文件总数
	bool b_err;				//错误标志
	bool b_new;
};




extern struct rcd_file_info 	soe_file_info;
extern struct rcd_file_info 	yk_file_info;
extern struct rcd_file_info 	fixpt_file_info;
extern struct rcd_file_info	exv_file_info;
extern struct rcd_file_info	 frz_file_info;
extern struct rcd_file_info	log_file_info;

extern struct exv_rcd_t exv_rcd;
extern struct fixpt_rcd_t fixpt_rcd;
 
extern LOG_FLAG  log_flag;

extern bool histrory_file_set;

extern bool b_hisfile_format;


extern bool hs_file_init_flag;

extern struct log_poll log_st; 

extern bool b_iec_change;

extern struct history_rcd_file event_rcd_file[2];	//事件记录，包括SOE及操作记录

extern fileInfo soeList;		//SOE事件记录
extern fileInfo coList;			//遥控操作记录

extern fileInfo flowrevList;	//潮流反向记录
extern fileInfo logList;		//日志记录



extern char *p_his_file[];

extern const char *history_file_name[][2];

int  acquire_exv_data(u8 day);


int create_event_rcd_file(u8 type);

int  read_exv_rcd_file(u8 day);

int create_exv_rcd_file(struct rcd_file_info *p_info);

void get_exv_rcd_data(struct rtc_time_t time,bool day_change);

int  read_fixpt_rcd_file(u8 day,u16 *p_part_id);

int read_frz_rcd_file(u8 day,u16 *p_part_id);

int read_flowrev_rcd_file(void);

int flowrev_rcd_file_deal(void);

int create_fixpt_rcd_file(void);

int create_frz_rcd_file(void);

int create_flowrev_rcd_file(void);

int exv_rcd_file_deal(struct rtc_time_t time);

int fixpt_rcd_file_deal(struct rtc_time_t time);

int frz_rcd_file_deal(struct rtc_time_t time);

int  read_log_rcd_file(void);

void get_fixpt_rcd_data(struct rtc_time_t time_bk);

int get_frz_rcd_data(struct rtc_time_t time_bk);


void refresh_soe_file(EVENT_STRUCT * er);

void refresh_co_file(bool master,u8 frametype,u8 op_type,u16 cp,u8 cot,u8 cmd);


void  init_hs_file(bool b_new_dir);

void refresh_exv_file(void);

void refresh_fixpt_file(void);

void hisory_file_app(void);

int create_log_rcd_file(void);


void load_hs_log_rcd(u8 id,bool rtc_flag,struct rtc_time_t *p_tm,char *buf,u8 val);


int get_flowrev_data(void);

void log_rcd_com_err_rst(int chnl);

bool log_file_app(void);

int hs_read_iec_crc(void);

bool hs_write_iec_crc(void);

int create_extreme_txt(u8 day);

int  exv_txt_file_deal(void);

int init_his_db(void);

int init_his_soe_db(void);

int init_his_co_db(void);

int init_his_flowrev_db(void);

int init_his_log_db(void);


int del_history_file(u8 type);

int rt_file_deldir(char *pathname);



#endif
