parkspace_manager.cpp 69 KB

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