#include "vehicle_driver_alarm.h" #include "db_api/CDBSingletonDefine.h" #include "log.h" #include "tr_tool.h" #include #include #include "three_rates.h" vehicle_driver_alarm::vehicle_driver_alarm() { } void vehicle_driver_alarm::init() { init_coalface_driver_warningparam(); init_driving_driver_warningparam(); } void vehicle_driver_alarm::init_coalface_driver_warningparam() { std::string sql ="SELECT ve.vehicle_id,ve.dept_id,b.coalface_id , b.driver_dist,b.driver_dist_time" " from dat_vehicle_extend ve left join dat_coalface_vehicle b on ve.vehicle_id = b.vehicle_id " " where b.vehicle_id = ve.vehicle_id"; std::string Error; YADB::CDBResultSet DBRes; sDBConnPool.Query(sql.c_str(),DBRes,Error); int nCount = DBRes.GetRecordCount( Error ); if (nCount > 0) { while ( DBRes.GetNextRecod(Error) ) { int vehicle_id = -1; if (!DBRes.GetField( "vehicle_id",vehicle_id, Error )) { continue; } uint32_t dept_id = 0; if (!DBRes.GetField( "dept_id",dept_id, Error )) { continue; } int coalface_id = -1; if (!DBRes.GetField( "coalface_id",coalface_id, Error )) { continue; } double driver_dist = 0; if (!DBRes.GetField( "driver_dist",driver_dist, Error )) { continue; } int driver_dist_time = 0; if (!DBRes.GetField( "driver_dist_time",driver_dist_time, Error )) { continue; } std::shared_ptr pWorkfaceParam = nullptr; WorkfaceParamMap::iterator it_f= m_WorkfaceParamMap.find(coalface_id); if (it_f == m_WorkfaceParamMap.end()) { pWorkfaceParam = std::make_shared(); m_WorkfaceParamMap.insert(make_pair(coalface_id, pWorkfaceParam)); } else { pWorkfaceParam = it_f->second; } pWorkfaceParam->dist = driver_dist; pWorkfaceParam->dist_time = driver_dist_time; std::shared_ptr pos= nullptr; VehiclePosMap::iterator it_p =m_CoalVehiclePosMap.find(vehicle_id); if (it_p == m_CoalVehiclePosMap.end()) { pos= std::make_shared(); m_CoalVehiclePosMap.insert(std::make_pair(vehicle_id,pos)); } else { pos = it_p->second; } pos->dept_id =dept_id; pos->workface_id = coalface_id; pos->starttime =time(NULL); pos->on_vehicle_state_changed(false); } } else { logn_error(2,"采煤面车卡与司机距离告警查询失败,sql=%s,", sql.c_str()); } } void vehicle_driver_alarm::init_driving_driver_warningparam(const std::string & sz_drivingface_id/* = "-1"*/) { std::string sql ="SELECT ve.vehicle_id,ve.dept_id,b.drivingface_id , b.driver_dist,b.driver_dist_time" " from dat_vehicle_extend ve left join dat_drivingface_vehicle b on ve.vehicle_id = b.vehicle_id " " where b.vehicle_id = ve.vehicle_id"; if (std::atoi(sz_drivingface_id.c_str()) > 0) { sql += " and b.drivingface_id = " + sz_drivingface_id ; } sql.append(" ;"); std::string Error; YADB::CDBResultSet DBRes; sDBConnPool.Query(sql.c_str(),DBRes,Error); int nCount = DBRes.GetRecordCount( Error ); if (nCount > 0) { while ( DBRes.GetNextRecod(Error) ) { int vehicle_id = -1; if (!DBRes.GetField( "vehicle_id",vehicle_id, Error )) { continue; } uint32_t dept_id = 0; if (!DBRes.GetField( "dept_id",dept_id, Error )) { continue; } int db_drivingface_id = -1; if (!DBRes.GetField( "drivingface_id",db_drivingface_id, Error )) { continue; } double driver_dist = 0; if (!DBRes.GetField( "driver_dist",driver_dist, Error )) { continue; } int driver_dist_time = 0; if (!DBRes.GetField( "driver_dist_time",driver_dist_time, Error )) { continue; } std::shared_ptr pWorkfaceParam = nullptr; WorkfaceParamMap::iterator it_f= m_WorkfaceParamMap.find(db_drivingface_id); if (it_f == m_WorkfaceParamMap.end()) { pWorkfaceParam = std::make_shared(); m_WorkfaceParamMap.insert(make_pair(db_drivingface_id, pWorkfaceParam)); } else { pWorkfaceParam = it_f->second; } pWorkfaceParam->dist = driver_dist; pWorkfaceParam->dist_time = driver_dist_time; std::shared_ptr pos= nullptr; VehiclePosMap::iterator it_p =m_DrivingVehiclePosMap.find(vehicle_id); if (it_p == m_DrivingVehiclePosMap.end()) { pos= std::make_shared(); m_DrivingVehiclePosMap.insert(std::make_pair(vehicle_id,pos)); } else { pos = it_p->second; } pos->dept_id =dept_id; pos->workface_id = db_drivingface_id; pos->on_vehicle_state_changed(false); } } else { logn_error(2,"挖掘面车卡与司机距离告警查询失败,sql=%s,", sql.c_str()); } logn_info(2,"init_driving_driver_warningparam: drivingface=%s,sqlcount=%d sql=%s" ,sz_drivingface_id.c_str(),nCount,sql.c_str()); } std::shared_ptr vehicle_driver_alarm::get_vehicle_pos(int _id,int _type) { #define GET_VEHICLE_POS_D(M,_ID) \ VehiclePosMap::iterator it_p = M.find(_ID);\ if (it_p != M.end()){\ return it_p->second;\ }\ return nullptr;\ if (tr_helper::is_coalminingface(_type) ) { GET_VEHICLE_POS_D(m_CoalVehiclePosMap,_id); } else { GET_VEHICLE_POS_D(m_DrivingVehiclePosMap,_id); } #undef GET_VEHICLE_POS_D return nullptr; } void vehicle_driver_alarm::deal_vehicle_driver_alarm(SDeal_Card_Pos & cp) { if (cp.type == THREE_RATES_CARD_TYPE::CT_PERSON && (cp.work_type_id == E_THREE_VEHICLE_TYPE::E_COALMINING_DRIVER || cp.work_type_id == E_THREE_VEHICLE_TYPE::E_DRIVING_DRIVER )) { DriverPosMap::iterator it_p =m_DriverPosMap.find(cp.identifier_id); std::shared_ptr driverPos =nullptr; if (it_p != m_DriverPosMap.end()) { driverPos = it_p->second; } else { driverPos = std::make_shared(); m_DriverPosMap.insert(std::make_pair(cp.identifier_id,driverPos)); driverPos->dept_id = 0; driverPos->on_vehicle_state_changed(false); } if (driverPos->dept_id != cp.dpt_id) { add_driver_to_vehicle(cp,driverPos); } if (driverPos->isOn ) { driverPos->calc_vehicle_driver_pos(cp.x,cp.y); } } else if (tr_helper::is_coalminingface(cp.dentifier_type) || tr_helper::is_drivingface(cp.dentifier_type)) { std::shared_ptr vehiclePos_ptr = get_vehicle_pos(cp.identifier_id,cp.dentifier_type); if(nullptr == vehiclePos_ptr) { return; } bool statechange =false; bool bcalc=false; std::shared_ptr faceParam = nullptr; WorkfaceParamMap::iterator it_v = m_WorkfaceParamMap.find(vehiclePos_ptr->workface_id); if (it_v != m_WorkfaceParamMap.end()) { faceParam = it_v->second; } else { return; } bool m_bOpen = three_rates::get_instance()->GetVehicleState(cp.identifier_id); if (m_bOpen^vehiclePos_ptr->isOn) { vehiclePos_ptr->isOn = m_bOpen; vehiclePos_ptr->reset_vehicle_driver_pos(); statechange= true; } if (m_bOpen ) { vehiclePos_ptr->calc_vehicle_driver_pos(cp.x,cp.y); int dist_time=faceParam ->dist_time; if (time(NULL)- vehiclePos_ptr->starttime > dist_time) { bcalc =true; } } if (statechange || bcalc) { uint64_t id = cp.type<<32|cp.id; double mean_vx= 0; double mean_vy= 0; if (bcalc) { if (vehiclePos_ptr->PointNum >0) { mean_vx =vehiclePos_ptr->Sum_x/vehiclePos_ptr->PointNum; mean_vy = vehiclePos_ptr->Sum_y/vehiclePos_ptr->PointNum; vehiclePos_ptr->reset_vehicle_driver_pos(); } } double minDistance = FLT_MAX; std::vector& drivelist = vehiclePos_ptr->m_drivelist; if (drivelist.size()<=0) { //开始报警 if (m_send_callback && bcalc) { m_send_callback(9, ET_COALING_DRIVING_DRIVER, id, faceParam->dist, minDistance, true); //log_error("工作车开机,但没有司机,id=%l,", id); } return; } double distance= 0; for (size_t i =0;i < drivelist.size();i++) { if (bcalc) { DriverPosMap::iterator it_p =m_DriverPosMap.find(drivelist[i]); if (it_p != m_DriverPosMap.end()) { if (it_p->second->calc_vehicle_driver_distance(mean_vx,mean_vy,distance)) { if (distance < minDistance) { minDistance = distance; } } } } if (statechange) { vehiclePos_ptr->on_vehicle_state_changed(m_bOpen); } } if (bcalc) { if (minDistance > faceParam->dist) { //开始报警 if (m_send_callback) { m_send_callback(9, ET_COALING_DRIVING_DRIVER, id, faceParam->dist, minDistance, true); } //log_info("工作车和司机距离超过门限,开始告警,id=%l,", id); } else { if (m_send_callback) { //结束报警 m_send_callback(9, ET_COALING_DRIVING_DRIVER, id, faceParam->dist, minDistance, false); //log_info("工作车和司机距离未超过门限,结束告警,id=%l,", id); } } } if (statechange && !m_bOpen) { if (m_send_callback) { //结束报警 m_send_callback(9, ET_COALING_DRIVING_DRIVER, id, faceParam->dist, 0, false); //log_info("工作车关机,结束车卡与司机距离告警,id=%l,", id); } } } } } void vehicle_driver_alarm::add_driver_to_vehicle(SDeal_Card_Pos &cp, std::shared_ptr driverPos) { if (cp.work_type_id == E_THREE_VEHICLE_TYPE::E_COALMINING_DRIVER) { add_driver_to_vehicle(m_CoalVehiclePosMap,cp,driverPos); } else if (cp.work_type_id == E_THREE_VEHICLE_TYPE::E_DRIVING_DRIVER) { add_driver_to_vehicle(m_DrivingVehiclePosMap,cp,driverPos); } } void vehicle_driver_alarm::add_driver_to_vehicle(VehiclePosMap& vehiclePosMap, SDeal_Card_Pos &cp,std::shared_ptr driverPos) { VehiclePosMap::iterator it_s = vehiclePosMap.begin(); bool badd = false; bool breduce = false; uint32_t & dept_id = driverPos->dept_id ; if (dept_id == 0) { breduce =true; } while (it_s != vehiclePosMap.end() ) { if (it_s->second->dept_id == cp.dpt_id) { if (!badd) { std::vector& driverlist = it_s->second->m_drivelist; std::vector::iterator it_d = std::find(driverlist.begin(),driverlist.end(),cp.identifier_id); if (it_d == driverlist.end()) { driverlist.push_back(cp.identifier_id); driverPos->isOn = it_s->second->isOn ; driverPos->dept_id = it_s->second->dept_id ; badd =true; } } } else if (it_s->second->dept_id == dept_id) { if (!breduce) { std::vector& driverlist = it_s->second->m_drivelist; std::vector::iterator it_d = std::find(driverlist.begin(),driverlist.end(),cp.identifier_id); if (it_d != driverlist.end()) { driverlist.erase(it_d); breduce = true; } } } if (badd && breduce) { return; } it_s ++; } } void DriverPos::calc_vehicle_driver_pos(double x,double y) { if (isOn) { if (PointNum == 0) { starttime = time(NULL); } Sum_x += x; Sum_y += y; PointNum++; } } /*重置记录的位置信息 * * param * * return * 是否计算成功,true为成功,false失败,没有记录位置点 */ void DriverPos::reset_vehicle_driver_pos() { PointNum = 0; Sum_x = 0; Sum_y =0; } bool DriverPos::calc_vehicle_driver_distance(double mean_vx,double mean_vy,double& distance) { if (PointNum >0 ) { double meanx = Sum_x/PointNum - mean_vx; double meany = Sum_y/PointNum - mean_vy; //double meanz = driverPos->Sum_z/driverPos->PointNum - mean_vz; distance = sqrt(meanx * meanx + meany * meany ); reset_vehicle_driver_pos(); return true; } return false; } void DriverPos::on_vehicle_state_changed(bool m_bOpen) { isOn = m_bOpen; reset_vehicle_driver_pos(); }