process_task.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // Created by zx on 2020/8/26.
  3. //
  4. #ifndef NNXX_TESTS_PROCESS_TASK_H
  5. #define NNXX_TESTS_PROCESS_TASK_H
  6. #include <error_code.h>
  7. #include <iostream>
  8. #include "thread_condition.h"
  9. #include "TaskQueue/BaseTask.h"
  10. #include "process_message.pb.h"
  11. #include "log_process.pb.h"
  12. #include <glog/logging.h>
  13. /*
  14. * 停取车流程基类, 包括任务创建, 状态消息发布线程启动, 任务取消等公共功能
  15. * 该类继承自tq::BaseTask类, 可放入 tq 线程池, 执行虚函数 Main
  16. * 派生出 停车任务类(StoreProcessTask)与取车任务类(PickupProcessTask)
  17. */
  18. class Process_task : public tq::BaseTask{
  19. public:
  20. Process_task(unsigned int command_id,message::Car_info car_info);
  21. virtual ~Process_task();
  22. /*
  23. * 获取当前任务相关属性
  24. */
  25. unsigned int terminal_id(){return m_terminor_id;}
  26. std::string license(){ return m_car_info.license();}
  27. message::Step_type current_step_type(){return m_current_step_type;}
  28. message::Step_statu current_step_statu(){return m_current_step_statu;}
  29. // 获取任务类型
  30. virtual message::Process_type get_process_type() const=0;
  31. /*
  32. * 取消任务
  33. */
  34. virtual void Cancel();
  35. /*
  36. * 任务是否取消
  37. */
  38. virtual bool is_canceled();
  39. /*
  40. * 控制流程到下一步
  41. */
  42. virtual Error_manager next_step(){
  43. LOG(ERROR)<<"process base virtual called";
  44. return ERROR;};
  45. virtual void updata_step_statu(message::Step_statu statu)=0;
  46. /*
  47. * 记录流程信息
  48. */
  49. virtual void to_proto(std::string proto_file);
  50. protected:
  51. virtual void Main();
  52. /*
  53. * 发布进度消息
  54. */
  55. static void publish_thread_func(Process_task* ptask);
  56. virtual void publish_step_status()=0;
  57. /*
  58. * 添加日志相关
  59. */
  60. class Log_container
  61. {
  62. public:
  63. Log_container(int severity,Process_task* process,bool condition=true){
  64. m_str="";
  65. m_process=process;
  66. m_severity=severity;
  67. m_condition=condition;
  68. }
  69. ~Log_container()
  70. {
  71. if(m_process&& m_str.length()>0)
  72. {
  73. m_process->add_log(m_severity,m_str);
  74. }
  75. }
  76. Log_container& operator<<(const std::string& data) {
  77. m_str+=data;
  78. return *this;
  79. }
  80. Log_container& operator<<(int data) {
  81. char buf[64]={0};
  82. sprintf(buf,"%d",data);
  83. m_str+=buf;
  84. return *this;
  85. }
  86. Log_container& operator<<(const message::Node_log& data) {
  87. if(m_process)
  88. m_process->add_log(m_severity,data);
  89. return *this;
  90. }
  91. Log_container& operator<<(const message::Manual_operation_log& data) {
  92. if(m_process)
  93. m_process->add_log(m_severity,data);
  94. return *this;
  95. }
  96. protected:
  97. Log_container(){}
  98. bool m_condition;
  99. Process_task* m_process;
  100. int m_severity;
  101. std::string m_str;
  102. };
  103. class Log_container_INFO :public Log_container
  104. {
  105. public:
  106. Log_container_INFO(Process_task* process,bool condition=true){
  107. m_process=process;
  108. m_severity=message::Log_data_Severity_INFO;
  109. m_condition=condition;
  110. }
  111. };
  112. class Log_container_WARNING :public Log_container
  113. {
  114. public:
  115. Log_container_WARNING(Process_task* process,bool condition=true){
  116. m_process=process;
  117. m_severity=message::Log_data_Severity_WARNING;
  118. m_condition=condition;
  119. }
  120. };
  121. class Log_container_ERROR :public Log_container
  122. {
  123. public:
  124. Log_container_ERROR(Process_task* process,bool condition=true){
  125. m_process=process;
  126. m_severity=message::Log_data_Severity_ERROR;
  127. m_condition=condition;
  128. }
  129. };
  130. //普通日志
  131. void add_log(int severity,std::string log);
  132. //以下是节点日志
  133. void add_log(int severity,const message::Node_log& data);
  134. void add_log(int severity,const message::Manual_operation_log& data);
  135. #define ALOG(severity) Log_container_##severity(this)
  136. #define ALOG_IF(severity,condition) Log_container_##severity(this,condition)
  137. protected:
  138. unsigned int m_terminor_id;
  139. message::Car_info m_car_info; //当前流程的车辆信息
  140. message::Step_type m_current_step_type; //当前流程正在哪一步
  141. message::Step_statu m_current_step_statu; //当前步骤所处的状态
  142. std::thread* m_publish_statu_thread; //广播状态线程
  143. Thread_condition m_publish_exit_condition; //发送的条件变量
  144. Thread_condition m_cancel_condition; //取消任务标志位
  145. std::mutex m_process_msg_lock; //状态消息锁
  146. message::Process_log m_process_log; //流程日志数据
  147. int m_step_index; //记录当前步骤的序号
  148. };
  149. #endif //NNXX_TESTS_PROCESS_TASK_H