card_car.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include <sstream>
  2. #include "card_car.h"
  3. #include "card_message_handle.h"
  4. #include "his_location.h"
  5. #include "area.h"
  6. #include "mine.h"
  7. #include "select_tool.h"
  8. #include "websocket/ws_common.h"
  9. #include "special_area.h"
  10. #include "common_tool.h"
  11. #include "tool_time.h"
  12. #include "mine_business.h"
  13. #include "loc_point.h"
  14. #include "module_service/module_call.h"
  15. #include "sys_setting.h"
  16. #include "event.h"
  17. #include "struct_def.h"
  18. car::car(const std::string&type,uint32_t cardid,uint16_t needdisplay,int16_t t,int32_t deptid,
  19. int32_t categoryid, int type_id,int32_t level_id,uint32_t cid)
  20. :card_location_base(type,cardid,needdisplay,t,deptid,level_id,cid)
  21. ,m_vehicle_category_id(categoryid)
  22. ,m_vehicle_type_id(type_id)
  23. {
  24. m_message_handle.reset(new card_message_handle(this));
  25. //m_his_location_card.reset(new location_vehicle(m_id,m_type,cid));
  26. }
  27. car::~car(){}
  28. std::shared_ptr<mine_tool> car::get_mine_tool()
  29. {
  30. return m_mine_tool;
  31. }
  32. void car::set_area_info(int mapid,double scale,int areaid,uint64_t t,int type)
  33. {
  34. m_area_tool->set_area_info(mapid, scale, areaid, *this, t, type);
  35. }
  36. void car::do_business(const std::shared_ptr<site>&site,const point &pt)
  37. {
  38. m_area_tool->on_point(shared_from_this(),pt);
  39. m_timeval = m_time;
  40. //handle_traffic_light(pt, site->m_id);
  41. //if(m_enable_anti_collision){
  42. if(CYaSetting::m_sys_setting.m_enable_anti_coll){
  43. handle_anti_coll(pt, site->m_id);
  44. }
  45. uint64_t id=tool_other::type_id_to_u64(m_type,m_id);
  46. mine_business::inst()->make_arg(id,pt,m_time);
  47. }
  48. int car::get_vehicle_type_id()
  49. {
  50. return m_vehicle_type_id;
  51. }
  52. void car::handle_anti_coll(const point& pt, const int& sid)
  53. {
  54. // 车卡下发最紧急的呼叫类型
  55. std::map<int, float> cd; // 人卡与车卡的距离,key为人卡id,value为距离
  56. std::map<int, call_card> cards;
  57. auto tmp_cards = card_list::instance()->m_map;
  58. log_info("[anti_coll] card'size = %d", tmp_cards.size());
  59. double min_d = 9999999.9;
  60. double cur_v = 0.0;
  61. for(auto k : CYaSetting::m_sys_setting.mp_anti_collision){
  62. //log_info("[anti_coll] key=%d, value=%.2f", k.first, k.second);
  63. for(auto& c : tmp_cards)
  64. {
  65. // 车卡不参与车车之间防碰撞
  66. if(c.second->m_type == CT_VEHICLE){
  67. continue;
  68. }
  69. // 司机卡不参与人车之间的防碰撞
  70. if(c.second->m_id>=9000&&c.second->m_id<=9999){
  71. continue;
  72. }
  73. // 避免历史定位数据的影响
  74. uint64_t now = time(0)*1000;
  75. if(m_time - now > 60){
  76. continue;
  77. }
  78. bool s = false;
  79. int d = (c.second->m_timeval >= m_timeval ? (c.second->m_timeval - m_timeval) : (m_timeval - c.second->m_timeval)) / 1000.0;
  80. s = ((c.second->m_timeval >= m_timeval ? (c.second->m_timeval - m_timeval) : (m_timeval - c.second->m_timeval)) /1000.0 <= 30);
  81. //float dist = pt.dist(*c.second);
  82. float dist = pt.dist(c.second->m_v_point);
  83. log_info("[anti_coll] vid=%d, pid=%d, dist=%.2f", m_id, c.second->m_id, dist);
  84. if(dist < k.second && s){
  85. int call_level = 5 - k.first;
  86. log_info("[anti_coll] distance=%.3f, level=%d, thre_value=%.3f, time_diff=%d", dist, call_level, k.second, d);
  87. if(min_d > k.second){
  88. min_d = k.second;
  89. cur_v = dist;
  90. }
  91. auto itc = cd.find(c.second->m_id);
  92. if(itc == cd.end()){
  93. cd.insert(std::make_pair(c.second->m_id, dist));
  94. }else{
  95. itc->second = dist;
  96. }
  97. auto it = cards.find(c.second->m_id);
  98. if(it != cards.end()){
  99. if(c.second->m_call_level > call_level){
  100. c.second->m_call_level = call_level;
  101. }
  102. }else{
  103. cards.insert(std::make_pair(c.second->m_id, call_card(c.second->m_id, c.second->m_type, call_level, CCT_CALL_APOINT, sid)));
  104. }
  105. log_info("[anti_coll] card_id=%d, ctype=%d, call_level=%d, call_type_id=%d, site_id=%d",c.second->m_id, c.second->m_type, call_level, CCT_CALL_APOINT, sid);
  106. }
  107. }
  108. }
  109. std::string desc = "";
  110. uint64_t id = tool_other::type_id_to_u64(m_type, m_id);
  111. if(cards.size() == 0){
  112. // delete event from event_list
  113. event_tool::instance()->handle_event(OT_CARD, ET_PERSON_VEHICLE_ANTI_COLLISION, id, 0, 0, false);
  114. }else{
  115. size_t i = 0;
  116. for(auto c : cd)
  117. {
  118. desc += std::to_string(c.first);
  119. std::ostringstream buf;
  120. buf<<c.second;
  121. desc += "," + buf.str();
  122. if(i != (cards.size() - 1)){
  123. desc += ";";
  124. }
  125. ++i;
  126. }
  127. log_info("[anti_coll] the distance's list between person and vehicle : %s", desc.c_str());
  128. event_tool::instance()->handle_event(OT_CARD, ET_PERSON_VEHICLE_ANTI_COLLISION, id, min_d, cur_v, true, DT_COMMON, desc);
  129. }
  130. if(cards.size() > 0){
  131. // find the best emengency call
  132. int call_level = 6;
  133. for(auto c : cards){
  134. if(c.second.call_level_id < call_level){
  135. call_level = c.second.call_level_id;
  136. }
  137. }
  138. cards.insert(std::make_pair(m_id, call_card(m_id, 2, call_level, CCT_CALL_APOINT, sid)));
  139. module_call::instance()->send_anti_collision(cards);
  140. }else{
  141. //log_info("[anti_coll] no card trigger anti collision rules.");
  142. return;
  143. }
  144. //module_call::instance()->send_anti_collision(cards);
  145. }
  146. /*
  147. * @brief 红绿灯业务模块
  148. * @param const point& p 定位数据
  149. * @param const int& sid 分站id
  150. * @return 无
  151. * @note
  152. * @warning
  153. * @bug
  154. * */
  155. void car::handle_traffic_light(const point& p, const int& sid)
  156. {
  157. card_pos cp;
  158. cp.x = p.x;
  159. cp.y = p.y;
  160. cp.z = p.z;
  161. cp.biz_stat = get_stat();
  162. cp.map_id = m_area_tool->get_mapid();
  163. cp.vibration = m_acc;
  164. cp.reader_id = sid;
  165. put_traffic_light(cp);
  166. }
  167. void car::on_timer()
  168. {
  169. if(!empty())
  170. make_package();
  171. }
  172. int car::get_area()
  173. {
  174. int status = m_biz_stat;
  175. int special_id = -1;
  176. if(status == STATUS_LOST)
  177. {
  178. special_id = special_area_list::instance()->get_special_id(m_id,*this,m_vehicle_category_id);
  179. log_info("enter_special_area:%.2f,%2.f,id:%d,special_area_id:%d",x,y,m_id,special_id);
  180. }
  181. return special_id;
  182. }
  183. void car::make_package()
  184. {
  185. sys::_CARD_POS_ cp;
  186. loc_point pt = getSmoothPoint();
  187. cp.area_info = m_area_tool->m_area_info;
  188. cp.map_id = m_area_tool->get_mapid();
  189. cp.biz_stat = get_stat();
  190. //cp.down_time = m_mine_tool->get_down_time();
  191. cp.work_time = m_mine_tool->get_work_time();
  192. cp.is_on_duty= m_mine_tool->is_on_duty();
  193. cp.down_time = m_last_point.x;
  194. cp.z = m_last_point.y;
  195. upt_card_pos(cp, pt);
  196. uint64_t _now = tool_time::now_to_ms();
  197. pt.m_time = _now;
  198. make_his_location(pt.m_time, pt);
  199. uint64_t t = _now>m_timeval?_now-m_timeval:m_timeval-_now;
  200. if(t>10*1000)
  201. {
  202. m_area_tool->on_point(shared_from_this(),pt);
  203. m_biz_stat=get_stat();
  204. }
  205. }
  206. void car::get_card(bool f)
  207. {
  208. if(f)
  209. mine_business::inst()->put(shared_from_this());
  210. }
  211. loc_point car::getSmoothPoint()
  212. {
  213. loc_point lp = m_smo_tool->smooth_strategy();
  214. m_speed = lp.m_speed;
  215. m_stat = lp.m_stat;
  216. lp.y = -lp.y;
  217. return lp;
  218. }