#include <time.h>
#include <string.h>
#include <stdio.h>
#include "serial.h"
#include "common.h"
#include "sample_process.h" 
#include "analysis.h"
#include "ysp_debug.h"

#define COMTADE_DIR "/COMTRADE"   //默认波形存储目录
#define RAW_WAVE_DIR "/usr_app/data"
#define VIN      4.096  //���������ѹ 
#define GND_OFFS 0.254  //����˷������׼ƫ�� 
#define AMP      5.94   //����˷ŷŴ��� 
#define IN_OFFS         //����˷Ÿ�����ƫ�õ�ѹ 
#define MCU_VREF 3.3
/*
typedef struct{
unsigned int data_flg1;       
unsigned int data_flg2;       
float    cold_temp;       
unsigned char  working_state[2];
unsigned char  relay_state[4];  
unsigned char  sensor_state[4]; 
unsigned char  health_state;    
}SAMPLE_INPUT_TYPE;
*/

/*
typedef struct{
unsigned char  relay_ctrl[4];   
unsigned char  sensor_ctrl[4];  
unsigned int   sample_interval; 
unsigned int   sample_length;  
float    temperature;     
//PID_SET  pid_para; 
unsigned int rsv[3];       
}SAMPLE_OUTPUT_TYPE;
*/
static const char *dir_store_data=RAW_WAVE_DIR;//COMTADE_DIR; //2021-10-28修改默认波形存储目录为RAW_WAVE_DIR
static unsigned char sample_rx_buf[SEGMENT_LENGTH+7];
static unsigned char sample_tx_buf[128];
static unsigned short data_buf[10000];
extern int sample_fd;

//SAMPLE_OUTPUT_TYPE sample_output_data;
//SAMPLE_INPUT_TYPE  sample_input_data;



unsigned int sample_encode(unsigned char *buf,unsigned char cmd,void *args,unsigned int args_len)
{
  unsigned int length=7;
  buf[0]=0xAA;
  buf[1]=0x1;//address
  buf[2]=(unsigned char)args_len;
  buf[3]=(unsigned char)(args_len>>8);
  buf[4]=cmd;
  if(args!=NULL&&args_len!=0)
   {
     memcpy(&buf[5],(unsigned char *)args,args_len);     
     length=args_len+7;	
   }
 buf[length-2]=cal_sum(buf,length-2);
 buf[length-1]=0x55;
 return length;
}

int sample_check(unsigned char *buf,unsigned char cmd,unsigned int buf_len)
{
  unsigned int length;
  if(buf[0]!=0xAA)
    return -1;
  if(buf[buf_len-1]!=0x55)
    return -2;
  if(buf[4]!=cmd)	
    return -3;
  length=(unsigned int)256*buf[3]+buf[2];
  if((length+7)!=buf_len)
    return -4;
  if(chk_sum(buf,buf_len-2,buf[buf_len-2])!=0)
    return -5;
  return 0;
}


//��ѯ�Ƿ��в������� 
unsigned int query_sample_data(unsigned char *ret_data,int len)
{
  int length;
  int chk_rlt;
  length=sample_encode(sample_tx_buf,CMD_NORMAL_READ,NULL,0);
  if(SerialWrite(sample_fd,sample_tx_buf,length)<length)
    return 1;
  memset(sample_rx_buf,0,64);
  if((length=SerialReadEx(sample_fd,sample_rx_buf,len+7,20))>=(len+7))
    {
   	  if((chk_rlt=sample_check(sample_rx_buf,CMD_NORMAL_READ,length))==0)
   	   {
   	   	 memcpy(&my_sample_data,&sample_rx_buf[5],sizeof(my_sample_data));
   	   	 //if(ret_data!=NULL&&len!=0)
   	   	 if(ret_data!=NULL)
            memcpy(ret_data,&my_sample_data.data_flg,sizeof(unsigned int));
         return 0;  	   	
  	   }
      else
       {
       	 LOG_DEBUG(ERROR_DEBUG,"query_sample_data chk_rlt=%d\n",chk_rlt);
       }
    }
  else
    {
      LOG_DEBUG(ERROR_DEBUG,"query_sample_data read resp failed length=%d\n",length);
	  return 2;
	}
  return 3;		
}

/*
unsigned int query_sample_data_ex(unsigned char *ret_data,int len)
{
  int length;
  length=sample_encode(sample_tx_buf,CMD_NORMAL_READ,NULL,0);
  if(SerialWrite(sample_fd,sample_tx_buf,length)<length)
    return 1;
  memset(sample_rx_buf,0,64);
  if((length=SerialReadEx(sample_fd,sample_rx_buf,sizeof(SAMPLE_INPUT_TYPE)+7,10))>=(sizeof(SAMPLE_INPUT_TYPE)+7))
    {
   	  if(sample_check(sample_rx_buf,CMD_NORMAL_READ,length)==0)
   	   {
   	   	 if(ret_data!=NULL&&len!=0)
            memcpy(ret_data,&sample_rx_buf[5],len);
         return 0;  	   	
  	   }
    }
  else
    {
	  return 2;
	}
  return 3;		
}*/


//����������� 
unsigned int clear_sample_data(void)
{
  int length;
  length=sample_encode(sample_tx_buf,CMD_CLR_DATA,NULL,0);
  if(SerialWrite(sample_fd,sample_tx_buf,length)<length)
    return 1;
  memset(sample_rx_buf,0,16);
  if((length=SerialReadEx(sample_fd,sample_rx_buf,8,8))>=8)
    {
   	  if(sample_check(sample_rx_buf,CMD_CLR_DATA,length)==0)
   	   {
         if(sample_rx_buf[5]==SUCCESSFULL)
            return 0;  	   	
  	   }
      else
       {
   	     LOG_DEBUG(ERROR_DEBUG,"clear_sample_data check sum error\n");
       }
    }
  else
    {
   	  LOG_DEBUG(ERROR_DEBUG,"clear_sample_data read resp failed length=%d\n",length);
	  return 2;
	}
  return 3;		
}


//����������� 
unsigned int clear_co2_data(void)
{
  int length;
  length=sample_encode(sample_tx_buf,CMD_CLR_CO2,NULL,0);
  if(SerialWrite(sample_fd,sample_tx_buf,length)<length)
    return 1;
  memset(sample_rx_buf,0,16);
  if((length=SerialReadEx(sample_fd,sample_rx_buf,8,8))>=8)
    {
   	  if(sample_check(sample_rx_buf,CMD_CLR_CO2,length)==0)
   	   {
         if(sample_rx_buf[5]==SUCCESSFULL)
            return 0;  	   	
  	   }
      else
       {
   	     LOG_DEBUG(ERROR_DEBUG,"clear_co2_data check sum error\n");
       }
    }
  else
    {
   	  LOG_DEBUG(ERROR_DEBUG,"clear_co2_data read resp failed length=%d\n",length);
	  return 2;
	}
  return 3;		
}

//�������� 
int start_sample_data(unsigned int ch,SAMPLE_PARA_TYPE *sample_para)
{ 
  int length;
  unsigned char arg_buf[12];
  arg_buf[0]=(unsigned char)ch;
  arg_buf[1]=(unsigned char)(ch>>8);
  arg_buf[2]=(unsigned char)(sample_para->len);
  arg_buf[3]=(unsigned char)((sample_para->len)>>8);
  arg_buf[4]=(unsigned char)(sample_para->intval);
  arg_buf[5]=(unsigned char)((sample_para->intval)>>8);
  arg_buf[6]=(unsigned char)(sample_para->offs);
  arg_buf[7]=(unsigned char)((sample_para->offs)>>8);
  arg_buf[8]=(unsigned char)(sample_para->co2_pos);
  arg_buf[9]=(unsigned char)((sample_para->co2_pos)>>8);
  arg_buf[10]=(unsigned char)(sample_para->co2_len);
  arg_buf[11]=(unsigned char)((sample_para->co2_len)>>8);
  length=sample_encode(sample_tx_buf,CMD_START_SAMPLE,arg_buf,sizeof(arg_buf));
  if(SerialWrite(sample_fd,sample_tx_buf,length)<length)
   {
   	LOG_DEBUG(ERROR_DEBUG,"start_sample_data send request failed\n");
   	return 1;
   }
  memset(sample_rx_buf,0,16);
  if((length=SerialReadEx(sample_fd,sample_rx_buf,8,8))>=8)
    {
   	  if(sample_check(sample_rx_buf,CMD_START_SAMPLE,length)==0)
   	   {
         if(sample_rx_buf[5]==SUCCESSFULL)
            return 0;  	   	
  	   }
      else
       {
       	 LOG_DEBUG(ERROR_DEBUG,"start_sample_data check sum error\n");
       }
    }
  else
    {
   	  LOG_DEBUG(ERROR_DEBUG,"start_sample_data read resp failed length=%d\n",length);
	  return 2;
	}
  return 3; 	
}

/*
//�������������� 
int start_sample_heat(unsigned char ch,unsigned char flg)
{  
  int length;
  unsigned char buf[2];
  buf[0]=ch;
  buf[1]=flg;
  length=sample_encode(sample_tx_buf,CMD_SET_RLY,buf,sizeof(buf));
  if(SerialWrite(sample_fd,sample_tx_buf,length)<length)
    return 1;
  memset(sample_rx_buf,0,16);
  if((length=SerialReadEx(sample_fd,sample_rx_buf,8,8))>=8)
    {
   	  if(sample_check(sample_rx_buf,CMD_SET_RLY,length)==0)
   	   {
         if(sample_rx_buf[5]==SUCCESSFULL)
            return 0;  	   	
  	   }
    }
  else
    {
	  return 2;
	}
  return 3;			  	
}*/

//������ 
//start_pos=��ʼλ��(�ֽ�),len=��ȡ����(�ֽ�) 
int read_co2_segment(int start_pos,int len)
{
  int length;
  unsigned char arg_buf[4];
  unsigned char *ptr;
  ptr=(unsigned char *)&data_buf[0];
  arg_buf[0]=(unsigned char)start_pos;
  arg_buf[1]=(unsigned char)(start_pos>>8);
  arg_buf[2]=(unsigned char)len;
  arg_buf[3]=(unsigned char)(len>>8);
  length=sample_encode(sample_tx_buf,CMD_READ_CO2,arg_buf,sizeof(arg_buf));
  if(SerialWrite(sample_fd,sample_tx_buf,length)<length)
   {
   	 LOG_DEBUG(ERROR_DEBUG,"read_co2_segment sent request failed start_pos=%d len=%d \n",start_pos,len);
   	 return 1;
   }
  memset(sample_rx_buf,0,sizeof(sample_rx_buf));
  if((length=SerialReadEx(sample_fd,sample_rx_buf,len+7,16))>=(len+7))
    {
   	  if(sample_check(sample_rx_buf,CMD_READ_CO2,length)==0)
   	   {
   	   	 ptr+=start_pos;
         memcpy((void *)ptr,(const void *)&sample_rx_buf[5],len);
         return 0;  	   	
  	   }
      else
       {
       	 LOG_DEBUG(ERROR_DEBUG,"read_co2_segment check sum failed\n");
       }
    }
  else
    {
   	  LOG_DEBUG(ERROR_DEBUG,"read_co2_segment read resp failed:received %d bytes\n",length);
	  return 2;
	}
  return 3; 	
}

//total_len�ֽڳ��� 
unsigned int read_co2_data(unsigned int total_len)
{
  unsigned int i;
  for(i=0;i<(total_len/SEGMENT_LENGTH);i++)
   {
     if(read_co2_segment(i*SEGMENT_LENGTH,SEGMENT_LENGTH)!=0)
	   {
 	     LOG_DEBUG(ERROR_DEBUG,"read co2 segment %d total_len=%d failed\n",i,total_len);
   		 if(read_co2_segment(i*SEGMENT_LENGTH,SEGMENT_LENGTH)!=0)
   		   {
   		   	  LOG_DEBUG(ERROR_DEBUG,"read co2 segment %d failed\n",i);
   	         if(read_co2_segment(i*SEGMENT_LENGTH,SEGMENT_LENGTH)!=0)
   		      {
   		      	if(read_co2_segment(i*SEGMENT_LENGTH,SEGMENT_LENGTH)!=0)
   		        {
   		      	if(read_co2_segment(i*SEGMENT_LENGTH,SEGMENT_LENGTH)!=0)
   		         {
      	           LOG_DEBUG(ERROR_DEBUG,"read co2 segment %d failed\n",i);
   		   	       return i+1;
   		         }
      	        //LOG_DEBUG(ERROR_DEBUG,"read segment %d failed\n",i);
   		   	    //return i+1;
   		        }
      	        //LOG_DEBUG(ERROR_DEBUG,"read segment %d failed\n",i);
   		   	    //return i+1;
   		      }
   		   }
   	   }	
   }
 if((total_len%SEGMENT_LENGTH)!=0)
   {
   	  //printf("read segment %d\n",i);
     if(read_co2_segment(i*SEGMENT_LENGTH,total_len%SEGMENT_LENGTH)!=0)
       {
       	 LOG_DEBUG(ERROR_DEBUG,"read co2 segment %d failed\n",i);
       	 if(read_co2_segment(i*SEGMENT_LENGTH,total_len%SEGMENT_LENGTH)!=0)
       	   {
       	   	 LOG_DEBUG(ERROR_DEBUG,"read co2 segment %d failed\n",i);
       	     if(read_co2_segment(i*SEGMENT_LENGTH,total_len%SEGMENT_LENGTH)!=0)
       	       {
       	       	LOG_DEBUG(ERROR_DEBUG,"read co2 segment %d failed\n",i);
       	       	return i+1; 
       	       }   		   	     	       	
   	       }
       }	
   }
  return 0; 	
}



//������ 
//start_pos=��ʼλ��(�ֽ�),len=��ȡ����(�ֽ�) 
int read_segment_data(int start_pos,int len)
{
  int length;
  unsigned char arg_buf[4];
  unsigned char *ptr;
  ptr=(unsigned char *)&data_buf[0];
  arg_buf[0]=(unsigned char)start_pos;
  arg_buf[1]=(unsigned char)(start_pos>>8);
  arg_buf[2]=(unsigned char)len;
  arg_buf[3]=(unsigned char)(len>>8);
  length=sample_encode(sample_tx_buf,CMD_READ_SAMPLE,arg_buf,sizeof(arg_buf));
  if(SerialWrite(sample_fd,sample_tx_buf,length)<length)
   {
   	 LOG_DEBUG(ERROR_DEBUG,"read_segment_data sent request failed start_pos=%d len=%d \n",start_pos,len);
   	 return 1;
   }
  memset(sample_rx_buf,0,sizeof(sample_rx_buf));
  if((length=SerialReadEx(sample_fd,sample_rx_buf,len+7,16))>=(len+7))
    {
   	  if(sample_check(sample_rx_buf,CMD_READ_SAMPLE,length)==0)
   	   {
   	   	 ptr+=start_pos;
         memcpy((void *)ptr,(const void *)&sample_rx_buf[5],len);
         return 0;  	   	
  	   }
      else
       {
       	 LOG_DEBUG(ERROR_DEBUG,"read_segment_data check sum failed\n");
       }
    }
  else
    {
   	  LOG_DEBUG(ERROR_DEBUG,"read_segment_data read resp failed:received %d bytes\n",length);
	  return 2;
	}
  return 3; 	
}

//total_len�ֽڳ��� 
unsigned int read_sample_data(unsigned int total_len)
{
  unsigned int i;
  for(i=0;i<(total_len/SEGMENT_LENGTH);i++)
   {
     if(read_segment_data(i*SEGMENT_LENGTH,SEGMENT_LENGTH)!=0)
	   {
 	     LOG_DEBUG(ERROR_DEBUG,"read segment %d total_len=%d failed\n",i,total_len);
   		 if(read_segment_data(i*SEGMENT_LENGTH,SEGMENT_LENGTH)!=0)
   		   {
   		   	  LOG_DEBUG(ERROR_DEBUG,"read segment %d failed\n",i);
   	         if(read_segment_data(i*SEGMENT_LENGTH,SEGMENT_LENGTH)!=0)
   		      {
   		      	if(read_segment_data(i*SEGMENT_LENGTH,SEGMENT_LENGTH)!=0)
   		      {
   		      	if(read_segment_data(i*SEGMENT_LENGTH,SEGMENT_LENGTH)!=0)
   		         {
      	           LOG_DEBUG(ERROR_DEBUG,"read segment %d failed\n",i);
   		   	       return i+1;
   		         }
      	        //LOG_DEBUG(ERROR_DEBUG,"read segment %d failed\n",i);
   		   	    //return i+1;
   		      }
      	        //LOG_DEBUG(ERROR_DEBUG,"read segment %d failed\n",i);
   		   	    //return i+1;
   		      }
   		   }
   	   }	
   }
 if((total_len%SEGMENT_LENGTH)!=0)
   {
   	  //printf("read segment %d\n",i);
     if(read_segment_data(i*SEGMENT_LENGTH,total_len%SEGMENT_LENGTH)!=0)
       {
       	 LOG_DEBUG(ERROR_DEBUG,"read segment %d failed\n",i);
       	 if(read_segment_data(i*SEGMENT_LENGTH,total_len%SEGMENT_LENGTH)!=0)
       	   {
       	   	 LOG_DEBUG(ERROR_DEBUG,"read segment %d failed\n",i);
       	     if(read_segment_data(i*SEGMENT_LENGTH,total_len%SEGMENT_LENGTH)!=0)
       	       {
       	       	LOG_DEBUG(ERROR_DEBUG,"read segment %d failed\n",i);
       	       	return i+1; 
       	       }   		   	     	       	
   	       }
       }	
   }
  return 0; 	
}



char *make_data_file_name(time_t now,unsigned int i)
{
  static char file_name[128];
  char date_str[32];
  memset(file_name,0,sizeof(file_name)); 
  if(i==0)
    strftime(date_str,sizeof(date_str),"%Y%m%d_%H%M%S.bin",localtime(&now));//二进制六组分文件
  else if(i==1)
    strftime(date_str,sizeof(date_str),"%Y%m%d_%H%M%S.csv",localtime(&now));//
  else if(i==2)
    strftime(date_str,sizeof(date_str),"%Y%m%d_%H%M%S.co2",localtime(&now));//co2文件
  else
    strftime(date_str,sizeof(date_str),"%Y%m%d_%H%M%S.csv",localtime(&now));//
  sprintf(file_name,"%s/%s",dir_store_data,date_str);//dir_store_data为默认波形存储目录
  return file_name;	
}


int savefile(const char *file,long offs,void *ptr,unsigned int size)
{
	FILE *fp;
	fp=fopen(file,"w");
	if(fp==NULL)
	  {	
	    LOG_DEBUG(ERROR_DEBUG,"Creat file %s failed when savefile\r\n",file);
	    return -1;
	  }
    if(offs!=0)
     {
       fseek(fp,offs,SEEK_SET);
     }
	if(fwrite((const void *)ptr,size,1,fp)!=1)
	  {
	   fclose(fp);
       LOG_DEBUG(ERROR_DEBUG,"Write file %s failed when savefile\r\n",file);
	   return -2;
	  }
	fclose(fp);
    return 0;	
}

//��������ֱ�Ӵ��32λ������,sizeΪ�ֽڳ��� 
int save_hex_file(const char *file,long offs,void *ptr,unsigned int size)
{
	FILE *fp;
	int i;
	static int hex_buf[5000]; 
	fp=fopen(file,"w");
	if(fp==NULL)
	  {	
        LOG_DEBUG(ERROR_DEBUG,"Creat file %s failed when savefile\r\n",file);
	    return -1;
	  }
    if(offs!=0)
     {
       fseek(fp,offs,SEEK_SET);
     }
    for(i=0;i<(size>>1);i++)
      {
      	memcpy(&hex_buf[i],ptr,sizeof(short));
      	ptr+=2;
      }
	if(fwrite((const void *)hex_buf,size*2,1,fp)!=1)
	  {
	   fclose(fp);
       LOG_DEBUG(ERROR_DEBUG,"Write file %s failed when savefile\r\n",file);
	   return -2;
	  }
	fclose(fp);
    return 0;	
}

/*
//�������ݴ�ɶ����Ƹ����� 
int save_float_file(const char *file,long offs,void *ptr,unsigned int size)
{
	static float file_buf[5000];
	FILE *fp;
	int i;
	unsigned char *data_ptr;
	
	if(size>10000)
	{
      LOG_DEBUG(ERROR_DEBUG,"save_float_file faile due to size=%d\r\n",size);
	  return -1;	
	}
	  
	data_ptr=(unsigned char *)ptr;
	for(i=0;i<(size/2);i++)
	{
	 file_buf[i]=(float)((*data_ptr)+256*(*(data_ptr+1)));
	 data_ptr+=2;	
	}
	fp=fopen(file,"w");
	if(fp==NULL)
     {	
        LOG_DEBUG(ERROR_DEBUG,"Creat file %s failed when savefile\r\n",file);
	    return -2;
     }
    if(offs!=0)
     {
       fseek(fp,offs,SEEK_SET);
     }
	if(fwrite((const void *)file_buf,2*size,1,fp)!=1)
	  {
	   fclose(fp);
       LOG_DEBUG(ERROR_DEBUG,"Write file %s failed when savefile\r\n",file);
	   return -3;
	  }
	fclose(fp);
    return 0;	
}
*/

void RawConvVolt(int tmp,float *volt)
{
  float temp;
  temp=((VIN*tmp)/65536-GND_OFFS)/AMP+((MCU_VREF*my_para_data.mach_run_para.sample_offs)/4096);
  if(volt!=NULL)
   *volt=temp;	
}

/*
//��������ת���ɵ�ѹ�ٴ�ɶ����Ƹ����� 
int save_float_file_ex(const char *file,long offs,void *ptr,unsigned int size)
{
	static float file_buf[5000];
    int tmp;
    int i;
	FILE *fp;
	unsigned char *data_ptr;
	
	if(size>10000)
	{
      LOG_DEBUG(ERROR_DEBUG,"save_float_file_ex faile due to size=%d\r\n",size);
	  return -1;	
	}
	  
	data_ptr=(unsigned char *)ptr;
	for(i=0;i<(size/2);i++)
	{
	 tmp=(int)256*(*(data_ptr+1))+(*data_ptr);
	 RawConvVolt(tmp,&file_buf[i]);
	 data_ptr+=2;	
	}
	fp=fopen(file,"w");
	if(fp==NULL)
	  {	
        LOG_DEBUG(ERROR_DEBUG,"Creat file %s failed when savefile\r\n",file);
	    return -2;
	  }
    if(offs!=0)
     {
       fseek(fp,offs,SEEK_SET);
     }
	if(fwrite((const void *)file_buf,2*size,1,fp)!=1)
	  {
	   fclose(fp);
       LOG_DEBUG(ERROR_DEBUG,"Write file %s failed when savefile\r\n",file);
	   return -3;
	  }
	fclose(fp);
    return 0;	
}
*/

int save_csv_file(const char *file,long offs,void *ptr,unsigned int size)
{
	FILE *fp;
	unsigned int i;
	int tmp;
	unsigned char *data_ptr;
	//float voltage;
	data_ptr=(unsigned char *)ptr;
	fp=fopen(file,"w");
	if(fp==NULL)
	  {	
        LOG_DEBUG(ERROR_DEBUG,"Creat file %s failed when savefile\r\n",file);
	    return -1;
	  }
    if(offs!=0)
     {
       fseek(fp,offs,SEEK_SET);
     }
    for(i=0;i<(size/2);i++)
      {
     	tmp=(int)256*(*(data_ptr+1))+(*data_ptr);
     	//RawConvVolt(tmp,&voltage);
      	fprintf(fp,"%d\n",tmp);
      	data_ptr+=2;
      }
    fprintf(fp,"\n");
	fclose(fp);
    return 0;	
}


//��������,lenΪ�ֽڳ��� 
int save_sample_data(unsigned int len)
{
  int ret;
  char *my_file_name;
  time_t now;
  
  now=time(NULL);
  last_sample_time=now;//存储本次采样时间
  
  my_file_name=make_data_file_name(now,0);//生成原始波形文件名
  
  if((ret=save_hex_file(my_file_name,0,(void *)data_buf,len))!=0)//保存原始波形文件
    {
   	  LOG_DEBUG(ERROR_DEBUG,"save bin file failed %s\n",my_file_name);
      return -1;
	}
  /*if(ret=save_float_file_ex(my_file_name,0,(void *)data_buf,len)!=0)
    {
   	  LOG_DEBUG(ERROR_DEBUG,"save float file failed %s\n",my_file_name);
      return -1;
	}*/
	
  if(send_analysis_req(my_file_name,strlen(my_file_name))!=0)//发送波形分析请求
    {
      LOG_DEBUG(ERROR_DEBUG,"Send analysis request failed:file name is %s\n",my_file_name);
	  return -2;	
    }
 
  my_file_name=make_data_file_name(now,1);//生成csv文件名
  if((ret=save_csv_file(my_file_name,0,(void *)data_buf,len))!=0)
    {
      LOG_DEBUG(ERROR_DEBUG,"save csv file failed %s\n",my_file_name);	
      return -3;
	}
	
  return ret; 	
}

//��������,lenΪ�ֽڳ��� 
int save_co2_data(unsigned int len)
{
  extern int analysis_co2(unsigned short *buf,unsigned int buf_len,float *result);
  extern int analysis_co2ex(unsigned short *buf,unsigned int buf_len,float *result);
  int ret=0;
  char *my_file_name;
  time_t now;
  float co2_ppm;
  
  now=time(NULL);
  //last_sample_time=now;  
  //if(analysis_co2(data_buf,len,&co2_ppm)==0)
  if(analysis_co2ex(data_buf,len/2,&co2_ppm)==0)//2020-06-11:change len to len/2
    {
   	  last_co2_ppm=co2_ppm;
   	  co2_is_ok=1;
      LOG_DEBUG(TRACE_DEBUG,"analysis co2=%fppm\n",co2_ppm);	
    }
  else
    {
   	 // last_co2_ppm=0;
   	  last_co2_ppm=co2_ppm;//2015-11-11ֱ�ߵ�ʱ���Ի���Ϊ����ֵ 
   	  co2_is_ok=0;
      LOG_DEBUG(ERROR_DEBUG,"analysis co2 failed co2=%fppm\n",co2_ppm);	
    }
  my_file_name=make_data_file_name(now,2);//生成co2原始文件名
  if((ret=save_hex_file(my_file_name,0,(void *)data_buf,len))!=0)
    {
   	  LOG_DEBUG(ERROR_DEBUG,"save bin file failed %s\n",my_file_name);
      return -1;
	}
 
  my_file_name=make_data_file_name(now,3);//生成csv文件名
  if((ret=save_csv_file(my_file_name,0,(void *)data_buf,len))!=0)
    {
      LOG_DEBUG(ERROR_DEBUG,"save csv file failed %s\n",my_file_name);	
      return -2;
	}
	
  return ret; 	
}



