parkspace_manager.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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.id_struct().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 ";
  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, 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 ";
  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, request.command_key());
  115. return SUCCESS;
  116. }
  117. //手动查询请求
  118. case Communication_message::eParkspace_manual_search_request_msg:
  119. {
  120. message::Parkspace_manual_search_request_msg request;
  121. bool result = request.ParseFromString(p_msg->get_message_buf());
  122. LOG(INFO)<<"manual search request, parkspace ";
  123. if(!result)
  124. {
  125. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_PARSE_ERROR, Error_level::MINOR_ERROR,
  126. " message::Parkspace_manual_search_request_msg ParseFromString error ");
  127. }
  128. //往线程池添加执行任务, 之后会唤醒一个线程去执行他.
  129. // std::cout<<"手动:"<<request.DebugString()<<std::endl;
  130. m_thread_pool.enqueue(&Parkspace_manager::execute_for_manual_search, this, request.car_info(), request.command_key());
  131. return SUCCESS;
  132. }
  133. // 车位确认占用请求消息
  134. case Communication_message::eParkspace_confirm_alloc_request_msg:
  135. {
  136. message::Parkspace_confirm_alloc_request_msg request;
  137. bool result = request.ParseFromString(p_msg->get_message_buf());
  138. LOG(INFO)<<"confirm alloc request ";
  139. if(!result)
  140. {
  141. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_PARSE_ERROR, Error_level::MINOR_ERROR,
  142. " message::Parkspace_force_update_request_msg ParseFromString error ");
  143. }
  144. m_thread_pool.enqueue(&Parkspace_manager::execute_for_confirm_alloc, this, request);
  145. return SUCCESS;
  146. }
  147. //车位数据刷新请求
  148. case Communication_message::eParkspace_refresh_request_msg:
  149. {
  150. return send_parkspace_data();
  151. }
  152. }
  153. return Error_manager(Error_code::PARKSPACE_ALLOCATOR_MSG_REQUEST_TYPE_ERROR, Error_level::MINOR_ERROR,
  154. " fun error ");
  155. }
  156. Error_manager Parkspace_manager::encapsulating_heartbeat_messages()
  157. {
  158. //封装心跳消息
  159. Database_controller::Database_controller_status t_database_controller_status;
  160. Parkspace_manager::Parkspace_manager_satus t_parkspace_manager_satus;
  161. t_database_controller_status = Database_controller::get_instance_pointer()->get_database_controller_status();
  162. t_parkspace_manager_satus = Parkspace_manager::get_instance_pointer()->get_parkspace_manage_status();
  163. //车位信息消息赋值
  164. message::Parkspace_allocation_status_msg t_parkspace_status_msg;
  165. message::Base_info t_base_info;
  166. message::Error_manager t_error;
  167. t_base_info.set_msg_type(message::Message_type::eParkspace_allocation_status_msg);
  168. t_base_info.set_timeout_ms(5000);
  169. t_base_info.set_sender(message::Communicator::eParkspace);
  170. t_base_info.set_receiver(message::Communicator::eMain);
  171. t_error.set_error_code(0);
  172. t_parkspace_status_msg.mutable_base_info()->CopyFrom(t_base_info);
  173. t_parkspace_status_msg.mutable_error_manager()->CopyFrom(t_error);
  174. t_parkspace_status_msg.set_database_controller_status((message::Database_controller_status)t_database_controller_status);
  175. t_parkspace_status_msg.set_parkspace_manager_satus((message::Parkspace_manager_satus)t_parkspace_manager_satus);
  176. message::Parkspace_allocation_data_msg parkspaceAllocationDataMsg[UNIT_NUMBER];
  177. int t_small_parkspace_size[UNIT_NUMBER];
  178. int t_medium_parkspace_size[UNIT_NUMBER];
  179. int t_large_parkspace_size[UNIT_NUMBER];
  180. int t_total_parkspace_size[UNIT_NUMBER];
  181. for (int i = 0; i< UNIT_NUMBER; ++i)
  182. {
  183. m_parkspace_operating_function.get_specify_the_type_status_unit_parkspace_info(parkspaceAllocationDataMsg[i],message::Parkspace_type::MIN_PARKINGSPACE,message::Parkspace_status::eParkspace_empty,i);
  184. t_small_parkspace_size[i] = parkspaceAllocationDataMsg[i].parkspace_info_ex_size();
  185. parkspaceAllocationDataMsg[i].clear_parkspace_info_ex();
  186. m_parkspace_operating_function.get_specify_the_type_status_unit_parkspace_info(parkspaceAllocationDataMsg[i],message::Parkspace_type::MID_PARKINGSPACE,message::Parkspace_status::eParkspace_empty,i);
  187. t_medium_parkspace_size[i] = parkspaceAllocationDataMsg[i].parkspace_info_ex_size();
  188. parkspaceAllocationDataMsg[i].clear_parkspace_info_ex();
  189. m_parkspace_operating_function.get_specify_the_type_status_unit_parkspace_info(parkspaceAllocationDataMsg[i],message::Parkspace_type::BIG_PARKINGSPACE,message::Parkspace_status::eParkspace_empty,i);
  190. t_large_parkspace_size[i] = parkspaceAllocationDataMsg[i].parkspace_info_ex_size();
  191. parkspaceAllocationDataMsg[i].clear_parkspace_info_ex();
  192. t_total_parkspace_size[i] = t_small_parkspace_size[i] + t_medium_parkspace_size[i] + t_large_parkspace_size[i];
  193. t_parkspace_status_msg.set_unit_id(i);
  194. t_parkspace_status_msg.set_small_parkspace_remaining_number(t_small_parkspace_size[i]);
  195. t_parkspace_status_msg.set_medium_parkspace_remaining_number(t_medium_parkspace_size[i]);
  196. t_parkspace_status_msg.set_large_parkspace_remaining_number(t_large_parkspace_size[i]);
  197. t_parkspace_status_msg.set_total_parkspace_remaining_number(t_total_parkspace_size[i]);
  198. Parkspace_communicator::get_instance_references().encapsulate_msg(t_parkspace_status_msg.SerializeAsString());
  199. //std::cout << " t_parkspace_status_msg.DebugString() = " << t_parkspace_status_msg.DebugString() << std::endl;
  200. }
  201. return SUCCESS;
  202. }
  203. //判断是否为待机,如果已经准备好,则可以执行任务。
  204. bool Parkspace_manager::is_ready()
  205. {
  206. if ( m_parkspace_manager_satus == eParkspace_manager_normal && m_thread_pool.thread_is_full_load() == false )
  207. {
  208. return true;
  209. }
  210. else
  211. {
  212. return false;
  213. }
  214. }
  215. Parkspace_manager::Parkspace_manager_satus Parkspace_manager::get_parkspace_manage_status()
  216. {
  217. return m_parkspace_manager_satus;
  218. }
  219. // 检查车辆是否已存在,通常分配前调用
  220. bool Parkspace_manager::check_car_existence(std::string license)
  221. {
  222. message::Parkspace_info parkspaceInfo;
  223. //获取车辆状态
  224. Error_manager error=m_parkspace_operating_function.query_one_parkspace_with_license(license,parkspaceInfo);
  225. //如果车辆处于停车中或者已入库 则车辆已存在
  226. if(parkspaceInfo.has_car_info() && parkspaceInfo.parkingspace_status() != message::eParkspace_empty)
  227. {
  228. return true;
  229. }
  230. return false;
  231. }
  232. //发送车位数据
  233. Error_manager Parkspace_manager::send_parkspace_data()
  234. {
  235. message::Parkspace_allocation_data_msg all_parkspace_data;
  236. Error_manager t_error=m_parkspace_operating_function.get_all_parkspace_info(all_parkspace_data);
  237. if(t_error != SUCCESS)
  238. {
  239. return t_error;
  240. }
  241. message::Base_info t_base_info;
  242. message::Error_manager error;
  243. t_base_info.set_msg_type(message::Message_type::eParkspace_allocation_data_response_msg);
  244. t_base_info.set_timeout_ms(5000);
  245. t_base_info.set_sender(message::Communicator::eParkspace);
  246. t_base_info.set_receiver(message::Communicator::eEmpty);
  247. all_parkspace_data.mutable_base_info()->CopyFrom(t_base_info);
  248. error.set_error_code(0);
  249. all_parkspace_data.mutable_error_manager()->CopyFrom(error);
  250. return Parkspace_communicator::get_instance_references().encapsulate_msg(all_parkspace_data.SerializeAsString());
  251. }
  252. //找到一个合适车位
  253. Error_manager Parkspace_manager::query_the_optimal_parkspace_server(message::Parkspace_info &parkspace_info,message::Car_info car_info, int terminal_id)
  254. {
  255. Error_manager error;
  256. //小车对应小车位 中车对应中车位 大车对应大车位
  257. message::Parkspace_info t_parkspace_info;
  258. if ( car_info.car_height() <= MIN_CAR_HIGH )
  259. {
  260. error = m_parkspace_operating_function.query_one_specified_unit_and_type_empty_parkspace(terminal_id/2,message::Parkspace_type::MIN_PARKINGSPACE,t_parkspace_info);
  261. if ( error == SUCCESS )
  262. {
  263. parkspace_info.CopyFrom(t_parkspace_info);
  264. return Error_code::SUCCESS;
  265. }
  266. }
  267. if ( car_info.car_height() <= MID_CAR_HIGH )
  268. {
  269. error = m_parkspace_operating_function.query_one_specified_unit_and_type_empty_parkspace(terminal_id/2,message::Parkspace_type::MID_PARKINGSPACE,t_parkspace_info);
  270. if ( error == SUCCESS )
  271. {
  272. parkspace_info.CopyFrom(t_parkspace_info);
  273. return Error_code::SUCCESS;
  274. }
  275. }
  276. if ( car_info.car_height() <= BIG_CAR_HIGH )
  277. {
  278. error = m_parkspace_operating_function.query_one_specified_unit_and_type_empty_parkspace(terminal_id/2,message::Parkspace_type::BIG_PARKINGSPACE,t_parkspace_info);
  279. if ( error == SUCCESS )
  280. {
  281. parkspace_info.CopyFrom(t_parkspace_info);
  282. return Error_code::SUCCESS;
  283. }
  284. }
  285. return error;
  286. }
  287. //分配车位线程函数
  288. void Parkspace_manager:: execute_for_allocate(message::Car_info car_info, int terminal_id, std::string command_key)
  289. {
  290. std::lock_guard<std::mutex> lck(m_parkspace_lock);
  291. LOG(INFO) << "分配车位 " <<"唯一码:"<< car_info.license()<< "牌号:" << car_info.car_numberplate() << " 车高:" << car_info.car_height()
  292. << " command_key=" << command_key;
  293. //根据请求的信息反馈分配的车位,并封装发送
  294. message::Parkspace_allocation_response_msg response_msg;
  295. message::Base_info t_response_header;
  296. t_response_header.set_msg_type(message::Message_type::eParkspace_allocation_response_msg);
  297. t_response_header.set_timeout_ms(1000);
  298. t_response_header.set_sender(message::Communicator::eParkspace);
  299. t_response_header.set_receiver(message::Communicator::eMain);
  300. message::Error_manager t_error;
  301. //分配的车位
  302. message::Parkspace_info t_allocated_space;
  303. Error_manager error;
  304. message::Car_type t_car_type;
  305. if ( car_info.car_height() <= MIN_CAR_HIGH )
  306. {
  307. t_car_type = message::Car_type::MIN_CAR;
  308. }
  309. else if ( car_info.car_height() <= MID_CAR_HIGH )
  310. {
  311. t_car_type = message::Car_type::MID_CAR;
  312. }
  313. else if ( car_info.car_height() <= BIG_CAR_HIGH )
  314. {
  315. t_car_type = message::Car_type::BIG_CAR;
  316. }
  317. else
  318. {
  319. t_car_type = message::Car_type::UNKNOW_CAR_TYPE;
  320. error = Error_manager(Error_code::HARDWARE_LIMIT_HEIGHT_OUT_RANGE,Error_level::MINOR_ERROR,"car height out of range");
  321. }
  322. //分配之前查询车辆是否已经存在
  323. if (error == Error_code::SUCCESS)
  324. {
  325. if (check_car_existence(car_info.car_numberplate()))
  326. {
  327. t_error.set_error_code(PARKSPACE_ALLOCATOR_CAR_ALREADY_EXIST);
  328. t_error.set_error_level(message::Error_level::MINOR_ERROR);
  329. t_error.set_error_description(" The vehicle already exists error");
  330. LOG(ERROR) << car_info.license() << " 车辆已存在";
  331. }
  332. else
  333. {
  334. //找到一个合适车位
  335. error = query_the_optimal_parkspace_server(t_allocated_space,car_info,terminal_id);
  336. if ( error != Error_code::SUCCESS)
  337. {
  338. t_error.set_error_code(PARKSPACE_ALLOCATOR_ALLOCATE_FAILED);
  339. t_error.set_error_level(message::Error_level::MINOR_ERROR);
  340. t_error.set_error_description(error.get_error_description().append("车辆类型:").append(std::to_string(t_car_type)));
  341. LOG(INFO) << " 分配车位失败,该单元无可供目标车辆停车的车位! 车辆类型:"<<t_car_type;
  342. }
  343. else
  344. {
  345. // 分配车位后更新数据库中车位状态
  346. t_allocated_space.set_parkingspace_status(message::Parkspace_status::eParkspace_locked);
  347. t_allocated_space.set_parkspace_status_target(message::Parkspace_status::eParkspace_locked);
  348. t_allocated_space.mutable_car_info()->CopyFrom(car_info);
  349. t_allocated_space.set_car_type(t_car_type);
  350. error = m_parkspace_operating_function.update_parkspace_data(t_allocated_space);
  351. if (error != SUCCESS)
  352. {
  353. t_error.set_error_code(error.get_error_code());
  354. t_error.set_error_level((message::Error_level)error.get_error_level());
  355. t_error.set_error_description(error.get_error_description());
  356. LOG(ERROR) << car_info.license() << " 停车更新数据库失败! " << error.to_string();
  357. }
  358. else
  359. {
  360. message::Vehicle_status t_car_status = message::Vehicle_status::eVehicle_parking;
  361. error = m_parkspace_operating_function.update_vehicle_with_parkspace(t_allocated_space,t_car_status);
  362. if ( error != Error_code::SUCCESS)
  363. {
  364. t_error.set_error_code(error.get_error_code());
  365. t_error.set_error_level((message::Error_level)error.get_error_level());
  366. t_error.set_error_description(error.get_error_description());
  367. LOG(ERROR) << car_info.license() << " 停车更新数据库车辆表失败! " << error.to_string();
  368. }
  369. else
  370. {
  371. t_error.set_error_code(SUCCESS);
  372. t_error.set_error_level(message::Error_level::NORMAL);
  373. LOG(INFO) << " 分配车位成功! 车位ID:"<<t_allocated_space.parkingspace_index_id()<<" 单元号:"<<t_allocated_space.parkingspace_unit_id()<<" 单元内部ID:"<<t_allocated_space.parkingspace_label_id();
  374. }
  375. }
  376. }
  377. }
  378. }
  379. else
  380. {
  381. t_error.set_error_code(error.get_error_code());
  382. t_error.set_error_level((message::Error_level)error.get_error_level());
  383. t_error.set_error_description(error.get_error_description());
  384. LOG(ERROR) <<"分配车位失败! "<< "终端号:"<<terminal_id<<" 唯一码:"<<car_info.license()<<" 号牌:"<<car_info.car_numberplate()<<" 车高:"<<car_info.car_height();
  385. }
  386. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  387. response_msg.set_command_key(command_key);
  388. response_msg.mutable_error_manager()->CopyFrom(t_error);
  389. response_msg.set_car_type(t_car_type);
  390. response_msg.add_allocated_parkspace_info_ex()->CopyFrom(t_allocated_space);
  391. Communication_message response = Communication_message();
  392. response.reset(t_response_header, response_msg.SerializeAsString());
  393. Parkspace_communicator::get_instance_references().send_response(&response);
  394. send_parkspace_data();
  395. std::cout << std::endl;
  396. }
  397. //确认分配车位线程函数
  398. void Parkspace_manager::execute_for_confirm_alloc(message::Parkspace_confirm_alloc_request_msg request)
  399. {
  400. std::lock_guard<std::mutex> lck(m_parkspace_lock);
  401. message::Parkspace_confirm_alloc_response_msg response_msg;
  402. message::Base_info t_response_header;
  403. t_response_header.set_msg_type(message::eParkspace_confirm_alloc_response_msg);
  404. t_response_header.set_sender(message::eParkspace);
  405. t_response_header.set_receiver(message::eMain);
  406. t_response_header.set_timeout_ms(1000);
  407. message::Error_manager t_error;
  408. t_error.set_error_code(Error_code::SUCCESS);
  409. t_error.set_error_level(message::Error_level::NORMAL);
  410. //确认的车位
  411. message::Parkspace_info confirm_alloc_space_info;
  412. message::Locate_information locate_information;
  413. LOG_IF(WARNING, request.confirm_parkspace_info_ex().size() <= 0) <<"confirm alloc space empty";
  414. Error_manager error;
  415. for (int i = 0; i < request.confirm_parkspace_info_ex().size(); ++i)
  416. {
  417. //核对确认车位信息
  418. error = m_parkspace_operating_function.query_one_parkspace_with_parkspace_id(request.confirm_parkspace_info_ex(i).parkingspace_index_id(),confirm_alloc_space_info);
  419. if ( !request.confirm_parkspace_info_ex(i).has_car_info() || \
  420. confirm_alloc_space_info.car_info().license() != request.confirm_parkspace_info_ex(i).car_info().license() )
  421. {
  422. t_error.set_error_code(PARKSPACE_ALLOCATOR_PARAM_ERROR);
  423. t_error.set_error_level(message::Error_level::MINOR_ERROR);
  424. t_error.set_error_description("Parking information does not match or the target status of the parking space is wrong error!");
  425. LOG(ERROR) << "确认分配失败 传入数据与数据库数据不匹配! 传入数据{ license:"<<request.confirm_parkspace_info_ex(i).car_info().license()<<"} 数据库数据{license:"<<confirm_alloc_space_info.car_info().license()<<"}";
  426. }
  427. else
  428. {
  429. confirm_alloc_space_info.mutable_car_info()->CopyFrom(request.confirm_parkspace_info_ex(i).car_info());
  430. locate_information.CopyFrom(request.locate_information());
  431. error = confirm_alloc_function(confirm_alloc_space_info,locate_information);
  432. if ( error != SUCCESS )
  433. {
  434. t_error.set_error_code(error.get_error_code());
  435. t_error.set_error_level((message::Error_level)error.get_error_level());
  436. t_error.set_error_description(error.get_error_description());
  437. }
  438. else
  439. {
  440. t_error.set_error_code(SUCCESS);
  441. t_error.set_error_level(message::Error_level::NORMAL);
  442. LOG(INFO) << " 确认分配成功! 车位ID:"<<confirm_alloc_space_info.parkingspace_index_id()<<" 单元号:"<<confirm_alloc_space_info.parkingspace_unit_id()<<" 单元内部ID:"<<confirm_alloc_space_info.parkingspace_label_id();
  443. }
  444. }
  445. }
  446. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  447. response_msg.set_command_key(request.command_key());
  448. response_msg.mutable_error_manager()->CopyFrom(t_error);
  449. response_msg.set_car_type(request.car_type());
  450. response_msg.add_confirm_parkspace_info_ex()->CopyFrom(confirm_alloc_space_info);
  451. Communication_message response=Communication_message();
  452. response.reset(t_response_header, response_msg.SerializeAsString());
  453. Parkspace_communicator::get_instance_references().send_response(&response);
  454. send_parkspace_data();
  455. std::cout<<std::endl;
  456. }
  457. //查询车位线程函数
  458. void Parkspace_manager::execute_for_search(message::Car_info car_info,std::string command_key)
  459. {
  460. std::lock_guard<std::mutex> lck(m_parkspace_lock);
  461. LOG(INFO) << "查询车位 "<<"唯一码:"<<car_info.license()<<" 车牌号:"<<car_info.car_numberplate()<<" command_key="<<command_key;
  462. //根据车辆凭证信息查询车辆位置
  463. message::Parkspace_search_response_msg response_msg;
  464. message::Base_info t_response_header;
  465. message::Error_manager t_error;
  466. t_response_header.set_msg_type(message::Message_type::eParkspace_search_response_msg);
  467. t_response_header.set_timeout_ms(1000);
  468. t_response_header.set_sender(message::Communicator::eParkspace);
  469. t_response_header.set_receiver(message::Communicator::eMain);
  470. //查询车辆位置
  471. message::Parkspace_info parkspace_info;
  472. Error_manager error=m_parkspace_operating_function.query_one_occupied_parkspace_with_license(car_info.license(),parkspace_info);
  473. if ( error != SUCCESS )
  474. {
  475. t_error.set_error_code(error.get_error_code());
  476. t_error.set_error_level((message::Error_level)error.get_error_level());
  477. t_error.set_error_description(error.get_error_description());
  478. LOG(ERROR) << "查询车位失败 "<<error.to_string();
  479. }
  480. else
  481. {
  482. t_error.set_error_code(SUCCESS);
  483. t_error.set_error_level(message::Error_level::NORMAL);
  484. LOG(INFO) << " 查询成功! 车位ID:"<<parkspace_info.parkingspace_index_id()<<" 单元号:"<<parkspace_info.parkingspace_unit_id()<<" 单元内部ID:"<<parkspace_info.parkingspace_label_id()<<" 唯一码:"<<parkspace_info.car_info().license()<<" 号牌:"<<parkspace_info.car_info().car_numberplate()<<" 车位当前状态:"<<parkspace_info.parkingspace_status();
  485. }
  486. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  487. response_msg.set_command_key(command_key);
  488. response_msg.mutable_error_manager()->CopyFrom(t_error);
  489. response_msg.mutable_query_parkspace_info_ex()->Add()->CopyFrom(parkspace_info);
  490. Communication_message response=Communication_message();
  491. response.reset(t_response_header, response_msg.SerializeAsString());
  492. Parkspace_communicator::get_instance_references().send_response(&response);
  493. std::cout<<std::endl;
  494. }
  495. //查询车位线程函数
  496. void Parkspace_manager::execute_for_manual_search(message::Car_info car_info,std::string command_key)
  497. {
  498. std::lock_guard<std::mutex> lck(m_parkspace_lock);
  499. LOG(INFO) << "手动查询车位 "<<"唯一码:"<<car_info.license()<<" 车牌号:"<<car_info.car_numberplate()<<" command_key="<<command_key;
  500. //根据车辆凭证信息查询车辆位置
  501. message::Parkspace_manual_search_response_msg response_msg;
  502. message::Base_info t_response_header;
  503. message::Error_manager t_error;
  504. t_response_header.set_msg_type(message::Message_type::eParkspace_manual_search_response_msg);
  505. t_response_header.set_timeout_ms(1000);
  506. t_response_header.set_sender(message::Communicator::eParkspace);
  507. t_response_header.set_receiver(message::Communicator::eEmpty);
  508. //查询车辆位置
  509. message::Parkspace_info parkspace_info;
  510. Error_manager error=m_parkspace_operating_function.query_one_parkspace_with_numberPlate(car_info.car_numberplate(),parkspace_info);
  511. if ( error != SUCCESS )
  512. {
  513. t_error.set_error_code(error.get_error_code());
  514. t_error.set_error_level((message::Error_level)error.get_error_level());
  515. t_error.set_error_description(error.get_error_description());
  516. LOG(ERROR) << "查询车位失败 "<<error.to_string();
  517. }
  518. else
  519. {
  520. t_error.set_error_code(SUCCESS);
  521. t_error.set_error_level(message::Error_level::NORMAL);
  522. LOG(INFO) << " 查询成功! 车位ID:"<<parkspace_info.parkingspace_index_id()<<" 单元号:"<<parkspace_info.parkingspace_unit_id()<<" 单元内部ID:"<<parkspace_info.parkingspace_label_id()<<" 唯一码:"<<parkspace_info.car_info().license()<<" 号牌:"<<parkspace_info.car_info().car_numberplate()<<" 车位当前状态:"<<parkspace_info.parkingspace_status();
  523. }
  524. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  525. response_msg.set_command_key(command_key);
  526. response_msg.mutable_error_manager()->CopyFrom(t_error);
  527. response_msg.mutable_query_parkspace_info_ex()->Add()->CopyFrom(parkspace_info);
  528. Communication_message response=Communication_message();
  529. response.reset(t_response_header, response_msg.SerializeAsString());
  530. Parkspace_communicator::get_instance_references().send_response(&response);
  531. std::cout<<std::endl;
  532. }
  533. //释放车位线程函数
  534. void Parkspace_manager::execute_for_release(message::Parkspace_release_request_msg release_msg, std::string command_key)
  535. {
  536. std::lock_guard<std::mutex> lck(m_parkspace_lock);
  537. //根据车位信息定位待释放车位位置,车辆凭证号用于校验
  538. message::Parkspace_release_response_msg response_msg;
  539. message::Base_info t_response_header;
  540. t_response_header.set_msg_type(message::eParkspace_release_response_msg);
  541. t_response_header.set_sender(message::eParkspace);
  542. t_response_header.set_receiver(message::eMain);
  543. t_response_header.set_timeout_ms(1000);
  544. message::Error_manager t_error;
  545. t_error.set_error_code(Error_code::SUCCESS);
  546. t_error.set_error_level(message::Error_level::NORMAL);
  547. Error_manager error;
  548. message::Parkspace_info t_release_space;
  549. for (int i = 0; i < release_msg.release_parkspace_info_ex_size(); ++i)
  550. {
  551. //将传入数据与数据库数据进行核对
  552. error = m_parkspace_operating_function.query_one_parkspace_with_parkspace_id(release_msg.release_parkspace_info_ex(i).parkingspace_index_id(),t_release_space);
  553. if ( !release_msg.release_parkspace_info_ex(i).has_car_info() || \
  554. t_release_space.car_info().license() != release_msg.release_parkspace_info_ex(i).car_info().license() )
  555. {
  556. t_error.set_error_code(PARKSPACE_ALLOCATOR_PARAM_ERROR);
  557. t_error.set_error_level(message::Error_level::MINOR_ERROR);
  558. t_error.set_error_description(" Incoming parking space data to be released does not match the database data error");
  559. LOG(ERROR) << "释放车位失败 传入数据与数据库数据不匹配! 传入数据{ license:"<<release_msg.release_parkspace_info_ex(i).car_info().license()<<"} 数据库数据{license:"<<t_release_space.car_info().license()<<"}";
  560. }
  561. else
  562. {
  563. Error_manager error=release_parkspace_function(t_release_space);
  564. if ( error != SUCCESS )
  565. {
  566. t_error.set_error_code(error.get_error_code());
  567. t_error.set_error_level((message::Error_level)error.get_error_level());
  568. t_error.set_error_description(error.get_error_description());
  569. }
  570. else
  571. {
  572. t_error.set_error_code(SUCCESS);
  573. t_error.set_error_level(message::Error_level::NORMAL);
  574. LOG(INFO) << " 释放车位成功! 车位ID:"<<t_release_space.parkingspace_index_id()<<" 单元号:"<<t_release_space.parkingspace_unit_id()<<" 单元内部ID:"<<t_release_space.parkingspace_label_id()<<" 唯一码:"<<t_release_space.car_info().license()<<" 号牌:"<<t_release_space.car_info().car_numberplate();
  575. }
  576. }
  577. }
  578. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  579. response_msg.set_command_key(command_key);
  580. response_msg.mutable_error_manager()->CopyFrom(t_error);
  581. response_msg.mutable_release_parkspace_info_ex()->Add()->CopyFrom(t_release_space);
  582. Communication_message response=Communication_message();
  583. response.reset(t_response_header, response_msg.SerializeAsString());
  584. Parkspace_communicator::get_instance_references().send_response(&response);
  585. send_parkspace_data();
  586. std::cout<<std::endl;
  587. }
  588. //强制更新车位信息线程函数
  589. void Parkspace_manager::execute_for_force_update(message::Parkspace_force_update_request_msg space_info, std::string command_key)
  590. {
  591. std::lock_guard<std::mutex> lck(m_parkspace_lock);
  592. LOG(INFO) << "手动 "<<"唯一码:"<<space_info.manual_parkspace_info_ex(0).car_info().license()<<"号牌:"<<space_info.manual_parkspace_info_ex(0).car_info().car_numberplate()<<" command_key="<<command_key;
  593. //根据车位信息定位待释放车位位置,车辆凭证号用于校验
  594. //!!!!!此处跳过外部处理与调用的过程,直接在内部调用,发送分配结果用于测试,目前一直发布第一个车位
  595. message::Parkspace_force_update_response_msg response_msg;
  596. message::Base_info t_response_header;
  597. t_response_header.set_msg_type(message::eParkspace_force_update_response_msg);
  598. t_response_header.set_sender(message::eParkspace);
  599. t_response_header.set_receiver(message::eMain);
  600. t_response_header.set_timeout_ms(1000);
  601. message::Error_manager t_error;
  602. message::Parkspace_info t_update_space = space_info.manual_parkspace_info_ex(0);
  603. Error_manager error;
  604. // id 9999为特殊情况,复位所有车位状态
  605. if (space_info.manual_parkspace_info_ex(0).parkingspace_index_id() == 9999)
  606. {
  607. // 复位所有车位状态
  608. error=m_parkspace_operating_function.clear_all_parkspace_info();
  609. if( error != SUCCESS)
  610. {
  611. t_error.set_error_code(error.get_error_code());
  612. t_error.set_error_level((message::Error_level) error.get_error_level());
  613. LOG(INFO) << "手动更新所有车位失败 "<<error.to_string();
  614. }
  615. else
  616. {
  617. t_error.set_error_code(SUCCESS);
  618. t_error.set_error_level(message::Error_level::NORMAL);
  619. LOG(INFO) << "所有车位已手动更新 ";
  620. }
  621. }
  622. else
  623. {
  624. //手动修改车位信息
  625. error = m_parkspace_operating_function.update_parkspace_data(t_update_space);
  626. if (error != SUCCESS)
  627. {
  628. t_error.set_error_code(error.get_error_code());
  629. t_error.set_error_level((message::Error_level)error.get_error_level());
  630. LOG(ERROR) << "手动 数据库更新车位失败 "<<error.to_string();
  631. }
  632. else
  633. {
  634. t_error.set_error_code(SUCCESS);
  635. t_error.set_error_level(message::Error_level::NORMAL);
  636. if(t_update_space.has_car_info() && t_update_space.car_info().has_license() && t_update_space.car_info().license() != "")
  637. {
  638. message::Vehicle_status t_vehicle_status=message::eVehicle_idle;
  639. error = m_parkspace_operating_function.update_vehicle_with_parkspace(t_update_space, t_vehicle_status);
  640. if (error != SUCCESS)
  641. {
  642. t_error.set_error_code(error.get_error_code());
  643. t_error.set_error_level((message::Error_level)error.get_error_level());
  644. LOG(ERROR) << "手动 数据库更新车辆失败 "<<error.to_string();
  645. }
  646. else
  647. {
  648. LOG(INFO) << "第" << t_update_space.parkingspace_index_id() << "号位已手动更新,"<<t_update_space.car_info().license()<<"车辆状态已手动更新";
  649. }
  650. }
  651. else
  652. {
  653. LOG(INFO) << "第" << t_update_space.parkingspace_index_id() << "号位已手动更新";
  654. }
  655. }
  656. }
  657. response_msg.mutable_base_info()->CopyFrom(t_response_header);
  658. response_msg.set_command_key(command_key);
  659. response_msg.mutable_error_manager()->CopyFrom(t_error);
  660. response_msg.mutable_manual_parkspace_info_ex()->Add()->CopyFrom(t_update_space);
  661. Communication_message response=Communication_message();
  662. response.reset(t_response_header, response_msg.SerializeAsString());
  663. Parkspace_communicator::get_instance_references().send_response(&response);
  664. send_parkspace_data();
  665. }
  666. //确认分配操作函数、
  667. Error_manager Parkspace_manager::confirm_alloc_function(message::Parkspace_info& t_confirm_space,message::Locate_information locate_information)
  668. {
  669. //根据车位信息定位待确认占用车位
  670. Error_manager error;
  671. //记录当前传入车辆的状态 为后续回退操作服务
  672. message::Parkspace_status t_parkingspace_status=t_confirm_space.parkingspace_status();
  673. if(t_confirm_space.parkingspace_status() == message::eParkspace_locked )
  674. {
  675. LOG(INFO) << "确认分配 车位id:" <<t_confirm_space.parkingspace_index_id()<< " 唯一码:" << t_confirm_space.car_info().license()<< " 车牌号:" << t_confirm_space.car_info().car_numberplate() ;
  676. //更新车位数据1
  677. t_confirm_space.set_parkingspace_status(message::eParkspace_occupied);
  678. t_confirm_space.set_parkspace_status_target(message::eParkspace_occupied);
  679. error = m_parkspace_operating_function.update_parkspace_data(t_confirm_space);
  680. if (error != SUCCESS)
  681. {
  682. LOG(ERROR) << "确认分配 数据库更新车位数据失败 " << error.to_string();
  683. return error;
  684. }
  685. else
  686. {
  687. //修改车辆状态为已入库
  688. message::Vehicle_status t_vehicle_status = message::Vehicle_status::eVehicle_in_garage;
  689. error = m_parkspace_operating_function.update_vehicle_with_parkspace(t_confirm_space, t_vehicle_status);
  690. if (error != SUCCESS)
  691. {
  692. //此处操作不成功就需要回退 因为前一步骤更改了车位表信息导致二表不统一
  693. LOG(ERROR) << "确认分配 更新数据库车辆数据失败 进行回退---" << error.to_string();
  694. t_confirm_space.set_parkingspace_status(t_parkingspace_status);
  695. t_confirm_space.set_parkspace_status_target(t_parkingspace_status);
  696. for (int i = 1; i < 4; ++i)
  697. {
  698. Error_manager e = m_parkspace_operating_function.update_parkspace_data(t_confirm_space);
  699. if (e != SUCCESS)
  700. {
  701. LOG(ERROR) << "回退第"<<i<<"次 失败! " << e.to_string();
  702. continue;
  703. }
  704. else
  705. {
  706. LOG(INFO) << " 回退成功! ";
  707. return e;
  708. }
  709. }
  710. //如果流程进行到这里说明回退三次都失败 需要报三级故障
  711. return Error_manager(Error_code::DB_UPDATE_FAILED, Error_level::MAJOR_ERROR,
  712. " update database error ");
  713. }
  714. error.set_error_code(SUCCESS);
  715. error.set_error_level_location(Error_level::NORMAL);
  716. //插入停车记录
  717. time_t tt = time(NULL);
  718. tm *t = localtime(&tt);
  719. char my_time_buf[255];
  720. memset(my_time_buf, 0, 255);
  721. sprintf(my_time_buf, "%d-%02d-%02d %02d:%02d:%02d",
  722. t->tm_year + 1900,
  723. t->tm_mon + 1,
  724. t->tm_mday,
  725. t->tm_hour,
  726. t->tm_min,
  727. t->tm_sec);
  728. t_confirm_space.set_entry_time(my_time_buf);
  729. error = m_parkspace_operating_function.insert_parking_record(t_confirm_space,locate_information);
  730. if ( error != SUCCESS )
  731. {
  732. LOG(WARNING) << "插入停车记录失败: " << error.to_string();
  733. }
  734. //查询停车记录的ID
  735. int t_record_id;
  736. error = m_parkspace_operating_function.query_parking_record(t_confirm_space,t_record_id);
  737. if ( error != SUCCESS )
  738. {
  739. LOG(WARNING) << "查询停车记录ID失败: " << error.to_string();
  740. }
  741. //更新停车记录ID
  742. error = m_parkspace_operating_function.update_record_id(t_confirm_space.car_info().license(),t_record_id);
  743. if ( error != SUCCESS )
  744. {
  745. LOG(WARNING) << "更新停车记录ID失败: " << error.to_string();
  746. }
  747. }
  748. }
  749. else
  750. {
  751. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  752. " 确认占有时车位状态与不正确 error ");
  753. }
  754. return Error_code::SUCCESS;
  755. }
  756. //释放车位操作函数
  757. Error_manager Parkspace_manager::release_parkspace_function(message::Parkspace_info& release_parkspace_info)
  758. {
  759. Error_manager error;
  760. if((release_parkspace_info.parkingspace_status() == message::Parkspace_status::eParkspace_occupied || \
  761. release_parkspace_info.parkingspace_status() == message::Parkspace_status::eParkspace_locked )&& release_parkspace_info.has_car_info())
  762. {
  763. LOG(INFO) << "释放车位 车位id:" <<release_parkspace_info.parkingspace_index_id()<< " 唯一码:" << release_parkspace_info.car_info().license()<< " 车牌号:" << release_parkspace_info.car_info().car_numberplate() ;
  764. release_parkspace_info.set_parkspace_status_target(message::eParkspace_empty);
  765. release_parkspace_info.set_parkingspace_status(message::eParkspace_empty);
  766. error = m_parkspace_operating_function.update_parkspace_data(release_parkspace_info);
  767. if (error != SUCCESS)
  768. {
  769. LOG(ERROR) << "释放车位 更新数据库车位数据失败 " << error.to_string();
  770. return error;
  771. }
  772. else
  773. {
  774. // search vehicle info
  775. message::Vehicle_status t_vehicle_status;
  776. //获取预约ID
  777. int t_park_record_id;
  778. error = m_parkspace_operating_function.query_vehicle(release_parkspace_info.car_info().license(),
  779. t_vehicle_status, t_park_record_id);
  780. if (error != SUCCESS)
  781. {
  782. LOG(ERROR) << "释放车位 数据库查询车辆信息失败 " << error.to_string();
  783. }
  784. // update vehicle info
  785. t_vehicle_status=message::eVehicle_idle;
  786. error = m_parkspace_operating_function.update_vehicle_with_parkspace(release_parkspace_info, t_vehicle_status);
  787. if (error != SUCCESS)
  788. {
  789. LOG(ERROR) << "释放车位 更新数据库车辆数据失败 " << error.to_string();
  790. return error;
  791. }
  792. error.set_error_code(SUCCESS);
  793. error.set_error_level_location(Error_level::NORMAL);
  794. // update records if neeeded (only in fetch)
  795. if(t_park_record_id>0)
  796. {
  797. time_t tt = time(NULL);
  798. tm *t = localtime(&tt);
  799. char my_time_buf[255];
  800. memset(my_time_buf, 0, 255);
  801. sprintf(my_time_buf, "%d-%02d-%02d %02d:%02d:%02d",
  802. t->tm_year + 1900,
  803. t->tm_mon + 1,
  804. t->tm_mday,
  805. t->tm_hour,
  806. t->tm_min,
  807. t->tm_sec);
  808. release_parkspace_info.set_leave_time(my_time_buf);
  809. error = m_parkspace_operating_function.update_parking_record(release_parkspace_info, t_park_record_id);
  810. if (error != SUCCESS) {
  811. LOG(WARNING) << "更新停车记录失败: " << error.to_string();
  812. }
  813. }
  814. }
  815. }
  816. else
  817. {
  818. return Error_manager(Error_code::PARAMETER_ERROR, Error_level::MINOR_ERROR,
  819. " 释放车位时车位状态不正确 error ");
  820. }
  821. return Error_code::SUCCESS;
  822. }