#include "para.h"
#include "common.h"
#include "string.h"
#include "stdio.h"
#include "minIni.h"
#include "ysp_debug.h"


int LoadGasSection(char *sec_name,GAS_CAL_PARA *gas_para)
{
 int i,n;
 char key_name[32];
 gas_para->position.start=ini_getl(sec_name,"start",0,ysp_cfg_file);
 LOG_DEBUG(PARA_TRACE_DEBUG,"%s_start=%d\n",sec_name,gas_para->position.start);
 gas_para->position.peak=ini_getl(sec_name,"peak",0,ysp_cfg_file);
 LOG_DEBUG(PARA_TRACE_DEBUG,"%s_peak=%d\n",sec_name,gas_para->position.peak);
 gas_para->position.end=ini_getl(sec_name,"end",0,ysp_cfg_file);
 LOG_DEBUG(PARA_TRACE_DEBUG,"%s_end=%d\n",sec_name,gas_para->position.end);
 gas_para->position.width=ini_getl(sec_name,"width",10,ysp_cfg_file);
 LOG_DEBUG(PARA_TRACE_DEBUG,"%s_width=%d\n",sec_name,gas_para->position.width);
 	
 gas_para->gradient.lYmin=ini_getf(sec_name,"lYmin",0,ysp_cfg_file);
 LOG_DEBUG(PARA_TRACE_DEBUG,"%s_lYmin=%f\n",sec_name,gas_para->gradient.lYmin);	
 
 gas_para->gradient.lXmax=ini_getf(sec_name,"lXmax",0,ysp_cfg_file);
 LOG_DEBUG(PARA_TRACE_DEBUG,"%s_lXmax=%f\n",sec_name,gas_para->gradient.lXmax);	
 
 gas_para->gradient.rYmin=ini_getf(sec_name,"rYmin",0,ysp_cfg_file);
 LOG_DEBUG(PARA_TRACE_DEBUG,"%s_rYmin=%f\n",sec_name,gas_para->gradient.rYmin);
 	
 gas_para->gradient.rXmax=ini_getf(sec_name,"rXmax",0,ysp_cfg_file);
 LOG_DEBUG(PARA_TRACE_DEBUG,"%s_rXmax=%f\n",sec_name,gas_para->gradient.rXmax);
 	
 gas_para->gradient.rXmax=ini_getf(sec_name,"rXmax",0,ysp_cfg_file);
 LOG_DEBUG(PARA_TRACE_DEBUG,"%s_rXmax=%f\n",sec_name,gas_para->gradient.rXmax);	
 for(i=0;i<12;i++)
   {
   	 for(n=0;n<6;n++)
   	  {
  	    sprintf(key_name,"k%d_fac%d",i+1,n+1);
		gas_para->correct_fac.k[i].fac[n]=ini_getf(sec_name,key_name,0,ysp_cfg_file);
		LOG_DEBUG(PARA_TRACE_DEBUG,"%s_%s=%f\n",sec_name,key_name,gas_para->correct_fac.k[i].fac[n]);	 	
      }  	   
   }
 return 0;
}

int LoadAllPara(YSP_PARA *para_ptr)
{
  //float f;
  int   i; 
  char key_name[64];
  
  ini_gets("MACH","version","1.00",para_ptr->mach_run_para.version,16,ysp_cfg_file);
  ini_gets("MACH","station","dummy station",para_ptr->mach_run_para.station,64,ysp_cfg_file);
  ini_gets("MACH","device","dummy device",para_ptr->mach_run_para.device,64,ysp_cfg_file);           
  LOG_DEBUG(PARA_TRACE_DEBUG,"version=%s\n",para_ptr->mach_run_para.version);
  LOG_DEBUG(PARA_TRACE_DEBUG,"station=%s\n",para_ptr->mach_run_para.station);
  LOG_DEBUG(PARA_TRACE_DEBUG,"device=%s\n",para_ptr->mach_run_para.device);
  para_ptr->mach_run_para.start_hour=ini_getl("MACH","start_hour",10,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"start_hour=%d\n",para_ptr->mach_run_para.start_hour);
  //printf("start_hour=%d\n",para_ptr->mach_run_para.start_hour);
  para_ptr->mach_run_para.start_minute=ini_getl("MACH","start_minute",0,ysp_cfg_file);	
  LOG_DEBUG(PARA_TRACE_DEBUG,"start_minute=%d\n",para_ptr->mach_run_para.start_minute);
  //printf("start_minute=%d\n",para_ptr->mach_run_para.start_minute);
  para_ptr->mach_run_para.start_interval=ini_getl("MACH","sample_interval",4,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"sample_interval=%d\n",para_ptr->mach_run_para.start_interval);
  //printf("sample_interval=%d\n",para_ptr->mach_run_para.start_interval);
  for(i=0;i<8;i++)
  {
  	sprintf(key_name,"pres%d_fac_a",i+1);
    para_ptr->mach_run_para.pres_fac[i].a=ini_getf("MACH",key_name,0.4,ysp_cfg_file);
    LOG_DEBUG(PARA_TRACE_DEBUG,"%s=%f\n",key_name,para_ptr->mach_run_para.pres_fac[i].a);
	sprintf(key_name,"pres%d_fac_b",i+1); 
	para_ptr->mach_run_para.pres_fac[i].b=ini_getf("MACH",key_name,0,ysp_cfg_file);	
    LOG_DEBUG(PARA_TRACE_DEBUG,"%s=%f\n",key_name,para_ptr->mach_run_para.pres_fac[i].b);
  }
  para_ptr->mach_run_para.volt_output[0]=ini_getf("MACH","volt_out1",0,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"volt_out1=%f\n",para_ptr->mach_run_para.volt_output[0]);
  para_ptr->mach_run_para.volt_output[1]=ini_getf("MACH","volt_out2",0,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"volt_out2=%f\n",para_ptr->mach_run_para.volt_output[1]);
  para_ptr->mach_run_para.temperature[0]=ini_getf("MACH","temp1",60,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"temp1=%f\n",para_ptr->mach_run_para.temperature[0]);
  para_ptr->mach_run_para.temperature[1]=ini_getf("MACH","temp2",60,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"temp2=%f\n",para_ptr->mach_run_para.temperature[1]);
  para_ptr->mach_run_para.temperature[2]=ini_getf("MACH","temp3",-40,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"temp3=%f\n",para_ptr->mach_run_para.temperature[2]);
  para_ptr->mach_run_para.temperature[3]=ini_getf("MACH","temp3",0,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"temp3=%f\n",para_ptr->mach_run_para.temperature[3]);
  for(i=0;i<4;i++)
  {
  	sprintf(key_name,"pid_en%d",i+1);
    para_ptr->mach_run_para.pid_en[i]=ini_getf("MACH",key_name,0,ysp_cfg_file);	
    LOG_DEBUG(PARA_TRACE_DEBUG,"%s=%d\n",key_name,para_ptr->mach_run_para.pid_en[i]);
  }
  
  for(i=0;i<4;i++)
  {
  	sprintf(key_name,"kp%d",i+1);
    para_ptr->mach_run_para.pid_type_def[i].Kp=ini_getf("MACH",key_name,12,ysp_cfg_file);
    LOG_DEBUG(PARA_TRACE_DEBUG,"%s=%f\n",key_name,para_ptr->mach_run_para.pid_type_def[i].Kp);
	sprintf(key_name,"ki%d",i+1);
	para_ptr->mach_run_para.pid_type_def[i].Ki=ini_getf("MACH",key_name,0.15,ysp_cfg_file);
    LOG_DEBUG(PARA_TRACE_DEBUG,"%s=%f\n",key_name,para_ptr->mach_run_para.pid_type_def[i].Ki);
	sprintf(key_name,"kd%d",i+1);
	para_ptr->mach_run_para.pid_type_def[i].Kd=ini_getf("MACH",key_name,500,ysp_cfg_file);	
    LOG_DEBUG(PARA_TRACE_DEBUG,"%s=%f\n",key_name,para_ptr->mach_run_para.pid_type_def[i].Kd);
  }
  para_ptr->mach_run_para.sample_len=ini_getl("MACH","sample_len",4000,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"sample_len=%d\n",para_ptr->mach_run_para.sample_len);
  para_ptr->mach_run_para.sample_rate=ini_getl("MACH","sample_rate",220,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"sample_rate=%d\n",para_ptr->mach_run_para.sample_rate);
  para_ptr->mach_run_para.sample_offs=ini_getl("MACH","sample_offs",1800,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"sample_offs=%d\n",para_ptr->mach_run_para.sample_offs);
  
  para_ptr->mach_run_para.sample_cool_en=ini_getl("MACH","sample_cool_en",0,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"sample_cool_en=%d\n",para_ptr->mach_run_para.sample_cool_en);
  
  para_ptr->mach_run_para.co2_pos=ini_getl("MACH","co2_pos",0,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"co2_pos=%d\n",para_ptr->mach_run_para.co2_pos);
  
  para_ptr->mach_run_para.h2o_en=ini_getl("MACH","h2o_en",0,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"h2o_en=%d\n",para_ptr->mach_run_para.h2o_en);
  
  para_ptr->mach_run_para.auto_gas_en=ini_getl("MACH","auto_gas_en",0,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"auto_gas_en=%d\n",para_ptr->mach_run_para.auto_gas_en);
  
  para_ptr->mach_run_para.run_mode=ini_getl("MACH","run_mode",0,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"run_mode=%d\n",para_ptr->mach_run_para.run_mode);
  
  para_ptr->mach_run_para.upload_time=ini_getl("MACH","upload_time",0,ysp_cfg_file);
  LOG_DEBUG(PARA_TRACE_DEBUG,"upload_time=%d\n",para_ptr->mach_run_para.upload_time);
  
  LoadGasSection("H2_CAL",&para_ptr->gas_cal_fac[0]);
  LoadGasSection("CO_CAL",&para_ptr->gas_cal_fac[1]);
  LoadGasSection("CH4_CAL",&para_ptr->gas_cal_fac[2]);
  LoadGasSection("C2H4_CAL",&para_ptr->gas_cal_fac[3]);
  LoadGasSection("C2H6_CAL",&para_ptr->gas_cal_fac[4]);
  LoadGasSection("C2H2_CAL",&para_ptr->gas_cal_fac[5]);
  LoadGasSection("CO2_CAL",&para_ptr->co2_cal_fac);
  return 0;
}

