123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893 |
- /*
- * @Description: 车位分配算法模块,使用单例模式,接收外部请求并通过调用通信块接口发送反馈
- * @Author: yct
- * @Date: 2020-07-10 11:02:40
- * @LastEditTime: 2020-08-06 10:32:28
- * @LastEditors: yct
- */
- #include "parkspace_manager.h"
- #include "parkspace_communicator.h"
- Parkspace_manager::Parkspace_manager()
- {
- }
- Parkspace_manager::~Parkspace_manager()
- {
- parkspace_manager_uninit();
- }
- //初始化
- Error_manager Parkspace_manager::parkspace_manager_init(int threads_size)
- {
- // 初始化任务处理线程池
- m_thread_pool.thread_pool_init(threads_size);
- m_parkspace_manager_satus = eParkspace_manager_normal;
- return Error_code::SUCCESS;
- }
- //反初始化
- Error_manager Parkspace_manager::parkspace_manager_uninit()
- {
- m_thread_pool.thread_pool_uninit();
- m_parkspace_manager_satus = eParkspace_manager_unknown;
- return Error_code::SUCCESS;
- }
- //检查执行者的状态, 判断能否处理这条消息,
- Error_manager Parkspace_manager::check_status()
- {
- if(get_parkspace_manage_status() != eParkspace_manager_normal)
- {
- return Error_manager(Error_code::DB_MANAGER_STATUS_ERROR, Error_level::MAJOR_ERROR,
- " Parkspace_manager::check_executer error ");
- }
- return SUCCESS;
- }
- //处理消息的执行函数
- Error_manager Parkspace_manager::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 ");
- }
- // std::cout<<"分配:"<<request.DebugString()<<std::endl;
- //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
- m_thread_pool.enqueue(&Parkspace_manager::execute_for_allocate, this,
- request.car_info(), request.id_struct().terminal_id(), request.command_key());
- 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 ");
- }
- //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
- // std::cout<<"查询:"<<request.DebugString()<<std::endl;
- m_thread_pool.enqueue(&Parkspace_manager::execute_for_search, this, request.car_info(), request.command_key());
- 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 ";
- if(!result)
- {
- return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_PARSE_ERROR, Error_level::MINOR_ERROR,
- " message::Parkspace_release_request_msg ParseFromString error ");
- }
- //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
- // std::cout<<"释放:"<<request.DebugString()<<std::endl;
- m_thread_pool.enqueue(&Parkspace_manager::execute_for_release, this, request, request.command_key());
- 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 ";
- if(!result)
- {
- return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_PARSE_ERROR, Error_level::MINOR_ERROR,
- " message::Parkspace_force_update_request_msg ParseFromString error ");
- }
- //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
- // std::cout<<"手动:"<<request.DebugString()<<std::endl;
- m_thread_pool.enqueue(&Parkspace_manager::execute_for_force_update, this, request, request.command_key());
- return SUCCESS;
- }
- //手动查询请求
- case Communication_message::eParkspace_manual_search_request_msg:
- {
- message::Parkspace_manual_search_request_msg request;
- bool result = request.ParseFromString(p_msg->get_message_buf());
- LOG(INFO)<<"manual search request, parkspace ";
- if(!result)
- {
- return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_PARSE_ERROR, Error_level::MINOR_ERROR,
- " message::Parkspace_manual_search_request_msg ParseFromString error ");
- }
- //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
- // std::cout<<"手动:"<<request.DebugString()<<std::endl;
- m_thread_pool.enqueue(&Parkspace_manager::execute_for_manual_search, this, request.car_info(), request.command_key());
- 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 ";
- 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_manager::execute_for_confirm_alloc, this, request);
- return SUCCESS;
- }
- //车位数据刷新请求
- case Communication_message::eParkspace_refresh_request_msg:
- {
- return send_parkspace_data();
- }
- }
- return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_REQUEST_TYPE_ERROR, Error_level::MINOR_ERROR,
- " fun error ");
- }
- Error_manager Parkspace_manager::encapsulating_heartbeat_messages()
- {
- //封装心跳消息
- Database_controller::Database_controller_status t_database_controller_status;
- Parkspace_manager::Parkspace_manager_satus t_parkspace_manager_satus;
- t_database_controller_status = Database_controller::get_instance_pointer()->get_database_controller_status();
- t_parkspace_manager_satus = Parkspace_manager::get_instance_pointer()->get_parkspace_manage_status();
- //车位信息消息赋值
- message::Parkspace_allocation_status_msg t_parkspace_status_msg;
- message::Base_info t_base_info;
- message::Error_manager t_error;
- t_base_info.set_msg_type(message::Message_type::eParkspace_allocation_status_msg);
- t_base_info.set_timeout_ms(5000);
- t_base_info.set_sender(message::Communicator::eParkspace);
- t_base_info.set_receiver(message::Communicator::eMain);
- t_error.set_error_code(0);
- t_parkspace_status_msg.mutable_base_info()->CopyFrom(t_base_info);
- t_parkspace_status_msg.mutable_error_manager()->CopyFrom(t_error);
- t_parkspace_status_msg.set_database_controller_status((message::Database_controller_status)t_database_controller_status);
- t_parkspace_status_msg.set_parkspace_manager_satus((message::Parkspace_manager_satus)t_parkspace_manager_satus);
- message::Parkspace_allocation_data_msg parkspaceAllocationDataMsg[UNIT_NUMBER];
- int t_small_parkspace_size[UNIT_NUMBER];
- int t_medium_parkspace_size[UNIT_NUMBER];
- int t_large_parkspace_size[UNIT_NUMBER];
- int t_total_parkspace_size[UNIT_NUMBER];
- for (int i = 0; i< UNIT_NUMBER; ++i)
- {
- m_parkspace_operating_function.get_specify_the_type_status_unit_parkspace_info(parkspaceAllocationDataMsg[i],message::Parkspace_type::MIN_PARKINGSPACE,message::Parkspace_status::eParkspace_empty,i);
- t_small_parkspace_size[i] = parkspaceAllocationDataMsg[i].parkspace_info_ex_size();
- parkspaceAllocationDataMsg[i].clear_parkspace_info_ex();
- m_parkspace_operating_function.get_specify_the_type_status_unit_parkspace_info(parkspaceAllocationDataMsg[i],message::Parkspace_type::MID_PARKINGSPACE,message::Parkspace_status::eParkspace_empty,i);
- t_medium_parkspace_size[i] = parkspaceAllocationDataMsg[i].parkspace_info_ex_size();
- parkspaceAllocationDataMsg[i].clear_parkspace_info_ex();
- m_parkspace_operating_function.get_specify_the_type_status_unit_parkspace_info(parkspaceAllocationDataMsg[i],message::Parkspace_type::BIG_PARKINGSPACE,message::Parkspace_status::eParkspace_empty,i);
- t_large_parkspace_size[i] = parkspaceAllocationDataMsg[i].parkspace_info_ex_size();
- parkspaceAllocationDataMsg[i].clear_parkspace_info_ex();
- t_total_parkspace_size[i] = t_small_parkspace_size[i] + t_medium_parkspace_size[i] + t_large_parkspace_size[i];
- t_parkspace_status_msg.set_unit_id(i);
- t_parkspace_status_msg.set_small_parkspace_remaining_number(t_small_parkspace_size[i]);
- t_parkspace_status_msg.set_medium_parkspace_remaining_number(t_medium_parkspace_size[i]);
- t_parkspace_status_msg.set_large_parkspace_remaining_number(t_large_parkspace_size[i]);
- t_parkspace_status_msg.set_total_parkspace_remaining_number(t_total_parkspace_size[i]);
- Parkspace_communicator::get_instance_references().encapsulate_msg(t_parkspace_status_msg.SerializeAsString());
-
- //std::cout << " t_parkspace_status_msg.DebugString() = " << t_parkspace_status_msg.DebugString() << std::endl;
- }
- return SUCCESS;
- }
- //判断是否为待机,如果已经准备好,则可以执行任务。
- bool Parkspace_manager::is_ready()
- {
- if ( m_parkspace_manager_satus == eParkspace_manager_normal && m_thread_pool.thread_is_full_load() == false )
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- Parkspace_manager::Parkspace_manager_satus Parkspace_manager::get_parkspace_manage_status()
- {
- return m_parkspace_manager_satus;
- }
- // 检查车辆是否已存在,通常分配前调用
- bool Parkspace_manager::check_car_existence(std::string license)
- {
- message::Parkspace_info parkspaceInfo;
- //获取车辆状态
- Error_manager error=m_parkspace_operating_function.query_one_parkspace_with_license(license,parkspaceInfo);
- //如果车辆处于停车中或者已入库 则车辆已存在
- if(parkspaceInfo.has_car_info() && parkspaceInfo.parkingspace_status() != message::eParkspace_empty)
- {
- return true;
- }
- return false;
- }
- //发送车位数据
- Error_manager Parkspace_manager::send_parkspace_data()
- {
- message::Parkspace_allocation_data_msg all_parkspace_data;
- Error_manager t_error=m_parkspace_operating_function.get_all_parkspace_info(all_parkspace_data);
- if(t_error != SUCCESS)
- {
- return t_error;
- }
- message::Base_info t_base_info;
- message::Error_manager error;
- t_base_info.set_msg_type(message::Message_type::eParkspace_allocation_data_response_msg);
- t_base_info.set_timeout_ms(5000);
- t_base_info.set_sender(message::Communicator::eParkspace);
- t_base_info.set_receiver(message::Communicator::eEmpty);
- all_parkspace_data.mutable_base_info()->CopyFrom(t_base_info);
- error.set_error_code(0);
- all_parkspace_data.mutable_error_manager()->CopyFrom(error);
- return Parkspace_communicator::get_instance_references().encapsulate_msg(all_parkspace_data.SerializeAsString());
- }
- //找到一个合适车位
- Error_manager Parkspace_manager::query_the_optimal_parkspace_server(message::Parkspace_info &parkspace_info,message::Car_info car_info, int terminal_id)
- {
- Error_manager error;
- //小车对应小车位 中车对应中车位 大车对应大车位
- message::Parkspace_info t_parkspace_info;
- if ( car_info.car_height() <= MIN_CAR_HIGH )
- {
- error = m_parkspace_operating_function.query_one_specified_unit_and_type_empty_parkspace(terminal_id/2,message::Parkspace_type::MIN_PARKINGSPACE,t_parkspace_info);
- if ( error == SUCCESS )
- {
- parkspace_info.CopyFrom(t_parkspace_info);
- return Error_code::SUCCESS;
- }
- }
- if ( car_info.car_height() <= MID_CAR_HIGH )
- {
- error = m_parkspace_operating_function.query_one_specified_unit_and_type_empty_parkspace(terminal_id/2,message::Parkspace_type::MID_PARKINGSPACE,t_parkspace_info);
- if ( error == SUCCESS )
- {
- parkspace_info.CopyFrom(t_parkspace_info);
- return Error_code::SUCCESS;
- }
- }
- if ( car_info.car_height() <= BIG_CAR_HIGH )
- {
- error = m_parkspace_operating_function.query_one_specified_unit_and_type_empty_parkspace(terminal_id/2,message::Parkspace_type::BIG_PARKINGSPACE,t_parkspace_info);
- if ( error == SUCCESS )
- {
- parkspace_info.CopyFrom(t_parkspace_info);
- return Error_code::SUCCESS;
- }
- }
- return error;
- }
- //分配车位线程函数
- void Parkspace_manager:: execute_for_allocate(message::Car_info car_info, int terminal_id, std::string command_key)
- {
- std::lock_guard<std::mutex> lck(m_parkspace_lock);
- LOG(INFO) << "分配车位 " <<"唯一码:"<< car_info.license()<< "牌号:" << car_info.car_numberplate() << " 车高:" << car_info.car_height()
- << " command_key=" << command_key;
- //根据请求的信息反馈分配的车位,并封装发送
- 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;
- Error_manager error;
- message::Car_type t_car_type;
- if ( car_info.car_height() <= MIN_CAR_HIGH )
- {
- t_car_type = message::Car_type::MIN_CAR;
- }
- else if ( car_info.car_height() <= MID_CAR_HIGH )
- {
- t_car_type = message::Car_type::MID_CAR;
- }
- else if ( car_info.car_height() <= BIG_CAR_HIGH )
- {
- t_car_type = message::Car_type::BIG_CAR;
- }
- else
- {
- t_car_type = message::Car_type::UNKNOW_CAR_TYPE;
- error = Error_manager(Error_code::HARDWARE_LIMIT_HEIGHT_OUT_RANGE,Error_level::MINOR_ERROR,"car height out of range");
- }
- //分配之前查询车辆是否已经存在
- if (error == Error_code::SUCCESS)
- {
- if (check_car_existence(car_info.car_numberplate()))
- {
- t_error.set_error_code(PARKSPACE_ALLOCATOR_CAR_ALREADY_EXIST);
- t_error.set_error_level(message::Error_level::MINOR_ERROR);
- t_error.set_error_description(" The vehicle already exists error");
- LOG(ERROR) << car_info.license() << " 车辆已存在";
- }
- else
- {
- //找到一个合适车位
- error = query_the_optimal_parkspace_server(t_allocated_space,car_info,terminal_id);
- if ( error != Error_code::SUCCESS)
- {
- t_error.set_error_code(PARKSPACE_ALLOCATOR_ALLOCATE_FAILED);
- t_error.set_error_level(message::Error_level::MINOR_ERROR);
- t_error.set_error_description(error.get_error_description().append("车辆类型:").append(std::to_string(t_car_type)));
- LOG(INFO) << " 分配车位失败,该单元无可供目标车辆停车的车位! 车辆类型:"<<t_car_type;
- }
- else
- {
- // 分配车位后更新数据库中车位状态
- t_allocated_space.set_parkingspace_status(message::Parkspace_status::eParkspace_locked);
- t_allocated_space.set_parkspace_status_target(message::Parkspace_status::eParkspace_locked);
- t_allocated_space.mutable_car_info()->CopyFrom(car_info);
- t_allocated_space.set_car_type(t_car_type);
- error = m_parkspace_operating_function.update_parkspace_data(t_allocated_space);
- if (error != SUCCESS)
- {
- t_error.set_error_code(error.get_error_code());
- t_error.set_error_level((message::Error_level)error.get_error_level());
- t_error.set_error_description(error.get_error_description());
- LOG(ERROR) << car_info.license() << " 停车更新数据库失败! " << error.to_string();
- }
- else
- {
- message::Vehicle_status t_car_status = message::Vehicle_status::eVehicle_parking;
- error = m_parkspace_operating_function.update_vehicle_with_parkspace(t_allocated_space,t_car_status);
- if ( error != Error_code::SUCCESS)
- {
- t_error.set_error_code(error.get_error_code());
- t_error.set_error_level((message::Error_level)error.get_error_level());
- t_error.set_error_description(error.get_error_description());
- LOG(ERROR) << car_info.license() << " 停车更新数据库车辆表失败! " << error.to_string();
- }
- else
- {
- t_error.set_error_code(SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- LOG(INFO) << " 分配车位成功! 车位ID:"<<t_allocated_space.parkingspace_index_id()<<" 单元号:"<<t_allocated_space.parkingspace_unit_id()<<" 单元内部ID:"<<t_allocated_space.parkingspace_label_id();
- }
- }
- }
- }
- }
- else
- {
- t_error.set_error_code(error.get_error_code());
- t_error.set_error_level((message::Error_level)error.get_error_level());
- t_error.set_error_description(error.get_error_description());
- LOG(ERROR) <<"分配车位失败! "<< "终端号:"<<terminal_id<<" 唯一码:"<<car_info.license()<<" 号牌:"<<car_info.car_numberplate()<<" 车高:"<<car_info.car_height();
- }
- response_msg.mutable_base_info()->CopyFrom(t_response_header);
- response_msg.set_command_key(command_key);
- response_msg.mutable_error_manager()->CopyFrom(t_error);
- response_msg.set_car_type(t_car_type);
- response_msg.add_allocated_parkspace_info_ex()->CopyFrom(t_allocated_space);
- Communication_message response = Communication_message();
- response.reset(t_response_header, response_msg.SerializeAsString());
- Parkspace_communicator::get_instance_references().send_response(&response);
- send_parkspace_data();
- std::cout << std::endl;
- }
- //确认分配车位线程函数
- void Parkspace_manager::execute_for_confirm_alloc(message::Parkspace_confirm_alloc_request_msg request)
- {
- std::lock_guard<std::mutex> lck(m_parkspace_lock);
- 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;
- t_error.set_error_code(Error_code::SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- //确认的车位
- message::Parkspace_info confirm_alloc_space_info;
- message::Locate_information locate_information;
- LOG_IF(WARNING, request.confirm_parkspace_info_ex().size() <= 0) <<"confirm alloc space empty";
- Error_manager error;
- for (int i = 0; i < request.confirm_parkspace_info_ex().size(); ++i)
- {
- //核对确认车位信息
- error = m_parkspace_operating_function.query_one_parkspace_with_parkspace_id(request.confirm_parkspace_info_ex(i).parkingspace_index_id(),confirm_alloc_space_info);
- if ( !request.confirm_parkspace_info_ex(i).has_car_info() || \
- confirm_alloc_space_info.car_info().license() != request.confirm_parkspace_info_ex(i).car_info().license() )
- {
- t_error.set_error_code(PARKSPACE_ALLOCATOR_PARAM_ERROR);
- t_error.set_error_level(message::Error_level::MINOR_ERROR);
- t_error.set_error_description("Parking information does not match or the target status of the parking space is wrong error!");
- LOG(ERROR) << "确认分配失败 传入数据与数据库数据不匹配! 传入数据{ license:"<<request.confirm_parkspace_info_ex(i).car_info().license()<<"} 数据库数据{license:"<<confirm_alloc_space_info.car_info().license()<<"}";
- }
- else
- {
- confirm_alloc_space_info.mutable_car_info()->CopyFrom(request.confirm_parkspace_info_ex(i).car_info());
- locate_information.CopyFrom(request.locate_information());
- error = confirm_alloc_function(confirm_alloc_space_info,locate_information);
- if ( error != SUCCESS )
- {
- t_error.set_error_code(error.get_error_code());
- t_error.set_error_level((message::Error_level)error.get_error_level());
- t_error.set_error_description(error.get_error_description());
- }
- else
- {
- t_error.set_error_code(SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- LOG(INFO) << " 确认分配成功! 车位ID:"<<confirm_alloc_space_info.parkingspace_index_id()<<" 单元号:"<<confirm_alloc_space_info.parkingspace_unit_id()<<" 单元内部ID:"<<confirm_alloc_space_info.parkingspace_label_id();
- }
- }
- }
- response_msg.mutable_base_info()->CopyFrom(t_response_header);
- response_msg.set_command_key(request.command_key());
- response_msg.mutable_error_manager()->CopyFrom(t_error);
- response_msg.set_car_type(request.car_type());
- response_msg.add_confirm_parkspace_info_ex()->CopyFrom(confirm_alloc_space_info);
- Communication_message response=Communication_message();
- response.reset(t_response_header, response_msg.SerializeAsString());
- Parkspace_communicator::get_instance_references().send_response(&response);
- send_parkspace_data();
- std::cout<<std::endl;
- }
- //查询车位线程函数
- void Parkspace_manager::execute_for_search(message::Car_info car_info,std::string command_key)
- {
- std::lock_guard<std::mutex> lck(m_parkspace_lock);
- LOG(INFO) << "查询车位 "<<"唯一码:"<<car_info.license()<<" 车牌号:"<<car_info.car_numberplate()<<" command_key="<<command_key;
- //根据车辆凭证信息查询车辆位置
- message::Parkspace_search_response_msg response_msg;
- message::Base_info t_response_header;
- message::Error_manager t_error;
- 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_info parkspace_info;
- Error_manager error=m_parkspace_operating_function.query_one_occupied_parkspace_with_license(car_info.license(),parkspace_info);
- if ( error != SUCCESS )
- {
- t_error.set_error_code(error.get_error_code());
- t_error.set_error_level((message::Error_level)error.get_error_level());
- t_error.set_error_description(error.get_error_description());
- LOG(ERROR) << "查询车位失败 "<<error.to_string();
- }
- else
- {
- t_error.set_error_code(SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- LOG(INFO) << " 查询成功! 车位ID:"<<parkspace_info.parkingspace_index_id()<<" 单元号:"<<parkspace_info.parkingspace_unit_id()<<" 单元内部ID:"<<parkspace_info.parkingspace_label_id()<<" 唯一码:"<<parkspace_info.car_info().license()<<" 号牌:"<<parkspace_info.car_info().car_numberplate()<<" 车位当前状态:"<<parkspace_info.parkingspace_status();
- }
- response_msg.mutable_base_info()->CopyFrom(t_response_header);
- response_msg.set_command_key(command_key);
- response_msg.mutable_error_manager()->CopyFrom(t_error);
- response_msg.mutable_query_parkspace_info_ex()->Add()->CopyFrom(parkspace_info);
- Communication_message response=Communication_message();
- response.reset(t_response_header, response_msg.SerializeAsString());
- Parkspace_communicator::get_instance_references().send_response(&response);
- std::cout<<std::endl;
- }
- //查询车位线程函数
- void Parkspace_manager::execute_for_manual_search(message::Car_info car_info,std::string command_key)
- {
- std::lock_guard<std::mutex> lck(m_parkspace_lock);
- LOG(INFO) << "手动查询车位 "<<"唯一码:"<<car_info.license()<<" 车牌号:"<<car_info.car_numberplate()<<" command_key="<<command_key;
- //根据车辆凭证信息查询车辆位置
- message::Parkspace_manual_search_response_msg response_msg;
- message::Base_info t_response_header;
- message::Error_manager t_error;
- t_response_header.set_msg_type(message::Message_type::eParkspace_manual_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::eEmpty);
- //查询车辆位置
- message::Parkspace_info parkspace_info;
- Error_manager error=m_parkspace_operating_function.query_one_parkspace_with_numberPlate(car_info.car_numberplate(),parkspace_info);
- if ( error != SUCCESS )
- {
- t_error.set_error_code(error.get_error_code());
- t_error.set_error_level((message::Error_level)error.get_error_level());
- t_error.set_error_description(error.get_error_description());
- LOG(ERROR) << "查询车位失败 "<<error.to_string();
- }
- else
- {
- t_error.set_error_code(SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- LOG(INFO) << " 查询成功! 车位ID:"<<parkspace_info.parkingspace_index_id()<<" 单元号:"<<parkspace_info.parkingspace_unit_id()<<" 单元内部ID:"<<parkspace_info.parkingspace_label_id()<<" 唯一码:"<<parkspace_info.car_info().license()<<" 号牌:"<<parkspace_info.car_info().car_numberplate()<<" 车位当前状态:"<<parkspace_info.parkingspace_status();
- }
- response_msg.mutable_base_info()->CopyFrom(t_response_header);
- response_msg.set_command_key(command_key);
- response_msg.mutable_error_manager()->CopyFrom(t_error);
- response_msg.mutable_query_parkspace_info_ex()->Add()->CopyFrom(parkspace_info);
- Communication_message response=Communication_message();
- response.reset(t_response_header, response_msg.SerializeAsString());
- Parkspace_communicator::get_instance_references().send_response(&response);
- std::cout<<std::endl;
- }
- //释放车位线程函数
- void Parkspace_manager::execute_for_release(message::Parkspace_release_request_msg release_msg, std::string command_key)
- {
- std::lock_guard<std::mutex> lck(m_parkspace_lock);
- //根据车位信息定位待释放车位位置,车辆凭证号用于校验
- 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;
- t_error.set_error_code(Error_code::SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- Error_manager error;
- message::Parkspace_info t_release_space;
- for (int i = 0; i < release_msg.release_parkspace_info_ex_size(); ++i)
- {
- //将传入数据与数据库数据进行核对
- error = m_parkspace_operating_function.query_one_parkspace_with_parkspace_id(release_msg.release_parkspace_info_ex(i).parkingspace_index_id(),t_release_space);
- if ( !release_msg.release_parkspace_info_ex(i).has_car_info() || \
- t_release_space.car_info().license() != release_msg.release_parkspace_info_ex(i).car_info().license() )
- {
- t_error.set_error_code(PARKSPACE_ALLOCATOR_PARAM_ERROR);
- t_error.set_error_level(message::Error_level::MINOR_ERROR);
- t_error.set_error_description(" Incoming parking space data to be released does not match the database data error");
- LOG(ERROR) << "释放车位失败 传入数据与数据库数据不匹配! 传入数据{ license:"<<release_msg.release_parkspace_info_ex(i).car_info().license()<<"} 数据库数据{license:"<<t_release_space.car_info().license()<<"}";
- }
- else
- {
- Error_manager error=release_parkspace_function(t_release_space);
- if ( error != SUCCESS )
- {
- t_error.set_error_code(error.get_error_code());
- t_error.set_error_level((message::Error_level)error.get_error_level());
- t_error.set_error_description(error.get_error_description());
- }
- else
- {
- t_error.set_error_code(SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- LOG(INFO) << " 释放车位成功! 车位ID:"<<t_release_space.parkingspace_index_id()<<" 单元号:"<<t_release_space.parkingspace_unit_id()<<" 单元内部ID:"<<t_release_space.parkingspace_label_id()<<" 唯一码:"<<t_release_space.car_info().license()<<" 号牌:"<<t_release_space.car_info().car_numberplate();
- }
- }
- }
- response_msg.mutable_base_info()->CopyFrom(t_response_header);
- response_msg.set_command_key(command_key);
- response_msg.mutable_error_manager()->CopyFrom(t_error);
- response_msg.mutable_release_parkspace_info_ex()->Add()->CopyFrom(t_release_space);
- Communication_message response=Communication_message();
- response.reset(t_response_header, response_msg.SerializeAsString());
- Parkspace_communicator::get_instance_references().send_response(&response);
- send_parkspace_data();
- std::cout<<std::endl;
- }
- //强制更新车位信息线程函数
- void Parkspace_manager::execute_for_force_update(message::Parkspace_force_update_request_msg space_info, std::string command_key)
- {
- std::lock_guard<std::mutex> lck(m_parkspace_lock);
- LOG(INFO) << "手动 "<<"唯一码:"<<space_info.manual_parkspace_info_ex(0).car_info().license()<<"号牌:"<<space_info.manual_parkspace_info_ex(0).car_info().car_numberplate()<<" command_key="<<command_key;
- //根据车位信息定位待释放车位位置,车辆凭证号用于校验
- //!!!!!此处跳过外部处理与调用的过程,直接在内部调用,发送分配结果用于测试,目前一直发布第一个车位
- 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 = space_info.manual_parkspace_info_ex(0);
- Error_manager error;
- // id 9999为特殊情况,复位所有车位状态
- if (space_info.manual_parkspace_info_ex(0).parkingspace_index_id() == 9999)
- {
- // 复位所有车位状态
- error=m_parkspace_operating_function.clear_all_parkspace_info();
- if( error != SUCCESS)
- {
- t_error.set_error_code(error.get_error_code());
- t_error.set_error_level((message::Error_level) error.get_error_level());
- LOG(INFO) << "手动更新所有车位失败 "<<error.to_string();
- }
- else
- {
- t_error.set_error_code(SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- LOG(INFO) << "所有车位已手动更新 ";
- }
- }
- else
- {
- //手动修改车位信息
- error = m_parkspace_operating_function.update_parkspace_data(t_update_space);
- if (error != SUCCESS)
- {
- t_error.set_error_code(error.get_error_code());
- t_error.set_error_level((message::Error_level)error.get_error_level());
- LOG(ERROR) << "手动 数据库更新车位失败 "<<error.to_string();
- }
- else
- {
- t_error.set_error_code(SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- if(t_update_space.has_car_info() && t_update_space.car_info().has_license() && t_update_space.car_info().license() != "")
- {
- message::Vehicle_status t_vehicle_status=message::eVehicle_idle;
- error = m_parkspace_operating_function.update_vehicle_with_parkspace(t_update_space, t_vehicle_status);
- if (error != SUCCESS)
- {
- t_error.set_error_code(error.get_error_code());
- t_error.set_error_level((message::Error_level)error.get_error_level());
- LOG(ERROR) << "手动 数据库更新车辆失败 "<<error.to_string();
- }
- else
- {
- LOG(INFO) << "第" << t_update_space.parkingspace_index_id() << "号位已手动更新,"<<t_update_space.car_info().license()<<"车辆状态已手动更新";
- }
- }
- else
- {
- LOG(INFO) << "第" << t_update_space.parkingspace_index_id() << "号位已手动更新";
- }
- }
- }
- response_msg.mutable_base_info()->CopyFrom(t_response_header);
- response_msg.set_command_key(command_key);
- response_msg.mutable_error_manager()->CopyFrom(t_error);
- response_msg.mutable_manual_parkspace_info_ex()->Add()->CopyFrom(t_update_space);
- Communication_message response=Communication_message();
- response.reset(t_response_header, response_msg.SerializeAsString());
- Parkspace_communicator::get_instance_references().send_response(&response);
- send_parkspace_data();
- }
- //确认分配操作函数、
- Error_manager Parkspace_manager::confirm_alloc_function(message::Parkspace_info& t_confirm_space,message::Locate_information locate_information)
- {
- //根据车位信息定位待确认占用车位
- Error_manager error;
- //记录当前传入车辆的状态 为后续回退操作服务
- message::Parkspace_status t_parkingspace_status=t_confirm_space.parkingspace_status();
- if(t_confirm_space.parkingspace_status() == message::eParkspace_locked )
- {
- LOG(INFO) << "确认分配 车位id:" <<t_confirm_space.parkingspace_index_id()<< " 唯一码:" << t_confirm_space.car_info().license()<< " 车牌号:" << t_confirm_space.car_info().car_numberplate() ;
- //更新车位数据1
- t_confirm_space.set_parkingspace_status(message::eParkspace_occupied);
- t_confirm_space.set_parkspace_status_target(message::eParkspace_occupied);
- error = m_parkspace_operating_function.update_parkspace_data(t_confirm_space);
- if (error != SUCCESS)
- {
- LOG(ERROR) << "确认分配 数据库更新车位数据失败 " << error.to_string();
- return error;
- }
- else
- {
- //修改车辆状态为已入库
- message::Vehicle_status t_vehicle_status = message::Vehicle_status::eVehicle_in_garage;
- error = m_parkspace_operating_function.update_vehicle_with_parkspace(t_confirm_space, t_vehicle_status);
- if (error != SUCCESS)
- {
- //此处操作不成功就需要回退 因为前一步骤更改了车位表信息导致二表不统一
- LOG(ERROR) << "确认分配 更新数据库车辆数据失败 进行回退---" << error.to_string();
- t_confirm_space.set_parkingspace_status(t_parkingspace_status);
- t_confirm_space.set_parkspace_status_target(t_parkingspace_status);
- for (int i = 1; i < 4; ++i)
- {
- Error_manager e = m_parkspace_operating_function.update_parkspace_data(t_confirm_space);
- if (e != SUCCESS)
- {
- LOG(ERROR) << "回退第"<<i<<"次 失败! " << e.to_string();
- continue;
- }
- else
- {
- LOG(INFO) << " 回退成功! ";
- return e;
- }
- }
- //如果流程进行到这里说明回退三次都失败 需要报三级故障
- return Error_manager(Error_code::DB_UPDATE_FAILED, Error_level::MAJOR_ERROR,
- " update database error ");
- }
- error.set_error_code(SUCCESS);
- error.set_error_level_location(Error_level::NORMAL);
- //插入停车记录
- 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);
- error = m_parkspace_operating_function.insert_parking_record(t_confirm_space,locate_information);
- if ( error != SUCCESS )
- {
- LOG(WARNING) << "插入停车记录失败: " << error.to_string();
- }
- //查询停车记录的ID
- int t_record_id;
- error = m_parkspace_operating_function.query_parking_record(t_confirm_space,t_record_id);
- if ( error != SUCCESS )
- {
- LOG(WARNING) << "查询停车记录ID失败: " << error.to_string();
- }
- //更新停车记录ID
- error = m_parkspace_operating_function.update_record_id(t_confirm_space.car_info().license(),t_record_id);
- if ( error != SUCCESS )
- {
- LOG(WARNING) << "更新停车记录ID失败: " << error.to_string();
- }
- }
- }
- else
- {
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- " 确认占有时车位状态与不正确 error ");
- }
- return Error_code::SUCCESS;
- }
- //释放车位操作函数
- Error_manager Parkspace_manager::release_parkspace_function(message::Parkspace_info& release_parkspace_info)
- {
- Error_manager error;
- if((release_parkspace_info.parkingspace_status() == message::Parkspace_status::eParkspace_occupied || \
- release_parkspace_info.parkingspace_status() == message::Parkspace_status::eParkspace_locked )&& release_parkspace_info.has_car_info())
- {
- LOG(INFO) << "释放车位 车位id:" <<release_parkspace_info.parkingspace_index_id()<< " 唯一码:" << release_parkspace_info.car_info().license()<< " 车牌号:" << release_parkspace_info.car_info().car_numberplate() ;
- release_parkspace_info.set_parkspace_status_target(message::eParkspace_empty);
- release_parkspace_info.set_parkingspace_status(message::eParkspace_empty);
- error = m_parkspace_operating_function.update_parkspace_data(release_parkspace_info);
- if (error != SUCCESS)
- {
- LOG(ERROR) << "释放车位 更新数据库车位数据失败 " << error.to_string();
- return error;
- }
- else
- {
- // search vehicle info
- message::Vehicle_status t_vehicle_status;
- //获取预约ID
- int t_park_record_id;
- error = m_parkspace_operating_function.query_vehicle(release_parkspace_info.car_info().license(),
- t_vehicle_status, t_park_record_id);
- if (error != SUCCESS)
- {
- LOG(ERROR) << "释放车位 数据库查询车辆信息失败 " << error.to_string();
- }
- // update vehicle info
- t_vehicle_status=message::eVehicle_idle;
- error = m_parkspace_operating_function.update_vehicle_with_parkspace(release_parkspace_info, t_vehicle_status);
- if (error != SUCCESS)
- {
- LOG(ERROR) << "释放车位 更新数据库车辆数据失败 " << error.to_string();
- return error;
- }
- error.set_error_code(SUCCESS);
- error.set_error_level_location(Error_level::NORMAL);
- // update records if neeeded (only in fetch)
- if(t_park_record_id>0)
- {
- 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);
- release_parkspace_info.set_leave_time(my_time_buf);
- error = m_parkspace_operating_function.update_parking_record(release_parkspace_info, t_park_record_id);
- if (error != SUCCESS) {
- LOG(WARNING) << "更新停车记录失败: " << error.to_string();
- }
- }
- }
- }
- else
- {
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- " 释放车位时车位状态不正确 error ");
- }
- return Error_code::SUCCESS;
- }
|