parkspace_manager.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  1. /*
  2. * @Description: 车位分配算法模块,使用单例模式,接收外部请求并通过调用通信块接口发送反馈
  3. * @Author: yct
  4. * @Date: 2020-07-10 11:02:40
  5. * @LastEditTime: 2020-08-06 10:32:28
  6. * @LastEditors: yct
  7. */
  8. #include "parkspace_manager.h"
  9. #include "parkspace_communicator.h"
  10. Parkspace_manager::Parkspace_manager()
  11. {
  12. }
  13. Parkspace_manager::~Parkspace_manager()
  14. {
  15. parkspace_manager_uninit();
  16. }
  17. //初始化
  18. Error_manager Parkspace_manager::parkspace_manager_init(int threads_size)
  19. {
  20. // 初始化任务处理线程池
  21. m_thread_pool.thread_pool_init(threads_size);
  22. m_parkspace_manager_satus = eParkspace_manager_normal;
  23. return Error_code::SUCCESS;
  24. }
  25. //反初始化
  26. Error_manager Parkspace_manager::parkspace_manager_uninit()
  27. {
  28. m_thread_pool.thread_pool_uninit();
  29. m_parkspace_manager_satus = eParkspace_manager_unknown;
  30. return Error_code::SUCCESS;
  31. }
  32. //检查执行者的状态, 判断能否处理这条消息,
  33. Error_manager Parkspace_manager::check_status()
  34. {
  35. if(get_parkspace_manage_status() != eParkspace_manager_normal)
  36. {
  37. return Error_manager(Error_code::DB_MANAGER_STATUS_ERROR, Error_level::MAJOR_ERROR,
  38. " Parkspace_manager::check_executer error ");
  39. }
  40. return SUCCESS;
  41. }
  42. //处理消息的执行函数
  43. Error_manager Parkspace_manager::execute_msg(Communication_message* p_msg)
  44. {
  45. if ( p_msg == nullptr )
  46. {
  47. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  48. " POINTER IS NULL ");
  49. }
  50. switch ( p_msg->get_message_type() )
  51. {
  52. ///车位分配请求消息
  53. case Communication_message::eParkspace_allocation_request_msg:
  54. {
  55. message::Parkspace_allocation_request_msg request;
  56. bool result = request.ParseFromString(p_msg->get_message_buf());
  57. LOG(INFO)<<"allocation request, car license: "<<request.car_info().license();
  58. if(!result)
  59. {
  60. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_PARSE_ERROR, Error_level::MINOR_ERROR,
  61. " message::Parkspace_allocation_request_msg ParseFromString error ");
  62. }
  63. // std::cout<<"分配:"<<request.DebugString()<<std::endl;
  64. //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
  65. m_thread_pool.enqueue(&Parkspace_manager::execute_for_allocate, this,
  66. request.car_info(), request.terminal_id(), request.command_key());
  67. return SUCCESS;
  68. }
  69. // 车位查询请求消息
  70. case Communication_message::eParkspace_search_request_msg:
  71. {
  72. message::Parkspace_search_request_msg request;
  73. bool result = request.ParseFromString(p_msg->get_message_buf());
  74. LOG(INFO)<<"search request, car license: "<<request.car_info().license();
  75. if(!result)
  76. {
  77. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_PARSE_ERROR, Error_level::MINOR_ERROR,
  78. " message::Parkspace_search_request_msg ParseFromString error ");
  79. }
  80. //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
  81. // std::cout<<"查询:"<<request.DebugString()<<std::endl;
  82. m_thread_pool.enqueue(&Parkspace_manager::execute_for_search, this, request.car_info(), request.command_key());
  83. return SUCCESS;
  84. }
  85. // 车位释放请求消息
  86. case Communication_message::eParkspace_release_request_msg:
  87. {
  88. message::Parkspace_release_request_msg request;
  89. bool result = request.ParseFromString(p_msg->get_message_buf());
  90. LOG(INFO)<<"release request, parkspace id: "<<request.release_space_info().parkingspace_index_id();
  91. if(!result)
  92. {
  93. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_PARSE_ERROR, Error_level::MINOR_ERROR,
  94. " message::Parkspace_release_request_msg ParseFromString error ");
  95. }
  96. //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
  97. // std::cout<<"释放:"<<request.DebugString()<<std::endl;
  98. m_thread_pool.enqueue(&Parkspace_manager::execute_for_release, this, request.release_space_info(), request.command_key());
  99. return SUCCESS;
  100. }
  101. // 车位手动更新请求消息
  102. case Communication_message::eParkspace_force_update_request_msg:
  103. {
  104. message::Parkspace_force_update_request_msg request;
  105. bool result = request.ParseFromString(p_msg->get_message_buf());
  106. LOG(INFO)<<"force update request, parkspace id: "<<request.update_space_info().parkingspace_index_id();
  107. if(!result)
  108. {
  109. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_PARSE_ERROR, Error_level::MINOR_ERROR,
  110. " message::Parkspace_force_update_request_msg ParseFromString error ");
  111. }
  112. //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
  113. // std::cout<<"手动:"<<request.DebugString()<<std::endl;
  114. m_thread_pool.enqueue(&Parkspace_manager::execute_for_force_update, this, request.update_space_info(), request.command_key());
  115. return SUCCESS;
  116. }
  117. // 车位确认占用请求消息
  118. case Communication_message::eParkspace_confirm_alloc_request_msg:
  119. {
  120. message::Parkspace_confirm_alloc_request_msg request;
  121. bool result = request.ParseFromString(p_msg->get_message_buf());
  122. LOG(INFO)<<"confirm alloc request, parkspace id: "<<request.confirm_space_info().parkingspace_index_id();
  123. if(!result)
  124. {
  125. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_PARSE_ERROR, Error_level::MINOR_ERROR,
  126. " message::Parkspace_force_update_request_msg ParseFromString error ");
  127. }
  128. m_thread_pool.enqueue(&Parkspace_manager::execute_for_confirm_alloc, this, request.confirm_space_info(), request.command_key());
  129. return SUCCESS;
  130. }
  131. //车位数据刷新请求
  132. case Communication_message::eParkspace_refresh_request_msg:
  133. {
  134. return send_parkspace_data();
  135. }
  136. }
  137. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_REQUEST_TYPE_ERROR, Error_level::MINOR_ERROR,
  138. " fun error ");
  139. }
  140. Error_manager Parkspace_manager::encapsulating_heartbeat_messages()
  141. {
  142. //封装心跳消息
  143. Database_controller::Database_controller_status t_database_controller_status;
  144. Parkspace_manager::Parkspace_manager_satus t_parkspace_manager_satus;
  145. t_database_controller_status = Database_controller::get_instance_pointer()->get_database_controller_status();
  146. t_parkspace_manager_satus = Parkspace_manager::get_instance_pointer()->get_parkspace_manage_status();
  147. //车位信息消息赋值
  148. message::Parkspace_allocation_status_msg t_parkspace_status_msg;
  149. message::Base_info t_base_info;
  150. message::Error_manager t_error;
  151. t_base_info.set_msg_type(message::Message_type::eParkspace_allocation_status_msg);
  152. t_base_info.set_timeout_ms(5000);
  153. t_base_info.set_sender(message::Communicator::eParkspace);
  154. t_base_info.set_receiver(message::Communicator::eMain);
  155. t_error.set_error_code(0);
  156. t_parkspace_status_msg.mutable_base_info()->CopyFrom(t_base_info);
  157. t_parkspace_status_msg.mutable_error_manager()->CopyFrom(t_error);
  158. t_parkspace_status_msg.set_database_controller_status((message::Database_controller_status)t_database_controller_status);
  159. t_parkspace_status_msg.set_parkspace_manager_satus((message::Parkspace_manager_satus)t_parkspace_manager_satus);
  160. return Parkspace_communicator::get_instance_references().encapsulate_msg(t_parkspace_status_msg.SerializeAsString());
  161. }
  162. //判断是否为待机,如果已经准备好,则可以执行任务。
  163. bool Parkspace_manager::is_ready()
  164. {
  165. if ( m_parkspace_manager_satus == eParkspace_manager_normal && m_thread_pool.thread_is_full_load() == false )
  166. {
  167. return true;
  168. }
  169. else
  170. {
  171. return false;
  172. }
  173. }
  174. Parkspace_manager::Parkspace_manager_satus Parkspace_manager::get_parkspace_manage_status()
  175. {
  176. return m_parkspace_manager_satus;
  177. }
  178. // 检查车辆是否已存在,通常分配前调用
  179. bool Parkspace_manager::check_car_existence(std::string license)
  180. {
  181. message::Parkspace_info parkspaceInfo;
  182. //获取车辆状态
  183. Error_manager error=m_parkspace_operating_function.query_one_parkspace_with_license(license,parkspaceInfo);
  184. //如果车辆处于停车中或者已入库 则车辆已存在
  185. if(!parkspaceInfo.has_car_info())
  186. {
  187. return false;
  188. }
  189. return true;
  190. }
  191. Error_manager Parkspace_manager::send_parkspace_data()
  192. {
  193. message::Parkspace_allocation_data_msg all_parkspace_data;
  194. Error_manager t_error=m_parkspace_operating_function.get_all_parkspace_info(all_parkspace_data);
  195. if(t_error != SUCCESS)
  196. {
  197. return t_error;
  198. }
  199. message::Base_info t_base_info;
  200. message::Error_manager error;
  201. t_base_info.set_msg_type(message::Message_type::eParkspace_allocation_data_msg);
  202. t_base_info.set_timeout_ms(5000);
  203. t_base_info.set_sender(message::Communicator::eParkspace);
  204. t_base_info.set_receiver(message::Communicator::eMain);
  205. all_parkspace_data.mutable_base_info()->CopyFrom(t_base_info);
  206. error.set_error_code(0);
  207. all_parkspace_data.mutable_error_manager()->CopyFrom(error);
  208. return Parkspace_communicator::get_instance_references().encapsulate_msg(all_parkspace_data.SerializeAsString());
  209. }
  210. //找到一个最优车位---方案一
  211. Error_manager Parkspace_manager::query_the_optimal_parkspace_serverA(message::Parkspace_info &parkspace_info,message::Car_info car_info,int terminal_id)
  212. {
  213. //车辆高大于1550mm分在四楼以下,小于1550mm分在四楼以上
  214. /*
  215. * K=j%15
  216. * P=i%6
  217. * S=15*d-(j%15)*(2*d-1)+[6*d-(i%6)(2*d-1)]*W W>1
  218. */
  219. float time_min=0;
  220. int parkingspace_index_id=-1;
  221. //判断是否是左半库
  222. if ( terminal_id<=3 && terminal_id>=0 )
  223. {
  224. LOG(INFO) << " 左半库 车牌号:"<<car_info.license()<<" 车高:"<<car_info.car_height()<< this;
  225. //如果是小车 (低于1.55米属于小车)
  226. if ( car_info.car_height() <= MIN_CAR_HIGH )
  227. {
  228. //获取所有小型车位信息
  229. message::Parkspace_allocation_data_msg all_min_parkspace_data;
  230. Error_manager t_error = m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_min_parkspace_data, message::Parkspace_type::MIN_PARKINGSPACE);
  231. if (t_error != SUCCESS)
  232. {
  233. return t_error;
  234. }
  235. //遍历所有小型车位
  236. int i = 0;
  237. while (i < all_min_parkspace_data.parkspace_info_size())
  238. {
  239. int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
  240. //筛选出一号车库的小型车位(第一优先级)的空车位
  241. if (room_id %15 <= 8 && room_id %15 >= 1
  242. && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
  243. {
  244. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  245. float time = terminal_id * TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  246. (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 +
  247. (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;
  248. if (time_min == 0)
  249. {
  250. time_min = time;
  251. parkingspace_index_id = i;
  252. }
  253. else
  254. {
  255. if ( time < time_min )
  256. {
  257. time_min = time;
  258. parkingspace_index_id = i;
  259. }
  260. }
  261. }
  262. ++i;
  263. }
  264. //判断小车车位(第一优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
  265. if ( time_min!=0 && parkingspace_index_id!=-1 )
  266. {
  267. parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
  268. return Error_code::SUCCESS;
  269. }
  270. else
  271. {
  272. LOG(INFO) << " 小车车位未找到合适车位 "<< this;
  273. }
  274. }
  275. if ( car_info.car_height() <= MID_CAR_HIGH )
  276. {
  277. //第二优先级(中型车位)
  278. //获取中型车位
  279. message::Parkspace_allocation_data_msg all_mid_parkspace_data;
  280. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_mid_parkspace_data,message::Parkspace_type::MID_PARKINGSPACE);
  281. if(t_error != SUCCESS)
  282. {
  283. return t_error;
  284. }
  285. int i=0;
  286. while(i<all_mid_parkspace_data.parkspace_info_size())
  287. {
  288. //三楼的中型车位有单独的中跑车停车 且机械臂可以直接抓车到三楼
  289. int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
  290. if ( room_id >= 19 && room_id <= 23)
  291. {
  292. //只需计算端口到车位的距离
  293. // 三楼中型用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
  294. float t_min=abs( room_id%15*all_mid_parkspace_data.parkspace_info(i).parkingspace_width()-(6-terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
  295. if ( time_min == 0 )
  296. {
  297. time_min=t_min;
  298. parkingspace_index_id=i;
  299. }
  300. else
  301. {
  302. if ( t_min<time_min )
  303. {
  304. time_min=t_min;
  305. parkingspace_index_id=i;
  306. }
  307. }
  308. }
  309. //如果是六楼的中型车位
  310. if ( room_id <= 68 && room_id >=61
  311. && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  312. {
  313. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  314. float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  315. (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 +
  316. 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;
  317. if ( time_min == 0 )
  318. {
  319. time_min=time;
  320. parkingspace_index_id=i;
  321. }
  322. else
  323. {
  324. if ( time<time_min )
  325. {
  326. time_min=time;
  327. parkingspace_index_id=i;
  328. }
  329. }
  330. }
  331. ++i;
  332. }
  333. //判断中型车位(第二优先级)是否有找到合适的空车位 如果找到了 直接返回
  334. if ( time_min!=0 && parkingspace_index_id!=-1 )
  335. {
  336. parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
  337. return Error_code::SUCCESS;
  338. }
  339. else
  340. {
  341. LOG(INFO) << " 中车车位未找到合适车位 "<< this;
  342. }
  343. }
  344. if ( car_info.car_height() <= BIG_CAR_HIGH )
  345. {
  346. //第三优先级(大型车位)
  347. //获取大型车位
  348. message::Parkspace_allocation_data_msg all_big_parkspace_data;
  349. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_big_parkspace_data,message::Parkspace_type::BIG_PARKINGSPACE);
  350. if(t_error != SUCCESS)
  351. {
  352. return t_error;
  353. }
  354. std::cout << " all_big_parkspace_data.parkspace_info_size() = " << all_big_parkspace_data.parkspace_info_size() << std::endl;
  355. int i=0;
  356. while(i<all_big_parkspace_data.parkspace_info_size())
  357. {
  358. int room_id=all_big_parkspace_data.parkspace_info(i).parkingspace_index_id();
  359. //筛选出一号车库的大型车位
  360. if ( room_id%15 <= 8 && room_id %15 >=1
  361. && all_big_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  362. {
  363. //如果是二楼车 则特殊处理
  364. if ( all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id() == 2 )
  365. {
  366. // 二楼用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
  367. float t_min=abs( room_id%15*all_big_parkspace_data.parkspace_info(i).parkingspace_width() - (6-terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
  368. if ( time_min == 0 )
  369. {
  370. time_min=t_min;
  371. parkingspace_index_id=i;
  372. }
  373. else
  374. {
  375. if ( t_min<time_min )
  376. {
  377. time_min=t_min;
  378. parkingspace_index_id=i;
  379. }
  380. }
  381. }
  382. else
  383. {
  384. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  385. float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  386. (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 +
  387. 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;
  388. if ( time_min == 0 )
  389. {
  390. time_min=time;
  391. parkingspace_index_id=i;
  392. }
  393. else
  394. {
  395. if ( time<time_min )
  396. {
  397. time_min=time;
  398. parkingspace_index_id=i;
  399. }
  400. }
  401. }
  402. }
  403. ++i;
  404. }
  405. //判断大型车位(第三优先级)是否有找到合适的空车位 如果找到了 直接返回
  406. if ( time_min!=0 && parkingspace_index_id!=-1 )
  407. {
  408. parkspace_info.CopyFrom(all_big_parkspace_data.parkspace_info(parkingspace_index_id));
  409. return Error_code::SUCCESS;
  410. }
  411. else
  412. {
  413. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_ALLOCATE_FAILED, Error_level::MINOR_ERROR,
  414. " query_the_optimal_parkspace_serverA error ");
  415. }
  416. }
  417. }
  418. //判断是否是右半库
  419. else if(terminal_id<=6 && terminal_id>=4)
  420. {
  421. //如果是小车 (低于1.55米属于小车)
  422. if ( car_info.car_height() <= MIN_CAR_HIGH )
  423. {
  424. //获取所有小型车位信息
  425. message::Parkspace_allocation_data_msg all_min_parkspace_data;
  426. Error_manager t_error = m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_min_parkspace_data, message::Parkspace_type::MIN_PARKINGSPACE);
  427. if (t_error != SUCCESS)
  428. {
  429. return t_error;
  430. }
  431. //遍历所有小型车位
  432. int i = 0;
  433. while (i < all_min_parkspace_data.parkspace_info_size())
  434. {
  435. int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
  436. //筛选出一号车库的小型车位(第一优先级)的空车位
  437. if (room_id <= 165 && room_id >= 54
  438. && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
  439. {
  440. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  441. float time = (6-terminal_id+2)* TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  442. (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 +
  443. (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;
  444. if (time_min == 0)
  445. {
  446. time_min = time;
  447. parkingspace_index_id = i;
  448. }
  449. else
  450. {
  451. if ( time < time_min )
  452. {
  453. time_min = time;
  454. parkingspace_index_id = i;
  455. }
  456. }
  457. }
  458. ++i;
  459. }
  460. //判断小车车位(第一优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
  461. if ( time_min!=0 && parkingspace_index_id!=-1 )
  462. {
  463. parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
  464. return Error_code::SUCCESS;
  465. }
  466. }
  467. if ( car_info.car_height() <= MID_CAR_HIGH )
  468. {
  469. //第二优先级(中型车位)
  470. //获取中型车位
  471. message::Parkspace_allocation_data_msg all_mid_parkspace_data;
  472. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_mid_parkspace_data,message::Parkspace_type::MID_PARKINGSPACE);
  473. if(t_error != SUCCESS)
  474. {
  475. return t_error;
  476. }
  477. int i=0;
  478. while(i<all_mid_parkspace_data.parkspace_info_size())
  479. {
  480. int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
  481. //三楼的中型车位有单独的中跑车停车 且机械臂可以直接抓车到三楼
  482. if ( room_id >= 24 && room_id <= 27 )
  483. {
  484. //只需计算端口到车位的距离
  485. // 三楼中型车位用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
  486. float parkingspace_width=15-room_id%15;
  487. if ( room_id%15==0 )
  488. {
  489. parkingspace_width=0;
  490. }
  491. float t_min=abs( parkingspace_width*all_mid_parkspace_data.parkspace_info(i).parkingspace_width()-(6-terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
  492. if ( time_min == 0 )
  493. {
  494. time_min=t_min;
  495. parkingspace_index_id=i;
  496. }
  497. else
  498. {
  499. if ( t_min<time_min )
  500. {
  501. time_min=t_min;
  502. parkingspace_index_id=i;
  503. }
  504. }
  505. }
  506. if ( room_id <= 75 && room_id >= 69
  507. && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  508. {
  509. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  510. float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  511. (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 +
  512. (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;
  513. if ( time_min == 0 )
  514. {
  515. time_min=time;
  516. parkingspace_index_id=i;
  517. }
  518. else
  519. {
  520. if ( time<time_min )
  521. {
  522. time_min=time;
  523. parkingspace_index_id=i;
  524. }
  525. }
  526. }
  527. ++i;
  528. }
  529. //判断中型车位(第二优先级)是否有找到合适的空车位 如果找到了 直接返回
  530. if ( time_min!=0 && parkingspace_index_id!=-1 )
  531. {
  532. parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
  533. return Error_code::SUCCESS;
  534. }
  535. }
  536. if ( car_info.car_height() <= BIG_CAR_HIGH )
  537. {
  538. //第三优先级(大型车位)
  539. //获取大型车位
  540. message::Parkspace_allocation_data_msg all_big_parkspace_data;
  541. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_big_parkspace_data,message::Parkspace_type::BIG_PARKINGSPACE);
  542. if(t_error != SUCCESS)
  543. {
  544. return t_error;
  545. }
  546. int i=0;
  547. while(i<all_big_parkspace_data.parkspace_info_size())
  548. {
  549. int room_id=all_big_parkspace_data.parkspace_info(i).parkingspace_index_id();
  550. //筛选出二号车库的大型车位的空车位
  551. if ( room_id %15 >=9 && room_id %15 <=14 || room_id %15 == 0
  552. && all_big_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  553. {
  554. //如果是二楼车 则特殊处理
  555. if ( all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id() == 2 )
  556. {
  557. // 二楼用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
  558. float parkingspace_width=15-room_id%15;
  559. if ( room_id%15==0 )
  560. {
  561. parkingspace_width=0;
  562. }
  563. float t_min=abs( parkingspace_width*all_big_parkspace_data.parkspace_info(i).parkingspace_width()-(6-terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
  564. std::cout << " room_id = " << room_id <<" t_min = "<<t_min<< std::endl;
  565. std::cout << " parkingspace_index_id = " << parkingspace_index_id <<" time_min = "<<time_min<< std::endl;
  566. std::cout << " ---------------------- " << std::endl;
  567. if ( time_min == 0 )
  568. {
  569. time_min=t_min;
  570. parkingspace_index_id=i;
  571. }
  572. else
  573. {
  574. if ( t_min<time_min )
  575. {
  576. time_min=t_min;
  577. parkingspace_index_id=i;
  578. }
  579. }
  580. }
  581. else
  582. {
  583. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  584. float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  585. (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 +
  586. (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;
  587. std::cout << " room_id = " << room_id <<" time = "<<time<< std::endl;
  588. std::cout << " parkingspace_index_id = " << parkingspace_index_id <<" time_min = "<<time_min<< std::endl;
  589. std::cout << " ---------------------- " << std::endl;
  590. if ( time_min == 0 )
  591. {
  592. time_min=time;
  593. parkingspace_index_id=i;
  594. }
  595. else
  596. {
  597. if ( time<time_min )
  598. {
  599. time_min=time;
  600. parkingspace_index_id=i;
  601. }
  602. }
  603. }
  604. }
  605. ++i;
  606. }
  607. //判断大型车位(第三优先级)是否有找到合适的空车位 如果找到了 直接返回
  608. if ( time_min!=0 && parkingspace_index_id!=-1 )
  609. {
  610. parkspace_info.CopyFrom(all_big_parkspace_data.parkspace_info(parkingspace_index_id));
  611. return Error_code::SUCCESS;
  612. }
  613. else
  614. {
  615. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_ALLOCATE_FAILED, Error_level::MINOR_ERROR,
  616. " query_the_optimal_parkspace_serverA error ");
  617. }
  618. }
  619. }
  620. else
  621. {
  622. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  623. "query_the_optimal_parkspace_serverA error ");
  624. }
  625. }
  626. //找到一个最优车位---方案二
  627. //找左侧车位
  628. Error_manager Parkspace_manager::query_the_optimal_parkspace_serverB1(message::Parkspace_info &parkspace_info,message::Car_info car_info,int terminal_id)
  629. {
  630. //车辆高大于1550mm分在四楼以下,小于1550mm分在四楼以上
  631. /*
  632. * K=j%15
  633. * P=i%6
  634. * S=15*d-(j%15)*(2*d-1)+[6*d-(i%6)(2*d-1)]*W W>1
  635. */
  636. float time_min=0;
  637. int parkingspace_index_id=-1;
  638. //找左侧车位
  639. //如果是小车 (低于1.55米属于小车)
  640. if ( car_info.car_height() <= MIN_CAR_HIGH )
  641. {
  642. //获取所有小型车位信息
  643. message::Parkspace_allocation_data_msg all_min_parkspace_data;
  644. Error_manager t_error = m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_min_parkspace_data, message::Parkspace_type::MIN_PARKINGSPACE);
  645. if (t_error != SUCCESS)
  646. {
  647. return t_error;
  648. }
  649. //遍历所有小型车位
  650. int i = 0;
  651. while (i < all_min_parkspace_data.parkspace_info_size())
  652. {
  653. int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
  654. //筛选出小型车位的中间部分(第一优先级)的空车位
  655. if (room_id % 15 <= 8 && room_id % 15 >= 4
  656. && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
  657. {
  658. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  659. float time = terminal_id * TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  660. (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 +
  661. (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;
  662. if (time_min == 0)
  663. {
  664. time_min = time;
  665. parkingspace_index_id = i;
  666. }
  667. else
  668. {
  669. if ( time<time_min )
  670. {
  671. time_min=time;
  672. parkingspace_index_id=i;
  673. }
  674. }
  675. }
  676. ++i;
  677. }
  678. //判断小车车位中间部分(第一优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
  679. if ( time_min!=0 && parkingspace_index_id!=-1 )
  680. {
  681. parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
  682. return Error_code::SUCCESS;
  683. }
  684. else
  685. {
  686. //如果没找到则找小车车位第二优先级 (侧边)
  687. int i = 0;
  688. while (i < all_min_parkspace_data.parkspace_info_size())
  689. {
  690. int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
  691. //筛选出一号车库的小型车位的中间部分(第一优先级)的空车位
  692. if (room_id % 15 <= 3 && room_id % 15 >= 1
  693. && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
  694. {
  695. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  696. float time = terminal_id * TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  697. (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 +
  698. (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;
  699. if (time_min == 0)
  700. {
  701. time_min = time;
  702. parkingspace_index_id = i;
  703. }
  704. else
  705. {
  706. if ( time < time_min )
  707. {
  708. time_min = time;
  709. parkingspace_index_id = i;
  710. }
  711. }
  712. }
  713. ++i;
  714. }
  715. //判断小车车位侧边部分(第二优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
  716. if ( time_min!=0 && parkingspace_index_id!=-1 )
  717. {
  718. parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
  719. return Error_code::SUCCESS;
  720. }
  721. }
  722. }
  723. if ( car_info.car_height() <= MID_CAR_HIGH )
  724. {
  725. //找中型车位
  726. //获取中型车位
  727. message::Parkspace_allocation_data_msg all_mid_parkspace_data;
  728. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_mid_parkspace_data,message::Parkspace_type::MID_PARKINGSPACE);
  729. if(t_error != SUCCESS)
  730. {
  731. return t_error;
  732. }
  733. int i=0;
  734. while(i<all_mid_parkspace_data.parkspace_info_size())
  735. {
  736. int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
  737. //如果是六楼的中型车位的中间部分(第一优先级)
  738. if ( room_id >=64 && room_id <= 68
  739. && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  740. {
  741. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  742. float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  743. (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 +
  744. (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;
  745. if ( time_min == 0 )
  746. {
  747. time_min=time;
  748. parkingspace_index_id=i;
  749. }
  750. else
  751. {
  752. if ( time<time_min )
  753. {
  754. time_min=time;
  755. parkingspace_index_id=i;
  756. }
  757. }
  758. }
  759. ++i;
  760. }
  761. //判断中型车位的中间部分(第一优先级)是否有找到合适的空车位 如果找到了 直接返回
  762. if ( time_min!=0 && parkingspace_index_id!=-1 )
  763. {
  764. parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
  765. return Error_code::SUCCESS;
  766. }
  767. else
  768. {
  769. //如果没找到则找侧边中型车位(第二优先级)
  770. int i=0;
  771. while(i<all_mid_parkspace_data.parkspace_info_size())
  772. {
  773. int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
  774. //如果是六楼的中型车位的侧边(第二优先级)
  775. if ( room_id >=61 && room_id <= 63
  776. && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  777. {
  778. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  779. float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  780. (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 +
  781. 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;
  782. if ( time_min == 0 )
  783. {
  784. time_min=time;
  785. parkingspace_index_id=i;
  786. }
  787. else
  788. {
  789. if ( time<time_min )
  790. {
  791. time_min=time;
  792. parkingspace_index_id=i;
  793. }
  794. }
  795. }
  796. ++i;
  797. }
  798. //判断中型车位侧边(第二优先级)是否有找到合适的空车位 如果找到了 直接返回
  799. if ( time_min!=0 && parkingspace_index_id!=-1 )
  800. {
  801. parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
  802. return Error_code::SUCCESS;
  803. }
  804. }
  805. }
  806. if ( car_info.car_height() <= BIG_CAR_HIGH )
  807. {
  808. //找大型车位
  809. //获取大型车位
  810. message::Parkspace_allocation_data_msg all_big_parkspace_data;
  811. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_big_parkspace_data,message::Parkspace_type::BIG_PARKINGSPACE);
  812. if(t_error != SUCCESS)
  813. {
  814. return t_error;
  815. }
  816. int i=0;
  817. while(i<all_big_parkspace_data.parkspace_info_size())
  818. {
  819. int room_id=all_big_parkspace_data.parkspace_info(i).parkingspace_index_id();
  820. //筛选出一号车库的小型车位的靠近电梯的左侧三排(第二优先级)的空车位
  821. if ( room_id %15 == 1 || room_id %15 == 2 || room_id %15 == 3 || room_id>=34 && room_id<=38
  822. && all_big_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  823. {
  824. //如果是二楼车 则特殊处理
  825. if ( all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id() == 2 )
  826. {
  827. // 二楼用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
  828. float t_min=abs( room_id%15*all_big_parkspace_data.parkspace_info(i).parkingspace_width()-(6-terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
  829. if ( time_min == 0 )
  830. {
  831. time_min=t_min;
  832. parkingspace_index_id=i;
  833. }
  834. else
  835. {
  836. if ( t_min<time_min )
  837. {
  838. time_min=t_min;
  839. parkingspace_index_id=i;
  840. }
  841. }
  842. }
  843. else
  844. {
  845. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  846. float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  847. (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 +
  848. 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;
  849. if ( time_min == 0 )
  850. {
  851. time_min=time;
  852. parkingspace_index_id=i;
  853. }
  854. else
  855. {
  856. if ( time<time_min )
  857. {
  858. time_min=time;
  859. parkingspace_index_id=i;
  860. }
  861. }
  862. }
  863. }
  864. ++i;
  865. }
  866. //判断大型车位是否有找到合适的空车位 如果找到了 直接返回
  867. if ( time_min!=0 && parkingspace_index_id!=-1 )
  868. {
  869. parkspace_info.CopyFrom(all_big_parkspace_data.parkspace_info(parkingspace_index_id));
  870. return Error_code::SUCCESS;
  871. }
  872. else
  873. {
  874. //如果没找到则代表左侧没找到合适车位
  875. parkspace_info.set_parkingspace_index_id(-1);
  876. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_ALLOCATE_FAILED, Error_level::NEGLIGIBLE_ERROR,
  877. "query_the_optimal_parkspace_serverB1 error ");
  878. }
  879. }
  880. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  881. " query_the_optimal_parkspace_serverB1 error ");
  882. }
  883. //找右侧车位
  884. Error_manager Parkspace_manager::query_the_optimal_parkspace_serverB2(message::Parkspace_info &parkspace_info,message::Car_info car_info,int terminal_id)
  885. {
  886. //车辆高大于1550mm分在四楼以下,小于1550mm分在四楼以上
  887. /*
  888. * K=j%15
  889. * P=i%6
  890. * S=15*d-(j%15)*(2*d-1)+[6*d-(i%6)(2*d-1)]*W W>1
  891. */
  892. float time_min=0;
  893. int parkingspace_index_id=-1;
  894. //找右侧车位
  895. //如果是小车 (低于1.55米属于小车)
  896. if ( car_info.car_height() <= MIN_CAR_HIGH )
  897. {
  898. //获取所有小型车位信息
  899. message::Parkspace_allocation_data_msg all_min_parkspace_data;
  900. Error_manager t_error = m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_min_parkspace_data, message::Parkspace_type::MIN_PARKINGSPACE);
  901. if (t_error != SUCCESS)
  902. {
  903. return t_error;
  904. }
  905. //遍历所有小型车位
  906. int i = 0;
  907. while (i < all_min_parkspace_data.parkspace_info_size())
  908. {
  909. int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
  910. //筛选出小型车位的中间部分(第一优先级)的空车位
  911. if (room_id % 15 >= 9 && room_id % 15 <= 12
  912. && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
  913. {
  914. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  915. float time = terminal_id * TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  916. (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 +
  917. (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;
  918. if (time_min == 0)
  919. {
  920. time_min = time;
  921. parkingspace_index_id = i;
  922. }
  923. else
  924. {
  925. if ( time<time_min )
  926. {
  927. time_min=time;
  928. parkingspace_index_id=i;
  929. }
  930. }
  931. }
  932. ++i;
  933. }
  934. //判断小车车位中间部分(第一优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
  935. if ( time_min!=0 && parkingspace_index_id!=-1 )
  936. {
  937. parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
  938. return Error_code::SUCCESS;
  939. }
  940. else
  941. {
  942. //如果没找到则找小车车位第二优先级 (侧边)
  943. int i = 0;
  944. while (i < all_min_parkspace_data.parkspace_info_size())
  945. {
  946. int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
  947. //筛选出一号车库的小型车位的中间部分(第一优先级)的空车位
  948. if (room_id % 15 == 13 || room_id % 15 == 14 || room_id % 15 == 0
  949. && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
  950. {
  951. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  952. float time = terminal_id * TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  953. (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 +
  954. (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;
  955. if (time_min == 0)
  956. {
  957. time_min = time;
  958. parkingspace_index_id = i;
  959. }
  960. else
  961. {
  962. if ( time < time_min )
  963. {
  964. time_min = time;
  965. parkingspace_index_id = i;
  966. }
  967. }
  968. }
  969. ++i;
  970. }
  971. //判断小车车位侧边部分(第二优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
  972. if ( time_min!=0 && parkingspace_index_id!=-1 )
  973. {
  974. parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
  975. return Error_code::SUCCESS;
  976. }
  977. }
  978. }
  979. if ( car_info.car_height() <= MID_CAR_HIGH )
  980. {
  981. //找中型车位
  982. //获取中型车位
  983. message::Parkspace_allocation_data_msg all_mid_parkspace_data;
  984. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_mid_parkspace_data,message::Parkspace_type::MID_PARKINGSPACE);
  985. if(t_error != SUCCESS)
  986. {
  987. return t_error;
  988. }
  989. int i=0;
  990. while(i<all_mid_parkspace_data.parkspace_info_size())
  991. {
  992. int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
  993. //如果是六楼的中型车位的中间部分(第一优先级)
  994. if ( room_id >= 69 && room_id <= 72
  995. && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  996. {
  997. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  998. float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  999. (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 +
  1000. 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;
  1001. if ( time_min == 0 )
  1002. {
  1003. time_min=time;
  1004. parkingspace_index_id=i;
  1005. }
  1006. else
  1007. {
  1008. if ( time<time_min )
  1009. {
  1010. time_min=time;
  1011. parkingspace_index_id=i;
  1012. }
  1013. }
  1014. }
  1015. ++i;
  1016. }
  1017. //判断中型车位的中间部分(第一优先级)是否有找到合适的空车位 如果找到了 直接返回
  1018. if ( time_min!=0 && parkingspace_index_id!=-1 )
  1019. {
  1020. parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
  1021. return Error_code::SUCCESS;
  1022. }
  1023. else
  1024. {
  1025. //如果没找到则找侧边中型车位(第二优先级)
  1026. int i=0;
  1027. while(i<all_mid_parkspace_data.parkspace_info_size())
  1028. {
  1029. int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
  1030. //如果是六楼的中型车位的侧边(第二优先级)
  1031. if ( room_id >=73 && room_id <= 75
  1032. && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  1033. {
  1034. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  1035. float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  1036. (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 +
  1037. 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;
  1038. if ( time_min == 0 )
  1039. {
  1040. time_min=time;
  1041. parkingspace_index_id=i;
  1042. }
  1043. else
  1044. {
  1045. if ( time<time_min )
  1046. {
  1047. time_min=time;
  1048. parkingspace_index_id=i;
  1049. }
  1050. }
  1051. }
  1052. ++i;
  1053. }
  1054. //判断中型车位侧边(第二优先级)是否有找到合适的空车位 如果找到了 直接返回
  1055. if ( time_min!=0 && parkingspace_index_id!=-1 )
  1056. {
  1057. parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
  1058. return Error_code::SUCCESS;
  1059. }
  1060. }
  1061. }
  1062. if ( car_info.car_height() <= BIG_CAR_HIGH )
  1063. {
  1064. //找大型车位
  1065. //获取大型车位
  1066. message::Parkspace_allocation_data_msg all_big_parkspace_data;
  1067. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_big_parkspace_data,message::Parkspace_type::BIG_PARKINGSPACE);
  1068. if(t_error != SUCCESS)
  1069. {
  1070. return t_error;
  1071. }
  1072. int i=0;
  1073. while(i<all_big_parkspace_data.parkspace_info_size())
  1074. {
  1075. int room_id=all_big_parkspace_data.parkspace_info(i).parkingspace_index_id();
  1076. //筛选出大型车位的靠近电梯的右侧三排(第二优先级)的空车位
  1077. if ( room_id %15 == 13 || room_id %15 == 14 || room_id %15 == 0|| room_id>=39 && room_id<=42
  1078. && all_big_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  1079. {
  1080. //如果是二楼车 则特殊处理
  1081. if ( all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id() == 2 )
  1082. {
  1083. // 二楼用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
  1084. float t_min=abs( room_id%15*all_big_parkspace_data.parkspace_info(i).parkingspace_width()-(6-terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
  1085. if ( time_min == 0 )
  1086. {
  1087. time_min=t_min;
  1088. parkingspace_index_id=i;
  1089. }
  1090. else
  1091. {
  1092. if ( t_min<time_min )
  1093. {
  1094. time_min=t_min;
  1095. parkingspace_index_id=i;
  1096. }
  1097. }
  1098. }
  1099. else
  1100. {
  1101. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  1102. float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  1103. (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 +
  1104. 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;
  1105. if ( time_min == 0 )
  1106. {
  1107. time_min=time;
  1108. parkingspace_index_id=i;
  1109. }
  1110. else
  1111. {
  1112. if ( time<time_min )
  1113. {
  1114. time_min=time;
  1115. parkingspace_index_id=i;
  1116. }
  1117. }
  1118. }
  1119. }
  1120. ++i;
  1121. }
  1122. //判断大型车位是否有找到合适的空车位 如果找到了 直接返回
  1123. if ( time_min!=0 && parkingspace_index_id!=-1 )
  1124. {
  1125. parkspace_info.CopyFrom(all_big_parkspace_data.parkspace_info(parkingspace_index_id));
  1126. return Error_code::SUCCESS;
  1127. }
  1128. else
  1129. {
  1130. //如果没找到则代表右侧没找到合适车位
  1131. parkspace_info.set_parkingspace_index_id(-1);
  1132. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_ALLOCATE_FAILED, Error_level::NEGLIGIBLE_ERROR,
  1133. "query_the_optimal_parkspace_serverB2 error ");
  1134. }
  1135. }
  1136. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  1137. " query_the_optimal_parkspace_serverB2 error ");
  1138. }
  1139. //找缓存区车位
  1140. Error_manager Parkspace_manager::query_the_optimal_parkspace_serverB3(message::Parkspace_info &parkspace_info,message::Car_info car_info,int terminal_id)
  1141. {
  1142. //车辆高大于1550mm分在四楼以下,小于1550mm分在四楼以上
  1143. /*
  1144. * K=j%15
  1145. * P=i%6
  1146. * S=15*d-(j%15)*(2*d-1)+[6*d-(i%6)(2*d-1)]*W W>1
  1147. */
  1148. float time_min=0;
  1149. int parkingspace_index_id=-1;
  1150. //找缓存区车位 缓冲区只有中型车位和大型车位
  1151. if ( car_info.car_height() <= MID_CAR_HIGH )
  1152. {
  1153. message::Parkspace_allocation_data_msg all_mid_parkspace_data;
  1154. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_mid_parkspace_data,message::Parkspace_type::MID_PARKINGSPACE);
  1155. if(t_error != SUCCESS)
  1156. {
  1157. return t_error;
  1158. }
  1159. int i=0;
  1160. while (i < all_mid_parkspace_data.parkspace_info_size())
  1161. {
  1162. //筛选中型车位缓冲区
  1163. int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
  1164. if ( room_id >= 19 && room_id <= 27
  1165. && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::eParkspace_empty)
  1166. {
  1167. //只需计算端口到车位的距离
  1168. // 三楼中型用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
  1169. float t_min=abs( room_id%15*all_mid_parkspace_data.parkspace_info(i).parkingspace_width()-(6-terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
  1170. if ( time_min == 0 )
  1171. {
  1172. time_min=t_min;
  1173. parkingspace_index_id=i;
  1174. }
  1175. else
  1176. {
  1177. if ( t_min<time_min )
  1178. {
  1179. time_min=t_min;
  1180. parkingspace_index_id=i;
  1181. }
  1182. }
  1183. }
  1184. }
  1185. //判断缓冲区的中型车位是否有找到合适的空车位 如果找到了 直接返回
  1186. if ( time_min!=0 && parkingspace_index_id!=-1 )
  1187. {
  1188. parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
  1189. return Error_code::SUCCESS;
  1190. }
  1191. }
  1192. if( car_info.car_height() <= BIG_CAR_HIGH )
  1193. {
  1194. message::Parkspace_allocation_data_msg all_big_parkspace_data;
  1195. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_big_parkspace_data,message::Parkspace_type::BIG_PARKINGSPACE);
  1196. if(t_error != SUCCESS)
  1197. {
  1198. return t_error;
  1199. }
  1200. int i=0;
  1201. while (i < all_big_parkspace_data.parkspace_info_size())
  1202. {
  1203. //筛选中型车位缓冲区
  1204. int room_id=all_big_parkspace_data.parkspace_info(i).parkingspace_index_id();
  1205. if ( room_id >= 4 && room_id <= 12
  1206. && all_big_parkspace_data.parkspace_info(i).parkingspace_status() == message::eParkspace_empty)
  1207. {
  1208. //只需计算端口到车位的距离
  1209. // 二楼中型用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
  1210. float t_min=abs( room_id%15*all_big_parkspace_data.parkspace_info(i).parkingspace_width()-(6-terminal_id+2)*TERMINAL_SPACING )/MEDIUM_CAR_SPEED+DOWNSTAIRS_SPORTS_TIME;
  1211. if ( time_min == 0 )
  1212. {
  1213. time_min=t_min;
  1214. parkingspace_index_id=i;
  1215. }
  1216. else
  1217. {
  1218. if ( t_min<time_min )
  1219. {
  1220. time_min=t_min;
  1221. parkingspace_index_id=i;
  1222. }
  1223. }
  1224. }
  1225. }
  1226. //判断缓冲区的中型车位是否有找到合适的空车位 如果找到了 直接返回
  1227. if ( time_min!=0 && parkingspace_index_id!=-1 )
  1228. {
  1229. parkspace_info.CopyFrom(all_big_parkspace_data.parkspace_info(parkingspace_index_id));
  1230. return Error_code::SUCCESS;
  1231. }
  1232. else
  1233. {
  1234. //如果没找到则代表缓冲区没找到合适车位
  1235. parkspace_info.set_parkingspace_index_id(-1);
  1236. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_ALLOCATE_FAILED, Error_level::NEGLIGIBLE_ERROR,
  1237. "query_the_optimal_parkspace_serverB2 error ");
  1238. }
  1239. }
  1240. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  1241. " query_the_optimal_parkspace_serverB3 error ");
  1242. }
  1243. //分配车位线程函数
  1244. void Parkspace_manager::execute_for_allocate(message::Car_info car_info, int terminal_id, std::string command_key)
  1245. {
  1246. LOG(INFO) << "分配车位 "<<"牌号:"<<car_info.license()<<" command_key="<<command_key; //huli printf command_key
  1247. //根据请求的信息反馈分配的车位,并封装发送
  1248. message::Parkspace_allocation_response_msg response_msg;
  1249. message::Base_info t_response_header;
  1250. t_response_header.set_msg_type(message::Message_type::eParkspace_allocation_response_msg);
  1251. t_response_header.set_timeout_ms(1000);
  1252. t_response_header.set_sender(message::Communicator::eParkspace);
  1253. t_response_header.set_receiver(message::Communicator::eMain);
  1254. message::Error_manager t_error;
  1255. message::Parkspace_info t_allocated_space;
  1256. Error_manager error;
  1257. //分配之前查询车辆是否已经存在
  1258. if(check_car_existence(car_info.license()))
  1259. {
  1260. t_error.set_error_code(PARKSPACE_ALLOCATOR_CAR_ALREADY_EXIST);
  1261. t_error.set_error_level(message::Error_level::MAJOR_ERROR);
  1262. t_error.set_error_description("车辆已存在");
  1263. LOG(ERROR) << car_info.license()<<" 车辆已存在";
  1264. }
  1265. else
  1266. {
  1267. m_parkspace_lock.lock();
  1268. //如果车辆不存在 则找到一个最优车位
  1269. error=query_the_optimal_parkspace_serverA(t_allocated_space,car_info,terminal_id);
  1270. // query_the_optimal_parkspace_serverA(t_allocated_space,car_info,terminal_id);
  1271. //如果车辆不存在则获取一个空车位
  1272. // error=m_parkspace_operating_function.query_one_empty_parkspace(t_allocated_space);
  1273. if ( error != Error_code::SUCCESS )
  1274. {
  1275. t_error.set_error_code(error.get_error_code());
  1276. t_error.set_error_level((message::Error_level)error.get_error_level());
  1277. t_error.set_error_description(error.get_error_description());
  1278. LOG(ERROR) << car_info.license()<<" 获取空车位失败 "<<error.to_string();
  1279. }
  1280. else
  1281. {
  1282. LOG(INFO) <<"分配的车位ID为:"<<t_allocated_space.parkingspace_index_id();
  1283. return ;
  1284. //修改车为状态为锁定
  1285. t_allocated_space.set_parkingspace_status(message::Parkspace_status::eParkspace_locked);
  1286. t_allocated_space.mutable_car_info()->CopyFrom(car_info);
  1287. // 分配车位后更新车位状态
  1288. error=m_parkspace_operating_function.update_parkspace_data(t_allocated_space);
  1289. if ( error != SUCCESS )
  1290. {
  1291. t_error.set_error_code(error.get_error_code());
  1292. t_error.set_error_level((message::Error_level)error.get_error_level());
  1293. t_error.set_error_description(error.get_error_description());
  1294. LOG(ERROR) << car_info.license()<<" 更新车位数据失败 "<<error.to_string();
  1295. }
  1296. else
  1297. {
  1298. //修改车辆状态为停车中
  1299. message::Vehicle_status t_vehicle_status = message::Vehicle_status::eVehicle_parking;
  1300. error=m_parkspace_operating_function.update_vehicle_with_parkspace(t_allocated_space,t_vehicle_status);
  1301. if ( error != SUCCESS )
  1302. {
  1303. t_error.set_error_code(error.get_error_code());
  1304. t_error.set_error_level((message::Error_level)error.get_error_level());
  1305. t_error.set_error_description(error.get_error_description());
  1306. LOG(ERROR) << car_info.license()<<" 更新车辆数据失败 "<<error.to_string();
  1307. }
  1308. else
  1309. {
  1310. t_error.set_error_code(SUCCESS);
  1311. t_error.set_error_level(message::Error_level::NORMAL);
  1312. LOG(INFO) <<"分配车位成功 "<<"车位ID:"<<t_allocated_space.parkingspace_index_id()<<" 车牌号:"<<t_allocated_space.car_info().license();
  1313. }
  1314. }
  1315. }
  1316. m_parkspace_lock.unlock();
  1317. }
  1318. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  1319. response_msg.set_command_key(command_key);
  1320. response_msg.mutable_error_manager()->CopyFrom(t_error);
  1321. response_msg.mutable_allocated_space_info()->CopyFrom(t_allocated_space);
  1322. Communication_message response=Communication_message();
  1323. response.reset(t_response_header, response_msg.SerializeAsString());
  1324. Parkspace_communicator::get_instance_references().send_response(&response);
  1325. send_parkspace_data();
  1326. std::cout<<std::endl;
  1327. }
  1328. //查询车位线程函数
  1329. void Parkspace_manager::execute_for_search(message::Car_info car_info,std::string command_key)
  1330. {
  1331. LOG(INFO) << "查询车位 "<<"牌号:"<<car_info.license()<<" command_key="<<command_key; //huli printf command_key
  1332. //根据车辆凭证信息查询车辆位置
  1333. message::Parkspace_search_response_msg response_msg;
  1334. message::Base_info t_response_header;
  1335. message::Error_manager t_error;
  1336. message::Parkspace_info t_car_position;
  1337. t_response_header.set_msg_type(message::Message_type::eParkspace_search_response_msg);
  1338. t_response_header.set_timeout_ms(1000);
  1339. t_response_header.set_sender(message::Communicator::eParkspace);
  1340. t_response_header.set_receiver(message::Communicator::eMain);
  1341. //查询车辆位置
  1342. message::Parkspace_info parkspace_info;
  1343. Error_manager error=m_parkspace_operating_function.query_one_parkspace_with_license(car_info.license(),parkspace_info);
  1344. if ( error != SUCCESS )
  1345. {
  1346. t_error.set_error_code(error.get_error_code());
  1347. t_error.set_error_level((message::Error_level)error.get_error_level());
  1348. t_error.set_error_description(error.get_error_description());
  1349. LOG(ERROR) << "查询车位失败 "<<error.to_string();
  1350. }
  1351. else
  1352. {
  1353. t_error.set_error_code(SUCCESS);
  1354. t_error.set_error_level(message::Error_level::NORMAL);
  1355. LOG(INFO) << "车辆"<<parkspace_info.car_info().license()<<" 在"<<parkspace_info.parkingspace_index_id()<<"号车位上";
  1356. }
  1357. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  1358. response_msg.set_command_key(command_key);
  1359. response_msg.mutable_error_manager()->CopyFrom(t_error);
  1360. response_msg.mutable_car_position()->CopyFrom(t_car_position);
  1361. Communication_message response=Communication_message();
  1362. response.reset(t_response_header, response_msg.SerializeAsString());
  1363. Parkspace_communicator::get_instance_references().send_response(&response);
  1364. std::cout<<std::endl;
  1365. }
  1366. //释放车位线程函数
  1367. void Parkspace_manager::execute_for_release(message::Parkspace_info space_info, std::string command_key)
  1368. {
  1369. LOG(INFO) << "释放车位 "<<"牌号:"<<space_info.car_info().license()<<" command_key="<<command_key;
  1370. //根据车位信息定位待释放车位位置,车辆凭证号用于校验
  1371. message::Parkspace_release_response_msg response_msg;
  1372. message::Base_info t_response_header;
  1373. t_response_header.set_msg_type(message::eParkspace_release_response_msg);
  1374. t_response_header.set_sender(message::eParkspace);
  1375. t_response_header.set_receiver(message::eMain);
  1376. t_response_header.set_timeout_ms(1000);
  1377. message::Error_manager t_error;
  1378. message::Parkspace_info t_release_space;
  1379. //找到待释放的车位
  1380. m_parkspace_lock.lock();
  1381. Error_manager error=m_parkspace_operating_function.query_one_parkspace_with_parkspace_id(space_info.parkingspace_index_id(),t_release_space);
  1382. if ( error != SUCCESS )
  1383. {
  1384. t_error.set_error_code(error.get_error_code());
  1385. t_error.set_error_level((message::Error_level)error.get_error_level());
  1386. t_error.set_error_description(error.get_error_description());
  1387. LOG(ERROR) << "释放车位失败 "<<error.to_string();
  1388. }
  1389. else if(!t_release_space.has_car_info())
  1390. {
  1391. t_error.set_error_code(PARKSPACE_ALLOCATOR_RELEASE_FAILED);
  1392. t_error.set_error_level(message::Error_level::MAJOR_ERROR);
  1393. LOG(WARNING) << "传入待释放车位无车辆信息";
  1394. }
  1395. else
  1396. {
  1397. t_release_space.set_parkingspace_status(message::Parkspace_status::eParkspace_empty);
  1398. error = m_parkspace_operating_function.update_parkspace_data(t_release_space);
  1399. if (error != SUCCESS)
  1400. {
  1401. t_error.set_error_code(error.get_error_code());
  1402. t_error.set_error_level((message::Error_level) error.get_error_level());
  1403. t_error.set_error_description(error.get_error_description());
  1404. LOG(ERROR) << "释放车位 更新数据库车位数据失败 " << error.to_string();
  1405. }
  1406. else
  1407. {
  1408. message::Vehicle_status t_vehicle_status = message::Vehicle_status::eVehicle_idle;
  1409. error = m_parkspace_operating_function.update_vehicle_with_parkspace(t_release_space, t_vehicle_status);
  1410. if (error != SUCCESS)
  1411. {
  1412. t_error.set_error_code(error.get_error_code());
  1413. t_error.set_error_level((message::Error_level) error.get_error_level());
  1414. t_error.set_error_description(error.get_error_description());
  1415. LOG(ERROR) << "释放车位 更新数据库车辆数据失败 " << error.to_string();
  1416. }
  1417. else
  1418. {
  1419. t_error.set_error_code(SUCCESS);
  1420. t_error.set_error_level(message::Error_level::NORMAL);
  1421. LOG(INFO) << "第:" << t_release_space.parkingspace_index_id() << "号位 车辆: " << t_release_space.car_info().license()
  1422. << " 释放成功";
  1423. //获取预约ID
  1424. int t_park_record_id;
  1425. error = m_parkspace_operating_function.query_vehicle(t_release_space.car_info().license(),
  1426. t_vehicle_status, t_park_record_id);
  1427. if (error != SUCCESS)
  1428. {
  1429. t_error.set_error_code(error.get_error_code());
  1430. t_error.set_error_level((message::Error_level) error.get_error_level());
  1431. t_error.set_error_description(error.get_error_description());
  1432. LOG(ERROR) << "释放车位 数据库查询车辆信息失败 " << error.to_string();
  1433. }
  1434. else if (t_vehicle_status == message::Vehicle_status::eVehicle_idle)
  1435. {
  1436. time_t tt = time(NULL);
  1437. tm *t = localtime(&tt);
  1438. char my_time_buf[255];
  1439. memset(my_time_buf, 0, 255);
  1440. sprintf(my_time_buf, "%d-%02d-%02d %02d:%02d:%02d",
  1441. t->tm_year + 1900,
  1442. t->tm_mon + 1,
  1443. t->tm_mday,
  1444. t->tm_hour,
  1445. t->tm_min,
  1446. t->tm_sec);
  1447. t_release_space.set_leave_time(my_time_buf);
  1448. error = m_parkspace_operating_function.update_parking_record(t_release_space, t_park_record_id);
  1449. if (error != SUCCESS)
  1450. {
  1451. t_error.set_error_code(error.get_error_code());
  1452. t_error.set_error_level((message::Error_level) error.get_error_level());
  1453. t_error.set_error_description(error.get_error_description());
  1454. LOG(ERROR) << "更新停车记录失败: " << error.to_string();
  1455. }
  1456. }
  1457. }
  1458. }
  1459. }
  1460. m_parkspace_lock.unlock();
  1461. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  1462. response_msg.set_command_key(command_key);
  1463. response_msg.mutable_error_manager()->CopyFrom(t_error);
  1464. response_msg.mutable_release_space_info()->CopyFrom(t_release_space);
  1465. Communication_message response=Communication_message();
  1466. response.reset(t_response_header, response_msg.SerializeAsString());
  1467. Parkspace_communicator::get_instance_references().send_response(&response);
  1468. send_parkspace_data();
  1469. std::cout<<std::endl;
  1470. }
  1471. //强制更新车位信息线程函数
  1472. void Parkspace_manager::execute_for_force_update(message::Parkspace_info space_info, std::string command_key)
  1473. {
  1474. LOG(INFO) << "手动 "<<"牌号:"<<space_info.car_info().license()<<" command_key="<<command_key;
  1475. //根据车位信息定位待释放车位位置,车辆凭证号用于校验
  1476. //!!!!!此处跳过外部处理与调用的过程,直接在内部调用,发送分配结果用于测试,目前一直发布第一个车位
  1477. message::Parkspace_force_update_response_msg response_msg;
  1478. message::Base_info t_response_header;
  1479. t_response_header.set_msg_type(message::eParkspace_force_update_response_msg);
  1480. t_response_header.set_sender(message::eParkspace);
  1481. t_response_header.set_receiver(message::eMain);
  1482. t_response_header.set_timeout_ms(1000);
  1483. message::Error_manager t_error;
  1484. message::Parkspace_info t_update_space;
  1485. Error_manager error;
  1486. // id 9999为特殊情况,复位所有车位状态
  1487. if (space_info.parkingspace_index_id() == 9999)
  1488. {
  1489. // 复位所有车位状态
  1490. error=m_parkspace_operating_function.clear_all_parkspace_info();
  1491. if( error != SUCCESS)
  1492. {
  1493. t_error.set_error_code(error.get_error_code());
  1494. t_error.set_error_level((message::Error_level) error.get_error_level());
  1495. LOG(INFO) << "手动更新所有车位失败 "<<error.to_string();
  1496. }
  1497. else
  1498. {
  1499. t_error.set_error_code(SUCCESS);
  1500. t_error.set_error_level(message::Error_level::NORMAL);
  1501. LOG(INFO) << "所有车位已手动更新 ";
  1502. }
  1503. }
  1504. else
  1505. {
  1506. //手动修改车位信息
  1507. error = m_parkspace_operating_function.update_parkspace_data(t_update_space);
  1508. if (error != SUCCESS)
  1509. {
  1510. t_error.set_error_code(error.get_error_code());
  1511. t_error.set_error_level((message::Error_level)error.get_error_level());
  1512. LOG(INFO) << "手动 数据库更新车位失败 "<<error.to_string();
  1513. }
  1514. else
  1515. {
  1516. t_error.set_error_code(SUCCESS);
  1517. t_error.set_error_level(message::Error_level::NORMAL);
  1518. LOG(INFO) << "第" << t_update_space.parkingspace_index_id() << "号位已手动更新";
  1519. }
  1520. }
  1521. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  1522. response_msg.set_command_key(command_key);
  1523. response_msg.mutable_error_manager()->CopyFrom(t_error);
  1524. response_msg.mutable_update_space_info()->CopyFrom(t_update_space);
  1525. Communication_message response=Communication_message();
  1526. response.reset(t_response_header, response_msg.SerializeAsString());
  1527. Parkspace_communicator::get_instance_references().send_response(&response);
  1528. send_parkspace_data();
  1529. }
  1530. //确认分配车位线程函数
  1531. void Parkspace_manager::execute_for_confirm_alloc(message::Parkspace_info space_info, std::string command_key)
  1532. {
  1533. LOG(INFO) << "确认分配 " << "牌号:" << space_info.car_info().license() << " command_key=" << command_key;
  1534. //根据车位信息定位待确认占用车位
  1535. message::Parkspace_confirm_alloc_response_msg response_msg;
  1536. message::Base_info t_response_header;
  1537. t_response_header.set_msg_type(message::eParkspace_confirm_alloc_response_msg);
  1538. t_response_header.set_sender(message::eParkspace);
  1539. t_response_header.set_receiver(message::eMain);
  1540. t_response_header.set_timeout_ms(1000);
  1541. message::Error_manager t_error;
  1542. message::Parkspace_info t_confirm_space;
  1543. Error_manager error;
  1544. //获取确认分配的车位
  1545. m_parkspace_lock.lock();
  1546. error = m_parkspace_operating_function.query_one_parkspace_with_parkspace_id(space_info.parkingspace_index_id(),
  1547. t_confirm_space);
  1548. if (error != SUCCESS)
  1549. {
  1550. t_error.set_error_code(error.get_error_code());
  1551. t_error.set_error_level((message::Error_level) error.get_error_level());
  1552. t_error.set_error_description(error.get_error_description());
  1553. LOG(ERROR) << "确认分配 查询车位信息失败 " << error.to_string();
  1554. }
  1555. else if (!t_confirm_space.has_car_info())
  1556. {
  1557. t_error.set_error_code(PARKSPACE_ALLOCATOR_RELEASE_FAILED);
  1558. t_error.set_error_level(message::Error_level::MAJOR_ERROR);
  1559. LOG(WARNING) << "传入待确认车位无车辆信息";
  1560. }
  1561. else
  1562. {
  1563. //校验车位状态是否为被锁定
  1564. if (t_confirm_space.parkingspace_status() != message::Parkspace_status::eParkspace_locked)
  1565. {
  1566. t_error.set_error_code(DB_QUERY_DATA_FAILED);
  1567. t_error.set_error_level(message::Error_level::MINOR_ERROR);
  1568. t_error.set_error_description("查询车位状态错误");
  1569. LOG(ERROR) << "确认分配 查询车位状态错误 " << error.to_string();
  1570. }
  1571. else
  1572. {
  1573. //修改车位状态为占用
  1574. t_confirm_space.set_parkingspace_status(message::Parkspace_status::eParkspace_occupied);
  1575. error = m_parkspace_operating_function.update_parkspace_data(t_confirm_space);
  1576. if (error != SUCCESS)
  1577. {
  1578. t_error.set_error_code(error.get_error_code());
  1579. t_error.set_error_level((message::Error_level) error.get_error_level());
  1580. LOG(INFO) << "确认分配 数据库更新车位失败 " << error.to_string();
  1581. }
  1582. else
  1583. {
  1584. //修改车辆状态为已入库
  1585. message::Vehicle_status t_vehicle_status = message::Vehicle_status::eVehicle_in_garage;
  1586. error = m_parkspace_operating_function.update_vehicle_with_parkspace(t_confirm_space, t_vehicle_status);
  1587. if (error != SUCCESS)
  1588. {
  1589. t_error.set_error_code(error.get_error_code());
  1590. t_error.set_error_level((message::Error_level) error.get_error_level());
  1591. t_error.set_error_description(error.get_error_description());
  1592. LOG(ERROR) << "确认分配 更新数据库车辆数据失败 " << error.to_string();
  1593. }
  1594. t_error.set_error_code(SUCCESS);
  1595. t_error.set_error_level(message::Error_level::NORMAL);
  1596. LOG(INFO) << "第:" << t_confirm_space.parkingspace_index_id() << "号位 车辆: " << t_confirm_space.car_info().license()<< " 确认占用成功";
  1597. //插入停车记录
  1598. time_t tt = time(NULL);
  1599. tm *t = localtime(&tt);
  1600. char my_time_buf[255];
  1601. memset(my_time_buf, 0, 255);
  1602. sprintf(my_time_buf, "%d-%02d-%02d %02d:%02d:%02d",
  1603. t->tm_year + 1900,
  1604. t->tm_mon + 1,
  1605. t->tm_mday,
  1606. t->tm_hour,
  1607. t->tm_min,
  1608. t->tm_sec);
  1609. t_confirm_space.set_entry_time(my_time_buf);
  1610. error = m_parkspace_operating_function.insert_parking_record(t_confirm_space);
  1611. if ( error != SUCCESS )
  1612. {
  1613. t_error.set_error_code(error.get_error_code());
  1614. t_error.set_error_level((message::Error_level) error.get_error_level());
  1615. LOG(ERROR) << "插入停车记录失败: " << error.to_string();
  1616. }
  1617. //查询停车记录的ID
  1618. int t_record_id;
  1619. error = m_parkspace_operating_function.query_parking_record(t_confirm_space,t_record_id);
  1620. if ( error != SUCCESS )
  1621. {
  1622. t_error.set_error_code(error.get_error_code());
  1623. t_error.set_error_level((message::Error_level) error.get_error_level());
  1624. LOG(ERROR) << "查询停车记录ID失败: " << error.to_string();
  1625. }
  1626. //更新停车记录ID
  1627. error = m_parkspace_operating_function.update_record_id(t_confirm_space.car_info().license(),t_record_id);
  1628. if ( error != SUCCESS )
  1629. {
  1630. t_error.set_error_code(error.get_error_code());
  1631. t_error.set_error_level((message::Error_level) error.get_error_level());
  1632. LOG(ERROR) << "更新停车记录ID失败: " << error.to_string();
  1633. }
  1634. }
  1635. }
  1636. }
  1637. m_parkspace_lock.unlock();
  1638. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  1639. response_msg.set_command_key(command_key);
  1640. response_msg.mutable_error_manager()->CopyFrom(t_error);
  1641. response_msg.mutable_confirm_alloc_space_info()->CopyFrom(t_confirm_space);
  1642. Communication_message response=Communication_message();
  1643. response.reset(t_response_header, response_msg.SerializeAsString());
  1644. Parkspace_communicator::get_instance_references().send_response(&response);
  1645. send_parkspace_data();
  1646. std::cout<<std::endl;
  1647. }