system_communicator.cpp 3.1 KB

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