process_task.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // Created by zx on 2020/8/26.
  3. //
  4. #include <google/protobuf/io/coded_stream.h>
  5. #include <google/protobuf/io/zero_copy_stream_impl.h>
  6. #include <google/protobuf/text_format.h>
  7. #include "process_task.h"
  8. #include "pathcreator.h"
  9. Process_task::Process_task(unsigned int terminal_id,message::Car_info car_info)
  10. :m_publish_statu_thread(nullptr)
  11. {
  12. m_terminor_id=terminal_id;
  13. m_car_info=car_info;
  14. m_step_index=0;
  15. }
  16. Process_task::~Process_task()
  17. {
  18. //退出线程
  19. m_publish_exit_condition.set_pass_ever(true);
  20. if(m_publish_statu_thread!= nullptr)
  21. {
  22. if(m_publish_statu_thread->joinable())
  23. {
  24. m_publish_statu_thread->join();
  25. }
  26. delete m_publish_statu_thread;
  27. m_publish_statu_thread=nullptr;
  28. }
  29. }
  30. /*
  31. * 取消任务
  32. */
  33. void Process_task::Cancel()
  34. {
  35. m_current_step_type= message::eBackComplete;
  36. m_cancel_condition.set_pass_ever(true);
  37. ALOG(WARNING)<<"取消当前任务";
  38. tq::BaseTask::Cancel();
  39. }
  40. /*
  41. * 发布进度
  42. */
  43. void Process_task::publish_thread_func(Process_task* ptask)
  44. {
  45. if(ptask)
  46. {
  47. //未收到退出信号
  48. while(false==ptask->m_publish_exit_condition.wait_for_ex(std::chrono::milliseconds(100))) {
  49. ptask->publish_step_status();
  50. }
  51. }
  52. }
  53. /*
  54. * 任务是否取消
  55. */
  56. bool Process_task::is_canceled()
  57. {
  58. return m_cancel_condition.wait_for_millisecond(1);
  59. }
  60. #include<fcntl.h>
  61. //普通日志
  62. void Process_task::add_log(int severity,std::string log)
  63. {
  64. message::Log_data log_data;
  65. log_data.set_log_severity(message::Log_data_Severity (severity));
  66. log_data.set_str_log(log);
  67. m_process_log.mutable_log_data()->Add(std::move(log_data));
  68. }
  69. //以下是节点日志
  70. void Process_task::add_log(int severity,const message::Parkspace_data& data)
  71. {
  72. message::Log_data log_data;
  73. log_data.set_log_severity(message::Log_data_Severity(severity));
  74. log_data.mutable_parkspace_log()->CopyFrom(data);
  75. m_process_log.mutable_log_data()->Add(std::move(log_data));
  76. }
  77. void Process_task::add_log(int severity,const message::Measure_data& data)
  78. {
  79. message::Log_data log_data;
  80. log_data.set_log_severity(message::Log_data_Severity(severity));
  81. log_data.mutable_measure_log()->CopyFrom(data);
  82. m_process_log.mutable_log_data()->Add(std::move(log_data));
  83. }
  84. void Process_task::add_log(int severity,const message::Dispatch_data& data)
  85. {
  86. message::Log_data log_data;
  87. log_data.set_log_severity(message::Log_data_Severity(severity));
  88. log_data.mutable_dispatch_log()->CopyFrom(data);
  89. m_process_log.mutable_log_data()->Add(std::move(log_data));
  90. }
  91. void Process_task::add_log(int severity,const message::Manual_operation_data& data)
  92. {
  93. message::Log_data log_data;
  94. log_data.set_log_severity(message::Log_data_Severity(severity));
  95. log_data.mutable_manual_operator_log()->CopyFrom(data);
  96. m_process_log.mutable_log_data()->Add(std::move(log_data));
  97. }
  98. void Process_task::Main()
  99. {
  100. //保存日志记录
  101. time_t tt;
  102. time( &tt );
  103. tt = tt + 8*3600; // transform the time zone
  104. tm* t= gmtime( &tt );
  105. char buf[255]={0};
  106. getcwd(buf,255);
  107. char strdir[255]={0};
  108. sprintf(strdir,"%s/process_log/%d/%02d/%02d",
  109. buf,t->tm_year + 1900,
  110. t->tm_mon + 1,
  111. t->tm_mday);
  112. PathCreator creator;
  113. creator.Mkdir(strdir);
  114. char filename[255]={0};
  115. std::string type=m_process_log.process_type()==message::eStoring?"停":"取";
  116. sprintf(filename,"%02d%02d%02d-%s-%s.proto",
  117. t->tm_hour,
  118. t->tm_min,
  119. t->tm_sec,
  120. m_car_info.license().c_str(),
  121. type.c_str());
  122. char logPath[255] = { 0 };
  123. sprintf(logPath, "%s/%s", strdir,filename);
  124. to_proto(logPath);
  125. /*for(int i=0;i<m_process_log.log_data_size();++i)
  126. {
  127. message::Log_data data=m_process_log.log_data(i);
  128. if(data.data_case()==message::Log_data::kStrLog)
  129. {
  130. std::cout<<" strlog:"<<data.str_log()<<std::endl;
  131. }
  132. }*/
  133. }
  134. void Process_task::to_proto(std::string proto_file)
  135. {
  136. /*using google::protobuf::io::FileInputStream;
  137. using google::protobuf::io::FileOutputStream;
  138. using google::protobuf::io::ZeroCopyInputStream;
  139. using google::protobuf::io::CodedInputStream;
  140. using google::protobuf::io::ZeroCopyOutputStream;
  141. using google::protobuf::io::CodedOutputStream;
  142. using google::protobuf::Message;
  143. int fd = open(proto_file.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
  144. FileOutputStream* output = new FileOutputStream(fd);
  145. CHECK(google::protobuf::TextFormat::Print(m_process_log, output));
  146. delete output;
  147. close(fd);*/
  148. int file=open(proto_file.c_str(),O_CREAT|O_TRUNC|O_RDWR,0644);
  149. if(-1!=file)
  150. {
  151. if(m_process_log.SerializeToFileDescriptor(file))
  152. return ;
  153. }
  154. LOG(WARNING)<<" process log ToProto failed :"<<proto_file;
  155. }