process_task.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. Process_task::Process_task(unsigned int terminal_id,message::Car_info car_info)
  11. :m_publish_statu_thread(nullptr)
  12. {
  13. m_terminor_id=terminal_id;
  14. m_car_info=car_info;
  15. m_step_index=0;
  16. }
  17. Process_task::~Process_task()
  18. {
  19. }
  20. Error_manager Process_task::init_task(const ::google::protobuf::Message& parameter)
  21. {
  22. ///创建状态发布线程
  23. if(m_publish_statu_thread== nullptr)
  24. {
  25. m_publish_exit_condition.reset(false, false, false);
  26. m_publish_statu_thread=new std::thread(publish_thread_func,this);
  27. }
  28. return SUCCESS;
  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::Node_log& data)
  71. {
  72. /*message::Log_data log_data;
  73. log_data.set_log_severity(message::Log_data_Severity(severity));
  74. log_data.mutable_node_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::Manual_operation_log& data)
  78. {
  79. /*message::Log_data log_data;
  80. log_data.set_log_severity(message::Log_data_Severity(severity));
  81. log_data.mutable_manual_operator_log()->CopyFrom(data);
  82. m_process_log.mutable_log_data()->Add(std::move(log_data));*/
  83. }
  84. void Process_task::Main()
  85. {
  86. //保存日志记录
  87. time_t tt;
  88. time( &tt );
  89. tt = tt + 8*3600; // transform the time zone
  90. tm* t= gmtime( &tt );
  91. char buf[255]={0};
  92. getcwd(buf,255);
  93. char strdir[255]={0};
  94. sprintf(strdir,"%s/process_log/%d/%02d/%02d",
  95. buf,t->tm_year + 1900,
  96. t->tm_mon + 1,
  97. t->tm_mday);
  98. PathCreator creator;
  99. creator.Mkdir(strdir);
  100. char filename[255]={0};
  101. std::string type=m_process_log.process_type()==message::eStoring?"停":"取";
  102. sprintf(filename,"%02d%02d%02d-%s-%s.proto",
  103. t->tm_hour,
  104. t->tm_min,
  105. t->tm_sec,
  106. m_car_info.license().c_str(),
  107. type.c_str());
  108. char logPath[255] = { 0 };
  109. sprintf(logPath, "%s/%s", strdir,filename);
  110. to_proto(logPath);
  111. /*for(int i=0;i<m_process_log.log_data_size();++i)
  112. {
  113. message::Log_data data=m_process_log.log_data(i);
  114. if(data.data_case()==message::Log_data::kStrLog)
  115. {
  116. std::cout<<" strlog:"<<data.str_log()<<std::endl;
  117. }
  118. }*/
  119. }
  120. void Process_task::to_proto(std::string proto_file)
  121. {
  122. /*using google::protobuf::io::FileInputStream;
  123. using google::protobuf::io::FileOutputStream;
  124. using google::protobuf::io::ZeroCopyInputStream;
  125. using google::protobuf::io::CodedInputStream;
  126. using google::protobuf::io::ZeroCopyOutputStream;
  127. using google::protobuf::io::CodedOutputStream;
  128. using google::protobuf::Message;
  129. int fd = open(proto_file.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
  130. FileOutputStream* output = new FileOutputStream(fd);
  131. CHECK(google::protobuf::TextFormat::Print(m_process_log, output));
  132. delete output;
  133. close(fd);*/
  134. int file=open(proto_file.c_str(),O_CREAT|O_TRUNC|O_RDWR,0644);
  135. if(-1!=file)
  136. {
  137. if(m_process_log.SerializeToFileDescriptor(file))
  138. return ;
  139. }
  140. LOG(WARNING)<<" process log ToProto failed :"<<proto_file;
  141. }