parkspace_allocator.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * @Description: 车位分配算法模块,使用单例模式,接收外部请求并通过调用通信块接口发送反馈
  3. * @Author: yct
  4. * @Date: 2020-07-10 09:25:56
  5. * @LastEditTime: 2020-09-11 11:44:47
  6. * @LastEditors: yct
  7. */
  8. #ifndef PARKSPACE_ALLOCATOR_HH
  9. #define PARKSPACE_ALLOCATOR_HH
  10. #include "../tool/thread_pool.h"
  11. #include "../tool/singleton.h"
  12. #include "../error_code/error_code.h"
  13. #include "../communication/communication_message.h"
  14. #include "../message/parkspace_allocation_message.pb.h"
  15. #include "parkspace_db_manager.h"
  16. #include <ctime>
  17. class Parkspace_allocator : public Singleton<Parkspace_allocator>
  18. {
  19. friend class Singleton<Parkspace_allocator>;
  20. public:
  21. enum parkspace_allocator_status
  22. {
  23. eParkspace_allocator_unknown,
  24. eParkspace_allocator_normal,
  25. eParkspace_allocator_fault
  26. };
  27. private:
  28. // 父类的构造函数必须保护,子类的构造函数必须私有。
  29. Parkspace_allocator();
  30. public:
  31. // 必须关闭拷贝构造和赋值构造,只能通过 get_instance 函数来进行操作唯一的实例。
  32. Parkspace_allocator(const Parkspace_allocator &) = delete;
  33. Parkspace_allocator &operator=(const Parkspace_allocator &) = delete;
  34. ~Parkspace_allocator();
  35. public://API functions
  36. //初始化
  37. Error_manager parkspace_allocator_init(int threads_size, parkspace_proto::garage_config config);
  38. //反初始化
  39. Error_manager parkspace_allocator_uninit();
  40. //检查执行者的状态, 判断能否处理这条消息,
  41. Error_manager check_executer(Communication_message* p_msg);
  42. //处理消息的执行函数
  43. Error_manager execute_msg(Communication_message* p_msg);
  44. //判断是否为待机,如果已经准备好,则可以执行任务。
  45. bool is_ready();
  46. // 获取模块状态
  47. parkspace_allocator_status get_parkspace_allocator_status();
  48. // 检查车辆是否已存在,通常分配前调用
  49. bool check_car_existence(std::string license, message::Parkspace_allocation_status_msg status_msg);
  50. //获取所有车位状态
  51. message::Parkspace_allocation_status_msg get_status();
  52. //更新所有车位状态,现有状态将被全部覆盖,车位数量可能变动
  53. Error_manager update_parkspace_status(message::Parkspace_allocation_status_msg message);
  54. //更新某个车位状态,不修改车位数量,车位按编号从0开始
  55. Error_manager update_parkspace_status(int parkspace_id, message::Parkspace_info parkspace_info);
  56. //获取车位状态消息
  57. message::Parkspace_allocation_status_msg& get_parkspace_status_msg();
  58. std::mutex& get_status_mutex();
  59. private://execute_msg创建各线程进行处理
  60. //分配车位线程函数
  61. void execute_for_allocate(message::Car_info car_info, int terminal_id, message::Command_info command_info);
  62. //查询车位线程函数
  63. void execute_for_search(message::Car_info car_info, message::Command_info command_info);
  64. //释放车位线程函数
  65. void execute_for_release(message::Parkspace_info space_info, message::Command_info command_info);
  66. //强制更新车位信息线程函数
  67. void execute_for_force_update(message::Parkspace_info space_info, message::Command_info command_info);
  68. //确认分配车位线程函数
  69. void execute_for_confirm_alloc(message::Parkspace_info space_info, message::Command_info command_info);
  70. // 分配车位函数
  71. // input: 当前完整车位状态信息, 终端id,
  72. // output: 分配的车位下标
  73. int allocate_parkspace(message::Parkspace_allocation_status_msg parkspace_status, message::Car_info car_info, int terminal_id);
  74. private:
  75. parkspace_proto::garage_config m_config; //车位配置文件
  76. parkspace_allocator_status m_current_status; //分配器当前状态
  77. Thread_pool m_thread_pool; //执行多任务的线程池
  78. std::mutex m_mutex; //车位变动锁
  79. Parkspace_db_manager* mp_db_manager; //车位模块数据库管理句柄
  80. message::Parkspace_allocation_status_msg m_parkspace_status_msg;
  81. std::mutex m_status_mutex;
  82. };
  83. #endif // !PARKSPACE_ALLOCATOR_HH