dispatch_command.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. #include "dispatch_command.h"
  2. #include <google/protobuf/text_format.h>
  3. #include "../tool/time_tool.h"
  4. #include "dispatch_manager.h"
  5. Dispatch_command::Dispatch_command()
  6. {
  7. m_dispatch_command_status = E_DISPATCH_COMMAND_UNKNOW;
  8. m_unit = 0;
  9. m_device_floor = 0;
  10. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_TYPE_UNKNOW;
  11. m_car_type = Common_data::Car_type::UNKNOW_CAR_TYPE;
  12. }
  13. Dispatch_command::~Dispatch_command()
  14. {
  15. }
  16. //调度开始前, 向数据库发送请求的相关操作, 输入 穿梭机所在的楼层, 调度id 0~2, 空闲出口id, 如果不是0,就表示有空闲的出口
  17. Error_manager Dispatch_command::dispatch_request_to_sql(int device_floor, int dispatch_id, int outlet_ready)
  18. {
  19. std::unique_lock<std::mutex> t_lock(m_lock);
  20. Error_manager t_error;
  21. m_device_floor = device_floor;
  22. m_dispatch_id = dispatch_id;
  23. m_unit = dispatch_id+1;
  24. m_outlet_ready = outlet_ready;
  25. //获取调度指令, 与数据库同步 command_queue
  26. t_error = query_all_dispatch_command(0);
  27. if ( t_error != Error_code::SUCCESS )
  28. {
  29. return t_error;
  30. }
  31. //排序
  32. if ( outlet_ready == 0 )
  33. {
  34. //对调度指令进行排序, 选出最优解, 只比较存车
  35. t_error = sort_dispatch_command_for_park();
  36. }
  37. else
  38. {
  39. //对调度指令进行排序, 选出最优解, 比较存车和取车
  40. t_error = sort_dispatch_command_for_total();
  41. }
  42. if ( t_error != Error_code::SUCCESS )
  43. {
  44. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_TYPE_UNKNOW;
  45. //没有找到合适的指令就返回NODATA, 然后设么也不做
  46. return t_error;
  47. }
  48. else
  49. {
  50. if ( m_dispatch_command_map[m_car_number_optimal].m_type == 1 )//存车
  51. {
  52. //注注注注注意了
  53. //存车自带感测信息,没有车位信息, 需要查询车位表,找到空车位,补全车位信息
  54. //取车自带车位信息,没有感测信息, 需要查询车辆表,补全车辆信息
  55. //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解
  56. t_error = query_dispatch_space_optimal();
  57. if ( t_error != Error_code::SUCCESS )
  58. {
  59. update_command_queue_for_statu(3);
  60. return t_error;
  61. }
  62. else
  63. {
  64. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_STORE;
  65. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_id(m_dispatch_space_info.m_id);
  66. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_unit_id(m_dispatch_space_info.m_unit);
  67. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_floor(m_dispatch_space_info.m_floor);
  68. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_room_id(m_dispatch_space_info.m_subID);
  69. //更新 车位状态, 根据车位ID 写入车牌号即可
  70. t_error = update_parkspace_info_write_car_number();
  71. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  72. t_error = update_command_queue_for_statu(1);
  73. //制作存车表单
  74. t_error = create_park_table();
  75. //增加 记录表, 存车指令 完成后添加存车记录
  76. t_error = insert_record_for_park_start();
  77. LOG(INFO) << " create_park_table 创建存车任务单 = "<< m_park_table_msg.DebugString() << " --- " << this;
  78. return Error_code::SUCCESS;
  79. }
  80. }
  81. else if ( m_dispatch_command_map[m_car_number_optimal].m_type == 2 )//取车
  82. {
  83. //注注注注注意了
  84. //存车自带感测信息,没有车位信息, 需要查询车位表,找到空车位,补全车位信息
  85. //取车自带车位信息,没有感测信息, 需要查询车辆表,补全车辆信息
  86. //查询 取车的车辆感测信息 取车指令 根据key 查询感测信息
  87. t_error = query_dispatch_vehicle_for_primary_key();
  88. if ( t_error != Error_code::SUCCESS )
  89. {
  90. update_command_queue_for_statu(3);
  91. return t_error;
  92. }
  93. else
  94. {
  95. //补充出口id
  96. m_dispatch_command_map[m_car_number_optimal].m_export_id = outlet_ready;
  97. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_PICKUP;
  98. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg = m_dispatch_vehicle_info.m_actually_measure_info_msg;
  99. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  100. t_error = update_command_queue_for_statu(1);
  101. //制作取车表单
  102. t_error = create_pick_table(outlet_ready);
  103. //更新 记录表, 取车指令 完成后更新取车记录
  104. t_error = updata_record_for_pick_start();
  105. LOG(INFO) << " create_pick_table 创建取车车任务单 = "<< m_pick_table_msg.DebugString() << " --- " << this;
  106. }
  107. }
  108. else
  109. {
  110. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  111. " fun error ");
  112. }
  113. }
  114. return Error_code::SUCCESS;
  115. }
  116. //调度完成后, 向数据库发送答复的相关操作
  117. Error_manager Dispatch_command::dispatch_response_to_sql(Error_manager dispatch_result)
  118. {
  119. std::unique_lock<std::mutex> t_lock(m_lock);
  120. Error_manager t_error;
  121. if ( dispatch_result == Error_code::SUCCESS )
  122. {
  123. //调度成功, 完善数据库
  124. if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
  125. {
  126. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_STORE 存车答复成功 = "<< t_error << " --- " << this;
  127. //增加 车辆表, 存车指令 完成后添加车辆信息
  128. t_error = insert_vehicle_for_car_number_ex();
  129. //更新 指令队列, 根据车牌号 删除指令
  130. t_error = delete_command_queue_for_statu();
  131. //增加 记录表, 存车指令 完成后添加存车记录
  132. t_error = updata_record_for_park_end();
  133. }
  134. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
  135. {
  136. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_PICKUP 取车答复成功 = "<< t_error << " --- " << this;
  137. //删除 车辆表, 取车指令 完成后删除车辆信息
  138. t_error = delete_vehicle_for_car_number();
  139. //更新 车位状态, 找到车牌号, 写NULL
  140. t_error = update_parkspace_info_clear_car_number();
  141. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  142. t_error = update_command_queue_for_statu(2);
  143. //更新 记录表, 取车指令 完成后更新取车记录
  144. t_error = updata_record_for_pick_end();
  145. }
  146. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
  147. {
  148. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_REALLOCATE 改道答复成功 = "<< t_error << " --- " << this;
  149. //增加 车辆表, 存车指令 完成后添加车辆信息
  150. t_error = insert_vehicle_for_car_number_ex();
  151. //更新 指令队列, 根据车牌号 删除指令
  152. t_error = delete_command_queue_for_statu();
  153. //增加 记录表, 存车指令 完成后添加存车记录
  154. t_error = updata_record_for_park_end();
  155. }
  156. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
  157. {
  158. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_REVOCATION 撤销答复成功 = "<< t_error << " --- " << this;
  159. //删除 车辆表, 取车指令 完成后删除车辆信息
  160. t_error = delete_vehicle_for_car_number();
  161. //更新 车位状态, 找到车牌号, 写NULL
  162. t_error = update_parkspace_info_clear_car_number();
  163. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  164. t_error = update_command_queue_for_statu(2);
  165. // LOG(INFO) << "llllllllllllllllllllllllllllllllllllllllll respons = "<< t_error << " --- " << this;
  166. //更新 记录表, 取车指令 完成后更新取车记录, 撤销指令, 没有取车时间差
  167. t_error = updata_record_for_pick_end_ex();
  168. }
  169. else
  170. {
  171. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  172. " fun error ");
  173. }
  174. return t_error;
  175. }
  176. else
  177. {
  178. //调度失败, 回退 数据库
  179. if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
  180. {
  181. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_STORE 存车答复失败 = "<< t_error << " --- " << this;
  182. //更新 车位状态, 找到车牌号, 写NULL
  183. update_parkspace_info_clear_car_number();
  184. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  185. update_command_queue_for_statu(3);
  186. }
  187. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
  188. {
  189. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_PICKUP 取车答复失败 = "<< t_error << " --- " << this;
  190. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  191. update_command_queue_for_statu(3);
  192. }
  193. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
  194. {
  195. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_STORE 改道答复失败 = "<< t_error << " --- " << this;
  196. //更新 车位状态, 找到车牌号, 写NULL
  197. update_parkspace_info_clear_car_number();
  198. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  199. update_command_queue_for_statu(3);
  200. }
  201. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
  202. {
  203. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_PICKUP 撤销答复失败 = "<< t_error << " --- " << this;
  204. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  205. update_command_queue_for_statu(3);
  206. }
  207. else
  208. {
  209. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  210. " fun error ");
  211. }
  212. }
  213. return Error_code::SUCCESS;
  214. }
  215. //调度 , 向数据库 重新分配车位,
  216. Error_manager Dispatch_command::dispatch_reallocate_to_sql(Common_data::Car_type reallocate_car_type, int outlet_ready)
  217. {
  218. std::unique_lock<std::mutex> t_lock(m_lock);
  219. Error_manager t_error;
  220. m_outlet_ready = outlet_ready;
  221. //把之前申请的车位清除 //更新 车位状态, 找到车牌号, 写NULL
  222. t_error = Dispatch_command::update_parkspace_info_clear_car_number();
  223. //重新申请车位
  224. //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解
  225. t_error = query_dispatch_space_optimal(reallocate_car_type);
  226. if ( t_error != Error_code::SUCCESS )
  227. {
  228. //没找到车位, 就把车放到出口
  229. if ( outlet_ready != 0 )
  230. {
  231. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_REVOCATION;
  232. //补充出口id
  233. m_dispatch_command_map[m_car_number_optimal].m_export_id = outlet_ready;
  234. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_id(0);
  235. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_unit_id(0);
  236. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_floor(0);
  237. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_room_id(0);
  238. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  239. // t_error = update_command_queue_for_statu(1);
  240. //制作取车表单
  241. t_error = create_pick_table(outlet_ready);
  242. LOG(INFO) << " create_pick_table 撤销存车, 取车到出口任务单 = "<< m_pick_table_msg.DebugString() << " --- " << this;
  243. return t_error;
  244. }
  245. else
  246. {
  247. //无限等待, 等待出口空闲
  248. return Error_code::NODATA;
  249. }
  250. }
  251. else
  252. {
  253. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_REALLOCATE;
  254. //找到新的车位, 重新执行任务
  255. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_id(m_dispatch_space_info.m_id);
  256. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_unit_id(m_dispatch_space_info.m_unit);
  257. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_floor(m_dispatch_space_info.m_floor);
  258. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_room_id(m_dispatch_space_info.m_subID);
  259. //更新 车位状态, 根据车位ID 写入车牌号即可
  260. t_error = update_parkspace_info_write_car_number();
  261. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  262. // t_error = update_command_queue_for_statu(1);
  263. //制作存车表单
  264. t_error = create_park_table();
  265. LOG(INFO) << " create_park_table 改道存车, 存车新车位任务单 = "<< m_park_table_msg.DebugString() << " --- " << this;
  266. }
  267. return Error_code::SUCCESS;
  268. }
  269. //检查出口是否空闲, 检查指令队列的取车完成的出口id是否存在, 不存在就是空闲,返回成功
  270. Error_manager Dispatch_command::check_export_id_is_ready(int export_id)
  271. {
  272. std::unique_lock<std::mutex> t_lock(m_lock);
  273. //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
  274. char check_export_id_sql[1024];
  275. memset(check_export_id_sql, 0, 1024);
  276. sprintf(check_export_id_sql,"select * from command_queue where statu = 2 and type = 2 and export_id = %d",export_id);
  277. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  278. Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
  279. if(t_error == Error_code::SUCCESS)
  280. {
  281. if (tp_result == nullptr)
  282. {
  283. return DB_RESULT_SET_EMPTY;
  284. }
  285. //如果存在, 就报错, 不存在就是空闲,返回成功
  286. if ( tp_result->next() )
  287. {
  288. return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
  289. " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
  290. }
  291. else
  292. {
  293. return Error_code::SUCCESS;
  294. }
  295. }
  296. else
  297. {
  298. return t_error;
  299. }
  300. return Error_code::SUCCESS;
  301. }
  302. //检查出口取车是否完成, 指定的出口有取车完成指令就返回成功, 否则报错.
  303. Error_manager Dispatch_command::check_pickup_is_finish(int export_id)
  304. {
  305. std::unique_lock<std::mutex> t_lock(m_lock);
  306. //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
  307. char check_export_id_sql[1024];
  308. memset(check_export_id_sql, 0, 1024);
  309. sprintf(check_export_id_sql,"select * from command_queue where statu = 2 and type = 2 and export_id = %d",export_id);
  310. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  311. Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
  312. if(t_error == Error_code::SUCCESS)
  313. {
  314. if (tp_result == nullptr)
  315. {
  316. return DB_RESULT_SET_EMPTY;
  317. }
  318. //如果存在, 就报错, 不存在就是空闲,返回成功
  319. if ( tp_result->next() )
  320. {
  321. return Error_code::SUCCESS;
  322. }
  323. else
  324. {
  325. return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
  326. " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
  327. }
  328. }
  329. else
  330. {
  331. return t_error;
  332. }
  333. return Error_code::SUCCESS;
  334. }
  335. //检查入口存车是否开始, 指定的入口有存车开始指令就返回成功, 否则报错.
  336. Error_manager Dispatch_command::check_park_is_start(int import_id)
  337. {
  338. std::unique_lock<std::mutex> t_lock(m_lock);
  339. //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
  340. char check_export_id_sql[1024];
  341. memset(check_export_id_sql, 0, 1024);
  342. sprintf(check_export_id_sql,"select * from command_queue where type = 1 and import_id = %d and (statu=0 or statu =1)",import_id);
  343. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  344. Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
  345. if(t_error == Error_code::SUCCESS)
  346. {
  347. if (tp_result == nullptr)
  348. {
  349. return DB_RESULT_SET_EMPTY;
  350. }
  351. //如果存在, 就报错, 不存在就是空闲,返回成功
  352. if ( tp_result->next() )
  353. {
  354. return Error_code::SUCCESS;
  355. }
  356. else
  357. {
  358. return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
  359. " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
  360. }
  361. }
  362. else
  363. {
  364. return t_error;
  365. }
  366. return Error_code::SUCCESS;
  367. }
  368. //获取指定入口的 存车流程的 唯一码,
  369. Error_manager Dispatch_command::get_primary_key_for_store(int import_id, std::string & primary_key )
  370. {
  371. std::unique_lock<std::mutex> t_lock(m_lock);
  372. //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
  373. char check_export_id_sql[1024];
  374. memset(check_export_id_sql, 0, 1024);
  375. sprintf(check_export_id_sql,"select * from command_queue where type = 1 and import_id = %d and (statu=0 or statu =1)",import_id);
  376. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  377. Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
  378. if(t_error == Error_code::SUCCESS)
  379. {
  380. if (tp_result == nullptr)
  381. {
  382. return DB_RESULT_SET_EMPTY;
  383. }
  384. //如果存在, 就报错, 不存在就是空闲,返回成功
  385. if ( tp_result->next() )
  386. {
  387. char buf[1024];
  388. memset(buf, 0, 1024);
  389. try
  390. {
  391. //解析数据
  392. primary_key = tp_result->getString("primary_key");
  393. }
  394. catch (sql::SQLException &e)
  395. {
  396. /* Use what() (derived from std::runtime_error) to fetch the error message */
  397. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  398. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  399. }
  400. catch (std::runtime_error &e)
  401. {
  402. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  403. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  404. }
  405. return Error_code::SUCCESS;
  406. }
  407. else
  408. {
  409. return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
  410. " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
  411. }
  412. }
  413. else
  414. {
  415. return t_error;
  416. }
  417. return Error_code::SUCCESS;
  418. }
  419. //删除 指令队列, 根据出口编号
  420. Error_manager Dispatch_command::delete_command_queue_for_export_id(int export_id)
  421. {
  422. std::unique_lock<std::mutex> t_lock(m_lock);
  423. char delete_command_sql[1024];
  424. memset(delete_command_sql, 0, 1024);
  425. sprintf(delete_command_sql, "delete from command_queue where statu = 2 and type = 2 and export_id = %d ",
  426. export_id );
  427. Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_command_sql);
  428. return ec;
  429. }
  430. //添加plc数据储存
  431. Error_manager Dispatch_command::insert_plc_data(std::string plc_data, float hearbeat)
  432. {
  433. std::unique_lock<std::mutex> t_lock(m_lock);
  434. char insert_vehicle_sql[4096];
  435. memset(insert_vehicle_sql, 0, 4096);
  436. sprintf(insert_vehicle_sql, "INSERT INTO plc_data (data, hearbeat) values ('%s',%f)",
  437. plc_data.c_str(), hearbeat );
  438. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
  439. std::cout << " huli test :::: " << " ec = " << ec << std::endl;
  440. return ec;
  441. }
  442. //获取调度指令, 与数据库同步 command_queue, statu指令状态, 0:排队等待, 1正在运行, 2:已经完成
  443. Error_manager Dispatch_command::query_all_dispatch_command(int statu)
  444. {
  445. //从command_queue获取所有排队的指令,
  446. char query_all_dispatch_command_sql[1024];
  447. memset(query_all_dispatch_command_sql, 0, 1024);
  448. sprintf(query_all_dispatch_command_sql,"select * from command_queue where statu = %d and unit = %d",statu, m_unit);
  449. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  450. Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(query_all_dispatch_command_sql, tp_result);
  451. m_dispatch_command_map.clear();
  452. if(t_error == Error_code::SUCCESS)
  453. {
  454. if (tp_result == nullptr)
  455. {
  456. return DB_RESULT_SET_EMPTY;
  457. }
  458. //循环检查所有行
  459. while (tp_result->next())
  460. {
  461. Dispatch_command_info t_dispatch_command_info;
  462. char t_error_buf[1024];
  463. memset(t_error_buf, 0, 1024);
  464. try
  465. {
  466. //解析数据
  467. t_dispatch_command_info.m_car_number = tp_result->getString("car_number");
  468. t_dispatch_command_info.m_primary_key = tp_result->getString("primary_key");
  469. t_dispatch_command_info.m_unit = tp_result->getInt("unit");
  470. t_dispatch_command_info.m_queue_id = tp_result->getInt("queue_id");
  471. t_dispatch_command_info.m_type = tp_result->getInt("type");
  472. t_dispatch_command_info.m_statu = tp_result->getInt("statu");
  473. t_dispatch_command_info.m_space_info = tp_result->getString("space_info");
  474. t_dispatch_command_info.m_measure_info = tp_result->getString("measure_info");
  475. // t_dispatch_command_info.m_export_id = tp_result->getInt("export_id");
  476. t_dispatch_command_info.m_export_id = 0;
  477. //m_type指令类型, 0无效, 1存车, 2取车,
  478. if ( t_dispatch_command_info.m_type == 1)
  479. {
  480. //存车, 数据库特有 雷达信息, 没有车位信息, 需要根据车高,再去车位表申请车位
  481. if(! google::protobuf::TextFormat::ParseFromString(t_dispatch_command_info.m_measure_info, &t_dispatch_command_info.m_measure_info_msg))
  482. {
  483. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  484. "ParseFromString fun error, m_measure_info ");
  485. }
  486. t_dispatch_command_info.m_useless_distance = m_device_floor-1;
  487. }
  488. else if ( t_dispatch_command_info.m_type == 2 )
  489. {
  490. //取车, 数据库特有车位信息
  491. if(! google::protobuf::TextFormat::ParseFromString(t_dispatch_command_info.m_space_info, &t_dispatch_command_info.m_parkspace_info_msg))
  492. {
  493. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  494. "ParseFromString fun error, m_space_info ");
  495. }
  496. t_dispatch_command_info.m_useless_distance = t_dispatch_command_info.m_parkspace_info_msg.floor()-m_device_floor;
  497. }
  498. else
  499. {
  500. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  501. "command_queue m_type error ");
  502. }
  503. if ( t_dispatch_command_info.m_useless_distance <0 )
  504. {
  505. t_dispatch_command_info.m_useless_distance = 0-t_dispatch_command_info.m_useless_distance;
  506. }
  507. m_dispatch_command_map[t_dispatch_command_info.m_car_number] = t_dispatch_command_info;
  508. }
  509. catch (sql::SQLException &e)
  510. {
  511. /* Use what() (derived from std::runtime_error) to fetch the error message */
  512. sprintf(t_error_buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(),
  513. e.getSQLState().c_str());
  514. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, t_error_buf);
  515. }
  516. catch (std::runtime_error &e)
  517. {
  518. sprintf(t_error_buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  519. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, t_error_buf);
  520. }
  521. }
  522. return SUCCESS;
  523. }
  524. else
  525. {
  526. return t_error;
  527. }
  528. return Error_code::SUCCESS;
  529. }
  530. //对调度指令进行排序, 选出最优解, 比较存车和取车
  531. Error_manager Dispatch_command::sort_dispatch_command_for_total()
  532. {
  533. int t_optimal_loss=0x7fffffff; //最优loss值
  534. m_car_number_optimal.clear();
  535. for (auto iter = m_dispatch_command_map.begin(); iter != m_dispatch_command_map.end(); ++iter)
  536. {
  537. //loss = 空跑路程 + 排队编号, 求最小值
  538. int t_current_loss = iter->second.m_useless_distance + iter->second.m_queue_id;
  539. if ( t_optimal_loss > t_current_loss )
  540. {
  541. t_optimal_loss = t_current_loss;
  542. m_car_number_optimal = iter->first;
  543. }
  544. }
  545. if ( m_car_number_optimal.empty() )
  546. {
  547. return Error_code::NODATA;
  548. }
  549. return Error_code::SUCCESS;
  550. }
  551. //对调度指令进行排序, 选出最优解, 只比较存车
  552. Error_manager Dispatch_command::sort_dispatch_command_for_park()
  553. {
  554. int t_optimal_loss=0x7fffffff; //最优loss值
  555. m_car_number_optimal.clear();
  556. for (auto iter = m_dispatch_command_map.begin(); iter != m_dispatch_command_map.end(); ++iter)
  557. {
  558. //loss = 空跑路程 + 排队编号, 求最小值
  559. int t_current_loss = iter->second.m_useless_distance + iter->second.m_queue_id;
  560. if ( t_optimal_loss > t_current_loss && iter->second.m_type == 1)
  561. {
  562. t_optimal_loss = t_current_loss;
  563. m_car_number_optimal = iter->first;
  564. }
  565. }
  566. if ( m_car_number_optimal.empty() )
  567. {
  568. return Error_code::NODATA;
  569. }
  570. return Error_code::SUCCESS;
  571. }
  572. //存车指令 获取 指定车高 指定单元 的车位信息,用于车位分配
  573. Error_manager Dispatch_command::query_specify_height_unit_parkspace_info(int unit, float height, Dispatch_space_info & dispatch_space_info)
  574. {
  575. //查询车位表
  576. char query_parkspace_sql[1024];
  577. memset(query_parkspace_sql, 0, 1024);
  578. int t_statu = 0; //车位状态, 0可用, 1故障
  579. sprintf(query_parkspace_sql,"select * from space where car_number is NULL and ABS(height- %f) < 1e-5 and statu = %d and unit = %d",height,t_statu,unit);
  580. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  581. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  582. if(ec == SUCCESS)
  583. {
  584. if(tp_result == nullptr)
  585. {
  586. return DB_RESULT_SET_EMPTY;
  587. }
  588. //只取第一条结果, 默认是id最小的,
  589. if (tp_result->next())
  590. {
  591. char buf[1024];
  592. memset(buf, 0, 1024);
  593. try
  594. {
  595. //解析数据
  596. dispatch_space_info.m_id = tp_result->getInt("id");
  597. dispatch_space_info.m_floor = tp_result->getInt("floor");
  598. dispatch_space_info.m_subID = tp_result->getInt("subID");
  599. dispatch_space_info.m_height = tp_result->getDouble("height");
  600. dispatch_space_info.m_car_number = tp_result->getString("car_number");
  601. dispatch_space_info.m_unit = tp_result->getInt("unit");
  602. dispatch_space_info.m_statu = tp_result->getInt("statu");
  603. }
  604. catch (sql::SQLException &e)
  605. {
  606. /* Use what() (derived from std::runtime_error) to fetch the error message */
  607. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  608. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  609. }
  610. catch (std::runtime_error &e)
  611. {
  612. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  613. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  614. }
  615. }
  616. else
  617. {
  618. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  619. "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
  620. }
  621. return SUCCESS;
  622. }
  623. else
  624. {
  625. return ec;
  626. }
  627. return SUCCESS;
  628. }
  629. //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解
  630. Error_manager Dispatch_command::query_dispatch_space_optimal()
  631. {
  632. Error_manager t_error;
  633. int t_unit = m_dispatch_command_map[m_car_number_optimal].m_unit;
  634. float t_height = m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height();
  635. //小车对应小车位 中车对应中车位 大车对应大车位
  636. if ( 0 < t_height && t_height <= CAR_HEIGHT_LIMIT_SMALL )
  637. {
  638. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_SMALL, m_dispatch_space_info);
  639. if ( t_error == Error_code::SUCCESS )
  640. {
  641. m_car_type = Common_data::Car_type::MIN_CAR;
  642. return t_error;
  643. }
  644. }
  645. if ( t_height <= CAR_HEIGHT_LIMIT_MIDDLE )
  646. {
  647. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_MIDDLE, m_dispatch_space_info);
  648. if ( t_error == Error_code::SUCCESS )
  649. {
  650. m_car_type = Common_data::Car_type::MID_CAR;
  651. return t_error;
  652. }
  653. }
  654. if ( t_height <= CAR_HEIGHT_LIMIT_BIG )
  655. {
  656. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_BIG, m_dispatch_space_info);
  657. if ( t_error == Error_code::SUCCESS )
  658. {
  659. m_car_type = Common_data::Car_type::BIG_CAR;
  660. return t_error;
  661. }
  662. }
  663. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  664. " query_dispatch_space_optimal error ");
  665. }
  666. //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解
  667. Error_manager Dispatch_command::query_dispatch_space_optimal(Common_data::Car_type reallocate_car_type)
  668. {
  669. Error_manager t_error;
  670. int t_unit = m_dispatch_command_map[m_car_number_optimal].m_unit;
  671. // float t_height = m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height();
  672. //小车对应小车位 中车对应中车位 大车对应大车位
  673. if ( reallocate_car_type == Common_data::Car_type::MIN_CAR )
  674. {
  675. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_SMALL, m_dispatch_space_info);
  676. if ( t_error == Error_code::SUCCESS )
  677. {
  678. m_car_type = Common_data::Car_type::MIN_CAR;
  679. return t_error;
  680. }
  681. }
  682. if ( reallocate_car_type == Common_data::Car_type::MIN_CAR ||
  683. reallocate_car_type == Common_data::Car_type::MID_CAR )
  684. {
  685. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_MIDDLE, m_dispatch_space_info);
  686. if ( t_error == Error_code::SUCCESS )
  687. {
  688. m_car_type = Common_data::Car_type::MID_CAR;
  689. return t_error;
  690. }
  691. }
  692. if ( reallocate_car_type == Common_data::Car_type::MIN_CAR ||
  693. reallocate_car_type == Common_data::Car_type::MID_CAR ||
  694. reallocate_car_type == Common_data::Car_type::BIG_CAR )
  695. {
  696. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_BIG, m_dispatch_space_info);
  697. if ( t_error == Error_code::SUCCESS )
  698. {
  699. m_car_type = Common_data::Car_type::BIG_CAR;
  700. return t_error;
  701. }
  702. }
  703. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  704. " query_dispatch_space_optimal error ");
  705. }
  706. //查询 取车的车位 取车指令 根据车牌号 查询对应的车位
  707. Error_manager Dispatch_command::query_dispatch_space_for_car_number()
  708. {
  709. //查询车位表
  710. char query_parkspace_sql[1024];
  711. memset(query_parkspace_sql, 0, 1024);
  712. int t_statu = 0; //车位状态, 0可用, 1故障
  713. sprintf(query_parkspace_sql,"select * from space where car_number = '%s' ",m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  714. // std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
  715. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  716. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  717. if(ec == SUCCESS)
  718. {
  719. if(tp_result == nullptr)
  720. {
  721. return DB_RESULT_SET_EMPTY;
  722. }
  723. //只取第一条结果, 默认是id最小的,
  724. if (tp_result->next())
  725. {
  726. char buf[1024];
  727. memset(buf, 0, 1024);
  728. try
  729. {
  730. //解析数据
  731. m_dispatch_space_info.m_id = tp_result->getInt("id");
  732. m_dispatch_space_info.m_floor = tp_result->getInt("floor");
  733. m_dispatch_space_info.m_subID = tp_result->getInt("subID");
  734. m_dispatch_space_info.m_height = tp_result->getDouble("height");
  735. m_dispatch_space_info.m_car_number = tp_result->getString("car_number");
  736. m_dispatch_space_info.m_unit = tp_result->getInt("unit");
  737. m_dispatch_space_info.m_statu = tp_result->getInt("statu");
  738. }
  739. catch (sql::SQLException &e)
  740. {
  741. /* Use what() (derived from std::runtime_error) to fetch the error message */
  742. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  743. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  744. }
  745. catch (std::runtime_error &e)
  746. {
  747. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  748. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  749. }
  750. }
  751. else
  752. {
  753. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  754. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  755. }
  756. return SUCCESS;
  757. }
  758. else
  759. {
  760. return ec;
  761. }
  762. return SUCCESS;
  763. }
  764. //查询 取车的车辆感测信息 取车指令 根据key 查询感测信息
  765. Error_manager Dispatch_command::query_dispatch_vehicle_for_primary_key()
  766. {
  767. //查询 车辆表
  768. char query_parkspace_sql[1024];
  769. memset(query_parkspace_sql, 0, 1024);
  770. int t_statu = 0; //车位状态, 0可用, 1故障
  771. sprintf(query_parkspace_sql,"select * from vehicle where primary_key = '%s' ",m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str());
  772. // std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
  773. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  774. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  775. if(ec == SUCCESS)
  776. {
  777. if(tp_result == nullptr)
  778. {
  779. return DB_RESULT_SET_EMPTY;
  780. }
  781. //只取第一条结果, 默认是id最小的,
  782. if (tp_result->next())
  783. {
  784. char buf[1024];
  785. memset(buf, 0, 1024);
  786. try
  787. {
  788. //解析数据
  789. m_dispatch_vehicle_info.m_car_number = tp_result->getString("car_number");
  790. m_dispatch_vehicle_info.m_primary_key = tp_result->getString("primary_key");
  791. m_dispatch_vehicle_info.m_actually_measure_info = tp_result->getString("actually_measure_info");
  792. //取车, 数据库特有车位信息
  793. if(! google::protobuf::TextFormat::ParseFromString(m_dispatch_vehicle_info.m_actually_measure_info, &m_dispatch_vehicle_info.m_actually_measure_info_msg))
  794. {
  795. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  796. "ParseFromString fun error, m_space_info ");
  797. }
  798. }
  799. catch (sql::SQLException &e)
  800. {
  801. /* Use what() (derived from std::runtime_error) to fetch the error message */
  802. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  803. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  804. }
  805. catch (std::runtime_error &e)
  806. {
  807. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  808. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  809. }
  810. }
  811. else
  812. {
  813. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  814. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  815. }
  816. return SUCCESS;
  817. }
  818. else
  819. {
  820. return ec;
  821. }
  822. return SUCCESS;
  823. }
  824. //更新 车位状态, 根据车位ID 写入车牌号即可
  825. Error_manager Dispatch_command::update_parkspace_info_write_car_number()
  826. {
  827. //执行sql操作
  828. char update_space_sql[1024];
  829. memset(update_space_sql, 0, 1024);
  830. sprintf(update_space_sql, "update space set car_number = '%s' where id = %d",
  831. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str(),
  832. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.id() );
  833. // m_dispatch_space_info.m_id);
  834. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  835. return ec;
  836. }
  837. //更新 车位状态, 找到车牌号, 写NULL
  838. Error_manager Dispatch_command::update_parkspace_info_clear_car_number()
  839. {
  840. //执行sql操作
  841. char update_space_sql[1024];
  842. memset(update_space_sql, 0, 1024);
  843. sprintf(update_space_sql, "update space set car_number = null where car_number = '%s' ",
  844. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  845. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  846. return ec;
  847. }
  848. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  849. Error_manager Dispatch_command::update_command_queue_for_statu(int statu)
  850. {
  851. char update_command_sql[1024];
  852. memset(update_command_sql, 0, 1024);
  853. sprintf(update_command_sql, "update command_queue set statu = %d, export_id = %d where car_number = '%s'",
  854. statu, m_dispatch_command_map[m_car_number_optimal].m_export_id,
  855. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  856. // LOG(INFO) << "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj update_command_sql = "<< update_command_sql << " --- " << this;
  857. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_command_sql);
  858. return ec;
  859. }
  860. //删除 指令队列, 根据车牌号 删除指令
  861. Error_manager Dispatch_command::delete_command_queue_for_statu()
  862. {
  863. char delete_command_sql[1024];
  864. memset(delete_command_sql, 0, 1024);
  865. sprintf(delete_command_sql, "delete from command_queue where car_number = '%s'",
  866. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  867. Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_command_sql);
  868. return ec;
  869. }
  870. //制作存车表单
  871. Error_manager Dispatch_command::create_park_table()
  872. {
  873. park_table t_park_table;
  874. t_park_table.mutable_statu()->set_execute_statu(eNormal);
  875. t_park_table.set_queue_id(m_dispatch_command_map[m_car_number_optimal].m_queue_id);
  876. t_park_table.set_car_number(m_dispatch_command_map[m_car_number_optimal].m_car_number);
  877. t_park_table.set_unit_id(m_dispatch_command_map[m_car_number_optimal].m_unit);
  878. t_park_table.set_primary_key(m_dispatch_command_map[m_car_number_optimal].m_primary_key);
  879. t_park_table.mutable_entrance_measure_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg);
  880. t_park_table.mutable_allocated_space_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg);
  881. //根据雷达x坐标, 计算入口id
  882. int t_terminal = 0;
  883. if (m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()<0)
  884. {
  885. t_terminal = m_dispatch_id*2+1;
  886. }
  887. else if(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()>0)
  888. {
  889. t_terminal = m_dispatch_id*2+2;
  890. }
  891. t_park_table.set_terminal_id(t_terminal);
  892. // std::cout << " huli test :::: " << " iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii m_park_table_msg.entrance_measure_info().cx() = " << m_park_table_msg.entrance_measure_info().cx() << std::endl;
  893. // std::cout << " huli test :::: " << " iiiiiiiiiiiiiiiiiiiiiiiiiiiiii t_terminal = " << t_terminal << std::endl;
  894. // LOG(INFO) << " m_park_table_msg.entrance_measure_info().cx() = "<< m_park_table_msg.entrance_measure_info().cx() << " --- " << this;
  895. // LOG(INFO) << " t_terminal = "<< t_terminal << " --- " << this;
  896. // LOG(INFO) << " m_device_floor = "<< m_device_floor << " --- " << this;
  897. // LOG(INFO) << " m_dispatch_id = "<< m_dispatch_id << " --- " << this;
  898. // LOG(INFO) << " m_unit = "<< m_unit << " --- " << this;
  899. // LOG(INFO) << " m_outlet_ready = "<< m_outlet_ready << " --- " << this;
  900. m_park_table_msg = t_park_table;
  901. return Error_code::SUCCESS;
  902. }
  903. //制作取车表单
  904. Error_manager Dispatch_command::create_pick_table(int outlet_ready)
  905. {
  906. pick_table t_pick_table;
  907. t_pick_table.mutable_statu()->set_execute_statu(eNormal);
  908. t_pick_table.set_queue_id(m_dispatch_command_map[m_car_number_optimal].m_queue_id);
  909. t_pick_table.set_car_number(m_dispatch_command_map[m_car_number_optimal].m_car_number);
  910. t_pick_table.set_unit_id(m_dispatch_command_map[m_car_number_optimal].m_unit);
  911. t_pick_table.set_primary_key(m_dispatch_command_map[m_car_number_optimal].m_primary_key);
  912. t_pick_table.mutable_actually_measure_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg);
  913. t_pick_table.mutable_actually_space_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg);
  914. t_pick_table.set_terminal_id(outlet_ready);
  915. // LOG(INFO) << " outlet_ready = "<< outlet_ready << " --- " << this;
  916. //
  917. // LOG(INFO) << " m_device_floor = "<< m_device_floor << " --- " << this;
  918. // LOG(INFO) << " m_dispatch_id = "<< m_dispatch_id << " --- " << this;
  919. // LOG(INFO) << " m_unit = "<< m_unit << " --- " << this;
  920. // LOG(INFO) << " m_outlet_ready = "<< m_outlet_ready << " --- " << this;
  921. m_pick_table_msg = t_pick_table;
  922. return Error_code::SUCCESS;
  923. }
  924. //增加 车辆表, 存车指令 完成后添加车辆信息
  925. Error_manager Dispatch_command::insert_vehicle_for_car_number()
  926. {
  927. char insert_vehicle_sql[1024];
  928. memset(insert_vehicle_sql, 0, 1024);
  929. sprintf(insert_vehicle_sql, "INSERT INTO vehicle (car_number,primary_key,actually_measure_info) values ('%s','%s','%s')",
  930. m_car_number_optimal.c_str(), m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
  931. m_dispatch_command_map[m_car_number_optimal].m_measure_info.c_str() );
  932. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
  933. return ec;
  934. }
  935. //20230213 huli add plc car_wheel_base
  936. Error_manager Dispatch_command::insert_vehicle_for_car_number_ex()
  937. {
  938. //20230213 huli add plc car_wheel_base
  939. measure_info t_plc_measure_info_msg = m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg;
  940. t_plc_measure_info_msg.set_wheelbase( Dispatch_manager::get_instance_references().m_dispatch_plc.m_plc_car_wheel_base);
  941. char insert_vehicle_sql[1024];
  942. memset(insert_vehicle_sql, 0, 1024);
  943. sprintf(insert_vehicle_sql, "INSERT INTO vehicle (car_number,primary_key,actually_measure_info, plc_measure_info, plc_clamp_lidar_deviation_1, plc_clamp_lidar_deviation_2, plc_clamp_lidar_deviation_3, plc_clamp_lidar_deviation_4) values ('%s','%s','%s', '%s', %f, %f, %f, %f)",
  944. m_car_number_optimal.c_str(), m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
  945. m_dispatch_command_map[m_car_number_optimal].m_measure_info.c_str() ,
  946. t_plc_measure_info_msg.DebugString().c_str(),
  947. Dispatch_manager::get_instance_references().m_dispatch_plc.m_plc_clamp_lidar_deviation_1,
  948. Dispatch_manager::get_instance_references().m_dispatch_plc.m_plc_clamp_lidar_deviation_2,
  949. Dispatch_manager::get_instance_references().m_dispatch_plc.m_plc_clamp_lidar_deviation_3,
  950. Dispatch_manager::get_instance_references().m_dispatch_plc.m_plc_clamp_lidar_deviation_4
  951. );
  952. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
  953. return ec;
  954. }
  955. //删除 车辆表, 取车指令 完成后删除车辆信息
  956. Error_manager Dispatch_command::delete_vehicle_for_car_number()
  957. {
  958. char delete_vehicle_sql[1024];
  959. memset(delete_vehicle_sql, 0, 1024);
  960. sprintf(delete_vehicle_sql, "delete from vehicle where car_number = '%s'",
  961. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  962. Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_vehicle_sql);
  963. return ec;
  964. }
  965. //增加 记录表, 存车指令 完成后添加存车记录
  966. Error_manager Dispatch_command::insert_record_for_park_start()
  967. {
  968. Time_tool::get_instance_references().time_start(PARK_TIME_FLAG);
  969. int t_terminal = 0;
  970. if (m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()<0)
  971. {
  972. t_terminal = m_dispatch_id*2+1;
  973. }
  974. else if(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()>0)
  975. {
  976. t_terminal = m_dispatch_id*2+2;
  977. }
  978. char insert_record_sql[1024];
  979. memset(insert_record_sql, 0, 1024);
  980. // sprintf(insert_record_sql, "INSERT INTO record (primary_key,car_number,in_time_start,measure_info,import_id) values ('%s','%s','%s','%s',%d)",
  981. sprintf(insert_record_sql, "INSERT INTO record (primary_key,car_number,in_time_start,center_x,center_y,theta,width,height,wheelbase,front_theta,import_id) values ('%s','%s','%s',%f,%f,%f,%f,%f,%f,%f,%d)",
  982. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
  983. m_car_number_optimal.c_str(),
  984. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  985. // m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.id(),
  986. // m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.DebugString().c_str(),
  987. // m_dispatch_command_map[m_car_number_optimal].m_measure_info.c_str(),
  988. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx(),
  989. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cy(),
  990. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.theta(),
  991. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.width(),
  992. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height(),
  993. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.wheelbase(),
  994. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.front_theta(),
  995. t_terminal );
  996. LOG(INFO) << " insert_record_sql = "<< insert_record_sql << " --- " << this;
  997. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_record_sql);
  998. return ec;
  999. }
  1000. //增加 记录表, 存车指令 完成后添加存车记录
  1001. Error_manager Dispatch_command::updata_record_for_park_end()
  1002. {
  1003. Time_tool::get_instance_references().time_end(PARK_TIME_FLAG);
  1004. int in_time_difference = Time_tool::get_instance_references().get_time_seconds(PARK_TIME_FLAG);
  1005. //t_index_id是指定单元的车位id, 1~78
  1006. int t_table_id = (m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.id() - 1 ) % 78;
  1007. t_table_id = t_table_id + 1;
  1008. //执行sql操作
  1009. char update_record_sql[1024];
  1010. memset(update_record_sql, 0, 1024);
  1011. // sprintf(update_record_sql, "update record set in_time_end = '%s', in_time_difference = %d, space_id = %d, space_info = '%s' where primary_key = '%s' ",
  1012. sprintf(update_record_sql, "update record set in_time_end = '%s', in_time_difference = %d, space_id = %d, table_id = %d, unit_id = %d, floor_id = %d, room_id = %d where primary_key = '%s' ",
  1013. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1014. in_time_difference,
  1015. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.id(),
  1016. t_table_id,
  1017. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.unit_id(),
  1018. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.floor(),
  1019. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.room_id(),
  1020. // m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.DebugString().c_str(),
  1021. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  1022. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  1023. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  1024. return ec;
  1025. }
  1026. //更新 记录表, 取车指令 完成后更新取车记录
  1027. Error_manager Dispatch_command::updata_record_for_pick_start()
  1028. {
  1029. Time_tool::get_instance_references().time_start(PICK_TIME_FLAG);
  1030. //执行sql操作
  1031. char update_record_sql[1024];
  1032. memset(update_record_sql, 0, 1024);
  1033. sprintf(update_record_sql, "update record set out_time_start = '%s', export_id = %d where primary_key = '%s' ",
  1034. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1035. m_dispatch_command_map[m_car_number_optimal].m_export_id,
  1036. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  1037. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  1038. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  1039. return ec;
  1040. }
  1041. //更新 记录表, 取车指令 完成后更新取车记录
  1042. Error_manager Dispatch_command::updata_record_for_pick_end()
  1043. {
  1044. Time_tool::get_instance_references().time_end(PICK_TIME_FLAG);
  1045. int out_time_difference = Time_tool::get_instance_references().get_time_seconds(PICK_TIME_FLAG);
  1046. int parking_time;
  1047. get_parking_time(m_dispatch_command_map[m_car_number_optimal].m_primary_key, parking_time);
  1048. char parking_time_string[256] = "";
  1049. int hour = parking_time / 3600;
  1050. int min = (parking_time / 60)%60;
  1051. int sec = parking_time % 60;
  1052. sprintf(parking_time_string, " %d:%02d:%02d", hour, min, sec);
  1053. //执行sql操作
  1054. char update_record_sql[1024];
  1055. memset(update_record_sql, 0, 1024);
  1056. sprintf(update_record_sql, "update record set out_time_end = '%s', out_time_difference = %d, parking_time = '%s' where primary_key = '%s' ",
  1057. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1058. out_time_difference,
  1059. parking_time_string,
  1060. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  1061. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  1062. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  1063. return ec;
  1064. }
  1065. //更新 记录表, 取车指令 完成后更新取车记录, 撤销指令, 没有取车时间差
  1066. Error_manager Dispatch_command::updata_record_for_pick_end_ex()
  1067. {
  1068. int parking_time;
  1069. get_parking_time(m_dispatch_command_map[m_car_number_optimal].m_primary_key, parking_time);
  1070. char parking_time_string[256] = "";
  1071. int hour = parking_time / 3600;
  1072. int min = parking_time / 60;
  1073. int sec = parking_time % 60;
  1074. sprintf(parking_time_string, " %d:%02d:%02d", hour, min, sec);
  1075. //执行sql操作
  1076. char update_record_sql[1024];
  1077. memset(update_record_sql, 0, 1024);
  1078. sprintf(update_record_sql, "update record set out_time_end = '%s', parking_time = '%s' where primary_key = '%s' ",
  1079. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1080. parking_time_string,
  1081. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  1082. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  1083. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  1084. return ec;
  1085. }
  1086. //计算汽车在车库的存车时间, 从存车开始到取车结束, 返回 parking_time, 单位秒.
  1087. Error_manager Dispatch_command::get_parking_time(std::string primary_key, int & parking_time )
  1088. {
  1089. //查询存车时间 in_time_start
  1090. char query_record_sql[1024];
  1091. memset(query_record_sql, 0, 1024);
  1092. sprintf(query_record_sql,"select * from record where primary_key = '%s' ", primary_key.c_str() );
  1093. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  1094. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_record_sql, tp_result);
  1095. if(ec == SUCCESS)
  1096. {
  1097. if(tp_result == nullptr)
  1098. {
  1099. return DB_RESULT_SET_EMPTY;
  1100. }
  1101. //只取第一条结果, 默认是id最小的,
  1102. if (tp_result->next())
  1103. {
  1104. char buf[1024];
  1105. memset(buf, 0, 1024);
  1106. try
  1107. {
  1108. //解析数据
  1109. std::string in_time_start_string = tp_result->getString("in_time_start");
  1110. std::chrono::system_clock::time_point in_time_start = Time_tool::get_instance_references().transform_time_string_seconds(in_time_start_string);
  1111. std::chrono::system_clock::time_point out_time_end = std::chrono::system_clock::now();
  1112. double t_parking_time = (out_time_end - in_time_start).count() / 1000000000;
  1113. parking_time = t_parking_time;
  1114. return Error_code::SUCCESS;
  1115. }
  1116. catch (sql::SQLException &e)
  1117. {
  1118. /* Use what() (derived from std::runtime_error) to fetch the error message */
  1119. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  1120. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1121. }
  1122. catch (std::runtime_error &e)
  1123. {
  1124. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  1125. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1126. }
  1127. }
  1128. else
  1129. {
  1130. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  1131. "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
  1132. }
  1133. return SUCCESS;
  1134. }
  1135. else
  1136. {
  1137. return ec;
  1138. }
  1139. return Error_code::SUCCESS;
  1140. }