parkspace_allocator.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * @Description: 车位分配算法模块,使用单例模式,接收外部请求并通过调用通信块接口发送反馈
  3. * @Author: yct
  4. * @Date: 2020-07-10 09:25:56
  5. * @LastEditTime: 2020-08-06 10:18:56
  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::database_config db_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. public://execute_msg创建各线程进行处理
  51. //分配车位线程函数
  52. void execute_for_allocate(message::Car_info car_info, int terminal_id,std::string command_key);
  53. //查询车位线程函数
  54. void execute_for_search(message::Car_info car_info,std::string command_key);
  55. //释放车位线程函数
  56. void execute_for_release(message::Parkspace_info space_info, std::string command_key);
  57. //强制更新车位信息线程函数
  58. void execute_for_force_update(message::Parkspace_info space_info, std::string command_key);
  59. //确认分配车位线程函数
  60. void execute_for_confirm_alloc(message::Parkspace_info space_info, std::string command_key);
  61. private:
  62. parkspace_allocator_status m_current_status; //分配器当前状态
  63. Thread_pool m_thread_pool; //执行多任务的线程池
  64. std::mutex m_mutex; //车位变动锁
  65. Parkspace_db_manager* mp_db_manager; //车位模块数据库管理句柄
  66. };
  67. #endif // !PARKSPACE_ALLOCATOR_HH