dispatch_command.cpp 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253
  1. #include "dispatch_command.h"
  2. #include <google/protobuf/text_format.h>
  3. #include "../tool/time_tool.h"
  4. #include "dispatch_manager.h"
  5. Dispatch_command::Dispatch_command()
  6. {
  7. m_dispatch_command_status = E_DISPATCH_COMMAND_UNKNOW;
  8. m_unit = 0;
  9. m_device_floor = 0;
  10. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_TYPE_UNKNOW;
  11. m_car_type = Common_data::Car_type::UNKNOW_CAR_TYPE;
  12. m_dispatch_space_info_count = 0;
  13. // m_wait_park_count = 0;
  14. // m_wait_pick_count = 0;
  15. }
  16. Dispatch_command::~Dispatch_command()
  17. {
  18. }
  19. #ifdef SHANGGUJIE_PROJECT_PROJECT
  20. //存车指令入队, 检查节点直接通过rabbitmq发送给调度, 调度接受后数据库指令入队,
  21. Error_manager Dispatch_command::insert_park_command(park_table park_command)
  22. {
  23. Dispatch_command_info t_dispatch_command_info;
  24. t_dispatch_command_info.m_car_number = park_command.car_number();
  25. t_dispatch_command_info.m_primary_key = park_command.primary_key();
  26. t_dispatch_command_info.m_unit = park_command.unit_id();
  27. t_dispatch_command_info.m_terminal_id = park_command.terminal_id();
  28. //queue_id 当前之间的总秒, (后续排序的权重参数是 搬运器和起点的距离 用户等待时间 ) (当前之间的总秒, 系统时间单位是纳秒, 除以10^9转化为秒)
  29. t_dispatch_command_info.m_queue_id = std::chrono::system_clock::now().time_since_epoch().count()/1000000000;
  30. t_dispatch_command_info.m_type = 1;//1表示存车, 2表示取车
  31. t_dispatch_command_info.m_statu = 0;//0表示排队中, 1表示运行中, 2表示完成, 3表示故障
  32. t_dispatch_command_info.m_import_id = get_passway_id(park_command.terminal_id(), 1);//
  33. t_dispatch_command_info.m_export_id = 0;//出口id, 存车时不写
  34. t_dispatch_command_info.m_height = park_command.entrance_measure_info().height();
  35. t_dispatch_command_info.m_space_info_msg;//入队时不写, 后续运行时补全
  36. t_dispatch_command_info.m_measure_info_msg = park_command.entrance_measure_info();
  37. t_dispatch_command_info.m_car_number_info_msg = park_command.car_number_info();
  38. char insert_command_queue_sql[DB_SQL_SIZE];
  39. memset(insert_command_queue_sql, 0, DB_SQL_SIZE);
  40. 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')",
  41. t_dispatch_command_info.m_car_number.c_str(),
  42. t_dispatch_command_info.m_primary_key.c_str(),
  43. t_dispatch_command_info.m_unit,
  44. t_dispatch_command_info.m_terminal_id,
  45. t_dispatch_command_info.m_queue_id,
  46. t_dispatch_command_info.m_type,
  47. t_dispatch_command_info.m_statu,
  48. t_dispatch_command_info.m_import_id,
  49. t_dispatch_command_info.m_export_id,
  50. t_dispatch_command_info.m_height,
  51. t_dispatch_command_info.m_space_info_msg.DebugString().c_str(),
  52. t_dispatch_command_info.m_measure_info_msg.DebugString().c_str(),
  53. t_dispatch_command_info.m_car_number_info_msg.plate_color().c_str(),
  54. t_dispatch_command_info.m_car_number_info_msg.plate_type().c_str(),
  55. t_dispatch_command_info.m_car_number_info_msg.plate_confidence(),
  56. t_dispatch_command_info.m_car_number_info_msg.recognition_time().c_str(),
  57. t_dispatch_command_info.m_car_number_info_msg.plate_full_image().c_str(),
  58. t_dispatch_command_info.m_car_number_info_msg.plate_clip_image().c_str()
  59. );
  60. LOG(INFO) << " insert_command_queue_sql = "<< insert_command_queue_sql << " " << this;
  61. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_command_queue_sql);
  62. return ec;
  63. return Error_code::SUCCESS;
  64. }
  65. //存车指令入队, 检查节点直接通过rabbitmq发送给调度, 调度接受后数据库指令入队,
  66. Error_manager Dispatch_command::insert_park_command(park_table park_command, Dispatch_command::Dispatch_space_info dispatch_space_info)
  67. {
  68. Dispatch_command_info t_dispatch_command_info;
  69. t_dispatch_command_info.m_car_number = park_command.car_number();
  70. t_dispatch_command_info.m_primary_key = park_command.primary_key();
  71. t_dispatch_command_info.m_unit = park_command.unit_id();
  72. t_dispatch_command_info.m_terminal_id = park_command.terminal_id();
  73. //queue_id 当前之间的总秒, (后续排序的权重参数是 搬运器和起点的距离 用户等待时间 ) (当前之间的总秒, 系统时间单位是纳秒, 除以10^9转化为秒)
  74. t_dispatch_command_info.m_queue_id = std::chrono::system_clock::now().time_since_epoch().count()/1000000000;
  75. t_dispatch_command_info.m_type = 1;//1表示存车, 2表示取车
  76. t_dispatch_command_info.m_statu = 0;//0表示排队中, 1表示运行中, 2表示完成, 3表示故障
  77. t_dispatch_command_info.m_import_id = get_passway_id(park_command.terminal_id(), 1);//
  78. t_dispatch_command_info.m_export_id = 0;//出口id, 存车时不写
  79. t_dispatch_command_info.m_height = park_command.entrance_measure_info().height();
  80. t_dispatch_command_info.m_space_info_msg.set_id(dispatch_space_info.m_id);
  81. t_dispatch_command_info.m_space_info_msg.set_serial_id(dispatch_space_info.m_serial_id);
  82. t_dispatch_command_info.m_space_info_msg.set_table_id(dispatch_space_info.m_table_id);
  83. t_dispatch_command_info.m_space_info_msg.set_unit_id(dispatch_space_info.m_unit);
  84. t_dispatch_command_info.m_space_info_msg.set_floor(dispatch_space_info.m_floor);
  85. t_dispatch_command_info.m_space_info_msg.set_room_id(dispatch_space_info.m_room_id);
  86. t_dispatch_command_info.m_space_info_msg.set_height(dispatch_space_info.m_height);
  87. t_dispatch_command_info.m_measure_info_msg = park_command.entrance_measure_info();
  88. t_dispatch_command_info.m_car_number_info_msg = park_command.car_number_info();
  89. char insert_command_queue_sql[DB_SQL_SIZE];
  90. memset(insert_command_queue_sql, 0, DB_SQL_SIZE);
  91. 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')",
  92. t_dispatch_command_info.m_car_number.c_str(),
  93. t_dispatch_command_info.m_primary_key.c_str(),
  94. t_dispatch_command_info.m_unit,
  95. t_dispatch_command_info.m_terminal_id,
  96. t_dispatch_command_info.m_queue_id,
  97. t_dispatch_command_info.m_type,
  98. t_dispatch_command_info.m_statu,
  99. t_dispatch_command_info.m_import_id,
  100. t_dispatch_command_info.m_export_id,
  101. t_dispatch_command_info.m_height,
  102. t_dispatch_command_info.m_space_info_msg.DebugString().c_str(),
  103. t_dispatch_command_info.m_measure_info_msg.DebugString().c_str(),
  104. t_dispatch_command_info.m_car_number_info_msg.plate_color().c_str(),
  105. t_dispatch_command_info.m_car_number_info_msg.plate_type().c_str(),
  106. t_dispatch_command_info.m_car_number_info_msg.plate_confidence(),
  107. t_dispatch_command_info.m_car_number_info_msg.recognition_time().c_str(),
  108. t_dispatch_command_info.m_car_number_info_msg.plate_full_image().c_str(),
  109. t_dispatch_command_info.m_car_number_info_msg.plate_clip_image().c_str()
  110. );
  111. LOG(INFO) << " insert_command_queue_sql = "<< insert_command_queue_sql << " " << this;
  112. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_command_queue_sql);
  113. return ec;
  114. return Error_code::SUCCESS;
  115. }
  116. //取车指令入队, 检查节点直接通过rabbitmq发送给调度, 调度接受后数据库指令入队,
  117. Error_manager Dispatch_command::insert_pick_command(pick_table pick_command)
  118. {
  119. Error_manager t_error;
  120. Dispatch_command_info t_dispatch_command_info;
  121. //只使用取车表单的唯一码, 其他信息去数据库查.
  122. t_dispatch_command_info.m_primary_key = pick_command.primary_key();
  123. 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);
  124. t_dispatch_command_info.m_unit = t_dispatch_command_info.m_space_info_msg.unit_id();
  125. t_dispatch_command_info.m_terminal_id = pick_command.terminal_id();
  126. //queue_id 当前之间的总秒, (后续排序的权重参数是 搬运器和起点的距离 用户等待时间 ) (当前之间的总秒, 系统时间单位是纳秒, 除以10^9转化为秒)
  127. t_dispatch_command_info.m_queue_id = std::chrono::system_clock::now().time_since_epoch().count()/1000000000;
  128. t_dispatch_command_info.m_type = 2;//1表示存车, 2表示取车
  129. t_dispatch_command_info.m_statu = 0;//0表示排队中, 1表示运行中, 2表示完成, 3表示故障
  130. t_dispatch_command_info.m_import_id = 0;//入口id, 取车时不写
  131. t_dispatch_command_info.m_export_id = 0;//出口id, 入队时不写, 后续运行时补全
  132. t_dispatch_command_info.m_height = t_dispatch_command_info.m_measure_info_msg.height();
  133. char insert_command_queue_sql[DB_SQL_SIZE];
  134. memset(insert_command_queue_sql, 0, DB_SQL_SIZE);
  135. 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')",
  136. t_dispatch_command_info.m_car_number.c_str(),
  137. t_dispatch_command_info.m_primary_key.c_str(),
  138. t_dispatch_command_info.m_unit,
  139. t_dispatch_command_info.m_terminal_id,
  140. t_dispatch_command_info.m_queue_id,
  141. t_dispatch_command_info.m_type,
  142. t_dispatch_command_info.m_statu,
  143. t_dispatch_command_info.m_import_id,
  144. t_dispatch_command_info.m_export_id,
  145. t_dispatch_command_info.m_height,
  146. t_dispatch_command_info.m_space_info_msg.DebugString().c_str(),
  147. t_dispatch_command_info.m_measure_info_msg.DebugString().c_str()
  148. );
  149. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_command_queue_sql);
  150. return ec;
  151. return Error_code::SUCCESS;
  152. }
  153. //取车指令入队, 检查节点直接通过rabbitmq发送给调度, 调度接受后数据库指令入队,
  154. Error_manager Dispatch_command::insert_pick_command(pick_table pick_command, Dispatch_command::Dispatch_space_info dispatch_space_info)
  155. {
  156. Error_manager t_error;
  157. Dispatch_command_info t_dispatch_command_info;
  158. //只使用取车表单的唯一码, 其他信息去数据库查.
  159. t_dispatch_command_info.m_primary_key = pick_command.primary_key();
  160. 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);
  161. t_dispatch_command_info.m_unit = t_dispatch_command_info.m_space_info_msg.unit_id();
  162. t_dispatch_command_info.m_terminal_id = pick_command.terminal_id();
  163. //queue_id 当前之间的总秒, (后续排序的权重参数是 搬运器和起点的距离 用户等待时间 ) (当前之间的总秒, 系统时间单位是纳秒, 除以10^9转化为秒)
  164. t_dispatch_command_info.m_queue_id = std::chrono::system_clock::now().time_since_epoch().count()/1000000000;
  165. t_dispatch_command_info.m_type = 2;//1表示存车, 2表示取车
  166. t_dispatch_command_info.m_statu = 0;//0表示排队中, 1表示运行中, 2表示完成, 3表示故障
  167. t_dispatch_command_info.m_import_id = 0;//入口id, 取车时不写
  168. t_dispatch_command_info.m_export_id = 0;//出口id, 入队时不写, 后续运行时补全
  169. t_dispatch_command_info.m_height = t_dispatch_command_info.m_measure_info_msg.height();
  170. t_dispatch_command_info.m_space_info_msg.set_id(dispatch_space_info.m_id);
  171. t_dispatch_command_info.m_space_info_msg.set_serial_id(dispatch_space_info.m_serial_id);
  172. t_dispatch_command_info.m_space_info_msg.set_table_id(dispatch_space_info.m_table_id);
  173. t_dispatch_command_info.m_space_info_msg.set_unit_id(dispatch_space_info.m_unit);
  174. t_dispatch_command_info.m_space_info_msg.set_floor(dispatch_space_info.m_floor);
  175. t_dispatch_command_info.m_space_info_msg.set_room_id(dispatch_space_info.m_room_id);
  176. t_dispatch_command_info.m_space_info_msg.set_height(dispatch_space_info.m_height);
  177. char insert_command_queue_sql[DB_SQL_SIZE];
  178. memset(insert_command_queue_sql, 0, DB_SQL_SIZE);
  179. 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')",
  180. t_dispatch_command_info.m_car_number.c_str(),
  181. t_dispatch_command_info.m_primary_key.c_str(),
  182. t_dispatch_command_info.m_unit,
  183. t_dispatch_command_info.m_terminal_id,
  184. t_dispatch_command_info.m_queue_id,
  185. t_dispatch_command_info.m_type,
  186. t_dispatch_command_info.m_statu,
  187. t_dispatch_command_info.m_import_id,
  188. t_dispatch_command_info.m_export_id,
  189. t_dispatch_command_info.m_height,
  190. t_dispatch_command_info.m_space_info_msg.DebugString().c_str(),
  191. t_dispatch_command_info.m_measure_info_msg.DebugString().c_str()
  192. );
  193. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_command_queue_sql);
  194. return ec;
  195. return Error_code::SUCCESS;
  196. }
  197. #endif//SHANGGUJIE_PROJECT_PROJECT
  198. //调度开始前, 向数据库发送请求的相关操作, 输入 穿梭机所在的楼层, 调度id 0~2, 空闲出口id, 如果不是0,就表示有空闲的出口
  199. Error_manager Dispatch_command::dispatch_request_to_sql(int device_floor, int dispatch_id, int outlet_ready)
  200. {
  201. std::unique_lock<std::mutex> t_lock(m_lock);
  202. Error_manager t_error;
  203. m_device_floor = device_floor;
  204. m_dispatch_id = dispatch_id;
  205. m_unit = dispatch_id;
  206. m_outlet_ready = outlet_ready;
  207. //获取调度指令, 与数据库同步 command_queue
  208. t_error = query_all_dispatch_command(0);
  209. if ( t_error != Error_code::SUCCESS )
  210. {
  211. return t_error;
  212. }
  213. //排序
  214. if ( outlet_ready == 0 )
  215. {
  216. //对调度指令进行排序, 选出最优解, 只比较存车
  217. t_error = sort_dispatch_command_for_park();
  218. }
  219. else
  220. {
  221. //对调度指令进行排序, 选出最优解, 比较存车和取车
  222. t_error = sort_dispatch_command_for_total();
  223. }
  224. if ( t_error != Error_code::SUCCESS )
  225. {
  226. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_TYPE_UNKNOW;
  227. //没有找到合适的指令就返回NODATA, 然后设么也不做
  228. return t_error;
  229. }
  230. else
  231. {
  232. if ( m_dispatch_command_map[m_car_number_optimal].m_type == 1 )//存车
  233. {
  234. //注注注注注意了
  235. //20230621, 排队存车进入指令表, 提前预约车位,提前写入指令表,车位表,记录表
  236. //判断车位是否提前预约,
  237. if ( m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.id() == 0 )
  238. {
  239. LOG(INFO) << " congxinfeipei================================================== congxinfeipei "<< this;
  240. LOG(INFO) << " m_dispatch_command_map[m_car_number_optimal].m_space_info_msg " << m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.DebugString() << " " << this;
  241. //如果没有预约, 那么再次分配一次。
  242. //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解 m_dispatch_space_info
  243. t_error = query_dispatch_space_optimal(m_dispatch_command_map[m_car_number_optimal].m_unit,
  244. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height(),
  245. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.wheelbase(),
  246. m_car_type,
  247. m_dispatch_space_info[0]);
  248. if ( t_error != Error_code::SUCCESS )
  249. {
  250. update_command_queue_for_statu(3);
  251. return t_error;
  252. }
  253. else
  254. {
  255. //将最优解车位信息 补充到指令表
  256. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_id(m_dispatch_space_info[0].m_id);
  257. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_serial_id(m_dispatch_space_info[0].m_serial_id);
  258. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_table_id(m_dispatch_space_info[0].m_table_id);
  259. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_unit_id(m_dispatch_space_info[0].m_unit);
  260. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_floor(m_dispatch_space_info[0].m_floor);
  261. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_room_id(m_dispatch_space_info[0].m_room_id);
  262. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_height(m_dispatch_space_info[0].m_height);
  263. 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();
  264. }
  265. }
  266. //完善数据库
  267. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_STORE;
  268. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  269. t_error = update_command_queue_for_statu(1);
  270. //更新 车位状态, 根据车位ID 写入车牌号即可
  271. t_error = update_parkspace_write_car_number();
  272. //制作存车表单
  273. t_error = create_park_table();
  274. t_error = check_record_for_no_primary_key(m_dispatch_command_map[m_car_number_optimal].m_primary_key);
  275. if ( t_error == SUCCESS )
  276. {
  277. //增加 记录表, 存车指令 完成后添加存车记录
  278. t_error = insert_record_for_park_start();
  279. }
  280. else
  281. {
  282. //更新 记录表, 存车指令 完成后只更新存车开始时间
  283. t_error = updata_record_for_park_start();
  284. }
  285. LOG(INFO) << " create_park_table 创建存车任务单 = "<< m_park_table_msg.DebugString() << " --- " << this;
  286. return Error_code::SUCCESS;
  287. }
  288. else if ( m_dispatch_command_map[m_car_number_optimal].m_type == 2 )//取车
  289. {
  290. //注注注注注意了
  291. //取车自带车位信息,没有感测信息, 需要查询车辆表,补全车辆信息
  292. //查询 取车的车辆感测信息 取车指令 根据key 查询感测信息
  293. // t_error = query_dispatch_vehicle_for_primary_key();
  294. if ( t_error != Error_code::SUCCESS )
  295. {
  296. update_command_queue_for_statu(3);
  297. return t_error;
  298. }
  299. else
  300. {
  301. //补充出口id
  302. m_dispatch_command_map[m_car_number_optimal].m_export_id = outlet_ready;
  303. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_PICKUP;
  304. // m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg = m_dispatch_vehicle_info.m_actually_measure_info_msg;
  305. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  306. t_error = update_command_queue_for_statu(1);
  307. //制作取车表单
  308. t_error = create_pick_table(outlet_ready);
  309. //更新 记录表, 取车指令 完成后更新取车记录
  310. t_error = updata_record_for_pick_start();
  311. LOG(INFO) << " create_pick_table 创建取车车任务单 = "<< m_pick_table_msg.DebugString() << " --- " << this;
  312. }
  313. }
  314. else
  315. {
  316. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  317. " fun error ");
  318. }
  319. }
  320. return Error_code::SUCCESS;
  321. }
  322. //调度完成后, 向数据库发送答复的相关操作
  323. Error_manager Dispatch_command::dispatch_response_to_sql(Error_manager dispatch_result)
  324. {
  325. std::unique_lock<std::mutex> t_lock(m_lock);
  326. Error_manager t_error;
  327. if ( dispatch_result == Error_code::SUCCESS )
  328. {
  329. //调度成功, 完善数据库
  330. if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
  331. {
  332. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_STORE 存车答复成功 = "<< t_error << " --- " << this;
  333. //增加 车辆表, 存车指令 完成后添加车辆信息
  334. // t_error = insert_vehicle_for_car_number();
  335. //更新车牌图片信息,
  336. update_parkspace_write_car_number_info();
  337. //更新 指令队列, 根据车牌号 删除指令
  338. t_error = delete_command_queue_for_car_number();
  339. //增加 记录表, 存车指令 完成后添加存车记录
  340. t_error = updata_record_for_park_end();
  341. }
  342. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
  343. {
  344. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_PICKUP 取车答复成功 = "<< t_error << " --- " << this;
  345. //删除 车辆表, 取车指令 完成后删除车辆信息
  346. // t_error = delete_vehicle_for_car_number();
  347. //删除车牌图片信息,
  348. t_error = update_parkspace_clear_car_number_info();
  349. //更新 车位状态, 找到车牌号, 写NULL
  350. t_error = update_parkspace_clear_car_number();
  351. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  352. t_error = update_command_queue_for_statu(2);
  353. //更新 记录表, 取车指令 完成后更新取车记录
  354. t_error = updata_record_for_pick_end();
  355. }
  356. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
  357. {
  358. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_REALLOCATE 改道答复成功 = "<< t_error << " --- " << this;
  359. //增加 车辆表, 存车指令 完成后添加车辆信息
  360. // t_error = insert_vehicle_for_car_number();
  361. //如果多次重新分配, 需要把之前申请的车位清除,
  362. for (int i = 0; i < m_dispatch_space_info_count; ++i)
  363. {
  364. //如果多次重新分配, 需要把之前申请的车位清除 //更新 车位状态, 找到车牌号, 写NULL
  365. t_error = update_parkspace_clear_car_number_by_id(m_dispatch_space_info[i].m_id, 10);
  366. }
  367. m_dispatch_space_info_count = 0;
  368. //更新车牌图片信息,
  369. update_parkspace_write_car_number_info();
  370. //更新 指令队列, 根据车牌号 删除指令
  371. t_error = delete_command_queue_for_car_number();
  372. //增加 记录表, 存车指令 完成后添加存车记录
  373. t_error = updata_record_for_park_end();
  374. }
  375. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
  376. {
  377. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_REVOCATION 撤销答复成功 = "<< t_error << " --- " << this;
  378. //删除 车辆表, 取车指令 完成后删除车辆信息
  379. // t_error = delete_vehicle_for_car_number();
  380. //如果多次重新分配, 需要把之前申请的车位清除,
  381. for (int i = 0; i < m_dispatch_space_info_count; ++i)
  382. {
  383. //如果多次重新分配, 需要把之前申请的车位清除 //更新 车位状态, 找到车牌号, 写NULL
  384. t_error = update_parkspace_clear_car_number_by_id(m_dispatch_space_info[i].m_id, 10);
  385. }
  386. m_dispatch_space_info_count = 0;
  387. //删除车牌图片信息,
  388. t_error = update_parkspace_clear_car_number_info();
  389. //更新 车位状态, 找到车牌号, 写NULL
  390. t_error = update_parkspace_clear_car_number();
  391. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  392. t_error = update_command_queue_for_statu(2);
  393. // LOG(INFO) << "llllllllllllllllllllllllllllllllllllllllll respons = "<< t_error << " --- " << this;
  394. //更新 记录表, 取车指令 完成后更新取车记录, 撤销指令, 没有取车时间差
  395. t_error = updata_record_for_pick_end_ex();
  396. }
  397. else
  398. {
  399. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  400. " fun error ");
  401. }
  402. return t_error;
  403. }
  404. else
  405. {
  406. //调度失败, 回退 数据库
  407. if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
  408. {
  409. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_STORE 存车答复失败 = "<< t_error << " --- " << this;
  410. //删除车牌图片信息,
  411. update_parkspace_clear_car_number_info();
  412. //更新 车位状态, 找到车牌号, 写NULL
  413. update_parkspace_clear_car_number();
  414. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  415. update_command_queue_for_statu(3);
  416. }
  417. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
  418. {
  419. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_PICKUP 取车答复失败 = "<< t_error << " --- " << this;
  420. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  421. update_command_queue_for_statu(3);
  422. }
  423. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
  424. {
  425. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_STORE 改道答复失败 = "<< t_error << " --- " << this;
  426. //如果多次重新分配, 需要把之前申请的车位清除,
  427. for (int i = 0; i < m_dispatch_space_info_count; ++i)
  428. {
  429. //如果多次重新分配, 需要把之前申请的车位清除 //更新 车位状态, 找到车牌号, 写NULL
  430. t_error = update_parkspace_clear_car_number_by_id(m_dispatch_space_info[i].m_id, 10);
  431. }
  432. m_dispatch_space_info_count = 0;
  433. //删除车牌图片信息,
  434. update_parkspace_clear_car_number_info();
  435. //更新 车位状态, 找到车牌号, 写NULL
  436. update_parkspace_clear_car_number();
  437. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  438. update_command_queue_for_statu(3);
  439. }
  440. else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
  441. {
  442. LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_PICKUP 撤销答复失败 = "<< t_error << " --- " << this;
  443. //如果多次重新分配, 需要把之前申请的车位清除,
  444. for (int i = 0; i < m_dispatch_space_info_count; ++i)
  445. {
  446. //如果多次重新分配, 需要把之前申请的车位清除 //更新 车位状态, 找到车牌号, 写NULL
  447. t_error = update_parkspace_clear_car_number_by_id(m_dispatch_space_info[i].m_id, 10);
  448. }
  449. m_dispatch_space_info_count = 0;
  450. //删除车牌图片信息,
  451. update_parkspace_clear_car_number_info();
  452. //更新 车位状态, 找到车牌号, 写NULL
  453. update_parkspace_clear_car_number();
  454. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  455. update_command_queue_for_statu(3);
  456. }
  457. else
  458. {
  459. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  460. " fun error ");
  461. }
  462. }
  463. return Error_code::SUCCESS;
  464. }
  465. //调度 , 向数据库 重新分配车位,
  466. Error_manager Dispatch_command::dispatch_reallocate_to_sql(Common_data::Car_type reallocate_car_type, int outlet_ready)
  467. {
  468. std::unique_lock<std::mutex> t_lock(m_lock);
  469. Error_manager t_error;
  470. Error_manager t_error2;
  471. m_outlet_ready = outlet_ready;
  472. //重新申请车位
  473. //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解
  474. m_dispatch_space_info_count = m_dispatch_space_info_count +1;
  475. t_error = query_dispatch_space_optimal_ex(reallocate_car_type, m_dispatch_space_info[m_dispatch_space_info_count]);
  476. if ( t_error != Error_code::SUCCESS )
  477. {
  478. //没找到车位, 就把车放到出口
  479. if ( outlet_ready != 0 )
  480. {
  481. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_REVOCATION;
  482. //补充出口id
  483. m_dispatch_command_map[m_car_number_optimal].m_export_id = outlet_ready;
  484. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_id(0);
  485. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_serial_id(0);
  486. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_table_id(0);
  487. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_unit_id(0);
  488. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_floor(0);
  489. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_room_id(0);
  490. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.set_height(0);
  491. 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();
  492. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  493. // t_error = update_command_queue_for_statu(1);
  494. //制作取车表单
  495. t_error = create_pick_table(outlet_ready);
  496. LOG(INFO) << " create_pick_table 撤销存车, 取车到出口任务单 = "<< m_pick_table_msg.DebugString() << " --- " << this;
  497. return t_error;
  498. }
  499. else
  500. {
  501. //无限等待, 等待出口空闲
  502. return Error_code::NODATA;
  503. }
  504. }
  505. else
  506. {
  507. m_dispatch_process_type = Common_data::DISPATCH_PROCESS_REALLOCATE;
  508. //找到新的车位, 重新执行任务
  509. 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);
  510. 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);
  511. 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);
  512. 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);
  513. 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);
  514. 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);
  515. 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);
  516. 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();
  517. //更新 车位状态, 根据车位ID 写入车牌号即可
  518. t_error = update_parkspace_write_car_number();
  519. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  520. // t_error = update_command_queue_for_statu(1);
  521. //制作存车表单
  522. // LOG(INFO) << " zxcxzczxczxczxczxczxczxczxczxczxcxzcx = "<< m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.id() << " --- " << this;
  523. t_error = create_park_table();
  524. LOG(INFO) << " create_park_table 改道存车, 存车新车位任务单 = "<< m_park_table_msg.DebugString() << " --- " << this;
  525. }
  526. return Error_code::SUCCESS;
  527. }
  528. //检查出口是否空闲, 检查指令队列的取车完成的出口id是否存在, 不存在就是空闲,返回成功
  529. Error_manager Dispatch_command::check_export_id_is_ready(int export_id)
  530. {
  531. std::unique_lock<std::mutex> t_lock(m_lock);
  532. //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
  533. char check_export_id_sql[DB_SQL_SIZE];
  534. memset(check_export_id_sql, 0, DB_SQL_SIZE);
  535. sprintf(check_export_id_sql,"select * from command_queue where statu = 2 and type = 2 and export_id = %d",export_id);
  536. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  537. Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
  538. if(t_error == Error_code::SUCCESS)
  539. {
  540. if (tp_result == nullptr)
  541. {
  542. return DB_RESULT_SET_EMPTY;
  543. }
  544. //如果存在, 就报错, 不存在就是空闲,返回成功
  545. if ( tp_result->next() )
  546. {
  547. return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
  548. " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
  549. }
  550. else
  551. {
  552. return Error_code::SUCCESS;
  553. }
  554. }
  555. else
  556. {
  557. return t_error;
  558. }
  559. return Error_code::SUCCESS;
  560. }
  561. //检查出口取车是否完成, 指定的出口有取车完成指令就返回成功, 否则报错.
  562. Error_manager Dispatch_command::check_pickup_is_finish(int unit_id, int export_id)
  563. {
  564. std::unique_lock<std::mutex> t_lock(m_lock);
  565. //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
  566. char check_export_id_sql[DB_SQL_SIZE];
  567. memset(check_export_id_sql, 0, DB_SQL_SIZE);
  568. sprintf(check_export_id_sql,"select * from command_queue where statu = 2 and type = 2 and unit = %d and export_id = %d",unit_id, export_id);
  569. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  570. Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
  571. if(t_error == Error_code::SUCCESS)
  572. {
  573. if (tp_result == nullptr)
  574. {
  575. return DB_RESULT_SET_EMPTY;
  576. }
  577. //如果存在, 就报错, 不存在就是空闲,返回成功
  578. if ( tp_result->next() )
  579. {
  580. return Error_code::SUCCESS;
  581. }
  582. else
  583. {
  584. return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
  585. " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
  586. }
  587. }
  588. else
  589. {
  590. return t_error;
  591. }
  592. return Error_code::SUCCESS;
  593. }
  594. //检查入口存车是否开始, 指定的入口有存车开始指令就返回成功, 否则报错.
  595. Error_manager Dispatch_command::check_park_is_start(int import_id)
  596. {
  597. std::unique_lock<std::mutex> t_lock(m_lock);
  598. //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
  599. char check_export_id_sql[DB_SQL_SIZE];
  600. memset(check_export_id_sql, 0, DB_SQL_SIZE);
  601. sprintf(check_export_id_sql,"select * from command_queue where type = 1 and import_id = %d and (statu=0 or statu =1)",import_id);
  602. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  603. Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
  604. if(t_error == Error_code::SUCCESS)
  605. {
  606. if (tp_result == nullptr)
  607. {
  608. return DB_RESULT_SET_EMPTY;
  609. }
  610. //如果存在, 就报错, 不存在就是空闲,返回成功
  611. if ( tp_result->next() )
  612. {
  613. return Error_code::SUCCESS;
  614. }
  615. else
  616. {
  617. return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
  618. " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
  619. }
  620. }
  621. else
  622. {
  623. return t_error;
  624. }
  625. return Error_code::SUCCESS;
  626. }
  627. //获取指定入口的 存车流程的 唯一码,
  628. Error_manager Dispatch_command::get_primary_key_for_store(int import_id, std::string & primary_key )
  629. {
  630. std::unique_lock<std::mutex> t_lock(m_lock);
  631. //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
  632. char check_export_id_sql[DB_SQL_SIZE];
  633. memset(check_export_id_sql, 0, DB_SQL_SIZE);
  634. sprintf(check_export_id_sql,"select * from command_queue where type = 1 and import_id = %d and (statu=0 or statu =1)",import_id);
  635. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  636. Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
  637. if(t_error == Error_code::SUCCESS)
  638. {
  639. if (tp_result == nullptr)
  640. {
  641. return DB_RESULT_SET_EMPTY;
  642. }
  643. //如果存在, 就报错, 不存在就是空闲,返回成功
  644. if ( tp_result->next() )
  645. {
  646. char buf[DB_SQL_SIZE];
  647. memset(buf, 0, DB_SQL_SIZE);
  648. try
  649. {
  650. //解析数据
  651. primary_key = tp_result->getString("primary_key");
  652. }
  653. catch (sql::SQLException &e)
  654. {
  655. /* Use what() (derived from std::runtime_error) to fetch the error message */
  656. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  657. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  658. }
  659. catch (std::runtime_error &e)
  660. {
  661. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  662. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  663. }
  664. return Error_code::SUCCESS;
  665. }
  666. else
  667. {
  668. return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
  669. " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
  670. }
  671. }
  672. else
  673. {
  674. return t_error;
  675. }
  676. return Error_code::SUCCESS;
  677. }
  678. //删除 指令队列, 根据出口编号
  679. Error_manager Dispatch_command::delete_command_queue_for_export_id(int unit_id, int export_id)
  680. {
  681. std::unique_lock<std::mutex> t_lock(m_lock);
  682. char delete_command_sql[DB_SQL_SIZE];
  683. memset(delete_command_sql, 0, DB_SQL_SIZE);
  684. sprintf(delete_command_sql, "delete from command_queue where statu = 2 and type = 2 and unit = %d and export_id = %d ",
  685. unit_id, export_id );
  686. Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_command_sql);
  687. return ec;
  688. }
  689. //添加plc数据储存
  690. Error_manager Dispatch_command::insert_plc_data(std::string plc_data, float hearbeat)
  691. {
  692. std::unique_lock<std::mutex> t_lock(m_lock);
  693. char insert_vehicle_sql[4096];
  694. memset(insert_vehicle_sql, 0, 4096);
  695. sprintf(insert_vehicle_sql, "INSERT INTO plc_data (data, hearbeat) values ('%s',%f)",
  696. plc_data.c_str(), hearbeat );
  697. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
  698. std::cout << " huli test :::: " << " ec = " << ec << std::endl;
  699. return ec;
  700. }
  701. //查询 取车的车位 取车指令 根据车牌号 查询对应的车位
  702. Error_manager Dispatch_command::query_dispatch_space_for_car_number(std::string car_number, parkspace_info & parkspace_info)
  703. {
  704. //查询车位表
  705. char query_parkspace_sql[DB_SQL_SIZE];
  706. memset(query_parkspace_sql, 0, DB_SQL_SIZE);
  707. int t_statu = 0; //车位状态, 0可用, 1故障
  708. sprintf(query_parkspace_sql,"select * from space where car_number = '%s' ",car_number.c_str());
  709. // std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
  710. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  711. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  712. if(ec == SUCCESS)
  713. {
  714. if(tp_result == nullptr)
  715. {
  716. return DB_RESULT_SET_EMPTY;
  717. }
  718. //只取第一条结果, 默认是id最小的,
  719. if (tp_result->next())
  720. {
  721. char buf[DB_SQL_SIZE];
  722. memset(buf, 0, DB_SQL_SIZE);
  723. try
  724. {
  725. //解析数据
  726. parkspace_info.set_id(tp_result->getInt("id"));
  727. parkspace_info.set_serial_id(tp_result->getInt("serial_id"));
  728. parkspace_info.set_table_id( tp_result->getInt("table_id"));
  729. parkspace_info.set_unit_id( tp_result->getInt("unit"));
  730. parkspace_info.set_floor( tp_result->getInt("floor"));
  731. parkspace_info.set_room_id( tp_result->getInt("room_id"));
  732. parkspace_info.set_height(tp_result->getDouble("height"));
  733. }
  734. catch (sql::SQLException &e)
  735. {
  736. /* Use what() (derived from std::runtime_error) to fetch the error message */
  737. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  738. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  739. }
  740. catch (std::runtime_error &e)
  741. {
  742. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  743. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  744. }
  745. }
  746. else
  747. {
  748. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  749. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  750. }
  751. return SUCCESS;
  752. }
  753. else
  754. {
  755. return ec;
  756. }
  757. return SUCCESS;
  758. }
  759. //查询 取车的车位 取车指令 根据车牌号 查询对应的车位
  760. 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)
  761. {
  762. //查询车位表
  763. char query_parkspace_sql[DB_SQL_SIZE];
  764. memset(query_parkspace_sql, 0, DB_SQL_SIZE);
  765. int t_statu = 0; //车位状态, 0可用, 1故障
  766. sprintf(query_parkspace_sql,"select * from space where primary_key = '%s' ",primary_key.c_str());
  767. // std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
  768. // LOG(INFO) << " ++++++++++++++++++++++++++++++++++++ query_parkspace_sql = "<< query_parkspace_sql << " --- " << this;
  769. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  770. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  771. if(ec == SUCCESS)
  772. {
  773. if(tp_result == nullptr)
  774. {
  775. return DB_RESULT_SET_EMPTY;
  776. }
  777. //只取第一条结果, 默认是id最小的,
  778. if (tp_result->next())
  779. {
  780. char buf[DB_SQL_SIZE];
  781. memset(buf, 0, DB_SQL_SIZE);
  782. try
  783. {
  784. //解析数据
  785. car_number = tp_result->getString("car_number");
  786. std::string parkspace_info_string = tp_result->getString("space_info");
  787. if(! google::protobuf::TextFormat::ParseFromString(parkspace_info_string, &parkspace_info))
  788. {
  789. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  790. "ParseFromString fun error, m_space_info ");
  791. }
  792. std::string measure_info_string = tp_result->getString("measure_info");
  793. if(! google::protobuf::TextFormat::ParseFromString(measure_info_string, &measure_info))
  794. {
  795. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  796. "ParseFromString fun error, m_space_info ");
  797. }
  798. }
  799. catch (sql::SQLException &e)
  800. {
  801. /* Use what() (derived from std::runtime_error) to fetch the error message */
  802. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  803. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  804. }
  805. catch (std::runtime_error &e)
  806. {
  807. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  808. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  809. }
  810. }
  811. else
  812. {
  813. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  814. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  815. }
  816. return SUCCESS;
  817. }
  818. else
  819. {
  820. return ec;
  821. }
  822. return SUCCESS;
  823. }
  824. //查询 取车的车辆感测信息 取车指令 根据key 查询感测信息
  825. Error_manager Dispatch_command::query_dispatch_vehicle_for_primary_key(std::string primary_key, measure_info & measure_info)
  826. {
  827. //查询 车辆表
  828. char query_parkspace_sql[DB_SQL_SIZE];
  829. memset(query_parkspace_sql, 0, DB_SQL_SIZE);
  830. int t_statu = 0; //车位状态, 0可用, 1故障
  831. sprintf(query_parkspace_sql,"select * from vehicle where primary_key = '%s' ",primary_key.c_str());
  832. // std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
  833. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  834. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  835. if(ec == SUCCESS)
  836. {
  837. if(tp_result == nullptr)
  838. {
  839. return DB_RESULT_SET_EMPTY;
  840. }
  841. //只取第一条结果, 默认是id最小的,
  842. if (tp_result->next())
  843. {
  844. char buf[DB_SQL_SIZE];
  845. memset(buf, 0, DB_SQL_SIZE);
  846. try
  847. {
  848. //解析数据
  849. std::string measure_info_string = tp_result->getString("actually_measure_info");
  850. //取车, 数据库特有车位信息
  851. if(! google::protobuf::TextFormat::ParseFromString(measure_info_string, &measure_info))
  852. {
  853. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  854. "ParseFromString fun error, m_space_info ");
  855. }
  856. }
  857. catch (sql::SQLException &e)
  858. {
  859. /* Use what() (derived from std::runtime_error) to fetch the error message */
  860. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  861. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  862. }
  863. catch (std::runtime_error &e)
  864. {
  865. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  866. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  867. }
  868. }
  869. else
  870. {
  871. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  872. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  873. }
  874. return SUCCESS;
  875. }
  876. else
  877. {
  878. return ec;
  879. }
  880. return SUCCESS;
  881. }
  882. //获取调度指令, 与数据库同步 command_queue, statu指令状态, 0:排队等待, 1正在运行, 2:已经完成
  883. Error_manager Dispatch_command::query_all_dispatch_command(int statu)
  884. {
  885. //从command_queue获取所有排队的指令,
  886. char query_all_dispatch_command_sql[DB_SQL_SIZE];
  887. memset(query_all_dispatch_command_sql, 0, DB_SQL_SIZE);
  888. sprintf(query_all_dispatch_command_sql,"select * from command_queue where statu = %d and unit = %d",statu, m_unit);
  889. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  890. Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(query_all_dispatch_command_sql, tp_result);
  891. m_dispatch_command_map.clear();
  892. unsigned short t_wait_small_park_count = 0; //排队小车存车的数量
  893. unsigned short t_wait_middle_park_count = 0; //排队中车存车的数量
  894. unsigned short t_wait_big_park_count = 0; //排队大车存车的数量
  895. unsigned short t_wait_total_park_count = 0; //排队总车存车的数量
  896. unsigned short t_wait_small_pick_count = 0; //排队小车存车的数量
  897. unsigned short t_wait_middle_pick_count = 0; //排队中车存车的数量
  898. unsigned short t_wait_big_pick_count = 0; //排队大车存车的数量
  899. unsigned short t_wait_total_pick_count = 0; //排队总车存车的数量
  900. if(t_error == Error_code::SUCCESS)
  901. {
  902. if (tp_result == nullptr)
  903. {
  904. return DB_RESULT_SET_EMPTY;
  905. }
  906. //循环检查所有行
  907. while (tp_result->next())
  908. {
  909. Dispatch_command_info t_dispatch_command_info;
  910. char t_error_buf[DB_SQL_SIZE];
  911. memset(t_error_buf, 0, DB_SQL_SIZE);
  912. try
  913. {
  914. //解析数据
  915. t_dispatch_command_info.m_car_number = tp_result->getString("car_number");
  916. t_dispatch_command_info.m_primary_key = tp_result->getString("primary_key");
  917. t_dispatch_command_info.m_unit = tp_result->getInt("unit");
  918. t_dispatch_command_info.m_terminal_id = tp_result->getInt("terminal_id");
  919. t_dispatch_command_info.m_queue_id = tp_result->getInt("queue_id");
  920. t_dispatch_command_info.m_type = tp_result->getInt("type");
  921. t_dispatch_command_info.m_statu = tp_result->getInt("statu");
  922. t_dispatch_command_info.m_export_id = tp_result->getInt("export_id");
  923. t_dispatch_command_info.m_import_id = tp_result->getInt("import_id");
  924. t_dispatch_command_info.m_height = tp_result->getDouble("height");
  925. t_dispatch_command_info.m_space_info_string = tp_result->getString("space_info");
  926. t_dispatch_command_info.m_measure_info_string = tp_result->getString("measure_info");
  927. t_dispatch_command_info.m_car_number_info_msg.set_plate_color(tp_result->getString("plate_color") );
  928. t_dispatch_command_info.m_car_number_info_msg.set_plate_type(tp_result->getString("plate_type") );
  929. t_dispatch_command_info.m_car_number_info_msg.set_plate_confidence(tp_result->getInt("plate_confidence") );
  930. t_dispatch_command_info.m_car_number_info_msg.set_recognition_time(tp_result->getString("recognition_time") );
  931. t_dispatch_command_info.m_car_number_info_msg.set_plate_full_image(tp_result->getString("plate_full_image") );
  932. t_dispatch_command_info.m_car_number_info_msg.set_plate_clip_image(tp_result->getString("plate_clip_image") );
  933. t_dispatch_command_info.m_car_number_info_string = t_dispatch_command_info.m_car_number_info_msg.DebugString();
  934. //存车, 数据库特有 雷达信息, 没有车位信息, 需要根据车高,再去车位表申请车位
  935. if(! google::protobuf::TextFormat::ParseFromString(t_dispatch_command_info.m_measure_info_string, &t_dispatch_command_info.m_measure_info_msg))
  936. {
  937. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  938. "ParseFromString fun error, m_measure_info ");
  939. }
  940. //取车, 数据库特有车位信息
  941. if(! google::protobuf::TextFormat::ParseFromString(t_dispatch_command_info.m_space_info_string, &t_dispatch_command_info.m_space_info_msg))
  942. {
  943. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  944. "ParseFromString fun error, m_space_info ");
  945. }
  946. //取车, 数据库特有 车牌图片信息
  947. // if(! google::protobuf::TextFormat::ParseFromString(t_dispatch_command_info.m_car_number_info_string, &t_dispatch_command_info.m_car_number_info_msg))
  948. // {
  949. // return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  950. // "ParseFromString fun error, m_space_info ");
  951. // }
  952. // if(! t_dispatch_command_info.m_car_number_info_msg.ParseFromString(t_dispatch_command_info.m_car_number_info_string) )
  953. // {
  954. // return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  955. // "ParseFromString fun error, m_space_info ");
  956. // }
  957. //m_type指令类型, 0无效, 1存车, 2取车,
  958. if ( t_dispatch_command_info.m_type == 1)
  959. {
  960. t_dispatch_command_info.m_useless_distance = m_device_floor-1;
  961. }
  962. else if ( t_dispatch_command_info.m_type == 2 )
  963. {
  964. t_dispatch_command_info.m_useless_distance = t_dispatch_command_info.m_space_info_msg.floor()-m_device_floor;
  965. }
  966. else
  967. {
  968. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  969. "command_queue m_type error ");
  970. }
  971. if ( t_dispatch_command_info.m_useless_distance <0 )
  972. {
  973. t_dispatch_command_info.m_useless_distance = 0-t_dispatch_command_info.m_useless_distance;
  974. }
  975. m_dispatch_command_map[t_dispatch_command_info.m_car_number] = t_dispatch_command_info;
  976. //huli 20230612, 统计排队信息
  977. if ( t_dispatch_command_info.m_type == 1 )
  978. {
  979. if ( t_dispatch_command_info.m_height >= 1.48-0.001 && t_dispatch_command_info.m_height <= 1.48+0.001)
  980. {
  981. t_wait_small_park_count++;
  982. }
  983. if ( t_dispatch_command_info.m_height >= 1.50-0.001 && t_dispatch_command_info.m_height <= 1.50+0.001)
  984. {
  985. t_wait_middle_park_count++;
  986. }
  987. if ( t_dispatch_command_info.m_height >= 2.05-0.001 && t_dispatch_command_info.m_height <= 2.05+0.001)
  988. {
  989. t_wait_big_park_count++;
  990. }
  991. }
  992. if ( t_dispatch_command_info.m_type == 2 )
  993. {
  994. if ( t_dispatch_command_info.m_height >= 1.48-0.001 && t_dispatch_command_info.m_height <= 1.48+0.001)
  995. {
  996. t_wait_small_pick_count++;
  997. }
  998. if ( t_dispatch_command_info.m_height >= 1.50-0.001 && t_dispatch_command_info.m_height <= 1.50+0.001)
  999. {
  1000. t_wait_middle_pick_count++;
  1001. }
  1002. if ( t_dispatch_command_info.m_height >= 2.05-0.001 && t_dispatch_command_info.m_height <= 2.05+0.001)
  1003. {
  1004. t_wait_big_pick_count++;
  1005. }
  1006. }
  1007. }
  1008. catch (sql::SQLException &e)
  1009. {
  1010. /* Use what() (derived from std::runtime_error) to fetch the error message */
  1011. sprintf(t_error_buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(),
  1012. e.getSQLState().c_str());
  1013. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, t_error_buf);
  1014. }
  1015. catch (std::runtime_error &e)
  1016. {
  1017. sprintf(t_error_buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  1018. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, t_error_buf);
  1019. }
  1020. }
  1021. Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_small_park_count = t_wait_small_park_count;
  1022. Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_middle_park_count = t_wait_middle_park_count;
  1023. Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_big_park_count = t_wait_big_park_count;
  1024. Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_total_park_count = t_wait_small_park_count + t_wait_middle_park_count + t_wait_big_park_count;
  1025. Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_small_pick_count = t_wait_small_pick_count;
  1026. Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_middle_pick_count = t_wait_middle_pick_count;
  1027. Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_big_pick_count = t_wait_big_pick_count;
  1028. Dispatch_manager::get_instance_references().m_dispatch_plc.m_dispatch_plc_status_from_manager_to_plc.m_wait_total_pick_count = t_wait_small_pick_count + t_wait_middle_pick_count + t_wait_big_pick_count;
  1029. return SUCCESS;
  1030. }
  1031. else
  1032. {
  1033. return t_error;
  1034. }
  1035. return Error_code::SUCCESS;
  1036. }
  1037. //对调度指令进行排序, 选出最优解, 比较存车和取车
  1038. Error_manager Dispatch_command::sort_dispatch_command_for_total()
  1039. {
  1040. int t_optimal_loss=0x7fffffff; //最优loss值
  1041. m_car_number_optimal.clear();
  1042. //筛选最优任务
  1043. for (auto iter = m_dispatch_command_map.begin(); iter != m_dispatch_command_map.end(); ++iter)
  1044. {
  1045. //loss = 空跑路程 + 排队编号, 求最小值
  1046. int t_current_loss = iter->second.m_useless_distance * SORT_LOSS_RATIO + iter->second.m_queue_id;
  1047. if ( Dispatch_manager::get_instance_references().m_dispatch_manager_status_last == Dispatch_manager::E_DISPATCH_MANAGER_STORE)
  1048. {
  1049. if ( t_optimal_loss > t_current_loss && iter->second.m_type == 2)
  1050. {
  1051. t_optimal_loss = t_current_loss;
  1052. m_car_number_optimal = iter->first;
  1053. // std::cout << " huli test :::: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << " find park = " << 1 << std::endl;
  1054. }
  1055. }
  1056. else
  1057. {
  1058. if ( t_optimal_loss > t_current_loss && iter->second.m_type == 1)
  1059. {
  1060. t_optimal_loss = t_current_loss;
  1061. m_car_number_optimal = iter->first;
  1062. // std::cout << " huli test :::: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << " find pick = " << 2 << std::endl;
  1063. }
  1064. }
  1065. }
  1066. std::cout << " huli test :::: " << " sort_dispatch_command_for_total = " << Dispatch_manager::get_instance_references().m_dispatch_manager_status_last << std::endl;
  1067. if ( m_car_number_optimal.empty() )
  1068. {
  1069. //没找到也要切换上一次的存取车状态.
  1070. if ( Dispatch_manager::get_instance_references().m_dispatch_manager_status_last == Dispatch_manager::E_DISPATCH_MANAGER_STORE )
  1071. {
  1072. Dispatch_manager::get_instance_references().m_dispatch_manager_status_last = Dispatch_manager::E_DISPATCH_MANAGER_PICKUP;
  1073. }
  1074. else
  1075. {
  1076. Dispatch_manager::get_instance_references().m_dispatch_manager_status_last = Dispatch_manager::E_DISPATCH_MANAGER_STORE;
  1077. }
  1078. //没找到合适的, 返回无效数据
  1079. return Error_code::NODATA;
  1080. }
  1081. else
  1082. {
  1083. if ( m_dispatch_command_map[m_car_number_optimal].m_type == 1 )
  1084. {
  1085. Dispatch_manager::get_instance_references().m_dispatch_manager_status_last = Dispatch_manager::E_DISPATCH_MANAGER_STORE;
  1086. }
  1087. else
  1088. {
  1089. Dispatch_manager::get_instance_references().m_dispatch_manager_status_last = Dispatch_manager::E_DISPATCH_MANAGER_PICKUP;
  1090. }
  1091. return Error_code::SUCCESS;
  1092. }
  1093. return Error_code::SUCCESS;
  1094. }
  1095. //对调度指令进行排序, 选出最优解, 只比较存车
  1096. Error_manager Dispatch_command::sort_dispatch_command_for_park()
  1097. {
  1098. int t_optimal_loss=0x7fffffff; //最优loss值
  1099. m_car_number_optimal.clear();
  1100. for (auto iter = m_dispatch_command_map.begin(); iter != m_dispatch_command_map.end(); ++iter)
  1101. {
  1102. //loss = 空跑路程 + 排队编号, 求最小值
  1103. int t_current_loss = iter->second.m_useless_distance * SORT_LOSS_RATIO + iter->second.m_queue_id;
  1104. if ( t_optimal_loss > t_current_loss && iter->second.m_type == 1)
  1105. {
  1106. t_optimal_loss = t_current_loss;
  1107. m_car_number_optimal = iter->first;
  1108. }
  1109. }
  1110. std::cout << " huli test :::: " << " sort_dispatch_command_for_park = " << Dispatch_manager::get_instance_references().m_dispatch_manager_status_last << std::endl;
  1111. if ( m_car_number_optimal.empty() )
  1112. {
  1113. return Error_code::NODATA;
  1114. }
  1115. return Error_code::SUCCESS;
  1116. }
  1117. //存车指令 获取 指定车高 指定单元 的车位信息,用于车位分配
  1118. Error_manager Dispatch_command::query_specify_height_unit_parkspace_info(int unit, float height, float wheel_base, Dispatch_space_info & dispatch_space_info)
  1119. {
  1120. //查询车位表
  1121. char query_parkspace_sql[DB_SQL_SIZE];
  1122. memset(query_parkspace_sql, 0, DB_SQL_SIZE);
  1123. int t_statu = 0; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  1124. sprintf(query_parkspace_sql,"select * from space where car_number is NULL and ABS(height- %f) < 1e-5 and wheel_base < %f + 0.0001 and statu = %d and unit = %d",height,wheel_base,t_statu,unit);
  1125. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  1126. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  1127. // std::cout << " huli test :::: " << " query_parkspace_sql = " << query_parkspace_sql << std::endl;
  1128. LOG(INFO) << " sssssssssssssssssssssssssssssssssss " << query_parkspace_sql << " "<< this;
  1129. if(ec == SUCCESS)
  1130. {
  1131. if(tp_result == nullptr)
  1132. {
  1133. return DB_RESULT_SET_EMPTY;
  1134. }
  1135. //只取第一条结果, 默认是id最小的,
  1136. if (tp_result->next())
  1137. {
  1138. char buf[DB_SQL_SIZE];
  1139. memset(buf, 0, DB_SQL_SIZE);
  1140. try
  1141. {
  1142. //解析数据
  1143. dispatch_space_info.m_id = tp_result->getInt("id");
  1144. dispatch_space_info.m_serial_id = tp_result->getInt("serial_id");
  1145. dispatch_space_info.m_table_id = tp_result->getInt("table_id");
  1146. dispatch_space_info.m_unit = tp_result->getInt("unit");
  1147. dispatch_space_info.m_floor = tp_result->getInt("floor");
  1148. dispatch_space_info.m_room_id = tp_result->getInt("room_id");
  1149. dispatch_space_info.m_height = tp_result->getDouble("height");
  1150. // dispatch_space_info.m_primary_key = tp_result->getString("primary_key");
  1151. // dispatch_space_info.m_car_number = tp_result->getString("car_number");
  1152. dispatch_space_info.m_statu = tp_result->getInt("statu");
  1153. // dispatch_space_info.m_space_info_string = tp_result->getString("space_info");
  1154. // dispatch_space_info.m_measure_info_string = tp_result->getString("measure_info");
  1155. // //存车, 数据库特有 雷达信息, 没有车位信息, 需要根据车高,再去车位表申请车位
  1156. // if(! google::protobuf::TextFormat::ParseFromString(dispatch_space_info.m_measure_info_string, &dispatch_space_info.m_measure_info_msg))
  1157. // {
  1158. // return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  1159. // "ParseFromString fun error, m_measure_info ");
  1160. // }
  1161. // //取车, 数据库特有车位信息
  1162. // if(! google::protobuf::TextFormat::ParseFromString(dispatch_space_info.m_space_info_string, &dispatch_space_info.m_space_info_msg))
  1163. // {
  1164. // return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  1165. // "ParseFromString fun error, m_space_info ");
  1166. // }
  1167. }
  1168. catch (sql::SQLException &e)
  1169. {
  1170. /* Use what() (derived from std::runtime_error) to fetch the error message */
  1171. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  1172. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1173. }
  1174. catch (std::runtime_error &e)
  1175. {
  1176. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  1177. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1178. }
  1179. }
  1180. else
  1181. {
  1182. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  1183. "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
  1184. }
  1185. return SUCCESS;
  1186. }
  1187. else
  1188. {
  1189. return ec;
  1190. }
  1191. return SUCCESS;
  1192. }
  1193. //获取 指定车高 指定单元 的空闲车位数量,
  1194. Error_manager Dispatch_command::query_specify_height_unit_parkspace_info(int unit, float height, float wheel_base, int & dispatch_space_count)
  1195. {
  1196. //查询车位表
  1197. char query_parkspace_sql[DB_SQL_SIZE];
  1198. memset(query_parkspace_sql, 0, DB_SQL_SIZE);
  1199. int t_statu = 0; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  1200. sprintf(query_parkspace_sql,"select count(*) from (select * from space where car_number is NULL and ABS(height- %f) < 1e-5 and statu = %d and unit = %d) as tablename2",height,t_statu,unit);
  1201. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  1202. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  1203. // std::cout << " huli test :::: " << " query_parkspace_sql = " << query_parkspace_sql << std::endl;
  1204. if(ec == SUCCESS)
  1205. {
  1206. if(tp_result == nullptr)
  1207. {
  1208. return DB_RESULT_SET_EMPTY;
  1209. }
  1210. //只取第一条结果, 默认是id最小的,
  1211. if (tp_result->next())
  1212. {
  1213. char buf[DB_SQL_SIZE];
  1214. memset(buf, 0, DB_SQL_SIZE);
  1215. try
  1216. {
  1217. //解析数据
  1218. dispatch_space_count = tp_result->getInt("count(*)");
  1219. }
  1220. catch (sql::SQLException &e)
  1221. {
  1222. /* Use what() (derived from std::runtime_error) to fetch the error message */
  1223. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  1224. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1225. }
  1226. catch (std::runtime_error &e)
  1227. {
  1228. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  1229. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1230. }
  1231. }
  1232. else
  1233. {
  1234. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  1235. "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
  1236. }
  1237. return SUCCESS;
  1238. }
  1239. else
  1240. {
  1241. return ec;
  1242. }
  1243. return SUCCESS;
  1244. }
  1245. //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解
  1246. Error_manager Dispatch_command::query_dispatch_space_optimal(int unit, float height, float wheel_base, Common_data::Car_type & car_type, Dispatch_space_info & dispatch_space_info)
  1247. {
  1248. Error_manager t_error;
  1249. int t_unit = unit;
  1250. float t_height = height;
  1251. //小车对应小车位 中车对应中车位 大车对应大车位
  1252. if ( 0 < t_height && t_height <= CAR_HEIGHT_LIMIT_SMALL +0.000001)
  1253. {
  1254. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_SMALL, CAR_WHEEL_BASE_LIMIT_SHORT, dispatch_space_info);
  1255. if ( t_error == Error_code::SUCCESS )
  1256. {
  1257. car_type = Common_data::Car_type::MIN_SHORT_CAR;
  1258. return t_error;
  1259. }
  1260. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_SMALL, CAR_WHEEL_BASE_LIMIT_LONG, dispatch_space_info);
  1261. if ( t_error == Error_code::SUCCESS )
  1262. {
  1263. car_type = Common_data::Car_type::MIN_LONG_CAR;
  1264. return t_error;
  1265. }
  1266. }
  1267. if ( t_height <= CAR_HEIGHT_LIMIT_MIDDLE +0.000001)
  1268. {
  1269. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_MIDDLE, CAR_WHEEL_BASE_LIMIT_SHORT, dispatch_space_info);
  1270. if ( t_error == Error_code::SUCCESS )
  1271. {
  1272. car_type = Common_data::Car_type::MID_SHORT_CAR;
  1273. return t_error;
  1274. }
  1275. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_MIDDLE, CAR_WHEEL_BASE_LIMIT_LONG, dispatch_space_info);
  1276. if ( t_error == Error_code::SUCCESS )
  1277. {
  1278. car_type = Common_data::Car_type::MID_LONG_CAR;
  1279. return t_error;
  1280. }
  1281. }
  1282. if ( t_height <= CAR_HEIGHT_LIMIT_BIG +0.000001)
  1283. {
  1284. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_BIG, CAR_WHEEL_BASE_LIMIT_SHORT, dispatch_space_info);
  1285. if ( t_error == Error_code::SUCCESS )
  1286. {
  1287. car_type = Common_data::Car_type::BIG_SHORT_CAR;
  1288. return t_error;
  1289. }
  1290. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_BIG, CAR_WHEEL_BASE_LIMIT_LONG, dispatch_space_info);
  1291. if ( t_error == Error_code::SUCCESS )
  1292. {
  1293. car_type = Common_data::Car_type::BIG_LONG_CAR;
  1294. return t_error;
  1295. }
  1296. }
  1297. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  1298. " query_dispatch_space_optimal error ");
  1299. }
  1300. //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解
  1301. Error_manager Dispatch_command::query_dispatch_space_optimal_ex(Common_data::Car_type reallocate_car_type, Dispatch_space_info & dispatch_space_info)
  1302. {
  1303. Error_manager t_error;
  1304. int t_unit = m_dispatch_command_map[m_car_number_optimal].m_unit;
  1305. // float t_height = m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height();
  1306. //小车对应小车位 中车对应中车位 大车对应大车位
  1307. if ( reallocate_car_type == Common_data::Car_type::MIN_SHORT_CAR )
  1308. {
  1309. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_SMALL, CAR_WHEEL_BASE_LIMIT_SHORT,
  1310. dispatch_space_info);
  1311. if (t_error == Error_code::SUCCESS)
  1312. {
  1313. m_car_type = Common_data::Car_type::MIN_SHORT_CAR;
  1314. return t_error;
  1315. }
  1316. }
  1317. if ( reallocate_car_type == Common_data::Car_type::MIN_SHORT_CAR ||
  1318. reallocate_car_type == Common_data::Car_type::MIN_LONG_CAR )
  1319. {
  1320. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_SMALL, CAR_WHEEL_BASE_LIMIT_LONG, dispatch_space_info);
  1321. if ( t_error == Error_code::SUCCESS )
  1322. {
  1323. m_car_type = Common_data::Car_type::MIN_LONG_CAR;
  1324. return t_error;
  1325. }
  1326. }
  1327. if ( reallocate_car_type == Common_data::Car_type::MIN_SHORT_CAR ||
  1328. reallocate_car_type == Common_data::Car_type::MID_SHORT_CAR )
  1329. {
  1330. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_MIDDLE, CAR_WHEEL_BASE_LIMIT_SHORT,
  1331. dispatch_space_info);
  1332. if (t_error == Error_code::SUCCESS)
  1333. {
  1334. m_car_type = Common_data::Car_type::MID_SHORT_CAR;
  1335. return t_error;
  1336. }
  1337. }
  1338. if ( reallocate_car_type == Common_data::Car_type::MIN_SHORT_CAR ||
  1339. reallocate_car_type == Common_data::Car_type::MID_SHORT_CAR ||
  1340. reallocate_car_type == Common_data::Car_type::MIN_LONG_CAR ||
  1341. reallocate_car_type == Common_data::Car_type::MID_LONG_CAR)
  1342. {
  1343. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_MIDDLE, CAR_WHEEL_BASE_LIMIT_LONG, dispatch_space_info);
  1344. if ( t_error == Error_code::SUCCESS )
  1345. {
  1346. m_car_type = Common_data::Car_type::MID_LONG_CAR;
  1347. return t_error;
  1348. }
  1349. }
  1350. if ( reallocate_car_type == Common_data::Car_type::MIN_SHORT_CAR ||
  1351. reallocate_car_type == Common_data::Car_type::MID_SHORT_CAR ||
  1352. reallocate_car_type == Common_data::Car_type::BIG_SHORT_CAR )
  1353. {
  1354. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_BIG, CAR_WHEEL_BASE_LIMIT_SHORT,
  1355. dispatch_space_info);
  1356. if (t_error == Error_code::SUCCESS)
  1357. {
  1358. m_car_type = Common_data::Car_type::BIG_SHORT_CAR;
  1359. return t_error;
  1360. }
  1361. }
  1362. if ( reallocate_car_type == Common_data::Car_type::MIN_SHORT_CAR ||
  1363. reallocate_car_type == Common_data::Car_type::MID_SHORT_CAR ||
  1364. reallocate_car_type == Common_data::Car_type::BIG_SHORT_CAR ||
  1365. reallocate_car_type == Common_data::Car_type::MIN_LONG_CAR ||
  1366. reallocate_car_type == Common_data::Car_type::MID_LONG_CAR ||
  1367. reallocate_car_type == Common_data::Car_type::BIG_LONG_CAR)
  1368. {
  1369. t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_BIG, CAR_WHEEL_BASE_LIMIT_LONG, dispatch_space_info);
  1370. if ( t_error == Error_code::SUCCESS )
  1371. {
  1372. m_car_type = Common_data::Car_type::BIG_LONG_CAR;
  1373. return t_error;
  1374. }
  1375. }
  1376. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  1377. " query_dispatch_space_optimal error ");
  1378. }
  1379. //查询 取车的车位 取车指令 根据车牌号 查询对应的车位
  1380. Error_manager Dispatch_command::query_dispatch_space_for_car_number()
  1381. {
  1382. //查询车位表
  1383. char query_parkspace_sql[DB_SQL_SIZE];
  1384. memset(query_parkspace_sql, 0, DB_SQL_SIZE);
  1385. int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  1386. sprintf(query_parkspace_sql,"select * from space where car_number = '%s' ",m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  1387. // std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
  1388. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  1389. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  1390. if(ec == SUCCESS)
  1391. {
  1392. if(tp_result == nullptr)
  1393. {
  1394. return DB_RESULT_SET_EMPTY;
  1395. }
  1396. //只取第一条结果, 默认是id最小的,
  1397. if (tp_result->next())
  1398. {
  1399. char buf[DB_SQL_SIZE];
  1400. memset(buf, 0, DB_SQL_SIZE);
  1401. try
  1402. {
  1403. //解析数据
  1404. m_dispatch_space_info[0].m_id = tp_result->getInt("id");
  1405. m_dispatch_space_info[0].m_serial_id = tp_result->getInt("serial_id");
  1406. m_dispatch_space_info[0].m_table_id = tp_result->getInt("table_id");
  1407. m_dispatch_space_info[0].m_unit = tp_result->getInt("unit");
  1408. m_dispatch_space_info[0].m_floor = tp_result->getInt("floor");
  1409. m_dispatch_space_info[0].m_room_id = tp_result->getInt("room_id");
  1410. m_dispatch_space_info[0].m_height = tp_result->getDouble("height");
  1411. m_dispatch_space_info[0].m_primary_key = tp_result->getString("primary_key");
  1412. m_dispatch_space_info[0].m_car_number = tp_result->getString("car_number");
  1413. m_dispatch_space_info[0].m_statu = tp_result->getInt("statu");
  1414. }
  1415. catch (sql::SQLException &e)
  1416. {
  1417. /* Use what() (derived from std::runtime_error) to fetch the error message */
  1418. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  1419. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1420. }
  1421. catch (std::runtime_error &e)
  1422. {
  1423. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  1424. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1425. }
  1426. }
  1427. else
  1428. {
  1429. return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  1430. "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  1431. }
  1432. return SUCCESS;
  1433. }
  1434. else
  1435. {
  1436. return ec;
  1437. }
  1438. return SUCCESS;
  1439. }
  1440. //查询 取车的车辆感测信息 取车指令 根据key 查询感测信息
  1441. //Error_manager Dispatch_command::query_dispatch_vehicle_for_primary_key()
  1442. //{
  1443. // //查询 车辆表
  1444. // char query_parkspace_sql[DB_SQL_SIZE];
  1445. // memset(query_parkspace_sql, 0, DB_SQL_SIZE);
  1446. // int t_statu = 0; //车位状态, 0可用, 1故障
  1447. // sprintf(query_parkspace_sql,"select * from vehicle where primary_key = '%s' ",m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str());
  1448. //// std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
  1449. // boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  1450. // Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
  1451. // if(ec == SUCCESS)
  1452. // {
  1453. // if(tp_result == nullptr)
  1454. // {
  1455. // return DB_RESULT_SET_EMPTY;
  1456. // }
  1457. // //只取第一条结果, 默认是id最小的,
  1458. // if (tp_result->next())
  1459. // {
  1460. // char buf[DB_SQL_SIZE];
  1461. // memset(buf, 0, DB_SQL_SIZE);
  1462. // try
  1463. // {
  1464. // //解析数据
  1465. // m_dispatch_vehicle_info.m_car_number = tp_result->getString("car_number");
  1466. // m_dispatch_vehicle_info.m_primary_key = tp_result->getString("primary_key");
  1467. // m_dispatch_vehicle_info.m_actually_measure_info = tp_result->getString("actually_measure_info");
  1468. //
  1469. // //取车, 数据库特有车位信息
  1470. // if(! google::protobuf::TextFormat::ParseFromString(m_dispatch_vehicle_info.m_actually_measure_info, &m_dispatch_vehicle_info.m_actually_measure_info_msg))
  1471. // {
  1472. // return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  1473. // "ParseFromString fun error, m_space_info ");
  1474. // }
  1475. // }
  1476. // catch (sql::SQLException &e)
  1477. // {
  1478. // /* Use what() (derived from std::runtime_error) to fetch the error message */
  1479. // sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  1480. // return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1481. // }
  1482. // catch (std::runtime_error &e)
  1483. // {
  1484. // sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  1485. // return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  1486. // }
  1487. // }
  1488. // else
  1489. // {
  1490. // return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
  1491. // "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
  1492. //
  1493. // }
  1494. // return SUCCESS;
  1495. // }
  1496. // else
  1497. // {
  1498. // return ec;
  1499. // }
  1500. // return SUCCESS;
  1501. //}
  1502. //更新 车位状态, 根据车位ID 写入车牌号即可
  1503. Error_manager Dispatch_command::update_parkspace_write_car_number()
  1504. {
  1505. //执行sql操作
  1506. char update_space_sql[DB_SQL_SIZE];
  1507. memset(update_space_sql, 0, DB_SQL_SIZE);
  1508. //int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  1509. sprintf(update_space_sql, "update space set car_number = '%s', primary_key = '%s', statu = 2, space_info = '%s', measure_info = '%s' where id = %d",
  1510. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str(),
  1511. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
  1512. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.DebugString().c_str(),
  1513. m_dispatch_command_map[m_car_number_optimal].m_measure_info_string.c_str(),
  1514. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.id()
  1515. );
  1516. LOG(INFO) << " update_space_sql = "<< update_space_sql << " --- " << this;
  1517. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  1518. return ec;
  1519. }
  1520. //更新 车位状态, 存车排队预约车位
  1521. Error_manager Dispatch_command::update_parkspace_write_car_number(park_table park_command, Dispatch_command::Dispatch_space_info dispatch_space_info)
  1522. {
  1523. parkspace_info t_space_info_msg;
  1524. t_space_info_msg.set_id(dispatch_space_info.m_id);
  1525. t_space_info_msg.set_serial_id(dispatch_space_info.m_serial_id);
  1526. t_space_info_msg.set_table_id(dispatch_space_info.m_table_id);
  1527. t_space_info_msg.set_unit_id(dispatch_space_info.m_unit);
  1528. t_space_info_msg.set_floor(dispatch_space_info.m_floor);
  1529. t_space_info_msg.set_room_id(dispatch_space_info.m_room_id);
  1530. t_space_info_msg.set_height(dispatch_space_info.m_height);
  1531. //执行sql操作
  1532. char update_space_sql[DB_SQL_SIZE];
  1533. memset(update_space_sql, 0, DB_SQL_SIZE);
  1534. //int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  1535. sprintf(update_space_sql, "update space set car_number = '%s', primary_key = '%s', statu = 1, space_info = '%s', measure_info = '%s',plate_color = '%s', plate_type = '%s', plate_confidence = %d, recognition_time = '%s', plate_full_image = '%s', plate_clip_image = '%s' where id = %d",
  1536. park_command.car_number().c_str(),
  1537. park_command.primary_key().c_str(),
  1538. t_space_info_msg.DebugString().c_str(),
  1539. park_command.entrance_measure_info().DebugString().c_str(),
  1540. park_command.car_number_info().plate_color().c_str(),
  1541. park_command.car_number_info().plate_type().c_str(),
  1542. park_command.car_number_info().plate_confidence(),
  1543. park_command.car_number_info().recognition_time().c_str(),
  1544. park_command.car_number_info().plate_full_image().c_str(),
  1545. park_command.car_number_info().plate_clip_image().c_str(),
  1546. t_space_info_msg.id()
  1547. );
  1548. LOG(INFO) << " update_space_sql = "<< update_space_sql << " --- " << this;
  1549. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  1550. return ec;
  1551. }
  1552. //更新 车位状态, 找到车牌号, 写NULL
  1553. Error_manager Dispatch_command::update_parkspace_clear_car_number()
  1554. {
  1555. //执行sql操作
  1556. char update_space_sql[DB_SQL_SIZE];
  1557. memset(update_space_sql, 0, DB_SQL_SIZE);
  1558. //int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  1559. 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' ",
  1560. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  1561. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  1562. return ec;
  1563. }
  1564. //更新 车位状态, 找到车牌号, 写NULL by id
  1565. Error_manager Dispatch_command::update_parkspace_clear_car_number_by_id(int space_id, int statu)
  1566. {
  1567. //执行sql操作
  1568. char update_space_sql[DB_SQL_SIZE];
  1569. memset(update_space_sql, 0, DB_SQL_SIZE);
  1570. //int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  1571. sprintf(update_space_sql, "update space set car_number = null, primary_key = null, statu = %d, space_info = null, measure_info = null where id = %d ",
  1572. statu, space_id);
  1573. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  1574. return ec;
  1575. }
  1576. //更新车牌图片信息,
  1577. Error_manager Dispatch_command::update_parkspace_write_car_number_info()
  1578. {
  1579. //执行sql操作
  1580. char update_space_sql[DB_SQL_SIZE];
  1581. memset(update_space_sql, 0, DB_SQL_SIZE);
  1582. //int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  1583. 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' ",
  1584. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_color().c_str(),
  1585. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_type().c_str(),
  1586. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_confidence(),
  1587. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.recognition_time().c_str(),
  1588. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_full_image().c_str(),
  1589. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_clip_image().c_str(),
  1590. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str()
  1591. );
  1592. LOG(INFO) << " huli 123123 update_parkspace_write_car_number_info = " << update_space_sql << " "<< this;
  1593. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  1594. return ec;
  1595. }
  1596. //删除车牌图片信息,
  1597. Error_manager Dispatch_command::update_parkspace_clear_car_number_info()
  1598. {
  1599. //执行sql操作
  1600. char update_space_sql[DB_SQL_SIZE];
  1601. memset(update_space_sql, 0, DB_SQL_SIZE);
  1602. //int t_statu = 2; //车位状态, 0空闲, 1预约, 2占有, 3锁定, 6入口, 7出口, 10故障
  1603. 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' ",
  1604. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str()
  1605. );
  1606. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
  1607. return ec;
  1608. }
  1609. //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
  1610. Error_manager Dispatch_command::update_command_queue_for_statu(int statu)
  1611. {
  1612. char update_command_sql[DB_SQL_SIZE];
  1613. memset(update_command_sql, 0, DB_SQL_SIZE);
  1614. sprintf(update_command_sql, "update command_queue set statu = %d, export_id = %d, space_info = '%s' where car_number = '%s'",
  1615. statu,
  1616. m_dispatch_command_map[m_car_number_optimal].m_export_id,
  1617. m_dispatch_command_map[m_car_number_optimal].m_space_info_string.c_str(),
  1618. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str()
  1619. );
  1620. // LOG(INFO) << "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj update_command_sql = "<< update_command_sql << " --- " << this;
  1621. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_command_sql);
  1622. return ec;
  1623. }
  1624. //把整个单元的排队指令改为故障
  1625. Error_manager Dispatch_command::update_command_queue_from_wait_to_fault_for_unit(int unit_id)
  1626. {
  1627. char update_command_sql[DB_SQL_SIZE];
  1628. memset(update_command_sql, 0, DB_SQL_SIZE);
  1629. sprintf(update_command_sql, "update command_queue set statu = 3 where statu = 0 and unit = %d",
  1630. unit_id
  1631. );
  1632. // LOG(INFO) << "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj update_command_sql = "<< update_command_sql << " --- " << this;
  1633. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_command_sql);
  1634. return ec;
  1635. }
  1636. //删除 指令队列, 根据车牌号 删除指令
  1637. Error_manager Dispatch_command::delete_command_queue_for_car_number()
  1638. {
  1639. char delete_command_sql[DB_SQL_SIZE];
  1640. memset(delete_command_sql, 0, DB_SQL_SIZE);
  1641. sprintf(delete_command_sql, "delete from command_queue where car_number = '%s'",
  1642. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  1643. LOG(INFO) << " huli 456456 delete_command_queue_for_car_number = " << delete_command_sql << " "<< this;
  1644. Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_command_sql);
  1645. return ec;
  1646. }
  1647. //制作存车表单
  1648. Error_manager Dispatch_command::create_park_table()
  1649. {
  1650. park_table t_park_table;
  1651. t_park_table.mutable_statu()->set_execute_statu(eNormal);
  1652. t_park_table.set_queue_id(m_dispatch_command_map[m_car_number_optimal].m_queue_id);
  1653. t_park_table.set_car_number(m_dispatch_command_map[m_car_number_optimal].m_car_number);
  1654. t_park_table.set_unit_id(m_dispatch_command_map[m_car_number_optimal].m_unit);
  1655. t_park_table.set_primary_key(m_dispatch_command_map[m_car_number_optimal].m_primary_key);
  1656. t_park_table.mutable_entrance_measure_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg);
  1657. t_park_table.mutable_allocated_space_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_space_info_msg);
  1658. //根据雷达x坐标, 计算入口id
  1659. // int t_terminal = 0;
  1660. // if (m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()<0)
  1661. // {
  1662. // t_terminal = m_dispatch_id*2+1;
  1663. // }
  1664. // else if(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()>0)
  1665. // {
  1666. // t_terminal = m_dispatch_id*2+2;
  1667. // }
  1668. t_park_table.set_terminal_id(m_dispatch_command_map[m_car_number_optimal].m_import_id);
  1669. // std::cout << " huli test :::: " << " iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii m_park_table_msg.entrance_measure_info().cx() = " << m_park_table_msg.entrance_measure_info().cx() << std::endl;
  1670. // std::cout << " huli test :::: " << " iiiiiiiiiiiiiiiiiiiiiiiiiiiiii t_terminal = " << t_terminal << std::endl;
  1671. // LOG(INFO) << " m_park_table_msg.entrance_measure_info().cx() = "<< m_park_table_msg.entrance_measure_info().cx() << " --- " << this;
  1672. // LOG(INFO) << " t_terminal = "<< t_terminal << " --- " << this;
  1673. // LOG(INFO) << " m_device_floor = "<< m_device_floor << " --- " << this;
  1674. // LOG(INFO) << " m_dispatch_id = "<< m_dispatch_id << " --- " << this;
  1675. // LOG(INFO) << " m_unit = "<< m_unit << " --- " << this;
  1676. // LOG(INFO) << " m_outlet_ready = "<< m_outlet_ready << " --- " << this;
  1677. m_park_table_msg = t_park_table;
  1678. return Error_code::SUCCESS;
  1679. }
  1680. //制作取车表单
  1681. Error_manager Dispatch_command::create_pick_table(int outlet_ready)
  1682. {
  1683. pick_table t_pick_table;
  1684. t_pick_table.mutable_statu()->set_execute_statu(eNormal);
  1685. t_pick_table.set_queue_id(m_dispatch_command_map[m_car_number_optimal].m_queue_id);
  1686. t_pick_table.set_car_number(m_dispatch_command_map[m_car_number_optimal].m_car_number);
  1687. t_pick_table.set_unit_id(m_dispatch_command_map[m_car_number_optimal].m_unit);
  1688. t_pick_table.set_primary_key(m_dispatch_command_map[m_car_number_optimal].m_primary_key);
  1689. t_pick_table.mutable_actually_measure_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg);
  1690. t_pick_table.mutable_actually_space_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_space_info_msg);
  1691. t_pick_table.set_terminal_id(outlet_ready);
  1692. // LOG(INFO) << " outlet_ready = "<< outlet_ready << " --- " << this;
  1693. //
  1694. // LOG(INFO) << " m_device_floor = "<< m_device_floor << " --- " << this;
  1695. // LOG(INFO) << " m_dispatch_id = "<< m_dispatch_id << " --- " << this;
  1696. // LOG(INFO) << " m_unit = "<< m_unit << " --- " << this;
  1697. // LOG(INFO) << " m_outlet_ready = "<< m_outlet_ready << " --- " << this;
  1698. m_pick_table_msg = t_pick_table;
  1699. return Error_code::SUCCESS;
  1700. }
  1701. //增加 车辆表, 存车指令 完成后添加车辆信息
  1702. Error_manager Dispatch_command::insert_vehicle_for_car_number()
  1703. {
  1704. char insert_vehicle_sql[DB_SQL_SIZE];
  1705. memset(insert_vehicle_sql, 0, DB_SQL_SIZE);
  1706. sprintf(insert_vehicle_sql, "INSERT INTO vehicle (car_number,primary_key,actually_measure_info) values ('%s','%s','%s')",
  1707. m_car_number_optimal.c_str(), m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
  1708. m_dispatch_command_map[m_car_number_optimal].m_measure_info_string.c_str() );
  1709. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
  1710. return ec;
  1711. }
  1712. //删除 车辆表, 取车指令 完成后删除车辆信息
  1713. Error_manager Dispatch_command::delete_vehicle_for_car_number()
  1714. {
  1715. char delete_vehicle_sql[DB_SQL_SIZE];
  1716. memset(delete_vehicle_sql, 0, DB_SQL_SIZE);
  1717. sprintf(delete_vehicle_sql, "delete from vehicle where car_number = '%s'",
  1718. m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
  1719. Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_vehicle_sql);
  1720. return ec;
  1721. }
  1722. //检查记录表是否提前创建。没有创建就返回成功
  1723. Error_manager Dispatch_command::check_record_for_no_primary_key(std::string primary_key)
  1724. {
  1725. //查询车位表
  1726. char query_record_sql[DB_SQL_SIZE];
  1727. memset(query_record_sql, 0, DB_SQL_SIZE);
  1728. sprintf(query_record_sql,"select * from record where primary_key = '%s' ",primary_key.c_str());
  1729. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  1730. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_record_sql, tp_result);
  1731. if(ec == SUCCESS)
  1732. {
  1733. if(tp_result == nullptr)
  1734. {
  1735. return DB_RESULT_SET_EMPTY;
  1736. }
  1737. if (tp_result->next() == false)
  1738. {
  1739. return Error_code::SUCCESS;
  1740. }
  1741. else
  1742. {
  1743. return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
  1744. "error, check_record_for_no_primary_key has primary_key ");
  1745. }
  1746. return SUCCESS;
  1747. }
  1748. else
  1749. {
  1750. return ec;
  1751. }
  1752. return SUCCESS;
  1753. }
  1754. //增加 记录表, 存车指令 完成后添加存车记录
  1755. Error_manager Dispatch_command::insert_record_for_park_start()
  1756. {
  1757. Time_tool::get_instance_references().time_start(PARK_TIME_FLAG);
  1758. int t_terminal = 0;
  1759. #ifdef CHUTIAN_PROJECT_PROJECT
  1760. if (m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()<0)
  1761. {
  1762. t_terminal = m_dispatch_id*2+1;
  1763. }
  1764. else if(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()>0)
  1765. {
  1766. t_terminal = m_dispatch_id*2+2;
  1767. }
  1768. #endif//CHUTIAN_PROJECT_PROJECT
  1769. #ifdef SHANGGUJIE_PROJECT_PROJECT
  1770. t_terminal = m_dispatch_command_map[m_car_number_optimal].m_import_id;
  1771. #endif //SHANGGUJIE_PROJECT_PROJECT
  1772. char insert_record_sql[DB_SQL_SIZE];
  1773. memset(insert_record_sql, 0, DB_SQL_SIZE);
  1774. // sprintf(insert_record_sql, "INSERT INTO record (primary_key,car_number,in_time_start,measure_info,import_id) values ('%s','%s','%s','%s',%d)",
  1775. sprintf(insert_record_sql, "INSERT INTO record (primary_key,car_number,import_id,enqueue_time, in_time_start, measure_info,center_x,center_y,theta,width,height,wheelbase,front_theta, space_info,space_id,table_id,unit_id,floor_id,room_id,plate_color,plate_type,plate_confidence,recognition_time,plate_full_image,plate_clip_image) values ('%s','%s',%d,'%s','%s','%s',%f,%f,%f,%f,%f,%f,%f,'%s',%d,%d,%d,%d,%d,'%s','%s',%d,'%s','%s','%s')",
  1776. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
  1777. m_car_number_optimal.c_str(),
  1778. m_dispatch_command_map[m_car_number_optimal].m_import_id,
  1779. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1780. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1781. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.DebugString().c_str(),
  1782. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx(),
  1783. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cy(),
  1784. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.theta(),
  1785. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.width(),
  1786. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height(),
  1787. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.wheelbase(),
  1788. m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.front_theta(),
  1789. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.DebugString().c_str(),
  1790. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.id(),
  1791. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.table_id(),
  1792. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.unit_id(),
  1793. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.floor(),
  1794. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.room_id(),
  1795. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_color().c_str(),
  1796. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_type().c_str(),
  1797. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_confidence(),
  1798. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.recognition_time().c_str(),
  1799. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_full_image().c_str(),
  1800. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_clip_image().c_str()
  1801. );
  1802. LOG(INFO) << " insert_record_sql = "<< insert_record_sql << " --- " << this;
  1803. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_record_sql);
  1804. return ec;
  1805. }
  1806. //更新 记录表, 存车指令 完成后只更新存车开始时间
  1807. Error_manager Dispatch_command::updata_record_for_park_start()
  1808. {
  1809. Time_tool::get_instance_references().time_start(PARK_TIME_FLAG);
  1810. //执行sql操作
  1811. char update_record_sql[DB_SQL_SIZE];
  1812. memset(update_record_sql, 0, DB_SQL_SIZE);
  1813. sprintf(update_record_sql, "update record set in_time_start = '%s', import_id = %d where primary_key = '%s' ",
  1814. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1815. m_dispatch_command_map[m_car_number_optimal].m_import_id,
  1816. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  1817. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  1818. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  1819. return ec;
  1820. }
  1821. //增加 记录表, 存车指令 完成后添加存车记录
  1822. Error_manager Dispatch_command::insert_record_for_park_start(park_table park_command, Dispatch_command::Dispatch_space_info dispatch_space_info)
  1823. {
  1824. // Time_tool::get_instance_references().time_start(PARK_TIME_FLAG);
  1825. int t_terminal = 0;
  1826. #ifdef CHUTIAN_PROJECT_PROJECT
  1827. if (m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()<0)
  1828. {
  1829. t_terminal = m_dispatch_id*2+1;
  1830. }
  1831. else if(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()>0)
  1832. {
  1833. t_terminal = m_dispatch_id*2+2;
  1834. }
  1835. #endif//CHUTIAN_PROJECT_PROJECT
  1836. #ifdef SHANGGUJIE_PROJECT_PROJECT
  1837. t_terminal = m_dispatch_command_map[m_car_number_optimal].m_import_id;
  1838. #endif //SHANGGUJIE_PROJECT_PROJECT
  1839. parkspace_info t_space_info_msg;
  1840. t_space_info_msg.set_id(dispatch_space_info.m_id);
  1841. t_space_info_msg.set_serial_id(dispatch_space_info.m_serial_id);
  1842. t_space_info_msg.set_table_id(dispatch_space_info.m_table_id);
  1843. t_space_info_msg.set_unit_id(dispatch_space_info.m_unit);
  1844. t_space_info_msg.set_floor(dispatch_space_info.m_floor);
  1845. t_space_info_msg.set_room_id(dispatch_space_info.m_room_id);
  1846. t_space_info_msg.set_height(dispatch_space_info.m_height);
  1847. char insert_record_sql[DB_SQL_SIZE];
  1848. memset(insert_record_sql, 0, DB_SQL_SIZE);
  1849. // sprintf(insert_record_sql, "INSERT INTO record (primary_key,car_number,in_time_start,measure_info,import_id) values ('%s','%s','%s','%s',%d)",
  1850. sprintf(insert_record_sql, "INSERT INTO record (primary_key,car_number,import_id,enqueue_time, measure_info,center_x,center_y,theta,width,height,wheelbase,front_theta, space_info,space_id,table_id,unit_id,floor_id,room_id,plate_color,plate_type,plate_confidence,recognition_time,plate_full_image,plate_clip_image) values ('%s','%s',%d,'%s','%s',%f,%f,%f,%f,%f,%f,%f,'%s',%d,%d,%d,%d,%d,'%s','%s',%d,'%s','%s','%s')",
  1851. park_command.primary_key().c_str(),
  1852. park_command.car_number().c_str(),
  1853. get_passway_id(park_command.terminal_id(), 1),
  1854. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1855. park_command.entrance_measure_info().DebugString().c_str(),
  1856. park_command.entrance_measure_info().cx(),
  1857. park_command.entrance_measure_info().cy(),
  1858. park_command.entrance_measure_info().theta(),
  1859. park_command.entrance_measure_info().width(),
  1860. park_command.entrance_measure_info().height(),
  1861. park_command.entrance_measure_info().wheelbase(),
  1862. park_command.entrance_measure_info().front_theta(),
  1863. t_space_info_msg.DebugString().c_str(),
  1864. t_space_info_msg.id(),
  1865. t_space_info_msg.table_id(),
  1866. t_space_info_msg.unit_id(),
  1867. t_space_info_msg.floor(),
  1868. t_space_info_msg.room_id(),
  1869. park_command.car_number_info().plate_color().c_str(),
  1870. park_command.car_number_info().plate_type().c_str(),
  1871. park_command.car_number_info().plate_confidence(),
  1872. park_command.car_number_info().recognition_time().c_str(),
  1873. park_command.car_number_info().plate_full_image().c_str(),
  1874. park_command.car_number_info().plate_clip_image().c_str()
  1875. );
  1876. LOG(INFO) << " insert_record_sql = "<< insert_record_sql << " --- " << this;
  1877. Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_record_sql);
  1878. return ec;
  1879. }
  1880. //增加 记录表, 存车指令 完成后添加存车记录
  1881. Error_manager Dispatch_command::updata_record_for_park_end()
  1882. {
  1883. Time_tool::get_instance_references().time_end(PARK_TIME_FLAG);
  1884. int in_time_difference = Time_tool::get_instance_references().get_time_seconds(PARK_TIME_FLAG);
  1885. //t_index_id是指定单元的车位id, 1~78
  1886. // int t_table_id = (m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.id() - 1 ) % 78;
  1887. // t_table_id = t_table_id + 1;
  1888. // int t_table_id = m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.table_id();
  1889. //执行sql操作
  1890. char update_record_sql[DB_SQL_SIZE];
  1891. memset(update_record_sql, 0, DB_SQL_SIZE);
  1892. // 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' ",
  1893. 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, plate_color = '%s', plate_type = '%s', plate_confidence = %d, recognition_time = '%s', plate_full_image = '%s', plate_clip_image = '%s' where primary_key = '%s' ",
  1894. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1895. in_time_difference,
  1896. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.id(),
  1897. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.table_id(),
  1898. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.unit_id(),
  1899. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.floor(),
  1900. m_dispatch_command_map[m_car_number_optimal].m_space_info_msg.room_id(),
  1901. // m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.DebugString().c_str(),
  1902. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_color().c_str(),
  1903. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_type().c_str(),
  1904. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_confidence(),
  1905. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.recognition_time().c_str(),
  1906. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_full_image().c_str(),
  1907. m_dispatch_command_map[m_car_number_optimal].m_car_number_info_msg.plate_clip_image().c_str(),
  1908. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str()
  1909. );
  1910. LOG(INFO) << " huli 78978978 updata_record_for_park_end = " << update_record_sql << " "<< this;
  1911. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  1912. return ec;
  1913. }
  1914. //更新 记录表, 取车指令 完成后更新取车记录
  1915. Error_manager Dispatch_command::updata_record_for_pick_start()
  1916. {
  1917. Time_tool::get_instance_references().time_start(PICK_TIME_FLAG);
  1918. //执行sql操作
  1919. char update_record_sql[DB_SQL_SIZE];
  1920. memset(update_record_sql, 0, DB_SQL_SIZE);
  1921. sprintf(update_record_sql, "update record set out_time_start = '%s', export_id = %d where primary_key = '%s' ",
  1922. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1923. m_dispatch_command_map[m_car_number_optimal].m_export_id,
  1924. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  1925. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  1926. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  1927. return ec;
  1928. }
  1929. //更新 记录表, 取车指令 完成后更新取车记录
  1930. Error_manager Dispatch_command::updata_record_for_pick_end()
  1931. {
  1932. Time_tool::get_instance_references().time_end(PICK_TIME_FLAG);
  1933. int out_time_difference = Time_tool::get_instance_references().get_time_seconds(PICK_TIME_FLAG);
  1934. int parking_time;
  1935. get_parking_time(m_dispatch_command_map[m_car_number_optimal].m_primary_key, parking_time);
  1936. char parking_time_string[256] = "";
  1937. int hour = parking_time / 3600;
  1938. int min = (parking_time / 60)%60;
  1939. int sec = parking_time % 60;
  1940. sprintf(parking_time_string, " %d:%02d:%02d", hour, min, sec);
  1941. //执行sql操作
  1942. char update_record_sql[DB_SQL_SIZE];
  1943. memset(update_record_sql, 0, DB_SQL_SIZE);
  1944. sprintf(update_record_sql, "update record set out_time_end = '%s', out_time_difference = %d, parking_time = '%s' where primary_key = '%s' ",
  1945. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1946. out_time_difference,
  1947. parking_time_string,
  1948. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  1949. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  1950. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  1951. return ec;
  1952. }
  1953. //更新 记录表, 取车指令 完成后更新取车记录, 撤销指令, 没有取车时间差
  1954. Error_manager Dispatch_command::updata_record_for_pick_end_ex()
  1955. {
  1956. int parking_time;
  1957. get_parking_time(m_dispatch_command_map[m_car_number_optimal].m_primary_key, parking_time);
  1958. char parking_time_string[256] = "";
  1959. int hour = parking_time / 3600;
  1960. int min = parking_time / 60;
  1961. int sec = parking_time % 60;
  1962. sprintf(parking_time_string, " %d:%02d:%02d", hour, min, sec);
  1963. //执行sql操作
  1964. char update_record_sql[DB_SQL_SIZE];
  1965. memset(update_record_sql, 0, DB_SQL_SIZE);
  1966. sprintf(update_record_sql, "update record set out_time_end = '%s', parking_time = '%s' where primary_key = '%s' ",
  1967. Time_tool::get_instance_references().get_current_time_seconds().c_str(),
  1968. parking_time_string,
  1969. m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
  1970. LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
  1971. Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
  1972. return ec;
  1973. }
  1974. //计算汽车在车库的存车时间, 从存车开始到取车结束, 返回 parking_time, 单位秒.
  1975. Error_manager Dispatch_command::get_parking_time(std::string primary_key, int & parking_time )
  1976. {
  1977. //查询存车时间 in_time_start
  1978. char query_record_sql[DB_SQL_SIZE];
  1979. memset(query_record_sql, 0, DB_SQL_SIZE);
  1980. sprintf(query_record_sql,"select * from record where primary_key = '%s' ", primary_key.c_str() );
  1981. boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
  1982. Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_record_sql, tp_result);
  1983. if(ec == SUCCESS)
  1984. {
  1985. if(tp_result == nullptr)
  1986. {
  1987. return DB_RESULT_SET_EMPTY;
  1988. }
  1989. //只取第一条结果, 默认是id最小的,
  1990. if (tp_result->next())
  1991. {
  1992. char buf[DB_SQL_SIZE];
  1993. memset(buf, 0, DB_SQL_SIZE);
  1994. try
  1995. {
  1996. //解析数据
  1997. std::string in_time_start_string = tp_result->getString("in_time_start");
  1998. std::chrono::system_clock::time_point in_time_start = Time_tool::get_instance_references().transform_time_string_seconds(in_time_start_string);
  1999. std::chrono::system_clock::time_point out_time_end = std::chrono::system_clock::now();
  2000. double t_parking_time = (out_time_end - in_time_start).count() / 1000000000;
  2001. parking_time = t_parking_time;
  2002. return Error_code::SUCCESS;
  2003. }
  2004. catch (sql::SQLException &e)
  2005. {
  2006. /* Use what() (derived from std::runtime_error) to fetch the error message */
  2007. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  2008. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  2009. }
  2010. catch (std::runtime_error &e)
  2011. {
  2012. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
  2013. return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
  2014. }
  2015. }
  2016. else
  2017. {
  2018. return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
  2019. "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
  2020. }
  2021. return SUCCESS;
  2022. }
  2023. else
  2024. {
  2025. return ec;
  2026. }
  2027. return Error_code::SUCCESS;
  2028. }
  2029. //获取出入口id 1~2为入口, 3~4为出口,
  2030. //输入terminal_id 1~19, 输入 action_mode 1:存车, 2:取车
  2031. int Dispatch_command::get_passway_id(int terminal_id, int action_mode)
  2032. {
  2033. terminal_id = terminal_id%100;
  2034. int passway_id = 0;
  2035. switch ( terminal_id )
  2036. {
  2037. case 1:
  2038. case 2:
  2039. case 4:
  2040. case 6:
  2041. case 8:
  2042. case 10:
  2043. case 12:
  2044. case 14:
  2045. case 16:
  2046. case 17:
  2047. case 19:
  2048. {
  2049. passway_id = 1 + (action_mode-1)*2;
  2050. break;
  2051. }
  2052. case 3:
  2053. case 5:
  2054. case 7:
  2055. case 9:
  2056. case 11:
  2057. case 13:
  2058. case 15:
  2059. case 18:
  2060. {
  2061. passway_id = 2 + (action_mode-1)*2;
  2062. break;
  2063. }
  2064. default:
  2065. {
  2066. break;
  2067. }
  2068. }
  2069. return passway_id;
  2070. }