system_communicator.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // Created by huli on 2020/6/28.
  3. //
  4. #include "system_communicator.h"
  5. #include "command_manager.h"
  6. System_communicator::System_communicator()
  7. {
  8. }
  9. System_communicator::~System_communicator()
  10. {
  11. }
  12. //定时封装发送消息, 一般为心跳和状态信息, 需要子类重载
  13. Error_manager System_communicator::encapsulate_send_data()
  14. {
  15. return Error_code::SUCCESS;
  16. }
  17. //发送停车流程进度
  18. Error_manager System_communicator::post_entrance_statu(message::Storing_process_statu_msg& msg)
  19. {
  20. Communication_message message;
  21. message.reset(msg.base_info(),msg.SerializeAsString());
  22. return encapsulate_msg(&message);
  23. }
  24. //发送取车流程进度
  25. Error_manager System_communicator::post_entrance_statu(message::Picking_process_statu_msg& msg)
  26. {
  27. Communication_message message;
  28. message.reset(msg.base_info(),msg.SerializeAsString());
  29. return encapsulate_msg(&message);
  30. }
  31. /*
  32. * 发送消息函数
  33. */
  34. Error_manager System_communicator::encapsulate_msg(Communication_message* message)
  35. {
  36. return Communication_socket_base::encapsulate_msg(message);
  37. }
  38. /*
  39. * 检测消息是否合法
  40. */
  41. Error_manager System_communicator::check_msg(Communication_message* p_msg)
  42. {
  43. return SUCCESS;
  44. }
  45. //检查消息是否可以被处理, 需要重载
  46. Error_manager System_communicator::check_executer(Communication_message* p_msg)
  47. {
  48. return SUCCESS;
  49. }
  50. /*
  51. * 处理接收到的消息
  52. */
  53. Error_manager System_communicator::execute_msg(Communication_message* p_msg)
  54. {
  55. /*
  56. * 接收终端指令, 生成流程
  57. */
  58. Error_manager code;
  59. switch(p_msg->get_message_type())
  60. {
  61. case Communication_message::eStore_command_request_msg:
  62. {
  63. if(Command_manager::get_instance_pointer()!= nullptr)
  64. {
  65. message::Store_command_request_msg request;
  66. if(request.ParseFromString(p_msg->get_message_buf())==false)
  67. {
  68. //严重错误
  69. return Error_manager(INVALID_MESSAGE,MAJOR_ERROR,"停车请求消息解析失败");
  70. }
  71. message::Store_command_response_msg response;
  72. code=Command_manager::get_instance_pointer()->execute_store_command(request,response);
  73. LOG_IF(ERROR,code!=SUCCESS)<<code.get_error_description();
  74. Communication_message send_response;
  75. send_response.reset(response.base_info(),response.SerializeAsString());
  76. //发送反馈
  77. encapsulate_msg(&send_response);
  78. }
  79. break;
  80. }
  81. case Communication_message::ePickup_command_request_msg:
  82. {
  83. if(Command_manager::get_instance_pointer()!= nullptr)
  84. {
  85. message::Pickup_command_request_msg request;
  86. if(request.ParseFromString(p_msg->get_message_buf())==false)
  87. {
  88. //严重错误
  89. return Error_manager(INVALID_MESSAGE,MAJOR_ERROR,"取车请求消息解析失败");
  90. }
  91. message::Pickup_command_response_msg response;
  92. //调用请求回调,无需判断返回值,错误信息保存在response中
  93. code=Command_manager::get_instance_pointer()->execute_pickup_command(request,response);
  94. LOG_IF(ERROR,code!=SUCCESS)<<code.get_error_description();
  95. Communication_message send_response;
  96. send_response.reset(response.base_info(),response.SerializeAsString());
  97. //发送反馈
  98. encapsulate_msg(&send_response);
  99. }
  100. break;
  101. }
  102. default:break;
  103. }
  104. return SUCCESS;
  105. }