exception_solver.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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::eProcess_manual_operation_msg:
  48. {
  49. message::Process_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. LOG(INFO)<<"接收到手动消息////////////////////////// : "<<msg.license();
  61. m_manual_msg_map[msg.license()]=msg;
  62. }
  63. case Communication_message::eEntrance_manual_operation_msg:
  64. {
  65. message::Entrance_manual_operation_msg msg;
  66. if(msg.ParseFromString(p_msg->get_message_buf())==false)
  67. {
  68. LOG(ERROR)<<" 手动消息解析失败 !!!";
  69. break;
  70. }
  71. LOG(INFO)<<" communication 接收到手动消息 : "<<msg.GetTypeName();
  72. switch (msg.process_type())
  73. {
  74. case message::eStoring:
  75. {
  76. Command_manager::get_instance_pointer()->enable_entrance(msg.terminal_id(),(Command_manager::Entrance_statu)msg.entrance_statu());
  77. break;
  78. }
  79. case message::ePicking:
  80. {
  81. Command_manager::get_instance_pointer()->enable_export(msg.terminal_id(),(Command_manager::Entrance_statu)msg.entrance_statu());
  82. break;
  83. }
  84. }
  85. }
  86. default:break;
  87. }
  88. return SUCCESS;
  89. }
  90. /*
  91. * 检测消息是否可被处理
  92. */
  93. Error_manager Exception_solver::check_msg(Communication_message* p_msg)
  94. {
  95. return SUCCESS;
  96. }
  97. /*
  98. * 心跳发送函数,重载
  99. */
  100. Error_manager Exception_solver::encapsulate_send_data()
  101. {
  102. return SUCCESS;
  103. }
  104. //检查消息是否可以被解析, 需要重载
  105. Error_manager Exception_solver::check_executer(Communication_message* p_msg)
  106. {
  107. return SUCCESS;
  108. }
  109. /*
  110. * 等待手动消息
  111. */
  112. Error_manager Exception_solver::waitfor_manual_operate_msg(Process_task* task,message::Process_manual_operation_msg& msg)
  113. {
  114. //清除旧的手动操作消息
  115. if(m_manual_msg_map.find(task->license()))
  116. m_manual_msg_map.erase(task->license());
  117. //监控新的手动操作消息
  118. while(task->is_canceled()==false)
  119. {
  120. if(m_manual_msg_map.find(task->license(),msg))
  121. {
  122. if(msg.has_base_info() && msg.has_license() && msg.has_operate_type() && msg.has_step_type())
  123. {
  124. if(msg.license()==task->license()&& msg.step_type()==task->current_step_type()) {
  125. LOG(INFO) << " 查询到: " << task->license()<<" 操作类型:"<<Process_operation_type_Name(msg.operate_type());
  126. return SUCCESS;
  127. }
  128. else
  129. {
  130. LOG(WARNING)<<"xxxx .. .. 非当前步骤的手动操作 车牌: "<<msg.license()<<", 操作步骤:"<<Step_type_Name(msg.step_type());
  131. m_manual_msg_map.erase(task->license());
  132. }
  133. }
  134. }
  135. }
  136. return TASK_CANCEL;
  137. }
  138. /*
  139. * 处理故障
  140. */
  141. Error_manager Exception_solver::solve_exception(Error_manager code,Process_task* task)
  142. {
  143. Error_manager code_operate;
  144. if(code.get_error_level()==CRITICAL_ERROR)
  145. {
  146. //四级故障,任务未完成, 且模块未能回退并恢复到初始待机状态
  147. //关闭出入口
  148. if(task->GetCategory()==message::eStoring) {
  149. Command_manager::get_instance_pointer()->enable_entrance(task->terminal_id(),Command_manager::Entrance_statu::Disable);
  150. LOG(ERROR)<<"关闭入口:"<<task->terminal_id()<<" 故障 !!!!!! 等待手动操作 ... ";
  151. message::Process_manual_operation_msg operate_msg;
  152. code_operate=waitfor_manual_operate_msg(task,operate_msg);
  153. if(code_operate!=SUCCESS)
  154. return code_operate;
  155. switch (operate_msg.operate_type())
  156. {
  157. //取消任务
  158. case message::eManual_cancel:
  159. {
  160. LOG(INFO) << "手动消息 ,车牌号 : " << task->license()<<" 操作类型:"<<Process_operation_type_Name(operate_msg.operate_type());
  161. task->Cancel();
  162. break;
  163. }
  164. case message::eManual_retry:{
  165. LOG(INFO) << "手动消息 ,车牌号 : " << task->license()<<" 操作类型:"<<Process_operation_type_Name(operate_msg.operate_type());
  166. break;
  167. }
  168. case message::eManual_ignore:
  169. {
  170. LOG(INFO) << "手动消息 ,车牌号 : " << task->license()<<" 操作类型:"<<Process_operation_type_Name(operate_msg.operate_type());
  171. task->next_step();
  172. break;
  173. }
  174. }
  175. }
  176. if(task->GetCategory()==message::ePicking) {
  177. Command_manager::get_instance_pointer()->enable_export(task->terminal_id(),Command_manager::Entrance_statu::Disable);
  178. LOG(ERROR)<<"关闭出口:"<<task->terminal_id()<<" 四级故障 !!!!!! 等待手动操作 ...";
  179. message::Process_manual_operation_msg operate_msg;
  180. code_operate=waitfor_manual_operate_msg(task,operate_msg);
  181. if(code_operate!=SUCCESS)
  182. return code_operate;
  183. }
  184. //系统急停
  185. //Command_manager::get_instance_pointer()->pause_system();
  186. }
  187. if(code.get_error_level()<=MAJOR_ERROR)
  188. {
  189. //三级故障, 任务未完成,已恢复到待机状态
  190. return SUCCESS;
  191. }
  192. switch(code.get_error_code())
  193. {
  194. case ERROR:
  195. {
  196. break;
  197. }
  198. }
  199. return SUCCESS;
  200. }