123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- /*
- * @Description: 车位分配算法模块,使用单例模式,接收外部请求并通过调用通信块接口发送反馈
- * @Author: yct
- * @Date: 2020-07-10 09:25:56
- * @LastEditTime: 2020-09-11 11:44:47
- * @LastEditors: yct
- */
- #ifndef PARKSPACE_ALLOCATOR_HH
- #define PARKSPACE_ALLOCATOR_HH
- #include "../tool/thread_pool.h"
- #include "../tool/singleton.h"
- #include "../error_code/error_code.h"
- #include "../communication/communication_message.h"
- #include "../message/parkspace_allocation_message.pb.h"
- #include "parkspace_db_manager.h"
- #include <ctime>
- class Parkspace_allocator : public Singleton<Parkspace_allocator>
- {
- friend class Singleton<Parkspace_allocator>;
- public:
- enum parkspace_allocator_status
- {
- eParkspace_allocator_unknown,
- eParkspace_allocator_normal,
- eParkspace_allocator_fault
- };
- private:
- // 父类的构造函数必须保护,子类的构造函数必须私有。
- Parkspace_allocator();
- public:
- // 必须关闭拷贝构造和赋值构造,只能通过 get_instance 函数来进行操作唯一的实例。
- Parkspace_allocator(const Parkspace_allocator &) = delete;
- Parkspace_allocator &operator=(const Parkspace_allocator &) = delete;
- ~Parkspace_allocator();
- public://API functions
- //初始化
- Error_manager parkspace_allocator_init(int threads_size, parkspace_proto::garage_config config);
- //反初始化
- Error_manager parkspace_allocator_uninit();
- //检查执行者的状态, 判断能否处理这条消息,
- Error_manager check_executer(Communication_message* p_msg);
- //处理消息的执行函数
- Error_manager execute_msg(Communication_message* p_msg);
- //判断是否为待机,如果已经准备好,则可以执行任务。
- bool is_ready();
- // 获取模块状态
- parkspace_allocator_status get_parkspace_allocator_status();
- // 检查车辆是否已存在,通常分配前调用
- bool check_car_existence(std::string license, message::Parkspace_allocation_status_msg status_msg);
- //获取所有车位状态
- message::Parkspace_allocation_status_msg get_status();
- //更新所有车位状态,现有状态将被全部覆盖,车位数量可能变动
- Error_manager update_parkspace_status(message::Parkspace_allocation_status_msg message);
- //更新某个车位状态,不修改车位数量,车位按编号从0开始
- Error_manager update_parkspace_status(int parkspace_id, message::Parkspace_info parkspace_info);
- //获取车位状态消息
- message::Parkspace_allocation_status_msg& get_parkspace_status_msg();
- std::mutex& get_status_mutex();
- private://execute_msg创建各线程进行处理
- //分配车位线程函数
- void execute_for_allocate(message::Car_info car_info, int terminal_id, message::Command_info command_info);
- //查询车位线程函数
- void execute_for_search(message::Car_info car_info, message::Command_info command_info);
- //释放车位线程函数
- void execute_for_release(message::Parkspace_info space_info, message::Command_info command_info);
- //强制更新车位信息线程函数
- void execute_for_force_update(message::Parkspace_info space_info, message::Command_info command_info);
- //确认分配车位线程函数
- void execute_for_confirm_alloc(message::Parkspace_info space_info, message::Command_info command_info);
- // 分配车位函数
- // input: 当前完整车位状态信息, 终端id,
- // output: 分配的车位下标
- int allocate_parkspace(message::Parkspace_allocation_status_msg parkspace_status, message::Car_info car_info, int terminal_id);
- private:
- parkspace_proto::garage_config m_config; //车位配置文件
- parkspace_allocator_status m_current_status; //分配器当前状态
- Thread_pool m_thread_pool; //执行多任务的线程池
- std::mutex m_mutex; //车位变动锁
- Parkspace_db_manager* mp_db_manager; //车位模块数据库管理句柄
- message::Parkspace_allocation_status_msg m_parkspace_status_msg;
- std::mutex m_status_mutex;
- };
- #endif // !PARKSPACE_ALLOCATOR_HH
|