exception_solver.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // Created by zx on 2020/8/20.
  3. //
  4. #include "exception_solver.h"
  5. #include "command_manager.h"
  6. #include "communication_message.h"
  7. Exception_solver::Exception_solver() {
  8. }
  9. Exception_solver::~Exception_solver() {
  10. }
  11. Error_manager Exception_solver::add_task_cancel_condition(std::string license,Process_task* task)
  12. {
  13. if(m_ptask_map.find(license)==true || task== nullptr)
  14. {
  15. return Error_manager(ERROR,MINOR_ERROR,"任务标识已存在,或者标识地址为null");
  16. }
  17. m_ptask_map[license]=task;
  18. return SUCCESS;
  19. }
  20. Error_manager Exception_solver::delete_task_cancel_condition(std::string license)
  21. {
  22. if(m_ptask_map.find(license)==true)
  23. {
  24. m_ptask_map.erase(license);
  25. return SUCCESS;
  26. }
  27. return Error_manager(ERROR,MINOR_ERROR,"任务取消标志不存在");
  28. }
  29. Error_manager Exception_solver::encapsulate_msg(Communication_message* message)
  30. {
  31. Error_manager code;
  32. //记录请求
  33. switch (message->get_message_type())
  34. {
  35. default:
  36. code= Communication_socket_base::encapsulate_msg(message);
  37. break;
  38. }
  39. return code;
  40. }
  41. Error_manager Exception_solver::execute_msg(Communication_message* p_msg)
  42. {
  43. if(p_msg== nullptr)
  44. return Error_manager(POINTER_IS_NULL,CRITICAL_ERROR,"dispatch response msg pointer is null");
  45. switch (p_msg->get_message_type())
  46. {
  47. case Communication_message::eManual_operation_msg:
  48. {
  49. message::Manual_operation_msg msg;
  50. if(msg.ParseFromString(p_msg->get_message_buf())==false)
  51. {
  52. LOG(ERROR)<<" 手动消息解析失败 !!!";
  53. break;
  54. }
  55. if(msg.has_license()==false)
  56. {
  57. LOG(ERROR)<<" 手动消息解析失败 !!!";
  58. break;
  59. }
  60. m_manual_msg_map[msg.license()]=msg;
  61. }
  62. default:break;
  63. }
  64. return SUCCESS;
  65. }
  66. /*
  67. * 检测消息是否可被处理
  68. */
  69. Error_manager Exception_solver::check_msg(Communication_message* p_msg)
  70. {
  71. //通过 p_msg->get_message_type() 和 p_msg->get_receiver() 判断这条消息是不是给我的.
  72. //子类重载时, 增加自己模块的判断逻辑, 以后再写.
  73. /*if ( (p_msg->get_message_type() == Communication_message::Message_type::eDispatch_response_msg
  74. ||p_msg->get_message_type() == Communication_message::Message_type::eDispatch_status_msg)
  75. && p_msg->get_receiver() == Communication_message::Communicator::eMain )
  76. {
  77. return Error_code::SUCCESS;
  78. }
  79. else
  80. {
  81. //认为接受人
  82. return Error_code::INVALID_MESSAGE;
  83. }*/
  84. }
  85. /*
  86. * 心跳发送函数,重载
  87. */
  88. Error_manager Exception_solver::encapsulate_send_data()
  89. {
  90. return SUCCESS;
  91. }
  92. //检查消息是否可以被解析, 需要重载
  93. Error_manager Exception_solver::check_executer(Communication_message* p_msg)
  94. {
  95. return SUCCESS;
  96. }
  97. /*
  98. * 等待手动消息
  99. */
  100. Error_manager Exception_solver::waitfor_manual_operate_msg(Process_task* task,message::Manual_operation_msg msg)
  101. {
  102. //清除旧的手动操作消息
  103. if(m_manual_msg_map.find(task->license()))
  104. m_manual_msg_map.erase(task->license());
  105. //监控新的手动操作消息
  106. while(task->is_canceled()==false)
  107. {
  108. if(m_manual_msg_map.find(task->license(),msg))
  109. {
  110. if(msg.has_base_info() && msg.has_license() && msg.has_operate_type() && msg.has_step_type())
  111. {
  112. return SUCCESS;
  113. }
  114. }
  115. }
  116. return TASK_CANCEL;
  117. }
  118. /*
  119. * 处理故障
  120. */
  121. Error_manager Exception_solver::solve_exception(Error_manager code,Process_task* task)
  122. {
  123. Error_manager code_operate;
  124. if(code.get_error_level()==CRITICAL_ERROR)
  125. {
  126. //四级故障,任务未完成, 且模块未能回退并恢复到初始待机状态
  127. //关闭出入口
  128. if(task->GetCategory()==message::eStoring) {
  129. Command_manager::get_instance_pointer()->enable_entrance(task->terminal_id(),Command_manager::Entrance_statu::Disable);
  130. LOG(ERROR)<<"关闭入口:"<<task->terminal_id()<<" 四级故障 !!!!!! 等待手动操作 ... ";
  131. message::Manual_operation_msg operate_msg;
  132. code_operate=waitfor_manual_operate_msg(task,operate_msg);
  133. if(code_operate!=SUCCESS)
  134. return code_operate;
  135. switch (operate_msg.operate_type())
  136. {
  137. //取消任务
  138. case message::eManual_cancel: {
  139. task->Cancel();
  140. return SUCCESS;
  141. }
  142. }
  143. }
  144. if(task->GetCategory()==message::ePicking) {
  145. Command_manager::get_instance_pointer()->enable_export(task->terminal_id(),Command_manager::Entrance_statu::Disable);
  146. LOG(ERROR)<<"关闭出口:"<<task->terminal_id()<<" 四级故障 !!!!!! 等待手动操作 ...";
  147. message::Manual_operation_msg operate_msg;
  148. code_operate=waitfor_manual_operate_msg(task,operate_msg);
  149. if(code_operate!=SUCCESS)
  150. return code_operate;
  151. }
  152. //系统急停
  153. //Command_manager::get_instance_pointer()->pause_system();
  154. }
  155. if(code.get_error_level()<=MAJOR_ERROR)
  156. {
  157. //三级故障, 任务未完成,已恢复到待机状态
  158. return SUCCESS;
  159. }
  160. switch(code.get_error_code())
  161. {
  162. case ERROR:
  163. {
  164. break;
  165. }
  166. }
  167. return SUCCESS;
  168. }