/*
 * main.c
 *
 *  Created on: Apr 15, 2015
 *      Author: root
 */
#include <signal.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include "ysp_debug.h"
#include "msq.h"
#include "main.h"
#include "common.h"
#include "para.h"
#include "analysis.h"
#include "serial.h"
#include "sample_process.h"
#include "ctrl_process.h"
#include "iec61850_process.h"
//#include "modbus_rtu_slave.h"

//#define APP_VERSION "ysp application v3.3i(2020-09-17 13:00)" 
//#define APP_VERSION "ysp application v3.3j(2021-05-25 13:00)" 
//#define APP_VERSION "ysp application v3.3k(2022-08-15 15:00)" 
//#define APP_VERSION "ysp application v3.3l(2022-09-19 23:00)" 
#define APP_VERSION "ysp application v3.3m(2022-09-26 10:00)" 
/*
2020-09-17 v3.3i 添加modbus rtu slave传输数据
*/

typedef struct 
{
 unsigned int room_start_time;//采样室开始时刻 
 unsigned int heat_start_time;//传感器加热开始时刻 
 unsigned int yv18_start_time;//吹扫阀开启时间 
 unsigned int yv18_poll_time; //
 unsigned int sample_start_time;//最近一次采样开始时刻 
 unsigned int dummy_sample_time; 
}TIME_RECORD_DEF;

extern int sample_fd;
CLEAN_STAT gas_clean_stat;
CLEAN_STAT gas_make_stat;
CLEAN_STAT demarcate_stat;
//PRE_ACTION_STATE my_action_state;
TIME_RECORD_DEF time_record;

SAMPLE_PARA_TYPE sample_para;

unsigned int relay_close_tick[32];
unsigned int relay_open_tick[32];

unsigned char main_rx_buf[MAIN_MESSAGE_SIZE];

mqd_t main_mq=(mqd_t)(-1);

time_t last_sample_time_ex;
static int doIt=1;

unsigned int curr_state=0;

static char *rly_name[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",
"气缸启停",
};
/*
const float sample_temp_table[]={
0,0,0,0,0,0,0,0,0,0,
0,0,7,7,7,7,7,7,9,9,
9,12,12,12,14,14,14,17,17,
19,19,19,22,22,22,22,22,22,22,
22,22	
};*/
const float sample_temp_table[]={
0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,4,4,4,4,4,
10,10,10,10,10,15,15,15,15,15,
18,18,18,18,18,18,18,18,18,18,
18,18	
};

//根据室温计算采样单元需要设定的温度 
void cal_sample_temp(float *evn_temp,float *sample_temp)
{
  int temp;
  temp=(int)(*evn_temp);
  if(temp<0)
   {
   	 *sample_temp=0;
   }
  else if((temp>=(sizeof(sample_temp_table)/sizeof(float))))
   {
     //*sample_temp=sample_temp_table[(sizeof(sample_temp_table)/sizeof(float))-1]-2;
     *sample_temp=sample_temp_table[(sizeof(sample_temp_table)/sizeof(float))-1];
   } 
  else
   {
   	 //if(sample_temp_table[temp]>2)
   	 //  *sample_temp=sample_temp_table[temp]-2;
    // else
       *sample_temp=sample_temp_table[temp];
   }  	
}//年,月,日,时,分,秒,微水 

void relay_dly_close_set(unsigned int rly_num,unsigned int dly_sec)
{
  if(rly_num>=32)
    return;
  relay_close_tick[rly_num]=dly_sec;
  relay_open_tick[rly_num]=0;
}

void relay_dly_open_set(unsigned int rly_num,unsigned int dly_sec)
{
  if(rly_num>=32)
    return;
  relay_open_tick[rly_num]=dly_sec;
  relay_close_tick[rly_num]=0;
}


void relay_dly_process(void)
{
  static unsigned int relay_tick=0;
  int i;
  relay_tick++;
  if((relay_tick&1)==0)//每秒钟运行一次 
    {
     for(i=0;i<(sizeof(relay_close_tick)/sizeof(int));i++)
      {
   	    if(relay_open_tick[i]>0)
   	      {
      	    relay_open_tick[i]--;
			if(relay_open_tick[i]==0)
			  {
		  	    if(i<32)
		  	      {
      	  		    relay_set(i,ON,0);
				    LOG_DEBUG(TRACE_DEBUG,"%s ON\n",rly_name[i]);	
      	  		  }                  
  			  }	
  	      }
			      	
   	    if(relay_close_tick[i]>0)
   	      {
      	    relay_close_tick[i]--;
			if(relay_close_tick[i]==0)
			  {
		  	    if(i<32)
		  	      {
      	  		    relay_set(i,OFF,0);
				    LOG_DEBUG(TRACE_DEBUG,"%s OFF\n",rly_name[i]);	
      	  		  }                  
  			  }	
  	      }
      }    	
    }
}

void ctrlCfun (int i)
{
  doIt = 0;
}

/*
void show_time(char *s)
{
 struct timeval  now;
 gettimeofday(&now,NULL);
 printf("sec=%d.%d\n",now.tv_sec,now.tv_usec/1000);
}*/

int SendActionMsg(ACTION_MSG_TYPE *msg)
{
  static mqd_t slave_main_mq=(mqd_t)-1;
  unsigned char dumy_msg[16];
  if(slave_main_mq==-1)
   {
	 if((slave_main_mq= open_mq(MAIN_MESSAGE_MQ))==((mqd_t)-1))
	    return -1;
   }
  if(mq_send(slave_main_mq,(const char *)msg,sizeof(ACTION_MSG_TYPE),MAIN_MESSAGE_PRIO)==-1)
	 return -2;
 
  return 0;
}

int SendActionReq(int cmd,int src)
{
 ACTION_MSG_TYPE msg;
 msg.cmd=cmd;
 msg.src=src;
 if(SendActionMsg(&msg)==0)
   return 0;
 return 1;
}

//手动启动采样 
void man_start_sample(void)
{
   
 //手动启动时,冷井和采样单元启动?传感器启动? 
  float my_temp=my_para_data.mach_run_para.temperature[TEMP_LJ]; 
  set_pid_temp(TEMP_LJ,&my_temp,0);//设置冷井温度 
  
  //printf("");
  //my_temp=my_para_data.mach_run_para.temperature[TEMP_SAM];
  //set_pid_temp(TEMP_SAM,&my_temp,0);//设置采样室温度 
  //set_pid_ch(PID_SAMPLE,PID_EN,0);//开启采样室温度控制 
  //relay_set(RLY_KA4,ON,0);//开采样单元风扇  

  //my_temp=my_para_data.mach_run_para.temperature[TEMP_LJ];
  //set_pid_temp(TEMP_LJ,&my_temp,0);//设置色谱柱温度 
  //set_pid_ch(PID_LJ,PID_EN,0);//开启色谱柱温度控制  
  //relay_set(RLY_KA6,ON,1);//开冷井风扇   	
}

int main_message_process(ACTION_MSG_TYPE *msg)
{
   float env_temp;//环境温度
   float my_temp;//
   int n;
  switch(curr_state)
   {
	 case SIX_SAMPLE:
	      if(msg->cmd==STOP_SAMPLE)
	        {
        	  memset(&gas_clean_stat,0,sizeof(gas_clean_stat));
        	  memset(&gas_make_stat,0,sizeof(gas_make_stat));
        	  set_pid_ch(PID_LJ,PID_DIS,0);//关冷井 
        	  set_pid_ch(PID_SAMPLE,PID_DIS,0);//关采样单元 
        	  //memset(&my_action_state,0,sizeof(&my_action_state));
		      relay_dly_close_set(RLY_SENSOR,4);//延时关传感器加热 		
        	  relay_clr((1<<RLY_SENSOR));
        	  curr_state=MODE_IDLE;	
        	}
	  	  break;
     case DEMARCATE:
          if(msg->cmd==STOP_SAMPLE)
	        {
        	  memset(&gas_make_stat,0,sizeof(gas_make_stat));
        	  set_pid_ch(PID_LJ,PID_DIS,0);//关冷井 
        	  set_pid_ch(PID_SAMPLE,PID_DIS,0);//关采样单元 	
        	  relay_clr((1<<RLY_SENSOR));
        	  curr_state=MODE_IDLE;	
        	}
          break;
     case REMOVE_GAS:
          if(msg->cmd==STOP_SAMPLE)
	        {
        	  memset(&gas_make_stat,0,sizeof(gas_make_stat));	
        	  //relay_clr((1<<RLY_SENSOR));
       	      relay_clr(0);
        	  curr_state=MODE_IDLE;	
        	}
          break;
     case GAS_FAULT_PROCESS:
          if(msg->cmd==STOP_SAMPLE)
	        {
        	  memset(&gas_make_stat,0,sizeof(gas_make_stat));	
        	  //relay_clr((1<<RLY_SENSOR));
       	      relay_clr(0);
        	  curr_state=MODE_IDLE;	
        	}
          break;
     case PUMP_OIL_PROCESS:
          if(msg->cmd==STOP_SAMPLE)
	        {
        	  memset(&gas_make_stat,0,sizeof(gas_make_stat));	
        	  ctrl_oil_pump(0);//停止
        	  curr_state=MODE_IDLE;	
        	}
          break;
	 default:
	      if(msg->cmd==START_SIX_SAMPLE)
	        { 
	          //采样开始前45分钟查表设置采样室温度,同时检测室外温度看是否启动伴热带
			  //启动采样室制冷 
              //get_temperature(TEMP_ENV,&env_temp);//获取环境温度 
              get_temperature(TEMP_OUTSIDE,&env_temp);//获取室外温度 
		      //if(env_temp>11)
               // { 									
                  cal_sample_temp(&env_temp,&my_temp);
                  LOG_DEBUG(TRACE_DEBUG,"Preaction:Cabinet temperature is %f,set sample room temperature to %f\n",env_temp,my_temp);
       	     
                  my_para_data.mach_run_para.temperature[TEMP_SAM]=my_temp;
                  set_pid_temp(TEMP_SAM,&my_temp,0);//设置采样室温度 
                  relay_set(RLY_KA4,ON,0);          //开采样室风扇 
                  relay_set(RLY_FAN,ON,0); 
                  set_pid_ch(PID_SAMPLE,PID_EN,1);  //开采样室温度控制 			   
                  LOG_DEBUG(TRACE_DEBUG,"Preaction:KA4 KA5 ON,Start sample room colding\n");   	            
               // }
             // else
                //{
                 // LOG_DEBUG(TRACE_DEBUG,"Preaction:sample room colding will not start,env_temp=%f\n",env_temp);	
               // }

			   /*2022-09-09添加I9处理*/
			   for(n=0;n<2;n++)
			   {
			  relay_set(RLY_YV4,ON,1);
			  LOG_DEBUG(ACTION_DEBUG,"YV4 ON\n");
              sleep(1);
              relay_set(RLY_YV11,ON,0);
			  relay_set(RLY_YV12,ON,1);
              LOG_DEBUG(ACTION_DEBUG,"YV11 ON YV12 ON\n");
			  sleep(10);
			  relay_set(RLY_YV4,OFF,1);
			  LOG_DEBUG(ACTION_DEBUG,"YV4 OFF\n");
              sleep(3);
			  relay_set(RLY_YV12,OFF,1);
			  LOG_DEBUG(ACTION_DEBUG,"YV12 OFF\n");
              sleep(3);
			  relay_set(RLY_YV5,ON,0);
			  relay_set(RLY_YV11,ON,0);
			  relay_set(RLY_YV13,ON,1);
			  LOG_DEBUG(ACTION_DEBUG,"YV5 YV11 YV13 ON\n");
              sleep(3);
			  relay_set(RLY_YV13,OFF,1);
			  LOG_DEBUG(ACTION_DEBUG,"YV13 OFF\n");
              sleep(1);
			  relay_set(RLY_YV5,OFF,0);
			  relay_set(RLY_YV11,OFF,1);
			  LOG_DEBUG(ACTION_DEBUG,"YV5 YV11 OFF\n");
			  sleep(1);
			   }

              gas_make_stat.dly_cnt=0;
		      gas_make_stat.repeat_cnt=0; 							 
              gas_make_stat.step=50;
		      curr_state=SIX_SAMPLE;	//开始标定  
        	}
      	  else if(msg->cmd==START_DEMARCATE)
      	    {
    	      curr_state=DEMARCATE;	//开始标定 
   	          memset(&gas_make_stat,0,sizeof(gas_make_stat));
			  gas_make_stat.step=1;	
  	        }
          else if(msg->cmd==START_RM_GAS)
            {
              curr_state=REMOVE_GAS;//开始脱气
              memset(&gas_make_stat,0,sizeof(gas_make_stat));
			  gas_make_stat.step=1;	
            }
          else if(msg->cmd==START_FAULT_PROCESS)
            {
              curr_state=GAS_FAULT_PROCESS;//开始脱气故障处理
		      memset(&gas_make_stat,0,sizeof(gas_make_stat));
			  gas_make_stat.step=1;		
            }
          else if(msg->cmd==START_OIL_PROCESS)
            {
              curr_state=PUMP_OIL_PROCESS;//
              ctrl_oil_pump(1);//启动	
            }         
	  	  break;
   }
 return 0;
}

void stop_gas_clean(int reason)
{
  SendActionReq(STOP_SAMPLE,reason);
  printf("clean gas failed\n");	
}

void stop_gas_make(int reason)
{
  SendActionReq(STOP_SAMPLE,reason);
  printf("make gas failed\n");		
}

void stop_curr_process(int reason)
{
  SendActionReq(STOP_SAMPLE,reason);
  printf("stop current process\n");		
}

int pull_yv6(int timeout)
{
 int i;
  relay_set(RLY_YV7,OFF,0);
  relay_set(RLY_YV6,ON,1); 
  for(i=0;i<timeout;i++)
  {
	usleep(500000);
	if(io_get(STA_I7)==ON)
	{
	  break;
	} 
  }
  if(io_get(STA_I7)==ON)
    return 0;
  return 1;
}

int push_yv7(int timeout)
{
  int i;
  relay_set(RLY_YV6,OFF,0);
  relay_set(RLY_YV14,ON,0);
  relay_set(RLY_YV7,ON,1); 
  for(i=0;i<timeout;i++)
  {
	usleep(500000);
	if(io_get(STA_I6)==ON)
	{
	  break;
	} 
  }
  relay_set(RLY_YV14,OFF,1);
  if(io_get(STA_I6)==ON)
    return 0;
  return 1;
}

int gas_clean(void)
{
 switch(gas_clean_stat.step)
  {
     case 1://检查I6 
          if(io_get(STA_I6)==ON)
            {	
		      gas_clean_stat.dly_cnt=0;	
			  gas_clean_stat.repeat_cnt=0;							 
			  gas_clean_stat.step++;
		      LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:I6 is in right position\n",gas_clean_stat.step); 							  
		    }
          else
		    {
			  //推动一下YV7,让I6到位
	    	  LOG_DEBUG(ERROR_DEBUG,"clean_step=%d:I6 is not in right pos\n",gas_clean_stat.step);
			  if(push_yv7(3*I6_WAIT_CNT/2))
			  {
			  	stop_gas_clean(gas_clean_stat.step);
			  	return gas_clean_stat.step;
			  }	
			  gas_clean_stat.dly_cnt=0;	
			  gas_clean_stat.repeat_cnt=0;							 
			  gas_clean_stat.step++;
		      LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:I6 is in right position after push yv7\n",gas_clean_stat.step);					  
		    }	      
	      break;
	 case 2://开清洗排空阀YV14,连通外界
          relay_set(RLY_YV14,ON,1);
          gas_clean_stat.dly_cnt=0;	
		  LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:YV14 ON\n",gas_clean_stat.step);
          gas_clean_stat.step++; 
	      break;
	 case 3://开YV6，拉气缸=从外界吸入空气 
	      if(gas_clean_stat.dly_cnt==0)
          {
            relay_set(RLY_YV7,OFF,0);
            relay_set(RLY_YV6,ON,1);
		    LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:YV6 ON YV7 OFF\n",gas_clean_stat.step);
            gas_clean_stat.dly_cnt++;
   	      }
          else
          {
            //等待I7到位
            if(gas_clean_stat.dly_cnt>I7_WAIT_CNT)
              {
      	        if(io_get(STA_I7)==ON)
			     {
  			       gas_clean_stat.dly_cnt=0;
		           LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:I7 is arrived in right position\n",gas_clean_stat.step);
                   gas_clean_stat.step++;
                   break;
  			     }
				else
				 {
	               if(gas_clean_stat.dly_cnt>I7_WAIT_MAX)
                    {
                   	  LOG_DEBUG(ERROR_DEBUG,"gas_step=%d:wait I7 ON timeout\n",gas_clean_stat.step);
                   	  stop_gas_clean(gas_clean_stat.step);
     				  gas_clean_stat.dly_cnt=0;							 
                      gas_clean_stat.step=0; 
					  return gas_clean_stat.step;                     
     				}	
 				 }	             	
              }
 	        gas_clean_stat.dly_cnt++;
          }
	      break;
	 case 4://开YV7，推气缸=将气缸里的气体推出到外界 
	      if(gas_clean_stat.dly_cnt==0)
          {
          	relay_set(RLY_YV6,OFF,0);
            relay_set(RLY_YV7,ON,1); 
		    LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:YV7 ON YV6 OFF\n",gas_clean_stat.step);
            gas_clean_stat.dly_cnt++;
   	      }
          else
          {
            //等待I6到位
            if(gas_clean_stat.dly_cnt>I6_WAIT_CNT)
              {
      	        if(io_get(STA_I6)==ON)
			     {
  			       gas_clean_stat.dly_cnt=0;
					LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:I6 is arrived in right position\n",gas_clean_stat.step);
                   gas_clean_stat.step++;
                   
                   break;
  			     }
				else
				 {
	               if(gas_clean_stat.dly_cnt>I6_WAIT_MAX)
                    {
                   	  LOG_DEBUG(ERROR_DEBUG,"gas_step=%d:wait I6 arrived in right position timeout\n",gas_clean_stat.step);
                   	  stop_gas_clean(gas_clean_stat.step);
     				  gas_clean_stat.dly_cnt=0;							 
                      gas_clean_stat.step=0; 
					  return gas_clean_stat.step;                       
     				}	
 				 }	             	
              }
 	        gas_clean_stat.dly_cnt++;
          }
	      break;
	 case 5://重复3-4 
	      gas_clean_stat.dly_cnt=0;	
	      gas_clean_stat.repeat_cnt++;
		  LOG_DEBUG(ACTION_DEBUG,"%dst gas clean completed,curr_step=%d\n",gas_clean_stat.repeat_cnt,gas_clean_stat.step);
	      if(gas_clean_stat.repeat_cnt>=CLEAN_AIR_REPEAT_CNT)
	        {
        	  gas_clean_stat.repeat_cnt=0;					 
              gas_clean_stat.step++;	
        	}
      	  else
      	    {
    	      gas_clean_stat.step=3; 	
  	        }
	      break;
	 case 6://关YV14 
	      relay_set(RLY_YV14,OFF,1);
        gas_clean_stat.dly_cnt=0;	
				LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:YV14 OFF\n",gas_clean_stat.step);	 
        gas_clean_stat.step++; 
	      break;
	 case 7://开YV15=气缸集气阀
	      relay_set(RLY_YV15,ON,1);
          gas_clean_stat.dly_cnt=0;	
          LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:YV15 ON\n",gas_clean_stat.step);		 
          gas_clean_stat.step++; 
	      break;
	 case 8://开YV6，从气杯抽气体
	      if(gas_clean_stat.dly_cnt==0)
          {
            relay_set(RLY_YV7,OFF,0);
            relay_set(RLY_YV6,ON,1);
		    LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:YV6 ON YV7 OFF\n",gas_clean_stat.step);	
            gas_clean_stat.dly_cnt++;
   	      }
          else
          {
            //等待I7到位
            if(gas_clean_stat.dly_cnt>I7_WAIT_CNT)
              {
      	        if(io_get(STA_I7)==ON)
			     {
  			       gas_clean_stat.dly_cnt=0;
		           LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:I7 have arrived in right position\n",gas_clean_stat.step);	
                   gas_clean_stat.step++;
                   break;
  			     }
				else
				 {
	               if(gas_clean_stat.dly_cnt>I7_WAIT_MAX)
                    {
            	      LOG_DEBUG(ERROR_DEBUG,"gas_step=%d:wait I7 arrvied in right position timeout\n",gas_clean_stat.step);
                   	  stop_gas_clean(gas_clean_stat.step);
     				  gas_clean_stat.dly_cnt=0;							 
                      gas_clean_stat.step=0;
					  return gas_clean_stat.step;                       
     				}	
 				 }	             	
              }
 	        gas_clean_stat.dly_cnt++;
          }
	      break;
	 case 9://关YV15 
	      relay_set(RLY_YV15,OFF,1);
          gas_clean_stat.dly_cnt=0;	
		  LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:YV15 OFF\n",gas_clean_stat.step);	
          gas_clean_stat.step++; 
	      break;
	 case 10://开YV10(连接到了YV20载气清洗阀),延时6秒 
	      if(gas_clean_stat.dly_cnt==0)
	       {
 	        relay_set(RLY_YV10,ON,1);
	        LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:YV10 ON,Start delay 6s\n",gas_clean_stat.step);	
            gas_clean_stat.dly_cnt++;							    		
       	   }
   	      else
   	       {
       	   	 //延时6秒 
       	   	 if(gas_clean_stat.dly_cnt>YV10_ON_TIME)
       	   	   {
   	   	       	 gas_clean_stat.dly_cnt=0;							 
                 gas_clean_stat.step++; 
								 LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:Delay 6s is completed\n",gas_clean_stat.step);
   	   	       }
   	         else
   	           {
           	   	 gas_clean_stat.dly_cnt++;
           	   }
       	   }
	      break;
     case 11://关YV10
	      relay_set(RLY_YV10,OFF,1);
          gas_clean_stat.dly_cnt=0;	
				LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:YV10 OFF\n",gas_clean_stat.step);
          gas_clean_stat.step++;  
	      break;
	 case 12://开YV14,开清洗排空阀，将管内气体用载气冲出到外界
	      relay_set(RLY_YV14,ON,1);
          gas_clean_stat.dly_cnt=0;	
		  LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:YV14 ON\n",gas_clean_stat.step);
          gas_clean_stat.step++;
	      break;
	 case 13://开YV7，同时将气缸载气推出外界
	      if(gas_clean_stat.dly_cnt==0)
          {
          	relay_set(RLY_YV6,OFF,0);
            relay_set(RLY_YV7,ON,1); 
		    LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:YV7 ON YV6 OFF\n",gas_clean_stat.step);
            gas_clean_stat.dly_cnt++;
   	      }
          else
          {
            //等待I6到位
            if(gas_clean_stat.dly_cnt>I6_WAIT_CNT)
              {
      	        if(io_get(STA_I6)==ON)
			     {
  			       gas_clean_stat.dly_cnt=0;
		           LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:I6 have arrived in right position\n",gas_clean_stat.step);
                   gas_clean_stat.step++;
                   break;
  			     }
				else
				 {
	               if(gas_clean_stat.dly_cnt>I6_WAIT_MAX)
                    {
            	      LOG_DEBUG(ERROR_DEBUG,"gas_step=%d:wait I6 arrived in right position timeout\n",gas_clean_stat.step);
                   	  stop_gas_clean(gas_clean_stat.step);
     				  gas_clean_stat.dly_cnt=0;							 
                      gas_clean_stat.step=0;
					  return gas_clean_stat.step;                       
     				}	
 				 }	             	
              }
 	        gas_clean_stat.dly_cnt++;
          }
	      break;
	 case 14://关YV14 
	      relay_set(RLY_YV14,OFF,1);
          gas_clean_stat.dly_cnt=0;	
		  LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:YV14 OFF\n",gas_clean_stat.step);
          gas_clean_stat.step++;
	      break;
	 case 15://重复7-14 
          gas_clean_stat.dly_cnt=0;	
	      gas_clean_stat.repeat_cnt++;
		 LOG_DEBUG(ACTION_DEBUG,"%dst gas clean completed,curr_step=%d\n",gas_clean_stat.repeat_cnt,gas_clean_stat.step);
	      if(gas_clean_stat.repeat_cnt>=CLEAN_CARRIER_REPEAT_CNT)
	        {
        	  gas_clean_stat.repeat_cnt=0;					 
              gas_clean_stat.step++;	
        	}
      	  else
      	    {
    	      gas_clean_stat.step=7; 	
  	        }
	      break;
	 case 16://开YV6 
	      if(gas_clean_stat.dly_cnt==0)
          {
            relay_set(RLY_YV7,OFF,0);
            relay_set(RLY_YV6,ON,1);
		    LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:YV6 ON YV7 OFF\n",gas_clean_stat.step);
            gas_clean_stat.dly_cnt++;
   	      }
          else
          {
            //等待I7到位
            if(gas_clean_stat.dly_cnt>I7_WAIT_CNT)
              {
      	        if(io_get(STA_I7)==ON)
			     {
  			       gas_clean_stat.dly_cnt=0;
		           LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:I7 have arrived in right position\n",gas_clean_stat.step);
                   gas_clean_stat.step++;
                   break;
  			     }
				else
				 {
	               if(gas_clean_stat.dly_cnt>I7_WAIT_MAX)
                    {
            	      LOG_DEBUG(ERROR_DEBUG,"gas_step=%d:wait I7 arrived in right position timeout\n",gas_clean_stat.step);
                   	  stop_gas_clean(gas_clean_stat.step);
     				  gas_clean_stat.dly_cnt=0;							 
                      gas_clean_stat.step=0; 
					  return gas_clean_stat.step;                     
     				}	
 				 }	             	
              }
 	        gas_clean_stat.dly_cnt++;
          }
	      break;
	 case 17://启动冷井工作 
	      relay_set(RLY_KA6,ON,0);//开冷井风扇 
          //relay_set(RLY_KA7,ON,0);//冷井电源 
	      set_pid_ch(PID_LJ,PID_EN,1);//开冷井控制    
          gas_clean_stat.dly_cnt=0;	
          gas_clean_stat.repeat_cnt=0;
          gas_clean_stat.step++;
          LOG_DEBUG(ACTION_DEBUG,"clean_step=%d:gas clean have completed,KA6 ON KA7 ON and start LJ cold\n",gas_clean_stat.step);
	      break;
     case 18:
          break;
	 default:
	      break;	 	
  }	
 return 0;
}

//启动停止油泵 
int ctrl_oil_pump(int str_or_stp)
{
 if(str_or_stp)
   {
    relay_set(RLY_YV16,ON,0);
    relay_set(RLY_YV13,ON,1);
    usleep(1000000);
    relay_set(RLY_OIL,ON,1);    	
   }
 else
   {  
    relay_set(RLY_OIL,OFF,1); 
    usleep(500000);
    relay_set(RLY_YV16,OFF,0);
    relay_set(RLY_YV13,OFF,1);    	
   }
 return 0;	
}

//脱气机故障处理 
int gas_flt_process(void)
{
  switch(gas_make_stat.step)
   {
    case 1://拉油缸 
        relay_set(RLY_YV4,ON,1);
        LOG_DEBUG(ACTION_DEBUG,"flt_step=%d:YV4 ON\n",gas_make_stat.step);
        gas_make_stat.dly_cnt=0;
        gas_make_stat.step++;  
        break;
    case 2://等油缸拉到位 
        if(io_get(STA_I5)==ON)
          {
	        gas_make_stat.dly_cnt=0;	
	        LOG_DEBUG(ACTION_DEBUG,"flt_step=%d:I5 have arrived in right position\n",gas_make_stat.step);  						 
            gas_make_stat.step++;
            relay_set(RLY_YV11,ON,0);//开YV11,YV12 
            relay_set(RLY_YV12,ON,1);
            LOG_DEBUG(ACTION_DEBUG,"flt_step=%d:YV11 ON,Y12 ON\n",gas_make_stat.step);  
            break;
          }
		else
	      {
            if(gas_make_stat.dly_cnt>I5_WAIT_MAX)
              {
           	    LOG_DEBUG(ERROR_DEBUG,"flt_step=%d:wait I5 arrived ON timeout,exit\n",gas_make_stat.step);
           	    stop_curr_process(gas_make_stat.step);
                gas_make_stat.dly_cnt=0;							 
                gas_make_stat.step=0; 
                return gas_make_stat.step;                     
              }	
            gas_make_stat.dly_cnt++;
	      }	             	
        break;
    case 3://延时3分钟 
        gas_make_stat.dly_cnt++;
        if(gas_make_stat.dly_cnt>(180*2))
          {
       	   relay_set(RLY_YV12,OFF,1);//关YV12 
       	   usleep(1000000);
           relay_set(RLY_YV4,OFF,1);//关YV4 
           LOG_DEBUG(ACTION_DEBUG,"flt_step=%d:Y12 OFF,YV4 OFF\n",gas_make_stat.step);
           gas_make_stat.dly_cnt=0;
           gas_make_stat.step++; 	
          }
        break;
    case 4:
        if(io_get(STA_I4)==ON)
          {
	        gas_make_stat.dly_cnt=0;	
	        LOG_DEBUG(ACTION_DEBUG,"flt_step=%d:I4 have arrived in right position\n",gas_make_stat.step);  						 
            gas_make_stat.step++;
            break;
          }
		else
	      {
            if(gas_make_stat.dly_cnt>I4_WAIT_MAX)
              {
           	    LOG_DEBUG(ERROR_DEBUG,"flt_step=%d:wait I4 arrived ON timeout,exit\n",gas_make_stat.step);
           	    stop_curr_process(gas_make_stat.step);
                gas_make_stat.dly_cnt=0;							 
                gas_make_stat.step=0; 
                return gas_make_stat.step;                     
              }	
            gas_make_stat.dly_cnt++;
	      }	            
        break;
    case 5:
        if(io_get(STA_I8)==ON)
          {
       	   relay_set(RLY_YV11,OFF,1);
       	   LOG_DEBUG(ACTION_DEBUG,"flt_step=%d:YV11 OFF\n",gas_make_stat.step);
           gas_make_stat.repeat_cnt=0;
           gas_make_stat.dly_cnt=0;							 
           gas_make_stat.step=0; 
		   //停止动作
	       stop_curr_process(gas_make_stat.step);
		   return gas_make_stat.step; 		
          }
        else
          {
            relay_set(RLY_YV5,ON,1);
            LOG_DEBUG(ACTION_DEBUG,"flt_step=%d:YV5 ON\n",gas_make_stat.step);
			gas_make_stat.dly_cnt=0;							 
            gas_make_stat.step++; 	
          }
        break;
    case 6:
        gas_make_stat.dly_cnt++;
        if(gas_make_stat.dly_cnt>8)
          {
          	relay_set(RLY_YV11,OFF,1);
            LOG_DEBUG(ACTION_DEBUG,"flt_step=%d:YV11 OFF,YV5 OFF\n",gas_make_stat.step);
          	usleep(1000000);
            relay_set(RLY_YV5,OFF,1);         
			if(io_get(STA_I8)==ON)
			  {
  			    gas_make_stat.repeat_cnt=0;
                gas_make_stat.dly_cnt=0;							 
                gas_make_stat.step=0; 
                stop_curr_process(gas_make_stat.step);
                return gas_make_stat.step; 
  		 	  }
			else
			  {
			  	gas_make_stat.repeat_cnt++;
  			    if(gas_make_stat.repeat_cnt<2)
  			      {     			    
					gas_make_stat.dly_cnt=0;							 
                    gas_make_stat.step=1; 	
      			  }
			    else
			      {
      			    //结束
			       LOG_DEBUG(ERROR_DEBUG,"flt_step=%d:I8 is not in right position,exit\n",gas_make_stat.step);
                   gas_make_stat.dly_cnt=0;							 
                   gas_make_stat.step=0; 
                   gas_make_stat.repeat_cnt=0; 
                   stop_curr_process(gas_make_stat.step);
                   return gas_make_stat.step;  	
      			  }
  			  }	
          }
        break;
    default:
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step=0; 
        gas_make_stat.repeat_cnt=0;
		stop_curr_process(gas_make_stat.step); 
        break;
    }
  return 0;
}

int remove_gas_process(void)
{
 switch(gas_make_stat.step)
  {
  	case 1://打开电机 
         if(io_get(STA_I4)!=ON)
		  {
	        LOG_DEBUG(ERROR_DEBUG,"make_step=%d:I4 is not in right position,exit\n",gas_make_stat.step);
			//stop_gas_make(gas_make_stat.step);
			stop_curr_process(gas_make_stat.step);		
			return gas_make_stat.step; 
		  }
	     //y4 on,等到位,y11 onf,1分钟,y4 off,等油缸到位,再检测i8,y5 on 3s,再检测I8,关y11,关yv5 
	     if(io_get(STA_I8)==OFF)
		  {
		  	usleep(500000);
		  	if(io_get(STA_I8)==OFF)
		  	{	  	
		     LOG_DEBUG(ERROR_DEBUG,"make_step=%d:I8 is not in right position,exit\n",gas_make_stat.step);
			 //stop_gas_make(gas_make_stat.step);
			 stop_curr_process(gas_make_stat.step);			
			 return gas_make_stat.step;
			}
		  }
	     relay_set(RLY_YV16,ON,0);
	     relay_set(RLY_YV13,ON,0);
	     relay_set(RLY_KA1,ON,1); 
         LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV16 ON,YV13 ON,KA1 ON\n",gas_make_stat.step);
	     gas_make_stat.dly_cnt=0;
	     gas_make_stat.repeat_cnt=0;
	     gas_make_stat.step++;
  	    break;
    case 2://延时3分钟
	    gas_make_stat.dly_cnt++;
		if(gas_make_stat.dly_cnt>=MOTOR_RUN_TIME)
		  {
  		    gas_make_stat.dly_cnt=0;
	        gas_make_stat.repeat_cnt=0;
	        LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 3 minutes completed\n",gas_make_stat.step);
	        gas_make_stat.step++;	
  		  } 
    	break;
   	case 3://关电机 
         relay_set(RLY_KA1,OFF,0);			 
         relay_set(RLY_YV16,OFF,0);
         relay_set(RLY_YV13,OFF,1);
         LOG_DEBUG(ACTION_DEBUG,"make_step=%d:KA1 OFF,YV16 OFF,YV13 OFF\n",gas_make_stat.step);
		gas_make_stat.dly_cnt=0;
        gas_make_stat.repeat_cnt=0;
        gas_make_stat.step++;	  	    
   		break;
	case 4:
	    if(io_get(STA_I8)==OFF)
         {
       		usleep(500000);
       		if(io_get(STA_I8)==OFF)
       		{		      
              LOG_DEBUG(ERROR_DEBUG,"make_step=%d:I8 is not in right position,exit\n",gas_make_stat.step);
              //stop_gas_make(gas_make_stat.step);
              stop_curr_process(gas_make_stat.step);
		      return gas_make_stat.step;
		    }	
       	 }
   	    gas_make_stat.dly_cnt=0;
        gas_make_stat.repeat_cnt=0;
        gas_make_stat.step++;
		break;
	case 5://延时2秒,等待气路清洗完成,
	    gas_make_stat.dly_cnt++;
		if(gas_make_stat.dly_cnt>=4) 
          {
		    if(gas_clean_stat.step>GAS_CLEAN_MAX_STEP)
		      {
		      	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 2 minutes completed\n",gas_make_stat.step);
     			gas_make_stat.dly_cnt=0;
                gas_make_stat.repeat_cnt=0;
                gas_make_stat.step++;
      		  }
  		    else
  		      {
      		  	if(gas_make_stat.dly_cnt>GAS_CLEAN_MAX_TIME)
      		  	  {
  	  		      	LOG_DEBUG(ERROR_DEBUG,"make_step=%d:gas clean timeout,exit\n",gas_make_stat.step);
  	  		        //stop_gas_make(gas_make_stat.step);
  	  		        stop_curr_process(gas_make_stat.step);
  	  		        return gas_make_stat.step;
				  }
      		  }
  		  }
	    break;
	case 6://开YV4 
	      if(gas_make_stat.dly_cnt==0)
          {
          	relay_set(RLY_YV5,OFF,0);
            relay_set(RLY_YV4,ON,1);  
			LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV4 ON,YV5 OFF\n",gas_make_stat.step);        
            gas_make_stat.dly_cnt++;
   	      }
          else
          {
            //等待I5到位
            if(gas_make_stat.dly_cnt>I5_WAIT_CNT)
              {
      	        if(io_get(STA_I5)==ON)
			     {
  			       gas_make_stat.dly_cnt=0;	
				   LOG_DEBUG(ACTION_DEBUG,"make_step=%d:I5 have arrived in right position\n",gas_make_stat.step);  						 
                   gas_make_stat.step++;
                   break;
  			     }
				else
				 {
	               if(gas_make_stat.dly_cnt>I5_WAIT_MAX)
                    {
                   	  LOG_DEBUG(ERROR_DEBUG,"make_step=%d:wait I5 arrived ON timeout,exit\n",gas_make_stat.step);
                   	  //stop_gas_make(gas_make_stat.step);
                   	  stop_curr_process(gas_make_stat.step);
     				  gas_make_stat.dly_cnt=0;							 
                      gas_make_stat.step=0; 
					  return gas_make_stat.step;                     
     				}	
 				 }	             	
              }
 	        gas_make_stat.dly_cnt++;
          }
		break;
	case 7://开YV11 
	     relay_set(RLY_YV11,ON,1);
		 LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV11 ON\n",gas_make_stat.step); 
         gas_make_stat.dly_cnt=0;							 
         gas_make_stat.step++;
		break;
	case 8://开YV12 
	    relay_set(RLY_YV12,ON,1);
	    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV12 ON\n",gas_make_stat.step);  
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;
		break;
	case 9://延时2分钟,制气时间
	    gas_make_stat.dly_cnt++; 
	    if(gas_make_stat.dly_cnt>GAS_MAKE_TIME)
	      {
      		gas_make_stat.dly_cnt=0;
		  	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 2 minutes completed\n",gas_make_stat.step); 							 
            gas_make_stat.step++;
      	  }
		break;
	case 10://关YV4,对应STM32上清洗油路第28步 
	    relay_set(RLY_YV4,OFF,0);
	    relay_set(RLY_YV15,ON,1);
    	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV4 OFF,YV15 ON\n",gas_make_stat.step); 
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;
		break;
	case 11://开YV15      
	    gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;		
		break;
 	case 12://延时5秒,进气时间,admission
        gas_make_stat.dly_cnt++; 
	    if(gas_make_stat.dly_cnt>GAS_ADMISSION_TIME)
	      {
      		gas_make_stat.dly_cnt=0;
		    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 5s completed\n",gas_make_stat.step);  							 
            gas_make_stat.step++;
      	  }	     
  	    break;
   	case 13://关YV12
        relay_set(RLY_YV12,OFF,1);
       	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV12 OFF\n",gas_make_stat.step); 
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++; 
   		break;
    case 14://关YV15 
        gas_make_stat.dly_cnt++;
        if(gas_make_stat.dly_cnt==1)
          {
            relay_set(RLY_YV15,OFF,1);
       	    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV15 OFF\n",gas_make_stat.step);	
          }
        else
          {
            if(gas_make_stat.dly_cnt>=32)
			  {
  			    gas_make_stat.dly_cnt=0;							 
                gas_make_stat.step++;	
  			  }	
          }
    	break;
	case 15://重复6-14,
        gas_make_stat.dly_cnt=0;	
        gas_make_stat.repeat_cnt++;
        LOG_DEBUG(ACTION_DEBUG,"make_step=%d:%dst make gas completed\n",gas_make_stat.step,gas_make_stat.repeat_cnt);
        if(gas_make_stat.repeat_cnt>=GAS_MAKE_REPEAT_CNT)//制气重复次数 
          {
       	    gas_make_stat.repeat_cnt=0;					 
            gas_make_stat.step++;	
       	  }
		else
   	      {
   	        gas_make_stat.step=6; 				
          } 
		break;
	case 16://开YV5,延时1秒 
	    if(gas_make_stat.dly_cnt==0)
	      {
      	    relay_set(RLY_YV5,ON,1);
 	    	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV5 ON\n",gas_make_stat.step); 
			gas_make_stat.dly_cnt++;	
      	  }
  	    else
  	      {
      	    if(gas_make_stat.dly_cnt>=3)
			  {
			    gas_make_stat.dly_cnt=0;
				LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 1s completed\n",gas_make_stat.step);							 
                gas_make_stat.step++; 
  			  }
			else
			  {
  			    gas_make_stat.dly_cnt++;	
  			  }	
      	  }
	    break;
    case 17://关YV11
        relay_set(RLY_YV11,OFF,1);
       	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV11 OFF\n",gas_make_stat.step);  
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;
		break; 
    case 18://关YV5 
        relay_set(RLY_YV5,OFF,1);
       	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV5 OFF\n",gas_make_stat.step);
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;
        break;
	case 19://开YV7,关YV6
	    relay_set(RLY_YV6,OFF,0);
        relay_set(RLY_YV7,ON,1);
       	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV7 ON,YV6 OFF\n",gas_make_stat.step);
		gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;      
		break;
	case 20:
	    gas_make_stat.dly_cnt++;
	    if(gas_make_stat.dly_cnt>=32)
	      {
      	    relay_set(RLY_YV7,OFF,1);
       	    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV7 OFF\n",gas_make_stat.step);
			gas_make_stat.dly_cnt=0;							 
            gas_make_stat.step=0; 
			stop_gas_make(gas_make_stat.step);	
      	  }
	    break;
	default:
	    break;
   }
 return 0;
}

int gas_make(void)
{
 float fval;
 static unsigned int yv17_on_flg;
 static unsigned int yv17_on_tick;
 unsigned int sample_data_length=0;
 static unsigned int pres_flg=0;
 
 static time_t start_time,now;
 static unsigned int co2_sample_state=0;
 
 if(gas_make_stat.step<44)
 { 
  if(io_get(STA_I9)==ON)
   { 
    usleep(500000);
    if(io_get(STA_I9)==ON)
      {
       LOG_DEBUG(ERROR_DEBUG,"gas_make_step=%d gas_clean_step=%d:I9 is not in right position,exit\n",gas_make_stat.step,gas_clean_stat.step);
       gas_clean_stat.step=0;
       stop_gas_make(gas_make_stat.step);
       return gas_make_stat.step;      	
      }
   }

  if(gas_clean()!=0)
   {
   	 LOG_DEBUG(ERROR_DEBUG,"gas clean failed\n");
   	 return gas_make_stat.step; 
   }
  }
 if(time_record.heat_start_time)
   {
   	 if(time(NULL)>=time_record.heat_start_time)
   	  {
  	    //开传感器
		time_record.heat_start_time=0;	
      }
   }
 switch(gas_make_stat.step)
  {
  	case 1://打开电机 
         if(io_get(STA_I4)!=ON)
		  {
	        LOG_DEBUG(ERROR_DEBUG,"make_step=%d:I4 is not in right position,exit\n",gas_make_stat.step);
			stop_gas_make(gas_make_stat.step);		
			return gas_make_stat.step; 
		  }
	     //y4 on,等到位,y11 onf,1分钟,y4 off,等油缸到位,再检测i8,y5 on 3s,再检测I8,关y11,关yv5 
	     if(io_get(STA_I8)==OFF)
		  {
		  	usleep(500000);
		  	if(io_get(STA_I8)==OFF)
		  	{	  	
		     LOG_DEBUG(ERROR_DEBUG,"make_step=%d:I8 is not in right position,exit\n",gas_make_stat.step);
			 stop_gas_make(gas_make_stat.step);		
			 return gas_make_stat.step;
			}
		  }
	     relay_set(RLY_YV16,ON,0);
	     relay_set(RLY_YV13,ON,0);
	     relay_set(RLY_KA1,ON,1); 
         LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV16 ON,YV13 ON,KA1 ON\n",gas_make_stat.step);
	     gas_make_stat.dly_cnt=0;
	     gas_make_stat.repeat_cnt=0;
	     gas_make_stat.step++;
  	    break;
    case 2://延时3分钟
	    gas_make_stat.dly_cnt++;
		if(gas_make_stat.dly_cnt>=MOTOR_RUN_TIME)
		  {
  		    gas_make_stat.dly_cnt=0;
	        gas_make_stat.repeat_cnt=0;
	        LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 3 minutes completed\n",gas_make_stat.step);
	        gas_make_stat.step++;	
  		  } 
    	break;
   	case 3://关电机 
        relay_set(RLY_KA1,OFF,0);			 
        relay_set(RLY_YV16,OFF,0);
        relay_set(RLY_YV13,OFF,1);
        LOG_DEBUG(ACTION_DEBUG,"make_step=%d:KA1 OFF,YV16 OFF,YV13 OFF\n",gas_make_stat.step);
		gas_make_stat.dly_cnt=0;
        gas_make_stat.repeat_cnt=0;
        gas_make_stat.step++;	  	    
   		break;
	case 4:
	    if(io_get(STA_I8)==OFF)
         {
       		usleep(500000);
       		if(io_get(STA_I8)==OFF)
       		{		      
              LOG_DEBUG(ERROR_DEBUG,"make_step=%d:I8 is not in right position,exit\n",gas_make_stat.step);
              stop_gas_make(gas_make_stat.step);
		      return gas_make_stat.step;
		    }	
       	 }
   	    gas_make_stat.dly_cnt=0;
        gas_make_stat.repeat_cnt=0;
        gas_make_stat.step++;
		break;
	case 5://延时2秒,等待气路清洗完成,
	    gas_make_stat.dly_cnt++;
		if(gas_make_stat.dly_cnt>=4) 
          {
		    if(gas_clean_stat.step>GAS_CLEAN_MAX_STEP)
		      {
		      	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 2 minutes completed\n",gas_make_stat.step);
     			gas_make_stat.dly_cnt=0;
                gas_make_stat.repeat_cnt=0;
                gas_make_stat.step++;
      		  }
  		    else
  		      {
      		  	if(gas_make_stat.dly_cnt>GAS_CLEAN_MAX_TIME)
      		  	  {
  	  		      	LOG_DEBUG(ERROR_DEBUG,"make_step=%d:gas clean timeout,exit\n",gas_make_stat.step);
  	  		        stop_gas_make(gas_make_stat.step);
  	  		        return gas_make_stat.step;
				  }
      		  }
  		  }
	    break;
	case 6://开YV4 
	      if(gas_make_stat.dly_cnt==0)
          {
          	relay_set(RLY_YV5,OFF,0);
            relay_set(RLY_YV4,ON,1);  
			LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV4 ON,YV5 OFF\n",gas_make_stat.step);        
            gas_make_stat.dly_cnt++;
   	      }
          else
          {
            //等待I5到位
            if(gas_make_stat.dly_cnt>I5_WAIT_CNT)
              {
      	        if(io_get(STA_I5)==ON)
			     {
  			       gas_make_stat.dly_cnt=0;	
				   LOG_DEBUG(ACTION_DEBUG,"make_step=%d:I5 have arrived in right position\n",gas_make_stat.step);  						 
                   gas_make_stat.step++;
                   break;
  			     }
				else
				 {
	               if(gas_make_stat.dly_cnt>I5_WAIT_MAX)
                    {
                   	  LOG_DEBUG(ERROR_DEBUG,"make_step=%d:wait I5 arrived ON timeout,exit\n",gas_make_stat.step);
                   	  stop_gas_make(gas_make_stat.step);
     				  gas_make_stat.dly_cnt=0;							 
                      gas_make_stat.step=0; 
					  return gas_make_stat.step;                     
     				}	
 				 }	             	
              }
 	        gas_make_stat.dly_cnt++;
          }
		break;
	case 7://开YV11 
	     relay_set(RLY_YV11,ON,1);
		 LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV11 ON\n",gas_make_stat.step); 
         gas_make_stat.dly_cnt=0;							 
         gas_make_stat.step++;
		break;
	case 8://开YV12 
	    relay_set(RLY_YV12,ON,1);
	    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV12 ON\n",gas_make_stat.step);  
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;
		break;
	case 9://延时2分钟,制气时间
	    gas_make_stat.dly_cnt++; 
	    if(gas_make_stat.dly_cnt>GAS_MAKE_TIME)
	      {
      		gas_make_stat.dly_cnt=0;
		  	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 2 minutes completed\n",gas_make_stat.step); 							 
            gas_make_stat.step++;
      	  }
		break;
	case 10://关YV4,对应STM32上清洗油路第28步 
	    relay_set(RLY_YV4,OFF,0);
	    relay_set(RLY_YV15,ON,1);
    	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV4 OFF,YV15 ON\n",gas_make_stat.step); 
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;
		break;
	case 11://开YV15 
       // relay_set(RLY_YV15,ON,1);
       	//LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV15 ON\n",gas_make_stat.step); 
       // gas_make_stat.dly_cnt++; 
        //if(gas_make_stat.dly_cnt>20)
         // {
         	gas_make_stat.dly_cnt=0;							 
            gas_make_stat.step++;	
         // }	
		break;
 	case 12://延时5秒,进气时间,admission
        gas_make_stat.dly_cnt++; 
	    if(gas_make_stat.dly_cnt>GAS_ADMISSION_TIME)
	      {
      		gas_make_stat.dly_cnt=0;
		    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 5s completed\n",gas_make_stat.step);  							 
            gas_make_stat.step++;
      	  }	     
  	    break;
   	case 13://关YV12
        relay_set(RLY_YV12,OFF,1);
       	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV12 OFF\n",gas_make_stat.step); 
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++; 
   		break;
    case 14://关YV15 
        /*relay_set(RLY_YV15,OFF,1);
       	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV15 OFF\n",gas_make_stat.step);  
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;*/
        gas_make_stat.dly_cnt++;
        if(gas_make_stat.dly_cnt==1)
          {
            relay_set(RLY_YV15,OFF,1);
       	    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV15 OFF\n",gas_make_stat.step);	
          }
        else
          {
            if(gas_make_stat.dly_cnt>=32)
			  {
  			    gas_make_stat.dly_cnt=0;							 
                gas_make_stat.step++;	
  			  }	
          }
    	break;
	case 15://重复6-14,
        gas_make_stat.dly_cnt=0;	
        gas_make_stat.repeat_cnt++;
        LOG_DEBUG(ACTION_DEBUG,"make_step=%d:%dst make gas completed\n",gas_make_stat.step,gas_make_stat.repeat_cnt);
        if(gas_make_stat.repeat_cnt>=GAS_MAKE_REPEAT_CNT)//制气重复次数 
          {
       	    gas_make_stat.repeat_cnt=0;					 
            gas_make_stat.step++;	
       	  }
		else
   	      {
   	        gas_make_stat.step=6; 				
          } 
		break;
	case 16://开YV5,延时1秒 
	    if(gas_make_stat.dly_cnt==0)
	      {
      	    relay_set(RLY_YV5,ON,1);
 	    	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV5 ON\n",gas_make_stat.step); 
			gas_make_stat.dly_cnt++;	
      	  }
  	    else
  	      {
      	    if(gas_make_stat.dly_cnt>=3)
			  {
			    gas_make_stat.dly_cnt=0;
				LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 1s completed\n",gas_make_stat.step);							 
                gas_make_stat.step++; 
  			  }
			else
			  {
  			    gas_make_stat.dly_cnt++;	
  			  }	
      	  }
	    break;
    case 17://关YV11
        relay_set(RLY_YV11,OFF,1);
       	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV11 OFF\n",gas_make_stat.step);  
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;
		break; 
    case 18://关YV5 
        relay_set(RLY_YV5,OFF,1);
       	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV5 OFF\n",gas_make_stat.step);
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;
        break;
	case 19://等待冷井温度<-20
	    get_temperature(TEMP_LJ,&fval);//获取冷井温度
	    //if(fval>(-25))
            //if(fval>(-20))//2016-07-03
			//2022-08-15 取消温度判断，改为延时约4秒
		/*if(fval>(-10))//2020-05-18
	      {
	      	gas_make_stat.dly_cnt++;
	      	if(gas_make_stat.dly_cnt>600)//5分钟未到达设置的温度 
	      	  { //等待冷井到达设置温度超时
    	        LOG_DEBUG(ERROR_DEBUG,"make_step=%d:wait lj temperature arrived -10 timeout %f,exit\n",gas_make_stat.step,fval);		
		        stop_gas_make(gas_make_stat.step);//停止造气
		        return gas_make_stat.step;	//返回失败
  	      	  }
     	    if((gas_make_stat.dly_cnt%16)==0)
     	      LOG_DEBUG(ACTION_DEBUG,"make_step=%d:leng jin temp is %f\n",gas_make_stat.step,fval);
      	    break;	
      	  }*/
		gas_make_stat.dly_cnt++;
		if(gas_make_stat.dly_cnt>=8)
		{
  	    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:leng jin temp is %f\n",gas_make_stat.step,fval);
	    gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;
		}
		break;
	case 20://开YV7,关YV6
	    relay_set(RLY_YV6,OFF,0);
        relay_set(RLY_YV7,ON,1);
       	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV7 ON,YV6 OFF\n",gas_make_stat.step);
		gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;
		yv17_on_flg=0;  
		yv17_on_tick=0;         
		break;
	case 21://等待3分钟 
	    gas_make_stat.dly_cnt++;
	    if(gas_make_stat.dly_cnt>120)//到达1分钟? 
	      {
	      	if(gas_make_stat.dly_cnt==121)
	      	   LOG_DEBUG(ACTION_DEBUG,"make_step=%d:1 minutes arrived\n",gas_make_stat.step);
	      	//查PA1,大于常压就开YV17,等于常压就关闭YV17 
	      	//VoltCnvToPresEx(PRES_PA1,&fval);
	      	get_presure(PRES_GAS_PUMP,&fval);
	      	switch(yv17_on_flg)
	      	  {
  	      	   case 0:
  	      	       if(fval>DELT_PRES)
	      	        {
  	      	         relay_set(RLY_YV17,ON,1);
  	      	         yv17_on_flg=1;
  	      	         LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV17 ON,pres=%fMpa\n",gas_make_stat.step,fval);
	      	        }
				   break;
		       case 1:
		           yv17_on_flg=2;
		           break;
               case 2:
		           yv17_on_flg=3;
		           break;
			   case 3:
                   relay_set(RLY_YV17,OFF,1);
 	               yv17_on_flg=4;
 	               yv17_on_tick=gas_make_stat.dly_cnt;
 	               LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV17 OFF,pres=%fMpa\n",gas_make_stat.step,fval);
			       break;
               case 4:
                   if(gas_make_stat.dly_cnt>=(yv17_on_tick+40))
                      yv17_on_flg=5;
                   break;
               case 5:
                   if(fval>DELT_PRES)
	      	        {
  	      	         relay_set(RLY_YV17,ON,1);
  	      	         yv17_on_flg=6;
  	      	         LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV17 ON,pres=%fMpa\n",gas_make_stat.step,fval);
	      	        }
                   break;
               case 6:
                   yv17_on_flg=7;
                   break;
               case 7:
                   yv17_on_flg=8;
                   break;
               case 8:
                    relay_set(RLY_YV17,OFF,1);
 	                yv17_on_flg=9;
 	                yv17_on_tick=gas_make_stat.dly_cnt;
 	                LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV17 OFF,pres=%fMpa\n",gas_make_stat.step,fval);
                   break;
			   default:
			       break;	
  	      	  }
	      /*	if(yv17_on_flg<2)
     	     {
              if(fval>DELT_PRES)
	      	    {
  	      	      relay_set(RLY_YV17,ON,1);
  	      	      if(pres_flg==0)
  	      	       {  	     	       
     	      	     LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV17 ON,pres=%fMpa\n",gas_make_stat.step,fval);	
	      	       }
				  pres_flg=1;
				  yv17_on_flg++;				   	
  	      	    }  	      		
     	     }*/
	      	/*
	      	if(yv17_on_flg==0)
	      	 { 	      
	      	  if(fval>DELT_PRES)
	      	    {
  	      	      relay_set(RLY_YV17,ON,0);
  	      	      if(pres_flg==0)
  	      	       {  	     	       
     	      	     LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV17 ON,pres=%fMpa\n",gas_make_stat.step,fval);	
	      	       }
				  pres_flg=1;				   	
  	      	    }
     	      else if(fabs(fval-DELT_PRES)<0.001)
     	        {
      	          relay_set(RLY_YV17,OFF,0);
      	          yv17_on_flg=1;
      	          if(pres_flg==1)
      	           {
         	         LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV17 OFF,pres=%fMpa\n",gas_make_stat.step,fval);	
     	           }					
      	          pres_flg=0;
	            }
	  	      }*/
   	        //if((gas_make_stat.dly_cnt%8)==0)
     	    //   LOG_DEBUG(ACTION_DEBUG,"make_step=%d:Pres=%fMpa\n",gas_make_stat.step,fval);
      	    if(gas_make_stat.dly_cnt>360)//3分钟到? 
			  {
			  	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 3 minutes completed\n",gas_make_stat.step);
  				gas_make_stat.dly_cnt=0;							 
                gas_make_stat.step++; 
                relay_set(RLY_YV17,OFF,0);             
  			  }
      	  }
		break;
	case 22://等待冷井温度<-28 
	    get_temperature(TEMP_LJ,&fval);
		//2022-08-15注释掉冷井温度判断,只延时大约4秒
	    //if(fval>(-28))
       /* if(fval>(-25))//2016-07-03
	      {
	      	gas_make_stat.dly_cnt++;
	      	if(gas_make_stat.dly_cnt>600)//5分钟未到达设置的温度 
	      	  {
	            LOG_DEBUG(ERROR_DEBUG,"make_step=%d:wait lj temperature arrived -28 timeout %f,exit\n",gas_make_stat.step,fval);
				
			    stop_gas_make(gas_make_stat.step);
		        return gas_make_stat.step;	
  	      	  }
     	    if((gas_make_stat.dly_cnt%16)==0)
               LOG_DEBUG(ACTION_DEBUG,"make_step=%d:leng jin wendu is %f\n",gas_make_stat.step,fval);
      	    break;	
      	  }*/
		gas_make_stat.dly_cnt++;
		if(gas_make_stat.dly_cnt>=8)
		{
  	    	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:leng jin wen du is %f\n",gas_make_stat.step,fval);
	    	gas_make_stat.dly_cnt=0;							 
        	gas_make_stat.step++;
		}
	    break;
	case 23://开YV19
	    //relay_set(RLY_YV20,ON,0);//和YV19一起开,和YV18一起关 
	    relay_set(RLY_YV19,ON,1);
		LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV20 ON,YV19 ON\n",gas_make_stat.step); 
		gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;  
		break;
	case 24://延时18秒 
	    gas_make_stat.dly_cnt++;
	    if(gas_make_stat.dly_cnt>36)
	      {
 	    	gas_make_stat.dly_cnt=0;
			LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 18s completed\n",gas_make_stat.step);  							 
            gas_make_stat.step++;	
      	  }
        break;
    case 25://关YV19 
        //relay_set(RLY_YV19,OFF,1);
		//LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV19 OFF\n",gas_make_stat.step); 
		relay_set(RLY_YV7,OFF,0);//张帅提出要添加的 
		relay_set(RLY_YV19,OFF,1);
		LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV19 OFF,YV7 OFF\n",gas_make_stat.step);
		gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++; 
        break;
    case 26://关冷井风扇和制冷,关采样单元风扇以及制冷输出，电柜顶上的风扇也关闭
        set_pid_ch(PID_LJ,PID_DIS,0);
	    relay_set(RLY_KA6,OFF,0);//冷井风扇 
	    //relay_set(RLY_KA7,OFF,0);//冷井电源 
	    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:KA6 OFF,KA7 OFF,LengJin Ctrl disable\n",gas_make_stat.step); 
	    
	    if(my_para_data.mach_run_para.sample_cool_en==0)
	     {
 	      set_pid_ch(PID_SAMPLE,PID_DIS,0);
 	      relay_set(RLY_FAN,OFF,0); 
          relay_set(RLY_KA4,OFF,0);//采样单元风扇 
		  LOG_DEBUG(ACTION_DEBUG,"make_step=%d:Sample room ctrl disable,KA3 OFF\n",gas_make_stat.step); 	
     	 }        
        relay_set(RLY_KA3,OFF,1);//柜子风扇   
		LOG_DEBUG(ACTION_DEBUG,"make_step=%d:柜子风扇KA3 OFF\n",gas_make_stat.step);     
        //关伴热带,目前未定义tmb
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;
	    break;
    case 27://延时3秒 
        gas_make_stat.dly_cnt++;
	    if(gas_make_stat.dly_cnt>6)
          {
 	    	gas_make_stat.dly_cnt=0;
		    gas_make_stat.repeat_cnt=0;	
			LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 3s completed\n",gas_make_stat.step);						 
            gas_make_stat.step++;			
      	  }
        break;
    case 28://检查色谱柱温度    
        if(get_spz_stable(&fval)==0)
          {
          	//printf("spz wendu delt=%f is stable\n",fval);
          	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:spz wendu delt=%f is stable\n",gas_make_stat.step,fval);
          	get_temperature(TEMP_SPZ,&fval);
          	ana_result.spzTemp1=fval;	
      		LOG_DEBUG(ACTION_DEBUG,"make_step=%d:spz wendu is %f\n",gas_make_stat.step,fval);
            gas_make_stat.dly_cnt=0;
		    gas_make_stat.repeat_cnt=0;							 
            gas_make_stat.step++; 	
          }
        else
		  {		  	
 		    gas_make_stat.repeat_cnt=0;
  		    gas_make_stat.dly_cnt++;
  		    if((gas_make_stat.dly_cnt%8)==0)
  		      {
  		  	    get_temperature(TEMP_SPZ,&fval);
  		  	    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:spz wendu is %f\n",gas_make_stat.step,fval);
      		  } 		       
			if(gas_make_stat.dly_cnt>600)//5分钟内未达到,退出采样 
			  {
	  	        get_temperature(TEMP_SPZ,&fval);
		        LOG_DEBUG(ERROR_DEBUG,"make_step=%d:chromatographic temperature is not stable for 5 minutes %f,exit\n",gas_make_stat.step,fval);
  				stop_gas_make(gas_make_stat.step);
		        return gas_make_stat.step;
  			  }	        	
          }
        break;
    case 29://启动采样 
        get_temperature(TEMP_SAM,&fval);
        ana_result.samTemp1=fval;
        ana_result.result.temp=my_input_data.temp;//油温 
        ana_result.result.Mst=my_input_data.water;//微水 
        LOG_DEBUG(ACTION_DEBUG,"make_step=%d:sample room temp is %f,sample_len=%d sample_intval=%dms\n",gas_make_stat.step,fval,my_para_data.mach_run_para.sample_len,my_para_data.mach_run_para.sample_rate);
        if(query_sample_data(NULL,sizeof(SAMPLE_INPUT_TYPE))==0)
		  {
  		    LOG_DEBUG(ACTION_DEBUG,"sample_len=%d sen_in=%f amp_in=%f\r\n",sample_data_length,3.3*my_sample_data.sen_in/4096,3.3*my_sample_data.amp_in/4096);	
  		  }	
	    else if(query_sample_data(NULL,sizeof(SAMPLE_INPUT_TYPE))==0)
	      {
      		LOG_DEBUG(ACTION_DEBUG,"sample_len=%d sen_in=%f amp_in=%f\r\n",sample_data_length,3.3*my_sample_data.sen_in/4096,3.3*my_sample_data.amp_in/4096);
      	  }
  	    else if(query_sample_data(NULL,sizeof(SAMPLE_INPUT_TYPE))==0)
	      {
      		LOG_DEBUG(ACTION_DEBUG,"sample_len=%d sen_in=%f amp_in=%f\r\n",sample_data_length,3.3*my_sample_data.sen_in/4096,3.3*my_sample_data.amp_in/4096);
      	  }
  	    else
  	      {
      	    LOG_DEBUG(ERROR_DEBUG,"query sample data failed\n");	
      	  }
		sample_para.len=my_para_data.mach_run_para.sample_len;
		sample_para.intval=my_para_data.mach_run_para.sample_rate;
		sample_para.offs=my_para_data.mach_run_para.sample_offs;	
		sample_para.co2_pos=my_para_data.mach_run_para.co2_pos;//CO2切换位置 
		sample_para.co2_len=CO2_SAMPLE_LEN;//CO2采样长度 
		if(start_sample_data(0,&sample_para)==0)
         {
           gas_make_stat.dly_cnt=0;							 
           gas_make_stat.step++;
		   start_time=time(NULL);//记录采样开始时刻 
		   co2_sample_state=0; //        	
         }
        else
         {
           gas_make_stat.dly_cnt++;
		   if(gas_make_stat.dly_cnt>8)
		    {
   			  LOG_DEBUG(ERROR_DEBUG,"make_step=%d:start data sample failed,exit\n",gas_make_stat.step);
   			  stop_gas_make(gas_make_stat.step);
              return gas_make_stat.step;
    		}
		   LOG_DEBUG(ERROR_DEBUG,"make_step=%d:WARNING:start data sample failed,Try again\n",gas_make_stat.step);	
         }
        break;
    case 30:  
        if(my_para_data.mach_run_para.co2_pos)
     	  {      	  	
             switch(co2_sample_state)
                {
                 case 0:
                    now=time(NULL);
                    //提前CO2_BACK_NUM个点切换co2检测阀 
                    if(now>=(((my_para_data.mach_run_para.co2_pos-CO2_BACK_NUM)*my_para_data.mach_run_para.sample_rate)/1000+start_time))
                     {
                      //打开CO2阀门	
                      co2_sample_state=1;
                      start_time=now+CO2_SAMPLE_LEN*3+(((CO2_BACK_NUM)*my_para_data.mach_run_para.sample_rate)/1000);
                      relay_set(RLY_YV21,ON,1);//  
					  LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV21 ON for co2\n",gas_make_stat.step);
					 }
				    break;
				 case 1:
				    now=time(NULL);
				    if((now>start_time))
                     {
                      //关闭CO2阀门	
                      co2_sample_state=2;
                      relay_set(RLY_YV21,OFF,1);//
		              LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV21 OFF\n",gas_make_stat.step);  
                     }
				    break;
				 default:
				    break;	
                }
          }		 
	    my_sample_data.co2_len=0;   
        if(query_sample_data((unsigned char *)&sample_data_length,sizeof(SAMPLE_INPUT_TYPE))==0)
          {
          	if(my_para_data.mach_run_para.co2_pos)
          	  {      	  					
				if(my_sample_data.co2_len>0)
                 {
                   //读CO2数据 
                   LOG_DEBUG(TRACE_DEBUG,"make_step=%d:co2_len=%d\n",gas_make_stat.step,my_sample_data.co2_len);	
				   if(read_co2_data(2*my_sample_data.co2_len)==0)
				     {
 				       LOG_DEBUG(ERROR_DEBUG,"make_step=%d:read co2 data ok\n",gas_make_stat.step);		
     				   if(save_co2_data(2*my_sample_data.co2_len)==0)
     				     {
     				       if(clear_co2_data()!=0)
     				         {
         				       if(clear_co2_data()!=0)
								 {
 								   if(clear_co2_data()!=0)
									 {
 									    LOG_DEBUG(ERROR_DEBUG,"make_step=%d:clear co2 data failed\n",gas_make_stat.step);		
 									 }	
 								 }	
         				     }
     				     }
 				       else
 				         {
         				   LOG_DEBUG(ERROR_DEBUG,"make_step=%d:save co2 data failed\n",gas_make_stat.step);	
         				 }
		             }
				   else if(read_co2_data(2*my_sample_data.co2_len)==0)
				     {
 				       LOG_DEBUG(ERROR_DEBUG,"make_step=%d:read co2 data ok\n",gas_make_stat.step);	
     				   if(save_co2_data(2*my_sample_data.co2_len)==0)
     				     {
     				       if(clear_co2_data()!=0)
     				         {
         				       if(clear_co2_data()!=0)
								 {
 								   if(clear_co2_data()!=0)
									 {
 									    LOG_DEBUG(ERROR_DEBUG,"make_step=%d:clear co2 data failed\n",gas_make_stat.step);		
 									 }	
 								 }	
         				     }
     				     }
 				        else
 				         {
         				   LOG_DEBUG(ERROR_DEBUG,"make_step=%d:save co2 data failed\n",gas_make_stat.step);	
         				 }
		             }
				   else if(read_co2_data(2*my_sample_data.co2_len)==0)
				     {
     				   LOG_DEBUG(ERROR_DEBUG,"make_step=%d:read co2 data ok\n",gas_make_stat.step);	
     				   if(save_co2_data(2*my_sample_data.co2_len)==0)
     				     {
     				       if(clear_co2_data()!=0)
     				         {
         				       if(clear_co2_data()!=0)
								 {
 								   if(clear_co2_data()!=0)
									 {
 									    LOG_DEBUG(ERROR_DEBUG,"make_step=%d:clear co2 data failed\n",gas_make_stat.step);		
 									 }	
 								 }	
         				     }
     				     }
		               else
 				         {
         				   LOG_DEBUG(ERROR_DEBUG,"make_step=%d:save co2 data failed\n",gas_make_stat.step);	
         				 }
		             }
				   else
				     {
     				   LOG_DEBUG(ERROR_DEBUG,"make_step=%d:read co2 data failed\n",gas_make_stat.step);	
     				 }	
                 }
  	          }		   
         	if(sample_data_length>0)
         	  {
      	  	   LOG_DEBUG(ACTION_DEBUG,"make_step=%d:sample data length is %d\n",gas_make_stat.step,sample_data_length);
               gas_make_stat.dly_cnt=0;							 
               gas_make_stat.step++;   	         	
  	          }
			else
			  {
  			    gas_make_stat.dly_cnt++;	
  			  }	
          }
        else
          {
          	gas_make_stat.dly_cnt++;
          }
        
		if(gas_make_stat.dly_cnt>20*60*2) //大于20分钟 
		  {
  		    LOG_DEBUG(ERROR_DEBUG,"make_step=%d:wait data sample complete timeout,exit\n",gas_make_stat.step);
            stop_gas_make(gas_make_stat.step);
            return gas_make_stat.step;
  		  }       
        break;
    case 31://读取数据 
        if(read_sample_data(2*my_para_data.mach_run_para.sample_len)==0)
          {
          	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:read sample data complete\n",gas_make_stat.step);
      	    if(save_sample_data(2*my_para_data.mach_run_para.sample_len)!=0)
      	      {
  	      	    LOG_DEBUG(ERROR_DEBUG,"make_step=%d:WARNING:save data to file failed\n",gas_make_stat.step);
      	      }
   	        else
   	          {
          	    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:save sample data success\n",gas_make_stat.step);	
       	      }
            gas_make_stat.dly_cnt=0;							 
            gas_make_stat.step++;
 
            if(my_para_data.mach_run_para.sample_cool_en!=0)
             {
               set_pid_ch(PID_SAMPLE,PID_DIS,0);
               relay_set(RLY_FAN,OFF,0);//采样单元电源 
               relay_set(RLY_KA4,OFF,1);//采样单元风扇               
			   LOG_DEBUG(ACTION_DEBUG,"make_step=%d:disable sample room cool,KA5 OFF KA4 OFF\n",gas_make_stat.step);	
             }   	
          }
        else
          {
            gas_make_stat.dly_cnt++;
			if(gas_make_stat.dly_cnt>4)	
			 {
 			   LOG_DEBUG(ERROR_DEBUG,"make_step=%d:read sample data failed,exit\n",gas_make_stat.step);
   			    if(my_para_data.mach_run_para.sample_cool_en!=0)
                {
                 set_pid_ch(PID_SAMPLE,PID_DIS,0);
                 relay_set(RLY_FAN,OFF,0);//采样单元电源 
                 relay_set(RLY_KA4,OFF,1);//采样单元风扇 
			     LOG_DEBUG(ACTION_DEBUG,"make_step=%d:disable sample room cool,KA4 OFF KA5 OFF\n",gas_make_stat.step);	
                }   
		       stop_gas_make(gas_make_stat.step);
               return gas_make_stat.step;	
 			 }
		    LOG_DEBUG(ERROR_DEBUG,"make_step=%d:WARNING:read sample data failed,Try again\n",gas_make_stat.step);
          }
        break;
    case 32://完成采样
        if(clear_sample_data()!=0)
          {
          	 LOG_DEBUG(ERROR_DEBUG,"make_step=%d:WARNING:clear sample data failed\n",gas_make_stat.step);
            if(clear_sample_data()!=0)
             {
           	    LOG_DEBUG(ERROR_DEBUG,"make_step=%d:WARNING:clear sample data failed\n",gas_make_stat.step);
               if(clear_sample_data()!=0)
                {
                   LOG_DEBUG(ERROR_DEBUG,"make_step=%d:WARNING:clear sample data failed\n",gas_make_stat.step); 
                }
             }
          }
        LOG_DEBUG(ACTION_DEBUG,"make_step=%d:All action have completed\n",gas_make_stat.step);	    
        get_temperature(TEMP_SPZ,&fval);
        ana_result.spzTemp2=fval;
        LOG_DEBUG(ACTION_DEBUG,"make_step=%d:色谱柱温度=%f度\n",gas_make_stat.step,fval);
       	get_temperature(TEMP_SAM,&fval);
	    ana_result.samTemp2=fval;
	    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:采样室温度=%f度\n",gas_make_stat.step,fval);
	    get_presure(PRES_PA1,&fval);
	    ana_result.pres=fval; 
		LOG_DEBUG(ACTION_DEBUG,"make_step=%d:载气压力=%fMpa\n",gas_make_stat.step,fval);
		relay_dly_close_set(RLY_YV18,1*60*2);//延时10分钟关吹扫阀 
		//relay_dly_close_set(RLY_YV20,10*60*2);//延时10分钟关YV20
		relay_dly_close_set(RLY_SENSOR,1*60*2);//延时10分钟关传感器加热 
		gas_make_stat.dly_cnt=0;
		if(io_get(STA_I8)==ON)
		  {
 			gas_make_stat.step=43;
  		  }
        else
          {
            gas_make_stat.step++;	
          }        
        break;
//此处开始新添加的 
	case 33:
	      if(gas_make_stat.dly_cnt==0)
          {
          	relay_set(RLY_YV5,OFF,0);
            relay_set(RLY_YV4,ON,1);  
			LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV4 ON,YV5 OFF\n",gas_make_stat.step);        
            gas_make_stat.dly_cnt++;
   	      }
          else
          {
            //等待I5到位
            if(gas_make_stat.dly_cnt>I5_WAIT_CNT)
              {
      	        if(io_get(STA_I5)==ON)
			     {
  			       gas_make_stat.dly_cnt=0;	
				   LOG_DEBUG(ACTION_DEBUG,"make_step=%d:I5 have arrived in right position\n",gas_make_stat.step);  						 
                   gas_make_stat.step++;
                   break;
  			     }
				else
				 {
	               if(gas_make_stat.dly_cnt>I5_WAIT_MAX)
                    {
                   	  LOG_DEBUG(ERROR_DEBUG,"make_step=%d:wait I5 arrived ON timeout,exit\n",gas_make_stat.step);
                   	  stop_gas_make(gas_make_stat.step);
     				  gas_make_stat.dly_cnt=0;							 
                      gas_make_stat.step=0; 
					  return gas_make_stat.step;                     
     				}	
 				 }	             	
              }
 	        gas_make_stat.dly_cnt++;
          }
	    break;
    case 34:
         relay_set(RLY_YV11,ON,1);
		 LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV11 ON\n",gas_make_stat.step); 
         gas_make_stat.dly_cnt=0;							 
         gas_make_stat.step++;
        break;
    case 35:
         relay_set(RLY_YV12,ON,1);
		 LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV12 ON\n",gas_make_stat.step); 
         gas_make_stat.dly_cnt=0;							 
         gas_make_stat.step++;
        break;
    case 36://等待2分钟 
         gas_make_stat.dly_cnt++; 
	    if(gas_make_stat.dly_cnt>(180*2))
	      {
      		gas_make_stat.dly_cnt=0;
		    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 1min completed\n",gas_make_stat.step);  							 
            gas_make_stat.step++;
      	  }	    
        break;
    case 37:
        relay_set(RLY_YV12,OFF,1);
		 LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV12 OFF\n",gas_make_stat.step); 
         gas_make_stat.dly_cnt=0;							 
         gas_make_stat.step++;
        break;
    case 38://等待5秒 
         gas_make_stat.dly_cnt++; 
	    if(gas_make_stat.dly_cnt>GAS_ADMISSION_TIME)
	      {
      		gas_make_stat.dly_cnt=0;
		    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 5s completed\n",gas_make_stat.step);  							 
            gas_make_stat.step++;
      	  }	   
        break;
    case 39://开YV5,关YV4,等待到位 
 	      if(gas_make_stat.dly_cnt==0)
          {
          	//relay_set(RLY_YV13,ON,0);
          	//relay_set(RLY_YV5,ON,0);
            relay_set(RLY_YV4,OFF,1);  
			LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV4 OFF\n",gas_make_stat.step);        
            gas_make_stat.dly_cnt++;
   	      }
          else
          {
            //等待I4到位
            if(gas_make_stat.dly_cnt>I4_WAIT_CNT)
              {
      	        if(io_get(STA_I4)==ON)
			     {
  			       gas_make_stat.dly_cnt=0;	
				   LOG_DEBUG(ACTION_DEBUG,"make_step=%d:I4 have arrived in right position\n",gas_make_stat.step);  						 
                   gas_make_stat.step++;
                   break;
  			     }
				else
				 {
	               if(gas_make_stat.dly_cnt>32)
                    {
                   	  LOG_DEBUG(ERROR_DEBUG,"make_step=%d:wait I4 arrived ON timeout,exit\n",gas_make_stat.step);
                   	  stop_gas_make(gas_make_stat.step);
     				  gas_make_stat.dly_cnt=0;							 
                      gas_make_stat.step=0; 
					  return gas_make_stat.step;                     
     				}	
 				 }	             	
              }
 	        gas_make_stat.dly_cnt++;
          }
        break;
    case 40://关YV11 
        gas_make_stat.dly_cnt++;
        if(gas_make_stat.dly_cnt>32)
         {
       		if(io_get(STA_I8)==ON)
       		 { 		      
              relay_set(RLY_YV11,OFF,1);
          	  LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV11 OFF\n",gas_make_stat.step);  
             }
			gas_make_stat.dly_cnt=0;							 
            gas_make_stat.step++;   
			//检测I8,有信号就不用开YV5,无信号就开YV5,等待3秒,再检测I8,有的话关YV11,YV5     	
         }
        break;
//此处开始新添加的结束 
    case 41:
    	if(io_get(STA_I8)==OFF)
         {      			      
           LOG_DEBUG(ERROR_DEBUG,"make_step=%d:I8 is not in right position,YV5 ON\n",gas_make_stat.step);
		   relay_set(RLY_YV5,ON,1);
	       gas_make_stat.dly_cnt=0;
           gas_make_stat.step++; 	
       	 }
   	    else
   	     {
   	      gas_make_stat.dly_cnt=0;
          gas_make_stat.step+=2;  	   	
  	     }
        break;
    case 42:
        gas_make_stat.dly_cnt++;
        if(gas_make_stat.dly_cnt>=8)
          {
            if(io_get(STA_I8)==OFF)
			  {
                LOG_DEBUG(ERROR_DEBUG,"make_step=%d:I8 is not in right position,exit\n",gas_make_stat.step); 	
  			  }
		    relay_set(RLY_YV11,OFF,1);
		    usleep(1000000);
            relay_set(RLY_YV5,OFF,1);           
			LOG_DEBUG(ERROR_DEBUG,"make_step=%d:YV11 OFF,YV5 OFF\n",gas_make_stat.step);
			gas_make_stat.step++;	
          }
        break;
    case 43://延时2分钟
        gas_make_stat.dly_cnt++;
        if(gas_make_stat.dly_cnt>1*60*2)
          {
            relay_set(RLY_SENSOR,OFF,0);//
          	relay_set(RLY_YV18,OFF,0);//
   	        //relay_set(RLY_YV20,OFF,1);//
   	        LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV18 OFF,YV20 OFF\n",gas_make_stat.step);
            curr_state=MODE_IDLE;
            gas_make_stat.step=0;//2015-08-17 22:06添加 
			//memset(&my_action_state,0,sizeof(&my_action_state));	
          }       
        break;
   //I8,I9故障处理 
  //y4 on,等到位,y11 onf,1分钟,y4 off,等油缸到位,再检测i8,y5 on 3s,再检测I8,关y11,关yv5 
    case 44:
        relay_set(RLY_YV4,ON,1);
        LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV4 ON\n",gas_make_stat.step);
        gas_make_stat.dly_cnt=0;
        gas_make_stat.step++;  
        break;
    case 45:
        if(io_get(STA_I5)==ON)
          {
	        gas_make_stat.dly_cnt=0;	
	        LOG_DEBUG(ACTION_DEBUG,"make_step=%d:I5 have arrived in right position\n",gas_make_stat.step);  						 
            gas_make_stat.step++;
            relay_set(RLY_YV11,ON,0);
            relay_set(RLY_YV12,ON,1);
            LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV11 ON,YV12 ON\n",gas_make_stat.step);  
            break;
          }
		else
	      {
            if(gas_make_stat.dly_cnt>I5_WAIT_MAX)
              {
           	    LOG_DEBUG(ERROR_DEBUG,"make_step=%d:wait I5 arrived ON timeout,exit\n",gas_make_stat.step);
           	    stop_gas_make(gas_make_stat.step);
                gas_make_stat.dly_cnt=0;							 
                gas_make_stat.step=0; 
                return gas_make_stat.step;                     
              }	
            gas_make_stat.dly_cnt++;
	      }	             	
        break;
    case 46:
         gas_make_stat.dly_cnt++;
        if(gas_make_stat.dly_cnt>(180*2))
          {
       	   relay_set(RLY_YV12,OFF,1);
		    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV12 OFF\n",gas_make_stat.step);
       	   usleep(1000000);
           relay_set(RLY_YV4,OFF,1);
           LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV4 OFF\n",gas_make_stat.step);
           gas_make_stat.dly_cnt=0;
           gas_make_stat.step++; 	
          }
        break;
    case 47:
        if(io_get(STA_I4)==ON)
          {
	        gas_make_stat.dly_cnt=0;	
	        LOG_DEBUG(ACTION_DEBUG,"make_step=%d:I4 have arrived in right position\n",gas_make_stat.step);  						 
            gas_make_stat.step++;
            break;
          }
		else
	      {
            if(gas_make_stat.dly_cnt>I4_WAIT_MAX)
              {
           	    LOG_DEBUG(ERROR_DEBUG,"make_step=%d:wait I4 arrived ON timeout,exit\n",gas_make_stat.step);
           	    stop_gas_make(gas_make_stat.step);
                gas_make_stat.dly_cnt=0;							 
                gas_make_stat.step=0; 
                return gas_make_stat.step;                     
              }	
            gas_make_stat.dly_cnt++;
	      }	            
        break;
    case 48:
        //if(io_get(STA_I8)==ON)
         if((io_get(STA_I8)==ON)&&(io_get(STA_I9)==OFF))
          {
       	   relay_set(RLY_YV11,OFF,1);
       	   LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV11 OFF\n",gas_make_stat.step);
           gas_make_stat.repeat_cnt=0;
           gas_make_stat.dly_cnt=0;							 
           gas_make_stat.step=1; 
				//开始动作		
          }
        else
          {			
            relay_set(RLY_YV5,ON,1);
			LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV5 ON\n",gas_make_stat.step);
			relay_set(RLY_YV13,ON,1);
            LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV13 ON\n",gas_make_stat.step);
			gas_make_stat.dly_cnt=0;							 
            gas_make_stat.step++; 	
          }
        break;
    case 49:
        gas_make_stat.dly_cnt++;
        if(gas_make_stat.dly_cnt>8)
          {
			relay_set(RLY_YV13,OFF,1);
            LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV13 OFF\n",gas_make_stat.step);

          	relay_set(RLY_YV11,OFF,1);
            LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV11 OFF,YV5 OFF\n",gas_make_stat.step);
          	usleep(1000000);
            relay_set(RLY_YV5,OFF,1);         
			//if(io_get(STA_I8)==ON)
		    if((io_get(STA_I8)==ON)&&(io_get(STA_I9)==OFF))
			  {
  			    gas_make_stat.repeat_cnt=0;
                gas_make_stat.dly_cnt=0;							 
                gas_make_stat.step=1; 
				//开始动作	
  		 	  }
			else
			  {
  			    if(gas_make_stat.repeat_cnt<2)
  			      {
      			    gas_make_stat.repeat_cnt++;
					gas_make_stat.dly_cnt=0;							 
                    gas_make_stat.step=44; 	
      			  }
			    else
			      {
      			    //结束
			       LOG_DEBUG(ERROR_DEBUG,"make_step=%d:I8 is not in right position,exit\n",gas_make_stat.step);
           	       stop_gas_make(gas_make_stat.step);
                   gas_make_stat.dly_cnt=0;							 
                   gas_make_stat.step=0; 
                   gas_make_stat.repeat_cnt=0; 
                   return gas_make_stat.step;  	
      			  }
  			  }	
          }
        break;
    case 50://提前15分钟开吹扫阀 
       if(time(NULL)>=(time_record.sample_start_time+30*60))
         {
       	   //提前15分钟开吹扫阀,开启 
  	   	   relay_set(RLY_YV18,ON,1);
      	   LOG_DEBUG(TRACE_DEBUG,"Preaction:YV18 ON\n");   	       	  
           gas_make_stat.dly_cnt=0;
		   gas_make_stat.repeat_cnt=0; 							 
           gas_make_stat.step++; 
		 }//passed 
        break;
    case 51://启动动作流程,
       if(time(NULL)>=(time_record.sample_start_time+45*60))
        {         
   	     memset(&gas_clean_stat,0,sizeof(gas_clean_stat));
   	     memset(&gas_make_stat,0,sizeof(gas_make_stat));
   	     gas_clean_stat.step=1;//设置清洗步奏 
         if((io_get(STA_I8)==ON)&&(io_get(STA_I9)==OFF))
 	       {
      	     LOG_DEBUG(ACTION_DEBUG,"enter normal make gas\n"); 
      	     gas_make_stat.step=1;//设置制气步奏 
 	       }       	     
         else
           {
          	 LOG_DEBUG(ACTION_DEBUG,"enter fault process for make gas\n");
       	   	 gas_make_stat.step=44;
       	   }
         relay_set(RLY_YV18,ON,0);//开吹扫阀 
	     relay_set(RLY_KA3,OFF,0);//关柜子风扇
		 LOG_DEBUG(ACTION_DEBUG,"YV18 ON,KA3 OFF\n");
		 time_record.heat_start_time=time(NULL)+60;//一分钟后开传感器加热 
		 relay_dly_open_set(RLY_SENSOR,1*60);//1分钟后开启 		     	
        } 
        break;
	default:
	    break;
  }
 return 0;	
}

int sensor_test(unsigned int num)
{ 	
 unsigned int sample_data_length=0;
 SAMPLE_PARA_TYPE tmp_sample_para;
  relay_set(RLY_YV18,ON,1);
  //while(start_sample_data(0,num,220,my_para_data.mach_run_para.sample_offs)!=0)
   // sleep(3);
 tmp_sample_para.len=num;
 tmp_sample_para.intval=my_para_data.mach_run_para.sample_rate;
 tmp_sample_para.offs=my_para_data.mach_run_para.sample_offs;	
 tmp_sample_para.co2_pos=my_para_data.mach_run_para.co2_pos;//CO2切换位置 
 tmp_sample_para.co2_len=CO2_SAMPLE_LEN;//CO2采样长度 
if(start_sample_data(0,&tmp_sample_para)==0)
  {
  	sleep((num*220+1000)/1000+4);
    if(query_sample_data((unsigned char *)&sample_data_length,sizeof(SAMPLE_INPUT_TYPE))==0)
	 {
	 	printf("sample_len=%d sen_in=%f amp_in=%f\r\n",sample_data_length,3.3*my_sample_data.sen_in/4096,3.3*my_sample_data.amp_in/4096);
 	    printf("co2_len=%d co2_ppm=%d\r\n",my_sample_data.co2_len,my_sample_data.co2_ppm);
		if(read_sample_data(2*num)==0)
		{
		  if(save_sample_data(2*num)==0)
		    {
   			  if(clear_sample_data()!=0)
   			    {
    			  printf("clear data failed\r\n");  	
  			    }
    		}
		  else
		    {
    		  printf("save data failed\r\n");	
    		}	
		}
	   else
	    {
    	   printf("read data failed\r\n");	
    	}	
 	 }
    else
     {
   	   printf("query data failed\r\n");
     }
  }
 else
  {
  	printf("start sample failed\r\n");
  }
 relay_set(RLY_YV18,OFF,1);
 return 0;
}
/*
开YV4,等待到位
开YV11,
开YV12
等待5秒
关YV12
等待4秒
关YV4,开关到位后关YV11 
*/ 
int demarcate_process(void)
{
  float fval;
  unsigned int sample_data_length=0;
  switch(demarcate_stat.step)
  {
  	case 1: 	    
  	    //if(start_sample_heat(0,1)==0)//开传感器加热 
  	      //{
  	      	relay_set(RLY_SENSOR,ON,0);//
     	    relay_set(RLY_YV18,ON,0);//
   	        //relay_set(RLY_YV20,ON,1);//
   	        LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:Sensor heat started,YV18 ON,YV20 ON,start delay 15 minutes\n",demarcate_stat.step);
      	  	demarcate_stat.dly_cnt=0;
      	  	demarcate_stat.step++;
      	  //}
        /*else
  	      {
  	      	demarcate_stat.dly_cnt++;
  	      	if(demarcate_stat.dly_cnt>4)
  	      	  {
	   	      	LOG_DEBUG(ERROR_DEBUG,"demarcate_step=%d:start sensor heating failed,exit\n",demarcate_stat.step);
      	        curr_state=MODE_IDLE;
			    return demarcate_stat.step;		
  	      	  }
      	  }*/
		break;
    case 2://延时15分钟 
        demarcate_stat.dly_cnt++;
        if(demarcate_stat.dly_cnt>(15*60*2))
          {
			LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:delay 15 minutes completed\n",demarcate_stat.step);	
            demarcate_stat.dly_cnt=0;
      	  	demarcate_stat.step++;
          }
        break;
    case 3:
         relay_set(RLY_KA6,OFF,0);//关冷井风扇 
         //relay_set(RLY_KA7,OFF,0);//冷井电源 
         relay_set(RLY_KA4,OFF,0);//关采样单元风扇 
         relay_set(RLY_FAN,OFF,0);//采样单元电源 
         relay_set(RLY_KA3,OFF,0);//关柜子风扇
         set_pid_ch(PID_SAMPLE,PID_DIS,0);//关采样室温度控制电源 
         set_pid_ch(PID_LJ,PID_DIS,1);//关冷井温度控制电源 
         LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:KA6 KA7 OFF,KA4 KA5 OFF,KA3 OFF,Sample room cold disable,lj cold disable\n",demarcate_stat.step);
         demarcate_stat.dly_cnt=0;
	  	 demarcate_stat.step++;
        break;
    case 4://开YV14,延时3秒,关YV14 
        if(demarcate_stat.dly_cnt==0)
          {
            relay_set(RLY_YV14,ON,1);//	
            LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:YV14 ON\n",demarcate_stat.step);
          }
        else
          {
            demarcate_stat.dly_cnt++;
			if(demarcate_stat.dly_cnt>6)
			  {
			  	relay_set(RLY_YV14,OFF,1);//	
                LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:YV14 OFF\n",demarcate_stat.step);
  				demarcate_stat.dly_cnt=0;
	  	        demarcate_stat.step++;
  			  } 	
          }
        break;
    case 5://开YV10 
        relay_set(RLY_YV10,ON,0);//
        LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:YV10 ON\n",demarcate_stat.step);
        demarcate_stat.dly_cnt=0;
  	    demarcate_stat.step++;
        break;
    case 6://延时6秒 
        demarcate_stat.dly_cnt++;
        if(demarcate_stat.dly_cnt>12)
         {
       	   demarcate_stat.dly_cnt=0;
       	   LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:delay 6s completed\n",demarcate_stat.step);
           demarcate_stat.step++;	
         }	  	
        break;
    case 7://关YV10 
        relay_set(RLY_YV10,OFF,0);//
        LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:YV10 OFF\n",demarcate_stat.step);
        demarcate_stat.dly_cnt=0;
  	    demarcate_stat.step++;
        break;
    case 8://检测压力,DLY 3 SECOND        
        demarcate_stat.dly_cnt++;
        //VoltCnvToPresEx(PRES_PA1,&fval); 
		get_presure(PRES_GAS_PUMP,&fval);  
        LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:Pres=%fMpa\n",demarcate_stat.step,fval);
   	    if(fval>DELT_PRES)
     	  {
   	        relay_set(RLY_YV17,ON,1);
		    LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:YV17 ON\n",demarcate_stat.step);	
 	      }
   	    else if(fabs(fval-DELT_PRES)<0.001)
 	      {
            relay_set(RLY_YV17,OFF,1);
			LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:YV17 OFF\n",demarcate_stat.step);	
          }        
        if(demarcate_stat.dly_cnt>6)
         {
       	   relay_set(RLY_YV17,OFF,1);
       	   LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:delay 3s completed,YV17 OFF\n",demarcate_stat.step);
       	   demarcate_stat.dly_cnt=0;
           demarcate_stat.step++;	
         }	 
        break;
    case 9://重复4-8两次 
        demarcate_stat.repeat_cnt++;
        LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:repeat %dst completed\n",demarcate_stat.step,demarcate_stat.repeat_cnt);
        if(demarcate_stat.repeat_cnt>=2)
          {
          	demarcate_stat.dly_cnt=0;
          	demarcate_stat.repeat_cnt=0;
            demarcate_stat.step++;
          }
        else
          {
            demarcate_stat.dly_cnt=0;
          	demarcate_stat.repeat_cnt=0;
            demarcate_stat.step=4;	
          }
        break;
    case 10://延时5秒 
        demarcate_stat.dly_cnt++;
        if(demarcate_stat.dly_cnt>10)
          {
          	LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:delay 5s completed\n",demarcate_stat.step);
            demarcate_stat.dly_cnt=0;
		    demarcate_stat.step++;	
          }
        break;
    case 11://开YV19 
         relay_set(RLY_YV19,ON,1);
       	 LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:YV19 ON\n",demarcate_stat.step);
       	 demarcate_stat.dly_cnt=0;
		 demarcate_stat.step++;	
        break;
    case 12://延时18秒 
        demarcate_stat.dly_cnt++;
        if(demarcate_stat.dly_cnt>36)
          {
          	LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:delay 18s completed\n",demarcate_stat.step);
            demarcate_stat.dly_cnt=0;
		    demarcate_stat.step++;	
          }
        break;
    case 13://关YV19 
        relay_set(RLY_YV19,OFF,1);
  	    LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:YV19 OFF\n",demarcate_stat.step);
  	    demarcate_stat.dly_cnt=0;
	    demarcate_stat.step++;	
        break;
    case 14://启动采样 
        sample_para.len=my_para_data.mach_run_para.sample_len;
		sample_para.intval=my_para_data.mach_run_para.sample_rate;
		sample_para.offs=my_para_data.mach_run_para.sample_offs;	
		sample_para.co2_pos=my_para_data.mach_run_para.co2_pos;
		sample_para.co2_len=CO2_SAMPLE_LEN;
		if(start_sample_data(0,&sample_para)==0)
         {
       	   LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:start sample sucessed\n",demarcate_stat.step);
           demarcate_stat.dly_cnt=0;							 
           demarcate_stat.step++;         	
         }
        else
         {
           demarcate_stat.dly_cnt++;
		   if(demarcate_stat.dly_cnt>4)
		    {
   			  LOG_DEBUG(ERROR_DEBUG,"demarcate_step=%d:start data sample failed,exit\n",demarcate_stat.step);
       	      relay_set(RLY_SENSOR,OFF,0);
    	  	  LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:stop sensor heat OK\n",demarcate_stat.step);
          	  //}
          	  relay_set(RLY_YV18,OFF,0);//
   	          //relay_set(RLY_YV20,OFF,1);//
   	          LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:YV18 OFF,YV20 OFF\n",demarcate_stat.step);
              curr_state=MODE_IDLE;
			  //memset(&my_action_state,0,sizeof(&my_action_state));
              return demarcate_stat.step;
    		}
		   LOG_DEBUG(ERROR_DEBUG,"demarcate_step=%d:WARNING:start data sample failed,Try again\n",demarcate_stat.step);	
         }
	    break;
	case 15://查是否有数据
        if(query_sample_data((unsigned char *)&sample_data_length,sizeof(SAMPLE_INPUT_TYPE))==0)
          {
         	if(sample_data_length>0)
         	  {
      	  	   LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:sample data length is %d\n",demarcate_stat.step,sample_data_length);
               demarcate_stat.dly_cnt=0;							 
               demarcate_stat.step++;   	         	
  	          }
			else
			  {
  			    demarcate_stat.dly_cnt++;	
  			  }	
          }
        else
          {
          	demarcate_stat.dly_cnt++;
          }
        
		if(demarcate_stat.dly_cnt>20*60*2) //大于20分钟 
		  {
  		    LOG_DEBUG(ERROR_DEBUG,"demarcate_step=%d:wait data sample complete timeout,exit\n",demarcate_stat.step);
            //关传感器加热,YV18,YV20 
			/*if(start_sample_heat(0,0)!=0)//停止感器加热
          	  {
  	            LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:stop sensor heat failed\n",demarcate_stat.step);	
  	          }
  	          else
  	          {*/
  	            relay_set(RLY_SENSOR,OFF,0);
          	  	LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:stop sensor heat OK\n",demarcate_stat.step);
          	 // }
          	  relay_set(RLY_YV18,OFF,0);//
   	          //relay_set(RLY_YV20,OFF,1);//
   	          LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:YV18 OFF,YV20 OFF\n",demarcate_stat.step);
              curr_state=MODE_IDLE;
			  //memset(&my_action_state,0,sizeof(&my_action_state));
            return demarcate_stat.step;
  		  }        
	    break;
	case 16://读数据
        if(read_sample_data(4000)==0)
          {
          	LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:read sample data complete\n",demarcate_stat.step);
      	    if(save_sample_data(8000)!=0)
      	      {
  	      	    LOG_DEBUG(ERROR_DEBUG,"demarcate_step=%d:WARNING:save data to file failed\n",demarcate_stat.step);
      	      }
   	        else
   	          {
          	    LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:save sample data success\n",demarcate_stat.step);	
       	      }
            demarcate_stat.dly_cnt=0;							 
            demarcate_stat.step++;          	
          }
        else
          {
            demarcate_stat.dly_cnt++;
			if(demarcate_stat.dly_cnt>4)	
			 {
 			   LOG_DEBUG(ERROR_DEBUG,"demarcate_step=%d:read sample data failed,exit\n",demarcate_stat.step);
   			   //关传感器加热,YV18,YV20 
		       //stop_gas_make(demarcate_stat.step);
               return demarcate_stat.step;	
 			 }
		    LOG_DEBUG(ERROR_DEBUG,"demarcate_step=%d:WARNING:read sample data failed,Try again\n",demarcate_stat.step);
          } 
	    break;
	case 17://清除数据 
		if(clear_sample_data()!=0)
          {
          	 LOG_DEBUG(ERROR_DEBUG,"demarcate_step=%d:WARNING:clear sample data failed\n",demarcate_stat.step);
            if(clear_sample_data()!=0)
             {
           	    LOG_DEBUG(ERROR_DEBUG,"demarcate_step=%d:WARNING:clear sample data failed\n",demarcate_stat.step);
               if(clear_sample_data()!=0)
                {
                   LOG_DEBUG(ERROR_DEBUG,"demarcate_step=%d:WARNING:clear sample data failed\n",demarcate_stat.step); 
                }
             }
          }
        demarcate_stat.dly_cnt=0;
		demarcate_stat.repeat_cnt=0;							 
        demarcate_stat.step++;    
	    break; 
    case 18://收尾 
 		//if(start_sample_heat(0,0)==0)//关传感器加热 
  	     // {
  	     	relay_set(RLY_SENSOR,OFF,0);
     	    relay_set(RLY_YV18,OFF,0);//
   	        //relay_set(RLY_YV20,OFF,1);//
   	        LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:Sensor heat stoped,YV18 OFF,YV20 OFF,All action have completed\n",demarcate_stat.step);
      	  	demarcate_stat.dly_cnt=0;
      	  	demarcate_stat.step=0;
      	  	curr_state=MODE_IDLE;
      	/*  }
        else
  	      {
  	      	demarcate_stat.dly_cnt++;
  	      	if(demarcate_stat.dly_cnt>8)
  	      	  {
     	  	    relay_set(RLY_YV18,OFF,0);//
   	            relay_set(RLY_YV20,OFF,1);//
   	            curr_state=MODE_IDLE;
	   	      	LOG_DEBUG(ERROR_DEBUG,"demarcate_step=%d:stop sensor heating failed,YV18 OFF,YV20 OFF\n",demarcate_stat.step);
      	        return demarcate_stat.step;		
  	      	  }
      	  }*/
        break;
    default:
        break;
  }
 return 0;
}

void idle_process(void)
{
  	
}

int main_timeout_process(void)
{
  switch(curr_state)
   {
  	 case SIX_SAMPLE:
  	    gas_make();
  		break;
     case DEMARCATE:
        demarcate_process();
        break;
     case REMOVE_GAS://脱气 
        remove_gas_process();
        break;
     case GAS_FAULT_PROCESS://脱气故障处理 
        gas_flt_process();
        break;
     case PUMP_OIL_PROCESS:
        break;
  	 default:
  	    idle_process(); 
  		break;
   }
 return 0;
}


void gas_pump_process(void)
{
 static unsigned int run_tick=0;
 static unsigned int less_tick=0;
 static unsigned int larg_tick=0;
 static unsigned int on_time_tick=0;
 static float start_pres;
 unsigned char curr_state;
 float curr_pres,pre_pres;
 run_tick++;
 if((run_tick&1)==0)//每隔1秒运行一次
 {
  get_presure(PRES_PA1,&curr_pres);//获取载气压力 
  relay_get(RLY_PUMP,&curr_state);//获取当前状态 
  if(curr_state==ON)//当前处于开启状态 
   {
     on_time_tick++;
     if(on_time_tick==2)
       {
       	 start_pres=curr_pres;//记录起始压力
       }
     if(on_time_tick==60)//2022-08-15由24秒改成60秒
       {
        if(curr_pres<(start_pres+0.1))
		  {
  		    relay_set(RLY_PUMP,OFF,1);
  		    on_time_tick=0;
  			LOG_DEBUG(ERROR_DEBUG,"gas pump working bad,start_pres=%fMpa,curr_pres=%fMpa\r\n",start_pres,curr_pres);
  		    my_para_data.mach_run_para.auto_gas_en=0;
  		    //载气异常告警
			//ysp_glb_data.ActCyGasSta.v.stVal=1;
			ysp_glb_data.ActCyGasSta.v.stVal=0;//2021-06-08屏蔽载气异常告警
			ysp_glb_data.ActCyGasSta.q=0;
			ysp_glb_data.ActCyGasSta.t.secs=time(NULL); 
		    return;
		  }
		else
		  {
  		    ysp_glb_data.ActCyGasSta.v.stVal=0;
			ysp_glb_data.ActCyGasSta.q=0;
			ysp_glb_data.ActCyGasSta.t.secs=time(NULL); 	
  		  } 
       }
     if(on_time_tick>120)//计时超过30秒就关闭,2022-08-15由30秒改为120秒
      {
      	relay_set(RLY_PUMP,OFF,1);
   	    LOG_DEBUG(ERROR_DEBUG,"Stop gas pump for timeout,pres=%fMpa\r\n",curr_pres);
      }
   }
  else
   {
   	 on_time_tick=0;
   }
   
  if(curr_pres<START_PRES) //气压低就启动气缸 
   {
	 //启动气缸
	 less_tick++;
	 if(less_tick>2)
	   {	   	 
	   	 if(curr_state==OFF)
	   	   {
 	   		 //if(comunication_flg)
	         relay_set(RLY_PUMP,ON,1);
   	   		 LOG_DEBUG(TRACE_DEBUG,"Start gas pump,pres=%fMpa comunication_flg=%d\r\n",curr_pres,comunication_flg); 
   	   	   }
   		 //启动气缸  
   	   }
	 larg_tick=0; 
   }
  else
   {
   	less_tick=0;
   }
   	
  if(curr_pres>STOP_PRES)//气压高就停止气缸 
   {
     //停止气缸 
	 /*larg_tick++;
	 if(larg_tick>2)
	  {
	   //停止气缸 
  	  }*/
   	 if(curr_state==ON)
   	   {
   	   	 relay_set(RLY_PUMP,OFF,1);
         LOG_DEBUG(TRACE_DEBUG,"Stop gas pump,pres=%fMpa\r\n",curr_pres);
   	   }     
	 less_tick=0;	
   }
  else
   {
     larg_tick=0;
   }
  pre_pres=curr_pres;	
 }	 	  	
}

void save_tune_data(float *ptr,int size)
{
 FILE *fp;
 int i;
 fp=fopen("tune.csv","w");
 if(fp==NULL)
  {	
    LOG_DEBUG(ERROR_DEBUG,"Creat tune.csv failed\r\n");
    return -1;
  }	
 for(i=0;i<size;i++)
  {
    fprintf(fp,"%f\n",*ptr++);
  }
 fclose(fp);
}


void HeatAutoTuneProcess(unsigned int now)
{
 //int tmp;
 float fval;
 static int first_run=0;
 static unsigned int last_time=0;
 static float tune_data[8192];
 static int tune_index=0;
 
 if(first_run==0)
  {
  	PID_ATune_Init(&HeatAutoTuner,&HeatTuneRecord.tune_input,&HeatTuneRecord.tune_output);
  	first_run=1;
  }
 HeatTuneRecord.tune_output=my_output_data.man_output[0]; 
 if(HeatTuneRecord.auto_turn)
   {
   	 my_output_data.pid_en[0]=0;
   	 //if(((HeatAutoTuner.start_time)>now)&&(((HeatAutoTuner.start_time)-now))>3600)
   	 if((now-last_time)>=4)
   	  {
   	   get_temperature(0,&fval);
   	   HeatTuneRecord.tune_input=fval;
   	   tune_data[tune_index++]=fval;
	   if(PID_ATune_Runtime(&HeatAutoTuner,now)==1)
	    {
  	      HeatTuneRecord.auto_turn=0;
  	      LOG_DEBUG(TRACE_DEBUG,"auto tune complete\n");
  	      LOG_DEBUG(TRACE_DEBUG,"Ku=%f Pu=%f absMax=%f absMin=%f\n",HeatAutoTuner.Ku,HeatAutoTuner.Pu,HeatAutoTuner.absMax,HeatAutoTuner.absMin);
  	      LOG_DEBUG(TRACE_DEBUG,"peak1=%d peak2=%d peakCount=%d\n",HeatAutoTuner.peak1,HeatAutoTuner.peak2,HeatAutoTuner.peakCount);
  	      LOG_DEBUG(TRACE_DEBUG,"noiseBand=%f oStep=%d nLookBack=%d sampleTime=%d\n",HeatAutoTuner.noiseBand,HeatAutoTuner.oStep,HeatAutoTuner.nLookBack,HeatAutoTuner.sampleTime);
	      LOG_DEBUG(TRACE_DEBUG,"pk_1=%f pk_2=%f pk_3=%f\n",HeatAutoTuner.peaks[HeatAutoTuner.peakCount-1],HeatAutoTuner.peaks[HeatAutoTuner.peakCount-2],HeatAutoTuner.peaks[HeatAutoTuner.peakCount-3]);
		  PID_ATune_GetKp(&HeatAutoTuner,&fval);
  	      my_para_data.mach_run_para.pid_type_def[0].Kp=fval;
  	      ini_putf("MACH","kp1",my_para_data.mach_run_para.pid_type_def[0].Kp,ysp_cfg_file);
  	      PID_ATune_GetKi(&HeatAutoTuner,&fval);
  	      my_para_data.mach_run_para.pid_type_def[0].Ki=fval;
  	      ini_putf("MACH","ki1",my_para_data.mach_run_para.pid_type_def[0].Ki,ysp_cfg_file);
	      PID_ATune_GetKd(&HeatAutoTuner,&fval);
  	      my_para_data.mach_run_para.pid_type_def[0].Kd=fval;
  	      ini_putf("MACH","kd1",my_para_data.mach_run_para.pid_type_def[0].Kd,ysp_cfg_file);
	      save_tune_data(tune_data,tune_index);
  	      
		  LOG_DEBUG(TRACE_DEBUG,"kp=%f ki=%f kd=%f\n",my_para_data.mach_run_para.pid_type_def[0].Kp,my_para_data.mach_run_para.pid_type_def[0].Ki,my_para_data.mach_run_para.pid_type_def[0].Kd); 
  	    }
	   else
	    {	
  	      set_man_out(0,HeatTuneRecord.tune_output);
	    } 
       last_time=now;
       //printf("auto tune running\r\n");
      }
   }
  else
   {
   	 tune_index=0;
   }	
}

int get_rand(float *percent)
{
int ret;
struct timeval tpstart;
gettimeofday(&tpstart,NULL);
srand(tpstart.tv_usec);
//1+(int) (100.0*rand()/(RAND_MAX+1.0));//1-100
ret=(rand()%8)+1;
if((ret%2)==0)
 ret=-ret;
if(percent!=NULL)
 *percent=(float)(100+ret)/100;//产生一个分数
return ret;
}


void old_log_process(time_t now)
{
 static time_t last_remove_time=0;
 if((last_remove_time+604800)<now)//7天检查一次 
  {
  	printf("remove old entry\n");
  	LOG_DEBUG(TRACE_DEBUG,"remove old entry\n");
    remove_old_log(15552000);//保留最近180天的数据 
    //remove_old_log(100);
    last_remove_time=now;  	
  }	
}

void sample_pre_process(void)
{
   float carrier_gas_pres;
   unsigned char pump_rly_state;
   int i;

   my_para_data.mach_run_para.auto_gas_en=1;
   relay_get(RLY_PUMP,&pump_rly_state);//获取当前状态 
   get_presure(PRES_PA1,&carrier_gas_pres);//获取载气压力 
   if(carrier_gas_pres<(STOP_PRES-0.09))
   {
	 LOG_DEBUG(TRACE_DEBUG,"carrier_gas_pres=%fMpa\r\n",carrier_gas_pres);
	 relay_set(RLY_PUMP,ON,1);//打开气泵
	 LOG_DEBUG(TRACE_DEBUG,"pump power on\n");
	 for(i=0;i<30;i++)
	 {
	   sleep(1);//延时1s
	   get_presure(PRES_PA1,&carrier_gas_pres);//获取载气压力	
	   if(carrier_gas_pres>=STOP_PRES) 
	   {		 
		 break;   
	   }
	 }
	 relay_set(RLY_PUMP,OFF,1);
	 LOG_DEBUG(TRACE_DEBUG,"carrier_gas_pres=%fMpa stop pump\r\n",carrier_gas_pres);
	 LOG_DEBUG(TRACE_DEBUG,"pump power off\n");	    
   }
  else
   {
	  /* code */
   }  
}

int main(int argc,char *argv[])
{
  extern int send_ctrl_message(void);
  ACTION_MSG_TYPE my_action_msg;
  time_t now,next_sample_time,today_sec,last_sample_time;
  float env_temp;//环境温度
  float my_temp;//
  float percent; 
  int cabinet_count=0;
  unsigned int start_time;
  int i;
  int KA3_Tick=0;
  int goahead_dly=0;
  int upload_tick=0; 
  int sample_cnt;
  unsigned int smp_len;
  struct stat st;
  YSP_PRI_DATA dumy_ysp_data;
  int gas_pump_stop_tick=0;
  
  chdir("/usr/shxy/ysp/app");
  if(argc>=3)
   {
   	 if(strcmp(argv[1],"-d")==0)
   	  {
       if(stat(argv[2],&st)==0)
        {
       	  printf ("change work dir to %s\n",argv[2]); 
          chdir(argv[2]);	
        }
       else
        {
          printf("change work dir to default work dir\n"); 
          chdir("/usr/shxy/ysp/app");
        }	
      }
   }

  printf("%s\n",APP_VERSION);

  signal(SIGINT,ctrlCfun);
  
  comunication_flg=0;
  my_input_data.com_flg=1;
  
  if((sample_fd=SerialOpen(SAMPLE_SERIAL_DEV,SAMPLE_SERIAL_SPEED))==-1)
   {
     printf("open sample serial device failed\n");
     exit(1);
   }
   
  mutex_init();
  
  my_log_init();
  
  if((main_mq=create_mq(MAIN_MESSAGE_MQ,MAIN_MAX_MESSAGE,MAIN_MESSAGE_SIZE))<0)
  {
   	 printf("create main message queue failed\n");
   	 exit(2);
  }

  LoadAllPara(&my_para_data);
  
  if(ctrl_init()!=0)
  {
	printf("create ctrl thread failed\n");
    exit(3);
  }
  
  sleep(8);
   
  my_temp=my_para_data.mach_run_para.temperature[TEMP_SPZ];
  set_pid_temp(TEMP_SPZ,&my_temp,0);//设置色谱柱温度 
  if(my_para_data.mach_run_para.pid_en[PID_SPZ])
     set_pid_ch(PID_SPZ,PID_EN,0);//开启色谱柱温度控制 
  else
     set_pid_ch(PID_SPZ,PID_DIS,0);//关闭色谱柱
  
  my_temp=my_para_data.mach_run_para.temperature[TEMP_LJ]; 
  set_pid_temp(TEMP_LJ,&my_temp,0);//设置冷井温度 
  
  //printf("");
  //my_temp=my_para_data.mach_run_para.temperature[TEMP_SAM];
  //set_pid_temp(TEMP_SAM,&my_temp,0);//设置采样室温度 
  //set_pid_ch(PID_SAMPLE,PID_EN,0);//开启采样室温度控制 
  //relay_set(RLY_KA4,ON,0);//开采样单元风扇  

  //my_temp=my_para_data.mach_run_para.temperature[TEMP_LJ];
  //set_pid_temp(TEMP_LJ,&my_temp,0);//设置色谱柱温度 
  //set_pid_ch(PID_LJ,PID_EN,0);//开启色谱柱温度控制  
  //relay_set(RLY_KA6,ON,1);//开冷井风扇 
        
  
  start_time=my_para_data.mach_run_para.start_hour*3600+my_para_data.mach_run_para.start_minute*60;
  now=time(NULL);//获取当前时间 
  today_sec=now%86400;
  if(today_sec>start_time) 
   {//当天的时间已经大于设定的起始时间
     next_sample_time=(my_para_data.mach_run_para.start_interval*3600)-((today_sec-start_time)%(my_para_data.mach_run_para.start_interval*3600));
	 next_sample_time+=(now-45*60);     
   }
  else
   { //当天的时间小于设定的起始时间 
   	 next_sample_time=now-today_sec+start_time-45*60; 
   }
  LOG_DEBUG(TRACE_DEBUG,"system reset:next_sample_time is %s\n",ctime(&next_sample_time)); 
  last_sample_time=next_sample_time-(my_para_data.mach_run_para.start_interval*3600);
  last_sample_time_ex=last_sample_time;
  time_record.dummy_sample_time=0;
  
  //modbus_rtu_slave_init();
  /*time_record.room_start_time=next_sample_time-45*60;
  time_record.yv18_start_time=next_sample_time-15*60;
  time_record.heat_start_time=next_sample_time+1*60; */
  analysis_init();
  //sleep(4);
  iec61850_rx_init();
  //send_analysis_req("/COMTRADE/20150907_235546.bin",strlen("/COMTRADE/20150907_235546.bin")); 
  //sensor_test(1000);
  //return 1;
  goahead_init();
  //printf("version is v3.3:2015-11-24 10:57\n");
  LOG_DEBUG(TRACE_DEBUG,APP_VERSION);
  smp_len=0;
  if(query_sample_data((unsigned char *)&smp_len,sizeof(SAMPLE_INPUT_TYPE))!=0)
    {
   	  sleep(1);
   	  if(query_sample_data((unsigned char *)&smp_len,sizeof(SAMPLE_INPUT_TYPE))!=0)
   	   {	
		 sleep(1);   
   	     if(query_sample_data((unsigned char *)&smp_len,sizeof(SAMPLE_INPUT_TYPE))!=0)
 	     {     	 
           LOG_DEBUG(TRACE_DEBUG,"query_sample_data failed:sample board is bad\n");
         }  
   	   }
    }
 /* printf("co2_len=%d\n",my_sample_data.co2_len);
  if(read_co2_data(2*my_sample_data.co2_len)==0)
    {
      printf("read co2 ok\n");
	  if(save_co2_data(2*my_sample_data.co2_len)==0)
	    {
    	 printf("save co2 ok\n");		
    	}
	  else 
	    {
    	  printf("save co2 failed\n");	
    	}	
    }
  else
    {
      printf("read co2 failed\n");		
    }*/

  if(smp_len>0)
   {
      if(read_sample_data(2*my_para_data.mach_run_para.sample_len)==0)
        {
       	  LOG_DEBUG(ACTION_DEBUG,"read sample data complete\n");
   	      if(save_sample_data(2*my_para_data.mach_run_para.sample_len)!=0)
   	        {
     	      LOG_DEBUG(ERROR_DEBUG,"WARNING:save data to file failed\n");
   	        }
          else
            {
       	      LOG_DEBUG(ACTION_DEBUG,"save sample data success\n");	
   	        }
		  if(clear_sample_data()!=0)
		    {
    		  if(clear_sample_data()!=0)
			    if(clear_sample_data()!=0)
				   LOG_DEBUG(ERROR_DEBUG,"clr sample data failed\n");	
    		}	
        }
      else
        {
          LOG_DEBUG(ERROR_DEBUG,"WARNING:read sample data failed,Try again\n");
          if(read_sample_data(2*my_para_data.mach_run_para.sample_len)==0)
           {
       	  LOG_DEBUG(ACTION_DEBUG,"read sample data complete\n");
   	      if(save_sample_data(2*my_para_data.mach_run_para.sample_len)!=0)
   	        {
     	      LOG_DEBUG(ERROR_DEBUG,"WARNING:save data to file failed\n");
   	        }
          else
            {
       	      LOG_DEBUG(ACTION_DEBUG,"save sample data success\n");	
   	        }
		  if(clear_sample_data()!=0)
		    {
    		  if(clear_sample_data()!=0)
			    if(clear_sample_data()!=0)
				   LOG_DEBUG(ERROR_DEBUG,"clr sample data failed\n");	
    		}	
           }
          else
           {
           	 LOG_DEBUG(ERROR_DEBUG,"EEROR:read sample data failed\n");
           }
        }   	
   }
   
  //sensor_test(256);
  while(doIt)
   {
     if(recv_mq_wait(main_mq,(void *)main_rx_buf,sizeof(main_rx_buf),500000)>0)
       {      	
       	 memcpy(&my_action_msg,main_rx_buf,sizeof(my_action_msg));
  	     LOG_DEBUG(TRACE_DEBUG,"receivd msg %d\n",my_action_msg.cmd); 
    	 main_message_process(&my_action_msg);
       }
     else
       {
       	 main_timeout_process();
       }
       
     now=time(NULL);
     
     today_sec=now%86400;//计算当天的秒数 
     
     start_time=my_para_data.mach_run_para.start_hour*3600+my_para_data.mach_run_para.start_minute*60;
     if(curr_state==MODE_IDLE)
      {
      	//当前时间处于采样完1小时后和下次采样前50分钟 
      	if((now>=(last_sample_time+6290))&&(now<(next_sample_time-3000))) 
      	 {
   	       if((today_sec%3600)<3)//判断是否整点 
            {    		  
              /*if(my_para_data.mach_run_para.start_interval>2)
               {
      	         //整点开始,YV20工作5分钟,停止3分钟		  
               } */     
               if(my_para_data.mach_run_para.start_interval>2)
                {    
   	              //每隔两小时,整点开始,YV18工作,延时20分钟开传感器加热,45分钟延时到,全停  	       
                  //if(time_record.yv18_poll_time=now)
                  if(relay_get(RLY_YV18,NULL)!=ON)
                   {
                    LOG_DEBUG(TRACE_DEBUG,"YV18 ON,Start sensor heating\n");
				    relay_set(RLY_YV18,ON,0);
				    relay_set(RLY_SENSOR,ON,1);
				    relay_dly_close_set(RLY_YV18,20*60);//开20分钟 
			        relay_dly_close_set(RLY_SENSOR,20*60);//开20分钟 
				    time_record.yv18_poll_time=now;                  	
                   }
			    }          
            }
           else
            {
         	  //if(now>) 
            }
  	      }
	   //室外温度-室内温度>3度,开柜子风扇35分钟,停5分钟,直到室外温度-室内温度<=3度 	   
	   if((cabinet_count)%120==0) //1分钟检测一次 
	     { 	   
           get_temperature(TEMP_OUTSIDE,&my_temp);//获取室外温度
           get_temperature(TEMP_ENV,&env_temp);//获取环境温度(柜内温度)
		   if((env_temp-my_temp)>8)
		     {
     		   //开柜子风扇KA3 3分钟,停5分钟,
			   if((KA3_Tick%10)==0)
			     {
  			       relay_set(RLY_KA3,ON,0);	
     			 }
			   else if((KA3_Tick%10)==1)
			     {
     			   relay_set(RLY_KA3,OFF,0);	
     			 }
			   KA3_Tick++;	
     	 	 }
		   else
		     {
 		       relay_set(RLY_KA3,OFF,0);
			   KA3_Tick=0;		
     		 }  		   
         } 
	   cabinet_count++;
      }        

     if(now>=next_sample_time)
       {
       	 time_record.sample_start_time=now;//记录采样开始时刻 
		 //2021-03-09添加排水阀处理，每次采样前开3秒钟
		 relay_set(RLY_KA8,ON,1);
		 LOG_DEBUG(TRACE_DEBUG,"KA8 ON\n"); 
		 sleep(3);
		 relay_set(RLY_KA8,OFF,1);
		  LOG_DEBUG(TRACE_DEBUG,"KA8 OFF\n");
       	 //启动采样 
       	 LoadAllPara(&my_para_data);
       	 LOG_DEBUG(TRACE_DEBUG,"reload all parameter\n"); 
		 //run_mod=0表示正常模式,并且要upload_time(实际采样间隔)大于两倍的采样间隔才生效      	
  	     if((my_para_data.mach_run_para.run_mode==0)&&(my_para_data.mach_run_para.upload_time>=(2*my_para_data.mach_run_para.start_interval)))
  	       {
       	  	 //正常模式
       	  	 //计算采样间隔次数 
       	  	 sample_cnt=(my_para_data.mach_run_para.upload_time/my_para_data.mach_run_para.start_interval);
  	         if((upload_tick%sample_cnt)==0)
  	           { 
				 sample_pre_process();//add by tmb at 20200829
				 //启动硬采样 
       	         if(SendActionReq(SIX_SAMPLE,0)!=0)
                    LOG_DEBUG(TRACE_DEBUG,"send start six sample request failed\n");
                 else
                    LOG_DEBUG(TRACE_DEBUG,"send start six sample request ok\n");
	           }
             else
   	           {
  	             //启动软采样,上传数据 
  	             time_record.dummy_sample_time=now+75*60+get_rand(&percent);
   	           }
	         upload_tick++;  
       	   }
   	     else
   	       {
       	   	 //调试模式
			 sample_pre_process();//add by tmb at 20200829
       	     if(SendActionReq(SIX_SAMPLE,0)!=0)
                LOG_DEBUG(TRACE_DEBUG,"send start six sample request failed\n");
             else
                LOG_DEBUG(TRACE_DEBUG,"send start six sample request ok\n");
       	   }
       	 
         last_sample_time=now;   //记录最后一次采样开始时刻 
         last_sample_time_ex=now;//记录最后一次采样开始时刻     
       	   
   	     if(start_time!=(my_para_data.mach_run_para.start_hour*3600+my_para_data.mach_run_para.start_minute*60))
  	      {
 	   	    start_time=(my_para_data.mach_run_para.start_hour*3600+my_para_data.mach_run_para.start_minute*60);
  	        today_sec=now%86400;
            if(today_sec>start_time)
              { //当天的时间大于采样开始时间 
                next_sample_time=(my_para_data.mach_run_para.start_interval*3600)-((today_sec-start_time)%(my_para_data.mach_run_para.start_interval*3600));
                next_sample_time+=(now-45*60);     
              }
            else
              {  //当天的时间小于采样开始时间 
                next_sample_time=now-today_sec+start_time-45*60; 
              }
            LOG_DEBUG(TRACE_DEBUG,"sample start time changed to %d:%d\n",my_para_data.mach_run_para.start_hour,my_para_data.mach_run_para.start_minute); 
          }
         else
          {
	        next_sample_time+=(my_para_data.mach_run_para.start_interval*3600); //计算下一次采样开始时刻 
          }      	   
	     LOG_DEBUG(TRACE_DEBUG,"next start sample time is %s\n",ctime(&next_sample_time));
       } 	
      
     if(time_record.dummy_sample_time)
      {
        if(now>=time_record.dummy_sample_time)
		 {
	 	   //上传软数据 
	 	   get_rand(&percent); 
	 	   LOG_DEBUG(TRACE_DEBUG,"send dummy data:percent=%f\n",percent);
	 	   dumy_ysp_data.H2ppm=ana_result.result.H2ppm*percent;	
     	   dumy_ysp_data.COppm=ana_result.result.COppm*percent;
		   dumy_ysp_data.CH4ppm=ana_result.result.CH4ppm*percent;
		   dumy_ysp_data.C2H4ppm=ana_result.result.C2H4ppm*percent;
		   dumy_ysp_data.C2H6ppm=ana_result.result.C2H6ppm*percent;
		   dumy_ysp_data.C2H2ppm=ana_result.result.C2H2ppm*percent;
		   dumy_ysp_data.TotHyd=ana_result.result.TotHyd*percent;
		   dumy_ysp_data.CO2ppm=ana_result.result.CO2ppm*percent;
	       LOG_DEBUG(TRACE_DEBUG,"H2=%f CO=%f CH4=%f C2H4=%f\n",dumy_ysp_data.H2ppm,dumy_ysp_data.COppm,dumy_ysp_data.CH4ppm,dumy_ysp_data.C2H4ppm);
	       LOG_DEBUG(TRACE_DEBUG,"C2H6=%f C2H2=%f TotHyd=%f CO2=%f\n",dumy_ysp_data.C2H6ppm,dumy_ysp_data.C2H2ppm,dumy_ysp_data.TotHyd,dumy_ysp_data.CO2ppm);
		   //dumy_ysp_data.Mst=ana_result.result.Mst;
		   //dumy_ysp_data.temp=ana_result.result
		   dumy_ysp_data.SmpTm=now;
		   send_ysp_msg(&dumy_ysp_data);					
 		   time_record.dummy_sample_time=0;	
 		 }	
      }
	   
     if(my_para_data.mach_run_para.auto_gas_en)
      {
        gas_pump_process();	
		gas_pump_stop_tick=0;
      }  
	 else
	  {
		//2022-08-15 添加每隔半小时开启自动造气  
		gas_pump_stop_tick++;
		if(gas_pump_stop_tick>=3600)
		{
			my_para_data.mach_run_para.auto_gas_en=1;
			LOG_DEBUG(TRACE_DEBUG,"force enable auto make gas\n");
		}
	  }   
     //采样完成后,延时10分钟关传感器加热,关吹扫阀,关YV20 
     relay_dly_process();
	 HeatAutoTuneProcess(now); 
	 old_log_process(now); 
   }
  mq_close(main_mq);
  return 0;
}
