parkspace_manager.cpp 66 KB

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