terminal_client.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // Created by zx on 2020/7/10.
  3. //
  4. //
  5. // Created by zx on 2020/7/3.
  6. //
  7. #include <unistd.h>
  8. #include <iostream>
  9. #include <nnxx/message>
  10. #include <nnxx/socket.h>
  11. #include <nnxx/bus.h>
  12. #include "terminal_message.pb.h"
  13. #include <thread>
  14. #include "threadSafeQueue.h"
  15. #include "Terminal_communication.h"
  16. #include "store_terminal.h"
  17. #include "pickup_terminal.h"
  18. #include "pathcreator.h"
  19. #include "process_message.pb.h"
  20. GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size)
  21. {
  22. time_t tt;
  23. time( &tt );
  24. tt = tt + 8*3600; // transform the time zone
  25. tm* t= gmtime( &tt );
  26. char buf[255]={0};
  27. sprintf(buf,"./%d%02d%02d-%02d%02d%02d-dump_client.txt",
  28. t->tm_year + 1900,
  29. t->tm_mon + 1,
  30. t->tm_mday,
  31. t->tm_hour,
  32. t->tm_min,
  33. t->tm_sec);
  34. FILE* tp_file=fopen(buf,"w");
  35. fprintf(tp_file,data,strlen(data));
  36. fclose(tp_file);
  37. }
  38. void init_glog()
  39. {
  40. time_t tt = time(0);//时间cuo
  41. struct tm* t = localtime(&tt);
  42. char strYear[255]={0};
  43. char strMonth[255]={0};
  44. char strDay[255]={0};
  45. sprintf(strYear,"%04d", t->tm_year+1900);
  46. sprintf(strMonth,"%02d", t->tm_mon+1);
  47. sprintf(strDay,"%02d", t->tm_mday);
  48. char buf[255]={0};
  49. getcwd(buf,255);
  50. char strdir[255]={0};
  51. sprintf(strdir,"%s/log_client/%s/%s/%s", buf,strYear,strMonth,strDay);
  52. PathCreator creator;
  53. creator.Mkdir(strdir);
  54. char logPath[255] = { 0 };
  55. sprintf(logPath, "%s/", strdir);
  56. FLAGS_max_log_size = 100;
  57. FLAGS_logbufsecs = 0;
  58. google::InitGoogleLogging("LidarMeasurement");
  59. google::SetStderrLogging(google::INFO);
  60. google::SetLogDestination(0, logPath);
  61. google::SetLogFilenameExtension("zxlog");
  62. google::InstallFailureSignalHandler();
  63. google::InstallFailureWriter(&shut_down_logging);
  64. FLAGS_colorlogtostderr = true; // Set log color
  65. FLAGS_logbufsecs = 0; // Set log output speed(s)
  66. FLAGS_max_log_size = 1024; // Set max log file size(GB)
  67. FLAGS_stop_logging_if_full_disk = true;
  68. }
  69. threadsafe_queue<message::Car_info> car_info_queue;
  70. void storing(int terminal_id)
  71. {
  72. while(1)
  73. {
  74. message::Car_info car_info;
  75. if(car_info_queue.try_pop(car_info))
  76. {
  77. LOG(INFO)<<" begin 执行停车:"<<car_info.license()<<" , 剩余待取:"<<store_command::p_command_queue->size();
  78. store_command command(car_info);
  79. Error_manager code=command.storing(terminal_id);
  80. if(code==SUCCESS)
  81. LOG(INFO)<<" completed 停车完成 :"<<car_info.license();
  82. else
  83. LOG(INFO)<<" 停车失败:"<<car_info.license()<<code.get_error_description();
  84. }
  85. usleep(1000*10);
  86. std::this_thread::yield();
  87. }
  88. }
  89. void picking(int terminal_id)
  90. {
  91. while(1)
  92. {
  93. if(store_command::p_command_queue->size()>0)
  94. {
  95. message::Car_info car_info;
  96. store_command::p_command_queue->wait_and_pop(car_info);
  97. LOG(WARNING) << " --------- begin 执行取车:" << car_info.license();
  98. pickup_terminal command(car_info);
  99. Error_manager code = command.pickup(terminal_id);
  100. if (code == SUCCESS)
  101. LOG(WARNING) << " --------- completed 取车完成 :" << car_info.license();
  102. else
  103. LOG(INFO) << " 取车失败:" << car_info.license() << code.get_error_description();
  104. }
  105. std::this_thread::yield();
  106. }
  107. }
  108. int main() {
  109. init_glog();
  110. Terminal_communication::get_instance_pointer()->communication_connect("tcp://192.168.3.14:30000");
  111. Terminal_communication::get_instance_pointer()->communication_run();
  112. usleep(2000*1000);
  113. const int n_input=6;
  114. const int n_output=6;
  115. std::thread** storing_thread_vec=0;
  116. std::thread** picking_thread_vec=0;
  117. storing_thread_vec=new std::thread*[n_input];
  118. picking_thread_vec=new std::thread*[n_output];
  119. for(int i=0;i<n_input;++i)
  120. {
  121. storing_thread_vec[i]=new std::thread(storing,i);
  122. }
  123. for(int i=0;i<n_output;++i)
  124. {
  125. picking_thread_vec[i]=new std::thread(picking,i);
  126. }
  127. int n = 0;
  128. bool run = true;
  129. Error_manager code;
  130. char c=0;
  131. int license_id = rand()%90000+10000;
  132. while (c!='q')
  133. {
  134. if(car_info_queue.size()>100)
  135. {
  136. usleep(1000*100);
  137. std::this_thread::yield();
  138. continue;
  139. }
  140. char C=rand()%10+'A';
  141. char license[255] = {0};
  142. sprintf(license, "鄂%c%d",C, license_id++);
  143. message::Car_info car_info;
  144. car_info.set_license(license);
  145. car_info.set_car_height(1.5);
  146. car_info.set_car_width(1.7);
  147. car_info_queue.push(car_info);
  148. n++;
  149. usleep(1000*100);
  150. std::this_thread::yield();
  151. LOG_EVERY_N(WARNING,100)<<"----队列指令:"<<car_info_queue.size()<<" 总停车数:"<<n;
  152. }
  153. return 0;
  154. }