123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- //
- // Created by zx on 2020/6/18.
- //
- #include <fcntl.h>
- #include <iostream>
- #include <glog/logging.h>
- #include "system_communicator.h"
- #include "Locate_communicator.h"
- #include "command_manager.h"
- #include "pathcreator.h"
- #include <Parkspace_communicator.h>
- #include <dispatch_communicator.h>
- //using google::protobuf::io::FileInputStream;
- //using google::protobuf::io::FileOutputStream;
- using google::protobuf::io::ZeroCopyInputStream;
- using google::protobuf::io::CodedInputStream;
- using google::protobuf::io::ZeroCopyOutputStream;
- using google::protobuf::io::CodedOutputStream;
- using google::protobuf::Message;
- #define MEASURE_CONNECT_STRING "tcp://192.168.0.201:30001"
- #define DISPATCH_CONNECT_STRING "tcp://192.168.0.202:30002"
- #define PARKSPACE_CONNECT_STRING "tcp://192.168.0.202:30001"
- #define SYSTEM_BIND_STRING "tcp://192.168.0.203:30001"
- #define INIT_TIME_OUT_SECONDS 300
- GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size);
- void init_glog();
- Error_manager init_communicators();
- int main(int argc,char* argv[])
- {
- init_glog();
- Error_manager code=init_communicators();
- if(code!=SUCCESS)
- {
- LOG(ERROR)<<code.to_string();
- return 0;
- }
- getchar();
- return 0;
- }
- Error_manager init_communicators()
- {
- ///初始化与终端通讯的对象
- if(System_communicator::get_instance_pointer()== nullptr)
- return FAILED;
- System_communicator::get_instance_pointer()->communication_bind(SYSTEM_BIND_STRING);
- LOG(INFO)<<"初始化通讯子节点......";
- Error_manager code;
- /*
- * 初始化各个通讯模块,
- */
- if(Locate_communicator::get_instance_pointer()== nullptr)
- return FAILED;
- code=Locate_communicator::get_instance_pointer()->communication_connect(MEASURE_CONNECT_STRING);
- if(code!=SUCCESS)
- {
- return code;
- }
- Locate_communicator::get_instance_pointer()->communication_run();
- //初始化车位分配通讯模块
- if(Parkspace_communicator::get_instance_pointer()== nullptr)
- return FAILED;
- code=Parkspace_communicator::get_instance_pointer()->communication_connect(PARKSPACE_CONNECT_STRING);
- if(code!=SUCCESS)
- {
- return code;
- }
- Parkspace_communicator::get_instance_pointer()->communication_run();
- /*
- * 初始化调度模块
- */
- Dispatch_communicator::get_instance_pointer()->communication_connect(DISPATCH_CONNECT_STRING);
- Dispatch_communicator::get_instance_pointer()->communication_run();
- /*
- * 此处添加等待各个通讯模块正常代码
- */
- std::chrono::system_clock::time_point t_start=std::chrono::system_clock::now();
- std::chrono::system_clock::time_point t_end=std::chrono::system_clock::now();
- Error_manager locate_code=ERROR,parkspace_code=ERROR,dispatch_code=ERROR;
- do{
- if(locate_code!=SUCCESS) {
- locate_code = Locate_communicator::get_instance_pointer()->check_statu();
- LOG_IF(INFO, locate_code == SUCCESS) << "测量模块初始化完成!!!";
- }
- if(parkspace_code!=SUCCESS) {
- parkspace_code = Parkspace_communicator::get_instance_pointer()->check_statu();
- LOG_IF(INFO, parkspace_code == SUCCESS) << "车位管理模块初始化完成!!!";
- }
- if(dispatch_code!=SUCCESS) {
- dispatch_code = Dispatch_communicator::get_instance_pointer()->check_statu();
- LOG_IF(INFO, dispatch_code == SUCCESS) << "调度模块初始化完成!!!";
- }
- if(locate_code==SUCCESS && parkspace_code==SUCCESS && dispatch_code==SUCCESS)
- break;
- t_end=std::chrono::system_clock::now();
- usleep(1000*300);
- }while(t_end-t_start<std::chrono::seconds(INIT_TIME_OUT_SECONDS));
- LOG_IF(ERROR,locate_code!=SUCCESS)<<"测量节点连接超时";
- LOG_IF(ERROR,parkspace_code!=SUCCESS)<<"车位管理节点连接超时";
- LOG_IF(ERROR,dispatch_code!=SUCCESS)<<"调度节点连接超时";
- if(!(locate_code==SUCCESS && parkspace_code==SUCCESS && dispatch_code==SUCCESS))
- {
- return Error_manager(ERROR,MAJOR_ERROR,"通讯节点初始化超时");
- }
- /*
- * 初始化指令执行模块
- */
- if(Command_manager::get_instance_pointer()== nullptr)
- return Error_manager(FAILED,CRITICAL_ERROR,"创建指令执行模块失败");
- code=Command_manager::get_instance_pointer()->init();
- usleep(1000*1000);
- if(code!=SUCCESS)
- {
- return code;
- }
- //运行接收指令通讯块,开始接收指令
- System_communicator::get_instance_pointer()->communication_run();
- LOG(INFO)<<"系统初始化完成 --------------------------------------------- !!!";
- return SUCCESS;
- }
- GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size)
- {
- time_t tt;
- time( &tt );
- tt = tt + 8*3600; // transform the time zone
- tm* t= gmtime( &tt );
- char buf[255]={0};
- sprintf(buf,"./%d%02d%02d-%02d%02d%02d-dump.txt",
- t->tm_year + 1900,
- t->tm_mon + 1,
- t->tm_mday,
- t->tm_hour,
- t->tm_min,
- t->tm_sec);
- FILE* tp_file=fopen(buf,"w");
- fprintf(tp_file,data,strlen(data));
- fclose(tp_file);
- }
- void init_glog()
- {
- time_t tt = time(0);//时间cuo
- struct tm* t = localtime(&tt);
- char strYear[255]={0};
- char strMonth[255]={0};
- char strDay[255]={0};
- sprintf(strYear,"%04d", t->tm_year+1900);
- sprintf(strMonth,"%02d", t->tm_mon+1);
- sprintf(strDay,"%02d", t->tm_mday);
- char buf[255]={0};
- getcwd(buf,255);
- char strdir[255]={0};
- sprintf(strdir,"%s/log/%s/%s/%s", buf,strYear,strMonth,strDay);
- PathCreator creator;
- creator.Mkdir(strdir);
- char logPath[255] = { 0 };
- sprintf(logPath, "%s/", strdir);
- FLAGS_max_log_size = 100;
- FLAGS_logbufsecs = 0;
- google::InitGoogleLogging("LidarMeasurement");
- google::SetStderrLogging(google::INFO);
- google::SetLogDestination(0, logPath);
- google::SetLogFilenameExtension("zxlog");
- google::InstallFailureSignalHandler();
- google::InstallFailureWriter(&shut_down_logging);
- FLAGS_colorlogtostderr = true; // Set log color
- FLAGS_logbufsecs = 0; // Set log output speed(s)
- FLAGS_max_log_size = 1024; // Set max log file size(GB)
- FLAGS_stop_logging_if_full_disk = true;
- }
|