123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- //
- // Created by zx on 2020/7/10.
- //
- //
- // Created by zx on 2020/7/3.
- //
- #include <unistd.h>
- #include <iostream>
- #include <nnxx/message>
- #include <nnxx/socket.h>
- #include <nnxx/bus.h>
- #include "terminal_message.pb.h"
- #include <thread>
- #include "threadSafeQueue.h"
- #include "Terminal_communication.h"
- #include "store_terminal.h"
- #include "pickup_terminal.h"
- #include "pathcreator.h"
- #include "process_message.pb.h"
- 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_client.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_client/%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;
- }
- threadsafe_queue<message::Car_info> car_info_queue;
- void storing(int terminal_id)
- {
- while(1)
- {
- message::Car_info car_info;
- if(car_info_queue.try_pop(car_info))
- {
- LOG(INFO)<<" begin 执行停车:"<<car_info.license()<<" , 剩余待取:"<<store_command::p_command_queue->size();
- store_command command(car_info);
- Error_manager code=command.storing(terminal_id);
- if(code==SUCCESS)
- LOG(INFO)<<" completed 停车完成 :"<<car_info.license();
- else
- LOG(INFO)<<" 停车失败:"<<car_info.license()<<code.get_error_description();
- }
- usleep(1000*10);
- std::this_thread::yield();
- }
- }
- void picking(int terminal_id)
- {
- while(1)
- {
- if(store_command::p_command_queue->size()>0)
- {
- message::Car_info car_info;
- store_command::p_command_queue->wait_and_pop(car_info);
- LOG(WARNING) << " --------- begin 执行取车:" << car_info.license();
- pickup_terminal command(car_info);
- Error_manager code = command.pickup(terminal_id);
- if (code == SUCCESS)
- LOG(WARNING) << " --------- completed 取车完成 :" << car_info.license();
- else
- LOG(INFO) << " 取车失败:" << car_info.license() << code.get_error_description();
- }
- std::this_thread::yield();
- }
- }
- int main() {
- init_glog();
- Terminal_communication::get_instance_pointer()->communication_connect("tcp://192.168.3.14:30000");
- Terminal_communication::get_instance_pointer()->communication_run();
- usleep(2000*1000);
- const int n_input=6;
- const int n_output=6;
- std::thread** storing_thread_vec=0;
- std::thread** picking_thread_vec=0;
- storing_thread_vec=new std::thread*[n_input];
- picking_thread_vec=new std::thread*[n_output];
- for(int i=0;i<n_input;++i)
- {
- storing_thread_vec[i]=new std::thread(storing,i);
- }
- for(int i=0;i<n_output;++i)
- {
- picking_thread_vec[i]=new std::thread(picking,i);
- }
- int n = 0;
- bool run = true;
- Error_manager code;
- char c=0;
- int license_id = rand()%90000+10000;
- while (c!='q')
- {
- if(car_info_queue.size()>100)
- {
- usleep(1000*100);
- std::this_thread::yield();
- continue;
- }
- char C=rand()%10+'A';
- char license[255] = {0};
- sprintf(license, "鄂%c%d",C, license_id++);
- message::Car_info car_info;
- car_info.set_license(license);
- car_info.set_car_height(1.5);
- car_info.set_car_width(1.7);
- car_info_queue.push(car_info);
- n++;
- usleep(1000*100);
- std::this_thread::yield();
- LOG_EVERY_N(WARNING,100)<<"----队列指令:"<<car_info_queue.size()<<" 总停车数:"<<n;
- }
- return 0;
- }
|