process_task.cpp 4.4 KB

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