parkspace_manager.cpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  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+2) * 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. (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()-(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=(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. int i=0;
  355. while(i<all_big_parkspace_data.parkspace_info_size())
  356. {
  357. int room_id=all_big_parkspace_data.parkspace_info(i).parkingspace_index_id();
  358. //筛选出一号车库的大型车位
  359. if ( room_id%15 <= 8 && room_id %15 >=1
  360. && all_big_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  361. {
  362. //如果是二楼车 则特殊处理
  363. if ( all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id() == 2 )
  364. {
  365. // 二楼用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
  366. 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;
  367. if ( time_min == 0 )
  368. {
  369. time_min=t_min;
  370. parkingspace_index_id=i;
  371. }
  372. else
  373. {
  374. if ( t_min<time_min )
  375. {
  376. time_min=t_min;
  377. parkingspace_index_id=i;
  378. }
  379. }
  380. }
  381. else
  382. {
  383. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  384. float time=(terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  385. (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 +
  386. 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;
  387. if ( time_min == 0 )
  388. {
  389. time_min=time;
  390. parkingspace_index_id=i;
  391. }
  392. else
  393. {
  394. if ( time<time_min )
  395. {
  396. time_min=time;
  397. parkingspace_index_id=i;
  398. }
  399. }
  400. }
  401. }
  402. ++i;
  403. }
  404. //判断大型车位(第三优先级)是否有找到合适的空车位 如果找到了 直接返回
  405. if ( time_min!=0 && parkingspace_index_id!=-1 )
  406. {
  407. parkspace_info.CopyFrom(all_big_parkspace_data.parkspace_info(parkingspace_index_id));
  408. return Error_code::SUCCESS;
  409. }
  410. else
  411. {
  412. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_ALLOCATE_FAILED, Error_level::MINOR_ERROR,
  413. " query_the_optimal_parkspace_serverA error ");
  414. }
  415. }
  416. }
  417. //判断是否是右半库
  418. else if(terminal_id<=6 && terminal_id>=4)
  419. {
  420. //如果是小车 (低于1.55米属于小车)
  421. if ( car_info.car_height() <= MIN_CAR_HIGH )
  422. {
  423. //获取所有小型车位信息
  424. message::Parkspace_allocation_data_msg all_min_parkspace_data;
  425. Error_manager t_error = m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_min_parkspace_data, message::Parkspace_type::MIN_PARKINGSPACE);
  426. if (t_error != SUCCESS)
  427. {
  428. return t_error;
  429. }
  430. //遍历所有小型车位
  431. int i = 0;
  432. while (i < all_min_parkspace_data.parkspace_info_size())
  433. {
  434. int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
  435. //筛选出一号车库的小型车位(第一优先级)的空车位
  436. if (room_id <= 165 && room_id >= 54
  437. && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
  438. {
  439. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  440. float time = (6-terminal_id+2)* TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  441. (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 +
  442. ( (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;
  443. if (time_min == 0)
  444. {
  445. time_min = time;
  446. parkingspace_index_id = i;
  447. }
  448. else
  449. {
  450. if ( time < time_min )
  451. {
  452. time_min = time;
  453. parkingspace_index_id = i;
  454. }
  455. }
  456. }
  457. ++i;
  458. }
  459. //判断小车车位(第一优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
  460. if ( time_min!=0 && parkingspace_index_id!=-1 )
  461. {
  462. parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
  463. return Error_code::SUCCESS;
  464. }
  465. }
  466. if ( car_info.car_height() <= MID_CAR_HIGH )
  467. {
  468. //第二优先级(中型车位)
  469. //获取中型车位
  470. message::Parkspace_allocation_data_msg all_mid_parkspace_data;
  471. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_mid_parkspace_data,message::Parkspace_type::MID_PARKINGSPACE);
  472. if(t_error != SUCCESS)
  473. {
  474. return t_error;
  475. }
  476. int i=0;
  477. while(i<all_mid_parkspace_data.parkspace_info_size())
  478. {
  479. int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
  480. //三楼的中型车位有单独的中跑车停车 且机械臂可以直接抓车到三楼
  481. if ( room_id >= 24 && room_id <= 27 )
  482. {
  483. //只需计算端口到车位的距离
  484. // 三楼中型车位用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
  485. 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;
  486. if ( time_min == 0 )
  487. {
  488. time_min=t_min;
  489. parkingspace_index_id=i;
  490. }
  491. else
  492. {
  493. if ( t_min<time_min )
  494. {
  495. time_min=t_min;
  496. parkingspace_index_id=i;
  497. }
  498. }
  499. }
  500. if ( room_id <= 75 && room_id >= 69
  501. && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  502. {
  503. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  504. float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  505. (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 +
  506. ( ((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;
  507. if ( time_min == 0 )
  508. {
  509. time_min=time;
  510. parkingspace_index_id=i;
  511. }
  512. else
  513. {
  514. if ( time<time_min )
  515. {
  516. time_min=time;
  517. parkingspace_index_id=i;
  518. }
  519. }
  520. }
  521. ++i;
  522. }
  523. //判断中型车位(第二优先级)是否有找到合适的空车位 如果找到了 直接返回
  524. if ( time_min!=0 && parkingspace_index_id!=-1 )
  525. {
  526. parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
  527. return Error_code::SUCCESS;
  528. }
  529. }
  530. if ( car_info.car_height() <= BIG_CAR_HIGH )
  531. {
  532. //第三优先级(大型车位)
  533. //获取大型车位
  534. message::Parkspace_allocation_data_msg all_big_parkspace_data;
  535. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_big_parkspace_data,message::Parkspace_type::BIG_PARKINGSPACE);
  536. if(t_error != SUCCESS)
  537. {
  538. return t_error;
  539. }
  540. int i=0;
  541. while(i<all_big_parkspace_data.parkspace_info_size())
  542. {
  543. int room_id=all_big_parkspace_data.parkspace_info(i).parkingspace_index_id();
  544. //筛选出二号车库的大型车位的空车位
  545. if ( room_id %15 >=9 && room_id %15 <=14 || room_id %15 == 0
  546. && all_big_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  547. {
  548. //如果是二楼车 则特殊处理
  549. if ( all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id() == 2 )
  550. {
  551. // 二楼用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
  552. 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;
  553. if ( time_min == 0 )
  554. {
  555. time_min=t_min;
  556. parkingspace_index_id=i;
  557. }
  558. else
  559. {
  560. if ( t_min<time_min )
  561. {
  562. time_min=t_min;
  563. parkingspace_index_id=i;
  564. }
  565. }
  566. }
  567. else
  568. {
  569. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  570. float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  571. (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 +
  572. ( ((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;
  573. if ( time_min == 0 )
  574. {
  575. time_min=time;
  576. parkingspace_index_id=i;
  577. }
  578. else
  579. {
  580. if ( time<time_min )
  581. {
  582. time_min=time;
  583. parkingspace_index_id=i;
  584. }
  585. }
  586. }
  587. }
  588. ++i;
  589. }
  590. //判断大型车位(第三优先级)是否有找到合适的空车位 如果找到了 直接返回
  591. if ( time_min!=0 && parkingspace_index_id!=-1 )
  592. {
  593. parkspace_info.CopyFrom(all_big_parkspace_data.parkspace_info(parkingspace_index_id));
  594. return Error_code::SUCCESS;
  595. }
  596. else
  597. {
  598. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_ALLOCATE_FAILED, Error_level::MINOR_ERROR,
  599. " query_the_optimal_parkspace_serverA error ");
  600. }
  601. }
  602. }
  603. else
  604. {
  605. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  606. "query_the_optimal_parkspace_serverA error ");
  607. }
  608. }
  609. //找到一个最优车位---方案二
  610. //找左侧车位
  611. Error_manager Parkspace_manager::query_the_optimal_parkspace_serverB1(message::Parkspace_info &parkspace_info,message::Car_info car_info,int terminal_id)
  612. {
  613. //车辆高大于1550mm分在四楼以下,小于1550mm分在四楼以上
  614. /*
  615. * K=j%15
  616. * P=i%6
  617. * S=15*d-(j%15)*(2*d-1)+[6*d-(i%6)(2*d-1)]*W W>1
  618. */
  619. float time_min=0;
  620. int parkingspace_index_id=-1;
  621. //找左侧车位
  622. //如果是小车 (低于1.55米属于小车)
  623. if ( car_info.car_height() <= MIN_CAR_HIGH )
  624. {
  625. //获取所有小型车位信息
  626. message::Parkspace_allocation_data_msg all_min_parkspace_data;
  627. Error_manager t_error = m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_min_parkspace_data, message::Parkspace_type::MIN_PARKINGSPACE);
  628. if (t_error != SUCCESS)
  629. {
  630. return t_error;
  631. }
  632. //遍历所有小型车位
  633. int i = 0;
  634. while (i < all_min_parkspace_data.parkspace_info_size())
  635. {
  636. int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
  637. //筛选出小型车位的中间部分(第一优先级)的空车位
  638. if (room_id % 15 <= 8 && room_id % 15 >= 4
  639. && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
  640. {
  641. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  642. float time = (terminal_id+2) * TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  643. (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 +
  644. (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;
  645. if (time_min == 0)
  646. {
  647. time_min = time;
  648. parkingspace_index_id = i;
  649. }
  650. else
  651. {
  652. if ( time<time_min )
  653. {
  654. time_min=time;
  655. parkingspace_index_id=i;
  656. }
  657. }
  658. }
  659. ++i;
  660. }
  661. //判断小车车位中间部分(第一优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
  662. if ( time_min!=0 && parkingspace_index_id!=-1 )
  663. {
  664. parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
  665. return Error_code::SUCCESS;
  666. }
  667. else
  668. {
  669. //如果没找到则找小车车位第二优先级 (侧边)
  670. int i = 0;
  671. while (i < all_min_parkspace_data.parkspace_info_size())
  672. {
  673. int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
  674. if (room_id % 15 <= 3 && room_id % 15 >= 1
  675. && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
  676. {
  677. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  678. float time = (terminal_id+2) * TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  679. (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 +
  680. (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;
  681. if (time_min == 0)
  682. {
  683. time_min = time;
  684. parkingspace_index_id = i;
  685. }
  686. else
  687. {
  688. if ( time < time_min )
  689. {
  690. time_min = time;
  691. parkingspace_index_id = i;
  692. }
  693. }
  694. }
  695. ++i;
  696. }
  697. //判断小车车位侧边部分(第二优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
  698. if ( time_min!=0 && parkingspace_index_id!=-1 )
  699. {
  700. parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
  701. return Error_code::SUCCESS;
  702. }
  703. }
  704. }
  705. if ( car_info.car_height() <= MID_CAR_HIGH )
  706. {
  707. //找中型车位
  708. //获取中型车位
  709. message::Parkspace_allocation_data_msg all_mid_parkspace_data;
  710. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_mid_parkspace_data,message::Parkspace_type::MID_PARKINGSPACE);
  711. if(t_error != SUCCESS)
  712. {
  713. return t_error;
  714. }
  715. int i=0;
  716. while(i<all_mid_parkspace_data.parkspace_info_size())
  717. {
  718. int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
  719. //如果是六楼的中型车位的中间部分(第一优先级)
  720. if ( room_id >=64 && room_id <= 68
  721. && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  722. {
  723. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  724. float time=(terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  725. (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 +
  726. (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;
  727. if ( time_min == 0 )
  728. {
  729. time_min=time;
  730. parkingspace_index_id=i;
  731. }
  732. else
  733. {
  734. if ( time<time_min )
  735. {
  736. time_min=time;
  737. parkingspace_index_id=i;
  738. }
  739. }
  740. }
  741. ++i;
  742. }
  743. //判断中型车位的中间部分(第一优先级)是否有找到合适的空车位 如果找到了 直接返回
  744. if ( time_min!=0 && parkingspace_index_id!=-1 )
  745. {
  746. parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
  747. return Error_code::SUCCESS;
  748. }
  749. else
  750. {
  751. //如果没找到则找侧边中型车位(第二优先级)
  752. int i=0;
  753. while(i<all_mid_parkspace_data.parkspace_info_size())
  754. {
  755. int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
  756. //如果是六楼的中型车位的侧边(第二优先级)
  757. if ( room_id >=61 && room_id <= 63
  758. && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  759. {
  760. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  761. float time=(terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  762. (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 +
  763. 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;
  764. if ( time_min == 0 )
  765. {
  766. time_min=time;
  767. parkingspace_index_id=i;
  768. }
  769. else
  770. {
  771. if ( time<time_min )
  772. {
  773. time_min=time;
  774. parkingspace_index_id=i;
  775. }
  776. }
  777. }
  778. ++i;
  779. }
  780. //判断中型车位侧边(第二优先级)是否有找到合适的空车位 如果找到了 直接返回
  781. if ( time_min!=0 && parkingspace_index_id!=-1 )
  782. {
  783. parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
  784. return Error_code::SUCCESS;
  785. }
  786. }
  787. }
  788. if ( car_info.car_height() <= BIG_CAR_HIGH )
  789. {
  790. //找大型车位
  791. //获取大型车位
  792. message::Parkspace_allocation_data_msg all_big_parkspace_data;
  793. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_big_parkspace_data,message::Parkspace_type::BIG_PARKINGSPACE);
  794. if(t_error != SUCCESS)
  795. {
  796. return t_error;
  797. }
  798. int i=0;
  799. while(i<all_big_parkspace_data.parkspace_info_size())
  800. {
  801. int room_id=all_big_parkspace_data.parkspace_info(i).parkingspace_index_id();
  802. //筛选出一号车库的小型车位的靠近电梯的左侧三排(第二优先级)的空车位
  803. if ( room_id %15 == 1 || room_id %15 == 2 || room_id %15 == 3 || room_id>=34 && room_id<=38
  804. && all_big_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  805. {
  806. //如果是二楼车 则特殊处理
  807. if ( all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id() == 2 )
  808. {
  809. // 二楼用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
  810. 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;
  811. if ( time_min == 0 )
  812. {
  813. time_min=t_min;
  814. parkingspace_index_id=i;
  815. }
  816. else
  817. {
  818. if ( t_min<time_min )
  819. {
  820. time_min=t_min;
  821. parkingspace_index_id=i;
  822. }
  823. }
  824. }
  825. else
  826. {
  827. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  828. float time=(terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  829. (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 +
  830. 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;
  831. if ( time_min == 0 )
  832. {
  833. time_min=time;
  834. parkingspace_index_id=i;
  835. }
  836. else
  837. {
  838. if ( time<time_min )
  839. {
  840. time_min=time;
  841. parkingspace_index_id=i;
  842. }
  843. }
  844. }
  845. }
  846. ++i;
  847. }
  848. //判断大型车位是否有找到合适的空车位 如果找到了 直接返回
  849. if ( time_min!=0 && parkingspace_index_id!=-1 )
  850. {
  851. parkspace_info.CopyFrom(all_big_parkspace_data.parkspace_info(parkingspace_index_id));
  852. return Error_code::SUCCESS;
  853. }
  854. else
  855. {
  856. //如果没找到则代表左侧没找到合适车位
  857. parkspace_info.set_parkingspace_index_id(-1);
  858. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_ALLOCATE_FAILED, Error_level::NEGLIGIBLE_ERROR,
  859. "query_the_optimal_parkspace_serverB1 error ");
  860. }
  861. }
  862. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  863. " query_the_optimal_parkspace_serverB1 error ");
  864. }
  865. //找右侧车位
  866. Error_manager Parkspace_manager::query_the_optimal_parkspace_serverB2(message::Parkspace_info &parkspace_info,message::Car_info car_info,int terminal_id)
  867. {
  868. //车辆高大于1550mm分在四楼以下,小于1550mm分在四楼以上
  869. /*
  870. * K=j%15
  871. * P=i%6
  872. * S=15*d-(j%15)*(2*d-1)+[6*d-(i%6)(2*d-1)]*W W>1
  873. */
  874. float time_min=0;
  875. int parkingspace_index_id=-1;
  876. //找右侧车位
  877. //如果是小车 (低于1.55米属于小车)
  878. if ( car_info.car_height() <= MIN_CAR_HIGH )
  879. {
  880. //获取所有小型车位信息
  881. message::Parkspace_allocation_data_msg all_min_parkspace_data;
  882. Error_manager t_error = m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_min_parkspace_data, message::Parkspace_type::MIN_PARKINGSPACE);
  883. if (t_error != SUCCESS)
  884. {
  885. return t_error;
  886. }
  887. //遍历所有小型车位
  888. int i = 0;
  889. while (i < all_min_parkspace_data.parkspace_info_size())
  890. {
  891. int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
  892. //筛选出小型车位的中间部分(第一优先级)的空车位
  893. if (room_id % 15 >= 9 && room_id % 15 <= 12
  894. && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
  895. {
  896. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  897. float time = (6-terminal_id+2) * TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  898. (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 +
  899. ( (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;
  900. if (time_min == 0)
  901. {
  902. time_min = time;
  903. parkingspace_index_id = i;
  904. }
  905. else
  906. {
  907. if ( time<time_min )
  908. {
  909. time_min=time;
  910. parkingspace_index_id=i;
  911. }
  912. }
  913. }
  914. ++i;
  915. }
  916. //判断小车车位中间部分(第一优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
  917. if ( time_min!=0 && parkingspace_index_id!=-1 )
  918. {
  919. parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
  920. return Error_code::SUCCESS;
  921. }
  922. else
  923. {
  924. //如果没找到则找小车车位第二优先级 (侧边)
  925. int i = 0;
  926. while (i < all_min_parkspace_data.parkspace_info_size())
  927. {
  928. int room_id=all_min_parkspace_data.parkspace_info(i).parkingspace_index_id();
  929. //筛选出一号车库的小型车位的中间部分(第一优先级)的空车位
  930. if (room_id % 15 == 13 || room_id % 15 == 14 || room_id % 15 == 0
  931. && all_min_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty)
  932. {
  933. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  934. float time = (6-terminal_id+2) * TERMINAL_SPACING / MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  935. (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 +
  936. ( (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;
  937. if (time_min == 0)
  938. {
  939. time_min = time;
  940. parkingspace_index_id = i;
  941. }
  942. else
  943. {
  944. if ( time < time_min )
  945. {
  946. time_min = time;
  947. parkingspace_index_id = i;
  948. }
  949. }
  950. }
  951. ++i;
  952. }
  953. //判断小车车位侧边部分(第二优先级)是否有找到合适的空车位 如果找到了 则不需要继续比较 直接返回
  954. if ( time_min!=0 && parkingspace_index_id!=-1 )
  955. {
  956. parkspace_info.CopyFrom(all_min_parkspace_data.parkspace_info(parkingspace_index_id));
  957. return Error_code::SUCCESS;
  958. }
  959. }
  960. }
  961. if ( car_info.car_height() <= MID_CAR_HIGH )
  962. {
  963. //找中型车位
  964. //获取中型车位
  965. message::Parkspace_allocation_data_msg all_mid_parkspace_data;
  966. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_mid_parkspace_data,message::Parkspace_type::MID_PARKINGSPACE);
  967. if(t_error != SUCCESS)
  968. {
  969. return t_error;
  970. }
  971. int i=0;
  972. while(i<all_mid_parkspace_data.parkspace_info_size())
  973. {
  974. int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
  975. //如果是六楼的中型车位的中间部分(第一优先级)
  976. if ( room_id >= 69 && room_id <= 72
  977. && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  978. {
  979. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  980. float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  981. (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 +
  982. ( ((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;
  983. if ( time_min == 0 )
  984. {
  985. time_min=time;
  986. parkingspace_index_id=i;
  987. }
  988. else
  989. {
  990. if ( time<time_min )
  991. {
  992. time_min=time;
  993. parkingspace_index_id=i;
  994. }
  995. }
  996. }
  997. ++i;
  998. }
  999. //判断中型车位的中间部分(第一优先级)是否有找到合适的空车位 如果找到了 直接返回
  1000. if ( time_min!=0 && parkingspace_index_id!=-1 )
  1001. {
  1002. parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
  1003. return Error_code::SUCCESS;
  1004. }
  1005. else
  1006. {
  1007. //如果没找到则找侧边中型车位(第二优先级)
  1008. int i=0;
  1009. while(i<all_mid_parkspace_data.parkspace_info_size())
  1010. {
  1011. int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
  1012. //如果是六楼的中型车位的侧边(第二优先级)
  1013. if ( room_id >=73 && room_id <= 75
  1014. && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  1015. {
  1016. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  1017. float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  1018. (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 +
  1019. ( ((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;
  1020. if ( time_min == 0 )
  1021. {
  1022. time_min=time;
  1023. parkingspace_index_id=i;
  1024. }
  1025. else
  1026. {
  1027. if ( time<time_min )
  1028. {
  1029. time_min=time;
  1030. parkingspace_index_id=i;
  1031. }
  1032. }
  1033. }
  1034. ++i;
  1035. }
  1036. //判断中型车位侧边(第二优先级)是否有找到合适的空车位 如果找到了 直接返回
  1037. if ( time_min!=0 && parkingspace_index_id!=-1 )
  1038. {
  1039. parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
  1040. return Error_code::SUCCESS;
  1041. }
  1042. }
  1043. }
  1044. if ( car_info.car_height() <= BIG_CAR_HIGH )
  1045. {
  1046. //找大型车位
  1047. //获取大型车位
  1048. message::Parkspace_allocation_data_msg all_big_parkspace_data;
  1049. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_big_parkspace_data,message::Parkspace_type::BIG_PARKINGSPACE);
  1050. if(t_error != SUCCESS)
  1051. {
  1052. return t_error;
  1053. }
  1054. int i=0;
  1055. while(i<all_big_parkspace_data.parkspace_info_size())
  1056. {
  1057. int room_id=all_big_parkspace_data.parkspace_info(i).parkingspace_index_id();
  1058. //筛选出大型车位的靠近电梯的右侧三排(第二优先级)的空车位
  1059. if ( room_id %15 == 13 || room_id %15 == 14 || room_id %15 == 0|| room_id>=39 && room_id<=42
  1060. && all_big_parkspace_data.parkspace_info(i).parkingspace_status() == message::Parkspace_status::eParkspace_empty )
  1061. {
  1062. //如果是二楼车 则特殊处理
  1063. if ( all_big_parkspace_data.parkspace_info(i).parkingspace_floor_id() == 2 )
  1064. {
  1065. // 二楼用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
  1066. 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;
  1067. if ( time_min == 0 )
  1068. {
  1069. time_min=t_min;
  1070. parkingspace_index_id=i;
  1071. }
  1072. else
  1073. {
  1074. if ( t_min<time_min )
  1075. {
  1076. time_min=t_min;
  1077. parkingspace_index_id=i;
  1078. }
  1079. }
  1080. }
  1081. else
  1082. {
  1083. //用时=目标终端到电梯口的距离/中跑车速度+(目标车位楼层-2)*楼层高度/电梯速度+电梯口到目标车位的距离/中跑车速度
  1084. float time=(6-terminal_id+2)*TERMINAL_SPACING/MEDIUM_CAR_SPEED + DOWNSTAIRS_SPORTS_TIME +
  1085. (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 +
  1086. ( (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;
  1087. if ( time_min == 0 )
  1088. {
  1089. time_min=time;
  1090. parkingspace_index_id=i;
  1091. }
  1092. else
  1093. {
  1094. if ( time<time_min )
  1095. {
  1096. time_min=time;
  1097. parkingspace_index_id=i;
  1098. }
  1099. }
  1100. }
  1101. }
  1102. ++i;
  1103. }
  1104. //判断大型车位是否有找到合适的空车位 如果找到了 直接返回
  1105. if ( time_min!=0 && parkingspace_index_id!=-1 )
  1106. {
  1107. parkspace_info.CopyFrom(all_big_parkspace_data.parkspace_info(parkingspace_index_id));
  1108. return Error_code::SUCCESS;
  1109. }
  1110. else
  1111. {
  1112. //如果没找到则代表右侧没找到合适车位
  1113. parkspace_info.set_parkingspace_index_id(-1);
  1114. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_ALLOCATE_FAILED, Error_level::NEGLIGIBLE_ERROR,
  1115. "query_the_optimal_parkspace_serverB2 error ");
  1116. }
  1117. }
  1118. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  1119. " query_the_optimal_parkspace_serverB2 error ");
  1120. }
  1121. //找缓存区车位
  1122. Error_manager Parkspace_manager::query_the_optimal_parkspace_serverB3(message::Parkspace_info &parkspace_info,message::Car_info car_info,int terminal_id)
  1123. {
  1124. //车辆高大于1550mm分在四楼以下,小于1550mm分在四楼以上
  1125. /*
  1126. * K=j%15
  1127. * P=i%6
  1128. * S=15*d-(j%15)*(2*d-1)+[6*d-(i%6)(2*d-1)]*W W>1
  1129. */
  1130. float time_min=0;
  1131. int parkingspace_index_id=-1;
  1132. //找缓存区车位 缓冲区只有中型车位和大型车位
  1133. if ( car_info.car_height() <= MID_CAR_HIGH )
  1134. {
  1135. message::Parkspace_allocation_data_msg all_mid_parkspace_data;
  1136. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_mid_parkspace_data,message::Parkspace_type::MID_PARKINGSPACE);
  1137. if(t_error != SUCCESS)
  1138. {
  1139. return t_error;
  1140. }
  1141. int i=0;
  1142. while (i < all_mid_parkspace_data.parkspace_info_size())
  1143. {
  1144. //筛选中型车位缓冲区
  1145. int room_id=all_mid_parkspace_data.parkspace_info(i).parkingspace_index_id();
  1146. if ( room_id >= 19 && room_id <= 27
  1147. && all_mid_parkspace_data.parkspace_info(i).parkingspace_status() == message::eParkspace_empty)
  1148. {
  1149. //只需计算端口到车位的距离
  1150. // 三楼中型用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
  1151. 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;
  1152. if ( time_min == 0 )
  1153. {
  1154. time_min=t_min;
  1155. parkingspace_index_id=i;
  1156. }
  1157. else
  1158. {
  1159. if ( t_min<time_min )
  1160. {
  1161. time_min=t_min;
  1162. parkingspace_index_id=i;
  1163. }
  1164. }
  1165. }
  1166. ++i;
  1167. }
  1168. //判断缓冲区的中型车位是否有找到合适的空车位 如果找到了 直接返回
  1169. if ( time_min!=0 && parkingspace_index_id!=-1 )
  1170. {
  1171. parkspace_info.CopyFrom(all_mid_parkspace_data.parkspace_info(parkingspace_index_id));
  1172. return Error_code::SUCCESS;
  1173. }
  1174. }
  1175. if( car_info.car_height() <= BIG_CAR_HIGH )
  1176. {
  1177. message::Parkspace_allocation_data_msg all_big_parkspace_data;
  1178. Error_manager t_error=m_parkspace_operating_function.get_specify_the_type_parkspace_info(all_big_parkspace_data,message::Parkspace_type::BIG_PARKINGSPACE);
  1179. if(t_error != SUCCESS)
  1180. {
  1181. return t_error;
  1182. }
  1183. int i=0;
  1184. while (i < all_big_parkspace_data.parkspace_info_size())
  1185. {
  1186. //筛选中型车位缓冲区
  1187. int room_id=all_big_parkspace_data.parkspace_info(i).parkingspace_index_id();
  1188. if ( room_id >= 4 && room_id <= 12
  1189. && all_big_parkspace_data.parkspace_info(i).parkingspace_status() == message::eParkspace_empty)
  1190. {
  1191. //只需计算端口到车位的距离
  1192. // 二楼中型用时=| 目标车位到电梯的距离 - 终端到电梯的距离 | / 中跑车速度
  1193. 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;
  1194. if ( time_min == 0 )
  1195. {
  1196. time_min=t_min;
  1197. parkingspace_index_id=i;
  1198. }
  1199. else
  1200. {
  1201. if ( t_min<time_min )
  1202. {
  1203. time_min=t_min;
  1204. parkingspace_index_id=i;
  1205. }
  1206. }
  1207. }
  1208. }
  1209. //判断缓冲区的中型车位是否有找到合适的空车位 如果找到了 直接返回
  1210. if ( time_min!=0 && parkingspace_index_id!=-1 )
  1211. {
  1212. parkspace_info.CopyFrom(all_big_parkspace_data.parkspace_info(parkingspace_index_id));
  1213. return Error_code::SUCCESS;
  1214. }
  1215. else
  1216. {
  1217. //如果没找到则代表缓冲区没找到合适车位
  1218. parkspace_info.set_parkingspace_index_id(-1);
  1219. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_ALLOCATE_FAILED, Error_level::NEGLIGIBLE_ERROR,
  1220. "query_the_optimal_parkspace_serverB2 error ");
  1221. }
  1222. }
  1223. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  1224. " query_the_optimal_parkspace_serverB3 error ");
  1225. }
  1226. //分配车位线程函数
  1227. void Parkspace_manager::execute_for_allocate(message::Car_info car_info, int terminal_id, std::string command_key)
  1228. {
  1229. LOG(INFO) << "分配车位 "<<"牌号:"<<car_info.license()<<" command_key="<<command_key; //huli printf command_key
  1230. //根据请求的信息反馈分配的车位,并封装发送
  1231. message::Parkspace_allocation_response_msg response_msg;
  1232. message::Base_info t_response_header;
  1233. t_response_header.set_msg_type(message::Message_type::eParkspace_allocation_response_msg);
  1234. t_response_header.set_timeout_ms(1000);
  1235. t_response_header.set_sender(message::Communicator::eParkspace);
  1236. t_response_header.set_receiver(message::Communicator::eMain);
  1237. message::Error_manager t_error;
  1238. message::Parkspace_info t_allocated_space;
  1239. Error_manager error;
  1240. //分配之前查询车辆是否已经存在
  1241. if(check_car_existence(car_info.license()))
  1242. {
  1243. t_error.set_error_code(PARKSPACE_ALLOCATOR_CAR_ALREADY_EXIST);
  1244. t_error.set_error_level(message::Error_level::MAJOR_ERROR);
  1245. t_error.set_error_description("车辆已存在");
  1246. LOG(ERROR) << car_info.license()<<" 车辆已存在";
  1247. }
  1248. else
  1249. {
  1250. m_parkspace_lock.lock();
  1251. // 如果车辆不存在 则找到一个最优车位(方案A)----车库分为两个独立部分,左库和右库如果1~3号终端来车 则在左库分配车位,4~号终端来车 则在右库分配车位
  1252. error=query_the_optimal_parkspace_serverA(t_allocated_space,car_info,terminal_id);
  1253. // 如果车辆不存在 则找到一个最优车位(方案B)----分配三个车位 左边分配一个 右边分配一个 缓冲区分配一个 最后使用那个由调度决定
  1254. // message::Parkspace_info t_allocated_space_left;
  1255. // message::Parkspace_info t_allocated_space_rigth;
  1256. // message::Parkspace_info t_allocated_space_temporary;
  1257. //
  1258. // error=query_the_optimal_parkspace_serverB1(t_allocated_space_left,car_info,terminal_id);
  1259. // LOG(INFO) << " 左侧车位ID: "<<t_allocated_space_left.parkingspace_index_id();
  1260. // error=query_the_optimal_parkspace_serverB2(t_allocated_space_rigth,car_info,terminal_id);
  1261. // LOG(INFO) << " 右侧车位ID: "<<t_allocated_space_rigth.parkingspace_index_id();
  1262. // error=query_the_optimal_parkspace_serverB3(t_allocated_space_temporary,car_info,terminal_id);
  1263. // LOG(INFO) << " 缓冲区车位ID: "<<t_allocated_space_temporary.parkingspace_index_id();
  1264. //如果车辆不存在则获取一个空车位
  1265. // error=m_parkspace_operating_function.query_one_empty_parkspace(t_allocated_space);
  1266. if ( error != Error_code::SUCCESS )
  1267. {
  1268. t_error.set_error_code(error.get_error_code());
  1269. t_error.set_error_level((message::Error_level)error.get_error_level());
  1270. t_error.set_error_description(error.get_error_description());
  1271. LOG(ERROR) << car_info.license()<<" 获取空车位失败 "<<error.to_string();
  1272. }
  1273. else
  1274. {
  1275. //尚未加入操作回退步骤
  1276. //修改车位状态为锁定
  1277. t_allocated_space.set_parkingspace_status(message::Parkspace_status::eParkspace_locked);
  1278. t_allocated_space.mutable_car_info()->CopyFrom(car_info);
  1279. // 分配车位后更新数据库中车位状态
  1280. error=m_parkspace_operating_function.update_parkspace_data(t_allocated_space);
  1281. if ( error != SUCCESS )
  1282. {
  1283. t_error.set_error_code(error.get_error_code());
  1284. t_error.set_error_level((message::Error_level)error.get_error_level());
  1285. t_error.set_error_description(error.get_error_description());
  1286. LOG(ERROR) << car_info.license()<<" 更新车位数据失败 "<<error.to_string();
  1287. }
  1288. else
  1289. {
  1290. //修改车辆状态为停车中
  1291. message::Vehicle_status t_vehicle_status = message::Vehicle_status::eVehicle_parking;
  1292. error=m_parkspace_operating_function.update_vehicle_with_parkspace(t_allocated_space,t_vehicle_status);
  1293. if ( error != SUCCESS )
  1294. {
  1295. t_error.set_error_code(error.get_error_code());
  1296. t_error.set_error_level((message::Error_level)error.get_error_level());
  1297. t_error.set_error_description(error.get_error_description());
  1298. LOG(ERROR) << car_info.license()<<" 更新车辆数据失败 "<<error.to_string();
  1299. }
  1300. else
  1301. {
  1302. t_error.set_error_code(SUCCESS);
  1303. t_error.set_error_level(message::Error_level::NORMAL);
  1304. LOG(INFO) <<" 分配车位成功 "<<"车位ID:"<<t_allocated_space.parkingspace_index_id()<<" 车牌号:"<<t_allocated_space.car_info().license();
  1305. }
  1306. }
  1307. }
  1308. m_parkspace_lock.unlock();
  1309. }
  1310. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  1311. response_msg.set_command_key(command_key);
  1312. response_msg.mutable_error_manager()->CopyFrom(t_error);
  1313. response_msg.mutable_allocated_space_info()->CopyFrom(t_allocated_space);
  1314. Communication_message response=Communication_message();
  1315. response.reset(t_response_header, response_msg.SerializeAsString());
  1316. Parkspace_communicator::get_instance_references().send_response(&response);
  1317. send_parkspace_data();
  1318. std::cout<<std::endl;
  1319. }
  1320. //查询车位线程函数
  1321. void Parkspace_manager::execute_for_search(message::Car_info car_info,std::string command_key)
  1322. {
  1323. LOG(INFO) << "查询车位 "<<"牌号:"<<car_info.license()<<" command_key="<<command_key; //huli printf command_key
  1324. //根据车辆凭证信息查询车辆位置
  1325. message::Parkspace_search_response_msg response_msg;
  1326. message::Base_info t_response_header;
  1327. message::Error_manager t_error;
  1328. message::Parkspace_info t_car_position;
  1329. t_response_header.set_msg_type(message::Message_type::eParkspace_search_response_msg);
  1330. t_response_header.set_timeout_ms(1000);
  1331. t_response_header.set_sender(message::Communicator::eParkspace);
  1332. t_response_header.set_receiver(message::Communicator::eMain);
  1333. //查询车辆位置
  1334. message::Parkspace_info parkspace_info;
  1335. Error_manager error=m_parkspace_operating_function.query_one_parkspace_with_license(car_info.license(),parkspace_info);
  1336. if ( error != SUCCESS )
  1337. {
  1338. t_error.set_error_code(error.get_error_code());
  1339. t_error.set_error_level((message::Error_level)error.get_error_level());
  1340. t_error.set_error_description(error.get_error_description());
  1341. LOG(ERROR) << "查询车位失败 "<<error.to_string();
  1342. }
  1343. else
  1344. {
  1345. t_error.set_error_code(SUCCESS);
  1346. t_error.set_error_level(message::Error_level::NORMAL);
  1347. LOG(INFO) << "车辆"<<parkspace_info.car_info().license()<<" 在"<<parkspace_info.parkingspace_index_id()<<"号车位上";
  1348. }
  1349. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  1350. response_msg.set_command_key(command_key);
  1351. response_msg.mutable_error_manager()->CopyFrom(t_error);
  1352. response_msg.mutable_car_position()->CopyFrom(t_car_position);
  1353. Communication_message response=Communication_message();
  1354. response.reset(t_response_header, response_msg.SerializeAsString());
  1355. Parkspace_communicator::get_instance_references().send_response(&response);
  1356. std::cout<<std::endl;
  1357. }
  1358. //释放车位线程函数
  1359. void Parkspace_manager::execute_for_release(message::Parkspace_info space_info, std::string command_key)
  1360. {
  1361. LOG(INFO) << "释放车位 "<<"牌号:"<<space_info.car_info().license()<<" command_key="<<command_key;
  1362. //根据车位信息定位待释放车位位置,车辆凭证号用于校验
  1363. message::Parkspace_release_response_msg response_msg;
  1364. message::Base_info t_response_header;
  1365. t_response_header.set_msg_type(message::eParkspace_release_response_msg);
  1366. t_response_header.set_sender(message::eParkspace);
  1367. t_response_header.set_receiver(message::eMain);
  1368. t_response_header.set_timeout_ms(1000);
  1369. message::Error_manager t_error;
  1370. message::Parkspace_info t_release_space;
  1371. //找到待释放的车位
  1372. m_parkspace_lock.lock();
  1373. Error_manager error=m_parkspace_operating_function.query_one_parkspace_with_parkspace_id(space_info.parkingspace_index_id(),t_release_space);
  1374. if ( error != SUCCESS )
  1375. {
  1376. t_error.set_error_code(error.get_error_code());
  1377. t_error.set_error_level((message::Error_level)error.get_error_level());
  1378. t_error.set_error_description(error.get_error_description());
  1379. LOG(ERROR) << "释放车位失败 "<<error.to_string();
  1380. }
  1381. else if(!t_release_space.has_car_info())
  1382. {
  1383. t_error.set_error_code(PARKSPACE_ALLOCATOR_RELEASE_FAILED);
  1384. t_error.set_error_level(message::Error_level::MAJOR_ERROR);
  1385. LOG(WARNING) << "传入待释放车位无车辆信息";
  1386. }
  1387. else
  1388. {
  1389. t_release_space.set_parkingspace_status(message::Parkspace_status::eParkspace_empty);
  1390. error = m_parkspace_operating_function.update_parkspace_data(t_release_space);
  1391. if (error != SUCCESS)
  1392. {
  1393. t_error.set_error_code(error.get_error_code());
  1394. t_error.set_error_level((message::Error_level) error.get_error_level());
  1395. t_error.set_error_description(error.get_error_description());
  1396. LOG(ERROR) << "释放车位 更新数据库车位数据失败 " << error.to_string();
  1397. }
  1398. else
  1399. {
  1400. message::Vehicle_status t_vehicle_status = message::Vehicle_status::eVehicle_idle;
  1401. error = m_parkspace_operating_function.update_vehicle_with_parkspace(t_release_space, t_vehicle_status);
  1402. if (error != SUCCESS)
  1403. {
  1404. t_error.set_error_code(error.get_error_code());
  1405. t_error.set_error_level((message::Error_level) error.get_error_level());
  1406. t_error.set_error_description(error.get_error_description());
  1407. LOG(ERROR) << "释放车位 更新数据库车辆数据失败 " << error.to_string();
  1408. }
  1409. else
  1410. {
  1411. t_error.set_error_code(SUCCESS);
  1412. t_error.set_error_level(message::Error_level::NORMAL);
  1413. LOG(INFO) << "第:" << t_release_space.parkingspace_index_id() << "号位 车辆: " << t_release_space.car_info().license()
  1414. << " 释放成功";
  1415. //获取预约ID
  1416. int t_park_record_id;
  1417. error = m_parkspace_operating_function.query_vehicle(t_release_space.car_info().license(),
  1418. t_vehicle_status, t_park_record_id);
  1419. if (error != SUCCESS)
  1420. {
  1421. t_error.set_error_code(error.get_error_code());
  1422. t_error.set_error_level((message::Error_level) error.get_error_level());
  1423. t_error.set_error_description(error.get_error_description());
  1424. LOG(ERROR) << "释放车位 数据库查询车辆信息失败 " << error.to_string();
  1425. }
  1426. else if (t_vehicle_status == message::Vehicle_status::eVehicle_idle)
  1427. {
  1428. time_t tt = time(NULL);
  1429. tm *t = localtime(&tt);
  1430. char my_time_buf[255];
  1431. memset(my_time_buf, 0, 255);
  1432. sprintf(my_time_buf, "%d-%02d-%02d %02d:%02d:%02d",
  1433. t->tm_year + 1900,
  1434. t->tm_mon + 1,
  1435. t->tm_mday,
  1436. t->tm_hour,
  1437. t->tm_min,
  1438. t->tm_sec);
  1439. t_release_space.set_leave_time(my_time_buf);
  1440. error = m_parkspace_operating_function.update_parking_record(t_release_space, t_park_record_id);
  1441. if (error != SUCCESS)
  1442. {
  1443. t_error.set_error_code(error.get_error_code());
  1444. t_error.set_error_level((message::Error_level) error.get_error_level());
  1445. t_error.set_error_description(error.get_error_description());
  1446. LOG(ERROR) << "更新停车记录失败: " << error.to_string();
  1447. }
  1448. }
  1449. }
  1450. }
  1451. }
  1452. m_parkspace_lock.unlock();
  1453. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  1454. response_msg.set_command_key(command_key);
  1455. response_msg.mutable_error_manager()->CopyFrom(t_error);
  1456. response_msg.mutable_release_space_info()->CopyFrom(t_release_space);
  1457. Communication_message response=Communication_message();
  1458. response.reset(t_response_header, response_msg.SerializeAsString());
  1459. Parkspace_communicator::get_instance_references().send_response(&response);
  1460. send_parkspace_data();
  1461. std::cout<<std::endl;
  1462. }
  1463. //强制更新车位信息线程函数
  1464. void Parkspace_manager::execute_for_force_update(message::Parkspace_info space_info, std::string command_key)
  1465. {
  1466. LOG(INFO) << "手动 "<<"牌号:"<<space_info.car_info().license()<<" command_key="<<command_key;
  1467. //根据车位信息定位待释放车位位置,车辆凭证号用于校验
  1468. //!!!!!此处跳过外部处理与调用的过程,直接在内部调用,发送分配结果用于测试,目前一直发布第一个车位
  1469. message::Parkspace_force_update_response_msg response_msg;
  1470. message::Base_info t_response_header;
  1471. t_response_header.set_msg_type(message::eParkspace_force_update_response_msg);
  1472. t_response_header.set_sender(message::eParkspace);
  1473. t_response_header.set_receiver(message::eMain);
  1474. t_response_header.set_timeout_ms(1000);
  1475. message::Error_manager t_error;
  1476. message::Parkspace_info t_update_space;
  1477. Error_manager error;
  1478. // id 9999为特殊情况,复位所有车位状态
  1479. if (space_info.parkingspace_index_id() == 9999)
  1480. {
  1481. // 复位所有车位状态
  1482. error=m_parkspace_operating_function.clear_all_parkspace_info();
  1483. if( error != SUCCESS)
  1484. {
  1485. t_error.set_error_code(error.get_error_code());
  1486. t_error.set_error_level((message::Error_level) error.get_error_level());
  1487. LOG(INFO) << "手动更新所有车位失败 "<<error.to_string();
  1488. }
  1489. else
  1490. {
  1491. t_error.set_error_code(SUCCESS);
  1492. t_error.set_error_level(message::Error_level::NORMAL);
  1493. LOG(INFO) << "所有车位已手动更新 ";
  1494. }
  1495. }
  1496. else
  1497. {
  1498. //手动修改车位信息
  1499. error = m_parkspace_operating_function.update_parkspace_data(t_update_space);
  1500. if (error != SUCCESS)
  1501. {
  1502. t_error.set_error_code(error.get_error_code());
  1503. t_error.set_error_level((message::Error_level)error.get_error_level());
  1504. LOG(INFO) << "手动 数据库更新车位失败 "<<error.to_string();
  1505. }
  1506. else
  1507. {
  1508. t_error.set_error_code(SUCCESS);
  1509. t_error.set_error_level(message::Error_level::NORMAL);
  1510. LOG(INFO) << "第" << t_update_space.parkingspace_index_id() << "号位已手动更新";
  1511. }
  1512. }
  1513. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  1514. response_msg.set_command_key(command_key);
  1515. response_msg.mutable_error_manager()->CopyFrom(t_error);
  1516. response_msg.mutable_update_space_info()->CopyFrom(t_update_space);
  1517. Communication_message response=Communication_message();
  1518. response.reset(t_response_header, response_msg.SerializeAsString());
  1519. Parkspace_communicator::get_instance_references().send_response(&response);
  1520. send_parkspace_data();
  1521. }
  1522. //确认分配车位线程函数
  1523. void Parkspace_manager::execute_for_confirm_alloc(message::Parkspace_info space_info, std::string command_key)
  1524. {
  1525. LOG(INFO) << "确认分配 " << "牌号:" << space_info.car_info().license() << " command_key=" << command_key;
  1526. //根据车位信息定位待确认占用车位
  1527. message::Parkspace_confirm_alloc_response_msg response_msg;
  1528. message::Base_info t_response_header;
  1529. t_response_header.set_msg_type(message::eParkspace_confirm_alloc_response_msg);
  1530. t_response_header.set_sender(message::eParkspace);
  1531. t_response_header.set_receiver(message::eMain);
  1532. t_response_header.set_timeout_ms(1000);
  1533. message::Error_manager t_error;
  1534. message::Parkspace_info t_confirm_space;
  1535. Error_manager error;
  1536. //获取确认分配的车位
  1537. m_parkspace_lock.lock();
  1538. error = m_parkspace_operating_function.query_one_parkspace_with_parkspace_id(space_info.parkingspace_index_id(),
  1539. t_confirm_space);
  1540. if (error != SUCCESS)
  1541. {
  1542. t_error.set_error_code(error.get_error_code());
  1543. t_error.set_error_level((message::Error_level) error.get_error_level());
  1544. t_error.set_error_description(error.get_error_description());
  1545. LOG(ERROR) << "确认分配 查询车位信息失败 " << error.to_string();
  1546. }
  1547. else if (!t_confirm_space.has_car_info())
  1548. {
  1549. t_error.set_error_code(PARKSPACE_ALLOCATOR_RELEASE_FAILED);
  1550. t_error.set_error_level(message::Error_level::MAJOR_ERROR);
  1551. LOG(WARNING) << "传入待确认车位无车辆信息";
  1552. }
  1553. else
  1554. {
  1555. //校验车位状态是否为被锁定
  1556. if (t_confirm_space.parkingspace_status() != message::Parkspace_status::eParkspace_locked)
  1557. {
  1558. t_error.set_error_code(DB_QUERY_DATA_FAILED);
  1559. t_error.set_error_level(message::Error_level::MINOR_ERROR);
  1560. t_error.set_error_description("查询车位状态错误");
  1561. LOG(ERROR) << "确认分配 查询车位状态错误 " << error.to_string();
  1562. }
  1563. else
  1564. {
  1565. //修改车位状态为占用
  1566. t_confirm_space.set_parkingspace_status(message::Parkspace_status::eParkspace_occupied);
  1567. error = m_parkspace_operating_function.update_parkspace_data(t_confirm_space);
  1568. if (error != SUCCESS)
  1569. {
  1570. t_error.set_error_code(error.get_error_code());
  1571. t_error.set_error_level((message::Error_level) error.get_error_level());
  1572. LOG(INFO) << "确认分配 数据库更新车位失败 " << error.to_string();
  1573. }
  1574. else
  1575. {
  1576. //修改车辆状态为已入库
  1577. message::Vehicle_status t_vehicle_status = message::Vehicle_status::eVehicle_in_garage;
  1578. error = m_parkspace_operating_function.update_vehicle_with_parkspace(t_confirm_space, t_vehicle_status);
  1579. if (error != SUCCESS)
  1580. {
  1581. t_error.set_error_code(error.get_error_code());
  1582. t_error.set_error_level((message::Error_level) error.get_error_level());
  1583. t_error.set_error_description(error.get_error_description());
  1584. LOG(ERROR) << "确认分配 更新数据库车辆数据失败 " << error.to_string();
  1585. }
  1586. t_error.set_error_code(SUCCESS);
  1587. t_error.set_error_level(message::Error_level::NORMAL);
  1588. LOG(INFO) << "第:" << t_confirm_space.parkingspace_index_id() << "号位 车辆: " << t_confirm_space.car_info().license()<< " 确认占用成功";
  1589. //插入停车记录
  1590. time_t tt = time(NULL);
  1591. tm *t = localtime(&tt);
  1592. char my_time_buf[255];
  1593. memset(my_time_buf, 0, 255);
  1594. sprintf(my_time_buf, "%d-%02d-%02d %02d:%02d:%02d",
  1595. t->tm_year + 1900,
  1596. t->tm_mon + 1,
  1597. t->tm_mday,
  1598. t->tm_hour,
  1599. t->tm_min,
  1600. t->tm_sec);
  1601. t_confirm_space.set_entry_time(my_time_buf);
  1602. error = m_parkspace_operating_function.insert_parking_record(t_confirm_space);
  1603. if ( error != SUCCESS )
  1604. {
  1605. t_error.set_error_code(error.get_error_code());
  1606. t_error.set_error_level((message::Error_level) error.get_error_level());
  1607. LOG(ERROR) << "插入停车记录失败: " << error.to_string();
  1608. }
  1609. //查询停车记录的ID
  1610. int t_record_id;
  1611. error = m_parkspace_operating_function.query_parking_record(t_confirm_space,t_record_id);
  1612. if ( error != SUCCESS )
  1613. {
  1614. t_error.set_error_code(error.get_error_code());
  1615. t_error.set_error_level((message::Error_level) error.get_error_level());
  1616. LOG(ERROR) << "查询停车记录ID失败: " << error.to_string();
  1617. }
  1618. //更新停车记录ID
  1619. error = m_parkspace_operating_function.update_record_id(t_confirm_space.car_info().license(),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. }
  1627. }
  1628. }
  1629. m_parkspace_lock.unlock();
  1630. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  1631. response_msg.set_command_key(command_key);
  1632. response_msg.mutable_error_manager()->CopyFrom(t_error);
  1633. response_msg.mutable_confirm_alloc_space_info()->CopyFrom(t_confirm_space);
  1634. Communication_message response=Communication_message();
  1635. response.reset(t_response_header, response_msg.SerializeAsString());
  1636. Parkspace_communicator::get_instance_references().send_response(&response);
  1637. send_parkspace_data();
  1638. std::cout<<std::endl;
  1639. }