1
0

card_car.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 "three_rates.h"
  8. #include "select_tool.h"
  9. #include "websocket/ws_common.h"
  10. #include "special_area.h"
  11. #include "common_tool.h"
  12. #include "tool_time.h"
  13. #include "mine_business.h"
  14. #include "loc_point.h"
  15. #include "module_service/module_call.h"
  16. #include "sys_setting.h"
  17. #include "event.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,double acc)
  37. {
  38. m_acc=acc;
  39. m_area_tool->on_point(shared_from_this(),pt);
  40. m_timeval = m_time;
  41. handle_three_rates(pt);
  42. handle_traffic_light(pt);
  43. if(m_enable_anti_collision){
  44. handle_anti_coll(pt, site->m_id);
  45. }
  46. uint64_t id=tool_other::type_id_to_u64(m_type,m_id);
  47. mine_business::inst()->make_arg(id,pt,m_time);
  48. }
  49. int car::get_vehicle_type_id()
  50. {
  51. return m_vehicle_type_id;
  52. }
  53. void car::handle_anti_coll(const point& pt, const int& sid)
  54. {
  55. // 车卡下发最紧急的呼叫类型
  56. std::map<int, float> cd; // 人卡与车卡的距离,key为人卡id,value为距离
  57. std::map<int, call_card> cards;
  58. auto tmp_cards = card_list::instance()->m_map;
  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. if(c.second->m_type == CT_VEHICLE){
  66. continue;
  67. }
  68. bool s = false;
  69. int d = (c.second->m_timeval >= m_timeval ? (c.second->m_timeval - m_timeval) : (m_timeval - c.second->m_timeval)) / 1000.0;
  70. s = ((c.second->m_timeval >= m_timeval ? (c.second->m_timeval - m_timeval) : (m_timeval - c.second->m_timeval)) /1000.0 <= 30);
  71. float dist = pt.dist(*c.second);
  72. if(dist < k.second && s){
  73. int call_level = 5 - k.first;
  74. log_info("[anti_coll] distance=%.3f, level=%d, thre_value=%.3f, time_diff=%d", dist, call_level, k.second, d);
  75. if(min_d > k.second){
  76. min_d = k.second;
  77. cur_v = dist;
  78. }
  79. auto itc = cd.find(c.second->m_id);
  80. if(itc == cd.end()){
  81. cd.insert(std::make_pair(c.second->m_id, dist));
  82. }else{
  83. itc->second = dist;
  84. }
  85. auto it = cards.find(c.second->m_id);
  86. if(it != cards.end()){
  87. if(c.second->m_call_level > call_level){
  88. c.second->m_call_level = call_level;
  89. }
  90. }else{
  91. 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)));
  92. }
  93. 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);
  94. }
  95. }
  96. }
  97. std::string desc = "";
  98. if(cards.size() == 0){
  99. // delete event from event_list
  100. event_tool::instance()->handle_event(OT_CARD, ET_PERSON_VEHICLE_ANTI_COLLISION, m_id, 0, 0, false);
  101. }else{
  102. size_t i = 0;
  103. for(auto c : cd)
  104. {
  105. desc += std::to_string(c.first);
  106. std::ostringstream buf;
  107. buf<<c.second;
  108. desc += "," + buf.str();
  109. if(i != (cards.size() - 1)){
  110. desc += ";";
  111. }
  112. ++i;
  113. }
  114. log_info("[anti_coll] the distance's list between person and vehicle : %s", desc.c_str());
  115. event_tool::instance()->handle_event(OT_CARD, ET_PERSON_VEHICLE_ANTI_COLLISION, m_id, min_d, cur_v, true, DT_COMMON, desc);
  116. }
  117. if(cards.size() > 0){
  118. // find the best emengency call
  119. int call_level = 6;
  120. for(auto c : cards){
  121. if(c.second.call_level_id < call_level){
  122. call_level = c.second.call_level_id;
  123. }
  124. }
  125. cards.insert(std::make_pair(m_id, call_card(m_id, 2, call_level, CCT_CALL_APOINT, sid)));
  126. }else{
  127. log_info("[anti_coll] no card trigger anti collision rules.");
  128. return;
  129. }
  130. module_call::instance()->send_anti_collision(cards);
  131. }
  132. void car::handle_three_rates(const point &pt)
  133. {
  134. card_pos cp;
  135. m_biz_stat = get_stat();
  136. cp.biz_stat = m_biz_stat;
  137. cp.x = pt.x;
  138. cp.y = pt.y;
  139. cp.z = pt.z;
  140. cp.map_id = m_area_tool->get_mapid();
  141. cp.vibration = m_acc;
  142. put_three_rates(cp);
  143. }
  144. void car::handle_traffic_light(const point& p)
  145. {
  146. card_pos cp;
  147. cp.x = p.x;
  148. cp.y = p.y;
  149. cp.z = p.z;
  150. cp.biz_stat = get_stat();
  151. cp.map_id = m_area_tool->get_mapid();
  152. cp.vibration = m_acc;
  153. put_traffic_light(cp);
  154. }
  155. void car::on_timer()
  156. {
  157. if(!empty())
  158. make_package();
  159. }
  160. int car::get_area()
  161. {
  162. int status = m_biz_stat;
  163. int special_id = -1;
  164. if(status == STATUS_LOST)
  165. {
  166. special_id = special_area_list::instance()->get_special_id(m_id,*this,m_vehicle_category_id);
  167. log_info("enter_special_area:%.2f,%2.f,id:%d,special_area_id:%d",x,y,m_id,special_id);
  168. }
  169. return special_id;
  170. }
  171. void car::make_package()
  172. {
  173. YA::_CARD_POS_ cp;
  174. loc_point pt = getSmoothPoint();
  175. cp.area_info = m_area_tool->m_area_info;
  176. cp.map_id = m_area_tool->get_mapid();
  177. cp.biz_stat = get_stat();
  178. cp.down_time = m_mine_tool->get_down_time();
  179. cp.work_time = m_mine_tool->get_work_time();
  180. cp.is_on_duty= m_mine_tool->is_on_duty();
  181. upt_card_pos(cp, pt);
  182. uint64_t _now = tool_time::now_to_ms();
  183. pt.m_time = _now;
  184. make_his_location(pt.m_time, pt);
  185. uint64_t t = _now>m_timeval?_now-m_timeval:m_timeval-_now;
  186. if(t>10*1000)
  187. {
  188. m_area_tool->on_point(shared_from_this(),pt);
  189. m_biz_stat=get_stat();
  190. }
  191. }
  192. void car::get_card(bool f)
  193. {
  194. if(f)
  195. mine_business::inst()->put(shared_from_this());
  196. }
  197. loc_point car::getSmoothPoint()
  198. {
  199. loc_point lp = m_smo_tool->smooth_strategy();
  200. m_speed = lp.m_speed;
  201. m_stat = lp.m_stat;
  202. lp.y = -lp.y;
  203. return lp;
  204. }