system_communicator.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_process_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_process_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. Error_manager System_communicator::post_central_statu(message::Central_controller_statu_msg& msg)
  33. {
  34. Communication_message message;
  35. message.reset(msg.base_info(),msg.SerializeAsString());
  36. return encapsulate_msg(&message);
  37. }
  38. /*
  39. * 发送消息函数
  40. */
  41. Error_manager System_communicator::encapsulate_msg(Communication_message* message)
  42. {
  43. return Communication_socket_base::encapsulate_msg(message);
  44. }
  45. /*
  46. * 检测消息是否合法
  47. */
  48. Error_manager System_communicator::check_msg(Communication_message* p_msg)
  49. {
  50. return SUCCESS;
  51. }
  52. //检查消息是否可以被处理, 需要重载
  53. Error_manager System_communicator::check_executer(Communication_message* p_msg)
  54. {
  55. return SUCCESS;
  56. }
  57. /*
  58. * 处理接收到的消息
  59. */
  60. Error_manager System_communicator::execute_msg(Communication_message* p_msg)
  61. {
  62. /*
  63. * 接收终端指令, 生成流程
  64. */
  65. Error_manager code;
  66. switch(p_msg->get_message_type())
  67. {
  68. case Communication_message::eStore_command_request_msg:
  69. {
  70. if(Command_manager::get_instance_pointer()!= nullptr)
  71. {
  72. message::Store_command_request_msg request;
  73. if(request.ParseFromString(p_msg->get_message_buf())==false)
  74. {
  75. //严重错误
  76. return Error_manager(INVALID_MESSAGE,MAJOR_ERROR,"停车请求消息解析失败");
  77. }
  78. message::Store_command_response_msg response;
  79. code=Command_manager::get_instance_pointer()->execute_store_command(request,response);
  80. LOG_IF(ERROR,code!=SUCCESS)<<code.get_error_description();
  81. Communication_message send_response;
  82. send_response.reset(response.base_info(),response.SerializeAsString());
  83. //发送反馈
  84. encapsulate_msg(&send_response);
  85. }
  86. break;
  87. }
  88. case Communication_message::ePickup_command_request_msg:
  89. {
  90. if(Command_manager::get_instance_pointer()!= nullptr)
  91. {
  92. message::Pickup_command_request_msg request;
  93. if(request.ParseFromString(p_msg->get_message_buf())==false)
  94. {
  95. //严重错误
  96. return Error_manager(INVALID_MESSAGE,MAJOR_ERROR,"取车请求消息解析失败");
  97. }
  98. message::Pickup_command_response_msg response;
  99. //调用请求回调,无需判断返回值,错误信息保存在response中
  100. code=Command_manager::get_instance_pointer()->execute_pickup_command(request,response);
  101. LOG_IF(ERROR,code!=SUCCESS)<<code.get_error_description();
  102. Communication_message send_response;
  103. send_response.reset(response.base_info(),response.SerializeAsString());
  104. //发送反馈
  105. encapsulate_msg(&send_response);
  106. }
  107. break;
  108. }
  109. default:break;
  110. }
  111. return SUCCESS;
  112. }