dispatch_command.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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, statu = 1 where car_number = '%s' ",
  699. sprintf(update_space_sql, "update space set car_number = null where car_number = '%s' ",
  700. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  701. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  702. return ec;
  703. }
  704. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  705. Error_manager Dispatch_command::update_command_queue_for_statu(int statu)
  706. {
  707. char update_command_sql[1024];
  708. memset(update_command_sql, 0, 1024);
  709. sprintf(update_command_sql, "update command_queue set statu = %d, export_id = %d where car_number = '%s'",
  710. statu, m_dispatch_command_map[m_car_number_optimal].m_export_id,
  711. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  712. // LOG(INFO) << "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj update_command_sql = "<< update_command_sql << " --- " << this;
  713. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_command_sql);
  714. return ec;
  715. }
  716. //更新 指令队列, 根据车牌号 删除指令
  717. Error_manager Dispatch_command::delete_command_queue_for_statu()
  718. {
  719. char delete_command_sql[1024];
  720. memset(delete_command_sql, 0, 1024);
  721. sprintf(delete_command_sql, "delete from command_queue where car_number = '%s'",
  722. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  723. Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_command_sql);
  724. return ec;
  725. }
  726. //制作存车表单
  727. Error_manager Dispatch_command::create_park_table()
  728. {
  729. park_table t_park_table;
  730. t_park_table.mutable_statu()->set_execute_statu(eNormal);
  731. t_park_table.set_queue_id(m_dispatch_command_map[m_car_number_optimal].m_queue_id);
  732. t_park_table.set_car_number(m_dispatch_command_map[m_car_number_optimal].m_car_number);
  733. t_park_table.set_unit_id(m_dispatch_command_map[m_car_number_optimal].m_unit);
  734. t_park_table.set_primary_key(m_dispatch_command_map[m_car_number_optimal].m_primary_key);
  735. t_park_table.mutable_entrance_measure_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg);
  736. t_park_table.mutable_allocated_space_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg);
  737. //根据雷达x坐标, 计算入口id
  738. int t_terminal = 0;
  739. if (m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()<0)
  740. {
  741. t_terminal = m_dispatch_id*2+1;
  742. }
  743. else if(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()>0)
  744. {
  745. t_terminal = m_dispatch_id*2+2;
  746. }
  747. t_park_table.set_terminal_id(t_terminal);
  748. // std::cout << " huli test :::: " << " iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii m_park_table_msg.entrance_measure_info().cx() = " << m_park_table_msg.entrance_measure_info().cx() << std::endl;
  749. // std::cout << " huli test :::: " << " iiiiiiiiiiiiiiiiiiiiiiiiiiiiii t_terminal = " << t_terminal << std::endl;
  750. LOG(INFO) << " m_park_table_msg.entrance_measure_info().cx() = "<< m_park_table_msg.entrance_measure_info().cx() << " --- " << this;
  751. LOG(INFO) << " t_terminal = "<< t_terminal << " --- " << this;
  752. LOG(INFO) << " m_device_floor = "<< m_device_floor << " --- " << this;
  753. LOG(INFO) << " m_dispatch_id = "<< m_dispatch_id << " --- " << this;
  754. LOG(INFO) << " m_unit = "<< m_unit << " --- " << this;
  755. LOG(INFO) << " m_outlet_ready = "<< m_outlet_ready << " --- " << this;
  756. m_park_table_msg = t_park_table;
  757. return Error_code::SUCCESS;
  758. }
  759. //制作取车表单
  760. Error_manager Dispatch_command::create_pick_table(int outlet_ready)
  761. {
  762. pick_table t_pick_table;
  763. t_pick_table.mutable_statu()->set_execute_statu(eNormal);
  764. t_pick_table.set_queue_id(m_dispatch_command_map[m_car_number_optimal].m_queue_id);
  765. t_pick_table.set_car_number(m_dispatch_command_map[m_car_number_optimal].m_car_number);
  766. t_pick_table.set_unit_id(m_dispatch_command_map[m_car_number_optimal].m_unit);
  767. t_pick_table.set_primary_key(m_dispatch_command_map[m_car_number_optimal].m_primary_key);
  768. t_pick_table.mutable_actually_measure_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg);
  769. t_pick_table.mutable_actually_space_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg);
  770. t_pick_table.set_terminal_id(outlet_ready);
  771. LOG(INFO) << " outlet_ready = "<< outlet_ready << " --- " << this;
  772. LOG(INFO) << " m_device_floor = "<< m_device_floor << " --- " << this;
  773. LOG(INFO) << " m_dispatch_id = "<< m_dispatch_id << " --- " << this;
  774. LOG(INFO) << " m_unit = "<< m_unit << " --- " << this;
  775. LOG(INFO) << " m_outlet_ready = "<< m_outlet_ready << " --- " << this;
  776. m_pick_table_msg = t_pick_table;
  777. return Error_code::SUCCESS;
  778. }
  779. //增加 车辆表, 存车指令 完成后添加车辆信息
  780. Error_manager Dispatch_command::insert_vehicle_for_car_number()
  781. {
  782. char insert_vehicle_sql[1024];
  783. memset(insert_vehicle_sql, 0, 1024);
  784. sprintf(insert_vehicle_sql, "INSERT INTO vehicle (car_number,primary_key,actually_measure_info) values ('%s','%s','%s')",
  785. m_car_number_optimal.c_str(), m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
  786. m_dispatch_command_map[m_car_number_optimal].m_measure_info.c_str() );
  787. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
  788. return ec;
  789. }
  790. //删除 车辆表, 取车指令 完成后删除车辆信息
  791. Error_manager Dispatch_command::delete_vehicle_for_car_number()
  792. {
  793. char delete_vehicle_sql[1024];
  794. memset(delete_vehicle_sql, 0, 1024);
  795. sprintf(delete_vehicle_sql, "delete from vehicle where car_number = '%s'",
  796. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  797. Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_vehicle_sql);
  798. return ec;
  799. }
  800. //增加 记录表, 存车指令 完成后添加存车记录
  801. Error_manager Dispatch_command::insert_record_for_park_start()
  802. {
  803. Time_tool::get_instance_references().time_start(PARK_TIME_FLAG);
  804. int t_terminal = 0;
  805. if (m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()<0)
  806. {
  807. t_terminal = m_dispatch_id*2+1;
  808. }
  809. else if(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()>0)
  810. {
  811. t_terminal = m_dispatch_id*2+2;
  812. }
  813. char insert_record_sql[1024];
  814. memset(insert_record_sql, 0, 1024);
  815. // sprintf(insert_record_sql, "INSERT INTO record (primary_key,car_number,in_time_start,measure_info,import_id) values ('%s','%s','%s','%s',%d)",
  816. 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)",
  817. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
  818. m_car_number_optimal.c_str(),
  819. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  820. // m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.id(),
  821. // m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.DebugString().c_str(),
  822. // m_dispatch_command_map[m_car_number_optimal].m_measure_info.c_str(),
  823. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx(),
  824. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cy(),
  825. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.theta(),
  826. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.width(),
  827. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height(),
  828. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.wheelbase(),
  829. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.front_theta(),
  830. t_terminal );
  831. LOG(INFO) << " insert_record_sql = "<< insert_record_sql << " --- " << this;
  832. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_record_sql);
  833. return ec;
  834. }
  835. //增加 记录表, 存车指令 完成后添加存车记录
  836. Error_manager Dispatch_command::updata_record_for_park_end()
  837. {
  838. Time_tool::get_instance_references().time_end(PARK_TIME_FLAG);
  839. int in_time_difference = Time_tool::get_instance_references().get_time_seconds(PARK_TIME_FLAG);
  840. //t_index_id是指定单元的车位id, 1~78
  841. int t_table_id = (m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.id() - 1 ) % 78;
  842. t_table_id = t_table_id + 1;
  843. //执行sql操作
  844. char update_record_sql[1024];
  845. memset(update_record_sql, 0, 1024);
  846. // 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' ",
  847. 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' ",
  848. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  849. in_time_difference,
  850. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.id(),
  851. t_table_id,
  852. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.unit_id(),
  853. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.floor(),
  854. m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.room_id(),
  855. // m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.DebugString().c_str(),
  856. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  857. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  858. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  859. return ec;
  860. }
  861. //更新 记录表, 取车指令 完成后更新取车记录
  862. Error_manager Dispatch_command::updata_record_for_pick_start()
  863. {
  864. Time_tool::get_instance_references().time_start(PICK_TIME_FLAG);
  865. //执行sql操作
  866. char update_record_sql[1024];
  867. memset(update_record_sql, 0, 1024);
  868. sprintf(update_record_sql, "update record set out_time_start = '%s', export_id = %d where primary_key = '%s' ",
  869. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  870. m_dispatch_command_map[m_car_number_optimal].m_export_id,
  871. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  872. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  873. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  874. return ec;
  875. }
  876. //更新 记录表, 取车指令 完成后更新取车记录
  877. Error_manager Dispatch_command::updata_record_for_pick_end()
  878. {
  879. Time_tool::get_instance_references().time_end(PICK_TIME_FLAG);
  880. int out_time_difference = Time_tool::get_instance_references().get_time_seconds(PICK_TIME_FLAG);
  881. int parking_time;
  882. get_parking_time(m_dispatch_command_map[m_car_number_optimal].m_primary_key, parking_time);
  883. char parking_time_string[256] = "";
  884. int hour = parking_time / 3600;
  885. int min = (parking_time / 60)%60;
  886. int sec = parking_time % 60;
  887. sprintf(parking_time_string, " %d:%02d:%02d", hour, min, sec);
  888. //执行sql操作
  889. char update_record_sql[1024];
  890. memset(update_record_sql, 0, 1024);
  891. sprintf(update_record_sql, "update record set out_time_end = '%s', out_time_difference = %d, parking_time = '%s' where primary_key = '%s' ",
  892. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  893. out_time_difference,
  894. parking_time_string,
  895. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  896. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  897. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  898. return ec;
  899. }
  900. //更新 记录表, 取车指令 完成后更新取车记录, 撤销指令, 没有取车时间差
  901. Error_manager Dispatch_command::updata_record_for_pick_end_ex()
  902. {
  903. int parking_time;
  904. get_parking_time(m_dispatch_command_map[m_car_number_optimal].m_primary_key, parking_time);
  905. char parking_time_string[256] = "";
  906. int hour = parking_time / 3600;
  907. int min = (parking_time / 60)%60;
  908. int sec = parking_time % 60;
  909. sprintf(parking_time_string, " %d:%02d:%02d", hour, min, sec);
  910. //执行sql操作
  911. char update_record_sql[1024];
  912. memset(update_record_sql, 0, 1024);
  913. sprintf(update_record_sql, "update record set out_time_end = '%s', parking_time = '%s' where primary_key = '%s' ",
  914. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  915. parking_time_string,
  916. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  917. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  918. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  919. return ec;
  920. }
  921. //计算汽车在车库的存车时间, 从存车开始到取车结束, 返回 parking_time, 单位秒.
  922. Error_manager Dispatch_command::get_parking_time(std::string primary_key, int & parking_time )
  923. {
  924. //查询存车时间 in_time_start
  925. char query_record_sql[1024];
  926. memset(query_record_sql, 0, 1024);
  927. sprintf(query_record_sql,"select * from record where primary_key = '%s' ", primary_key.c_str() );
  928. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  929. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_record_sql, tp_result);
  930. if(ec == SUCCESS)
  931. {
  932. if(tp_result == nullptr)
  933. {
  934. return DB_RESULT_SET_EMPTY;
  935. }
  936. //只取第一条结果, 默认是id最小的,
  937. if (tp_result->next())
  938. {
  939. char buf[1024];
  940. memset(buf, 0, 1024);
  941. try
  942. {
  943. //解析数据
  944. std::string in_time_start_string = tp_result->getString("in_time_start");
  945. std::chrono::system_clock::time_point in_time_start = Time_tool::get_instance_references().transform_time_string_seconds(in_time_start_string);
  946. std::chrono::system_clock::time_point out_time_end = std::chrono::system_clock::now();
  947. double t_parking_time = (out_time_end - in_time_start).count() / 1000000000;
  948. parking_time = t_parking_time;
  949. return Error_code::SUCCESS;
  950. }
  951. catch (sql::SQLException &e)
  952. {
  953. /* Use what() (derived from std::runtime_error) to fetch the error message */
  954. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  955. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  956. }
  957. catch (std::runtime_error &e)
  958. {
  959. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  960. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  961. }
  962. }
  963. else
  964. {
  965. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  966. "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
  967. }
  968. return SUCCESS;
  969. }
  970. else
  971. {
  972. return ec;
  973. }
  974. return Error_code::SUCCESS;
  975. }