dispatch_command.cpp 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  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. m_dispatch_space_info_count = 0;
  12. }
  13. Dispatch_command::~Dispatch_command()
  14. {
  15. }
  16. #ifdef SHANGGUJIE_PROJECT_PROJECT
  17. //存车指令入队, 检查节点直接通过rabbitmq发送给调度, 调度接受后数据库指令入队,
  18. Error_manager Dispatch_command::insert_park_command(park_table park_command)
  19. {
  20. Dispatch_command_info t_dispatch_command_info;
  21. t_dispatch_command_info.m_car_number = park_command.car_number();
  22. t_dispatch_command_info.m_primary_key = park_command.primary_key();
  23. t_dispatch_command_info.m_unit = park_command.unit_id();
  24. t_dispatch_command_info.m_terminal_id = park_command.terminal_id();
  25. //queue_id 当前之间的总秒, (后续排序的权重参数是 搬运器和起点的距离 用户等待时间 ) (当前之间的总秒, 系统时间单位是纳秒, 除以10^9转化为秒)
  26. t_dispatch_command_info.m_queue_id = std::chrono::system_clock::now().time_since_epoch().count()/1000000000;
  27. t_dispatch_command_info.m_type = 1;//1表示存车, 2表示取车
  28. t_dispatch_command_info.m_statu = 0;//0表示排队中, 1表示运行中, 2表示完成, 3表示故障
  29. t_dispatch_command_info.m_import_id = get_passway_id(park_command.terminal_id(), 1);//
  30. t_dispatch_command_info.m_export_id = 0;//出口id, 存车时不写
  31. t_dispatch_command_info.m_height = park_command.entrance_measure_info().height();
  32. t_dispatch_command_info.m_space_info_msg;//入队时不写, 后续运行时补全
  33. t_dispatch_command_info.m_measure_info_msg = park_command.entrance_measure_info();
  34. t_dispatch_command_info.m_car_number_info_msg = park_command.car_number_info();
  35. char insert_command_queue_sql[1024*1024*2];
  36. memset(insert_command_queue_sql, 0, 1024*1024*2);
  37. sprintf(insert_command_queue_sql, "INSERT INTO command_queue (car_number,primary_key, unit, terminal_id , queue_id, type, statu, import_id, export_id, height, space_info, measure_info, plate_color, plate_type, plate_confidence, recognition_time, plate_full_image, plate_clip_image) values ('%s','%s', %d, %d, %d, %d, %d, %d, %d, %f, '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s')",
  38. t_dispatch_command_info.m_car_number.c_str(),
  39. t_dispatch_command_info.m_primary_key.c_str(),
  40. t_dispatch_command_info.m_unit,
  41. t_dispatch_command_info.m_terminal_id,
  42. t_dispatch_command_info.m_queue_id,
  43. t_dispatch_command_info.m_type,
  44. t_dispatch_command_info.m_statu,
  45. t_dispatch_command_info.m_import_id,
  46. t_dispatch_command_info.m_export_id,
  47. t_dispatch_command_info.m_height,
  48. t_dispatch_command_info.m_space_info_msg.DebugString().c_str(),
  49. t_dispatch_command_info.m_measure_info_msg.DebugString().c_str(),
  50. t_dispatch_command_info.m_car_number_info_msg.plate_color().c_str(),
  51. t_dispatch_command_info.m_car_number_info_msg.plate_type().c_str(),
  52. t_dispatch_command_info.m_car_number_info_msg.plate_confidence(),
  53. t_dispatch_command_info.m_car_number_info_msg.recognition_time().c_str(),
  54. t_dispatch_command_info.m_car_number_info_msg.plate_full_image().c_str(),
  55. t_dispatch_command_info.m_car_number_info_msg.plate_clip_image().c_str()
  56. );
  57. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_command_queue_sql);
  58. return ec;
  59. return Error_code::SUCCESS;
  60. }
  61. //取车指令入队, 检查节点直接通过rabbitmq发送给调度, 调度接受后数据库指令入队,
  62. Error_manager Dispatch_command::insert_pick_command(pick_table pick_command)
  63. {
  64. Error_manager t_error;
  65. Dispatch_command_info t_dispatch_command_info;
  66. //只使用取车表单的唯一码, 其他信息去数据库查.
  67. t_dispatch_command_info.m_primary_key = pick_command.primary_key();
  68. t_error = query_dispatch_space_for_primary_key(t_dispatch_command_info.m_primary_key, t_dispatch_command_info.m_car_number, t_dispatch_command_info.m_space_info_msg, t_dispatch_command_info.m_measure_info_msg);
  69. t_dispatch_command_info.m_unit = t_dispatch_command_info.m_space_info_msg.unit_id();
  70. t_dispatch_command_info.m_terminal_id = pick_command.terminal_id();
  71. //queue_id 当前之间的总秒, (后续排序的权重参数是 搬运器和起点的距离 用户等待时间 ) (当前之间的总秒, 系统时间单位是纳秒, 除以10^9转化为秒)
  72. t_dispatch_command_info.m_queue_id = std::chrono::system_clock::now().time_since_epoch().count()/1000000000;
  73. t_dispatch_command_info.m_type = 2;//1表示存车, 2表示取车
  74. t_dispatch_command_info.m_statu = 0;//0表示排队中, 1表示运行中, 2表示完成, 3表示故障
  75. t_dispatch_command_info.m_import_id = 0;//入口id, 取车时不写
  76. t_dispatch_command_info.m_export_id = 0;//出口id, 入队时不写, 后续运行时补全
  77. t_dispatch_command_info.m_height = t_dispatch_command_info.m_measure_info_msg.height();
  78. char insert_command_queue_sql[1024];
  79. memset(insert_command_queue_sql, 0, 1024);
  80. sprintf(insert_command_queue_sql, "INSERT INTO command_queue (car_number,primary_key, unit, terminal_id , queue_id, type, statu, import_id, export_id, height, space_info, measure_info) values ('%s','%s', %d, %d, %d, %d, %d, %d, %d, %f, '%s', '%s')",
  81. t_dispatch_command_info.m_car_number.c_str(),
  82. t_dispatch_command_info.m_primary_key.c_str(),
  83. t_dispatch_command_info.m_unit,
  84. t_dispatch_command_info.m_terminal_id,
  85. t_dispatch_command_info.m_queue_id,
  86. t_dispatch_command_info.m_type,
  87. t_dispatch_command_info.m_statu,
  88. t_dispatch_command_info.m_import_id,
  89. t_dispatch_command_info.m_export_id,
  90. t_dispatch_command_info.m_height,
  91. t_dispatch_command_info.m_space_info_msg.DebugString().c_str(),
  92. t_dispatch_command_info.m_measure_info_msg.DebugString().c_str()
  93. );
  94. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_command_queue_sql);
  95. return ec;
  96. return Error_code::SUCCESS;
  97. }
  98. #endif//SHANGGUJIE_PROJECT_PROJECT
  99. //调度开始前, 向数据库发送请求的相关操作, 输入 穿梭机所在的楼层, 调度id 0~2, 空闲出口id, 如果不是0,就表示有空闲的出口
  100. Error_manager Dispatch_command::dispatch_request_to_sql(int device_floor, int dispatch_id, int outlet_ready)
  101. {
  102. std::unique_lock<std::mutex> t_lock(m_lock);
  103. Error_manager t_error;
  104. m_device_floor = device_floor;
  105. m_dispatch_id = dispatch_id;
  106. m_unit = dispatch_id;
  107. m_outlet_ready = outlet_ready;
  108. //获取调度指令, 与数据库同步 command_queue
  109. t_error = query_all_dispatch_command(0);
  110. if ( t_error != Error_code::SUCCESS )
  111. {
  112. return t_error;
  113. }
  114. //排序
  115. if ( outlet_ready == 0 )
  116. {
  117. //对调度指令进行排序, 选出最优解, 只比较存车
  118. t_error = sort_dispatch_command_for_park();
  119. }
  120. else
  121. {
  122. //对调度指令进行排序, 选出最优解, 比较存车和取车
  123. t_error = sort_dispatch_command_for_total();
  124. }
  125. if ( t_error != Error_code::SUCCESS )
  126. {
  127. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_TYPE_UNKNOW;
  128. //没有找到合适的指令就返回NODATA, 然后设么也不做
  129. return t_error;
  130. }
  131. else
  132. {
  133. if ( m_dispatch_command_map[m_car_number_optimal].m_type == 1 )//存车
  134. {
  135. //注注注注注意了
  136. //存车自带感测信息,没有车位信息, 需要查询车位表,找到空车位,补全车位信息
  137. //取车自带车位信息,没有感测信息, 需要查询车辆表,补全车辆信息
  138. //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解
  139. t_error = query_dispatch_space_optimal(); //获得最优解 m_dispatch_space_info
  140. if ( t_error != Error_code::SUCCESS )
  141. {
  142. update_command_queue_for_statu(3);
  143. return t_error;
  144. }
  145. else
  146. {
  147. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_STORE;
  148. //将最优解车位信息 补充到指令表
  149. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_id(m_dispatch_space_info[0].m_id);
  150. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_serial_id(m_dispatch_space_info[0].m_serial_id);
  151. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_table_id(m_dispatch_space_info[0].m_table_id);
  152. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_unit_id(m_dispatch_space_info[0].m_unit);
  153. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_floor(m_dispatch_space_info[0].m_floor);
  154. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_room_id(m_dispatch_space_info[0].m_room_id);
  155. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_height(m_dispatch_space_info[0].m_height);
  156. m_dispatch_command_map[m_car_number_optimal].m_space_info_string = m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.DebugString();
  157. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  158. t_error = update_command_queue_for_statu(1);
  159. //更新 车位状态, 根据车位ID 写入车牌号即可
  160. t_error = update_parkspace_write_car_number();
  161. //制作存车表单
  162. t_error = create_park_table();
  163. //增加 记录表, 存车指令 完成后添加存车记录
  164. t_error = insert_record_for_park_start();
  165. LOG(INFO) << " create_park_table 创建存车任务单 = "<< m_park_table_msg.DebugString() << " --- " << this;
  166. return Error_code::SUCCESS;
  167. }
  168. }
  169. else if ( m_dispatch_command_map[m_car_number_optimal].m_type == 2 )//取车
  170. {
  171. //注注注注注意了
  172. //存车自带感测信息,没有车位信息, 需要查询车位表,找到空车位,补全车位信息
  173. //取车自带车位信息,没有感测信息, 需要查询车辆表,补全车辆信息
  174. //查询 取车的车辆感测信息 取车指令 根据key 查询感测信息
  175. // t_error = query_dispatch_vehicle_for_primary_key();
  176. if ( t_error != Error_code::SUCCESS )
  177. {
  178. update_command_queue_for_statu(3);
  179. return t_error;
  180. }
  181. else
  182. {
  183. //补充出口id
  184. m_dispatch_command_map[m_car_number_optimal].m_export_id = outlet_ready;
  185. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_PICKUP;
  186. // m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg = m_dispatch_vehicle_info.m_actually_measure_info_msg;
  187. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  188. t_error = update_command_queue_for_statu(1);
  189. //制作取车表单
  190. t_error = create_pick_table(outlet_ready);
  191. //更新 记录表, 取车指令 完成后更新取车记录
  192. t_error = updata_record_for_pick_start();
  193. LOG(INFO) << " create_pick_table 创建取车车任务单 = "<< m_pick_table_msg.DebugString() << " --- " << this;
  194. }
  195. }
  196. else
  197. {
  198. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  199. " fun error ");
  200. }
  201. }
  202. return Error_code::SUCCESS;
  203. }
  204. //调度完成后, 向数据库发送答复的相关操作
  205. Error_manager Dispatch_command::dispatch_response_to_sql(Error_manager dispatch_result)
  206. {
  207. std::unique_lock<std::mutex> t_lock(m_lock);
  208. Error_manager t_error;
  209. if ( dispatch_result == Error_code::SUCCESS )
  210. {
  211. //调度成功, 完善数据库
  212. if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
  213. {
  214. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_STORE 存车答复成功 = "<< t_error << " --- " << this;
  215. //增加 车辆表, 存车指令 完成后添加车辆信息
  216. // t_error = insert_vehicle_for_car_number();
  217. //更新车牌图片信息,
  218. update_parkspace_write_car_number_info();
  219. //更新 指令队列, 根据车牌号 删除指令
  220. t_error = delete_command_queue_for_car_number();
  221. //增加 记录表, 存车指令 完成后添加存车记录
  222. t_error = updata_record_for_park_end();
  223. }
  224. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
  225. {
  226. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_PICKUP 取车答复成功 = "<< t_error << " --- " << this;
  227. //删除 车辆表, 取车指令 完成后删除车辆信息
  228. // t_error = delete_vehicle_for_car_number();
  229. //删除车牌图片信息,
  230. t_error = update_parkspace_clear_car_number_info();
  231. //更新 车位状态, 找到车牌号, 写NULL
  232. t_error = update_parkspace_clear_car_number();
  233. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  234. t_error = update_command_queue_for_statu(2);
  235. //更新 记录表, 取车指令 完成后更新取车记录
  236. t_error = updata_record_for_pick_end();
  237. }
  238. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
  239. {
  240. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_REALLOCATE 改道答复成功 = "<< t_error << " --- " << this;
  241. //增加 车辆表, 存车指令 完成后添加车辆信息
  242. // t_error = insert_vehicle_for_car_number();
  243. //如果多次重新分配, 需要把之前申请的车位清除,
  244. for (int i = 0; i < m_dispatch_space_info_count; ++i)
  245. {
  246. //如果多次重新分配, 需要把之前申请的车位清除 //更新 车位状态, 找到车牌号, 写NULL
  247. t_error = update_parkspace_clear_car_number_by_id(m_dispatch_space_info[i].m_id);
  248. }
  249. m_dispatch_space_info_count = 0;
  250. //更新车牌图片信息,
  251. update_parkspace_write_car_number_info();
  252. //更新 指令队列, 根据车牌号 删除指令
  253. t_error = delete_command_queue_for_car_number();
  254. //增加 记录表, 存车指令 完成后添加存车记录
  255. t_error = updata_record_for_park_end();
  256. }
  257. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
  258. {
  259. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_REVOCATION 撤销答复成功 = "<< t_error << " --- " << this;
  260. //删除 车辆表, 取车指令 完成后删除车辆信息
  261. // t_error = delete_vehicle_for_car_number();
  262. //如果多次重新分配, 需要把之前申请的车位清除,
  263. for (int i = 0; i < m_dispatch_space_info_count; ++i)
  264. {
  265. //如果多次重新分配, 需要把之前申请的车位清除 //更新 车位状态, 找到车牌号, 写NULL
  266. t_error = update_parkspace_clear_car_number_by_id(m_dispatch_space_info[i].m_id);
  267. }
  268. m_dispatch_space_info_count = 0;
  269. //删除车牌图片信息,
  270. t_error = update_parkspace_clear_car_number_info();
  271. //更新 车位状态, 找到车牌号, 写NULL
  272. t_error = update_parkspace_clear_car_number();
  273. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  274. t_error = update_command_queue_for_statu(2);
  275. // LOG(INFO) << "llllllllllllllllllllllllllllllllllllllllll respons = "<< t_error << " --- " << this;
  276. //更新 记录表, 取车指令 完成后更新取车记录, 撤销指令, 没有取车时间差
  277. t_error = updata_record_for_pick_end_ex();
  278. }
  279. else
  280. {
  281. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  282. " fun error ");
  283. }
  284. return t_error;
  285. }
  286. else
  287. {
  288. //调度失败, 回退 数据库
  289. if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
  290. {
  291. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_STORE 存车答复失败 = "<< t_error << " --- " << this;
  292. //删除车牌图片信息,
  293. update_parkspace_clear_car_number_info();
  294. //更新 车位状态, 找到车牌号, 写NULL
  295. update_parkspace_clear_car_number();
  296. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  297. update_command_queue_for_statu(3);
  298. }
  299. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
  300. {
  301. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_PICKUP 取车答复失败 = "<< t_error << " --- " << this;
  302. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  303. update_command_queue_for_statu(3);
  304. }
  305. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
  306. {
  307. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_STORE 改道答复失败 = "<< t_error << " --- " << this;
  308. //如果多次重新分配, 需要把之前申请的车位清除,
  309. for (int i = 0; i < m_dispatch_space_info_count; ++i)
  310. {
  311. //如果多次重新分配, 需要把之前申请的车位清除 //更新 车位状态, 找到车牌号, 写NULL
  312. t_error = update_parkspace_clear_car_number_by_id(m_dispatch_space_info[i].m_id);
  313. }
  314. m_dispatch_space_info_count = 0;
  315. //删除车牌图片信息,
  316. update_parkspace_clear_car_number_info();
  317. //更新 车位状态, 找到车牌号, 写NULL
  318. update_parkspace_clear_car_number();
  319. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  320. update_command_queue_for_statu(3);
  321. }
  322. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
  323. {
  324. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_PICKUP 撤销答复失败 = "<< t_error << " --- " << this;
  325. //如果多次重新分配, 需要把之前申请的车位清除,
  326. for (int i = 0; i < m_dispatch_space_info_count; ++i)
  327. {
  328. //如果多次重新分配, 需要把之前申请的车位清除 //更新 车位状态, 找到车牌号, 写NULL
  329. t_error = update_parkspace_clear_car_number_by_id(m_dispatch_space_info[i].m_id);
  330. }
  331. m_dispatch_space_info_count = 0;
  332. //删除车牌图片信息,
  333. update_parkspace_clear_car_number_info();
  334. //更新 车位状态, 找到车牌号, 写NULL
  335. update_parkspace_clear_car_number();
  336. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  337. update_command_queue_for_statu(3);
  338. }
  339. else
  340. {
  341. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  342. " fun error ");
  343. }
  344. }
  345. return Error_code::SUCCESS;
  346. }
  347. //调度 , 向数据库 重新分配车位,
  348. Error_manager Dispatch_command::dispatch_reallocate_to_sql(Common_data::Car_type reallocate_car_type, int outlet_ready)
  349. {
  350. std::unique_lock<std::mutex> t_lock(m_lock);
  351. Error_manager t_error;
  352. Error_manager t_error2;
  353. m_outlet_ready = outlet_ready;
  354. //重新申请车位
  355. //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解
  356. m_dispatch_space_info_count = m_dispatch_space_info_count +1;
  357. t_error = query_dispatch_space_optimal_ex(reallocate_car_type, m_dispatch_space_info[m_dispatch_space_info_count]);
  358. if ( t_error != Error_code::SUCCESS )
  359. {
  360. //没找到车位, 就把车放到出口
  361. if ( outlet_ready != 0 )
  362. {
  363. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_REVOCATION;
  364. //补充出口id
  365. m_dispatch_command_map[m_car_number_optimal].m_export_id = outlet_ready;
  366. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_id(0);
  367. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_serial_id(0);
  368. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_table_id(0);
  369. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_unit_id(0);
  370. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_floor(0);
  371. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_room_id(0);
  372. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_height(0);
  373. m_dispatch_command_map[m_car_number_optimal].m_space_info_string = m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.DebugString();
  374. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  375. // t_error = update_command_queue_for_statu(1);
  376. //制作取车表单
  377. t_error = create_pick_table(outlet_ready);
  378. LOG(INFO) << " create_pick_table 撤销存车, 取车到出口任务单 = "<< m_pick_table_msg.DebugString() << " --- " << this;
  379. return t_error;
  380. }
  381. else
  382. {
  383. //无限等待, 等待出口空闲
  384. return Error_code::NODATA;
  385. }
  386. }
  387. else
  388. {
  389. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_REALLOCATE;
  390. //找到新的车位, 重新执行任务
  391. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_id(m_dispatch_space_info[m_dispatch_space_info_count].m_id);
  392. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_serial_id(m_dispatch_space_info[m_dispatch_space_info_count].m_serial_id);
  393. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_table_id(m_dispatch_space_info[m_dispatch_space_info_count].m_table_id);
  394. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_unit_id(m_dispatch_space_info[m_dispatch_space_info_count].m_unit);
  395. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_floor(m_dispatch_space_info[m_dispatch_space_info_count].m_floor);
  396. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_room_id(m_dispatch_space_info[m_dispatch_space_info_count].m_room_id);
  397. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_height(m_dispatch_space_info[m_dispatch_space_info_count].m_height);
  398. m_dispatch_command_map[m_car_number_optimal].m_space_info_string = m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.DebugString();
  399. //更新 车位状态, 根据车位ID 写入车牌号即可
  400. t_error = update_parkspace_write_car_number();
  401. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  402. // t_error = update_command_queue_for_statu(1);
  403. //制作存车表单
  404. LOG(INFO) << " zxcxzczxczxczxczxczxczxczxczxczxcxzcx = "<< m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.id() << " --- " << this;
  405. t_error = create_park_table();
  406. LOG(INFO) << " create_park_table 改道存车, 存车新车位任务单 = "<< m_park_table_msg.DebugString() << " --- " << this;
  407. }
  408. return Error_code::SUCCESS;
  409. }
  410. //检查出口是否空闲, 检查指令队列的取车完成的出口id是否存在, 不存在就是空闲,返回成功
  411. Error_manager Dispatch_command::check_export_id_is_ready(int export_id)
  412. {
  413. std::unique_lock<std::mutex> t_lock(m_lock);
  414. //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
  415. char check_export_id_sql[1024];
  416. memset(check_export_id_sql, 0, 1024);
  417. sprintf(check_export_id_sql,"select * from command_queue where statu = 2 and type = 2 and export_id = %d",export_id);
  418. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  419. Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
  420. if(t_error == Error_code::SUCCESS)
  421. {
  422. if (tp_result == nullptr)
  423. {
  424. return DB_RESULT_SET_EMPTY;
  425. }
  426. //如果存在, 就报错, 不存在就是空闲,返回成功
  427. if ( tp_result->next() )
  428. {
  429. return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
  430. " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
  431. }
  432. else
  433. {
  434. return Error_code::SUCCESS;
  435. }
  436. }
  437. else
  438. {
  439. return t_error;
  440. }
  441. return Error_code::SUCCESS;
  442. }
  443. //检查出口取车是否完成, 指定的出口有取车完成指令就返回成功, 否则报错.
  444. Error_manager Dispatch_command::check_pickup_is_finish(int export_id)
  445. {
  446. std::unique_lock<std::mutex> t_lock(m_lock);
  447. //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
  448. char check_export_id_sql[1024];
  449. memset(check_export_id_sql, 0, 1024);
  450. sprintf(check_export_id_sql,"select * from command_queue where statu = 2 and type = 2 and export_id = %d",export_id);
  451. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  452. Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
  453. if(t_error == Error_code::SUCCESS)
  454. {
  455. if (tp_result == nullptr)
  456. {
  457. return DB_RESULT_SET_EMPTY;
  458. }
  459. //如果存在, 就报错, 不存在就是空闲,返回成功
  460. if ( tp_result->next() )
  461. {
  462. return Error_code::SUCCESS;
  463. }
  464. else
  465. {
  466. return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
  467. " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
  468. }
  469. }
  470. else
  471. {
  472. return t_error;
  473. }
  474. return Error_code::SUCCESS;
  475. }
  476. //检查入口存车是否开始, 指定的入口有存车开始指令就返回成功, 否则报错.
  477. Error_manager Dispatch_command::check_park_is_start(int import_id)
  478. {
  479. std::unique_lock<std::mutex> t_lock(m_lock);
  480. //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
  481. char check_export_id_sql[1024];
  482. memset(check_export_id_sql, 0, 1024);
  483. sprintf(check_export_id_sql,"select * from command_queue where type = 1 and import_id = %d and (statu=0 or statu =1)",import_id);
  484. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  485. Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
  486. if(t_error == Error_code::SUCCESS)
  487. {
  488. if (tp_result == nullptr)
  489. {
  490. return DB_RESULT_SET_EMPTY;
  491. }
  492. //如果存在, 就报错, 不存在就是空闲,返回成功
  493. if ( tp_result->next() )
  494. {
  495. return Error_code::SUCCESS;
  496. }
  497. else
  498. {
  499. return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
  500. " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
  501. }
  502. }
  503. else
  504. {
  505. return t_error;
  506. }
  507. return Error_code::SUCCESS;
  508. }
  509. //获取指定入口的 存车流程的 唯一码,
  510. Error_manager Dispatch_command::get_primary_key_for_store(int import_id, std::string & primary_key )
  511. {
  512. std::unique_lock<std::mutex> t_lock(m_lock);
  513. //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
  514. char check_export_id_sql[1024];
  515. memset(check_export_id_sql, 0, 1024);
  516. sprintf(check_export_id_sql,"select * from command_queue where type = 1 and import_id = %d and (statu=0 or statu =1)",import_id);
  517. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  518. Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
  519. if(t_error == Error_code::SUCCESS)
  520. {
  521. if (tp_result == nullptr)
  522. {
  523. return DB_RESULT_SET_EMPTY;
  524. }
  525. //如果存在, 就报错, 不存在就是空闲,返回成功
  526. if ( tp_result->next() )
  527. {
  528. char buf[1024];
  529. memset(buf, 0, 1024);
  530. try
  531. {
  532. //解析数据
  533. primary_key = tp_result->getString("primary_key");
  534. }
  535. catch (sql::SQLException &e)
  536. {
  537. /* Use what() (derived from std::runtime_error) to fetch the error message */
  538. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  539. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  540. }
  541. catch (std::runtime_error &e)
  542. {
  543. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  544. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  545. }
  546. return Error_code::SUCCESS;
  547. }
  548. else
  549. {
  550. return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
  551. " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
  552. }
  553. }
  554. else
  555. {
  556. return t_error;
  557. }
  558. return Error_code::SUCCESS;
  559. }
  560. //删除 指令队列, 根据出口编号
  561. Error_manager Dispatch_command::delete_command_queue_for_export_id(int export_id)
  562. {
  563. std::unique_lock<std::mutex> t_lock(m_lock);
  564. char delete_command_sql[1024];
  565. memset(delete_command_sql, 0, 1024);
  566. sprintf(delete_command_sql, "delete from command_queue where statu = 2 and type = 2 and export_id = %d ",
  567. export_id );
  568. Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_command_sql);
  569. return ec;
  570. }
  571. //添加plc数据储存
  572. Error_manager Dispatch_command::insert_plc_data(std::string plc_data, float hearbeat)
  573. {
  574. std::unique_lock<std::mutex> t_lock(m_lock);
  575. char insert_vehicle_sql[4096];
  576. memset(insert_vehicle_sql, 0, 4096);
  577. sprintf(insert_vehicle_sql, "INSERT INTO plc_data (data, hearbeat) values ('%s',%f)",
  578. plc_data.c_str(), hearbeat );
  579. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
  580. std::cout << " huli test :::: " << " ec = " << ec << std::endl;
  581. return ec;
  582. }
  583. //查询 取车的车位 取车指令 根据车牌号 查询对应的车位
  584. Error_manager Dispatch_command::query_dispatch_space_for_car_number(std::string car_number, parkspace_info & parkspace_info)
  585. {
  586. //查询车位表
  587. char query_parkspace_sql[1024];
  588. memset(query_parkspace_sql, 0, 1024);
  589. int t_statu = 0; //车位状态, 0可用, 1故障
  590. sprintf(query_parkspace_sql,"select * from space where car_number = '%s' ",car_number.c_str());
  591. // std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
  592. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  593. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  594. if(ec == SUCCESS)
  595. {
  596. if(tp_result == nullptr)
  597. {
  598. return DB_RESULT_SET_EMPTY;
  599. }
  600. //只取第一条结果, 默认是id最小的,
  601. if (tp_result->next())
  602. {
  603. char buf[1024];
  604. memset(buf, 0, 1024);
  605. try
  606. {
  607. //解析数据
  608. parkspace_info.set_id(tp_result->getInt("id"));
  609. parkspace_info.set_serial_id(tp_result->getInt("serial_id"));
  610. parkspace_info.set_table_id( tp_result->getInt("table_id"));
  611. parkspace_info.set_unit_id( tp_result->getInt("unit"));
  612. parkspace_info.set_floor( tp_result->getInt("floor"));
  613. parkspace_info.set_room_id( tp_result->getInt("room_id"));
  614. parkspace_info.set_height(tp_result->getDouble("height"));
  615. }
  616. catch (sql::SQLException &e)
  617. {
  618. /* Use what() (derived from std::runtime_error) to fetch the error message */
  619. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  620. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  621. }
  622. catch (std::runtime_error &e)
  623. {
  624. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  625. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  626. }
  627. }
  628. else
  629. {
  630. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  631. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  632. }
  633. return SUCCESS;
  634. }
  635. else
  636. {
  637. return ec;
  638. }
  639. return SUCCESS;
  640. }
  641. //查询 取车的车位 取车指令 根据车牌号 查询对应的车位
  642. Error_manager Dispatch_command::query_dispatch_space_for_primary_key(std::string primary_key,std::string &car_number, parkspace_info & parkspace_info, measure_info & measure_info)
  643. {
  644. //查询车位表
  645. char query_parkspace_sql[1024];
  646. memset(query_parkspace_sql, 0, 1024);
  647. int t_statu = 0; //车位状态, 0可用, 1故障
  648. sprintf(query_parkspace_sql,"select * from space where primary_key = '%s' ",primary_key.c_str());
  649. // std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
  650. // LOG(INFO) << " ++++++++++++++++++++++++++++++++++++ query_parkspace_sql = "<< query_parkspace_sql << " --- " << this;
  651. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  652. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  653. if(ec == SUCCESS)
  654. {
  655. if(tp_result == nullptr)
  656. {
  657. return DB_RESULT_SET_EMPTY;
  658. }
  659. //只取第一条结果, 默认是id最小的,
  660. if (tp_result->next())
  661. {
  662. char buf[1024];
  663. memset(buf, 0, 1024);
  664. try
  665. {
  666. //解析数据
  667. car_number = tp_result->getString("car_number");
  668. std::string parkspace_info_string = tp_result->getString("space_info");
  669. if(! google::protobuf::TextFormat::ParseFromString(parkspace_info_string, &parkspace_info))
  670. {
  671. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  672. "ParseFromString fun error, m_space_info ");
  673. }
  674. std::string measure_info_string = tp_result->getString("measure_info");
  675. if(! google::protobuf::TextFormat::ParseFromString(measure_info_string, &measure_info))
  676. {
  677. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  678. "ParseFromString fun error, m_space_info ");
  679. }
  680. }
  681. catch (sql::SQLException &e)
  682. {
  683. /* Use what() (derived from std::runtime_error) to fetch the error message */
  684. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  685. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  686. }
  687. catch (std::runtime_error &e)
  688. {
  689. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  690. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  691. }
  692. }
  693. else
  694. {
  695. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  696. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  697. }
  698. return SUCCESS;
  699. }
  700. else
  701. {
  702. return ec;
  703. }
  704. return SUCCESS;
  705. }
  706. //查询 取车的车辆感测信息 取车指令 根据key 查询感测信息
  707. Error_manager Dispatch_command::query_dispatch_vehicle_for_primary_key(std::string primary_key, measure_info & measure_info)
  708. {
  709. //查询 车辆表
  710. char query_parkspace_sql[1024];
  711. memset(query_parkspace_sql, 0, 1024);
  712. int t_statu = 0; //车位状态, 0可用, 1故障
  713. sprintf(query_parkspace_sql,"select * from vehicle where primary_key = '%s' ",primary_key.c_str());
  714. // std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
  715. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  716. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  717. if(ec == SUCCESS)
  718. {
  719. if(tp_result == nullptr)
  720. {
  721. return DB_RESULT_SET_EMPTY;
  722. }
  723. //只取第一条结果, 默认是id最小的,
  724. if (tp_result->next())
  725. {
  726. char buf[1024];
  727. memset(buf, 0, 1024);
  728. try
  729. {
  730. //解析数据
  731. std::string measure_info_string = tp_result->getString("actually_measure_info");
  732. //取车, 数据库特有车位信息
  733. if(! google::protobuf::TextFormat::ParseFromString(measure_info_string, &measure_info))
  734. {
  735. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  736. "ParseFromString fun error, m_space_info ");
  737. }
  738. }
  739. catch (sql::SQLException &e)
  740. {
  741. /* Use what() (derived from std::runtime_error) to fetch the error message */
  742. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  743. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  744. }
  745. catch (std::runtime_error &e)
  746. {
  747. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  748. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  749. }
  750. }
  751. else
  752. {
  753. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  754. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  755. }
  756. return SUCCESS;
  757. }
  758. else
  759. {
  760. return ec;
  761. }
  762. return SUCCESS;
  763. }
  764. //获取调度指令, 与数据库同步 command_queue, statu指令状态, 0:排队等待, 1正在运行, 2:已经完成
  765. Error_manager Dispatch_command::query_all_dispatch_command(int statu)
  766. {
  767. //从command_queue获取所有排队的指令,
  768. char query_all_dispatch_command_sql[1024];
  769. memset(query_all_dispatch_command_sql, 0, 1024);
  770. sprintf(query_all_dispatch_command_sql,"select * from command_queue where statu = %d and unit = %d",statu, m_unit);
  771. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  772. Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(query_all_dispatch_command_sql, tp_result);
  773. m_dispatch_command_map.clear();
  774. if(t_error == Error_code::SUCCESS)
  775. {
  776. if (tp_result == nullptr)
  777. {
  778. return DB_RESULT_SET_EMPTY;
  779. }
  780. //循环检查所有行
  781. while (tp_result->next())
  782. {
  783. Dispatch_command_info t_dispatch_command_info;
  784. char t_error_buf[1024];
  785. memset(t_error_buf, 0, 1024);
  786. try
  787. {
  788. //解析数据
  789. t_dispatch_command_info.m_car_number = tp_result->getString("car_number");
  790. t_dispatch_command_info.m_primary_key = tp_result->getString("primary_key");
  791. t_dispatch_command_info.m_unit = tp_result->getInt("unit");
  792. t_dispatch_command_info.m_terminal_id = tp_result->getInt("terminal_id");
  793. t_dispatch_command_info.m_queue_id = tp_result->getInt("queue_id");
  794. t_dispatch_command_info.m_type = tp_result->getInt("type");
  795. t_dispatch_command_info.m_statu = tp_result->getInt("statu");
  796. t_dispatch_command_info.m_export_id = tp_result->getInt("export_id");
  797. t_dispatch_command_info.m_import_id = tp_result->getInt("import_id");
  798. t_dispatch_command_info.m_space_info_string = tp_result->getString("space_info");
  799. t_dispatch_command_info.m_measure_info_string = tp_result->getString("measure_info");
  800. t_dispatch_command_info.m_car_number_info_msg.set_plate_color(tp_result->getString("plate_color") );
  801. t_dispatch_command_info.m_car_number_info_msg.set_plate_type(tp_result->getString("plate_type") );
  802. t_dispatch_command_info.m_car_number_info_msg.set_plate_confidence(tp_result->getInt("plate_confidence") );
  803. t_dispatch_command_info.m_car_number_info_msg.set_recognition_time(tp_result->getString("recognition_time") );
  804. t_dispatch_command_info.m_car_number_info_msg.set_plate_full_image(tp_result->getString("plate_full_image") );
  805. t_dispatch_command_info.m_car_number_info_msg.set_plate_clip_image(tp_result->getString("plate_clip_image") );
  806. t_dispatch_command_info.m_car_number_info_string = t_dispatch_command_info.m_car_number_info_msg.DebugString();
  807. //存车, 数据库特有 雷达信息, 没有车位信息, 需要根据车高,再去车位表申请车位
  808. if(! google::protobuf::TextFormat::ParseFromString(t_dispatch_command_info.m_measure_info_string, &t_dispatch_command_info.m_measure_info_msg))
  809. {
  810. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  811. "ParseFromString fun error, m_measure_info ");
  812. }
  813. //取车, 数据库特有车位信息
  814. if(! google::protobuf::TextFormat::ParseFromString(t_dispatch_command_info.m_space_info_string, &t_dispatch_command_info.m_space_info_msg))
  815. {
  816. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  817. "ParseFromString fun error, m_space_info ");
  818. }
  819. //取车, 数据库特有 车牌图片信息
  820. // if(! google::protobuf::TextFormat::ParseFromString(t_dispatch_command_info.m_car_number_info_string, &t_dispatch_command_info.m_car_number_info_msg))
  821. // {
  822. // return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  823. // "ParseFromString fun error, m_space_info ");
  824. // }
  825. // if(! t_dispatch_command_info.m_car_number_info_msg.ParseFromString(t_dispatch_command_info.m_car_number_info_string) )
  826. // {
  827. // return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  828. // "ParseFromString fun error, m_space_info ");
  829. // }
  830. //m_type指令类型, 0无效, 1存车, 2取车,
  831. if ( t_dispatch_command_info.m_type == 1)
  832. {
  833. t_dispatch_command_info.m_useless_distance = m_device_floor-1;
  834. }
  835. else if ( t_dispatch_command_info.m_type == 2 )
  836. {
  837. t_dispatch_command_info.m_useless_distance = t_dispatch_command_info.m_space_info_msg.floor()-m_device_floor;
  838. }
  839. else
  840. {
  841. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  842. "command_queue m_type error ");
  843. }
  844. if ( t_dispatch_command_info.m_useless_distance <0 )
  845. {
  846. t_dispatch_command_info.m_useless_distance = 0-t_dispatch_command_info.m_useless_distance;
  847. }
  848. m_dispatch_command_map[t_dispatch_command_info.m_car_number] = t_dispatch_command_info;
  849. }
  850. catch (sql::SQLException &e)
  851. {
  852. /* Use what() (derived from std::runtime_error) to fetch the error message */
  853. sprintf(t_error_buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(),
  854. e.getSQLState().c_str());
  855. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, t_error_buf);
  856. }
  857. catch (std::runtime_error &e)
  858. {
  859. sprintf(t_error_buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  860. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, t_error_buf);
  861. }
  862. }
  863. return SUCCESS;
  864. }
  865. else
  866. {
  867. return t_error;
  868. }
  869. return Error_code::SUCCESS;
  870. }
  871. //对调度指令进行排序, 选出最优解, 比较存车和取车
  872. Error_manager Dispatch_command::sort_dispatch_command_for_total()
  873. {
  874. int t_optimal_loss=0x7fffffff; //最优loss值
  875. m_car_number_optimal.clear();
  876. for (auto iter = m_dispatch_command_map.begin(); iter != m_dispatch_command_map.end(); ++iter)
  877. {
  878. //loss = 空跑路程 + 排队编号, 求最小值
  879. int t_current_loss = iter->second.m_useless_distance * SORT_LOSS_RATIO + iter->second.m_queue_id;
  880. if ( t_optimal_loss > t_current_loss )
  881. {
  882. t_optimal_loss = t_current_loss;
  883. m_car_number_optimal = iter->first;
  884. }
  885. }
  886. if ( m_car_number_optimal.empty() )
  887. {
  888. return Error_code::NODATA;
  889. }
  890. return Error_code::SUCCESS;
  891. }
  892. //对调度指令进行排序, 选出最优解, 只比较存车
  893. Error_manager Dispatch_command::sort_dispatch_command_for_park()
  894. {
  895. int t_optimal_loss=0x7fffffff; //最优loss值
  896. m_car_number_optimal.clear();
  897. for (auto iter = m_dispatch_command_map.begin(); iter != m_dispatch_command_map.end(); ++iter)
  898. {
  899. //loss = 空跑路程 + 排队编号, 求最小值
  900. int t_current_loss = iter->second.m_useless_distance * SORT_LOSS_RATIO + iter->second.m_queue_id;
  901. if ( t_optimal_loss > t_current_loss && iter->second.m_type == 1)
  902. {
  903. t_optimal_loss = t_current_loss;
  904. m_car_number_optimal = iter->first;
  905. }
  906. }
  907. if ( m_car_number_optimal.empty() )
  908. {
  909. return Error_code::NODATA;
  910. }
  911. return Error_code::SUCCESS;
  912. }
  913. //存车指令 获取 指定车高 指定单元 的车位信息,用于车位分配
  914. Error_manager Dispatch_command::query_specify_height_unit_parkspace_info(int unit, float height, Dispatch_space_info & dispatch_space_info)
  915. {
  916. //查询车位表
  917. char query_parkspace_sql[1024];
  918. memset(query_parkspace_sql, 0, 1024);
  919. int t_statu = 0; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  920. 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);
  921. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  922. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  923. std::cout << " huli test :::: " << " query_parkspace_sql = " << query_parkspace_sql << std::endl;
  924. if(ec == SUCCESS)
  925. {
  926. if(tp_result == nullptr)
  927. {
  928. return DB_RESULT_SET_EMPTY;
  929. }
  930. //只取第一条结果, 默认是id最小的,
  931. if (tp_result->next())
  932. {
  933. char buf[1024];
  934. memset(buf, 0, 1024);
  935. try
  936. {
  937. //解析数据
  938. dispatch_space_info.m_id = tp_result->getInt("id");
  939. dispatch_space_info.m_serial_id = tp_result->getInt("serial_id");
  940. dispatch_space_info.m_table_id = tp_result->getInt("table_id");
  941. dispatch_space_info.m_unit = tp_result->getInt("unit");
  942. dispatch_space_info.m_floor = tp_result->getInt("floor");
  943. dispatch_space_info.m_room_id = tp_result->getInt("room_id");
  944. dispatch_space_info.m_height = tp_result->getDouble("height");
  945. // dispatch_space_info.m_primary_key = tp_result->getString("primary_key");
  946. // dispatch_space_info.m_car_number = tp_result->getString("car_number");
  947. dispatch_space_info.m_statu = tp_result->getInt("statu");
  948. // dispatch_space_info.m_space_info_string = tp_result->getString("space_info");
  949. // dispatch_space_info.m_measure_info_string = tp_result->getString("measure_info");
  950. // //存车, 数据库特有 雷达信息, 没有车位信息, 需要根据车高,再去车位表申请车位
  951. // if(! google::protobuf::TextFormat::ParseFromString(dispatch_space_info.m_measure_info_string, &dispatch_space_info.m_measure_info_msg))
  952. // {
  953. // return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  954. // "ParseFromString fun error, m_measure_info ");
  955. // }
  956. // //取车, 数据库特有车位信息
  957. // if(! google::protobuf::TextFormat::ParseFromString(dispatch_space_info.m_space_info_string, &dispatch_space_info.m_space_info_msg))
  958. // {
  959. // return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  960. // "ParseFromString fun error, m_space_info ");
  961. // }
  962. }
  963. catch (sql::SQLException &e)
  964. {
  965. /* Use what() (derived from std::runtime_error) to fetch the error message */
  966. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  967. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  968. }
  969. catch (std::runtime_error &e)
  970. {
  971. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  972. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  973. }
  974. }
  975. else
  976. {
  977. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  978. "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
  979. }
  980. return SUCCESS;
  981. }
  982. else
  983. {
  984. return ec;
  985. }
  986. return SUCCESS;
  987. }
  988. //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解
  989. Error_manager Dispatch_command::query_dispatch_space_optimal()
  990. {
  991. Error_manager t_error;
  992. int t_unit = m_dispatch_command_map[m_car_number_optimal].m_unit;
  993. float t_height = m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height();
  994. //小车对应小车位 中车对应中车位 大车对应大车位
  995. if ( 0 < t_height && t_height <= CAR_HEIGHT_LIMIT_SMALL )
  996. {
  997. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_SMALL, m_dispatch_space_info[0]);
  998. if ( t_error == Error_code::SUCCESS )
  999. {
  1000. m_car_type = Common_data::Car_type::MIN_CAR;
  1001. return t_error;
  1002. }
  1003. }
  1004. if ( t_height <= CAR_HEIGHT_LIMIT_MIDDLE )
  1005. {
  1006. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_MIDDLE, m_dispatch_space_info[0]);
  1007. if ( t_error == Error_code::SUCCESS )
  1008. {
  1009. m_car_type = Common_data::Car_type::MID_CAR;
  1010. return t_error;
  1011. }
  1012. }
  1013. if ( t_height <= CAR_HEIGHT_LIMIT_BIG )
  1014. {
  1015. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_BIG, m_dispatch_space_info[0]);
  1016. if ( t_error == Error_code::SUCCESS )
  1017. {
  1018. m_car_type = Common_data::Car_type::BIG_CAR;
  1019. return t_error;
  1020. }
  1021. }
  1022. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  1023. " query_dispatch_space_optimal error ");
  1024. }
  1025. //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解
  1026. Error_manager Dispatch_command::query_dispatch_space_optimal_ex(Common_data::Car_type reallocate_car_type, Dispatch_space_info & dispatch_space_info)
  1027. {
  1028. Error_manager t_error;
  1029. int t_unit = m_dispatch_command_map[m_car_number_optimal].m_unit;
  1030. // float t_height = m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height();
  1031. //小车对应小车位 中车对应中车位 大车对应大车位
  1032. if ( reallocate_car_type == Common_data::Car_type::MIN_CAR )
  1033. {
  1034. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_SMALL, dispatch_space_info);
  1035. if ( t_error == Error_code::SUCCESS )
  1036. {
  1037. m_car_type = Common_data::Car_type::MIN_CAR;
  1038. return t_error;
  1039. }
  1040. }
  1041. if ( reallocate_car_type == Common_data::Car_type::MIN_CAR ||
  1042. reallocate_car_type == Common_data::Car_type::MID_CAR )
  1043. {
  1044. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_MIDDLE, dispatch_space_info);
  1045. if ( t_error == Error_code::SUCCESS )
  1046. {
  1047. m_car_type = Common_data::Car_type::MID_CAR;
  1048. return t_error;
  1049. }
  1050. }
  1051. if ( reallocate_car_type == Common_data::Car_type::MIN_CAR ||
  1052. reallocate_car_type == Common_data::Car_type::MID_CAR ||
  1053. reallocate_car_type == Common_data::Car_type::BIG_CAR )
  1054. {
  1055. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_BIG, dispatch_space_info);
  1056. if ( t_error == Error_code::SUCCESS )
  1057. {
  1058. m_car_type = Common_data::Car_type::BIG_CAR;
  1059. return t_error;
  1060. }
  1061. }
  1062. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  1063. " query_dispatch_space_optimal error ");
  1064. }
  1065. //查询 取车的车位 取车指令 根据车牌号 查询对应的车位
  1066. Error_manager Dispatch_command::query_dispatch_space_for_car_number()
  1067. {
  1068. //查询车位表
  1069. char query_parkspace_sql[1024];
  1070. memset(query_parkspace_sql, 0, 1024);
  1071. int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  1072. sprintf(query_parkspace_sql,"select * from space where car_number = '%s' ",m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  1073. // std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
  1074. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  1075. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  1076. if(ec == SUCCESS)
  1077. {
  1078. if(tp_result == nullptr)
  1079. {
  1080. return DB_RESULT_SET_EMPTY;
  1081. }
  1082. //只取第一条结果, 默认是id最小的,
  1083. if (tp_result->next())
  1084. {
  1085. char buf[1024];
  1086. memset(buf, 0, 1024);
  1087. try
  1088. {
  1089. //解析数据
  1090. m_dispatch_space_info[0].m_id = tp_result->getInt("id");
  1091. m_dispatch_space_info[0].m_serial_id = tp_result->getInt("serial_id");
  1092. m_dispatch_space_info[0].m_table_id = tp_result->getInt("table_id");
  1093. m_dispatch_space_info[0].m_unit = tp_result->getInt("unit");
  1094. m_dispatch_space_info[0].m_floor = tp_result->getInt("floor");
  1095. m_dispatch_space_info[0].m_room_id = tp_result->getInt("room_id");
  1096. m_dispatch_space_info[0].m_height = tp_result->getDouble("height");
  1097. m_dispatch_space_info[0].m_primary_key = tp_result->getString("primary_key");
  1098. m_dispatch_space_info[0].m_car_number = tp_result->getString("car_number");
  1099. m_dispatch_space_info[0].m_statu = tp_result->getInt("statu");
  1100. }
  1101. catch (sql::SQLException &e)
  1102. {
  1103. /* Use what() (derived from std::runtime_error) to fetch the error message */
  1104. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  1105. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1106. }
  1107. catch (std::runtime_error &e)
  1108. {
  1109. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  1110. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1111. }
  1112. }
  1113. else
  1114. {
  1115. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  1116. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  1117. }
  1118. return SUCCESS;
  1119. }
  1120. else
  1121. {
  1122. return ec;
  1123. }
  1124. return SUCCESS;
  1125. }
  1126. //查询 取车的车辆感测信息 取车指令 根据key 查询感测信息
  1127. //Error_manager Dispatch_command::query_dispatch_vehicle_for_primary_key()
  1128. //{
  1129. // //查询 车辆表
  1130. // char query_parkspace_sql[1024];
  1131. // memset(query_parkspace_sql, 0, 1024);
  1132. // int t_statu = 0; //车位状态, 0可用, 1故障
  1133. // sprintf(query_parkspace_sql,"select * from vehicle where primary_key = '%s' ",m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str());
  1134. //// std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
  1135. // boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  1136. // Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  1137. // if(ec == SUCCESS)
  1138. // {
  1139. // if(tp_result == nullptr)
  1140. // {
  1141. // return DB_RESULT_SET_EMPTY;
  1142. // }
  1143. // //只取第一条结果, 默认是id最小的,
  1144. // if (tp_result->next())
  1145. // {
  1146. // char buf[1024];
  1147. // memset(buf, 0, 1024);
  1148. // try
  1149. // {
  1150. // //解析数据
  1151. // m_dispatch_vehicle_info.m_car_number = tp_result->getString("car_number");
  1152. // m_dispatch_vehicle_info.m_primary_key = tp_result->getString("primary_key");
  1153. // m_dispatch_vehicle_info.m_actually_measure_info = tp_result->getString("actually_measure_info");
  1154. //
  1155. // //取车, 数据库特有车位信息
  1156. // if(! google::protobuf::TextFormat::ParseFromString(m_dispatch_vehicle_info.m_actually_measure_info, &m_dispatch_vehicle_info.m_actually_measure_info_msg))
  1157. // {
  1158. // return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  1159. // "ParseFromString fun error, m_space_info ");
  1160. // }
  1161. // }
  1162. // catch (sql::SQLException &e)
  1163. // {
  1164. // /* Use what() (derived from std::runtime_error) to fetch the error message */
  1165. // sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  1166. // return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1167. // }
  1168. // catch (std::runtime_error &e)
  1169. // {
  1170. // sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  1171. // return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1172. // }
  1173. // }
  1174. // else
  1175. // {
  1176. // return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  1177. // "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  1178. //
  1179. // }
  1180. // return SUCCESS;
  1181. // }
  1182. // else
  1183. // {
  1184. // return ec;
  1185. // }
  1186. // return SUCCESS;
  1187. //}
  1188. //更新 车位状态, 根据车位ID 写入车牌号即可
  1189. Error_manager Dispatch_command::update_parkspace_write_car_number()
  1190. {
  1191. //执行sql操作
  1192. char update_space_sql[1024];
  1193. memset(update_space_sql, 0, 1024);
  1194. //int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  1195. sprintf(update_space_sql, "update space set car_number = '%s', primary_key = '%s', statu = 2, space_info = '%s', measure_info = '%s' where id = %d",
  1196. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str(),
  1197. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
  1198. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.DebugString().c_str(),
  1199. m_dispatch_command_map[m_car_number_optimal].m_measure_info_string.c_str(),
  1200. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.id()
  1201. );
  1202. // m_dispatch_space_info.m_id);
  1203. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  1204. return ec;
  1205. }
  1206. //更新 车位状态, 找到车牌号, 写NULL
  1207. Error_manager Dispatch_command::update_parkspace_clear_car_number()
  1208. {
  1209. //执行sql操作
  1210. char update_space_sql[1024];
  1211. memset(update_space_sql, 0, 1024);
  1212. //int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  1213. sprintf(update_space_sql, "update space set car_number = null, primary_key = null, statu = 0, space_info = null, measure_info = null where car_number = '%s' ",
  1214. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  1215. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  1216. return ec;
  1217. }
  1218. //更新 车位状态, 找到车牌号, 写NULL by id
  1219. Error_manager Dispatch_command::update_parkspace_clear_car_number_by_id(int space_id)
  1220. {
  1221. //执行sql操作
  1222. char update_space_sql[1024];
  1223. memset(update_space_sql, 0, 1024);
  1224. //int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  1225. sprintf(update_space_sql, "update space set car_number = null, primary_key = null, statu = 0, space_info = null, measure_info = null where id = %d ",
  1226. space_id);
  1227. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  1228. return ec;
  1229. }
  1230. //更新车牌图片信息,
  1231. Error_manager Dispatch_command::update_parkspace_write_car_number_info()
  1232. {
  1233. //执行sql操作
  1234. char update_space_sql[1024*1024*2];
  1235. memset(update_space_sql, 0, 1024*1024*2);
  1236. //int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  1237. sprintf(update_space_sql, "update space set plate_color = '%s', plate_type = '%s', plate_confidence = %d, recognition_time = '%s', plate_full_image = '%s', plate_clip_image = '%s' where car_number = '%s' ",
  1238. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_color().c_str(),
  1239. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_type().c_str(),
  1240. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_confidence(),
  1241. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.recognition_time().c_str(),
  1242. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_full_image().c_str(),
  1243. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_clip_image().c_str(),
  1244. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str()
  1245. );
  1246. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  1247. return ec;
  1248. }
  1249. //删除车牌图片信息,
  1250. Error_manager Dispatch_command::update_parkspace_clear_car_number_info()
  1251. {
  1252. //执行sql操作
  1253. char update_space_sql[1024];
  1254. memset(update_space_sql, 0, 1024);
  1255. //int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  1256. sprintf(update_space_sql, "update space set plate_color = null, plate_type = null, plate_confidence = null, recognition_time = null, plate_full_image = null, plate_clip_image = null where car_number = '%s' ",
  1257. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str()
  1258. );
  1259. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  1260. return ec;
  1261. }
  1262. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  1263. Error_manager Dispatch_command::update_command_queue_for_statu(int statu)
  1264. {
  1265. char update_command_sql[1024];
  1266. memset(update_command_sql, 0, 1024);
  1267. sprintf(update_command_sql, "update command_queue set statu = %d, export_id = %d, space_info = '%s' where car_number = '%s'",
  1268. statu,
  1269. m_dispatch_command_map[m_car_number_optimal].m_export_id,
  1270. m_dispatch_command_map[m_car_number_optimal].m_space_info_string.c_str(),
  1271. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str()
  1272. );
  1273. // LOG(INFO) << "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj update_command_sql = "<< update_command_sql << " --- " << this;
  1274. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_command_sql);
  1275. return ec;
  1276. }
  1277. //删除 指令队列, 根据车牌号 删除指令
  1278. Error_manager Dispatch_command::delete_command_queue_for_car_number()
  1279. {
  1280. char delete_command_sql[1024];
  1281. memset(delete_command_sql, 0, 1024);
  1282. sprintf(delete_command_sql, "delete from command_queue where car_number = '%s'",
  1283. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  1284. Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_command_sql);
  1285. return ec;
  1286. }
  1287. //制作存车表单
  1288. Error_manager Dispatch_command::create_park_table()
  1289. {
  1290. park_table t_park_table;
  1291. t_park_table.mutable_statu()->set_execute_statu(eNormal);
  1292. t_park_table.set_queue_id(m_dispatch_command_map[m_car_number_optimal].m_queue_id);
  1293. t_park_table.set_car_number(m_dispatch_command_map[m_car_number_optimal].m_car_number);
  1294. t_park_table.set_unit_id(m_dispatch_command_map[m_car_number_optimal].m_unit);
  1295. t_park_table.set_primary_key(m_dispatch_command_map[m_car_number_optimal].m_primary_key);
  1296. t_park_table.mutable_entrance_measure_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg);
  1297. t_park_table.mutable_allocated_space_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_space_info_msg);
  1298. //根据雷达x坐标, 计算入口id
  1299. // int t_terminal = 0;
  1300. // if (m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()<0)
  1301. // {
  1302. // t_terminal = m_dispatch_id*2+1;
  1303. // }
  1304. // else if(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()>0)
  1305. // {
  1306. // t_terminal = m_dispatch_id*2+2;
  1307. // }
  1308. t_park_table.set_terminal_id(m_dispatch_command_map[m_car_number_optimal].m_import_id);
  1309. // std::cout << " huli test :::: " << " iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii m_park_table_msg.entrance_measure_info().cx() = " << m_park_table_msg.entrance_measure_info().cx() << std::endl;
  1310. // std::cout << " huli test :::: " << " iiiiiiiiiiiiiiiiiiiiiiiiiiiiii t_terminal = " << t_terminal << std::endl;
  1311. // LOG(INFO) << " m_park_table_msg.entrance_measure_info().cx() = "<< m_park_table_msg.entrance_measure_info().cx() << " --- " << this;
  1312. // LOG(INFO) << " t_terminal = "<< t_terminal << " --- " << this;
  1313. // LOG(INFO) << " m_device_floor = "<< m_device_floor << " --- " << this;
  1314. // LOG(INFO) << " m_dispatch_id = "<< m_dispatch_id << " --- " << this;
  1315. // LOG(INFO) << " m_unit = "<< m_unit << " --- " << this;
  1316. // LOG(INFO) << " m_outlet_ready = "<< m_outlet_ready << " --- " << this;
  1317. m_park_table_msg = t_park_table;
  1318. return Error_code::SUCCESS;
  1319. }
  1320. //制作取车表单
  1321. Error_manager Dispatch_command::create_pick_table(int outlet_ready)
  1322. {
  1323. pick_table t_pick_table;
  1324. t_pick_table.mutable_statu()->set_execute_statu(eNormal);
  1325. t_pick_table.set_queue_id(m_dispatch_command_map[m_car_number_optimal].m_queue_id);
  1326. t_pick_table.set_car_number(m_dispatch_command_map[m_car_number_optimal].m_car_number);
  1327. t_pick_table.set_unit_id(m_dispatch_command_map[m_car_number_optimal].m_unit);
  1328. t_pick_table.set_primary_key(m_dispatch_command_map[m_car_number_optimal].m_primary_key);
  1329. t_pick_table.mutable_actually_measure_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg);
  1330. t_pick_table.mutable_actually_space_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_space_info_msg);
  1331. t_pick_table.set_terminal_id(outlet_ready);
  1332. // LOG(INFO) << " outlet_ready = "<< outlet_ready << " --- " << this;
  1333. //
  1334. // LOG(INFO) << " m_device_floor = "<< m_device_floor << " --- " << this;
  1335. // LOG(INFO) << " m_dispatch_id = "<< m_dispatch_id << " --- " << this;
  1336. // LOG(INFO) << " m_unit = "<< m_unit << " --- " << this;
  1337. // LOG(INFO) << " m_outlet_ready = "<< m_outlet_ready << " --- " << this;
  1338. m_pick_table_msg = t_pick_table;
  1339. return Error_code::SUCCESS;
  1340. }
  1341. //增加 车辆表, 存车指令 完成后添加车辆信息
  1342. Error_manager Dispatch_command::insert_vehicle_for_car_number()
  1343. {
  1344. char insert_vehicle_sql[1024];
  1345. memset(insert_vehicle_sql, 0, 1024);
  1346. sprintf(insert_vehicle_sql, "INSERT INTO vehicle (car_number,primary_key,actually_measure_info) values ('%s','%s','%s')",
  1347. m_car_number_optimal.c_str(), m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
  1348. m_dispatch_command_map[m_car_number_optimal].m_measure_info_string.c_str() );
  1349. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
  1350. return ec;
  1351. }
  1352. //删除 车辆表, 取车指令 完成后删除车辆信息
  1353. Error_manager Dispatch_command::delete_vehicle_for_car_number()
  1354. {
  1355. char delete_vehicle_sql[1024];
  1356. memset(delete_vehicle_sql, 0, 1024);
  1357. sprintf(delete_vehicle_sql, "delete from vehicle where car_number = '%s'",
  1358. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  1359. Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_vehicle_sql);
  1360. return ec;
  1361. }
  1362. //增加 记录表, 存车指令 完成后添加存车记录
  1363. Error_manager Dispatch_command::insert_record_for_park_start()
  1364. {
  1365. Time_tool::get_instance_references().time_start(PARK_TIME_FLAG);
  1366. int t_terminal = 0;
  1367. #ifdef CHUTIAN_PROJECT_PROJECT
  1368. if (m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()<0)
  1369. {
  1370. t_terminal = m_dispatch_id*2+1;
  1371. }
  1372. else if(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()>0)
  1373. {
  1374. t_terminal = m_dispatch_id*2+2;
  1375. }
  1376. #endif//CHUTIAN_PROJECT_PROJECT
  1377. #ifdef SHANGGUJIE_PROJECT_PROJECT
  1378. t_terminal = m_dispatch_command_map[m_car_number_optimal].m_import_id;
  1379. #endif //SHANGGUJIE_PROJECT_PROJECT
  1380. char insert_record_sql[1024];
  1381. memset(insert_record_sql, 0, 1024);
  1382. // sprintf(insert_record_sql, "INSERT INTO record (primary_key,car_number,in_time_start,measure_info,import_id) values ('%s','%s','%s','%s',%d)",
  1383. 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)",
  1384. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
  1385. m_car_number_optimal.c_str(),
  1386. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1387. // m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.id(),
  1388. // m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.DebugString().c_str(),
  1389. // m_dispatch_command_map[m_car_number_optimal].m_measure_info.c_str(),
  1390. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx(),
  1391. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cy(),
  1392. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.theta(),
  1393. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.width(),
  1394. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height(),
  1395. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.wheelbase(),
  1396. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.front_theta(),
  1397. t_terminal );
  1398. LOG(INFO) << " insert_record_sql = "<< insert_record_sql << " --- " << this;
  1399. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_record_sql);
  1400. return ec;
  1401. }
  1402. //增加 记录表, 存车指令 完成后添加存车记录
  1403. Error_manager Dispatch_command::updata_record_for_park_end()
  1404. {
  1405. Time_tool::get_instance_references().time_end(PARK_TIME_FLAG);
  1406. int in_time_difference = Time_tool::get_instance_references().get_time_seconds(PARK_TIME_FLAG);
  1407. //t_index_id是指定单元的车位id, 1~78
  1408. // int t_table_id = (m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.id() - 1 ) % 78;
  1409. // t_table_id = t_table_id + 1;
  1410. int t_table_id = m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.table_id();
  1411. //执行sql操作
  1412. char update_record_sql[1024];
  1413. memset(update_record_sql, 0, 1024);
  1414. // 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' ",
  1415. 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' ",
  1416. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1417. in_time_difference,
  1418. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.id(),
  1419. t_table_id,
  1420. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.unit_id(),
  1421. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.floor(),
  1422. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.room_id(),
  1423. // m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.DebugString().c_str(),
  1424. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  1425. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  1426. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  1427. return ec;
  1428. }
  1429. //更新 记录表, 取车指令 完成后更新取车记录
  1430. Error_manager Dispatch_command::updata_record_for_pick_start()
  1431. {
  1432. Time_tool::get_instance_references().time_start(PICK_TIME_FLAG);
  1433. //执行sql操作
  1434. char update_record_sql[1024];
  1435. memset(update_record_sql, 0, 1024);
  1436. sprintf(update_record_sql, "update record set out_time_start = '%s', export_id = %d where primary_key = '%s' ",
  1437. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1438. m_dispatch_command_map[m_car_number_optimal].m_export_id,
  1439. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  1440. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  1441. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  1442. return ec;
  1443. }
  1444. //更新 记录表, 取车指令 完成后更新取车记录
  1445. Error_manager Dispatch_command::updata_record_for_pick_end()
  1446. {
  1447. Time_tool::get_instance_references().time_end(PICK_TIME_FLAG);
  1448. int out_time_difference = Time_tool::get_instance_references().get_time_seconds(PICK_TIME_FLAG);
  1449. int parking_time;
  1450. get_parking_time(m_dispatch_command_map[m_car_number_optimal].m_primary_key, parking_time);
  1451. char parking_time_string[256] = "";
  1452. int hour = parking_time / 3600;
  1453. int min = (parking_time / 60)%60;
  1454. int sec = parking_time % 60;
  1455. sprintf(parking_time_string, " %d:%02d:%02d", hour, min, sec);
  1456. //执行sql操作
  1457. char update_record_sql[1024];
  1458. memset(update_record_sql, 0, 1024);
  1459. sprintf(update_record_sql, "update record set out_time_end = '%s', out_time_difference = %d, parking_time = '%s' where primary_key = '%s' ",
  1460. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1461. out_time_difference,
  1462. parking_time_string,
  1463. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  1464. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  1465. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  1466. return ec;
  1467. }
  1468. //更新 记录表, 取车指令 完成后更新取车记录, 撤销指令, 没有取车时间差
  1469. Error_manager Dispatch_command::updata_record_for_pick_end_ex()
  1470. {
  1471. int parking_time;
  1472. get_parking_time(m_dispatch_command_map[m_car_number_optimal].m_primary_key, parking_time);
  1473. char parking_time_string[256] = "";
  1474. int hour = parking_time / 3600;
  1475. int min = parking_time / 60;
  1476. int sec = parking_time % 60;
  1477. sprintf(parking_time_string, " %d:%02d:%02d", hour, min, sec);
  1478. //执行sql操作
  1479. char update_record_sql[1024];
  1480. memset(update_record_sql, 0, 1024);
  1481. sprintf(update_record_sql, "update record set out_time_end = '%s', parking_time = '%s' where primary_key = '%s' ",
  1482. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1483. parking_time_string,
  1484. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  1485. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  1486. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  1487. return ec;
  1488. }
  1489. //计算汽车在车库的存车时间, 从存车开始到取车结束, 返回 parking_time, 单位秒.
  1490. Error_manager Dispatch_command::get_parking_time(std::string primary_key, int & parking_time )
  1491. {
  1492. //查询存车时间 in_time_start
  1493. char query_record_sql[1024];
  1494. memset(query_record_sql, 0, 1024);
  1495. sprintf(query_record_sql,"select * from record where primary_key = '%s' ", primary_key.c_str() );
  1496. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  1497. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_record_sql, tp_result);
  1498. if(ec == SUCCESS)
  1499. {
  1500. if(tp_result == nullptr)
  1501. {
  1502. return DB_RESULT_SET_EMPTY;
  1503. }
  1504. //只取第一条结果, 默认是id最小的,
  1505. if (tp_result->next())
  1506. {
  1507. char buf[1024];
  1508. memset(buf, 0, 1024);
  1509. try
  1510. {
  1511. //解析数据
  1512. std::string in_time_start_string = tp_result->getString("in_time_start");
  1513. std::chrono::system_clock::time_point in_time_start = Time_tool::get_instance_references().transform_time_string_seconds(in_time_start_string);
  1514. std::chrono::system_clock::time_point out_time_end = std::chrono::system_clock::now();
  1515. double t_parking_time = (out_time_end - in_time_start).count() / 1000000000;
  1516. parking_time = t_parking_time;
  1517. return Error_code::SUCCESS;
  1518. }
  1519. catch (sql::SQLException &e)
  1520. {
  1521. /* Use what() (derived from std::runtime_error) to fetch the error message */
  1522. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  1523. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1524. }
  1525. catch (std::runtime_error &e)
  1526. {
  1527. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  1528. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1529. }
  1530. }
  1531. else
  1532. {
  1533. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  1534. "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
  1535. }
  1536. return SUCCESS;
  1537. }
  1538. else
  1539. {
  1540. return ec;
  1541. }
  1542. return Error_code::SUCCESS;
  1543. }
  1544. //获取出入口id 1~2为入口, 3~4为出口,
  1545. //输入terminal_id 1~19, 输入mode 1:存车, 2:取车
  1546. int Dispatch_command::get_passway_id(int terminal_id, int mode)
  1547. {
  1548. int passway_id = 0;
  1549. switch ( terminal_id )
  1550. {
  1551. case 1:
  1552. case 2:
  1553. case 4:
  1554. case 6:
  1555. case 8:
  1556. case 10:
  1557. case 12:
  1558. case 14:
  1559. case 16:
  1560. case 17:
  1561. case 19:
  1562. {
  1563. passway_id = 1 + (mode-1)*2;
  1564. break;
  1565. }
  1566. case 3:
  1567. case 5:
  1568. case 7:
  1569. case 9:
  1570. case 11:
  1571. case 13:
  1572. case 15:
  1573. case 18:
  1574. {
  1575. passway_id = 2 + (mode-1)*2;
  1576. break;
  1577. }
  1578. default:
  1579. {
  1580. break;
  1581. }
  1582. }
  1583. return passway_id;
  1584. }