card_car.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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, site->m_id);
  43. //if(m_enable_anti_collision){
  44. if(CYaSetting::m_sys_setting.m_enable_anti_coll){
  45. handle_anti_coll(pt, site->m_id);
  46. }
  47. uint64_t id=tool_other::type_id_to_u64(m_type,m_id);
  48. mine_business::inst()->make_arg(id,pt,m_time);
  49. }
  50. int car::get_vehicle_type_id()
  51. {
  52. return m_vehicle_type_id;
  53. }
  54. void car::handle_anti_coll(const point& pt, const int& sid)
  55. {
  56. // 车卡下发最紧急的呼叫类型
  57. std::map<int, float> cd; // 人卡与车卡的距离,key为人卡id,value为距离
  58. std::map<int, call_card> cards;
  59. auto tmp_cards = card_list::instance()->m_map;
  60. log_info("[anti_coll] card'size = %d", tmp_cards.size());
  61. double min_d = 9999999.9;
  62. double cur_v = 0.0;
  63. for(auto k : CYaSetting::m_sys_setting.mp_anti_collision){
  64. //log_info("[anti_coll] key=%d, value=%.2f", k.first, k.second);
  65. for(auto& c : tmp_cards)
  66. {
  67. // 车卡不参与车车之间防碰撞
  68. if(c.second->m_type == CT_VEHICLE){
  69. continue;
  70. }
  71. // 司机卡不参与人车之间的防碰撞
  72. if(c.second->m_id>=9000&&c.second->m_id<=9999){
  73. continue;
  74. }
  75. // 避免历史定位数据的影响
  76. uint64_t now = time(0)*1000;
  77. if(m_time - now > 60){
  78. continue;
  79. }
  80. bool s = false;
  81. int d = (c.second->m_timeval >= m_timeval ? (c.second->m_timeval - m_timeval) : (m_timeval - c.second->m_timeval)) / 1000.0;
  82. s = ((c.second->m_timeval >= m_timeval ? (c.second->m_timeval - m_timeval) : (m_timeval - c.second->m_timeval)) /1000.0 <= 30);
  83. //float dist = pt.dist(*c.second);
  84. float dist = pt.dist(c.second->m_v_point);
  85. log_info("[anti_coll] vid=%d, pid=%d, dist=%.2f", m_id, c.second->m_id, dist);
  86. if(dist < k.second && s){
  87. int call_level = 5 - k.first;
  88. log_info("[anti_coll] distance=%.3f, level=%d, thre_value=%.3f, time_diff=%d", dist, call_level, k.second, d);
  89. if(min_d > k.second){
  90. min_d = k.second;
  91. cur_v = dist;
  92. }
  93. auto itc = cd.find(c.second->m_id);
  94. if(itc == cd.end()){
  95. cd.insert(std::make_pair(c.second->m_id, dist));
  96. }else{
  97. itc->second = dist;
  98. }
  99. auto it = cards.find(c.second->m_id);
  100. if(it != cards.end()){
  101. if(c.second->m_call_level > call_level){
  102. c.second->m_call_level = call_level;
  103. }
  104. }else{
  105. 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)));
  106. }
  107. 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);
  108. }
  109. }
  110. }
  111. std::string desc = "";
  112. uint64_t id = tool_other::type_id_to_u64(m_type, m_id);
  113. if(cards.size() == 0){
  114. // delete event from event_list
  115. event_tool::instance()->handle_event(OT_CARD, ET_PERSON_VEHICLE_ANTI_COLLISION, id, 0, 0, false);
  116. }else{
  117. size_t i = 0;
  118. for(auto c : cd)
  119. {
  120. desc += std::to_string(c.first);
  121. std::ostringstream buf;
  122. buf<<c.second;
  123. desc += "," + buf.str();
  124. if(i != (cards.size() - 1)){
  125. desc += ";";
  126. }
  127. ++i;
  128. }
  129. log_info("[anti_coll] the distance's list between person and vehicle : %s", desc.c_str());
  130. event_tool::instance()->handle_event(OT_CARD, ET_PERSON_VEHICLE_ANTI_COLLISION, id, min_d, cur_v, true, DT_COMMON, desc);
  131. }
  132. if(cards.size() > 0){
  133. // find the best emengency call
  134. int call_level = 6;
  135. for(auto c : cards){
  136. if(c.second.call_level_id < call_level){
  137. call_level = c.second.call_level_id;
  138. }
  139. }
  140. cards.insert(std::make_pair(m_id, call_card(m_id, 2, call_level, CCT_CALL_APOINT, sid)));
  141. module_call::instance()->send_anti_collision(cards);
  142. }else{
  143. //log_info("[anti_coll] no card trigger anti collision rules.");
  144. return;
  145. }
  146. //module_call::instance()->send_anti_collision(cards);
  147. }
  148. /*
  149. * @brief 三率业务处理模块
  150. * @param const point& pt 定位数据
  151. * @return 无
  152. * @note
  153. * @warning
  154. * @bug
  155. * */
  156. void car::handle_three_rates(const point &pt)
  157. {
  158. card_pos cp;
  159. m_biz_stat = get_stat();
  160. cp.biz_stat = m_biz_stat;
  161. cp.x = pt.x;
  162. cp.y = pt.y;
  163. cp.z = pt.z;
  164. cp.map_id = m_area_tool->get_mapid();
  165. cp.vibration = m_acc;
  166. //put_three_rates(cp);
  167. }
  168. /*
  169. * @brief 红绿灯业务模块
  170. * @param const point& p 定位数据
  171. * @param const int& sid 分站id
  172. * @return 无
  173. * @note
  174. * @warning
  175. * @bug
  176. * */
  177. void car::handle_traffic_light(const point& p, const int& sid)
  178. {
  179. card_pos cp;
  180. cp.x = p.x;
  181. cp.y = p.y;
  182. cp.z = p.z;
  183. cp.biz_stat = get_stat();
  184. cp.map_id = m_area_tool->get_mapid();
  185. cp.vibration = m_acc;
  186. cp.reader_id = sid;
  187. put_traffic_light(cp);
  188. }
  189. void car::on_timer()
  190. {
  191. if(!empty())
  192. make_package();
  193. }
  194. int car::get_area()
  195. {
  196. int status = m_biz_stat;
  197. int special_id = -1;
  198. if(status == STATUS_LOST)
  199. {
  200. special_id = special_area_list::instance()->get_special_id(m_id,*this,m_vehicle_category_id);
  201. log_info("enter_special_area:%.2f,%2.f,id:%d,special_area_id:%d",x,y,m_id,special_id);
  202. }
  203. return special_id;
  204. }
  205. void car::make_package()
  206. {
  207. sys::_CARD_POS_ cp;
  208. loc_point pt = getSmoothPoint();
  209. cp.area_info = m_area_tool->m_area_info;
  210. cp.map_id = m_area_tool->get_mapid();
  211. cp.biz_stat = get_stat();
  212. //cp.down_time = m_mine_tool->get_down_time();
  213. cp.work_time = m_mine_tool->get_work_time();
  214. cp.is_on_duty= m_mine_tool->is_on_duty();
  215. cp.down_time = m_last_point.x;
  216. cp.z = m_last_point.y;
  217. upt_card_pos(cp, pt);
  218. uint64_t _now = tool_time::now_to_ms();
  219. pt.m_time = _now;
  220. make_his_location(pt.m_time, pt);
  221. uint64_t t = _now>m_timeval?_now-m_timeval:m_timeval-_now;
  222. if(t>10*1000)
  223. {
  224. m_area_tool->on_point(shared_from_this(),pt);
  225. m_biz_stat=get_stat();
  226. }
  227. }
  228. void car::get_card(bool f)
  229. {
  230. if(f)
  231. mine_business::inst()->put(shared_from_this());
  232. }
  233. loc_point car::getSmoothPoint()
  234. {
  235. loc_point lp = m_smo_tool->smooth_strategy();
  236. m_speed = lp.m_speed;
  237. m_stat = lp.m_stat;
  238. lp.y = -lp.y;
  239. return lp;
  240. }