drivingface_alarm.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "drivingface_alarm.h"
  2. #include <log.h>
  3. #include "db_api/CDBResultSet.h"
  4. #include "db_api/CDBSingletonDefine.h"
  5. void DrivingFaceAlarm::init(int drivingface_id/* = 0*/)
  6. {
  7. init_drivingface_db(drivingface_id);
  8. m_send_callback = nullptr;
  9. }
  10. void DrivingFaceAlarm::init_drivingface_db(int drivingface_id/* = 0*/ )
  11. {
  12. 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";
  13. if (drivingface_id > 0)
  14. {
  15. sql += " and b.drivingface_id = " + std::to_string(drivingface_id) ;
  16. }
  17. sql.append(";");
  18. std::string Error;
  19. YADB::CDBResultSet DBRes;
  20. sDBConnPool.Query(sql.c_str(), DBRes, Error);
  21. int nCount = DBRes.GetRecordCount( Error );
  22. if (nCount < 1)
  23. {
  24. logn_error(2,"增加或修改失败,数据库中找不到: sql=%s", sql.c_str());
  25. return;
  26. }
  27. while (DBRes.GetNextRecod(Error))
  28. {
  29. int db_drivingface_id = 0;
  30. DBRes.GetField("drivingface_id", db_drivingface_id, Error);
  31. DrivingFaceInfo tmp_driving_face;
  32. DBRes.GetField("vehicle_id", tmp_driving_face.vehicle_id, Error);
  33. DBRes.GetField("vehicle_type_id", tmp_driving_face.vehicle_type_id, Error);
  34. DBRes.GetField("warning_threshold", tmp_driving_face.warning_threshold, Error);
  35. m_map_drivingface[tmp_driving_face.vehicle_id] = tmp_driving_face;
  36. logn_info(2,"init_drivingface_db: drivingface=%d vehicle=%d-%d warning_threshold=%.2f"
  37. ,db_drivingface_id,tmp_driving_face.vehicle_type_id,tmp_driving_face.vehicle_id,tmp_driving_face.warning_threshold);
  38. }
  39. }
  40. void DrivingFaceAlarm::del_drivingface(int drivingface_id )
  41. {
  42. auto it = m_map_drivingface.find(drivingface_id);
  43. if (it != m_map_drivingface.end())
  44. {
  45. it->second.clean();
  46. }
  47. logn_info(2,"DrivingFaceAlarm::del_drivingface: drivingface=%d ",drivingface_id);
  48. }
  49. void DrivingFaceAlarm::driving_alarm(DrivingCard &card)
  50. {
  51. map<int, DrivingFaceInfo>::iterator itmap = m_map_drivingface.find(card.vehicle_id);
  52. if (itmap == m_map_drivingface.end())
  53. {
  54. return;
  55. }
  56. point p1(card.x, card.y);
  57. point p2(card.reader_x, card.reader_y);
  58. if(p1.empty() || p2.empty())
  59. {
  60. return;
  61. }
  62. double dist = p1.dist(p2) * card.map_scale;
  63. if (!itmap->second.bWarning && dist > itmap->second.warning_threshold)
  64. {
  65. //超过距离有50次,产生报警
  66. itmap->second.warning_count++;
  67. if(itmap->second.warning_count >= 50)
  68. {
  69. itmap->second.distance = dist;
  70. itmap->second.warning_count = 0;
  71. itmap->second.bWarning = true;
  72. uint64_t id = ((uint64_t)card.card_type<<32)|card.vehicle_card_id;
  73. if (m_send_callback)
  74. m_send_callback(OT_CARD, ET_CARD_DRIVINGFACE_WARNING_AREA, id,
  75. itmap->second.warning_threshold, dist, itmap->second.bWarning);
  76. }
  77. }
  78. else if (itmap->second.bWarning && dist <= itmap->second.warning_threshold)
  79. {
  80. //没有超过距离50次,取消报警
  81. itmap->second.warning_count--;
  82. if (itmap->second.warning_count < 50)
  83. {
  84. itmap->second.distance = 0;
  85. itmap->second.warning_count = 0;
  86. itmap->second.bWarning = false;
  87. uint64_t id = ((uint64_t)card.card_type<<32)|card.vehicle_card_id;
  88. if (m_send_callback)
  89. m_send_callback(OT_CARD, ET_CARD_DRIVINGFACE_WARNING_AREA, id,
  90. itmap->second.warning_threshold, dist, itmap->second.bWarning);
  91. }
  92. }
  93. }