⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cb_tools.cpp

📁 LINUX系统下基于ARM处理器的使用GPRS方式通过SOCKET数据包进行煤矿瓦斯监控的程序.这是在黑龙江省现场运行的程序源代码.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
       strcat(buf_dest[i],",");
       pStr = str.getNext();
	   CB_Unit::trim(pStr);
       strcat(buf_dest[i],pStr); 
       strcat(buf_dest[i],",");
       pStr = cStr_init.getNext();
	   CB_Unit::trim(pStr);
       strcat(buf_dest[i],pStr);
	   strcat(buf_dest[i],"\r\n");
           
	}
 
    i+=1;
  }

  pFile = fopen(cSysParam->getInit(),"w+");
  for(int k=0;k<lineCount;k++)
    fputs(buf_dest[k],pFile);
  fclose(pFile);
  EXIT:
  
  return 0;
}

int convertInitdata(CB_SysParam *cSysParam,CB_User *cUser)
{
  if(convert_jw(cSysParam)<0)
   return -1;
  printf("initdata.txt start.\n");
  struct stat sb;
  stat(cSysParam->getInit(),&sb);
  FILE *pFile;
  pFile = fopen(cSysParam->getInit(),"r");
  char *buf_all = new char[sb.st_size];
  fread(buf_all,sb.st_size,1,pFile);
  fclose(pFile);
  CB_Split cSplit(buf_all,'\n');
  int i = 0;
  while(1==1)
  {
    char *buf;   
    buf = cSplit.getNext();
	if(buf == NULL)
	{
      break;   
	}
	CB_Unit::lineToStr(buf);
    CB_Unit::trim(buf);
    CB_String str;
    str.split(buf,',');
    char *pStr;
    pStr = str.getNext();
    CB_Unit::trim(pStr);
    if(strcmp(pStr,"F1")==0)
    {
      int len = strlen(pStr);
	  len += 1;
      memmove(buf,buf+len,strlen(buf)-len);  
	  CB_F1 f1;
	  f1.setUser(cUser);
	  f1.setSocketEnable(cUser->isSocketEnable());
	  f1.instance(buf,cSysParam->getColliery());
    }
	else if(strcmp(pStr,"F3")==0){
       int len = strlen(pStr);
  	   len += 1;
	   memmove(buf,buf+len,strlen(buf)-len+1);  
	   CB_F3 f3;
       f3.setSysParam(cSysParam);
	   f3.instance(buf,cSysParam->getSensor());
	} 
	else if(strcmp(pStr,"F4")==0){
       int len = strlen(pStr);
       len += 1;
	   memmove(buf,buf+len,strlen(buf)-len);  
	   CB_F4 f4;
	   f4.setUser(cUser);
	   f4.setSocketEnable(cUser->isSocketEnable());
       f4.setSysParam(cSysParam);
	   i+=1;
	   f4.instance(buf,cSysParam->getPoint(),i);
	}
   
  }

  printf("initdata.txt stop.\n");

  return 0;
}

void checkInitdataChange(CB_SysParam *cSysParam,CB_User *cUser)
{
  time_t late_time;
  time_t curr_time;
  getInitdataCurrentTime(&late_time,"/dat/dev.txt");
  while(1==1)
  {
    printf("----------------------\n");
    printf("check initdata.txt \n");
    getInitdataCurrentTime(&curr_time,"/dat/dev.txt");
	if(curr_time != late_time)
	{
      printf("initdata.txt change.\n"); 
      if(convertInitdata(cSysParam,cUser)<0)
	  {
         printf("convert initdata.txt error. \n");
      }
      late_time = curr_time;
	}
    sleep(60);
 } 

}

void getInitdataCurrentTime(time_t *curr_time,char *fileName)
{
  struct stat file_stat;
  stat(fileName,&file_stat); 
  *curr_time = file_stat.st_mtime;       
}

int convert_jw_real(CB_SysParam *cSysParam)
{
  struct stat sb;
  stat("/dat/dev.txt",&sb);
  FILE *pFile;
  pFile = fopen(cSysParam->getRealJw(),"r");
  char *buf_all = new char[sb.st_size];
  fread(buf_all,sb.st_size,1,pFile);
  fclose(pFile);
 
  CB_Split *cSplit_temp = new CB_Split(buf_all,'\n');
  int lineCount =1;
  
  while(1==1){
   char *pStr;
   pStr = cSplit_temp->getNext();
   if(pStr == NULL){
      break;
   }
   lineCount +=1;
  }
  
  lineCount = lineCount-1;
  if(lineCount <=0)
    return 0;
  char (*buf_dest)[128]; 
  buf_dest= new char[lineCount][128];
  
  CB_Split cSplit(buf_all,'\n');
  char sensor[20];
  int i = 0;
  char buf_colliery_id[20];
  char buf_date[20];
  CB_Unit::clearBuf(buf_colliery_id,20);
  CB_Unit::clearBuf(buf_date,20);
  while(1==1)
  {
    char *buf;   
    buf = cSplit.getNext();
	if(buf == NULL)
	{
      break;   
	}
    CB_Unit::lineToStr(buf);
    CB_Unit::trim(buf);
    
	if(i==0){
	   CB_String str;
	   str.split(buf,',');
	   char *pStr;
	   pStr = str.getNext();
       if(pStr == NULL)
         continue;
	   CB_Unit::trim(pStr);
	   strcpy(buf_colliery_id,"30000");
	   pStr = str.getNext();
       CB_Unit::trim(pStr);
       strcpy(buf_date,pStr);
	   i+=1;
	}else{
	   CB_String str;
	   str.split(buf,',');
	   char *pStr;
       strcpy(buf_dest[i-1],"E1");
       strcat(buf_dest[i-1],",");
	   strcat(buf_dest[i-1],buf_colliery_id);
       strcat(buf_dest[i-1],",");
	   pStr = str.getNext();
       CB_Unit::trim(pStr);
       strcat(buf_dest[i-1],pStr);
	   strcat(buf_dest[i-1],",");
	   pStr = str.getNext();
	   CB_Unit::trim(pStr);
	   strcat(buf_dest[i-1],pStr);
	   strcat(buf_dest[i-1],",");
	   pStr = str.getNext();
	   CB_Unit::trim(pStr);
	   strcat(buf_dest[i-1],pStr);
	   strcat(buf_dest[i-1],",");
	   if(atoi(pStr)!=1){
         CB_Unit::clearBuf(buf_dest[i-1],128);
		 continue;
	   }
	   strcat(buf_dest[i-1],buf_date);
	   strcat(buf_dest[i-1],"\r\n"); 
	   i+=1; 
  	}
   
  }
  pFile = fopen(cSysParam->getReal(),"w+");
  for(int k=0;k<i;k++)
    fputs(buf_dest[k],pFile);
  fclose(pFile);

  EXIT:
 
  return 0;
}

int convertRealdata(CB_SysParam *cSysParam,CB_User *cUser)
{
  
  if(convert_jw_real(cSysParam)<0)
   return -1;

  printf("convert realdata.txt start.\n");
  struct stat sb;
  stat(cSysParam->getReal(),&sb);
  FILE *pFile;
  pFile = fopen(cSysParam->getReal(),"r");
  if(pFile == NULL)
  {
    perror("can not open realdata.dest.");
    return -1;
  }
  char *buf_all = new char[sb.st_size];
  fread(buf_all,sb.st_size,1,pFile);
  fclose(pFile);
  
  int i=0;
  CB_Split cSplit(buf_all,'\n');
  while(1==1)
  {
     char *buf;   
     buf = cSplit.getNext();
	 if(buf == NULL)
	 {
      break;   
	 }
     CB_Unit::lineToStr(buf);
     CB_Unit::trim(buf);

	 CB_String str;
	 str.split(buf,',');
	 char *pStr;
	 pStr = str.getNext();
	 CB_Unit::trim(pStr);
	 
	 if(strcmp(pStr,"E1")==0)
	 {
       if(str.getSplitCount()!=6 ){
    	  continue;
	   }
	   int len = strlen(pStr);
	   len += 1;
	   int len_dest = strlen(buf)-len;
	   memmove(buf,buf+len,len_dest);
	   buf[len_dest] = '\0';
	   CB_Freal real;
	   real.setUser(cUser);
	   if(real.instance(buf,cSysParam->getRealDest())<0)
	   {
         continue;  
	   }
	 }
   
  }

  printf("realdata.txt stop.\n");
  return 0;
}

void checkRealdataChange(CB_SysParam *cSysParam,CB_User *cUser)
{
  time_t late_time;
  time_t curr_time;
  int iTime = 0;
  int iTime_ip = 0;
  getRealdataCurrentTime(&late_time,cSysParam->getRealJw());
  
  while(1==1)
  {
    char strSend[128];
    printf("---------\n");
    getRealdataCurrentTime(&curr_time,cSysParam->getRealJw());
	if(curr_time != late_time)
	{
	  printf("realdata.txt change.\n");
	  if(convertRealdata(cSysParam,cUser)<0)
	  {
         printf("convert realdata.txt error. \n");
	  }
	  
      late_time = curr_time;
	  //
 	  iTime = 0;
	  realStop(cUser,strSend,"1");
	  printf("%s\n",strSend);
	  char *rtnBuf = NULL;
	  CB_Socket cSocket;
      cSocket.initiaze();
      cSocket.send(strSend,rtnBuf);
      printf("returnBuf:%s\n",rtnBuf);

	}else{
	  
      iTime ++;
	  if(iTime>=2)
  	  {
        iTime = 0;
	    printf("real stop.\n");
		realStop(cUser,strSend,"0");
		printf("%s\n",strSend);
  	    char *rtnBuf = NULL;
        CB_Socket cSocket;
        cSocket.initiaze();
  		cSocket.send(strSend,rtnBuf);
	  }
      
	}
  if(setIP(cUser)<0){
       system("reboot");     
     }
   sleep(10);
   iTime_ip += 1;
  } 

}

void getRealdataCurrentTime(time_t *curr_time,char *fileName)
{
  struct stat file_stat;
  stat(fileName,&file_stat);
  *curr_time = file_stat.st_mtime;       
}

void checkRealStop(CB_SysParam *cSysParam,CB_User *cUser)
{
  time_t late_time;
  time_t curr_time;
  int iTime = 0;
  getRealdataCurrentTime(&late_time,cSysParam->getReal());
  while(1==1)
  {
    char strSend[128];
    strSend[0] = '\0';
	printf("---------\n");
    printf("check real. \n");
    getRealdataCurrentTime(&curr_time,cSysParam->getReal());
	if(curr_time != late_time)
	{
      printf("real change. \n");
	  late_time = curr_time;
	  iTime = 0;
	  realStop(cUser,strSend,"1");
	  char *rtnBuf = NULL;
	  CB_Socket cSocket;
      cSocket.initiaze();
      cSocket.send(strSend,rtnBuf);
	}else{
      iTime ++;
	  if(iTime>=6)
  	  {
        printf("real stop.\n");
		realStop(cUser,strSend,"0");
		printf("%s\n",strSend);
        char *rtnBuf = NULL;
        CB_Socket cSocket;
        cSocket.initiaze();
  		cSocket.send(strSend,rtnBuf);
        printf("returnBuf:%s\n",rtnBuf);
	  }

	}
   sleep(30);
  } 
}
//////
void realStop(CB_User *m_user,char *m_sendData,char *str_flag)
{
  char message[160];
  CB_Unit::clearBuf(message,160);
  
  strcpy(message,"11S+0+");
  char *p;
  p = m_user->getPerson()->name;
  strcat(message,p);
  strcat(message,"+");
  p =m_user->getPerson()->office;
  strcat(message,p);
  strcat(message,"+");
  p =m_user->getPerson()->area;
  strcat(message,p);
  strcat(message,"+");
  strcat(message,str_flag);
  strcat(message,"+");
  strcat(message,"E");
  strcpy(m_sendData,message); 
}

void cb_restart(bool isDelay)
{
   if(isDelay){
     printf("The System will be restarted after 60 sec.\n");
     sleep(60);
   }else{
     printf("The System whill be restarted.\n");
   }
   system("reboot");
}
void cb_setSysParam(CB_SysParam *cSysParam,char *argv[])
{
  if(setSysParam(cSysParam,argv)<0)
  {
    printf("setSysParam failed.\n");
	cb_restart(true);
  }
  printf("setSysParam successed.\n");
}

void cb_sleep(unsigned int seconds)
{
  sleep(seconds);
}

void cb_setUserMsg(CB_SysParam *cSysParam,CB_User *cUser)
{
  if(setUserMsg(cSysParam,cUser)<0)
  {
    printf("setUserMsg failed.\n");
	cb_restart(true);
  }
  printf("setUserMsg successed.\n");
}

void cb_convertInitData(CB_SysParam *cSysParam,CB_User *cUser)
{
  
  if(convertInitdata(cSysParam,cUser)<0){
    printf("convert initdata.txt error. \n");
	cb_restart(true);
  }
  printf("convert initdata.txt success. \n");

}

void cb_process_init(CB_SysParam *cSysParam,CB_User *cUser)
{
  pid_t pid_chg_init;
  if((pid_chg_init=fork())==0){
	checkInitdataChange(cSysParam,cUser);
  }else if(pid_chg_init < 0){
    perror("pid_chg_init error."); 
	cb_restart(true);
  }
}

void cb_process_reall(CB_SysParam *cSysParam,CB_User *cUser)
{
  pid_t pid_chg_real;
  if((pid_chg_real=fork())==0){
	checkRealdataChange(cSysParam,cUser);
  }else if(pid_chg_real < 0){
    perror("pid_chg_real error."); 
	cb_restart(true);
  }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -