/*
    goahead.c -- Main program for GoAhead

    Usage: goahead [options] [documents] [IP][:port] ...
        Options:
        --auth authFile        # User and role configuration
        --background           # Run as a Linux daemon
        --home directory       # Change to directory to run
        --log logFile:level    # Log to file file at verbosity level
        --route routeFile      # Route configuration file
        --verbose              # Same as --log stdout:2
        --version              # Output version information

    Copyright (c) All Rights Reserved. See details at the end of the file.
 */

/********************************* Includes ***********************************/

#include "goahead.h"
#include "common.h" 
#include <string.h>
#include "para.h"
#include "minIni.h"
#include "ysp_debug.h" 

#include "sysinfo.h"

/********************************* Defines ************************************/


static int finished = 0;

/********************************* Forwards ***********************************/

static void initPlatform();
static void logHeader();
static void usage();

static int show_sample_para(int eid, Webs *wp, int argc, char **argv);
static void sample_para_upload(Webs *wp);

static int aspTest(int eid, Webs *wp, int argc, char **argv);
static int bigTest(int eid, Webs *wp, int argc, char **argv);

#if ME_GOAHEAD_UPLOAD
static void uploadTest(Webs *wp);
#endif


#if WINDOWS
static void windowsClose();
static int windowsInit();
static LRESULT CALLBACK websWindProc(HWND hwnd, UINT msg, UINT wp, LPARAM lp);
#endif

#if ME_UNIX_LIKE
static void sigHandler(int signo);
#endif 

static char *rly_name_array[32]={
"unused",
"unused",
"unused",
"油缸退YV4",
"油缸进YV5",
"气缸退YV6",
"气缸进YV7",
"样气进气缸阀YV15",
"进油阀YV16",
"unused",
"气缸进油阀YV11",
"进气到集气缸阀YV12",
"出油阀YV13",
"清洗排空阀YV14",
"油泵YV3",	
"unused",
"样气平衡阀YV17",
"吹扫阀YV18",
"定量阀YV19",
"传感器平衡阀YV20",
"CO2检测阀YV21",
"排水阀KA8",
"unused",
"柜子风扇KA3",
"采样单元风扇KA4",
"采样电源控制KA5",
"冷井风扇KA6",
"冷井电源控制KA7",
"unused",
"传感器加热",
"unused",
"气缸启停",
};

static char *io_name_array[8]={
"油缸进I4",
"油缸退I5",
"气缸进I6",
"气缸退I7",
"备用1",
"备用2",
"油位I8",
"进油I9",	
};



static void showTest(Webs *wp)
{
    WebsKey     *s;

    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteEndHeaders(wp);
    websWrite(wp, "<html><body><pre>\n");
    for (s = hashFirst(wp->vars); s; s = hashNext(wp->vars, s)) {
        websWrite(wp,"%s=%s\n", s->name.value.string, s->content.value.string);
    }
    websWrite(wp, "</pre></body></html>\n");
    websDone(wp);
}

static int web_get_para_f(int eid, Webs *wp, int argc, char **argv)
{
 char *sec_name,*key_name;
 float f;
 if(jsArgs(argc,argv,"%s %s",&sec_name,&key_name)<2) 
  {
	websError(wp,400,"Insufficient args\n");
	return -1;
  }
 f=ini_getf(sec_name,key_name,0,ysp_cfg_file);
 return (int) websWrite(wp,"\"%f\"",f);
}

static int web_get_para_i(int eid, Webs *wp,int argc,char **argv)
{
 char *sec_name,*key_name;
 int i;
 if(jsArgs(argc,argv,"%s %s",&sec_name,&key_name)<2) 
  {
	websError(wp,400,"Insufficient args\n");
	return -1;
  }
 i=ini_getl(sec_name,key_name,0,ysp_cfg_file);
 return (int) websWrite(wp,"\"%d\"",i);
}
// 
static int web_show_mx_data(int eid, Webs *wp,int argc,char **argv)
{
 ANALY_RESULT tmp_his_data;
 YSP_PRI_DATA *tmp_pri_data;
 int data_index;
 int val;
 float f;
 struct tm *uptime;
 load_last_result(&tmp_his_data);
 tmp_pri_data=&tmp_his_data.result;
 //if(load_last_result(&tmp_pri_data)==0)
  //{
//	send_ysp_msg(&ysp_pri_data);	
  //}
 if(jsArgs(argc,argv,"%d",&data_index)<1) 
  {
	websError(wp,400,"Insufficient args\n");
	return -1;
  }
 if(data_index<9)
  {
  	switch(data_index)
  	 {
 	  case 0://h2
 	     f=tmp_pri_data->H2ppm;
	     break;
	  case 1://co
	     f=tmp_pri_data->COppm;
	     break;
	  case 2://ch4
	     f=tmp_pri_data->CH4ppm;
	     break;
	  case 3://c2h4
	     f=tmp_pri_data->C2H4ppm;
	     break;
	  case 4://c2h6
	     f=tmp_pri_data->C2H6ppm;
	     break;
	  case 5://c2h2
	     f=tmp_pri_data->C2H2ppm;
	     break;
	  case 6://总烃 
	     f=tmp_pri_data->TotHyd;
	     break;
	  case 7://CO2
	     f=0;
	     break;
	  case 8://H2O 
	     f=0;
	     break;
	  default:
	     break;	
     }
    return (int) websWrite(wp,"\"%f\"",f);	
  }
 else if(data_index<15)
  {
  	uptime=localtime(&(tmp_pri_data->SmpTm));
  	switch(data_index)
  	 {
 	  case 9://year
	     val=uptime->tm_year+1900; 
	     break;
	  case 10://month
	     val=uptime->tm_mon+1; 
	     break;
	  case 11://day
	     val=uptime->tm_mday;
	     break;
	  case 12://hour
	     val=uptime->tm_hour;
	     break;
	  case 13://min
	     val=uptime->tm_min;
	     break;
	  case 14://sec
	     val=uptime->tm_sec;
	     break;
	  default:
	     break;	
     }
    return (int) websWrite(wp,"\"%d\"",val);
  }
}

static int web_get_sys_time(int eid, Webs *wp,int argc,char **argv)
{ 
 int data_index;
 time_t now;
 int val;

 struct tm *uptime;
 if(jsArgs(argc,argv,"%d",&data_index)<1) 
  {
	websError(wp,400,"Insufficient args\n");
	return -1;
  }
 now=time(NULL);
 uptime=localtime(&now);
 switch(data_index)
  {
 	  case 0://year
	     val=uptime->tm_year+1900; 
	     break;
	  case 1://month
	     val=uptime->tm_mon+1; 
	     break;
	  case 2://day
	     val=uptime->tm_mday;
	     break;
	  case 3://hour
	     val=uptime->tm_hour;
	     break;
	  case 4://min
	     val=uptime->tm_min;
	     break;
	  case 5://sec
	     val=uptime->tm_sec;
	     break;
	  default:
	     break;	
  }
  return (int) websWrite(wp,"\"%d\"",val);
}


static void web_mx_data_process(Webs *wp)
{
 char *str;
 int i;
 float f;
 time_t now;
 struct tm uptime;
 ANALY_RESULT tmp_his_data;
 YSP_PRI_DATA *tmp_pri_data;
 tmp_pri_data=&tmp_his_data.result;
 str=websGetVar(wp,"H2","0");
 if(str!=NULL)
 {
  f=atof(str);
  tmp_pri_data->H2ppm=f;
 }
 str=websGetVar(wp,"CO","0");
 if(str!=NULL)
 {
  f=atof(str);
  tmp_pri_data->COppm=f;
 } 
 str=websGetVar(wp,"CH4","0");
 if(str!=NULL)
 {
  f=atof(str);
  tmp_pri_data->CH4ppm=f;
 }
 str=websGetVar(wp,"C2H4","0");
 if(str!=NULL)
 {
  f=atof(str);
  tmp_pri_data->C2H4ppm=f;
 }
 str=websGetVar(wp,"C2H6","0");
 if(str!=NULL)
 {
  f=atof(str);
  tmp_pri_data->C2H6ppm=f;
 } 
 str=websGetVar(wp,"C2H2","0");
 if(str!=NULL)
 {
  f=atof(str);
  tmp_pri_data->C2H2ppm=f;
 }
 tmp_pri_data->TotHyd=tmp_pri_data->CH4ppm+tmp_pri_data->C2H2ppm+tmp_pri_data->C2H4ppm+tmp_pri_data->C2H6ppm;
 str=websGetVar(wp,"year","2015");
 if(str!=NULL)
 {
  i=atoi(str);
  uptime.tm_year=i-1900;
 } 
 str=websGetVar(wp,"month","9");
 if(str!=NULL)
 {
  i=atoi(str);
  uptime.tm_mon=i-1;
 }
 str=websGetVar(wp,"day","1");
 if(str!=NULL)
 {
  i=atoi(str);
  uptime.tm_mday=i;
 }  
 str=websGetVar(wp,"hour","0");
 if(str!=NULL)
 {
  i=atoi(str);
  uptime.tm_hour=i;
 } 
 str=websGetVar(wp,"min","0");
 if(str!=NULL)
 {
  i=atoi(str);
  uptime.tm_min=i;
 } 
 str=websGetVar(wp,"sec","0");
 if(str!=NULL)
 {
  i=atoi(str);
  uptime.tm_sec=i;
 }
 now=mktime(&uptime);
 tmp_pri_data->SmpTm=now;
 save_last_result(&tmp_his_data); 
 send_ysp_msg(tmp_pri_data);
 websRedirect(wp,"/show_data.jst");	  	 	  	  	 	
}

static int web_io_show(int eid, Webs *wp, int argc, char **argv)
{	
 INPUT_TYPE_DEF web_input;
 int io_state;//=my_input_data.io_input;
 int i;
 float temp[7];
 
 lock_input_data();  
 memcpy(&web_input,&my_input_data,sizeof(web_input)); 
 unlock_input_data();
 io_state=web_input.io_input;
 websWrite(wp,"<tr><td width=\"10%\"></td><td colspan=\"4\">");
 for(i=0;i<8;i++)
  {
   if((io_state&(1<<i))!=0)
    {
      websWrite(wp,"<div id=\"i%d\" class=\"on\"><span>%s</span></div>",i+1,io_name_array[i]);	
    }
   else
    {
      websWrite(wp,"<div id=\"i%d\" class=\"off\"><span>%s</span></div>",i+1,io_name_array[i]);	
    }
  }									
 websWrite(wp,"</td></tr>");
 memcpy(&temp,web_input.temperature,sizeof(web_input.temperature));
 get_presure(PRES_PA1,&temp[5]);
 get_presure(PRES_GAS_PUMP,&temp[6]);
 websWrite(wp,"<tr><td colspan=\"4\">色谱柱温度=%5.2f℃   柜内温度=%5.2f℃   冷井温度=%5.2f℃   采样室温度=%5.2f℃   柜外温度=%5.2f℃   载气压力=%5.3fMpa   气缸压力=%5.3fMpa</td></tr>",temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6]);
 websWrite(wp,"<tr><td colspan=\"4\">色谱柱输出=%d  冷井输出=%d   采样室输出=%d   ",web_input.pwm_out[0],web_input.vi_out[0],web_input.vi_out[1]);
 if(web_input.com_flg)
  {
    //微水通讯失败
    websWrite(wp,"微水=---    油温=---"); 
  }
 else
  {
    //微水正常
    websWrite(wp,"微水=%5.2fppm    油温=%5.2f℃",web_input.water,web_input.temp); 	
  }
 websWrite(wp,"</td></tr>");
 return 0;
}


static int web_relay_show(int eid, Webs *wp, int argc, char **argv)
{
 int relay_state;
 int i,tmp;
 relay_state=relay_get_all(NULL);
 for(i=0;i<32;i+=2)
  {
  	tmp=relay_state&(1<<i);
    if(tmp)
      websWrite(wp,"<tr><td align=\"right\" width=\"200\">%s</td><td id=\"y%d_td\"><input type=\"radio\" value=\"1\" name=\"Y%d\" checked>ON<input type=\"radio\" value=\"0\" name=\"Y%d\">OFF</td>",rly_name_array[i],i+1,i+1,i+1);
    else
      websWrite(wp,"<tr><td align=\"right\" width=\"200\">%s</td><td id=\"y%d_td\"><input type=\"radio\" value=\"1\" name=\"Y%d\">ON<input type=\"radio\" value=\"0\" name=\"Y%d\" checked>OFF</td>",rly_name_array[i],i+1,i+1,i+1);
    tmp=relay_state&(1<<(i+1));
    if(tmp)
      {
       websWrite(wp,"<td align=\"right\" width=\"200\">%s</td><td id=\"y%d_td\"><input type=\"radio\" value=\"1\" name=\"Y%d\" checked>ON<input type=\"radio\" value=\"0\" name=\"Y%d\">OFF</td></tr>",rly_name_array[i+1],i+2,i+2,i+2); 
	  }     
    else
      { 
	   websWrite(wp,"<td align=\"right\" width=\"200\">%s</td><td id=\"y%d_td\"><input type=\"radio\" value=\"1\" name=\"Y%d\">ON<input type=\"radio\" value=\"0\" name=\"Y%d\" checked>OFF</td></tr>",rly_name_array[i+1],i+2,i+2,i+2);    	
      }
  }						
 return 0;
}

static int web_temp_para_show(int eid, Webs *wp, int argc, char **argv)
{	
  websWrite(wp,"<td align=\"right\">色谱柱温度控制输出使能</td>");
  if(my_para_data.mach_run_para.pid_en[0])
   {
    websWrite(wp,"<td><input type=\"radio\" name=\"pid_en1\" value=\"0\"/>关闭");
    websWrite(wp,"<input type=\"radio\" name=\"pid_en1\" value=\"1\" checked=\"checked\"/>使能</td>");    	
   }
  else
   {
    websWrite(wp,"<td><input type=\"radio\" name=\"pid_en1\" value=\"0\" checked=\"checked\"/>关闭");
    websWrite(wp,"<input type=\"radio\" name=\"pid_en1\" value=\"1\"/>使能</td>");   	
   }
  websWrite(wp,"<td align=\"right\">冷井温度控制输出使能</td>");
  
  if(my_para_data.mach_run_para.pid_en[2])
   {
    websWrite(wp,"<td><input type=\"radio\" name=\"pid_en3\" value=\"0\"/>关闭");
    websWrite(wp,"<input type=\"radio\" name=\"pid_en3\" value=\"1\" checked=\"checked\"/>使能</td>");   	
   }
  else
   {							
    websWrite(wp,"<td><input type=\"radio\" name=\"pid_en3\" value=\"0\" checked=\"checked\"/>关闭");
    websWrite(wp,"<input type=\"radio\" name=\"pid_en3\" value=\"1\"/>使能</td>");						
   }
  websWrite(wp,"<td align=\"right\">采样室温度控制输出使能</td>");
  
  if(my_para_data.mach_run_para.pid_en[3])
  {
   websWrite(wp,"<td><input type=\"radio\" name=\"pid_en4\" value=\"0\" />关闭");						
   websWrite(wp,"<input type=\"radio\" name=\"pid_en4\" value=\"1\" checked=\"checked\"/>使能</td>");  	
  }
  else
  {
  websWrite(wp,"<td><input type=\"radio\" name=\"pid_en4\" value=\"0\" checked=\"checked\"/>关闭");						
  websWrite(wp,"<input type=\"radio\" name=\"pid_en4\" value=\"1\"/>使能</td>");
  }																		
  return 0;	
}

static void process_para_upload(Webs *wp)
{
 char *str;
 int i;
 float f;
 str=websGetVar(wp,"volt_out1","0");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.mach_run_para.volt_output[0]=f;
  i=(int)f;
  my_output_data.man_output[0]=(unsigned short)i;
  //HeatTuneRecord.tune_output=(unsigned short)i;
  ini_putf("MACH","volt_out1",f,ysp_cfg_file);
 } 
 str=websGetVar(wp,"volt_out2","0");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.mach_run_para.volt_output[1]=f;
  ini_putf("MACH","volt_out2",f,ysp_cfg_file);
 }

  str=websGetVar(wp,"temp1","0");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.mach_run_para.temperature[0]=f;
  ini_putf("MACH","temp1",f,ysp_cfg_file);
 } 	
 
  str=websGetVar(wp,"temp2","0");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.mach_run_para.temperature[1]=f;
  ini_putf("MACH","temp2",f,ysp_cfg_file);
 } 	
 
  str=websGetVar(wp,"temp3","0");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.mach_run_para.temperature[2]=f;
  ini_putf("MACH","temp3",f,ysp_cfg_file);
 } 	
 
  str=websGetVar(wp,"temp4","0");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.mach_run_para.temperature[3]=f;
  ini_putf("MACH","temp4",f,ysp_cfg_file);
 } 	 		
 websRedirect(wp,"/process_param.jst");	
}

static void pres_fac_process(Webs *wp)
{
 char *str;
 char key_name[32];
 int i;
 float f;
 for(i=0;i<8;i++)
  {
  	sprintf(key_name,"pres%d_fac_a",i+1);
  	str=websGetVar(wp,key_name,"1");
    if(str!=NULL)
     {
       f=atof(str);
       my_para_data.mach_run_para.pres_fac[i].a=f;
       ini_putf("MACH",key_name,f,ysp_cfg_file);
     }
     
     
	sprintf(key_name,"pres%d_fac_b",i+1); 
    str=websGetVar(wp,key_name,"0");
    if(str!=NULL)
     {
      f=atof(str);
      my_para_data.mach_run_para.pres_fac[i].b=f;
      ini_putf("MACH",key_name,f,ysp_cfg_file);
     } 
  }
 websRedirect(wp,"/press_factor.jst");		
}

static void H2ParaProcess(Webs *wp)
{
 char *str;
 char key_name[32];
 int i,n;
 float f;
 str=websGetVar(wp,"start","170");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_H2].position.start=i;
  ini_putl("H2_CAL","start",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"peak","256");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_H2].position.peak=i;
  ini_putl("H2_CAL","peak",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"end","320");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_H2].position.end=i;
  ini_putl("H2_CAL","end",i,ysp_cfg_file);
 }
 
 str=websGetVar(wp,"width","16");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_H2].position.width=i;
  ini_putl("H2_CAL","width",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"lYmin","16");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_H2].gradient.lYmin=f;
  ini_putf("H2_CAL","lYmin",f,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"lXmax","32");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_H2].gradient.lXmax=f;
  ini_putf("H2_CAL","lXmax",f,ysp_cfg_file);
 }
 
 str=websGetVar(wp,"rYmin","16");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_H2].gradient.rYmin=f;
  ini_putf("H2_CAL","rYmin",f,ysp_cfg_file);
 }
 
str=websGetVar(wp,"rXmax","32");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_H2].gradient.rXmax=f;
  ini_putf("H2_CAL","rXmax",f,ysp_cfg_file);
 }
 
 for(i=0;i<12;i++)
  {
  	for(n=0;n<6;n++)
  	 {
      sprintf(key_name,"k%d_fac%d",i+1,n+1);
      str=websGetVar(wp,key_name,"1");
      if(str!=NULL)
       {
        f=atof(str);
        my_para_data.gas_cal_fac[INDEX_H2].correct_fac.k[i].fac[n]=f;
        ini_putf("H2_CAL",key_name,f,ysp_cfg_file);
       }  	 	  	
     }
  }
 websRedirect(wp,"/H2.jst");	
}

static void COParaProcess(Webs *wp)
{
 char *str;
 char key_name[32];
 int i,n;
 float f;
 str=websGetVar(wp,"start","290");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_CO].position.start=i;
  ini_putl("CO_CAL","start",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"peak","350");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_CO].position.peak=i;
  ini_putl("CO_CAL","peak",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"end","440");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_CO].position.end=i;
  ini_putl("CO_CAL","end",i,ysp_cfg_file);
 }
 
 str=websGetVar(wp,"width","20");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_CO].position.width=i;
  ini_putl("CO_CAL","width",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"lYmin","4");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_CO].gradient.lYmin=f;
  ini_putf("CO_CAL","lYmin",f,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"lXmax","16");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_CO].gradient.lXmax=f;
  ini_putf("CO_CAL","lXmax",f,ysp_cfg_file);
 }
 
 str=websGetVar(wp,"rYmin","4");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_CO].gradient.rYmin=f;
  ini_putf("CO_CAL","rYmin",f,ysp_cfg_file);
 }
 
str=websGetVar(wp,"rXmax","16");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_CO].gradient.rXmax=f;
  ini_putf("CO_CAL","rXmax",f,ysp_cfg_file);
 }
 
 for(i=0;i<12;i++)
  {
  	for(n=0;n<6;n++)
  	 {
      sprintf(key_name,"k%d_fac%d",i+1,n+1);
      str=websGetVar(wp,key_name,"1");
      if(str!=NULL)
       {
        f=atof(str);
        my_para_data.gas_cal_fac[INDEX_CO].correct_fac.k[i].fac[n]=f;
        ini_putf("CO_CAL",key_name,f,ysp_cfg_file);
       }  	 	  	
     }
  }
 websRedirect(wp,"/CO.jst");	
}

static void CH4ParaProcess(Webs *wp)
{
 char *str;
 char key_name[32];
 int i,n;
 float f;
 str=websGetVar(wp,"start","460");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_CH4].position.start=i;
  ini_putl("CH4_CAL","start",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"peak","515");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_CH4].position.peak=i;
  ini_putl("CH4_CAL","peak",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"end","600");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_CH4].position.end=i;
  ini_putl("CH4_CAL","end",i,ysp_cfg_file);
 }
 
 str=websGetVar(wp,"width","30");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_CH4].position.width=i;
  ini_putl("CH4_CAL","width",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"lYmin","4");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_CH4].gradient.lYmin=f;
  ini_putf("CH4_CAL","lYmin",f,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"lXmax","16");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_CH4].gradient.lXmax=f;
  ini_putf("CH4_CAL","lXmax",f,ysp_cfg_file);
 }
 
 str=websGetVar(wp,"rYmin","4");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_CH4].gradient.rYmin=f;
  ini_putf("CH4_CAL","rYmin",f,ysp_cfg_file);
 }
 
str=websGetVar(wp,"rXmax","16");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_CH4].gradient.rXmax=f;
  ini_putf("CH4_CAL","rXmax",f,ysp_cfg_file);
 }
 
 for(i=0;i<12;i++)
  {
  	for(n=0;n<6;n++)
  	 {
      sprintf(key_name,"k%d_fac%d",i+1,n+1);
      str=websGetVar(wp,key_name,"1");
      if(str!=NULL)
       {
        f=atof(str);
        my_para_data.gas_cal_fac[INDEX_CH4].correct_fac.k[i].fac[n]=f;
        ini_putf("CH4_CAL",key_name,f,ysp_cfg_file);
       }  	 	  	
     }
  }
 websRedirect(wp,"/CH4.jst");	
}

static void C2H4ParaProcess(Webs *wp)
{
 char *str;
 char key_name[32];
 int i,n;
 float f;
 str=websGetVar(wp,"start","1600");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_C2H4].position.start=i;
  ini_putl("C2H4_CAL","start",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"peak","1900");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_C2H4].position.peak=i;
  ini_putl("C2H4_CAL","peak",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"end","2200");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_C2H4].position.end=i;
  ini_putl("C2H4_CAL","end",i,ysp_cfg_file);
 }
 
 str=websGetVar(wp,"width","32");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_C2H4].position.width=i;
  ini_putl("C2H4_CAL","width",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"lYmin","4");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_C2H4].gradient.lYmin=f;
  ini_putf("C2H4_CAL","lYmin",f,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"lXmax","16");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_C2H4].gradient.lXmax=f;
  ini_putf("C2H4_CAL","lXmax",f,ysp_cfg_file);
 }
 
 str=websGetVar(wp,"rYmin","4");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_C2H4].gradient.rYmin=f;
  ini_putf("C2H4_CAL","rYmin",f,ysp_cfg_file);
 }
 
str=websGetVar(wp,"rXmax","16");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_C2H4].gradient.rXmax=f;
  ini_putf("C2H4_CAL","rXmax",f,ysp_cfg_file);
 }
 
 for(i=0;i<12;i++)
  {
  	for(n=0;n<6;n++)
  	 {
      sprintf(key_name,"k%d_fac%d",i+1,n+1);
      str=websGetVar(wp,key_name,"1");
      if(str!=NULL)
       {
        f=atof(str);
        my_para_data.gas_cal_fac[INDEX_C2H4].correct_fac.k[i].fac[n]=f;
        ini_putf("C2H4_CAL",key_name,f,ysp_cfg_file);
       }  	 	  	
     }
  }
 websRedirect(wp,"/C2H4.jst");	
}

static void C2H6ParaProcess(Webs *wp)
{
 char *str;
 char key_name[32];
 int i,n;
 float f;
 str=websGetVar(wp,"start","2201");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_C2H6].position.start=i;
  ini_putl("C2H6_CAL","start",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"peak","2430");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_C2H6].position.peak=i;
  ini_putl("C2H6_CAL","peak",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"end","2800");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_C2H6].position.end=i;
  ini_putl("C2H6_CAL","end",i,ysp_cfg_file);
 }
 
 str=websGetVar(wp,"width","32");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_C2H6].position.width=i;
  ini_putl("C2H6_CAL","width",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"lYmin","6");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_C2H6].gradient.lYmin=f;
  ini_putf("C2H6_CAL","lYmin",f,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"lXmax","16");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_C2H6].gradient.lXmax=f;
  ini_putf("C2H6_CAL","lXmax",f,ysp_cfg_file);
 }
 
 str=websGetVar(wp,"rYmin","4");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_C2H6].gradient.rYmin=f;
  ini_putf("C2H6_CAL","rYmin",f,ysp_cfg_file);
 }
 
str=websGetVar(wp,"rXmax","16");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_C2H6].gradient.rXmax=f;
  ini_putf("C2H6_CAL","rXmax",f,ysp_cfg_file);
 }
 
 for(i=0;i<12;i++)
  {
  	for(n=0;n<6;n++)
  	 {
      sprintf(key_name,"k%d_fac%d",i+1,n+1);
      str=websGetVar(wp,key_name,"1");
      if(str!=NULL)
       {
        f=atof(str);
        my_para_data.gas_cal_fac[INDEX_C2H6].correct_fac.k[i].fac[n]=f;
        ini_putf("C2H6_CAL",key_name,f,ysp_cfg_file);
       }  	 	  	
     }
  }
 websRedirect(wp,"/C2H6.jst");	
}

static void C2H2ParaProcess(Webs *wp)
{
 char *str;
 char key_name[32];
 int i,n;
 float f;
 str=websGetVar(wp,"start","2801");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_C2H2].position.start=i;
  ini_putl("C2H2_CAL","start",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"peak","2930");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_C2H2].position.peak=i;
  ini_putl("C2H2_CAL","peak",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"end","3300");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_C2H2].position.end=i;
  ini_putl("C2H2_CAL","end",i,ysp_cfg_file);
 }
 
 str=websGetVar(wp,"width","32");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.gas_cal_fac[INDEX_C2H2].position.width=i;
  ini_putl("C2H2_CAL","width",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"lYmin","6");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_C2H2].gradient.lYmin=f;
  ini_putf("C2H2_CAL","lYmin",f,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"lXmax","16");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_C2H2].gradient.lXmax=f;
  ini_putf("C2H2_CAL","lXmax",f,ysp_cfg_file);
 }
 
 str=websGetVar(wp,"rYmin","4");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_C2H2].gradient.rYmin=f;
  ini_putf("C2H2_CAL","rYmin",f,ysp_cfg_file);
 }
 
str=websGetVar(wp,"rXmax","16");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.gas_cal_fac[INDEX_C2H2].gradient.rXmax=f;
  ini_putf("C2H2_CAL","rXmax",f,ysp_cfg_file);
 }
 
 for(i=0;i<12;i++)
  {
  	for(n=0;n<6;n++)
  	 {
      sprintf(key_name,"k%d_fac%d",i+1,n+1);
      str=websGetVar(wp,key_name,"1");
      if(str!=NULL)
       {
        f=atof(str);
        my_para_data.gas_cal_fac[INDEX_C2H2].correct_fac.k[i].fac[n]=f;
        ini_putf("C2H2_CAL",key_name,f,ysp_cfg_file);
       }  	 	  	
     }
  }
 websRedirect(wp,"/C2H2.jst");	
}

static void CO2ParaProcess(Webs *wp)
{
 char *str;
 char key_name[32];
 int i,n;
 float f;
 str=websGetVar(wp,"start","80");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.co2_cal_fac.position.start=i;
  ini_putl("CO2_CAL","start",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"peak","100");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.co2_cal_fac.position.peak=i;
  ini_putl("CO2_CAL","peak",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"end","120");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.co2_cal_fac.position.end=i;
  ini_putl("CO2_CAL","end",i,ysp_cfg_file);
 }
 
 str=websGetVar(wp,"width","16");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.co2_cal_fac.position.width=i;
  ini_putl("CO2_CAL","width",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"lYmin","6");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.co2_cal_fac.gradient.lYmin=f;
  ini_putf("CO2_CAL","lYmin",f,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"lXmax","16");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.co2_cal_fac.gradient.lXmax=f;
  ini_putf("CO2_CAL","lXmax",f,ysp_cfg_file);
 }
 
 str=websGetVar(wp,"rYmin","4");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.co2_cal_fac.gradient.rYmin=f;
  ini_putf("CO2_CAL","rYmin",f,ysp_cfg_file);
 }
 
str=websGetVar(wp,"rXmax","16");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.co2_cal_fac.gradient.rXmax=f;
  ini_putf("CO2_CAL","rXmax",f,ysp_cfg_file);
 }
 
 for(i=0;i<12;i++)
  {
  	for(n=0;n<6;n++)
  	 {
      sprintf(key_name,"k%d_fac%d",i+1,n+1);
      str=websGetVar(wp,key_name,"1");
      if(str!=NULL)
       {
        f=atof(str);
        my_para_data.co2_cal_fac.correct_fac.k[i].fac[n]=f;
        ini_putf("CO2_CAL",key_name,f,ysp_cfg_file);
       }  	 	  	
     }
  }
 websRedirect(wp,"/CO2.jst");	
}
						

static int web_ctrl_state_show(int eid, Webs *wp, int argc, char **argv)
{
  if(curr_state==SIX_SAMPLE)
    websWrite(wp,"<input type=\"radio\" name=\"command\"  value=\"1\" checked/>启动采样");	
  else
    websWrite(wp,"<input type=\"radio\" name=\"command\"  value=\"1\"/>启动采样");
  
  if(curr_state==DEMARCATE)
    websWrite(wp,"<input type=\"radio\" name=\"command\" value=\"2\" checked/>启动标定");
  else
    websWrite(wp,"<input type=\"radio\" name=\"command\" value=\"2\"/>启动标定");
  
  if(curr_state==REMOVE_GAS)
    websWrite(wp,"<input type=\"radio\" name=\"command\" value=\"3\" checked/>启动脱气机");
  else
    websWrite(wp,"<input type=\"radio\" name=\"command\" value=\"3\"/>启动脱气机");
 
  if(curr_state==GAS_FAULT_PROCESS)
    websWrite(wp,"<input type=\"radio\" name=\"command\" value=\"4\" checked/>脱气机故障处理");
  else
    websWrite(wp,"<input type=\"radio\" name=\"command\" value=\"4\"/>脱气机故障处理");
  
  if(curr_state==PUMP_OIL_PROCESS)
   websWrite(wp,"<input type=\"radio\" name=\"command\" value=\"5\" checked/>启动油泵");
  else
   websWrite(wp,"<input type=\"radio\" name=\"command\" value=\"5\"/>启动油泵");
  
  if(curr_state==MODE_IDLE)
   websWrite(wp,"<input type=\"radio\" name=\"command\" value=\"0\" checked/>终止当前操作");
  else
   websWrite(wp,"<input type=\"radio\" name=\"command\" value=\"0\"/>终止当前操作");
  return 0;
}

static int web_man_ctrl_show(int eid, Webs *wp, int argc, char **argv)
{  				  	
  websWrite(wp,"<tr><td align=\"center\" id=\"tune_td\">"); 
  if(HeatTuneRecord.auto_turn)
   {
    websWrite(wp,"<input type=\"radio\" name=\"autotune\" value=\"1\" checked/>启动参数整定");	
    websWrite(wp,"<input type=\"radio\" name=\"autotune\" value=\"0\"/>停止参数整定");
   }      
  else
   {
   	websWrite(wp,"<input type=\"radio\" name=\"autotune\" value=\"1\"/>启动参数整定");	
    websWrite(wp,"<input type=\"radio\" name=\"autotune\" value=\"0\" checked/>停止参数整定");
   } 
  websWrite(wp,"</td></tr>");
    
  websWrite(wp,"<tr><td align=\"center\" id=\"lj_td\">"); 
  if(my_output_data.pid_en[PID_LJ])
   {
    websWrite(wp,"<input type=\"radio\" name=\"lj\" value=\"1\" checked/>启动冷井");	
    websWrite(wp,"<input type=\"radio\" name=\"lj\" value=\"0\"/>停止冷井");
   }      
  else
   {
   	websWrite(wp,"<input type=\"radio\" name=\"lj\" value=\"1\"/>启动冷井");	
    websWrite(wp,"<input type=\"radio\" name=\"lj\" value=\"0\" checked/>停止冷井");
   } 
  websWrite(wp,"</td></tr>");  
  
  websWrite(wp,"<tr><td align=\"center\" id=\"sample_td\">"); 
  if(my_output_data.pid_en[PID_SAMPLE])
   {
    websWrite(wp,"<input type=\"radio\" name=\"cy\" value=\"1\" checked/>启动采样单元");	
    websWrite(wp,"<input type=\"radio\" name=\"cy\" value=\"0\"/>停止采样单元");
   }      
  else
   {
   	websWrite(wp,"<input type=\"radio\" name=\"cy\" value=\"1\"/>启动采样单元");	
    websWrite(wp,"<input type=\"radio\" name=\"cy\" value=\"0\" checked/>停止采样单元");
   } 
  websWrite(wp,"</td></tr>");			
  return 0;
}


static void man_ctrl_process(Webs *wp)
{
 char *str;
  int tmp;
 str=websGetVar(wp,"autotune","0");
 if(str!=NULL)
  {
    tmp=atoi(str);
    if(HeatTuneRecord.auto_turn==0)
      {
      	if(tmp!=0)
      	 {
           HeatTuneRecord.tune_output=my_output_data.man_output[0];
 	       HeatAutoTuner.start_time=time(NULL);	
         }
      }
    else
      {
        if(tmp==0)
		    {
 			
 		    }	
      }
    HeatTuneRecord.auto_turn=tmp; 	  	
  }
  
 str=websGetVar(wp,"command","0");
 if(str!=NULL)
  {
    tmp=atoi(str);
    if(curr_state==0)
      {
      	if(tmp!=0)
          SendActionReq(tmp,0);	
      }
    else
      {
     	if(tmp==0)
		  {
  		    stop_curr_process(0);	
  		  }	
      }	  	
  }
  
 str=websGetVar(wp,"lj","0");
 if(str!=NULL)
  {
    tmp=atoi(str);
    if(curr_state==0)
     { 
      if(tmp)
        {
          //启动冷井
		  my_output_data.pid_en[PID_LJ]=1;
	      //relay_set(RLY_KA5,ON,0);//开冷井电源
		  relay_set(RLY_KA6,ON,1);//开冷井风扇 	
        }
      else
        {
          //停止冷井
		  my_output_data.pid_en[PID_LJ]=0;
		  //relay_set(RLY_KA5,OFF,0);//关冷井电源
		  relay_set(RLY_KA6,OFF,1);//关冷井风扇 	
        }
     } 
  }
  
 str=websGetVar(wp,"cy","0");
 if(str!=NULL)
  {
    tmp=atoi(str);
   if(curr_state==0)
     { 
      if(tmp)
        {
          //启动采样单元
		  my_output_data.pid_en[PID_SAMPLE]=1;
		   relay_set(RLY_FAN,ON,0); 	
		  relay_set(RLY_KA4,ON,1);//
	     
        }
      else
        {
          //停止采样单元
		  my_output_data.pid_en[PID_SAMPLE]=0;
		  relay_set(RLY_FAN,OFF,0); 
		  relay_set(RLY_KA4,OFF,1);//
	      //relay_set(RLY_KA5,OFF,1); 	
        }
     } 
  } 
 sleep(2);
 websRedirect(wp,"/man_control.jst");	
}


static void web_temp_ctrl_process(Webs *wp)
{
 char *str;
 int i;
 float f;
 str=websGetVar(wp,"pid_en1","1");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.mach_run_para.pid_en[0]=i;
  my_output_data.pid_en[0]=i;
  ini_putl("MACH","pid_en1",i,ysp_cfg_file);
 } 
 
 str=websGetVar(wp,"pid_en3","0");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.mach_run_para.pid_en[2]=i;
  //my_output_data.pid_en[2]=i;
  ini_putl("MACH","pid_en3",i,ysp_cfg_file);
 }
 
 str=websGetVar(wp,"pid_en4","0");
 if(str!=NULL)
 {
  i=atoi(str);
  my_para_data.mach_run_para.pid_en[3]=i;
  //my_output_data.pid_en[3]=i;
  ini_putl("MACH","pid_en4",i,ysp_cfg_file);
 }  
 str=websGetVar(wp,"kp1","0");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.mach_run_para.pid_type_def[0].Kp=f;
  my_output_data.pid_para[0].Kp=f;
  ini_putf("MACH","kp1",f,ysp_cfg_file);
 }
 str=websGetVar(wp,"ki1","0");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.mach_run_para.pid_type_def[0].Ki=f;
  my_output_data.pid_para[0].Ki=f;
  ini_putf("MACH","ki1",f,ysp_cfg_file);
 } 
 str=websGetVar(wp,"kd1","0");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.mach_run_para.pid_type_def[0].Kd=f;
  my_output_data.pid_para[0].Kd=f;
  ini_putf("MACH","kd1",f,ysp_cfg_file);
 } 
  str=websGetVar(wp,"kp3","0");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.mach_run_para.pid_type_def[2].Kp=f;
  my_output_data.pid_para[2].Kp=f;
  ini_putf("MACH","kp3",f,ysp_cfg_file);
 }
 str=websGetVar(wp,"ki3","0");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.mach_run_para.pid_type_def[2].Ki=f;
  my_output_data.pid_para[2].Ki=f;
  ini_putf("MACH","ki3",f,ysp_cfg_file);
 } 
 str=websGetVar(wp,"kd3","0");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.mach_run_para.pid_type_def[2].Kd=f;
  my_output_data.pid_para[2].Kd=f;
  ini_putf("MACH","kd3",f,ysp_cfg_file);
 }
 str=websGetVar(wp,"kp4","0");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.mach_run_para.pid_type_def[3].Kp=f;
  my_output_data.pid_para[3].Kp=f;
  ini_putf("MACH","kp4",f,ysp_cfg_file);
 }
 str=websGetVar(wp,"ki4","0");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.mach_run_para.pid_type_def[3].Ki=f;
  my_output_data.pid_para[3].Ki=f;
  ini_putf("MACH","ki4",f,ysp_cfg_file);
 } 
 str=websGetVar(wp,"kd4","0");
 if(str!=NULL)
 {
  f=atof(str);
  my_para_data.mach_run_para.pid_type_def[3].Kd=f;
  my_output_data.pid_para[3].Kd=f;
  ini_putf("MACH","kd4",f,ysp_cfg_file);
 }     
 websRedirect(wp,"/temp_factor.jst");	
}

static void web_relay_ctrl(Webs *wp)
{
 extern unsigned int curr_state;
 int relay_val=0;
 int i,tmp;
 char *str;
 char var_name[8];
 for(i=0;i<32;i++)
  {
   sprintf(var_name,"Y%d",i+1);
   str=websGetVar(wp,var_name,"0");
   if(str!=NULL)
    {
     tmp=atoi(str);
     if(tmp)
      {
       relay_val|=(1<<i);	
      }
    }  	
  }
 if(curr_state==MODE_IDLE)
  {
   relay_set_all(relay_val,1);
   usleep(1000000);  	
  }
 websRedirect(wp,"/relay_state_ctrl.jst");
}

static void analysis_file_process(Webs *wp)
{
 char *str;
 struct stat buf;
 char file_path[128];
 str=websGetVar(wp,"file_name","dumyfile.bin");
 if(str!=NULL)
  {
    if(strcmp(str,"dumyfile.bin")!=0)
	  { //2021-10-28:波形目录修改至/usr_app/data目录
	  	sprintf(file_path,"/usr_app/data/%s",str);//sprintf(file_path,"/COMTRADE/%s",str);
        if(stat(file_path,&buf)==0)
		  {
  		    if(buf.st_size>4096)
			  {
  				send_analysis_req(file_path,strlen(file_path));
				sleep(1);
				websRedirect(wp,"/show_data.jst"); 
  			  }	
  		  }	
  	  }	
  }
 websRedirect(wp,"/sample_param.jst");   
}

//保存用户上传的参数,用户提交保存按钮后的处理函数 
static void sample_para_upload(Webs *wp)
{
 char *str;
  int tmp;
 str=websGetVar(wp,"start_hour","4");
 if(str!=NULL)
  {
    tmp=atoi(str);
    my_para_data.mach_run_para.start_hour=tmp;
	ini_putl("MACH","start_hour",tmp,ysp_cfg_file);  	
  }
  
 str=websGetVar(wp,"start_minute","0");
 if(str!=NULL)
  {
    tmp=atoi(str);
    my_para_data.mach_run_para.start_minute=tmp;
	ini_putl("MACH","start_minute",tmp,ysp_cfg_file);   	
  }

  str=websGetVar(wp,"start_interval","4");
 if(str!=NULL)
  {
    tmp=atoi(str);
    my_para_data.mach_run_para.start_interval=tmp;
	ini_putl("MACH","sample_interval",tmp,ysp_cfg_file);   	
  }
  
 str=websGetVar(wp,"sample_len","4000");
 if(str!=NULL)
  {
    tmp=atoi(str);
    my_para_data.mach_run_para.sample_len=tmp; 
	ini_putl("MACH","sample_len",tmp,ysp_cfg_file);  	
  }
  
 str=websGetVar(wp,"sample_rate","220");
 if(str!=NULL)
  {
    tmp=atoi(str);
    my_para_data.mach_run_para.sample_rate=tmp; 
	ini_putl("MACH","sample_rate",tmp,ysp_cfg_file);  	
  }
  
 str=websGetVar(wp,"sample_offs","1600");
 if(str!=NULL)
  {
    tmp=atoi(str);
    my_para_data.mach_run_para.sample_offs=tmp; 
	ini_putl("MACH","sample_offs",tmp,ysp_cfg_file);  	
  }
  
 str=websGetVar(wp,"co2_pos","0");
 if(str!=NULL)
  {
    tmp=atoi(str);
    my_para_data.mach_run_para.co2_pos=tmp; 
	ini_putl("MACH","co2_pos",tmp,ysp_cfg_file);  	
  }
  
 str=websGetVar(wp,"sample_cool_en","0");
 if(str!=NULL)
  {
    tmp=atoi(str);
    my_para_data.mach_run_para.sample_cool_en=tmp; 
	ini_putl("MACH","sample_cool_en",tmp,ysp_cfg_file);  	
  }
  
 str=websGetVar(wp,"h2o_en","0");
 if(str!=NULL)
  {
    tmp=atoi(str);
    my_para_data.mach_run_para.h2o_en=tmp; 
	ini_putl("MACH","h2o_en",tmp,ysp_cfg_file);  	
  }
  
 str=websGetVar(wp,"run_mode","0");
 if(str!=NULL)
  {
    tmp=atoi(str);
    my_para_data.mach_run_para.run_mode=tmp; 
	ini_putl("MACH","run_mode",tmp,ysp_cfg_file);  	
  }
  
 str=websGetVar(wp,"auto_gas_en","0");
 if(str!=NULL)
  {
    tmp=atoi(str);
    my_para_data.mach_run_para.auto_gas_en=tmp; 
	ini_putl("MACH","auto_gas_en",tmp,ysp_cfg_file);  	
  }

 str=websGetVar(wp,"upload_time","4");
 if(str!=NULL)
  {
    tmp=atoi(str);
    my_para_data.mach_run_para.upload_time=tmp; 
	ini_putl("MACH","upload_time",tmp,ysp_cfg_file);  	
  } 
 websRedirect(wp,"/sample_param.jst");
}

/*********************************** Code *************************************/

void *goahead(void *argv)
{
    char    *argp, *home, *documents, *endpoints, *endpoint, *route, *auth, *tok, *lspec;
    int     argind;

    //sleep(16);
    route = "route.txt";
    auth = "auth.txt";

    documents = ME_GOAHEAD_DOCUMENTS;
    if (argv != NULL) {
        documents = (char *)argv;
    }
    //initPlatform();
    if (websOpen(documents, route) < 0) {
        error("Cannot initialize server. Exiting.");
        return -1;
    }
    if (websLoad(auth) < 0) {
        error("Cannot load %s", auth);
        return -1;
    }
    logHeader();
    /*if (argind < argc) {
        while (argind < argc) {
            endpoint = argv[argind++];
            if (websListen(endpoint) < 0) {
                return -1;
            }
        }
    } else {*/
        endpoints = sclone(ME_GOAHEAD_LISTEN);
        for (endpoint = stok(endpoints, ", \t", &tok); endpoint; endpoint = stok(NULL, ", \t,", &tok)) {
#if !ME_COM_SSL
            if (strstr(endpoint, "https")) continue;
#endif
            if (websListen(endpoint) < 0) {
                return -1;
            }
        }
        wfree(endpoints);
    //}
#if ME_ROM && KEEP
    /*
        If not using a route/auth config files, then manually create the routes like this:
        If custom matching is required, use websSetRouteMatch. If authentication is required, use websSetRouteAuth.
     */
    websAddRoute("/","file",0);
#endif
    websDefineJst("show_sample_para",show_sample_para);	
    websDefineJst("show_rly_state",web_relay_show);	
	websDefineJst("show_io",web_io_show);	
	websDefineJst("temp_para_show",web_temp_para_show);	
	websDefineJst("get_para_f",web_get_para_f);
	websDefineJst("get_para_i",web_get_para_i);
	websDefineJst("man_ctrl_show",web_man_ctrl_show);
	websDefineJst("show_mx_data",web_show_mx_data);
	websDefineJst("showTest",showTest);	
    websDefineJst("ctrl_state_show",web_ctrl_state_show);
	websDefineJst("get_sys_info",web_get_sys_para);
	websDefineJst("get_sys_time",web_get_sys_time);
	
	websDefineAction("sample_para_upload",sample_para_upload);
	websDefineAction("web_relay_ctrl",web_relay_ctrl);
	websDefineAction("temp_ctrl_process",web_temp_ctrl_process);
	websDefineAction("process_para_upload",process_para_upload);
	websDefineAction("pres_fac_process",pres_fac_process);
	websDefineAction("H2ParaProcess",H2ParaProcess);
	websDefineAction("COParaProcess",COParaProcess);
	websDefineAction("CH4ParaProcess",CH4ParaProcess);
	websDefineAction("C2H4ParaProcess",C2H4ParaProcess);
	websDefineAction("C2H6ParaProcess",C2H6ParaProcess);
	websDefineAction("C2H2ParaProcess",C2H2ParaProcess);
	websDefineAction("CO2ParaProcess",CO2ParaProcess);
	websDefineAction("man_ctrl_process",man_ctrl_process);
	websDefineAction("mx_data_process",web_mx_data_process);
	websDefineAction("sys_reboot",sys_reboot);
	websDefineAction("ethx_process",ethx_process);
	websDefineAction("sntp_process",sntp_process);
	websDefineAction("rtc_process",rtc_process);
	websDefineAction("ied_rename",ied_rename_process);
	websDefineAction("analysis_file_process",analysis_file_process);
#if ME_GOAHEAD_JAVASCRIPT
    websDefineJst("aspTest",aspTest);
#endif

#if ME_GOAHEAD_UPLOAD
    websDefineAction("uploadTest",uploadTest);
#endif
/*
#if ME_UNIX_LIKE && !MACOSX
    if (websGetBackground()) {
        if (daemon(0, 0) < 0) {
            error("Cannot run as daemon");
            return -1;
        }
    }
#endif*/
    websServiceEvents(&finished);
    logmsg(1, "Instructed to exit");
    websClose();
#if WINDOWS
    windowsClose();
#endif
    return ((void *)0);
}

//#if ME_GOAHEAD_JAVASCRIPT
/*
    Parse the form variables: name, address and echo back
 */
static int aspTest(int eid, Webs *wp, int argc, char **argv)
{
	char	*name, *address;

	if (jsArgs(argc, argv, "%s %s", &name, &address) < 2) {
		websError(wp, 400, "Insufficient args\n");
		return -1;
	}
	return (int) websWrite(wp, "Name: %s, Address %s", name, address);
}


//显示参数 
static int show_sample_para(int eid, Webs *wp, int argc, char **argv)
{				 				  
 websWrite(wp,"<tr><td align=\"right\">开始采样时刻(0-23)</td><td><input type=\"text\" size=\"8\" name=\"start_hour\" value=\"%d\"/>时<input type=\"text\" size=\"8\" name=\"start_minute\" value=\"%d\"/>分</td>",my_para_data.mach_run_para.start_hour,my_para_data.mach_run_para.start_minute);				  
 websWrite(wp,"<td align=\"right\">采样间隔(2-24时)</td><td><input type=\"text\" name=\"start_interval\" value=\"%d\"/>时</td></tr>",my_para_data.mach_run_para.start_interval);
 websWrite(wp,"<tr><td align=\"right\">采样长度(100-5000)</td><td><input type=\"text\" name=\"sample_len\" value=\"%d\"/></td>",my_para_data.mach_run_para.sample_len);
 websWrite(wp,"<td align=\"right\">采样速率(ms)</td><td><input type=\"text\" name=\"sample_rate\" value=\"%d\"/></td></tr>",my_para_data.mach_run_para.sample_rate);
 websWrite(wp,"<tr><td align=\"right\">采样偏置(0-4095)</td><td><input type=\"text\" name=\"sample_offs\" value=\"%d\"/></td>",my_para_data.mach_run_para.sample_offs);
 websWrite(wp,"<td align=\"right\">CO2切换位置(0-5000)</td><td><input type=\"text\" name=\"co2_pos\" value=\"%d\"/></td></tr>",my_para_data.mach_run_para.co2_pos);
 
 websWrite(wp,"<tr><td align=\"right\" >采样室制冷</td><td id=\"sample_cool\">");
 if(my_para_data.mach_run_para.sample_cool_en)
   websWrite(wp,"<input type=\"radio\" value=\"1\" name=\"sample_cool_en\" checked>开启<input type=\"radio\" value=\"0\" name=\"sample_cool_en\">关闭</td>");
 else
   websWrite(wp,"<input type=\"radio\" value=\"1\" name=\"sample_cool_en\">开启<input type=\"radio\" value=\"0\" name=\"sample_cool_en\" checked>关闭</td>");
 
 websWrite(wp,"<td align=\"right\" >微水检测使能</td><td id=\"h2o\">");
 if(my_para_data.mach_run_para.h2o_en)
   websWrite(wp,"<input type=\"radio\" value=\"1\" name=\"h2o_en\" checked>开启<input type=\"radio\" value=\"0\" name=\"h2o_en\">关闭</td></tr>");
 else
   websWrite(wp,"<input type=\"radio\" value=\"1\" name=\"h2o_en\">开启<input type=\"radio\" value=\"0\" name=\"h2o_en\" checked>关闭</td></tr>");
 
 if(my_para_data.mach_run_para.run_mode)
   {
   	 websWrite(wp,"<tr><td align=\"right\" >运行模式</td><td id=\"run_mode\"><input type=\"radio\" value=\"1\" name=\"run_mode\" checked>调试<input type=\"radio\" value=\"0\" name=\"run_mode\">正常</td>");
   }
 else
   {
   	 websWrite(wp,"<tr><td align=\"right\" >运行模式</td><td id=\"run_mode\"><input type=\"radio\" value=\"1\" name=\"run_mode\">调试<input type=\"radio\" value=\"0\" name=\"run_mode\" checked>正常</td>");
   }
   
 if(my_para_data.mach_run_para.auto_gas_en)
   {
   	 websWrite(wp,"<td align=\"right\" >自动制气</td><td id=\"auto_gas_en\"><input type=\"radio\" value=\"1\" name=\"auto_gas_en\" checked>使能<input type=\"radio\" value=\"0\" name=\"auto_gas_en\">关闭</td></tr>");
   }
 else
   {
   	 websWrite(wp,"<td align=\"right\" >自动制气</td><td id=\"auto_gas_en\"><input type=\"radio\" value=\"1\" name=\"auto_gas_en\">使能<input type=\"radio\" value=\"0\" name=\"auto_gas_en\" checked>关闭</td></tr>");
   }
 websWrite(wp,"<tr><td align=\"right\">数据上传间隔(小时)</td><td><input type=\"text\" name=\"upload_time\" value=\"%d\"/></td><td></td></tr>",my_para_data.mach_run_para.upload_time);
 return 0;
}





//#endif


static void logHeader()
{
    char    home[ME_GOAHEAD_LIMIT_STRING];

    getcwd(home, sizeof(home));
    logmsg(2, "Configuration for %s", ME_TITLE);
    logmsg(2, "---------------------------------------------");
    logmsg(2, "Version:            %s", ME_VERSION);
    logmsg(2, "BuildType:          %s", ME_DEBUG ? "Debug" : "Release");
    logmsg(2, "CPU:                %s", ME_CPU);
    logmsg(2, "OS:                 %s", ME_OS);
    logmsg(2, "Host:               %s", websGetServer());
    logmsg(2, "Directory:          %s", home);
    logmsg(2, "Documents:          %s", websGetDocuments());
    logmsg(2, "Configure:          %s", ME_CONFIG_CMD);
    logmsg(2, "---------------------------------------------");
}


static void usage() {
    fprintf(stdout, "\n%s Usage:\n\n"
        "  %s [options] [documents] [[IPaddress][:port] ...]\n\n"
        "  Options:\n"
        "    --auth authFile        # User and role configuration\n"
#if ME_UNIX_LIKE && !MACOSX
        "    --background           # Run as a Unix daemon\n"
#endif
        "    --debugger             # Run in debug mode\n"
        "    --home directory       # Change to directory to run\n"
        "    --log logFile:level    # Log to file file at verbosity level\n"
        "    --route routeFile      # Route configuration file\n"
        "    --verbose              # Same as --log stdout:2\n"
        "    --version              # Output version information\n\n",
        ME_TITLE, ME_NAME);
    exit(-1);
}


static void initPlatform() 
{
#if ME_UNIX_LIKE
    signal(SIGTERM, sigHandler);
    signal(SIGKILL, sigHandler);
    #ifdef SIGPIPE
        signal(SIGPIPE, SIG_IGN);
    #endif
#elif ME_WIN_LIKE
    _fmode=_O_BINARY;
#endif
}


#if ME_UNIX_LIKE
static void sigHandler(int signo)
{
    finished = 1;
}
#endif


#if WINDOWS
/*
    Create a taskbar entry. Register the window class and create a window
 */
static int windowsInit()
{
    HINSTANCE   inst;
    WNDCLASS    wc;                     /* Window class */
    HMENU       hSysMenu;
    HWND        hwnd;

    inst = websGetInst();
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = inst;
    wc.hIcon         = NULL;
    wc.lpfnWndProc   = (WNDPROC) websWindProc;
    wc.lpszMenuName  = wc.lpszClassName = ME_NAME;
    if (! RegisterClass(&wc)) {
        return -1;
    }
    /*
        Create a window just so we can have a taskbar to close this web server
     */
    hwnd = CreateWindow(ME_NAME, ME_TITLE, WS_MINIMIZE | WS_POPUPWINDOW, CW_USEDEFAULT, 
        0, 0, 0, NULL, NULL, inst, NULL);
    if (hwnd == NULL) {
        return -1;
    }

    /*
        Add the about box menu item to the system menu
     */
    hSysMenu = GetSystemMenu(hwnd, FALSE);
    if (hSysMenu != NULL) {
        AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL);
    }
    ShowWindow(hwnd, SW_SHOWNORMAL);
    UpdateWindow(hwnd);
    return 0;
}


static void windowsClose()
{
    HINSTANCE   inst;

    inst = websGetInst();
    UnregisterClass(ME_NAME, inst);
}


/*
    Main menu window message handler.
 */
static LRESULT CALLBACK websWindProc(HWND hwnd, UINT msg, UINT wp, LPARAM lp)
{
    switch (msg) {
        case WM_DESTROY:
            PostQuitMessage(0);
            finished++;
            return 0;

        case WM_SYSCOMMAND:
            break;
    }
    return DefWindowProc(hwnd, msg, wp, lp);
}


/*
    Check for Windows Messages
 */
WPARAM checkWindowsMsgLoop()
{
    MSG     msg;

    if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
        if (!GetMessage(&msg, NULL, 0, 0) || msg.message == WM_QUIT) {
            return msg.wParam;
        }
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}


/*
    Windows message handler
 */
static LRESULT CALLBACK websAboutProc(HWND hwndDlg, uint msg, uint wp, long lp)
{
    LRESULT    lResult;

    lResult = DefWindowProc(hwndDlg, msg, wp, lp);

    switch (msg) {
        case WM_CREATE:
            break;
        case WM_DESTROY:
            break;
        case WM_COMMAND:
            break;
    }
    return lResult;
}

#endif

#if ME_GOAHEAD_UPLOAD
/*
    Dump the file upload details. Don't actually do anything with the uploaded file.
 */
static void uploadTest(Webs *wp)
{
    WebsKey         *s;
    WebsUpload  *up;
    char            *upfile;

    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteHeader(wp, "Content-Type", "text/plain");
    websWriteEndHeaders(wp);
    if (scaselessmatch(wp->method, "POST")) {
        for (s = hashFirst(wp->files); s; s = hashNext(wp->files, s)) {
            up = s->content.value.symbol;
            websWrite(wp, "FILE: %s\r\n", s->name.value.string);
            websWrite(wp, "FILENAME=%s\r\n", up->filename);///COMTRADE/tmp-0.tmp
            websWrite(wp, "CLIENT=%s\r\n", up->clientFilename);//small.dat
            websWrite(wp, "TYPE=%s\r\n", up->contentType);
            websWrite(wp, "SIZE=%d\r\n", up->size);
            //upfile = sfmt("%s/tmp/%s",websGetDocuments(),up->clientFilename);
            upfile = sfmt("%s/%s","/COMTRADE",up->clientFilename);
            printf("upfile=%s\n",upfile);
            rename(up->filename, upfile);
            wfree(upfile);
        }
        websWrite(wp, "\r\nVARS:\r\n");
        for (s = hashFirst(wp->vars); s; s = hashNext(wp->vars, s)) {
            websWrite(wp, "%s=%s\r\n", s->name.value.string, s->content.value.string);
        }
    }
    websDone(wp);
}
#endif


int goahead_init(void)
{
  pthread_t goahead_thread;
  if((goahead_thread=task_create(goahead,WEB_DIR,0,0))==-1)
   {
	 LOG_DEBUG(ERROR_DEBUG,"create goahead_routine failed\n");
	 return 1;
   }
  return 0;
}

/*
    @copy   default

    Copyright (c) Embedthis Software. All Rights Reserved.

    This software is distributed under commercial and open source licenses.
    You may use the Embedthis GoAhead open source license or you may acquire 
    a commercial license from Embedthis Software. You agree to be fully bound
    by the terms of either license. Consult the LICENSE.md distributed with
    this software for full details and other copyrights.

    Local variables:
    tab-width: 4
    c-basic-offset: 4
    End:
    vim: sw=4 ts=4 expandtab

    @end
 */
