1
0

area_business_geofault.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "area_business_geofault.h"
  2. #include <memory>
  3. #include <string>
  4. #include "area_business.h"
  5. #include "event.h"
  6. #include "db_api/CDBSingletonDefine.h"
  7. #include "sys_setting.h"
  8. #include "common_tool.h"
  9. #include "log.h"
  10. #include "card.h"
  11. #include "config_file.h"
  12. #include "point.h"
  13. #include "area.h"
  14. struct geofault_data:business_data
  15. {
  16. geofault_data()
  17. {
  18. m_near_geofault_count=0;
  19. m_far_geofault_count=0;
  20. m_is_warning=false;
  21. }
  22. int m_near_geofault_count;
  23. int m_far_geofault_count;
  24. bool m_is_warning;
  25. };
  26. std::unordered_map<int, std::vector<point>> area_business_geofault::_area_geofault_map;
  27. int area_business_geofault::_geofault_count_limit;
  28. void area_business_geofault::on_load_his(const std::shared_ptr<area_hover>&area_hover_ptr,
  29. const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>&ptr)
  30. {
  31. on_enter(area_hover_ptr, card_ptr, ptr);
  32. }
  33. void area_business_geofault::on_enter(const std::shared_ptr<area_hover>&area_hover_ptr,
  34. const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>& ptr)
  35. {
  36. if(!tool_other::is_coal_or_driving(card_ptr->m_type,card_ptr->get_vehicle_type_id()))
  37. {
  38. return;
  39. }
  40. auto ptr_temp = std::make_shared<geofault_data>();
  41. ptr = ptr_temp;
  42. double dist=0;
  43. if(_is_near(area_hover_ptr->m_area, card_ptr->x, card_ptr->y, dist))
  44. {
  45. ptr_temp->m_near_geofault_count++;
  46. }
  47. else
  48. {
  49. ptr_temp->m_far_geofault_count++;
  50. }
  51. auto ev_ptr_temp = event_list::instance()->get_event_card(card_ptr->m_id, card_ptr->m_type, ET_VEHICLE_NEAR_GEOFAULT,DT_COMMON);
  52. ptr_temp->m_is_warning = (nullptr != ev_ptr_temp && !ev_ptr_temp->is_end());
  53. }
  54. void area_business_geofault::on_hover(const std::shared_ptr<area_hover>&area_hover_ptr,
  55. const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
  56. {
  57. if(!tool_other::is_coal_or_driving(card_ptr->m_type,card_ptr->get_vehicle_type_id()))
  58. {
  59. return;
  60. }
  61. if(nullptr == ptr)
  62. {
  63. log_error("area_business_geofault::on_hover:nullptr == ptr");
  64. return;
  65. }
  66. auto ptr_temp = static_cast<geofault_data*>(ptr.get());
  67. double dist=0;
  68. if(_is_near(area_hover_ptr->m_area, card_ptr->x, card_ptr->y, dist))
  69. {
  70. ptr_temp->m_far_geofault_count=0;
  71. ptr_temp->m_near_geofault_count++;
  72. }
  73. else
  74. {
  75. ptr_temp->m_near_geofault_count=0;
  76. ptr_temp->m_far_geofault_count++;
  77. }
  78. //确定告警
  79. if(_geofault_count_limit <= ptr_temp->m_near_geofault_count)
  80. {
  81. ptr_temp->m_near_geofault_count=_geofault_count_limit;
  82. ptr_temp->m_is_warning = true;
  83. uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
  84. event_tool::instance()->handle_event(OT_CARD, ET_VEHICLE_NEAR_GEOFAULT, id,
  85. CYaSetting::m_sys_setting.geofault_warn_dis, dist, true);
  86. }
  87. //确定正常
  88. if(_geofault_count_limit <= ptr_temp->m_far_geofault_count)
  89. {
  90. ptr_temp->m_far_geofault_count=_geofault_count_limit;
  91. if(ptr_temp->m_is_warning)
  92. {
  93. ptr_temp->m_is_warning = false;
  94. uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
  95. event_tool::instance()->handle_event(OT_CARD, ET_VEHICLE_NEAR_GEOFAULT, id,
  96. CYaSetting::m_sys_setting.geofault_warn_dis, dist, false);
  97. }
  98. }
  99. }
  100. void area_business_geofault::on_leave(const std::shared_ptr<area_hover>&area_hover_ptr,
  101. const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
  102. {
  103. if(!tool_other::is_coal_or_driving(card_ptr->m_type,card_ptr->get_vehicle_type_id()))
  104. {
  105. return;
  106. }
  107. if(nullptr == ptr)
  108. {
  109. log_error("area_business_geofault::on_leave:nullptr == ptr");
  110. return;
  111. }
  112. uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
  113. event_tool::instance()->handle_event(OT_CARD, ET_VEHICLE_NEAR_GEOFAULT, id, CYaSetting::m_sys_setting.geofault_warn_dis, 1000, false);
  114. }
  115. void area_business_geofault::init(config_file& config)
  116. {
  117. _geofault_count_limit = std::stoi(config.get("service.geofault_count_limit","10"));
  118. }
  119. std::string area_business_geofault::_points2str(std::vector<point>& points)
  120. {
  121. std::string str;
  122. for(auto&p : points)
  123. {
  124. char ch[64] = {0};
  125. sprintf(ch, "x=%f,y=%f;", p.x, p.y);
  126. str.append(ch);
  127. }
  128. return str;
  129. }
  130. void area_business_geofault::init_geofault_from_db()
  131. {
  132. const char *sql = "select area_id, pt_data from dat_geofault g, dat_work_face w \
  133. where g.workface_id=w.work_face_id;";
  134. std::string Error;
  135. YADB::CDBResultSet DBRes;
  136. sDBConnPool.Query(sql,DBRes,Error);
  137. if(!Error.empty())
  138. {
  139. log_error("init_geofault_from_db Error,%s",Error.c_str());
  140. return;
  141. }
  142. uint64_t nCount = DBRes.GetRecordCount( Error );
  143. if (static_cast<int64_t>(nCount) < 0)
  144. {
  145. log_error("错误,init_geofault_from_db. The record count=%ld\n", nCount );
  146. return;
  147. }
  148. log_info( "init_geofault_from_db. The record count=%ld\n", nCount );
  149. _area_geofault_map.clear();
  150. while ( DBRes.GetNextRecod(Error) )
  151. {
  152. int area_id = 0;
  153. DBRes.GetField( "area_id",area_id, Error );
  154. std::string pt_data;
  155. DBRes.GetField( "pt_data",pt_data, Error );
  156. std::vector<point> points;
  157. std::vector<std::string> strs = split(pt_data,';');
  158. for (const auto & s:strs)
  159. {
  160. std::vector<std::string> xy = split(s, ',');
  161. if (xy.size()!=2)
  162. {
  163. continue;
  164. }
  165. point pt;
  166. pt.x = std::atof(xy[0].c_str());
  167. pt.y = std::atof(xy[1].c_str());
  168. points.push_back(pt);
  169. }
  170. _area_geofault_map.insert({area_id,points});
  171. }
  172. for(const auto &p : _area_geofault_map)
  173. {
  174. auto kk = p.second;
  175. std_debug("init_geofault_from_db:area_id:%d--points: %s",p.first, _points2str(kk).c_str());
  176. log_info("init_geofault_from_db:area_id:%d--points: %s",p.first, _points2str(kk).c_str());
  177. }
  178. }
  179. bool area_business_geofault::_is_near(std::shared_ptr<area>& area_ptr, double x, double y, double& out_dist)
  180. {
  181. auto serch = _area_geofault_map.find(area_ptr->m_id);
  182. if(serch == _area_geofault_map.end())
  183. {
  184. return false;
  185. }
  186. point pt(x, y);
  187. double min=0;
  188. out_dist=1000000;
  189. for(auto& p : serch->second)
  190. {
  191. min = pt.dist(p) * area_ptr->m_scale;
  192. if(min < out_dist)
  193. {
  194. out_dist = min;
  195. }
  196. if(min < CYaSetting::m_sys_setting.geofault_warn_dis)
  197. {
  198. return true;
  199. }
  200. }
  201. return false;
  202. }