#include "drivingface_alarm.h" #include #include "db_api/CDBResultSet.h" #include "db_api/CDBSingletonDefine.h" void DrivingFaceAlarm::init(int drivingface_id/* = 0*/) { init_drivingface_db(drivingface_id); m_send_callback = nullptr; } void DrivingFaceAlarm::init_drivingface_db(int drivingface_id/* = 0*/ ) { std::string sql = "SELECT b.drivingface_id, a.vehicle_id, a.vehicle_type_id, b.warning_threshold FROM dat_vehicle a, dat_drivingface_vehicle b where vehicle_type_id = 26 and a.vehicle_id = b.vehicle_id"; if (drivingface_id > 0) { sql += " and b.drivingface_id = " + std::to_string(drivingface_id) ; } sql.append(";"); std::string Error; YADB::CDBResultSet DBRes; sDBConnPool.Query(sql.c_str(), DBRes, Error); int nCount = DBRes.GetRecordCount( Error ); if (nCount < 1) { logn_error(2,"增加或修改失败,数据库中找不到: sql=%s", sql.c_str()); return; } while (DBRes.GetNextRecod(Error)) { int db_drivingface_id = 0; DBRes.GetField("drivingface_id", db_drivingface_id, Error); DrivingFaceInfo tmp_driving_face; DBRes.GetField("vehicle_id", tmp_driving_face.vehicle_id, Error); DBRes.GetField("vehicle_type_id", tmp_driving_face.vehicle_type_id, Error); DBRes.GetField("warning_threshold", tmp_driving_face.warning_threshold, Error); m_map_drivingface[tmp_driving_face.vehicle_id] = tmp_driving_face; logn_info(2,"init_drivingface_db: drivingface=%d vehicle=%d-%d warning_threshold=%.2f" ,db_drivingface_id,tmp_driving_face.vehicle_type_id,tmp_driving_face.vehicle_id,tmp_driving_face.warning_threshold); } } void DrivingFaceAlarm::del_drivingface(int drivingface_id ) { auto it = m_map_drivingface.find(drivingface_id); if (it != m_map_drivingface.end()) { it->second.clean(); } logn_info(2,"DrivingFaceAlarm::del_drivingface: drivingface=%d ",drivingface_id); } void DrivingFaceAlarm::driving_alarm(DrivingCard &card) { map::iterator itmap = m_map_drivingface.find(card.vehicle_id); if (itmap == m_map_drivingface.end()) { return; } point p1(card.x, card.y); point p2(card.reader_x, card.reader_y); if(p1.empty() || p2.empty()) { return; } double dist = p1.dist(p2) * card.map_scale; if (!itmap->second.bWarning && dist > itmap->second.warning_threshold) { //超过距离有50次,产生报警 itmap->second.warning_count++; if(itmap->second.warning_count >= 50) { itmap->second.distance = dist; itmap->second.warning_count = 0; itmap->second.bWarning = true; uint64_t id = ((uint64_t)card.card_type<<32)|card.vehicle_card_id; if (m_send_callback) m_send_callback(OT_CARD, ET_CARD_DRIVINGFACE_WARNING_AREA, id, itmap->second.warning_threshold, dist, itmap->second.bWarning); } } else if (itmap->second.bWarning && dist <= itmap->second.warning_threshold) { //没有超过距离50次,取消报警 itmap->second.warning_count--; if (itmap->second.warning_count < 50) { itmap->second.distance = 0; itmap->second.warning_count = 0; itmap->second.bWarning = false; uint64_t id = ((uint64_t)card.card_type<<32)|card.vehicle_card_id; if (m_send_callback) m_send_callback(OT_CARD, ET_CARD_DRIVINGFACE_WARNING_AREA, id, itmap->second.warning_threshold, dist, itmap->second.bWarning); } } }