123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344 |
- #include "dispatch_command.h"
- #include <google/protobuf/text_format.h>
- #include "../tool/time_tool.h"
- #include "dispatch_manager.h"
- Dispatch_command::Dispatch_command()
- {
- m_dispatch_command_status = E_DISPATCH_COMMAND_UNKNOW;
- m_unit = 0;
- m_device_floor = 0;
- m_dispatch_process_type = Common_data::DISPATCH_PROCESS_TYPE_UNKNOW;
- m_car_type = Common_data::Car_type::UNKNOW_CAR_TYPE;
- }
- Dispatch_command::~Dispatch_command()
- {
- }
- //调度开始前, 向数据库发送请求的相关操作, 输入 穿梭机所在的楼层, 调度id 0~2, 空闲出口id, 如果不是0,就表示有空闲的出口
- Error_manager Dispatch_command::dispatch_request_to_sql(int device_floor, int dispatch_id, int outlet_ready)
- {
- std::unique_lock<std::mutex> t_lock(m_lock);
- Error_manager t_error;
- m_device_floor = device_floor;
- m_dispatch_id = dispatch_id;
- m_unit = dispatch_id+1;
- m_outlet_ready = outlet_ready;
- //获取调度指令, 与数据库同步 command_queue
- t_error = query_all_dispatch_command(0);
- if ( t_error != Error_code::SUCCESS )
- {
- return t_error;
- }
- //排序
- if ( outlet_ready == 0 )
- {
- //对调度指令进行排序, 选出最优解, 只比较存车
- t_error = sort_dispatch_command_for_park();
- }
- else
- {
- //对调度指令进行排序, 选出最优解, 比较存车和取车
- t_error = sort_dispatch_command_for_total();
- }
- if ( t_error != Error_code::SUCCESS )
- {
- m_dispatch_process_type = Common_data::DISPATCH_PROCESS_TYPE_UNKNOW;
- //没有找到合适的指令就返回NODATA, 然后设么也不做
- return t_error;
- }
- else
- {
- if ( m_dispatch_command_map[m_car_number_optimal].m_type == 1 )//存车
- {
- //注注注注注意了
- //存车自带感测信息,没有车位信息, 需要查询车位表,找到空车位,补全车位信息
- //取车自带车位信息,没有感测信息, 需要查询车辆表,补全车辆信息
- //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解
- t_error = query_dispatch_space_optimal();
- if ( t_error != Error_code::SUCCESS )
- {
- update_command_queue_for_statu(3);
- return t_error;
- }
- else
- {
- m_dispatch_process_type = Common_data::DISPATCH_PROCESS_STORE;
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_id(m_dispatch_space_info.m_id);
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_unit_id(m_dispatch_space_info.m_unit);
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_floor(m_dispatch_space_info.m_floor);
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_room_id(m_dispatch_space_info.m_subID);
- //更新 车位状态, 根据车位ID 写入车牌号即可
- t_error = update_parkspace_info_write_car_number();
- //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
- t_error = update_command_queue_for_statu(1);
- //制作存车表单
- t_error = create_park_table();
- //增加 记录表, 存车指令 完成后添加存车记录
- t_error = insert_record_for_park_start();
- LOG(INFO) << " create_park_table 创建存车任务单 = "<< m_park_table_msg.DebugString() << " --- " << this;
- return Error_code::SUCCESS;
- }
- }
- else if ( m_dispatch_command_map[m_car_number_optimal].m_type == 2 )//取车
- {
- //注注注注注意了
- //存车自带感测信息,没有车位信息, 需要查询车位表,找到空车位,补全车位信息
- //取车自带车位信息,没有感测信息, 需要查询车辆表,补全车辆信息
- //查询 取车的车辆感测信息 取车指令 根据key 查询感测信息
- t_error = query_dispatch_vehicle_for_primary_key();
- if ( t_error != Error_code::SUCCESS )
- {
- update_command_queue_for_statu(3);
- return t_error;
- }
- else
- {
- //补充出口id
- m_dispatch_command_map[m_car_number_optimal].m_export_id = outlet_ready;
- m_dispatch_process_type = Common_data::DISPATCH_PROCESS_PICKUP;
- //查询 取车的车辆plc感测信息 取车指令 根据key 查询感测信息
- t_error = query_dispatch_vehicle_for_primary_key_ex();
- if ( t_error == Error_code::SUCCESS &&
- m_dispatch_vehicle_info.m_plc_measure_info_msg.wheelbase()>1)
- {
- m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg = m_dispatch_vehicle_info.m_plc_measure_info_msg;
- }
- else
- {
- m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg = m_dispatch_vehicle_info.m_actually_measure_info_msg;
- }
- //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
- t_error = update_command_queue_for_statu(1);
- //制作取车表单
- t_error = create_pick_table(outlet_ready);
- //更新 记录表, 取车指令 完成后更新取车记录
- t_error = updata_record_for_pick_start();
- LOG(INFO) << " create_pick_table 创建取车车任务单 = "<< m_pick_table_msg.DebugString() << " --- " << this;
- }
- }
- else
- {
- return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
- " fun error ");
- }
- }
- return Error_code::SUCCESS;
- }
- //调度完成后, 向数据库发送答复的相关操作
- Error_manager Dispatch_command::dispatch_response_to_sql(Error_manager dispatch_result)
- {
- std::unique_lock<std::mutex> t_lock(m_lock);
- Error_manager t_error;
- if ( dispatch_result == Error_code::SUCCESS )
- {
- //调度成功, 完善数据库
- if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
- {
- LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_STORE 存车答复成功 = "<< t_error << " --- " << this;
- //增加 车辆表, 存车指令 完成后添加车辆信息
- t_error = insert_vehicle_for_car_number_ex();
- //更新 指令队列, 根据车牌号 删除指令
- t_error = delete_command_queue_for_statu();
- //增加 记录表, 存车指令 完成后添加存车记录
- t_error = updata_record_for_park_end();
- }
- else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
- {
- LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_PICKUP 取车答复成功 = "<< t_error << " --- " << this;
- //删除 车辆表, 取车指令 完成后删除车辆信息
- t_error = delete_vehicle_for_car_number();
- //更新 车位状态, 找到车牌号, 写NULL
- t_error = update_parkspace_info_clear_car_number();
- //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
- t_error = update_command_queue_for_statu(2);
- //更新 记录表, 取车指令 完成后更新取车记录
- t_error = updata_record_for_pick_end();
- }
- else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
- {
- LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_REALLOCATE 改道答复成功 = "<< t_error << " --- " << this;
- //增加 车辆表, 存车指令 完成后添加车辆信息
- t_error = insert_vehicle_for_car_number_ex();
- //更新 指令队列, 根据车牌号 删除指令
- t_error = delete_command_queue_for_statu();
- //增加 记录表, 存车指令 完成后添加存车记录
- t_error = updata_record_for_park_end();
- }
- else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
- {
- LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_REVOCATION 撤销答复成功 = "<< t_error << " --- " << this;
- //删除 车辆表, 取车指令 完成后删除车辆信息
- t_error = delete_vehicle_for_car_number();
- //更新 车位状态, 找到车牌号, 写NULL
- t_error = update_parkspace_info_clear_car_number();
- //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
- t_error = update_command_queue_for_statu(2);
- // LOG(INFO) << "llllllllllllllllllllllllllllllllllllllllll respons = "<< t_error << " --- " << this;
- //更新 记录表, 取车指令 完成后更新取车记录, 撤销指令, 没有取车时间差
- t_error = updata_record_for_pick_end_ex();
- }
- else
- {
- return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
- " fun error ");
- }
- return t_error;
- }
- else
- {
- //调度失败, 回退 数据库
- if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_STORE )
- {
- LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_STORE 存车答复失败 = "<< t_error << " --- " << this;
- //更新 车位状态, 找到车牌号, 写NULL
- update_parkspace_info_clear_car_number();
- //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
- update_command_queue_for_statu(3);
- }
- else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_PICKUP )
- {
- LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_PICKUP 取车答复失败 = "<< t_error << " --- " << this;
- //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
- update_command_queue_for_statu(3);
- }
- else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REALLOCATE )
- {
- LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_STORE 改道答复失败 = "<< t_error << " --- " << this;
- //更新 车位状态, 找到车牌号, 写NULL
- update_parkspace_info_clear_car_number();
- //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
- update_command_queue_for_statu(3);
- }
- else if ( m_dispatch_process_type == Common_data::DISPATCH_PROCESS_REVOCATION )
- {
- LOG(INFO) << " dispatch_response_to_sql DISPATCH_PROCESS_PICKUP 撤销答复失败 = "<< t_error << " --- " << this;
- //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
- update_command_queue_for_statu(3);
- }
- else
- {
- return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
- " fun error ");
- }
- }
- return Error_code::SUCCESS;
- }
- //调度 , 向数据库 重新分配车位,
- Error_manager Dispatch_command::dispatch_reallocate_to_sql(Common_data::Car_type reallocate_car_type, int outlet_ready)
- {
- std::unique_lock<std::mutex> t_lock(m_lock);
- Error_manager t_error;
- m_outlet_ready = outlet_ready;
- //把之前申请的车位清除 //更新 车位状态, 找到车牌号, 写NULL
- t_error = Dispatch_command::update_parkspace_info_clear_car_number();
- //重新申请车位
- //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解
- t_error = query_dispatch_space_optimal(reallocate_car_type);
- if ( t_error != Error_code::SUCCESS )
- {
- //没找到车位, 就把车放到出口
- if ( outlet_ready != 0 )
- {
- m_dispatch_process_type = Common_data::DISPATCH_PROCESS_REVOCATION;
- //补充出口id
- m_dispatch_command_map[m_car_number_optimal].m_export_id = outlet_ready;
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_id(0);
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_unit_id(0);
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_floor(0);
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_room_id(0);
- //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
- // t_error = update_command_queue_for_statu(1);
- //制作取车表单
- t_error = create_pick_table(outlet_ready);
- LOG(INFO) << " create_pick_table 撤销存车, 取车到出口任务单 = "<< m_pick_table_msg.DebugString() << " --- " << this;
- return t_error;
- }
- else
- {
- //无限等待, 等待出口空闲
- return Error_code::NODATA;
- }
- }
- else
- {
- m_dispatch_process_type = Common_data::DISPATCH_PROCESS_REALLOCATE;
- //找到新的车位, 重新执行任务
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_id(m_dispatch_space_info.m_id);
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_unit_id(m_dispatch_space_info.m_unit);
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_floor(m_dispatch_space_info.m_floor);
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.set_room_id(m_dispatch_space_info.m_subID);
- //更新 车位状态, 根据车位ID 写入车牌号即可
- t_error = update_parkspace_info_write_car_number();
- //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
- // t_error = update_command_queue_for_statu(1);
- //制作存车表单
- t_error = create_park_table();
- LOG(INFO) << " create_park_table 改道存车, 存车新车位任务单 = "<< m_park_table_msg.DebugString() << " --- " << this;
- }
- return Error_code::SUCCESS;
- }
- //检查出口是否空闲, 检查指令队列的取车完成的出口id是否存在, 不存在就是空闲,返回成功
- Error_manager Dispatch_command::check_export_id_is_ready(int export_id)
- {
- std::unique_lock<std::mutex> t_lock(m_lock);
- //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
- char check_export_id_sql[1024];
- memset(check_export_id_sql, 0, 1024);
- sprintf(check_export_id_sql,"select * from command_queue where statu = 2 and type = 2 and export_id = %d",export_id);
- boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
- Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
- if(t_error == Error_code::SUCCESS)
- {
- if (tp_result == nullptr)
- {
- return DB_RESULT_SET_EMPTY;
- }
- //如果存在, 就报错, 不存在就是空闲,返回成功
- if ( tp_result->next() )
- {
- return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
- " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
- }
- else
- {
- return Error_code::SUCCESS;
- }
- }
- else
- {
- return t_error;
- }
- return Error_code::SUCCESS;
- }
- //检查出口取车是否完成, 指定的出口有取车完成指令就返回成功, 否则报错.
- Error_manager Dispatch_command::check_pickup_is_finish(int export_id)
- {
- std::unique_lock<std::mutex> t_lock(m_lock);
- //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
- char check_export_id_sql[1024];
- memset(check_export_id_sql, 0, 1024);
- sprintf(check_export_id_sql,"select * from command_queue where statu = 2 and type = 2 and export_id = %d",export_id);
- boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
- Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
- if(t_error == Error_code::SUCCESS)
- {
- if (tp_result == nullptr)
- {
- return DB_RESULT_SET_EMPTY;
- }
- //如果存在, 就报错, 不存在就是空闲,返回成功
- if ( tp_result->next() )
- {
- return Error_code::SUCCESS;
- }
- else
- {
- return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
- " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
- }
- }
- else
- {
- return t_error;
- }
- return Error_code::SUCCESS;
- }
- //检查入口存车是否开始, 指定的入口有存车开始指令就返回成功, 否则报错.
- Error_manager Dispatch_command::check_park_is_start(int import_id)
- {
- std::unique_lock<std::mutex> t_lock(m_lock);
- //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
- char check_export_id_sql[1024];
- memset(check_export_id_sql, 0, 1024);
- sprintf(check_export_id_sql,"select * from command_queue where type = 1 and import_id = %d and (statu=0 or statu =1)",import_id);
- boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
- Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
- if(t_error == Error_code::SUCCESS)
- {
- if (tp_result == nullptr)
- {
- return DB_RESULT_SET_EMPTY;
- }
- //如果存在, 就报错, 不存在就是空闲,返回成功
- if ( tp_result->next() )
- {
- return Error_code::SUCCESS;
- }
- else
- {
- return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
- " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
- }
- }
- else
- {
- return t_error;
- }
- return Error_code::SUCCESS;
- }
- //获取指定入口的 存车流程的 唯一码,
- Error_manager Dispatch_command::get_primary_key_for_store(int import_id, std::string & primary_key )
- {
- std::unique_lock<std::mutex> t_lock(m_lock);
- //检查指令队列, 查询指定单元的取车完成状态指令的 出口id是否存在
- char check_export_id_sql[1024];
- memset(check_export_id_sql, 0, 1024);
- sprintf(check_export_id_sql,"select * from command_queue where type = 1 and import_id = %d and (statu=0 or statu =1)",import_id);
- boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
- Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(check_export_id_sql, tp_result);
- if(t_error == Error_code::SUCCESS)
- {
- if (tp_result == nullptr)
- {
- return DB_RESULT_SET_EMPTY;
- }
- //如果存在, 就报错, 不存在就是空闲,返回成功
- if ( tp_result->next() )
- {
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- //解析数据
- primary_key = tp_result->getString("primary_key");
- }
- catch (sql::SQLException &e)
- {
- /* Use what() (derived from std::runtime_error) to fetch the error message */
- sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
- return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
- }
- catch (std::runtime_error &e)
- {
- sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
- return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
- }
- return Error_code::SUCCESS;
- }
- else
- {
- return Error_manager(Error_code::DB_QUERY_OUTLET_OCCUPY, Error_level::MINOR_ERROR,
- " check_export_id_is_ready fun error, 数据库 查询出口被占用 ");
- }
- }
- else
- {
- return t_error;
- }
- return Error_code::SUCCESS;
- }
- //删除 指令队列, 根据出口编号
- Error_manager Dispatch_command::delete_command_queue_for_export_id(int export_id)
- {
- std::unique_lock<std::mutex> t_lock(m_lock);
- char delete_command_sql[1024];
- memset(delete_command_sql, 0, 1024);
- sprintf(delete_command_sql, "delete from command_queue where statu = 2 and type = 2 and export_id = %d ",
- export_id );
- Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_command_sql);
- return ec;
- }
- //添加plc数据储存
- Error_manager Dispatch_command::insert_plc_data(std::string plc_data, float hearbeat)
- {
- std::unique_lock<std::mutex> t_lock(m_lock);
- char insert_vehicle_sql[4096];
- memset(insert_vehicle_sql, 0, 4096);
- sprintf(insert_vehicle_sql, "INSERT INTO plc_data (data, hearbeat) values ('%s',%f)",
- plc_data.c_str(), hearbeat );
- Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
- std::cout << " huli test :::: " << " ec = " << ec << std::endl;
- return ec;
- }
- //获取调度指令, 与数据库同步 command_queue, statu指令状态, 0:排队等待, 1正在运行, 2:已经完成
- Error_manager Dispatch_command::query_all_dispatch_command(int statu)
- {
- //从command_queue获取所有排队的指令,
- char query_all_dispatch_command_sql[1024];
- memset(query_all_dispatch_command_sql, 0, 1024);
- sprintf(query_all_dispatch_command_sql,"select * from command_queue where statu = %d and unit = %d",statu, m_unit);
- boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
- Error_manager t_error = Database_controller::get_instance_pointer()->sql_query(query_all_dispatch_command_sql, tp_result);
- m_dispatch_command_map.clear();
- if(t_error == Error_code::SUCCESS)
- {
- if (tp_result == nullptr)
- {
- return DB_RESULT_SET_EMPTY;
- }
- //循环检查所有行
- while (tp_result->next())
- {
- Dispatch_command_info t_dispatch_command_info;
- char t_error_buf[1024];
- memset(t_error_buf, 0, 1024);
- try
- {
- //解析数据
- t_dispatch_command_info.m_car_number = tp_result->getString("car_number");
- t_dispatch_command_info.m_primary_key = tp_result->getString("primary_key");
- t_dispatch_command_info.m_unit = tp_result->getInt("unit");
- t_dispatch_command_info.m_queue_id = tp_result->getInt("queue_id");
- t_dispatch_command_info.m_type = tp_result->getInt("type");
- t_dispatch_command_info.m_statu = tp_result->getInt("statu");
- t_dispatch_command_info.m_space_info = tp_result->getString("space_info");
- t_dispatch_command_info.m_measure_info = tp_result->getString("measure_info");
- // t_dispatch_command_info.m_export_id = tp_result->getInt("export_id");
- t_dispatch_command_info.m_export_id = 0;
- //m_type指令类型, 0无效, 1存车, 2取车,
- if ( t_dispatch_command_info.m_type == 1)
- {
- //存车, 数据库特有 雷达信息, 没有车位信息, 需要根据车高,再去车位表申请车位
- if(! google::protobuf::TextFormat::ParseFromString(t_dispatch_command_info.m_measure_info, &t_dispatch_command_info.m_measure_info_msg))
- {
- return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
- "ParseFromString fun error, m_measure_info ");
- }
- t_dispatch_command_info.m_useless_distance = m_device_floor-1;
- }
- else if ( t_dispatch_command_info.m_type == 2 )
- {
- //取车, 数据库特有车位信息
- if(! google::protobuf::TextFormat::ParseFromString(t_dispatch_command_info.m_space_info, &t_dispatch_command_info.m_parkspace_info_msg))
- {
- return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
- "ParseFromString fun error, m_space_info ");
- }
- t_dispatch_command_info.m_useless_distance = t_dispatch_command_info.m_parkspace_info_msg.floor()-m_device_floor;
- }
- else
- {
- return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
- "command_queue m_type error ");
- }
- if ( t_dispatch_command_info.m_useless_distance <0 )
- {
- t_dispatch_command_info.m_useless_distance = 0-t_dispatch_command_info.m_useless_distance;
- }
- m_dispatch_command_map[t_dispatch_command_info.m_car_number] = t_dispatch_command_info;
- }
- catch (sql::SQLException &e)
- {
- /* Use what() (derived from std::runtime_error) to fetch the error message */
- sprintf(t_error_buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(),
- e.getSQLState().c_str());
- return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, t_error_buf);
- }
- catch (std::runtime_error &e)
- {
- sprintf(t_error_buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
- return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, t_error_buf);
- }
- }
- return SUCCESS;
- }
- else
- {
- return t_error;
- }
- return Error_code::SUCCESS;
- }
- //对调度指令进行排序, 选出最优解, 比较存车和取车
- Error_manager Dispatch_command::sort_dispatch_command_for_total()
- {
- int t_optimal_loss=0x7fffffff; //最优loss值
- m_car_number_optimal.clear();
- for (auto iter = m_dispatch_command_map.begin(); iter != m_dispatch_command_map.end(); ++iter)
- {
- //loss = 空跑路程 + 排队编号, 求最小值
- int t_current_loss = iter->second.m_useless_distance + iter->second.m_queue_id;
- if ( t_optimal_loss > t_current_loss )
- {
- t_optimal_loss = t_current_loss;
- m_car_number_optimal = iter->first;
- }
- }
- if ( m_car_number_optimal.empty() )
- {
- return Error_code::NODATA;
- }
- return Error_code::SUCCESS;
- }
- //对调度指令进行排序, 选出最优解, 只比较存车
- Error_manager Dispatch_command::sort_dispatch_command_for_park()
- {
- int t_optimal_loss=0x7fffffff; //最优loss值
- m_car_number_optimal.clear();
- for (auto iter = m_dispatch_command_map.begin(); iter != m_dispatch_command_map.end(); ++iter)
- {
- //loss = 空跑路程 + 排队编号, 求最小值
- int t_current_loss = iter->second.m_useless_distance + iter->second.m_queue_id;
- if ( t_optimal_loss > t_current_loss && iter->second.m_type == 1)
- {
- t_optimal_loss = t_current_loss;
- m_car_number_optimal = iter->first;
- }
- }
- if ( m_car_number_optimal.empty() )
- {
- return Error_code::NODATA;
- }
- return Error_code::SUCCESS;
- }
- //存车指令 获取 指定车高 指定单元 的车位信息,用于车位分配
- Error_manager Dispatch_command::query_specify_height_unit_parkspace_info(int unit, float height, Dispatch_space_info & dispatch_space_info)
- {
- //查询车位表
- char query_parkspace_sql[1024];
- memset(query_parkspace_sql, 0, 1024);
- int t_statu = 0; //车位状态, 0可用, 1故障
- sprintf(query_parkspace_sql,"select * from space where car_number is NULL and ABS(height- %f) < 1e-5 and statu = %d and unit = %d",height,t_statu,unit);
- boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
- if(ec == SUCCESS)
- {
- if(tp_result == nullptr)
- {
- return DB_RESULT_SET_EMPTY;
- }
- //只取第一条结果, 默认是id最小的,
- if (tp_result->next())
- {
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- //解析数据
- dispatch_space_info.m_id = tp_result->getInt("id");
- dispatch_space_info.m_floor = tp_result->getInt("floor");
- dispatch_space_info.m_subID = tp_result->getInt("subID");
- dispatch_space_info.m_height = tp_result->getDouble("height");
- dispatch_space_info.m_car_number = tp_result->getString("car_number");
- dispatch_space_info.m_unit = tp_result->getInt("unit");
- dispatch_space_info.m_statu = tp_result->getInt("statu");
- }
- catch (sql::SQLException &e)
- {
- /* Use what() (derived from std::runtime_error) to fetch the error message */
- sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
- return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
- }
- catch (std::runtime_error &e)
- {
- sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
- return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
- }
- }
- else
- {
- return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
- "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
- }
- return SUCCESS;
- }
- else
- {
- return ec;
- }
- return SUCCESS;
- }
- //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解
- Error_manager Dispatch_command::query_dispatch_space_optimal()
- {
- Error_manager t_error;
- int t_unit = m_dispatch_command_map[m_car_number_optimal].m_unit;
- float t_height = m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height();
- //小车对应小车位 中车对应中车位 大车对应大车位
- if ( 0 < t_height && t_height <= CAR_HEIGHT_LIMIT_SMALL )
- {
- t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_SMALL, m_dispatch_space_info);
- if ( t_error == Error_code::SUCCESS )
- {
- m_car_type = Common_data::Car_type::MIN_CAR;
- return t_error;
- }
- }
- if ( t_height <= CAR_HEIGHT_LIMIT_MIDDLE )
- {
- t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_MIDDLE, m_dispatch_space_info);
- if ( t_error == Error_code::SUCCESS )
- {
- m_car_type = Common_data::Car_type::MID_CAR;
- return t_error;
- }
- }
- if ( t_height <= CAR_HEIGHT_LIMIT_BIG )
- {
- t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_BIG, m_dispatch_space_info);
- if ( t_error == Error_code::SUCCESS )
- {
- m_car_type = Common_data::Car_type::BIG_CAR;
- return t_error;
- }
- }
- return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
- " query_dispatch_space_optimal error ");
- }
- //查询空闲车位最优解, 存车指令 根据调度指令最优解 获取 空闲车位最优解
- Error_manager Dispatch_command::query_dispatch_space_optimal(Common_data::Car_type reallocate_car_type)
- {
- Error_manager t_error;
- int t_unit = m_dispatch_command_map[m_car_number_optimal].m_unit;
- // float t_height = m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height();
- //小车对应小车位 中车对应中车位 大车对应大车位
- if ( reallocate_car_type == Common_data::Car_type::MIN_CAR )
- {
- t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_SMALL, m_dispatch_space_info);
- if ( t_error == Error_code::SUCCESS )
- {
- m_car_type = Common_data::Car_type::MIN_CAR;
- return t_error;
- }
- }
- if ( reallocate_car_type == Common_data::Car_type::MIN_CAR ||
- reallocate_car_type == Common_data::Car_type::MID_CAR )
- {
- t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_MIDDLE, m_dispatch_space_info);
- if ( t_error == Error_code::SUCCESS )
- {
- m_car_type = Common_data::Car_type::MID_CAR;
- return t_error;
- }
- }
- if ( reallocate_car_type == Common_data::Car_type::MIN_CAR ||
- reallocate_car_type == Common_data::Car_type::MID_CAR ||
- reallocate_car_type == Common_data::Car_type::BIG_CAR )
- {
- t_error = query_specify_height_unit_parkspace_info(t_unit, CAR_HEIGHT_LIMIT_BIG, m_dispatch_space_info);
- if ( t_error == Error_code::SUCCESS )
- {
- m_car_type = Common_data::Car_type::BIG_CAR;
- return t_error;
- }
- }
- return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
- " query_dispatch_space_optimal error ");
- }
- //查询 取车的车位 取车指令 根据车牌号 查询对应的车位
- Error_manager Dispatch_command::query_dispatch_space_for_car_number()
- {
- //查询车位表
- char query_parkspace_sql[1024];
- memset(query_parkspace_sql, 0, 1024);
- int t_statu = 0; //车位状态, 0可用, 1故障
- sprintf(query_parkspace_sql,"select * from space where car_number = '%s' ",m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
- // std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
- boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
- if(ec == SUCCESS)
- {
- if(tp_result == nullptr)
- {
- return DB_RESULT_SET_EMPTY;
- }
- //只取第一条结果, 默认是id最小的,
- if (tp_result->next())
- {
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- //解析数据
- m_dispatch_space_info.m_id = tp_result->getInt("id");
- m_dispatch_space_info.m_floor = tp_result->getInt("floor");
- m_dispatch_space_info.m_subID = tp_result->getInt("subID");
- m_dispatch_space_info.m_height = tp_result->getDouble("height");
- m_dispatch_space_info.m_car_number = tp_result->getString("car_number");
- m_dispatch_space_info.m_unit = tp_result->getInt("unit");
- m_dispatch_space_info.m_statu = tp_result->getInt("statu");
- }
- catch (sql::SQLException &e)
- {
- /* Use what() (derived from std::runtime_error) to fetch the error message */
- sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
- return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
- }
- catch (std::runtime_error &e)
- {
- sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
- return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
- }
- }
- else
- {
- return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
- "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
- }
- return SUCCESS;
- }
- else
- {
- return ec;
- }
- return SUCCESS;
- }
- //查询 取车的车辆感测信息 取车指令 根据key 查询感测信息
- Error_manager Dispatch_command::query_dispatch_vehicle_for_primary_key()
- {
- //查询 车辆表
- char query_parkspace_sql[1024];
- memset(query_parkspace_sql, 0, 1024);
- int t_statu = 0; //车位状态, 0可用, 1故障
- sprintf(query_parkspace_sql,"select * from vehicle where primary_key = '%s' ",m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str());
- // std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
- boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
- if(ec == SUCCESS)
- {
- if(tp_result == nullptr)
- {
- return DB_RESULT_SET_EMPTY;
- }
- //只取第一条结果, 默认是id最小的,
- if (tp_result->next())
- {
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- //解析数据
- m_dispatch_vehicle_info.m_car_number = tp_result->getString("car_number");
- m_dispatch_vehicle_info.m_primary_key = tp_result->getString("primary_key");
- m_dispatch_vehicle_info.m_actually_measure_info = tp_result->getString("actually_measure_info");
- //取车, 数据库特有车位信息
- if(! google::protobuf::TextFormat::ParseFromString(m_dispatch_vehicle_info.m_actually_measure_info, &m_dispatch_vehicle_info.m_actually_measure_info_msg))
- {
- return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
- "ParseFromString fun error, m_space_info ");
- }
- }
- catch (sql::SQLException &e)
- {
- /* Use what() (derived from std::runtime_error) to fetch the error message */
- sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
- return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
- }
- catch (std::runtime_error &e)
- {
- sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
- return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
- }
- }
- else
- {
- return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
- "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
- }
- return SUCCESS;
- }
- else
- {
- return ec;
- }
- return SUCCESS;
- }
- //查询 取车的车辆plc感测信息 取车指令 根据key 查询感测信息
- Error_manager Dispatch_command::query_dispatch_vehicle_for_primary_key_ex()
- {
- //查询 车辆表
- char query_parkspace_sql[1024];
- memset(query_parkspace_sql, 0, 1024);
- int t_statu = 0; //车位状态, 0可用, 1故障
- sprintf(query_parkspace_sql,"select * from vehicle where primary_key = '%s' ",m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str());
- // std::cout<<"query_parkspace_sql = "<<query_parkspace_sql<<std::endl;
- boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_parkspace_sql, tp_result);
- if(ec == SUCCESS)
- {
- if(tp_result == nullptr)
- {
- return DB_RESULT_SET_EMPTY;
- }
- //只取第一条结果, 默认是id最小的,
- if (tp_result->next())
- {
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- //解析数据
- m_dispatch_vehicle_info.m_car_number = tp_result->getString("car_number");
- m_dispatch_vehicle_info.m_primary_key = tp_result->getString("primary_key");
- m_dispatch_vehicle_info.m_plc_measure_info = tp_result->getString("plc_measure_info");
- //取车, 数据库特有车位信息
- if(! google::protobuf::TextFormat::ParseFromString(m_dispatch_vehicle_info.m_plc_measure_info, &m_dispatch_vehicle_info.m_plc_measure_info_msg))
- {
- return Error_manager(Error_code::ERROR, Error_level::MINOR_ERROR,
- "ParseFromString fun error, m_space_info ");
- }
- }
- catch (sql::SQLException &e)
- {
- /* Use what() (derived from std::runtime_error) to fetch the error message */
- sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
- return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
- }
- catch (std::runtime_error &e)
- {
- sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
- return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
- }
- }
- else
- {
- return Error_manager(Error_code::DB_QUERY_NOT_DATA, Error_level::MINOR_ERROR,
- "数据库未查询到数据 Parkspace_operating_function::query_one_parkspace_with_license error ");
- }
- return SUCCESS;
- }
- else
- {
- return ec;
- }
- return SUCCESS;
- }
- //更新 车位状态, 根据车位ID 写入车牌号即可
- Error_manager Dispatch_command::update_parkspace_info_write_car_number()
- {
- //执行sql操作
- char update_space_sql[1024];
- memset(update_space_sql, 0, 1024);
- sprintf(update_space_sql, "update space set car_number = '%s' where id = %d",
- m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str(),
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.id() );
- // m_dispatch_space_info.m_id);
- Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
- return ec;
- }
- //更新 车位状态, 找到车牌号, 写NULL
- Error_manager Dispatch_command::update_parkspace_info_clear_car_number()
- {
- //执行sql操作
- char update_space_sql[1024];
- memset(update_space_sql, 0, 1024);
- sprintf(update_space_sql, "update space set car_number = null where car_number = '%s' ",
- m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
- Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_space_sql);
- return ec;
- }
- //更新 指令队列, 根据车牌号 修改状态即可, //指令状态, 0排队中, 1正在工作, 2已完成, 3故障异常
- Error_manager Dispatch_command::update_command_queue_for_statu(int statu)
- {
- char update_command_sql[1024];
- memset(update_command_sql, 0, 1024);
- sprintf(update_command_sql, "update command_queue set statu = %d, export_id = %d where car_number = '%s'",
- statu, m_dispatch_command_map[m_car_number_optimal].m_export_id,
- m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
- // LOG(INFO) << "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj update_command_sql = "<< update_command_sql << " --- " << this;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_command_sql);
- return ec;
- }
- //删除 指令队列, 根据车牌号 删除指令
- Error_manager Dispatch_command::delete_command_queue_for_statu()
- {
- char delete_command_sql[1024];
- memset(delete_command_sql, 0, 1024);
- sprintf(delete_command_sql, "delete from command_queue where car_number = '%s'",
- m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
- Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_command_sql);
- return ec;
- }
- //制作存车表单
- Error_manager Dispatch_command::create_park_table()
- {
- park_table t_park_table;
- t_park_table.mutable_statu()->set_execute_statu(eNormal);
- t_park_table.set_queue_id(m_dispatch_command_map[m_car_number_optimal].m_queue_id);
- t_park_table.set_car_number(m_dispatch_command_map[m_car_number_optimal].m_car_number);
- t_park_table.set_unit_id(m_dispatch_command_map[m_car_number_optimal].m_unit);
- t_park_table.set_primary_key(m_dispatch_command_map[m_car_number_optimal].m_primary_key);
- t_park_table.mutable_entrance_measure_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg);
- t_park_table.mutable_allocated_space_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg);
- //根据雷达x坐标, 计算入口id
- int t_terminal = 0;
- if (m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()<0)
- {
- t_terminal = m_dispatch_id*2+1;
- }
- else if(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()>0)
- {
- t_terminal = m_dispatch_id*2+2;
- }
- t_park_table.set_terminal_id(t_terminal);
- // std::cout << " huli test :::: " << " iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii m_park_table_msg.entrance_measure_info().cx() = " << m_park_table_msg.entrance_measure_info().cx() << std::endl;
- // std::cout << " huli test :::: " << " iiiiiiiiiiiiiiiiiiiiiiiiiiiiii t_terminal = " << t_terminal << std::endl;
- // LOG(INFO) << " m_park_table_msg.entrance_measure_info().cx() = "<< m_park_table_msg.entrance_measure_info().cx() << " --- " << this;
- // LOG(INFO) << " t_terminal = "<< t_terminal << " --- " << this;
- // LOG(INFO) << " m_device_floor = "<< m_device_floor << " --- " << this;
- // LOG(INFO) << " m_dispatch_id = "<< m_dispatch_id << " --- " << this;
- // LOG(INFO) << " m_unit = "<< m_unit << " --- " << this;
- // LOG(INFO) << " m_outlet_ready = "<< m_outlet_ready << " --- " << this;
- m_park_table_msg = t_park_table;
- return Error_code::SUCCESS;
- }
- //制作取车表单
- Error_manager Dispatch_command::create_pick_table(int outlet_ready)
- {
- pick_table t_pick_table;
- t_pick_table.mutable_statu()->set_execute_statu(eNormal);
- t_pick_table.set_queue_id(m_dispatch_command_map[m_car_number_optimal].m_queue_id);
- t_pick_table.set_car_number(m_dispatch_command_map[m_car_number_optimal].m_car_number);
- t_pick_table.set_unit_id(m_dispatch_command_map[m_car_number_optimal].m_unit);
- t_pick_table.set_primary_key(m_dispatch_command_map[m_car_number_optimal].m_primary_key);
- t_pick_table.mutable_actually_measure_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg);
- t_pick_table.mutable_actually_space_info()->CopyFrom(m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg);
- t_pick_table.set_terminal_id(outlet_ready);
- // LOG(INFO) << " outlet_ready = "<< outlet_ready << " --- " << this;
- //
- // LOG(INFO) << " m_device_floor = "<< m_device_floor << " --- " << this;
- // LOG(INFO) << " m_dispatch_id = "<< m_dispatch_id << " --- " << this;
- // LOG(INFO) << " m_unit = "<< m_unit << " --- " << this;
- // LOG(INFO) << " m_outlet_ready = "<< m_outlet_ready << " --- " << this;
- m_pick_table_msg = t_pick_table;
- return Error_code::SUCCESS;
- }
- //增加 车辆表, 存车指令 完成后添加车辆信息
- Error_manager Dispatch_command::insert_vehicle_for_car_number()
- {
- char insert_vehicle_sql[1024];
- memset(insert_vehicle_sql, 0, 1024);
- sprintf(insert_vehicle_sql, "INSERT INTO vehicle (car_number,primary_key,actually_measure_info) values ('%s','%s','%s')",
- m_car_number_optimal.c_str(), m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
- m_dispatch_command_map[m_car_number_optimal].m_measure_info.c_str() );
- Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
- return ec;
- }
- //20230213 huli add plc car_wheel_base
- Error_manager Dispatch_command::insert_vehicle_for_car_number_ex()
- {
- //20230213 huli add plc car_wheel_base
- measure_info t_plc_measure_info_msg = m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg;
- t_plc_measure_info_msg.set_wheelbase( Dispatch_manager::get_instance_references().m_dispatch_plc.m_plc_car_wheel_base);
- if ( Dispatch_manager::get_instance_references().m_dispatch_plc.m_plc_car_wheel_base ==0 )
- {
- char insert_vehicle_sql[1024];
- memset(insert_vehicle_sql, 0, 1024);
- sprintf(insert_vehicle_sql, "INSERT INTO vehicle (car_number,primary_key,actually_measure_info, plc_clamp_lidar_deviation_1, plc_clamp_lidar_deviation_2, plc_clamp_lidar_deviation_3, plc_clamp_lidar_deviation_4) values ('%s','%s','%s', %f, %f, %f, %f)",
- m_car_number_optimal.c_str(), m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
- m_dispatch_command_map[m_car_number_optimal].m_measure_info.c_str() ,
- Dispatch_manager::get_instance_references().m_dispatch_plc.m_plc_clamp_lidar_deviation_1,
- Dispatch_manager::get_instance_references().m_dispatch_plc.m_plc_clamp_lidar_deviation_2,
- Dispatch_manager::get_instance_references().m_dispatch_plc.m_plc_clamp_lidar_deviation_3,
- Dispatch_manager::get_instance_references().m_dispatch_plc.m_plc_clamp_lidar_deviation_4
- );
- Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
- return ec;
- }
- else
- {
- char insert_vehicle_sql[1024];
- memset(insert_vehicle_sql, 0, 1024);
- sprintf(insert_vehicle_sql, "INSERT INTO vehicle (car_number,primary_key,actually_measure_info, plc_measure_info, plc_clamp_lidar_deviation_1, plc_clamp_lidar_deviation_2, plc_clamp_lidar_deviation_3, plc_clamp_lidar_deviation_4) values ('%s','%s','%s', '%s', %f, %f, %f, %f)",
- m_car_number_optimal.c_str(), m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
- m_dispatch_command_map[m_car_number_optimal].m_measure_info.c_str() ,
- t_plc_measure_info_msg.DebugString().c_str(),
- Dispatch_manager::get_instance_references().m_dispatch_plc.m_plc_clamp_lidar_deviation_1,
- Dispatch_manager::get_instance_references().m_dispatch_plc.m_plc_clamp_lidar_deviation_2,
- Dispatch_manager::get_instance_references().m_dispatch_plc.m_plc_clamp_lidar_deviation_3,
- Dispatch_manager::get_instance_references().m_dispatch_plc.m_plc_clamp_lidar_deviation_4
- );
- Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_vehicle_sql);
- return ec;
- }
- return Error_code::SUCCESS;
- }
- //删除 车辆表, 取车指令 完成后删除车辆信息
- Error_manager Dispatch_command::delete_vehicle_for_car_number()
- {
- char delete_vehicle_sql[1024];
- memset(delete_vehicle_sql, 0, 1024);
- sprintf(delete_vehicle_sql, "delete from vehicle where car_number = '%s'",
- m_dispatch_command_map[m_car_number_optimal].m_car_number.c_str());
- Error_manager ec = Database_controller::get_instance_pointer()->sql_delete(delete_vehicle_sql);
- return ec;
- }
- //增加 记录表, 存车指令 完成后添加存车记录
- Error_manager Dispatch_command::insert_record_for_park_start()
- {
- Time_tool::get_instance_references().time_start(PARK_TIME_FLAG);
- int t_terminal = 0;
- if (m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()<0)
- {
- t_terminal = m_dispatch_id*2+1;
- }
- else if(m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx()>0)
- {
- t_terminal = m_dispatch_id*2+2;
- }
- char insert_record_sql[1024];
- memset(insert_record_sql, 0, 1024);
- // sprintf(insert_record_sql, "INSERT INTO record (primary_key,car_number,in_time_start,measure_info,import_id) values ('%s','%s','%s','%s',%d)",
- sprintf(insert_record_sql, "INSERT INTO record (primary_key,car_number,in_time_start,center_x,center_y,theta,width,height,wheelbase,front_theta,import_id) values ('%s','%s','%s',%f,%f,%f,%f,%f,%f,%f,%d)",
- m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str(),
- m_car_number_optimal.c_str(),
- Time_tool::get_instance_references().get_current_time_seconds().c_str(),
- // m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.id(),
- // m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.DebugString().c_str(),
- // m_dispatch_command_map[m_car_number_optimal].m_measure_info.c_str(),
- m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cx(),
- m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.cy(),
- m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.theta(),
- m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.width(),
- m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.height(),
- m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.wheelbase(),
- m_dispatch_command_map[m_car_number_optimal].m_measure_info_msg.front_theta(),
- t_terminal );
- LOG(INFO) << " insert_record_sql = "<< insert_record_sql << " --- " << this;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_insert(insert_record_sql);
- return ec;
- }
- //增加 记录表, 存车指令 完成后添加存车记录
- Error_manager Dispatch_command::updata_record_for_park_end()
- {
- Time_tool::get_instance_references().time_end(PARK_TIME_FLAG);
- int in_time_difference = Time_tool::get_instance_references().get_time_seconds(PARK_TIME_FLAG);
- //t_index_id是指定单元的车位id, 1~78
- int t_table_id = (m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.id() - 1 ) % 78;
- t_table_id = t_table_id + 1;
- //执行sql操作
- char update_record_sql[1024];
- memset(update_record_sql, 0, 1024);
- // 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' ",
- sprintf(update_record_sql, "update record set in_time_end = '%s', in_time_difference = %d, space_id = %d, table_id = %d, unit_id = %d, floor_id = %d, room_id = %d where primary_key = '%s' ",
- Time_tool::get_instance_references().get_current_time_seconds().c_str(),
- in_time_difference,
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.id(),
- t_table_id,
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.unit_id(),
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.floor(),
- m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.room_id(),
- // m_dispatch_command_map[m_car_number_optimal].m_parkspace_info_msg.DebugString().c_str(),
- m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
- LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
- return ec;
- }
- //更新 记录表, 取车指令 完成后更新取车记录
- Error_manager Dispatch_command::updata_record_for_pick_start()
- {
- Time_tool::get_instance_references().time_start(PICK_TIME_FLAG);
- //执行sql操作
- char update_record_sql[1024];
- memset(update_record_sql, 0, 1024);
- sprintf(update_record_sql, "update record set out_time_start = '%s', export_id = %d where primary_key = '%s' ",
- Time_tool::get_instance_references().get_current_time_seconds().c_str(),
- m_dispatch_command_map[m_car_number_optimal].m_export_id,
- m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
- LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
- return ec;
- }
- //更新 记录表, 取车指令 完成后更新取车记录
- Error_manager Dispatch_command::updata_record_for_pick_end()
- {
- Time_tool::get_instance_references().time_end(PICK_TIME_FLAG);
- int out_time_difference = Time_tool::get_instance_references().get_time_seconds(PICK_TIME_FLAG);
- int parking_time;
- get_parking_time(m_dispatch_command_map[m_car_number_optimal].m_primary_key, parking_time);
- char parking_time_string[256] = "";
- int hour = parking_time / 3600;
- int min = (parking_time / 60)%60;
- int sec = parking_time % 60;
- sprintf(parking_time_string, " %d:%02d:%02d", hour, min, sec);
- //执行sql操作
- char update_record_sql[1024];
- memset(update_record_sql, 0, 1024);
- sprintf(update_record_sql, "update record set out_time_end = '%s', out_time_difference = %d, parking_time = '%s' where primary_key = '%s' ",
- Time_tool::get_instance_references().get_current_time_seconds().c_str(),
- out_time_difference,
- parking_time_string,
- m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
- LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
- return ec;
- }
- //更新 记录表, 取车指令 完成后更新取车记录, 撤销指令, 没有取车时间差
- Error_manager Dispatch_command::updata_record_for_pick_end_ex()
- {
- int parking_time;
- get_parking_time(m_dispatch_command_map[m_car_number_optimal].m_primary_key, parking_time);
- char parking_time_string[256] = "";
- int hour = parking_time / 3600;
- int min = parking_time / 60;
- int sec = parking_time % 60;
- sprintf(parking_time_string, " %d:%02d:%02d", hour, min, sec);
- //执行sql操作
- char update_record_sql[1024];
- memset(update_record_sql, 0, 1024);
- sprintf(update_record_sql, "update record set out_time_end = '%s', parking_time = '%s' where primary_key = '%s' ",
- Time_tool::get_instance_references().get_current_time_seconds().c_str(),
- parking_time_string,
- m_dispatch_command_map[m_car_number_optimal].m_primary_key.c_str() );
- LOG(INFO) << " update_record_sql = "<< update_record_sql << " --- " << this;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_update(update_record_sql);
- return ec;
- }
- //计算汽车在车库的存车时间, 从存车开始到取车结束, 返回 parking_time, 单位秒.
- Error_manager Dispatch_command::get_parking_time(std::string primary_key, int & parking_time )
- {
- //查询存车时间 in_time_start
- char query_record_sql[1024];
- memset(query_record_sql, 0, 1024);
- sprintf(query_record_sql,"select * from record where primary_key = '%s' ", primary_key.c_str() );
- boost::shared_ptr<sql::ResultSet> tp_result = nullptr;
- Error_manager ec = Database_controller::get_instance_pointer()->sql_query(query_record_sql, tp_result);
- if(ec == SUCCESS)
- {
- if(tp_result == nullptr)
- {
- return DB_RESULT_SET_EMPTY;
- }
- //只取第一条结果, 默认是id最小的,
- if (tp_result->next())
- {
- char buf[1024];
- memset(buf, 0, 1024);
- try
- {
- //解析数据
- std::string in_time_start_string = tp_result->getString("in_time_start");
- std::chrono::system_clock::time_point in_time_start = Time_tool::get_instance_references().transform_time_string_seconds(in_time_start_string);
- std::chrono::system_clock::time_point out_time_end = std::chrono::system_clock::now();
- double t_parking_time = (out_time_end - in_time_start).count() / 1000000000;
- parking_time = t_parking_time;
- return Error_code::SUCCESS;
- }
- catch (sql::SQLException &e)
- {
- /* Use what() (derived from std::runtime_error) to fetch the error message */
- sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
- return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
- }
- catch (std::runtime_error &e)
- {
- sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s ", e.what(), __FILE__);
- return Error_manager(DB_RESULT_SET_PARSE_ERROR, NEGLIGIBLE_ERROR, buf);
- }
- }
- else
- {
- return Error_manager(Error_code::DB_NOT_QUERY_EMPTY_PARKSPACE, Error_level::MINOR_ERROR,
- "数据库未查询到空车位 Parkspace_operating_function::query_one_empty_parkspace error ");
- }
- return SUCCESS;
- }
- else
- {
- return ec;
- }
- return Error_code::SUCCESS;
- }
|