123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- //
- // Created by zx on 2020/8/26.
- //
- #include <google/protobuf/io/coded_stream.h>
- #include <google/protobuf/io/zero_copy_stream_impl.h>
- #include <google/protobuf/text_format.h>
- #include "process_task.h"
- #include "pathcreator.h"
- Process_task::Process_task(unsigned int terminal_id,message::Car_info car_info)
- :m_publish_statu_thread(nullptr)
- {
- m_terminor_id=terminal_id;
- m_car_info=car_info;
- m_step_index=0;
- }
- Process_task::~Process_task()
- {
- //退出线程
- m_publish_exit_condition.set_pass_ever(true);
- if(m_publish_statu_thread!= nullptr)
- {
- if(m_publish_statu_thread->joinable())
- {
- m_publish_statu_thread->join();
- }
- delete m_publish_statu_thread;
- m_publish_statu_thread=nullptr;
- }
- }
- /*
- * 取消任务
- */
- void Process_task::Cancel()
- {
- m_current_step_type= message::eBackComplete;
- m_cancel_condition.set_pass_ever(true);
- ALOG(WARNING)<<"取消当前任务";
- tq::BaseTask::Cancel();
- }
- /*
- * 发布进度
- */
- void Process_task::publish_thread_func(Process_task* ptask)
- {
- if(ptask)
- {
- //未收到退出信号
- while(false==ptask->m_publish_exit_condition.wait_for_ex(std::chrono::milliseconds(100))) {
- ptask->publish_step_status();
- }
- }
- }
- /*
- * 任务是否取消
- */
- bool Process_task::is_canceled()
- {
- return m_cancel_condition.wait_for_millisecond(1);
- }
- #include<fcntl.h>
- //普通日志
- void Process_task::add_log(int severity,std::string log)
- {
- message::Log_data log_data;
- log_data.set_log_severity(message::Log_data_Severity (severity));
- log_data.set_str_log(log);
- m_process_log.mutable_log_data()->Add(std::move(log_data));
- }
- //以下是节点日志
- void Process_task::add_log(int severity,const message::Parkspace_data& data)
- {
- message::Log_data log_data;
- log_data.set_log_severity(message::Log_data_Severity(severity));
- log_data.mutable_parkspace_log()->CopyFrom(data);
- m_process_log.mutable_log_data()->Add(std::move(log_data));
- }
- void Process_task::add_log(int severity,const message::Measure_data& data)
- {
- message::Log_data log_data;
- log_data.set_log_severity(message::Log_data_Severity(severity));
- log_data.mutable_measure_log()->CopyFrom(data);
- m_process_log.mutable_log_data()->Add(std::move(log_data));
- }
- void Process_task::add_log(int severity,const message::Dispatch_data& data)
- {
- message::Log_data log_data;
- log_data.set_log_severity(message::Log_data_Severity(severity));
- log_data.mutable_dispatch_log()->CopyFrom(data);
- m_process_log.mutable_log_data()->Add(std::move(log_data));
- }
- void Process_task::add_log(int severity,const message::Manual_operation_data& data)
- {
- message::Log_data log_data;
- log_data.set_log_severity(message::Log_data_Severity(severity));
- log_data.mutable_manual_operator_log()->CopyFrom(data);
- m_process_log.mutable_log_data()->Add(std::move(log_data));
- }
- void Process_task::Main()
- {
- //保存日志记录
- time_t tt;
- time( &tt );
- tt = tt + 8*3600; // transform the time zone
- tm* t= gmtime( &tt );
- char buf[255]={0};
- getcwd(buf,255);
- char strdir[255]={0};
- sprintf(strdir,"%s/process_log/%d/%02d/%02d",
- buf,t->tm_year + 1900,
- t->tm_mon + 1,
- t->tm_mday);
- PathCreator creator;
- creator.Mkdir(strdir);
- char filename[255]={0};
- std::string type=m_process_log.process_type()==message::eStoring?"停":"取";
- sprintf(filename,"%02d%02d%02d-%s-%s.proto",
- t->tm_hour,
- t->tm_min,
- t->tm_sec,
- m_car_info.license().c_str(),
- type.c_str());
- char logPath[255] = { 0 };
- sprintf(logPath, "%s/%s", strdir,filename);
- to_proto(logPath);
- /*for(int i=0;i<m_process_log.log_data_size();++i)
- {
- message::Log_data data=m_process_log.log_data(i);
- if(data.data_case()==message::Log_data::kStrLog)
- {
- std::cout<<" strlog:"<<data.str_log()<<std::endl;
- }
- }*/
- }
- void Process_task::to_proto(std::string proto_file)
- {
- /*using google::protobuf::io::FileInputStream;
- using google::protobuf::io::FileOutputStream;
- using google::protobuf::io::ZeroCopyInputStream;
- using google::protobuf::io::CodedInputStream;
- using google::protobuf::io::ZeroCopyOutputStream;
- using google::protobuf::io::CodedOutputStream;
- using google::protobuf::Message;
- int fd = open(proto_file.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
- FileOutputStream* output = new FileOutputStream(fd);
- CHECK(google::protobuf::TextFormat::Print(m_process_log, output));
- delete output;
- close(fd);*/
- int file=open(proto_file.c_str(),O_CREAT|O_TRUNC|O_RDWR,0644);
- if(-1!=file)
- {
- if(m_process_log.SerializeToFileDescriptor(file))
- return ;
- }
- LOG(WARNING)<<" process log ToProto failed :"<<proto_file;
- }
|