#include "head.h"

#ifdef METERING_ENERGY

//static ENERGY_CURVE   energy_curve;

#define ENERGY_DATA_SPACE		8
#define ENERGY_TIME_SPACE		strlen("2020-01-01 01:01:01,") 
#define ENERGY_STRING_SPACE		strlen("2020-01-01 01:01:01,1234.123\r\n")


#define ENERGY_FILE_ROW			1024
//#define ENERGY_FILE_COLUMN		CURVE_NUM

//#define ENERGY_CURVE_ROW		12

//static char row_str[ENERGY_FILE_COLUMN*ENERGY_ITEM_SPACE + 3] = {0};

fileInfo curve_file_info[CURVE_NUM];

char* curve_file_name[CURVE_NUM] = 
{
	"curve_day_epp.csv",
	"curve_day_eqp.csv",
	"curve_day_epn.csv",
	"curve_day_eqn.csv",
	"curve_day_ep.csv",
	"curve_day_eq.csv",
	
	"curve_mon_epp.csv",
	"curve_mon_eqp.csv",
	"curve_mon_epn.csv",
	"curve_mon_eqn.csv",
	"curve_mon_ep.csv",
//	"curve_mon_eq.csv",

	"curve_p.csv",
	"curve_pa.csv",
	"curve_pb.csv",
	"curve_pc.csv",	

	"curve_q.csv",
	"curve_qa.csv",
	"curve_qb.csv",
	"curve_qc.csv",	

	"curve_cos.csv",
	"curve_epp.csv",
	"curve_eqp.csv",
	"curve_epn.csv",
	"curve_eqn.csv",

	"curve_u.csv",
	"curve_i.csv",

};


void energy_curve_init(void)
{
	u8 i;

	for(i=0; i<CURVE_NUM; i++)
	{
		memset((char *)&curve_file_info[i], 0, sizeof( curve_file_info[0]));
		sprintf(curve_file_info[i].fileName, "%s", curve_file_name[i]);
		sprintf(curve_file_info[i].dirName, "%s", "/app/CURVE");
		curve_file_info[i].listMax   		= ENERGY_FILE_ROW;
		if(i == CURVE_U || i == CURVE_I)
			curve_file_info[i].listSize  	= ENERGY_TIME_SPACE + ENERGY_DATA_SPACE*3 + 4;	
		else
			curve_file_info[i].listSize  	= ENERGY_STRING_SPACE;
		curve_file_info[i].fileTatal 		= 1;
		curve_file_info[i].fileTableSize	= 0;
		curve_file_info[i].fileWRType		= ALL_LIST_NUM_REPLACE_TYPE;
		initListData(&curve_file_info[i]);	
	}
	
}

/*
int energy_curve_create(u8 file_id)
{
	int i,j;
	unsigned int file_len;
	char *file_buf;
	loff_t pos;
	struct file * pfile;
	int ret = 0;
	char tmp_buf[64]={0};
	u8 size;
	ENERGY_CURVE_DATA* curve_data;
	
	if(file_id >= CURVE_NUM)
		return -1;

	file_len = ENERGY_CURVE_DATA*curve_file_info[i].listNum;
	// 分配并初始化空间
	file_buf = rt_malloc(file_len);
	if(!file_buf)
		return -2;

	memset(file_buf, 0, file_len);
	for(i=0; i<curve_file_info[i].listNum; i++)
	{
		size = readListData(&curve_file_info[i], i+1, tmp_buf);
		if(size = sizeof(ENERGY_CURVE_DATA))
		{
			curve_data = (ENERGY_CURVE_DATA*)tmp_buf;
		}
		sprintf(p_file+strlen(p_file), "20%02d-%02d-%02d %02d:%02d:%02d,%4.3f\r\n", curve_data->time.year, curve_data->time.month, curve_data->time.day, curve_data->time.hour, curve_data->time.min, curve_data->time.ms/1000, curve_data->data);
	}
	pfile = rt_file_open(curve_file_name[file_id], O_CREAT|O_RDWR, 0);
	if(IS_ERR(pfile))
	{
		rt_printf("%s:%s open error!\r\n",__func__, curve_file_name[file_id]);
		rt_free(file_buf);
		return -3;
	}

	pos = 0;
	if(rt_file_write(pfile, p_file, file_len, &pos) != file_len)
	{
		rt_free(file_buf);
		rt_free(p_file);
		return -4;
	}

	rt_file_close(pfile,0);

	//释放内存
	rt_free(file_buf);

	return ret;

}
*/


void energy_curve_refresh(u8 interval)
{
	float tmp[3];
	
	if(interval == ENERGY_CURVE_DAY) //日冻结数据
	{
		energy_curve_write_data(CURVE_DAY_POS_EP, &meter_dd[ID_EP_P].value, 1);
		energy_curve_write_data(CURVE_DAY_POS_EQ, &meter_dd[ID_EQ_P].value, 1);
		energy_curve_write_data(CURVE_DAY_INV_EP, &meter_dd[ID_EP_N].value, 1);
		energy_curve_write_data(CURVE_DAY_INV_EQ, &meter_dd[ID_EQ_N].value, 1);		
		energy_curve_write_data(CURVE_DAY_EP, &meter_dd[ID_EP].value, 1);
		energy_curve_write_data(CURVE_DAY_EQ, &meter_dd[ID_EQ].value, 1);	
		
	}
	else if(interval == ENERGY_CURVE_MON) //月冻结数据
	{
		energy_curve_write_data(CURVE_MON_POS_EP, &meter_dd[ID_EP_P].value, 1);
		energy_curve_write_data(CURVE_MON_POS_EQ, &meter_dd[ID_EQ_P].value, 1);
		energy_curve_write_data(CURVE_MON_INV_EP, &meter_dd[ID_EP_N].value, 1);
		energy_curve_write_data(CURVE_MON_INV_EQ, &meter_dd[ID_EQ_N].value, 1);	
		energy_curve_write_data(CURVE_MON_EP, &meter_dd[ID_EP].value, 1);
	//	energy_curve_write_data(CURVE_MON_EQ, &meter_dd[ID_EQ].value, 1);			
	}
	else if(interval == ENERGY_CURVE_RT) //实时冻结数据
	{
		energy_curve_write_data(CURVE_P,  &meter_yc[ID_P].value, 1);
		energy_curve_write_data(CURVE_PA, &meter_yc[ID_PA].value, 1);
		energy_curve_write_data(CURVE_PB, &meter_yc[ID_PB].value, 1);
		energy_curve_write_data(CURVE_PC, &meter_yc[ID_PC].value, 1);
		
		energy_curve_write_data(CURVE_Q,  &meter_yc[ID_Q].value, 1);
		energy_curve_write_data(CURVE_QA, &meter_yc[ID_QA].value, 1);
		energy_curve_write_data(CURVE_QB, &meter_yc[ID_QB].value, 1);
		energy_curve_write_data(CURVE_QC, &meter_yc[ID_QC].value, 1);

		energy_curve_write_data(CURVE_COS, 	  &meter_yc[ID_COS].value, 1);
		energy_curve_write_data(CURVE_POS_EP, &meter_dd[ID_EP_P].value, 1);
		energy_curve_write_data(CURVE_POS_EQ, &meter_dd[ID_EQ_P].value, 1);
		energy_curve_write_data(CURVE_INV_EP, &meter_dd[ID_EP_N].value, 1);
		energy_curve_write_data(CURVE_INV_EQ, &meter_dd[ID_EQ_N].value, 1);

		tmp[0] = meter_yc[ID_UA].value;
		tmp[1] = meter_yc[ID_UB].value;
		tmp[2] = meter_yc[ID_UC].value;
		energy_curve_write_data(CURVE_U, tmp, 3);
		tmp[0] = meter_yc[ID_IA].value;
		tmp[1] = meter_yc[ID_IB].value;
		tmp[2] = meter_yc[ID_IC].value;
		energy_curve_write_data(CURVE_I, tmp, 3);
	}
}

/*
int energy_curve_write_row(u8 file_id, float* data)
{
	struct timespec ts;
	struct rtc_time_t time;	//时标
	static char row_str[64] = {0};

	if(file_id > CURVE_NUM)
		return -1;
	
	//获取当前时间
	clk_time_get(&ts);
	timespec_to_rtc(ts,&time,0);

	sprintf(row_str, "20%02d-%02d-%02d %02d:%02d:%02d,%4.3f\r\n", time.year, time.month, time.day, time.hour, time.min, time.ms/1000, data);
	

	return 0;
}
*/

int energy_curve_write_data(u8 file_id, float* data, u8 data_num)
{
	struct timespec ts;
	struct rtc_time_t time;	//时标
	char row_str1[64] = {0};
	char row_str2[64] = {0};
	int len = 0, i;

	if(file_id > CURVE_NUM)
		return -1;
	
	//获取当前时间
	clk_time_get(&ts);
	timespec_to_rtc(ts,&time,0);

	sprintf(row_str2, "20%02d-%02d-%02d %02d:%02d:%02d,", time.year, time.month, time.day, time.hour, time.min, time.ms/1000);
	for(i=0; i<data_num; i++)
	{
		len = float_format(row_str1 + strlen(row_str1), ENERGY_DATA_SPACE, "%4.3f", data[i], 1);
		if(i < (data_num - 1))
			strcat(row_str1, ",");
	}
	strcat(row_str2, row_str1);
	strcat(row_str2, "\r\n");
//	rt_printf("\r\n float_format: %s", row_str2);
	
	writeListData(&curve_file_info[file_id], row_str2);
//	len = strlen(row_str2);
//	rt_printf("row_str2 len：%d\r\n", len);

	return strlen(row_str2);
}


#if 0
void energy_curve_write_file(u8 file_id, u8 data_id)
{
	struct timespec ts;
	struct rtc_time_t time;	//时标

	if(file_id > CURVE_NUM)
		return -1;
	
	//获取当前时间
	clk_time_get(&ts);
	timespec_to_rtc(ts,&time,0);

//	ENERGY_CURVE_DATA curve_data;

//	curve_data.time = time;
	

#if 0
	float data[3];
	uint8_t i = 0;

	data[0] = 123;
	data[1] = 456;
	data[2] = 789;
	memset(row_str, 0, sizeof(row_str));
	float_format_row("%4.3f", ENERGY_ITEM_SPACE, row_str, &data[0], 3);
	csv_write_row(&energy_curve.csv, row_str, 0);
	
//	data[0] = 123;
//	data[1] = 456;
//	data[2] = 789;
//	memset(line_str, 0, sizeof(line_str));
//	float_format_row("%4.3f", line_str, &data[0], 3);
//	csv_write_row(&energy_curve.csv, line_str, 1);
/*	
	data[0] = 22;
	data[1] = 1;
	data[2] = 2;
	memset(row_str, 0, sizeof(row_str));
	float_format_row("%4.3f", ENERGY_ITEM_SPACE, row_str, &data[0], 3);
	csv_write_row(&energy_curve.csv, row_str, 1);
	*/
//	rt_printf("内容：%s", line_str);
#else
	//冻结曲线
//	memset(row_str, 0, sizeof(row_str));
//	float_format_row("%4.3f", ENERGY_ITEM_SPACE, row_str, energy_curve.data, CURVE_NUM);
//	csv_write_row(&energy_curve.csv, row_str, energy_curve.csv.cur_row);
//	energy_curve.csv.cur_row++;
//	if(energy_curve.csv.cur_row >= ENERGY_FILE_ROW)
//		energy_curve.csv.cur_row = 0;
#endif
}
#endif


#endif




