main.cpp 6.0 KB

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