dispatch_command.cpp 41 KB

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