123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- //
- // Created by zx on 2020/8/26.
- //
- #ifndef NNXX_TESTS_PROCESS_TASK_H
- #define NNXX_TESTS_PROCESS_TASK_H
- #include <error_code.h>
- #include <iostream>
- #include "thread_condition.h"
- #include "TaskQueue/BaseTask.h"
- #include "process_message.pb.h"
- #include "log_process.pb.h"
- #include <glog/logging.h>
- /*
- * 停取车流程基类, 包括任务创建, 状态消息发布线程启动, 任务取消等公共功能
- * 该类继承自tq::BaseTask类, 可放入 tq 线程池, 执行虚函数 Main
- * 派生出 停车任务类(StoreProcessTask)与取车任务类(PickupProcessTask)
- */
- class Process_task : public tq::BaseTask{
- public:
- Process_task(unsigned int command_id,message::Car_info car_info);
- virtual ~Process_task();
- /*
- * 获取当前任务相关属性
- */
- unsigned int terminal_id(){return m_terminor_id;}
- std::string license(){ return m_car_info.license();}
- message::Step_type current_step_type(){return m_current_step_type;}
- message::Step_statu current_step_statu(){return m_current_step_statu;}
- // 获取任务类型
- virtual message::Process_type get_process_type() const=0;
- /*
- * 取消任务
- */
- virtual void Cancel();
- /*
- * 任务是否取消
- */
- virtual bool is_canceled();
- /*
- * 控制流程到下一步
- */
- virtual Error_manager next_step(){
- LOG(ERROR)<<"process base virtual called";
- return ERROR;};
- virtual void updata_step_statu(message::Step_statu statu)=0;
- /*
- * 记录流程信息
- */
- virtual void to_proto(std::string proto_file);
- protected:
- virtual void Main();
- /*
- * 发布进度消息
- */
- static void publish_thread_func(Process_task* ptask);
- virtual void publish_step_status()=0;
- /*
- * 添加日志相关
- */
- class Log_container
- {
- public:
- Log_container(int severity,Process_task* process,bool condition=true){
- m_str="";
- m_process=process;
- m_severity=severity;
- m_condition=condition;
- }
- ~Log_container()
- {
- if(m_process&& m_str.length()>0)
- {
- m_process->add_log(m_severity,m_str);
- }
- }
- Log_container& operator<<(const std::string& data) {
- m_str+=data;
- return *this;
- }
- Log_container& operator<<(int data) {
- char buf[64]={0};
- sprintf(buf,"%d",data);
- m_str+=buf;
- return *this;
- }
- Log_container& operator<<(const message::Node_log& data) {
- if(m_process)
- m_process->add_log(m_severity,data);
- return *this;
- }
- Log_container& operator<<(const message::Manual_operation_log& data) {
- if(m_process)
- m_process->add_log(m_severity,data);
- return *this;
- }
- protected:
- Log_container(){}
- bool m_condition;
- Process_task* m_process;
- int m_severity;
- std::string m_str;
- };
- class Log_container_INFO :public Log_container
- {
- public:
- Log_container_INFO(Process_task* process,bool condition=true){
- m_process=process;
- m_severity=message::Log_data_Severity_INFO;
- m_condition=condition;
- }
- };
- class Log_container_WARNING :public Log_container
- {
- public:
- Log_container_WARNING(Process_task* process,bool condition=true){
- m_process=process;
- m_severity=message::Log_data_Severity_WARNING;
- m_condition=condition;
- }
- };
- class Log_container_ERROR :public Log_container
- {
- public:
- Log_container_ERROR(Process_task* process,bool condition=true){
- m_process=process;
- m_severity=message::Log_data_Severity_ERROR;
- m_condition=condition;
- }
- };
- //普通日志
- void add_log(int severity,std::string log);
- //以下是节点日志
- void add_log(int severity,const message::Node_log& data);
- void add_log(int severity,const message::Manual_operation_log& data);
- #define ALOG(severity) Log_container_##severity(this)
- #define ALOG_IF(severity,condition) Log_container_##severity(this,condition)
- protected:
- unsigned int m_terminor_id;
- message::Car_info m_car_info; //当前流程的车辆信息
- message::Step_type m_current_step_type; //当前流程正在哪一步
- message::Step_statu m_current_step_statu; //当前步骤所处的状态
- std::thread* m_publish_statu_thread; //广播状态线程
- Thread_condition m_publish_exit_condition; //发送的条件变量
- Thread_condition m_cancel_condition; //取消任务标志位
- std::mutex m_process_msg_lock; //状态消息锁
- message::Process_log m_process_log; //流程日志数据
- int m_step_index; //记录当前步骤的序号
- };
- #endif //NNXX_TESTS_PROCESS_TASK_H
|