/*
 * 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 "ysp_debug.h"
#include "msq.h"
#include "main.h"
#include "common.h"
#include "para.h"

typedef struct
{
int heat_flg; //传感器加热标志
int csf_flg;  //吹扫阀开启标志
int smp_flg;  //采样室温度控制标志	
}PRE_ACTION_STATE;

extern int sample_fd;
CLEAN_STAT gas_clean_stat;
CLEAN_STAT gas_make_stat;
CLEAN_STAT demarcate_stat;
PRE_ACTION_STATE my_action_state;

unsigned int relay_close_tick[61];
unsigned int relay_open_tick[61];

unsigned char main_rx_buf[MAIN_MESSAGE_SIZE];

mqd_t main_mq=(mqd_t)(-1);
static int doIt=1;

unsigned int curr_state=0;

const float sample_temp_table[]={
0,0,0,0,0,0,0,0,0,0,0,0,1.9,2.8,3.7,4.7,5.6,6.5,7.4,8.4,9.3,10.2,11.0,12.0,12.9,13.9,14.8,15.7,16.6,17.5	
};

//根据室温计算采样单元需要设定的温度 
void get_sample_temp(float *evn_temp,float *sample_temp)
{
  int temp;
  temp=(int)(*evn_temp);
  if(temp<sizeof(sample_temp_table)/sizeof(float)&&temp>11)
   {
     *sample_temp=sample_temp_table[temp]; 
   } 
  else
   {
   	 *sample_temp=sample_temp_table[12];
   }  	
}

void relay_dly_close_set(unsigned int rly_num,unsigned int dly_sec)
{
  if(rly_num>=60)
    return;
  relay_close_tick[rly_num]=2*dly_sec;
}

void relay_dly_open_set(unsigned int rly_num,unsigned int dly_sec)
{
  if(rly_num>=60)
    return;
  relay_open_tick[rly_num]=2*dly_sec;
}


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<60)
		  	      {
      	  		    relay_set(i,ON,0);	
      	  		  }                  
                else
                  {
                    if(start_sample_heat(0,1)!=0)
					  {
  						if(start_sample_heat(0,1)!=0)
  						  {
  						  	if(start_sample_heat(0,1)!=0)
  						  	  {
  	  						  	printf("start sensor heat failed\n");
  	  						  }
  						  }
  					  }	
                  }
  			  }	
  	      }
			      	
   	    if(relay_close_tick[i]>0)
   	      {
      	    relay_close_tick[i]--;
			if(relay_close_tick[i]==0)
			  {
		  	    if(i<60)
		  	      {
      	  		    relay_set(i,OFF,0);	
      	  		  }                  
                else
                  {
                    if(start_sample_heat(0,0)!=0)
					  {
  						if(start_sample_heat(0,0)!=0)
  						  {
  						  	if(start_sample_heat(0,0)!=0)
  						  	  {
  	  						  	printf("stop sensor heat failed\n");
  	  						  }
  						  }
  					  }	
                  }
  			  }	
  	      }
      }    	
    }
}

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;
}

int main_message_process(ACTION_MSG_TYPE *msg)
{
  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();
        	  curr_state=MODE_IDLE;	
        	}
	  	  break;
     case CO2_SAMPLE:
          break;
     case DEMARCATE:
          break;
	 default:
	      if(msg->cmd==START_SIX_SAMPLE)
	        {
        	  memset(&gas_clean_stat,0,sizeof(gas_clean_stat));
        	  memset(&gas_make_stat,0,sizeof(gas_make_stat));
        	  gas_clean_stat.step=1;
        	  gas_make_stat.step=1;
        	  //gas_make_stat.step=29;
        	  curr_state=SIX_SAMPLE;
       	      //relay_set(RLY_YV18,ON,0);//开吹扫阀 	         
			  printf("start six sample\n");	
		      relay_set(RLY_YV20,ON,1);//开YV20 
		      printf("YV20 ON\n");	
        	}
      	  else if(msg->cmd==START_DEMARCATE)
      	    {
    	      curr_state=DEMARCATE;	
  	        }
	  	  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");		
}

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
		    {
	    	  LOG_DEBUG(ERROR_DEBUG,"clean_step=%d:I6 is not in right pos\n",gas_clean_stat.step);
			  stop_gas_clean(gas_clean_stat.step);
			  return 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,延时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);//开冷井风扇 
	      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 and start LJ cold\n",gas_clean_stat.step);
	      break;
     case 18:
          break;
	 default:
	      break;	 	
  }	
 return 0;
}


int gas_make(void)
{
 float fval;
 unsigned int sample_data_length=0;
 
 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; 
   }
 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; 
		  }
	     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://开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=0;							 
        gas_make_stat.step++;
		break;
	case 10://延时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 11:
        if(gas_make_stat.dly_cnt==0)
          {
            relay_set(RLY_YV7,OFF,0);
            relay_set(RLY_YV6,ON,1);
		    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV6 ON YV7 OFF\n",gas_make_stat.step);
            gas_make_stat.dly_cnt++;
   	      }
          else
          {
            //等待I7到位
            if(gas_make_stat.dly_cnt>I7_WAIT_CNT)
              {
      	        if(io_get(STA_I7)==ON)
			     {
  			       gas_make_stat.dly_cnt=0;
	               LOG_DEBUG(ACTION_DEBUG,"make_step=%d:I7 have arrived in right position\n",gas_make_stat.step);
                   gas_make_stat.step++;
                   break;
  			     }
				else
				 {
	               if(gas_make_stat.dly_cnt>I7_WAIT_MAX)
                    {
            	      LOG_DEBUG(ERROR_DEBUG,"make_step=%d:wait I7 arrived in right position timeout\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 12://延时1分钟,制气时间
	    gas_make_stat.dly_cnt++; 
	    if(gas_make_stat.dly_cnt>120)
	      {
      		gas_make_stat.dly_cnt=0;
		  	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 1 minutes completed\n",gas_make_stat.step); 							 
            gas_make_stat.step++;
      	  }
		break;
	case 13://关YV4,对应STM32上清洗油路第28步 
	    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 14://开YV15 
        relay_set(RLY_YV12,OFF,0);
        relay_set(RLY_YV11,OFF,0);
        relay_set(RLY_YV15,OFF,1);
       	LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV12 OFF,YV11 OFF,YV15 OFF\n",gas_make_stat.step); 
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;
		break;
	case 15:
	    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 16://延时10秒,进气时间,admission
        gas_make_stat.dly_cnt++; 
	    if(gas_make_stat.dly_cnt>20)
	      {
      		gas_make_stat.dly_cnt=0;
		    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 10s completed\n",gas_make_stat.step);  							 
            gas_make_stat.step++;
      	  }	     
  	    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://开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 19://延时10秒 
        gas_make_stat.dly_cnt++; 
	    if(gas_make_stat.dly_cnt>20)
	      {
      		gas_make_stat.dly_cnt=0;
		    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 10s completed\n",gas_make_stat.step);  							 
            gas_make_stat.step++;
      	  }	 
        break;
    case 20:
        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); 
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;
        break;
    case 21://延时1分钟 
        gas_make_stat.dly_cnt++; 
	    if(gas_make_stat.dly_cnt>120)
	      {
      		gas_make_stat.dly_cnt=0;
		    LOG_DEBUG(ACTION_DEBUG,"make_step=%d:delay 1minutes completed\n",gas_make_stat.step);  							 
            gas_make_stat.step++;
      	  }	 
        break;
    case 22:
        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 23:
        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 24:
        gas_make_stat.dly_cnt++;
        if(gas_make_stat.dly_cnt>4)
        {       
        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 25://重复6-23
        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 24://开YV5,延时1秒 
	    gas_make_stat.dly_cnt=0;
        gas_make_stat.step++; 
	    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 25://关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 26://关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 26://等待冷井温度<-25 
	    get_temperature(TEMP_LJ,&fval);
	    if(fval>(-25))
	      {
	      	gas_make_stat.dly_cnt++;
	      	if(gas_make_stat.dly_cnt>600)//5分钟未到达设置的温度 
	      	  {
    	        LOG_DEBUG(ERROR_DEBUG,"make_step=%d:wait lj temperature arrived -25 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;	
      	  }
  	    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 27://开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 28://等待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 
	      	CurrCnvToPresEx(0,&fval);
	      	if(fval>DELT_PRES)
	      	  {
  	      	    relay_set(RLY_YV17,ON,1);
				LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV17 ON,pres=%fMpa\n",gas_make_stat.step,fval);	
  	      	  }
     	    else if(fabs(fval-DELT_PRES)<0.001)
     	      {
      	        relay_set(RLY_YV17,OFF,1);
				LOG_DEBUG(ACTION_DEBUG,"make_step=%d:YV17 OFF,pres=%fMpa\n",gas_make_stat.step,fval);	
      	      }
   	        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++;              
  			  }
      	  }
		break;
	case 29://等待冷井温度<-28 
	    get_temperature(TEMP_LJ,&fval);
	    if(fval>(-28))
	      {
	      	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;	
      	  }
  	    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 30://开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 31://延时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 32://关YV19 
        relay_set(RLY_YV19,OFF,0);
        relay_set(RLY_YV7,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 33://关冷井风扇和制冷,关采样单元风扇以及制冷输出，电柜顶上的风扇也关闭
        set_pid_ch(PID_LJ,PID_DIS,0);
	    relay_set(RLY_KA6,OFF,1);//冷井风扇 
	    
	    set_pid_ch(PID_SAMPLE,PID_DIS,0);
        relay_set(RLY_KA4,OFF,1);//采样单元风扇 
        
        relay_set(RLY_KA3,OFF,1);//柜子风扇   
		LOG_DEBUG(ACTION_DEBUG,"make_step=%d:KA6 OFF,LengJin Ctrl disable,KA4 OFF,Sample room ctrl disable,KA3 OFF\n",gas_make_stat.step);     
        //关伴热带,目前未定义tmb
        gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;
	    break;
    case 34://延时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 35://检查色谱柱温度    
        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);
      		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;
  			  }	        	
          }
        /*if(fabs(fval-60)<=0.2)
		  {
		  	gas_make_stat.repeat_cnt++;
		  	if(gas_make_stat.repeat_cnt>120)//连续稳定1分钟,即可认为稳定 
  	        {
              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)
  		       	printf("spz wendu is %f\n",fval);
			if(gas_make_stat.dly_cnt>600)//5分钟内未达到,退出采样 
			  {
  				printf("spz wendu %f is not statble,exit\n",fval);
  				stop_gas_make(gas_make_stat.step);
		        return gas_make_stat.step;
  			  }	
  		  } */
        break;
    case 36://启动采样 
        get_temperature(TEMP_SAM,&fval);
        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(start_sample_data(0,my_para_data.mach_run_para.sample_len,my_para_data.mach_run_para.sample_rate)==0)
         {
           gas_make_stat.dly_cnt=0;							 
           gas_make_stat.step++;         	
         }
        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 37:      
        if(query_sample_data((unsigned char *)&sample_data_length,sizeof(sample_data_length))==0)
          {
         	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 38://读取数据 
        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++;          	
          }
        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);
   			   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 39://完成采样
        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);
	    gas_make_stat.dly_cnt=0;							 
        gas_make_stat.step++;
        
		relay_dly_close_set(RLY_YV18,10*60*2);//延时10分钟关吹扫阀 
		relay_dly_close_set(RLY_YV20,10*60*2);//延时10分钟关YV20
		relay_dly_close_set(RLY_SENSOR,10*60*2);//延时10分钟关传感器加热 
        break;
    case 40://延时10分钟 
        gas_make_stat.dly_cnt++;
        if(gas_make_stat.dly_cnt>10*60*2)
          {
          	if(start_sample_heat(0,0)!=0)//停止感器加热
          	  {
  	            LOG_DEBUG(ACTION_DEBUG,"make_step=%d:stop sensor heat failed\n",gas_make_stat.step);	
  	          }
          	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;
			memset(&my_action_state,0,sizeof(&my_action_state));	
          }       
        break;
	default:
	    break;
  }
 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_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_KA4,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 OFF,KA4 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++;
        CurrCnvToPresEx(0,&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 %st 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://启动采样 
        if(start_sample_data(0,4000,230)==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);
       	      if(start_sample_heat(0,0)!=0)//停止感器加热
          	  {
  	            LOG_DEBUG(ACTION_DEBUG,"demarcate_step=%d:stop sensor heat failed\n",demarcate_stat.step);	
  	          }
  	          else
  	          {
          	  	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_data_length))==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
  	          {
          	  	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_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;
  }
}

void idle_process(void)
{
  	
}

int main_timeout_process(void)
{
  switch(curr_state)
   {
  	 case SIX_SAMPLE:
  	    gas_make();
  		break;
     case CO2_SAMPLE:
        break;
     case DEMARCATE:
        demarcate_process();
        break;
  	 default:
  	    idle_process(); 
  		break;
   }
 return 0;
}





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;
  unsigned char val=0;
  int i=0;
  float env_temp;//环境温度
  float my_temp;//
  int cabinet_count=0;
  unsigned int start_time;
  unsigned int sample_intval;
  
  signal(SIGINT,ctrlCfun);
  
  if((sample_fd=SerialOpen(SAMPLE_SERIAL_DEV,SAMPLE_SERIAL_SPEED))==-1)
   {
     printf("open sample serial device failed\n");
     exit(1);
   }
   

  mutex_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_para_data.start_time=8;
  //my_para_data.sample_intval=4;
     
  my_temp=60;
  set_pid_temp(TEMP_SPZ,&my_temp,0);//设置色谱柱温度 
  my_temp=-40;
  set_pid_temp(TEMP_LJ,&my_temp,0);//设置冷井温度 
  
 // my_output_data.temperature[TEMP_SAM]
  set_pid_ch(PID_SPZ,PID_EN,0);//开启色谱柱温度控制  
  //set_pid_ch(PID_SAMPLE,PID_EN,0);//开启采样室温度控制    
  //relay_set(RLY_KA4,ON,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;     
   }
  else
   {
   	 next_sample_time=now-today_sec+start_time; 
   }
  LOG_DEBUG(TRACE_DEBUG,"system reset:next_sample_time is %s",ctime(&next_sample_time)); 
  last_sample_time=next_sample_time;
  analysis_init();
  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));
       	  printf("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)
      {
   	    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>4)
             {    
   	           //每隔两小时,整点开始,YV18工作,延时20分钟开传感器加热,45分钟延时到,全停  	       
             }          
         }
        else
         {
         	
         }
	   //室外温度-室内温度>3度,开柜子风扇35分钟,停5分钟,直到室外温度-室内温度<=3度 	   
	   if((cabinet_count)%120==0) //1分钟检测一次 
	     { 	   
           get_temperature(TEMP_OUTSIDE,&my_temp);//获取室外温度
           get_temperature(TEMP_ENV,&env_temp);//获取环境温度(柜内温度)
		   if((my_temp-env_temp)>3)
		     {
     		   //开柜子风扇KA3 35分钟,停5分钟,	
     	 	 }
		   else
		     {
     			
     		 } 
         } 
	   cabinet_count++;
      }

       
     //采样开始前45分钟查表设置采样室温度,同时检测室外温度看是否启动伴热带 
      if(now>=(next_sample_time-45*60))
        {
       	  if(my_action_state.smp_flg==0)
       	    {
  	    	  get_temperature(TEMP_ENV,&env_temp);//获取环境温度 						
  	    	  get_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);
       	      set_pid_temp(TEMP_SAM,&my_temp,0);//设置采样室温度 
              relay_set(RLY_KA4,ON,0);          //开采样室风扇 
              set_pid_ch(PID_SAMPLE,PID_EN,1);  //开采样室温度控制  
              my_action_state.smp_flg=1; 
              LOG_DEBUG(TRACE_DEBUG,"Preaction:KA4 ON,Start sample room colding\n");
   	        }
         } //passed
	  
     //采样单元工作30分钟后开吹扫阀,即提前15分钟开
      if(now>=(next_sample_time-15*60))
        {
       	 //提前15分钟开吹扫阀,开启 
       	  if(my_action_state.csf_flg==0)
       	    {
       	   	  relay_set(RLY_YV18,ON,1);
   	       	  my_action_state.csf_flg=1;
   	       	  LOG_DEBUG(TRACE_DEBUG,"Preaction:YV18 ON\n"); 
   	        }
        }//passed 
	  
     //传感器加热开始于采样开始后8分钟    
       if(now>=(last_sample_time+8*60))
        { 
       	 if(my_action_state.heat_flg==0)
       	   {
     	   	 if(curr_state!=0)
     	   	  {
      	   	   if(start_sample_heat(0,1)==0)
       	   	    {
  	   	          LOG_DEBUG(TRACE_DEBUG,"Preheat:start sensor heat ok\n");
   	       	      my_action_state.heat_flg=1; 	   	       	
   	            }
               else
                {
				  LOG_DEBUG(TRACE_DEBUG,"Preheat:start sensor heat failed\n");	
                } 	   	     	
  	   	     }
   	       }
         }//passed
	   	         

     if(now>=next_sample_time)
       {
       	 //启动采样 
       	 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;//记录最后一次采样开始时刻 
       	 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(my_action_state.csf_flg==0)
		   {
		     LOG_DEBUG(TRACE_DEBUG,"Warning:csf YV18 is not opened\n");
   		   }
         if(my_action_state.heat_flg==0)
           {
		     LOG_DEBUG(TRACE_DEBUG,"Warning:sensor heating is not started\n");
           }
       }
     
     //采样完成后,延时10分钟关传感器加热,关吹扫阀,关YV20 
     relay_dly_process();	 	  
   }
  mq_close(main_mq);
  return 0;
}
