12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700 |
- /*
- * @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.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, parkspace id: "<<request.release_space_info().parkingspace_index_id();
- 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.release_space_info(), 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 id: "<<request.update_space_info().parkingspace_index_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 ");
- }
- //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
- // std::cout<<"手动:"<<request.DebugString()<<std::endl;
- m_thread_pool.enqueue(&Parkspace_manager::execute_for_force_update, this, request.update_space_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, parkspace id: "<<request.confirm_space_info().parkingspace_index_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_manager::execute_for_confirm_alloc, this, request.confirm_space_info(), request.command_key());
- 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);
- return Parkspace_communicator::get_instance_references().encapsulate_msg(t_parkspace_status_msg.SerializeAsString());
- }
- //判断是否为待机,如果已经准备好,则可以执行任务。
- 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())
- {
- return false;
- }
- return true;
- }
- 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_msg);
- t_base_info.set_timeout_ms(5000);
- t_base_info.set_sender(message::Communicator::eParkspace);
- t_base_info.set_receiver(message::Communicator::eMain);
- 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_serverA(message::Parkspace_info &parkspace_info,message::Car_info car_info,int terminal_id)
- {
- //车辆高大于1550mm分在四楼以下,小于1550mm分在四楼以上
- /*
- * K=j%15
- * P=i%6
- * S=15*d-(j%15)*(2*d-1)+[6*d-(i%6)(2*d-1)]*W W>1
- */
- float time_min=0;
- int parkingspace_index_id=-1;
- //判断是否是左半库
- if ( terminal_id<=3 && terminal_id>=0 )
- {
- LOG(INFO) << " 左半库 车牌号:"<<car_info.license()<<" 车高:"<<car_info.car_height()<< this;
- //如果是小车 (低于1.55米属于小车)
- if ( car_info.car_height() <= MIN_CAR_HIGH )
- {
- //获取所有小型车位信息
- message::Parkspace_allocation_data_msg all_min_parkspace_data;
- Error_manager t_error = m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_min_parkspace_data, message::Parkspace_type::MIN_PARKINGSPACE);
- if (t_error != SUCCESS)
- {
- return t_error;
- }
- //遍历所有小型车位
- int i = 0;
- while (i < all_min_parkspace_data.parkspace_info_size())
- {
- int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
- //筛选出一号车库的小型车位(第一优先级)的空车位
- if (room_id %15 <= 7 && room_id %15 >= 1
- && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time = (terminal_id+2) * TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_min_parkspace_data.parkspace_info(i).parkingspace_floor_id() - 2) *all_min_parkspace_data.parkspace_info(i).parkingspace_height() /THE_ELEVATOR_SPEED +ELEVATOR_MOVEMENT_TIME +
- (all_min_parkspace_data.parkspace_info(i).parkingspace_index_id() % 15) *all_min_parkspace_data.parkspace_info(i).parkingspace_width() / MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if (time_min == 0)
- {
- time_min = time;
- parkingspace_index_id = i;
- }
- else
- {
- if ( time < time_min )
- {
- time_min = time;
- parkingspace_index_id = i;
- }
- }
- }
- ++i;
- }
- //判断小车车位(第一优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- else
- {
- LOG(INFO) << " 小车车位未找到合适车位 "<< this;
- }
- }
- if ( car_info.car_height() <= MID_CAR_HIGH )
- {
- //第二优先级(中型车位)
- //获取中型车位
- message::Parkspace_allocation_data_msg all_mid_parkspace_data;
- Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_mid_parkspace_data,message::Parkspace_type::MID_PARKINGSPACE);
- if(t_error != SUCCESS)
- {
- return t_error;
- }
- int i=0;
- while(i<all_mid_parkspace_data.parkspace_info_size())
- {
- //三楼的中型车位有单独的中跑车停车 且机械臂可以直接抓车到三楼
- int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
- if ( room_id >= 19 && room_id <= 22)
- {
- //只需计算端口到车位的距离
- // 三楼中型用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
- float t_min=abs( room_id%15*all_mid_parkspace_data.parkspace_info(i).parkingspace_width()-(terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- else
- {
- if ( t_min<time_min )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- }
- }
- //如果是六楼的中型车位
- if ( room_id <= 67 && room_id >=61
- && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time=(terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_mid_parkspace_data.parkspace_info(i).parkingspace_floor_id()-2)*all_mid_parkspace_data.parkspace_info(i).parkingspace_height()/THE_ELEVATOR_SPEED + ELEVATOR_MOVEMENT_TIME +
- all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id()%15*all_mid_parkspace_data.parkspace_info(i).parkingspace_width()/MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- else
- {
- if ( time<time_min )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- }
- }
- ++i;
- }
- //判断中型车位(第二优先级)是否有找到合适的空车位 如果找到了 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- else
- {
- LOG(INFO) << " 中车车位未找到合适车位 "<< this;
- }
- }
- if ( car_info.car_height() <= BIG_CAR_HIGH )
- {
- //第三优先级(大型车位)
- //获取大型车位
- message::Parkspace_allocation_data_msg all_big_parkspace_data;
- Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_big_parkspace_data,message::Parkspace_type::BIG_PARKINGSPACE);
- if(t_error != SUCCESS)
- {
- return t_error;
- }
- int i=0;
- while(i<all_big_parkspace_data.parkspace_info_size())
- {
- int room_id=all_big_parkspace_data.parkspace_info(i).parkingspace_index_id();
- //筛选出一号车库的大型车位
- if ( room_id%15 <= 7 && room_id %15 >=1
- && all_big_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
- {
- //如果是二楼车 则特殊处理
- if ( all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id() == 2 )
- {
- // 二楼用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
- float t_min=abs( room_id%15*all_big_parkspace_data.parkspace_info(i).parkingspace_width() - (terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- else
- {
- if ( t_min<time_min )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- }
- }
- else
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time=(terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id()-2)*all_big_parkspace_data.parkspace_info(i).parkingspace_height()/THE_ELEVATOR_SPEED + ELEVATOR_MOVEMENT_TIME +
- all_big_parkspace_data.parkspace_info(i).parkingspace_index_id()%15*all_big_parkspace_data.parkspace_info(i).parkingspace_width()/MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- else
- {
- if ( time<time_min )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- }
- }
- }
- ++i;
- }
- //判断大型车位(第三优先级)是否有找到合适的空车位 如果找到了 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_big_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- else
- {
- return Error_manager(Error_code::PARKSPACE_ALLOCATOR_ALLOCATE_FAILED, Error_level::MINOR_ERROR,
- " query_the_optimal_parkspace_serverA error ");
- }
- }
- }
- //判断是否是右半库
- else if(terminal_id<=6 && terminal_id>=4)
- {
- //如果是小车 (低于1.55米属于小车)
- if ( car_info.car_height() <= MIN_CAR_HIGH )
- {
- //获取所有小型车位信息
- message::Parkspace_allocation_data_msg all_min_parkspace_data;
- Error_manager t_error = m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_min_parkspace_data, message::Parkspace_type::MIN_PARKINGSPACE);
- if (t_error != SUCCESS)
- {
- return t_error;
- }
- //遍历所有小型车位
- int i = 0;
- while (i < all_min_parkspace_data.parkspace_info_size())
- {
- int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
- //筛选出一号车库的小型车位(第一优先级)的空车位
- if (room_id <= 165 && room_id >= 53
- && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time = (6-terminal_id+2)* TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_min_parkspace_data.parkspace_info(i).parkingspace_floor_id() - 2) *all_min_parkspace_data.parkspace_info(i).parkingspace_height() /THE_ELEVATOR_SPEED +ELEVATOR_MOVEMENT_TIME +
- ( (15-all_min_parkspace_data.parkspace_info(i).parkingspace_index_id() % 15)==15?0:(15-all_min_parkspace_data.parkspace_info(i).parkingspace_index_id() % 15) ) *all_min_parkspace_data.parkspace_info(i).parkingspace_width() / MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if (time_min == 0)
- {
- time_min = time;
- parkingspace_index_id = i;
- }
- else
- {
- if ( time < time_min )
- {
- time_min = time;
- parkingspace_index_id = i;
- }
- }
- }
- ++i;
- }
- //判断小车车位(第一优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- }
- if ( car_info.car_height() <= MID_CAR_HIGH )
- {
- //第二优先级(中型车位)
- //获取中型车位
- message::Parkspace_allocation_data_msg all_mid_parkspace_data;
- Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_mid_parkspace_data,message::Parkspace_type::MID_PARKINGSPACE);
- if(t_error != SUCCESS)
- {
- return t_error;
- }
- int i=0;
- while(i<all_mid_parkspace_data.parkspace_info_size())
- {
- int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
- //三楼的中型车位有单独的中跑车停车 且机械臂可以直接抓车到三楼
- if ( room_id >= 23 && room_id <= 27 )
- {
- //只需计算端口到车位的距离
- // 三楼中型车位用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
- float t_min=abs( ((15-room_id%15)==15?0:(15-room_id%15))*all_mid_parkspace_data.parkspace_info(i).parkingspace_width()-(6-terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- else
- {
- if ( t_min<time_min )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- }
- }
- if ( room_id <= 75 && room_id >= 68
- && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_mid_parkspace_data.parkspace_info(i).parkingspace_floor_id()-2)*all_mid_parkspace_data.parkspace_info(i).parkingspace_height()/THE_ELEVATOR_SPEED + ELEVATOR_MOVEMENT_TIME +
- ( ((15-all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id()%15) ==15?0:(15-all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id()%15)) )*all_mid_parkspace_data.parkspace_info(i).parkingspace_width()/MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- else
- {
- if ( time<time_min )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- }
- }
- ++i;
- }
- //判断中型车位(第二优先级)是否有找到合适的空车位 如果找到了 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- }
- if ( car_info.car_height() <= BIG_CAR_HIGH )
- {
- //第三优先级(大型车位)
- //获取大型车位
- message::Parkspace_allocation_data_msg all_big_parkspace_data;
- Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_big_parkspace_data,message::Parkspace_type::BIG_PARKINGSPACE);
- if(t_error != SUCCESS)
- {
- return t_error;
- }
- int i=0;
- while(i<all_big_parkspace_data.parkspace_info_size())
- {
- int room_id=all_big_parkspace_data.parkspace_info(i).parkingspace_index_id();
- //筛选出二号车库的大型车位的空车位
- if ( room_id %15 >=8 && room_id %15 <=14 || room_id %15 == 0
- && all_big_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
- {
- //如果是二楼车 则特殊处理
- if ( all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id() == 2 )
- {
- // 二楼用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
- float t_min=abs( ((15-room_id%15)==15?0:(15-room_id%15))*all_big_parkspace_data.parkspace_info(i).parkingspace_width()-(6-terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- else
- {
- if ( t_min<time_min )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- }
- }
- else
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id()-2)*all_big_parkspace_data.parkspace_info(i).parkingspace_height()/THE_ELEVATOR_SPEED + ELEVATOR_MOVEMENT_TIME +
- ( ((15-all_big_parkspace_data.parkspace_info(i).parkingspace_index_id()%15) ==15?0:(15-all_big_parkspace_data.parkspace_info(i).parkingspace_index_id()%15)) )*all_big_parkspace_data.parkspace_info(i).parkingspace_width()/MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- else
- {
- if ( time<time_min )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- }
- }
- }
- ++i;
- }
- //判断大型车位(第三优先级)是否有找到合适的空车位 如果找到了 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_big_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- else
- {
- return Error_manager(Error_code::PARKSPACE_ALLOCATOR_ALLOCATE_FAILED, Error_level::MINOR_ERROR,
- " query_the_optimal_parkspace_serverA error ");
- }
- }
- }
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- "query_the_optimal_parkspace_serverA error ");
- }
- //找到一个最优车位---方案二
- //找左侧车位
- Error_manager Parkspace_manager::query_the_optimal_parkspace_serverB1(message::Parkspace_info &parkspace_info,message::Car_info car_info,int terminal_id)
- {
- //车辆高大于1550mm分在四楼以下,小于1550mm分在四楼以上
- /*
- * K=j%15
- * P=i%6
- * S=15*d-(j%15)*(2*d-1)+[6*d-(i%6)(2*d-1)]*W W>1
- */
- float time_min=0;
- int parkingspace_index_id=-1;
- //找左侧车位
- //如果是小车 (低于1.55米属于小车)
- if ( car_info.car_height() <= MIN_CAR_HIGH )
- {
- //获取所有小型车位信息
- message::Parkspace_allocation_data_msg all_min_parkspace_data;
- Error_manager t_error = m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_min_parkspace_data, message::Parkspace_type::MIN_PARKINGSPACE);
- if (t_error != SUCCESS)
- {
- return t_error;
- }
- //遍历所有小型车位
- int i = 0;
- while (i < all_min_parkspace_data.parkspace_info_size())
- {
- int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
- //筛选出小型车位的中间部分(第一优先级)的空车位
- if (room_id % 15 <= 7 && room_id % 15 >= 4
- && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time = (terminal_id+2) * TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_min_parkspace_data.parkspace_info(i).parkingspace_floor_id() - 2) *all_min_parkspace_data.parkspace_info(i).parkingspace_height() /THE_ELEVATOR_SPEED +ELEVATOR_MOVEMENT_TIME +
- (all_min_parkspace_data.parkspace_info(i).parkingspace_index_id() % 15) *all_min_parkspace_data.parkspace_info(i).parkingspace_width() / MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if (time_min == 0)
- {
- time_min = time;
- parkingspace_index_id = i;
- }
- else
- {
- if ( time<time_min )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- }
- }
- ++i;
- }
- //判断小车车位中间部分(第一优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- else
- {
- //如果没找到则找小车车位第二优先级 (侧边)
- int i = 0;
- while (i < all_min_parkspace_data.parkspace_info_size())
- {
- int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
- if (room_id % 15 <= 3 && room_id % 15 >= 1
- && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time = (terminal_id+2) * TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_min_parkspace_data.parkspace_info(i).parkingspace_floor_id() - 2) *all_min_parkspace_data.parkspace_info(i).parkingspace_height() /THE_ELEVATOR_SPEED +ELEVATOR_MOVEMENT_TIME +
- (all_min_parkspace_data.parkspace_info(i).parkingspace_index_id() % 15) *all_min_parkspace_data.parkspace_info(i).parkingspace_width() / MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if (time_min == 0)
- {
- time_min = time;
- parkingspace_index_id = i;
- }
- else
- {
- if ( time < time_min )
- {
- time_min = time;
- parkingspace_index_id = i;
- }
- }
- }
- ++i;
- }
- //判断小车车位侧边部分(第二优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- }
- }
- if ( car_info.car_height() <= MID_CAR_HIGH )
- {
- //找中型车位
- //获取中型车位
- message::Parkspace_allocation_data_msg all_mid_parkspace_data;
- Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_mid_parkspace_data,message::Parkspace_type::MID_PARKINGSPACE);
- if(t_error != SUCCESS)
- {
- return t_error;
- }
- int i=0;
- while(i<all_mid_parkspace_data.parkspace_info_size())
- {
- int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
- //如果是六楼的中型车位的中间部分(第一优先级)
- if ( room_id >=64 && room_id <= 67
- && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time=(terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_mid_parkspace_data.parkspace_info(i).parkingspace_floor_id()-2)*all_mid_parkspace_data.parkspace_info(i).parkingspace_height()/THE_ELEVATOR_SPEED + ELEVATOR_MOVEMENT_TIME +
- (all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id()%15)*all_mid_parkspace_data.parkspace_info(i).parkingspace_width()/MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- else
- {
- if ( time<time_min )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- }
- }
- ++i;
- }
- //判断中型车位的中间部分(第一优先级)是否有找到合适的空车位 如果找到了 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- else
- {
- //如果没找到则找侧边中型车位(第二优先级)
- int i=0;
- while(i<all_mid_parkspace_data.parkspace_info_size())
- {
- int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
- //如果是六楼的中型车位的侧边(第二优先级)
- if ( room_id >=61 && room_id <= 63
- && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time=(terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_mid_parkspace_data.parkspace_info(i).parkingspace_floor_id()-2)*all_mid_parkspace_data.parkspace_info(i).parkingspace_height()/THE_ELEVATOR_SPEED + ELEVATOR_MOVEMENT_TIME +
- all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id()%15*all_mid_parkspace_data.parkspace_info(i).parkingspace_width()/MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- else
- {
- if ( time<time_min )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- }
- }
- ++i;
- }
- //判断中型车位侧边(第二优先级)是否有找到合适的空车位 如果找到了 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- }
- }
- if ( car_info.car_height() <= BIG_CAR_HIGH )
- {
- //找大型车位
- //获取大型车位
- message::Parkspace_allocation_data_msg all_big_parkspace_data;
- Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_big_parkspace_data,message::Parkspace_type::BIG_PARKINGSPACE);
- if(t_error != SUCCESS)
- {
- return t_error;
- }
- int i=0;
- while(i<all_big_parkspace_data.parkspace_info_size())
- {
- int room_id=all_big_parkspace_data.parkspace_info(i).parkingspace_index_id();
- //筛选出一号车库的小型车位的靠近电梯的左侧三排(第二优先级)的空车位
- if ( room_id %15 == 1 || room_id %15 == 2 || room_id %15 == 3 || room_id>=34 && room_id<=37
- && all_big_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
- {
- //如果是二楼车 则特殊处理
- if ( all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id() == 2 )
- {
- // 二楼用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
- float t_min=abs( room_id%15*all_big_parkspace_data.parkspace_info(i).parkingspace_width()-(terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- else
- {
- if ( t_min<time_min )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- }
- }
- else
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time=(terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id()-2)*all_big_parkspace_data.parkspace_info(i).parkingspace_height()/THE_ELEVATOR_SPEED + ELEVATOR_MOVEMENT_TIME +
- all_big_parkspace_data.parkspace_info(i).parkingspace_index_id()%15*all_big_parkspace_data.parkspace_info(i).parkingspace_width()/MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- else
- {
- if ( time<time_min )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- }
- }
- }
- ++i;
- }
- //判断大型车位是否有找到合适的空车位 如果找到了 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_big_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- else
- {
- //如果没找到则代表左侧没找到合适车位
- parkspace_info.set_parkingspace_index_id(-1);
- return Error_manager(Error_code::PARKSPACE_ALLOCATOR_ALLOCATE_FAILED, Error_level::NEGLIGIBLE_ERROR,
- "query_the_optimal_parkspace_serverB1 error ");
- }
- }
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- " query_the_optimal_parkspace_serverB1 error ");
- }
- //找右侧车位
- Error_manager Parkspace_manager::query_the_optimal_parkspace_serverB2(message::Parkspace_info &parkspace_info,message::Car_info car_info,int terminal_id)
- {
- //车辆高大于1550mm分在四楼以下,小于1550mm分在四楼以上
- /*
- * K=j%15
- * P=i%6
- * S=15*d-(j%15)*(2*d-1)+[6*d-(i%6)(2*d-1)]*W W>1
- */
- float time_min=0;
- int parkingspace_index_id=-1;
- //找右侧车位
- //如果是小车 (低于1.55米属于小车)
- if ( car_info.car_height() <= MIN_CAR_HIGH )
- {
- //获取所有小型车位信息
- message::Parkspace_allocation_data_msg all_min_parkspace_data;
- Error_manager t_error = m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_min_parkspace_data, message::Parkspace_type::MIN_PARKINGSPACE);
- if (t_error != SUCCESS)
- {
- return t_error;
- }
- //遍历所有小型车位
- int i = 0;
- while (i < all_min_parkspace_data.parkspace_info_size())
- {
- int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
- //筛选出小型车位的中间部分(第一优先级)的空车位
- if (room_id % 15 >= 8 && room_id % 15 <= 12
- && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time = (6-terminal_id+2) * TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_min_parkspace_data.parkspace_info(i).parkingspace_floor_id() - 2) *all_min_parkspace_data.parkspace_info(i).parkingspace_height() /THE_ELEVATOR_SPEED +ELEVATOR_MOVEMENT_TIME +
- ( (15-all_min_parkspace_data.parkspace_info(i).parkingspace_index_id() % 15)==15?0:(15-all_min_parkspace_data.parkspace_info(i).parkingspace_index_id() % 15) ) *all_min_parkspace_data.parkspace_info(i).parkingspace_width() / MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if (time_min == 0)
- {
- time_min = time;
- parkingspace_index_id = i;
- }
- else
- {
- if ( time<time_min )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- }
- }
- ++i;
- }
- //判断小车车位中间部分(第一优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- else
- {
- //如果没找到则找小车车位第二优先级 (侧边)
- int i = 0;
- while (i < all_min_parkspace_data.parkspace_info_size())
- {
- int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
- //筛选出一号车库的小型车位的中间部分(第一优先级)的空车位
- if (room_id % 15 == 13 || room_id % 15 == 14 || room_id % 15 == 0
- && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time = (6-terminal_id+2) * TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_min_parkspace_data.parkspace_info(i).parkingspace_floor_id() - 2) *all_min_parkspace_data.parkspace_info(i).parkingspace_height() /THE_ELEVATOR_SPEED +ELEVATOR_MOVEMENT_TIME +
- ( (15-all_min_parkspace_data.parkspace_info(i).parkingspace_index_id() % 15)==15?0:(15-all_min_parkspace_data.parkspace_info(i).parkingspace_index_id() % 15) ) *all_min_parkspace_data.parkspace_info(i).parkingspace_width() / MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if (time_min == 0)
- {
- time_min = time;
- parkingspace_index_id = i;
- }
- else
- {
- if ( time < time_min )
- {
- time_min = time;
- parkingspace_index_id = i;
- }
- }
- }
- ++i;
- }
- //判断小车车位侧边部分(第二优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- }
- }
- if ( car_info.car_height() <= MID_CAR_HIGH )
- {
- //找中型车位
- //获取中型车位
- message::Parkspace_allocation_data_msg all_mid_parkspace_data;
- Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_mid_parkspace_data,message::Parkspace_type::MID_PARKINGSPACE);
- if(t_error != SUCCESS)
- {
- return t_error;
- }
- int i=0;
- while(i<all_mid_parkspace_data.parkspace_info_size())
- {
- int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
- //如果是六楼的中型车位的中间部分(第一优先级)
- if ( room_id >= 68 && room_id <= 72
- && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_mid_parkspace_data.parkspace_info(i).parkingspace_floor_id()-2)*all_mid_parkspace_data.parkspace_info(i).parkingspace_height()/THE_ELEVATOR_SPEED + ELEVATOR_MOVEMENT_TIME +
- ( ((15-all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id()%15) ==15?0:(15-all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id()%15)) )*all_mid_parkspace_data.parkspace_info(i).parkingspace_width()/MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- else
- {
- if ( time<time_min )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- }
- }
- ++i;
- }
- //判断中型车位的中间部分(第一优先级)是否有找到合适的空车位 如果找到了 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- else
- {
- //如果没找到则找侧边中型车位(第二优先级)
- int i=0;
- while(i<all_mid_parkspace_data.parkspace_info_size())
- {
- int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
- //如果是六楼的中型车位的侧边(第二优先级)
- if ( room_id >=73 && room_id <= 75
- && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_mid_parkspace_data.parkspace_info(i).parkingspace_floor_id()-2)*all_mid_parkspace_data.parkspace_info(i).parkingspace_height()/THE_ELEVATOR_SPEED + ELEVATOR_MOVEMENT_TIME +
- ( ((15-all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id()%15) ==15?0:(15-all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id()%15)) )*all_mid_parkspace_data.parkspace_info(i).parkingspace_width()/MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- else
- {
- if ( time<time_min )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- }
- }
- ++i;
- }
- //判断中型车位侧边(第二优先级)是否有找到合适的空车位 如果找到了 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- }
- }
- if ( car_info.car_height() <= BIG_CAR_HIGH )
- {
- //找大型车位
- //获取大型车位
- message::Parkspace_allocation_data_msg all_big_parkspace_data;
- Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_big_parkspace_data,message::Parkspace_type::BIG_PARKINGSPACE);
- if(t_error != SUCCESS)
- {
- return t_error;
- }
- int i=0;
- while(i<all_big_parkspace_data.parkspace_info_size())
- {
- int room_id=all_big_parkspace_data.parkspace_info(i).parkingspace_index_id();
- //筛选出大型车位的靠近电梯的右侧三排(第二优先级)的空车位
- if ( room_id %15 == 13 || room_id %15 == 14 || room_id %15 == 0|| room_id>=38 && room_id<=42
- && all_big_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
- {
- //如果是二楼车 则特殊处理
- if ( all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id() == 2 )
- {
- // 二楼用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
- float t_min=abs( ((15-room_id%15)==15?0:(15-room_id%15))*all_big_parkspace_data.parkspace_info(i).parkingspace_width()-(6-terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- else
- {
- if ( t_min<time_min )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- }
- }
- else
- {
- //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
- float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
- (all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id()-2)*all_big_parkspace_data.parkspace_info(i).parkingspace_height()/THE_ELEVATOR_SPEED + ELEVATOR_MOVEMENT_TIME +
- ( (15-all_big_parkspace_data.parkspace_info(i).parkingspace_index_id() % 15)==15?0:(15-all_big_parkspace_data.parkspace_info(i).parkingspace_index_id() % 15) )*all_big_parkspace_data.parkspace_info(i).parkingspace_width()/MEDIUM_CAR_SPEED + UPSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- else
- {
- if ( time<time_min )
- {
- time_min=time;
- parkingspace_index_id=i;
- }
- }
- }
- }
- ++i;
- }
- //判断大型车位是否有找到合适的空车位 如果找到了 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_big_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- else
- {
- //如果没找到则代表右侧没找到合适车位
- parkspace_info.set_parkingspace_index_id(-1);
- return Error_manager(Error_code::PARKSPACE_ALLOCATOR_ALLOCATE_FAILED, Error_level::NEGLIGIBLE_ERROR,
- "query_the_optimal_parkspace_serverB2 error ");
- }
- }
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- " query_the_optimal_parkspace_serverB2 error ");
- }
- //找缓存区车位
- Error_manager Parkspace_manager::query_the_optimal_parkspace_serverB3(message::Parkspace_info &parkspace_info,message::Car_info car_info,int terminal_id)
- {
- //车辆高大于1550mm分在四楼以下,小于1550mm分在四楼以上
- /*
- * K=j%15
- * P=i%6
- * S=15*d-(j%15)*(2*d-1)+[6*d-(i%6)(2*d-1)]*W W>1
- */
- float time_min=0;
- int parkingspace_index_id=-1;
- //找缓存区车位 缓冲区只有中型车位和大型车位
- if ( car_info.car_height() <= MID_CAR_HIGH )
- {
- message::Parkspace_allocation_data_msg all_mid_parkspace_data;
- Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_mid_parkspace_data,message::Parkspace_type::MID_PARKINGSPACE);
- if(t_error != SUCCESS)
- {
- return t_error;
- }
- int i=0;
- while (i < all_mid_parkspace_data.parkspace_info_size())
- {
- //筛选中型车位缓冲区
- int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
- if ( room_id >= 19 && room_id <= 27
- && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::eParkspace_empty)
- {
- //只需计算端口到车位的距离
- // 三楼中型用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
- float t_min=abs( room_id%15*all_mid_parkspace_data.parkspace_info(i).parkingspace_width()-(terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- else
- {
- if ( t_min<time_min )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- }
- }
- ++i;
- }
- //判断缓冲区的中型车位是否有找到合适的空车位 如果找到了 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- }
- if( car_info.car_height() <= BIG_CAR_HIGH )
- {
- message::Parkspace_allocation_data_msg all_big_parkspace_data;
- Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_big_parkspace_data,message::Parkspace_type::BIG_PARKINGSPACE);
- if(t_error != SUCCESS)
- {
- return t_error;
- }
- int i=0;
- while (i < all_big_parkspace_data.parkspace_info_size())
- {
- //筛选中型车位缓冲区
- int room_id=all_big_parkspace_data.parkspace_info(i).parkingspace_index_id();
- if ( room_id >= 4 && room_id <= 12
- && all_big_parkspace_data.parkspace_info(i).parkingspace_status() == message::eParkspace_empty)
- {
- //只需计算端口到车位的距离
- // 二楼中型用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
- float t_min=abs( room_id%15*all_big_parkspace_data.parkspace_info(i).parkingspace_width()-(terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
- if ( time_min == 0 )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- else
- {
- if ( t_min<time_min )
- {
- time_min=t_min;
- parkingspace_index_id=i;
- }
- }
- }
- }
- //判断缓冲区的中型车位是否有找到合适的空车位 如果找到了 直接返回
- if ( time_min!=0 && parkingspace_index_id!=-1 )
- {
- parkspace_info.CopyFrom(all_big_parkspace_data.parkspace_info(parkingspace_index_id));
- return Error_code::SUCCESS;
- }
- else
- {
- //如果没找到则代表缓冲区没找到合适车位
- parkspace_info.set_parkingspace_index_id(-1);
- return Error_manager(Error_code::PARKSPACE_ALLOCATOR_ALLOCATE_FAILED, Error_level::NEGLIGIBLE_ERROR,
- "query_the_optimal_parkspace_serverB2 error ");
- }
- }
- return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
- " query_the_optimal_parkspace_serverB3 error ");
- }
- //分配车位线程函数
- void Parkspace_manager::execute_for_allocate(message::Car_info car_info, int terminal_id, std::string command_key)
- {
- LOG(INFO) << "分配车位 "<<"牌号:"<<car_info.license()<<" command_key="<<command_key; //huli printf 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;
- //分配之前查询车辆是否已经存在
- if(check_car_existence(car_info.license()))
- {
- t_error.set_error_code(PARKSPACE_ALLOCATOR_CAR_ALREADY_EXIST);
- t_error.set_error_level(message::Error_level::MAJOR_ERROR);
- t_error.set_error_description("车辆已存在");
- LOG(ERROR) << car_info.license()<<" 车辆已存在";
- }
- else
- {
- m_parkspace_lock.lock();
- // 如果车辆不存在 则找到一个最优车位(方案A)----车库分为两个独立部分,左库和右库如果1~3号终端来车 则在左库分配车位,4~号终端来车 则在右库分配车位
- error=query_the_optimal_parkspace_serverA(t_allocated_space,car_info,terminal_id);
- // 如果车辆不存在 则找到一个最优车位(方案B)----分配三个车位 左边分配一个 右边分配一个 缓冲区分配一个 最后使用那个由调度决定
- // message::Parkspace_info t_allocated_space_left;
- // message::Parkspace_info t_allocated_space_rigth;
- // message::Parkspace_info t_allocated_space_temporary;
- //
- // error=query_the_optimal_parkspace_serverB1(t_allocated_space_left,car_info,terminal_id);
- // LOG(INFO) << " 左侧车位ID: "<<t_allocated_space_left.parkingspace_index_id();
- // error=query_the_optimal_parkspace_serverB2(t_allocated_space_rigth,car_info,terminal_id);
- // LOG(INFO) << " 右侧车位ID: "<<t_allocated_space_rigth.parkingspace_index_id();
- // error=query_the_optimal_parkspace_serverB3(t_allocated_space_temporary,car_info,terminal_id);
- // LOG(INFO) << " 缓冲区车位ID: "<<t_allocated_space_temporary.parkingspace_index_id();
- //如果车辆不存在则获取一个空车位
- // error=m_parkspace_operating_function.query_one_empty_parkspace(t_allocated_space);
- 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_allocated_space.set_parkingspace_status(message::Parkspace_status::eParkspace_locked);
- t_allocated_space.mutable_car_info()->CopyFrom(car_info);
- // 分配车位后更新数据库中车位状态
- 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_vehicle_status = message::Vehicle_status::eVehicle_parking;
- error=m_parkspace_operating_function.update_vehicle_with_parkspace(t_allocated_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());
- 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.car_info().license();
- }
- }
- }
- m_parkspace_lock.unlock();
- }
- 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_allocated_space_info()->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_search(message::Car_info car_info,std::string command_key)
- {
- LOG(INFO) << "查询车位 "<<"牌号:"<<car_info.license()<<" command_key="<<command_key; //huli printf command_key
- //根据车辆凭证信息查询车辆位置
- 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_info parkspace_info;
- Error_manager error=m_parkspace_operating_function.query_one_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) << "车辆"<<parkspace_info.car_info().license()<<" 在"<<parkspace_info.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_car_position()->CopyFrom(t_car_position);
- 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_info space_info, std::string command_key)
- {
- LOG(INFO) << "释放车位 "<<"牌号:"<<space_info.car_info().license()<<" command_key="<<command_key;
- //根据车位信息定位待释放车位位置,车辆凭证号用于校验
- 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;
- //找到待释放的车位
- m_parkspace_lock.lock();
- Error_manager error=m_parkspace_operating_function.query_one_parkspace_with_parkspace_id(space_info.parkingspace_index_id(),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());
- LOG(ERROR) << "释放车位失败 "<<error.to_string();
- }
- else if(!t_release_space.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
- {
- t_release_space.set_parkingspace_status(message::Parkspace_status::eParkspace_empty);
- error = m_parkspace_operating_function.update_parkspace_data(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());
- LOG(ERROR) << "释放车位 更新数据库车位数据失败 " << error.to_string();
- }
- else
- {
- message::Vehicle_status t_vehicle_status = message::Vehicle_status::eVehicle_idle;
- error = m_parkspace_operating_function.update_vehicle_with_parkspace(t_release_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());
- 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) << "第:" << t_release_space.parkingspace_index_id() << "号位 车辆: " << t_release_space.car_info().license()
- << " 释放成功";
- //获取预约ID
- int t_park_record_id;
- error = m_parkspace_operating_function.query_vehicle(t_release_space.car_info().license(),
- t_vehicle_status, t_park_record_id);
- 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 if (t_vehicle_status == message::Vehicle_status::eVehicle_idle)
- {
- 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_release_space.set_leave_time(my_time_buf);
- error = m_parkspace_operating_function.update_parking_record(t_release_space, t_park_record_id);
- 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();
- }
- }
- }
- }
- }
- m_parkspace_lock.unlock();
- 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_space_info()->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_info space_info, std::string command_key)
- {
- LOG(INFO) << "手动 "<<"牌号:"<<space_info.car_info().license()<<" 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;
- Error_manager error;
- // id 9999为特殊情况,复位所有车位状态
- if (space_info.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(INFO) << "手动 数据库更新车位失败 "<<error.to_string();
- }
- else
- {
- t_error.set_error_code(SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- 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_update_space_info()->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();
- }
- //确认分配车位线程函数
- void Parkspace_manager::execute_for_confirm_alloc(message::Parkspace_info space_info, std::string command_key)
- {
- LOG(INFO) << "确认分配 " << "牌号:" << space_info.car_info().license() << " command_key=" << command_key;
- //根据车位信息定位待确认占用车位
- 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;
- Error_manager error;
- //获取确认分配的车位
- m_parkspace_lock.lock();
- error = m_parkspace_operating_function.query_one_parkspace_with_parkspace_id(space_info.parkingspace_index_id(),
- t_confirm_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) << "确认分配 查询车位信息失败 " << error.to_string();
- }
- else if (!t_confirm_space.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
- {
- //校验车位状态是否为被锁定
- if (t_confirm_space.parkingspace_status() != message::Parkspace_status::eParkspace_locked)
- {
- t_error.set_error_code(DB_QUERY_DATA_FAILED);
- t_error.set_error_level(message::Error_level::MINOR_ERROR);
- t_error.set_error_description("查询车位状态错误");
- LOG(ERROR) << "确认分配 查询车位状态错误 " << error.to_string();
- }
- else
- {
- //修改车位状态为占用
- t_confirm_space.set_parkingspace_status(message::Parkspace_status::eParkspace_occupied);
- error = m_parkspace_operating_function.update_parkspace_data(t_confirm_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(INFO) << "确认分配 数据库更新车位失败 " << error.to_string();
- }
- 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)
- {
- 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();
- }
- t_error.set_error_code(SUCCESS);
- t_error.set_error_level(message::Error_level::NORMAL);
- LOG(INFO) << "第:" << t_confirm_space.parkingspace_index_id() << "号位 车辆: " << t_confirm_space.car_info().license()<< " 确认占用成功";
- //插入停车记录
- 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);
- 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();
- }
- //查询停车记录的ID
- int t_record_id;
- error = m_parkspace_operating_function.query_parking_record(t_confirm_space,t_record_id);
- 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) << "查询停车记录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 )
- {
- t_error.set_error_code(error.get_error_code());
- t_error.set_error_level((message::Error_level) error.get_error_level());
- LOG(ERROR) << "更新停车记录ID失败: " << error.to_string();
- }
- }
- }
- }
- m_parkspace_lock.unlock();
- 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_confirm_alloc_space_info()->CopyFrom(t_confirm_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;
- }
|