main.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // Created by zx on 2020/6/18.
  3. //
  4. #include <fcntl.h>
  5. #include <iostream>
  6. #include <glog/logging.h>
  7. #include "system_communicator.h"
  8. #include "Locate_communicator.h"
  9. #include "command_manager.h"
  10. #include "pathcreator.h"
  11. #include <Parkspace_communicator.h>
  12. #include <dispatch_communicator.h>
  13. //using google::protobuf::io::FileInputStream;
  14. //using google::protobuf::io::FileOutputStream;
  15. using google::protobuf::io::ZeroCopyInputStream;
  16. using google::protobuf::io::CodedInputStream;
  17. using google::protobuf::io::ZeroCopyOutputStream;
  18. using google::protobuf::io::CodedOutputStream;
  19. using google::protobuf::Message;
  20. #define MEASURE_CONNECT_STRING "tcp://192.168.0.201:30001"
  21. #define DISPATCH_CONNECT_STRING "tcp://192.168.0.202:30002"
  22. #define PARKSPACE_CONNECT_STRING "tcp://192.168.0.202:30001"
  23. #define SYSTEM_BIND_STRING "tcp://192.168.0.203:30001"
  24. #define INIT_TIME_OUT_SECONDS 300
  25. GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size);
  26. void init_glog();
  27. Error_manager init_communicators();
  28. int main(int argc,char* argv[])
  29. {
  30. init_glog();
  31. Error_manager code=init_communicators();
  32. if(code!=SUCCESS)
  33. {
  34. LOG(ERROR)<<code.to_string();
  35. return 0;
  36. }
  37. getchar();
  38. return 0;
  39. }
  40. Error_manager init_communicators()
  41. {
  42. ///初始化与终端通讯的对象
  43. if(System_communicator::get_instance_pointer()== nullptr)
  44. return FAILED;
  45. System_communicator::get_instance_pointer()->communication_bind(SYSTEM_BIND_STRING);
  46. LOG(INFO)<<"初始化通讯子节点......";
  47. Error_manager code;
  48. /*
  49. * 初始化各个通讯模块,
  50. */
  51. if(Locate_communicator::get_instance_pointer()== nullptr)
  52. return FAILED;
  53. code=Locate_communicator::get_instance_pointer()->communication_connect(MEASURE_CONNECT_STRING);
  54. if(code!=SUCCESS)
  55. {
  56. return code;
  57. }
  58. Locate_communicator::get_instance_pointer()->communication_run();
  59. //初始化车位分配通讯模块
  60. if(Parkspace_communicator::get_instance_pointer()== nullptr)
  61. return FAILED;
  62. code=Parkspace_communicator::get_instance_pointer()->communication_connect(PARKSPACE_CONNECT_STRING);
  63. if(code!=SUCCESS)
  64. {
  65. return code;
  66. }
  67. Parkspace_communicator::get_instance_pointer()->communication_run();
  68. /*
  69. * 初始化调度模块
  70. */
  71. Dispatch_communicator::get_instance_pointer()->communication_connect(DISPATCH_CONNECT_STRING);
  72. Dispatch_communicator::get_instance_pointer()->communication_run();
  73. /*
  74. * 此处添加等待各个通讯模块正常代码
  75. */
  76. std::chrono::system_clock::time_point t_start=std::chrono::system_clock::now();
  77. std::chrono::system_clock::time_point t_end=std::chrono::system_clock::now();
  78. Error_manager locate_code=ERROR,parkspace_code=ERROR,dispatch_code=ERROR;
  79. do{
  80. if(locate_code!=SUCCESS) {
  81. locate_code = Locate_communicator::get_instance_pointer()->check_statu();
  82. LOG_IF(INFO, locate_code == SUCCESS) << "测量模块初始化完成!!!";
  83. }
  84. if(parkspace_code!=SUCCESS) {
  85. parkspace_code = Parkspace_communicator::get_instance_pointer()->check_statu();
  86. LOG_IF(INFO, parkspace_code == SUCCESS) << "车位管理模块初始化完成!!!";
  87. }
  88. if(dispatch_code!=SUCCESS) {
  89. dispatch_code = Dispatch_communicator::get_instance_pointer()->check_statu();
  90. LOG_IF(INFO, dispatch_code == SUCCESS) << "调度模块初始化完成!!!";
  91. }
  92. if(locate_code==SUCCESS && parkspace_code==SUCCESS && dispatch_code==SUCCESS)
  93. break;
  94. t_end=std::chrono::system_clock::now();
  95. usleep(1000*300);
  96. }while(t_end-t_start<std::chrono::seconds(INIT_TIME_OUT_SECONDS));
  97. LOG_IF(ERROR,locate_code!=SUCCESS)<<"测量节点连接超时";
  98. LOG_IF(ERROR,parkspace_code!=SUCCESS)<<"车位管理节点连接超时";
  99. LOG_IF(ERROR,dispatch_code!=SUCCESS)<<"调度节点连接超时";
  100. if(!(locate_code==SUCCESS && parkspace_code==SUCCESS && dispatch_code==SUCCESS))
  101. {
  102. return Error_manager(ERROR,MAJOR_ERROR,"通讯节点初始化超时");
  103. }
  104. /*
  105. * 初始化指令执行模块
  106. */
  107. if(Command_manager::get_instance_pointer()== nullptr)
  108. return Error_manager(FAILED,CRITICAL_ERROR,"创建指令执行模块失败");
  109. code=Command_manager::get_instance_pointer()->init();
  110. usleep(1000*1000);
  111. if(code!=SUCCESS)
  112. {
  113. return code;
  114. }
  115. //运行接收指令通讯块,开始接收指令
  116. System_communicator::get_instance_pointer()->communication_run();
  117. LOG(INFO)<<"系统初始化完成 --------------------------------------------- !!!";
  118. return SUCCESS;
  119. }
  120. GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size)
  121. {
  122. time_t tt;
  123. time( &tt );
  124. tt = tt + 8*3600; // transform the time zone
  125. tm* t= gmtime( &tt );
  126. char buf[255]={0};
  127. sprintf(buf,"./%d%02d%02d-%02d%02d%02d-dump.txt",
  128. t->tm_year + 1900,
  129. t->tm_mon + 1,
  130. t->tm_mday,
  131. t->tm_hour,
  132. t->tm_min,
  133. t->tm_sec);
  134. FILE* tp_file=fopen(buf,"w");
  135. fprintf(tp_file,data,strlen(data));
  136. fclose(tp_file);
  137. }
  138. void init_glog()
  139. {
  140. time_t tt = time(0);//时间cuo
  141. struct tm* t = localtime(&tt);
  142. char strYear[255]={0};
  143. char strMonth[255]={0};
  144. char strDay[255]={0};
  145. sprintf(strYear,"%04d", t->tm_year+1900);
  146. sprintf(strMonth,"%02d", t->tm_mon+1);
  147. sprintf(strDay,"%02d", t->tm_mday);
  148. char buf[255]={0};
  149. getcwd(buf,255);
  150. char strdir[255]={0};
  151. sprintf(strdir,"%s/log/%s/%s/%s", buf,strYear,strMonth,strDay);
  152. PathCreator creator;
  153. creator.Mkdir(strdir);
  154. char logPath[255] = { 0 };
  155. sprintf(logPath, "%s/", strdir);
  156. FLAGS_max_log_size = 100;
  157. FLAGS_logbufsecs = 0;
  158. google::InitGoogleLogging("LidarMeasurement");
  159. google::SetStderrLogging(google::INFO);
  160. google::SetLogDestination(0, logPath);
  161. google::SetLogFilenameExtension("zxlog");
  162. google::InstallFailureSignalHandler();
  163. google::InstallFailureWriter(&shut_down_logging);
  164. FLAGS_colorlogtostderr = true; // Set log color
  165. FLAGS_logbufsecs = 0; // Set log output speed(s)
  166. FLAGS_max_log_size = 1024; // Set max log file size(GB)
  167. FLAGS_stop_logging_if_full_disk = true;
  168. }