123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584 |
- /*
- * @Description: 车位分配算法模块,使用单例模式,接收外部请求并通过调用通信块接口发送反馈
- * @Author: yct
- * @Date: 2020-07-10 11:02:40
- * @LastEditTime: 2020-07-22 19:21:03
- * @LastEditors: yct
- */
- #include "parkspace_allocator.h"
- #include "parkspace_allocation_communicator.h"
- Parkspace_allocator::Parkspace_allocator()
- {
- }
- Parkspace_allocator::~Parkspace_allocator()
- {
- parkspace_allocator_uninit();
- }
- //初始化
- Error_manager Parkspace_allocator::parkspace_allocator_init(int threads_size, parkspace_proto::database_config db_config)
- {
- // 初始化db连接
- mp_db_manager = Parkspace_db_manager::get_instance_pointer();
- if(mp_db_manager == nullptr)
- {
- return POINTER_IS_NULL;
- }
- Error_manager ec = mp_db_manager->Parkspace_db_manager_init(db_config);
- if(ec != SUCCESS)
- {
- return ec;
- }
- // 初始化任务处理线程池
- m_thread_pool.thread_pool_init(threads_size);
- // 读数据库车位数据,初始化车位管理
- message::Parkspace_allocation_status_msg all_parkspace_info;
- ec = mp_db_manager->get_all_parkspace_info(all_parkspace_info);
- if(ec != SUCCESS)
- {
- return ec;
- }
- Parkspace_allocation_communicator::get_instance_references().update_parkspace_status(all_parkspace_info);
- m_current_status = eParkspace_allocator_normal;
- return Error_code::SUCCESS;
- }
- //反初始化
- Error_manager Parkspace_allocator::parkspace_allocator_uninit()
- {
- m_thread_pool.thread_pool_uninit();
- mp_db_manager->Parkspace_db_manager_uninit();
- m_current_status = eParkspace_allocator_unknown;
- return Error_code::SUCCESS;
- }
- //检查执行者的状态, 判断能否处理这条消息,
- Error_manager Parkspace_allocator::check_executer(Communication_message* p_msg)
- {
- if ( p_msg == NULL )
- {
- return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
- " POINTER IS NULL ");
- }
- Error_manager t_error = SUCCESS;
- return t_error;
- }
- //处理消息的执行函数
- Error_manager Parkspace_allocator::execute_msg(Communication_message* p_msg)
- {
- if ( p_msg == nullptr )
- {
- return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
- " POINTER IS NULL ");
- }
- switch ( p_msg->get_message_type() )
- {
- ///车位分配请求消息
- case Communication_message::eParkspace_allocation_request_msg:
- {
- message::Parkspace_allocation_request_msg request;
- bool result = request.ParseFromString(p_msg->get_message_buf());
- LOG(INFO)<<"allocation request, car license: "<<request.car_info().license();
- if(!result)
- {
- return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_PARSE_ERROR, Error_level::MINOR_ERROR,
- " message::Parkspace_allocation_request_msg ParseFromString error ");
- }
- //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
- m_thread_pool.enqueue(&Parkspace_allocator::execute_for_allocate, this,
- request.car_info(), request.terminal_id(), request.command_id());
- return SUCCESS;
- }
- // 车位查询请求消息
- case Communication_message::eParkspace_search_request_msg:
- {
- message::Parkspace_search_request_msg request;
- bool result = request.ParseFromString(p_msg->get_message_buf());
- LOG(INFO)<<"search request, car license: "<<request.car_info().license();
- if(!result)
- {
- return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_PARSE_ERROR, Error_level::MINOR_ERROR,
- " message::Parkspace_search_request_msg ParseFromString error ");
- }
- //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
- m_thread_pool.enqueue(&Parkspace_allocator::execute_for_search, this, request.car_info(), request.command_id());
- return SUCCESS;
- }
- // 车位释放请求消息
- case Communication_message::eParkspace_release_request_msg:
- {
- message::Parkspace_release_request_msg request;
- bool result = request.ParseFromString(p_msg->get_message_buf());
- LOG(INFO)<<"release request, parkspace id: "<<request.release_space_info().parkspace_id();
- if(!result)
- {
- return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_PARSE_ERROR, Error_level::MINOR_ERROR,
- " message::Parkspace_release_request_msg ParseFromString error ");
- }
- //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
- m_thread_pool.enqueue(&Parkspace_allocator::execute_for_release, this, request.release_space_info(), request.command_id());
- return SUCCESS;
- }
- // 车位手动更新请求消息
- case Communication_message::eParkspace_force_update_request_msg:
- {
- message::Parkspace_force_update_request_msg request;
- bool result = request.ParseFromString(p_msg->get_message_buf());
- LOG(INFO)<<"force update request, parkspace id: "<<request.update_space_info().parkspace_id();
- if(!result)
- {
- return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_PARSE_ERROR, Error_level::MINOR_ERROR,
- " message::Parkspace_force_update_request_msg ParseFromString error ");
- }
- //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
- m_thread_pool.enqueue(&Parkspace_allocator::execute_for_force_update, this, request.update_space_info(), request.command_id());
- return SUCCESS;
- }
- // 车位确认占用请求消息
- case Communication_message::eParkspace_confirm_alloc_request_msg:
- {
- message::Parkspace_confirm_alloc_request_msg request;
- bool result = request.ParseFromString(p_msg->get_message_buf());
- LOG(INFO)<<"confirm alloc request, parkspace id: "<<request.confirm_space_info().parkspace_id();
- if(!result)
- {
- return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_PARSE_ERROR, Error_level::MINOR_ERROR,
- " message::Parkspace_force_update_request_msg ParseFromString error ");
- }
- //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
- m_thread_pool.enqueue(&Parkspace_allocator::execute_for_confirm_alloc, this, request.confirm_space_info(), request.command_id());
- return SUCCESS;
- }
- }
- return Error_code::PARKSPACE_ALLOCATOR_MSG_REQUEST_TYPE_ERROR;
- }
- //判断是否为待机,如果已经准备好,则可以执行任务。
- bool Parkspace_allocator::is_ready()
- {
- if ( m_current_status == eParkspace_allocator_normal && m_thread_pool.thread_is_full_load() == false )
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- Parkspace_allocator::parkspace_allocator_status Parkspace_allocator::get_parkspace_allocator_status()
- {
- return m_current_status;
- }
- //分配车位线程函数
- void Parkspace_allocator::execute_for_allocate(message::Car_info car_info, int terminal_id, int command_id)
- {
- std::lock_guard<std::mutex> lck(m_mutex);
- LOG(INFO) << "分配";
- //根据请求的信息反馈分配的车位,并封装发送
- message::Parkspace_allocation_response_msg response_msg;
- message::Base_info t_response_header;
- t_response_header.set_msg_type(message::Message_type::eParkspace_allocation_response_msg);
- t_response_header.set_timeout_ms(1000);
- t_response_header.set_sender(message::Communicator::eParkspace);
- t_response_header.set_receiver(message::Communicator::eMain);
- message::Error_manager t_error;
- message::Parkspace_info t_allocated_space;
- //获取当前所有车位状态,分配车位
- message::Parkspace_allocation_status_msg t_current_parkspace_status = Parkspace_allocation_communicator::get_instance_references().get_status();
- int index = -1;
- if(t_current_parkspace_status.parkspace_info_size()<=0)
- {
- t_error.set_error_code(PARKSPACE_ALLOCATOR_SPACE_EMPTY);
- t_error.set_error_level(message::Error_level::MAJOR_ERROR);
- LOG(ERROR) << "无车位";
- }
- else
- {
- for (size_t i = 0; i < t_current_parkspace_status.parkspace_info_size(); i++)
- {
- // 找到高于车高且空闲车位,则分配
- if (t_current_parkspace_status.parkspace_info(i).has_car_info()
- /* && t_current_parkspace_status.parkspace_info(i).height() > car_info.car_height()
- && t_current_parkspace_status.parkspace_info(i).width() > car_info.car_width() */
- && t_current_parkspace_status.parkspace_info(i).parkspace_status() == message::Parkspace_status::eParkspace_empty)
- {
- index = i;
- break;
- }
- }
-
- if(index<0){
- t_error.set_error_code(PARKSPACE_ALLOCATOR_ALLOCATE_FAILED);
- t_error.set_error_level(message::Error_level::MAJOR_ERROR);
- t_allocated_space.CopyFrom(t_current_parkspace_status.parkspace_info(0));
- t_allocated_space.mutable_car_info()->CopyFrom(car_info);
- LOG(WARNING) << "分配车位失败";
- }else{
- t_error.set_error_code(SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- message::Parkspace_info t_space_to_lock = t_current_parkspace_status.parkspace_info(index);
- t_space_to_lock.set_parkspace_status(message::Parkspace_status::eParkspace_locked);
- t_space_to_lock.mutable_car_info()->CopyFrom(car_info);
- t_allocated_space.CopyFrom(t_space_to_lock);
- // 分配车位后更新车位状态
- Parkspace_allocation_communicator::get_instance_references().update_parkspace_status(index, t_space_to_lock);
- LOG(INFO) << "第"<<index<<"号位占用, id: "<<t_space_to_lock.parkspace_id()<<", 号牌: "<<t_space_to_lock.car_info().license();
- // 数据库操作,修改车位状态为锁定,车辆状态为停车中
- message::Vehicle_status t_vehicle_status = message::Vehicle_status::eVehicle_parking;
- Error_manager ec = Parkspace_db_manager::get_instance_references().update_parkspace_status(t_space_to_lock);
- if(ec != SUCCESS)
- {
- LOG(ERROR) << "更新db车位状态失败: "<< ec.to_string();
- }
- ec = Parkspace_db_manager::get_instance_references().update_vehicle_status(t_space_to_lock, t_vehicle_status);
- if (ec != SUCCESS)
- {
- LOG(ERROR) << "更新db车辆状态失败: " <<ec.to_string();
- }
- }
- }
- response_msg.mutable_base_info()->CopyFrom(t_response_header);
- response_msg.set_command_id(command_id);
- response_msg.mutable_error_manager()->CopyFrom(t_error);
- response_msg.mutable_allocated_space_info()->CopyFrom(t_allocated_space);
- Communication_message response=Communication_message();
- response.reset(t_response_header, response_msg.SerializeAsString());
- Parkspace_allocation_communicator::get_instance_references().send_response(&response);
- }
- //查询车位线程函数
- void Parkspace_allocator::execute_for_search(message::Car_info car_info, int command_id)
- {
- std::lock_guard<std::mutex> lck(m_mutex);
- LOG(INFO) << "查询";
- //根据车辆凭证信息查询车辆位置
- message::Parkspace_search_response_msg response_msg;
- message::Base_info t_response_header;
- message::Error_manager t_error;
- message::Parkspace_info t_car_position;
- t_response_header.set_msg_type(message::Message_type::eParkspace_search_response_msg);
- t_response_header.set_timeout_ms(1000);
- t_response_header.set_sender(message::Communicator::eParkspace);
- t_response_header.set_receiver(message::Communicator::eMain);
- //获取当前所有车位状态,查询车辆位置
- message::Parkspace_allocation_status_msg t_current_parkspace_status = Parkspace_allocation_communicator::get_instance_references().get_status();
- int index = -1;
- if(t_current_parkspace_status.parkspace_info_size()<=0)
- {
- t_error.set_error_code(PARKSPACE_ALLOCATOR_SPACE_EMPTY);
- t_error.set_error_level(message::Error_level::MAJOR_ERROR);
- LOG(ERROR) << "无车位";
- }
- else
- {
- for (size_t i = 0; i < t_current_parkspace_status.parkspace_info_size(); i++)
- {
- // 找到凭证号/号牌对应车位,记录
- if (t_current_parkspace_status.parkspace_info(i).has_car_info()
- && t_current_parkspace_status.parkspace_info(i).car_info().license() == car_info.license()
- && t_current_parkspace_status.parkspace_info(i).parkspace_status() == message::Parkspace_status::eParkspace_occupied)
- {
- index = i;
- break;
- }
- }
- if(index<0){
- t_error.set_error_code(PARKSPACE_ALLOCATOR_SEARCH_FAILED);
- t_error.set_error_level(message::Error_level::MAJOR_ERROR);
- // 默认第一个车位
- t_car_position.CopyFrom(t_current_parkspace_status.parkspace_info(0));
- LOG(INFO) << "查询车位失败";
- }else{
- t_error.set_error_code(SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- message::Parkspace_info t_space_searching = t_current_parkspace_status.parkspace_info(index);
- t_car_position.CopyFrom(t_space_searching);
- LOG(INFO) << "车辆"<<car_info.license() <<"在第"<<index<<"号位, id: "<<t_space_searching.parkspace_id();
- // 无数据库操作
- }
- }
- response_msg.mutable_base_info()->CopyFrom(t_response_header);
- response_msg.set_command_id(command_id);
- response_msg.mutable_error_manager()->CopyFrom(t_error);
- response_msg.mutable_car_position()->CopyFrom(t_car_position);
- Communication_message response=Communication_message();
- response.reset(t_response_header, response_msg.SerializeAsString());
- Parkspace_allocation_communicator::get_instance_references().send_response(&response);
- }
- //释放车位线程函数
- void Parkspace_allocator::execute_for_release(message::Parkspace_info space_info, int command_id)
- {
- std::lock_guard<std::mutex> lck(m_mutex);
- LOG(INFO) << "释放";
- //根据车位信息定位待释放车位位置,车辆凭证号用于校验
- message::Parkspace_release_response_msg response_msg;
- message::Base_info t_response_header;
- t_response_header.set_msg_type(message::eParkspace_release_response_msg);
- t_response_header.set_sender(message::eParkspace);
- t_response_header.set_receiver(message::eMain);
- t_response_header.set_timeout_ms(1000);
- message::Error_manager t_error;
- message::Parkspace_info t_release_space;
- //获取当前所有车位状态,找到待释放的车位
- message::Parkspace_allocation_status_msg t_current_parkspace_status = Parkspace_allocation_communicator::get_instance_references().get_status();
- int index = -1;
- if(t_current_parkspace_status.parkspace_info_size()<=0)
- {
- t_error.set_error_code(PARKSPACE_ALLOCATOR_SPACE_EMPTY);
- t_error.set_error_level(message::Error_level::MAJOR_ERROR);
- LOG(ERROR) << "无车位";
- }
- else if (!space_info.has_car_info())
- {
- t_error.set_error_code(PARKSPACE_ALLOCATOR_RELEASE_FAILED);
- t_error.set_error_level(message::Error_level::MAJOR_ERROR);
- LOG(WARNING) << "传入待释放车位无车辆信息";
- }
- else
- {
- for (size_t i = 0; i < t_current_parkspace_status.parkspace_info_size(); i++)
- {
- // 找到占用或锁定的车位,且车辆信息匹配
- if (t_current_parkspace_status.parkspace_info(i).parkspace_id() == space_info.parkspace_id()
- && (t_current_parkspace_status.parkspace_info(i).parkspace_status() == message::Parkspace_status::eParkspace_occupied
- ||t_current_parkspace_status.parkspace_info(i).parkspace_status() == message::Parkspace_status::eParkspace_locked)
- && t_current_parkspace_status.parkspace_info(i).has_car_info()
- && t_current_parkspace_status.parkspace_info(i).car_info().license() == space_info.car_info().license())
- {
- index = i;
- break;
- }
- }
- if(index<0){
- t_error.set_error_code(PARKSPACE_ALLOCATOR_RELEASE_FAILED);
- t_error.set_error_level(message::Error_level::MAJOR_ERROR);
- // 传入默认车位信息
- t_release_space.CopyFrom(space_info);
- LOG(WARNING) << "释放车位失败";
- }else{
- t_error.set_error_code(SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- message::Parkspace_info t_space_to_release = t_current_parkspace_status.parkspace_info(index);
- t_space_to_release.set_parkspace_status(message::Parkspace_status::eParkspace_empty);
- t_release_space.CopyFrom(t_space_to_release);
- // 分配车位后更新车位状态
- Parkspace_allocation_communicator::get_instance_references().update_parkspace_status(index, t_space_to_release);
- LOG(INFO) << "第"<<index<<"号位释放, id: "<<t_space_to_release.parkspace_id();
- // 数据库操作,车位状态改为空闲,查询车辆状态,确定是停车失败还是取车完成
- // 停车失败仅修改车位与车辆状态
- // 取车完成同时还修改停车记录
- Error_manager ec = Parkspace_db_manager::get_instance_references().update_parkspace_status(t_space_to_release);
- if(ec != SUCCESS)
- {
- LOG(ERROR) << "更新db车位状态失败: "<< ec.to_string();
- }
- message::Vehicle_status t_vehicle_status = message::Vehicle_status::eVehicle_idle;
- ec = Parkspace_db_manager::get_instance_references().update_vehicle_status(t_space_to_release, t_vehicle_status);
- if(ec != SUCCESS)
- {
- LOG(ERROR) << "更新db车辆状态失败: "<< ec.to_string();
- }
- int t_park_record_id;
- ec = Parkspace_db_manager::get_instance_references().query_vehicle(t_space_to_release.car_info().license(), t_vehicle_status, t_park_record_id);
- if(ec != SUCCESS)
- {
- LOG(ERROR) << "查询db车辆状态失败: "<< ec.to_string();
- }else if(t_vehicle_status == message::Vehicle_status::eVehicle_in_garage)
- {
- time_t tt = time(NULL);
- tm *t = localtime(&tt);
- char my_time_buf[255];
- memset(my_time_buf, 0, 255);
- sprintf(my_time_buf, "%d-%02d-%02d %02d:%02d:%02d",
- t->tm_year + 1900,
- t->tm_mon + 1,
- t->tm_mday,
- t->tm_hour,
- t->tm_min,
- t->tm_sec);
- t_space_to_release.set_leave_time(my_time_buf);
- ec = Parkspace_db_manager::get_instance_references().update_parking_record(t_space_to_release, t_park_record_id);
- if(ec != SUCCESS)
- {
- LOG(ERROR) << "更新db停车记录失败: "<< ec.to_string();
- }
- }
- }
- }
- response_msg.mutable_base_info()->CopyFrom(t_response_header);
- response_msg.set_command_id(command_id);
- response_msg.mutable_error_manager()->CopyFrom(t_error);
- response_msg.mutable_release_space_info()->CopyFrom(t_release_space);
- Communication_message response=Communication_message();
- response.reset(t_response_header, response_msg.SerializeAsString());
- Parkspace_allocation_communicator::get_instance_references().send_response(&response);
- }
- //强制更新车位信息线程函数
- void Parkspace_allocator::execute_for_force_update(message::Parkspace_info space_info, int command_id)
- {
- std::lock_guard<std::mutex> lck(m_mutex);
- LOG(INFO) << "手动";
- //根据车位信息定位待释放车位位置,车辆凭证号用于校验
- //!!!!!此处跳过外部处理与调用的过程,直接在内部调用,发送分配结果用于测试,目前一直发布第一个车位
- message::Parkspace_force_update_response_msg response_msg;
- message::Base_info t_response_header;
- t_response_header.set_msg_type(message::eParkspace_force_update_response_msg);
- t_response_header.set_sender(message::eParkspace);
- t_response_header.set_receiver(message::eMain);
- t_response_header.set_timeout_ms(1000);
- message::Error_manager t_error;
- message::Parkspace_info t_update_space;
- //获取当前所有车位状态,找到待释放的车位
- message::Parkspace_allocation_status_msg t_current_parkspace_status = Parkspace_allocation_communicator::get_instance_references().get_status();
- int index = -1;
- if(t_current_parkspace_status.parkspace_info_size()<=0)
- {
- t_error.set_error_code(PARKSPACE_ALLOCATOR_SPACE_EMPTY);
- t_error.set_error_level(message::Error_level::MAJOR_ERROR);
- LOG(ERROR) << "无车位";
- }
- else
- {
- for (size_t i = 0; i < t_current_parkspace_status.parkspace_info_size(); i++)
- {
- // 找到高于车高且空闲车位,则分配
- if (t_current_parkspace_status.parkspace_info(i).parkspace_id() == space_info.parkspace_id())
- {
- index = i;
- break;
- }
- }
- if(index<0){
- t_error.set_error_code(PARKSPACE_ALLOCATOR_FORCE_UPDATE_FAILED);
- t_error.set_error_level(message::Error_level::MAJOR_ERROR);
- t_update_space.CopyFrom(space_info);
- LOG(WARNING) << "手动更新车位失败";
- }else{
- t_error.set_error_code(SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- t_update_space.CopyFrom(space_info);
- // 分配车位后更新车位状态
- Parkspace_allocation_communicator::get_instance_references().update_parkspace_status(index, t_update_space);
- LOG(INFO) << "第"<<index<<"号位已手动更新, id: "<<t_update_space.parkspace_id();
- // 数据库操作,车位状态根据手动值进行修改
- Error_manager ec = Parkspace_db_manager::get_instance_references().update_parkspace_status(t_update_space);
- if(ec != SUCCESS)
- {
- LOG(ERROR) << "更新db车位状态失败: "<< ec.to_string();
- }
- }
- }
- response_msg.mutable_base_info()->CopyFrom(t_response_header);
- response_msg.set_command_id(command_id);
- response_msg.mutable_error_manager()->CopyFrom(t_error);
- response_msg.mutable_update_space_info()->CopyFrom(t_update_space);
- Communication_message response=Communication_message();
- response.reset(t_response_header, response_msg.SerializeAsString());
- Parkspace_allocation_communicator::get_instance_references().send_response(&response);
- }
- //确认分配车位线程函数
- void Parkspace_allocator::execute_for_confirm_alloc(message::Parkspace_info space_info, int command_id)
- {
- std::lock_guard<std::mutex> lck(m_mutex);
- LOG(INFO) << "确认分配";
- //根据车位信息定位待确认占用车位
- message::Parkspace_confirm_alloc_response_msg response_msg;
- message::Base_info t_response_header;
- t_response_header.set_msg_type(message::eParkspace_confirm_alloc_response_msg);
- t_response_header.set_sender(message::eParkspace);
- t_response_header.set_receiver(message::eMain);
- t_response_header.set_timeout_ms(1000);
- message::Error_manager t_error;
- message::Parkspace_info t_confirm_space;
- //获取当前所有车位状态,找到待释放的车位
- message::Parkspace_allocation_status_msg t_current_parkspace_status = Parkspace_allocation_communicator::get_instance_references().get_status();
- int index = -1;
- if(t_current_parkspace_status.parkspace_info_size()<=0)
- {
- t_error.set_error_code(PARKSPACE_ALLOCATOR_SPACE_EMPTY);
- t_error.set_error_level(message::Error_level::MAJOR_ERROR);
- LOG(ERROR) << "无车位";
- }
- else if (!space_info.has_car_info())
- {
- t_error.set_error_code(PARKSPACE_ALLOCATOR_PARAM_ERROR);
- t_error.set_error_level(message::Error_level::MAJOR_ERROR);
- LOG(WARNING) << "传入确认占用车位无车辆信息";
- }
- else
- {
- for (size_t i = 0; i < t_current_parkspace_status.parkspace_info_size(); i++)
- {
- // 找到相同车位,车辆信息匹配,且车位为锁定状态
- if (t_current_parkspace_status.parkspace_info(i).parkspace_id() == space_info.parkspace_id()
- && t_current_parkspace_status.parkspace_info(i).parkspace_status() == message::Parkspace_status::eParkspace_locked
- && t_current_parkspace_status.parkspace_info(i).has_car_info()
- && t_current_parkspace_status.parkspace_info(i).car_info().license() == space_info.car_info().license())
- {
- index = i;
- break;
- }
- }
- if(index<0){
- t_error.set_error_code(PARKSPACE_ALLOCATOR_CONFIRM_ALLOC_ERROR);
- t_error.set_error_level(message::Error_level::MAJOR_ERROR);
- t_confirm_space.CopyFrom(space_info);
- LOG(WARNING) << "确认占用车位失败";
- }else{
- t_error.set_error_code(SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- t_confirm_space.CopyFrom(space_info);
- t_confirm_space.set_parkspace_status(message::Parkspace_status::eParkspace_occupied);
- time_t tt = time(NULL);
- tm *t = localtime(&tt);
- char my_time_buf[255];
- memset(my_time_buf, 0, 255);
- sprintf(my_time_buf, "%d-%02d-%02d %02d:%02d:%02d",
- t->tm_year + 1900,
- t->tm_mon + 1,
- t->tm_mday,
- t->tm_hour,
- t->tm_min,
- t->tm_sec);
- t_confirm_space.set_entry_time(my_time_buf);
- // 分配车位后更新车位状态
- Parkspace_allocation_communicator::get_instance_references().update_parkspace_status(index, t_confirm_space);
- LOG(INFO) << "第" << index << "号位已确认占用, id: " << t_confirm_space.parkspace_id();
- // 数据库操作,车位状态改为占用,插入停车记录,更新车辆状态为入库
- Error_manager ec = Parkspace_db_manager::get_instance_references().update_parkspace_status(t_confirm_space);
- if(ec != SUCCESS)
- {
- LOG(ERROR) << "更新db车位状态失败: "<< ec.to_string();
- }
- ec = Parkspace_db_manager::get_instance_references().insert_parking_record(t_confirm_space);
- if(ec != SUCCESS)
- {
- LOG(ERROR) << "插入db停车记录失败: "<< ec.to_string();
- }
- message::Vehicle_status t_vehicle_status = message::Vehicle_status::eVehicle_in_garage;
- ec = Parkspace_db_manager::get_instance_references().update_vehicle_status(t_confirm_space, t_vehicle_status);
- if(ec != SUCCESS)
- {
- LOG(ERROR) << "更新db车辆状态失败: "<< ec.to_string();
- }
- }
- }
- response_msg.mutable_base_info()->CopyFrom(t_response_header);
- response_msg.set_command_id(command_id);
- response_msg.mutable_error_manager()->CopyFrom(t_error);
- response_msg.mutable_confirm_alloc_space_info()->CopyFrom(t_confirm_space);
- Communication_message response=Communication_message();
- response.reset(t_response_header, response_msg.SerializeAsString());
- Parkspace_allocation_communicator::get_instance_references().send_response(&response);
- }
|