card_car.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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_speeds.set_capacity(30);
  26. //m_his_location_card.reset(new location_vehicle(m_id,m_type,cid));
  27. }
  28. car::~car(){}
  29. std::shared_ptr<mine_tool> car::get_mine_tool()
  30. {
  31. return m_mine_tool;
  32. }
  33. void car::set_area_info(int mapid,double scale,int areaid,uint64_t t,int type)
  34. {
  35. m_area_tool->set_area_info(mapid, scale, areaid, *this, t, type);
  36. }
  37. /*
  38. * @brief
  39. * 车辆业务
  40. * @param
  41. * const std::shared_ptr<site>& site 基站对象
  42. * const point& pt 车辆定位坐标
  43. * double acc 加速度值
  44. * @return
  45. * 无
  46. * @note
  47. * @warning
  48. * @bug
  49. * */
  50. void car::do_business(const std::shared_ptr<site>&site,const point &pt,double acc)
  51. {
  52. m_acc=acc;
  53. m_area_tool->on_point(shared_from_this(),pt);
  54. m_timeval = m_time;
  55. //handle_three_rates(pt);
  56. handle_traffic_light(pt, site->m_id);
  57. //if(m_enable_anti_collision){
  58. if(CYaSetting::m_sys_setting.m_enable_anti_coll){
  59. handle_anti_coll(pt, site->m_id);
  60. }
  61. uint64_t id=tool_other::type_id_to_u64(m_type,m_id);
  62. mine_business::inst()->make_arg(id,pt,m_time);
  63. //超速告警业务
  64. handle_over_speed();
  65. }
  66. /*
  67. * @brief
  68. * 超速告警,频率为1hz,定位一秒一次;
  69. * 开始:连续30个定位点超过35km认为开始超速告警
  70. * 结束:连续10个定位点小于35km认为结束超速告警
  71. * @param
  72. * 无
  73. * @return
  74. * @note
  75. * @warning
  76. * @bug
  77. * */
  78. void car::handle_over_speed()
  79. {
  80. // 超速告警,频率为1hz,定位一秒一次;
  81. // 开始:连续30个定位点超过35km认为开始超速告警
  82. // 结束:连续10个定位点小于35km认为结束超速告警
  83. m_speeds.push_back(m_speed);
  84. if(m_speeds.size() < 30){
  85. return;
  86. }
  87. bool status = true;
  88. for(size_t i = 0;i < m_speeds.size();i++){
  89. if(m_speed < 35){
  90. status = false;
  91. }
  92. }
  93. if(status){
  94. // 产生告警
  95. event_tool::instance()->handle_event(OT_CARD, ET_CARD_OVER_SPEED, m_id, 0, 0, status);
  96. }
  97. status = true;
  98. for(size_t i = 20;i < m_speeds.size();i++){
  99. if(m_speed>35){
  100. status = false;
  101. }
  102. }
  103. if(status){
  104. // 结束告警
  105. event_tool::instance()->handle_event(OT_CARD, ET_CARD_OVER_SPEED, m_id, 0, 0, false);
  106. }
  107. }
  108. int car::get_vehicle_type_id()
  109. {
  110. return m_vehicle_type_id;
  111. }
  112. /*
  113. * @brief
  114. * 车辆防碰撞判断
  115. * 车辆定位后,检查车辆附近(根据防碰撞的三个档位距离阈值)是否有人,有人则产生防碰撞告警
  116. * 如果车辆在距离最大档位内没人,则结束告警;
  117. * 以车为单位,通知web端,进入一档二档三挡会将三个档位每个档位有多少人通知web端,
  118. * 当人从一档变为二档也会将人档位的变化发送给web端;
  119. * 发送告警的级别会根据档位的变化动态变化;产生一条告警
  120. * 允许重复发送
  121. * 在本方法中实现上述逻辑;
  122. * @param
  123. * const point& pt 车辆定位位置
  124. * const int& sid
  125. * @return
  126. * 无
  127. * @note
  128. * modified by zhuyf,2022-04-24
  129. * @warning
  130. * @bug
  131. * */
  132. void car::handle_anti_coll(const point& pt, const int& sid)
  133. {
  134. // 车卡下发最紧急的呼叫类型
  135. std::map<int, float> cd; // 人卡与车卡的距离,key为人卡id,value为距离
  136. std::map<int, call_card> cards;
  137. auto tmp_cards = card_list::instance()->m_map;
  138. log_info("[anti_coll] card'size = %d", tmp_cards.size());
  139. double min_d = 9999999.9;
  140. double cur_v = 0.0;
  141. for(auto k : CYaSetting::m_sys_setting.mp_anti_collision){
  142. //log_info("[anti_coll] key=%d, value=%.2f", k.first, k.second);
  143. for(auto& c : tmp_cards)
  144. {
  145. // 车卡不参与车车之间防碰撞
  146. if(c.second->m_type == CT_VEHICLE){
  147. continue;
  148. }
  149. // 司机卡不参与人车之间的防碰撞
  150. if(c.second->m_id>=9000&&c.second->m_id<=9999){
  151. continue;
  152. }
  153. // 避免历史定位数据的影响
  154. uint64_t now = time(0)*1000;
  155. if(m_time - now > 60){
  156. continue;
  157. }
  158. bool s = false;
  159. int d = (c.second->m_timeval >= m_timeval ? (c.second->m_timeval - m_timeval) : (m_timeval - c.second->m_timeval)) / 1000.0;
  160. s = ((c.second->m_timeval >= m_timeval ? (c.second->m_timeval - m_timeval) : (m_timeval - c.second->m_timeval)) /1000.0 <= 30);
  161. //float dist = pt.dist(*c.second);
  162. float dist = pt.dist(c.second->m_v_point);
  163. log_info("[anti_coll] vid=%d, pid=%d, dist=%.2f", m_id, c.second->m_id, dist);
  164. if(dist < k.second && s){
  165. int call_level = 5 - k.first;
  166. log_info("[anti_coll] distance=%.3f, level=%d, thre_value=%.3f, time_diff=%d", dist, call_level, k.second, d);
  167. if(min_d > k.second){
  168. min_d = k.second;
  169. cur_v = dist;
  170. }
  171. auto itc = cd.find(c.second->m_id);
  172. if(itc == cd.end()){
  173. cd.insert(std::make_pair(c.second->m_id, dist));
  174. }else{
  175. itc->second = dist;
  176. }
  177. auto it = cards.find(c.second->m_id);
  178. if(it != cards.end()){
  179. if(c.second->m_call_level > call_level){
  180. c.second->m_call_level = call_level;
  181. }
  182. }else{
  183. 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)));
  184. }
  185. 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);
  186. }
  187. }
  188. }
  189. std::string desc = "";
  190. uint64_t id = tool_other::type_id_to_u64(m_type, m_id);
  191. if(cards.size() == 0){
  192. // delete event from event_list
  193. event_tool::instance()->handle_event(OT_CARD, ET_PERSON_VEHICLE_ANTI_COLLISION, id, 0, 0, false);
  194. }else{
  195. size_t i = 0;
  196. for(auto c : cd)
  197. {
  198. desc += std::to_string(c.first);
  199. std::ostringstream buf;
  200. buf<<c.second;
  201. desc += "," + buf.str();
  202. if(i != (cards.size() - 1)){
  203. desc += ";";
  204. }
  205. ++i;
  206. }
  207. log_info("[anti_coll] the distance's list between person and vehicle : %s", desc.c_str());
  208. event_tool::instance()->handle_event(OT_CARD, ET_PERSON_VEHICLE_ANTI_COLLISION, id, min_d, cur_v, true, DT_COMMON, desc);
  209. }
  210. if(cards.size() > 0){
  211. // find the best emengency call
  212. int call_level = 6;
  213. for(auto c : cards){
  214. if(c.second.call_level_id < call_level){
  215. call_level = c.second.call_level_id;
  216. }
  217. }
  218. cards.insert(std::make_pair(m_id, call_card(m_id, 2, call_level, CCT_CALL_APOINT, sid)));
  219. module_call::instance()->send_anti_collision(cards);
  220. }else{
  221. //log_info("[anti_coll] no card trigger anti collision rules.");
  222. return;
  223. }
  224. //module_call::instance()->send_anti_collision(cards);
  225. }
  226. /*
  227. * @brief 三率业务处理模块
  228. * @param const point& pt 定位数据
  229. * @return 无
  230. * @note
  231. * @warning
  232. * @bug
  233. * */
  234. void car::handle_three_rates(const point &pt)
  235. {
  236. card_pos cp;
  237. m_biz_stat = get_stat();
  238. cp.biz_stat = m_biz_stat;
  239. cp.x = pt.x;
  240. cp.y = pt.y;
  241. cp.z = pt.z;
  242. cp.map_id = m_area_tool->get_mapid();
  243. cp.vibration = m_acc;
  244. //put_three_rates(cp);
  245. }
  246. /*
  247. * @brief 红绿灯业务模块
  248. * @param const point& p 定位数据
  249. * @param const int& sid 分站id
  250. * @return 无
  251. * @note
  252. * @warning
  253. * @bug
  254. * */
  255. void car::handle_traffic_light(const point& p, const int& sid)
  256. {
  257. card_pos cp;
  258. cp.x = p.x;
  259. cp.y = p.y;
  260. cp.z = p.z;
  261. cp.biz_stat = get_stat();
  262. cp.map_id = m_area_tool->get_mapid();
  263. cp.vibration = m_acc;
  264. cp.reader_id = sid;
  265. put_traffic_light(cp);
  266. }
  267. void car::on_timer()
  268. {
  269. if(!empty())
  270. make_package();
  271. }
  272. int car::get_area()
  273. {
  274. int status = m_biz_stat;
  275. int special_id = -1;
  276. if(status == STATUS_LOST)
  277. {
  278. special_id = special_area_list::instance()->get_special_id(m_id,*this,m_vehicle_category_id);
  279. log_info("enter_special_area:%.2f,%2.f,id:%d,special_area_id:%d",x,y,m_id,special_id);
  280. }
  281. return special_id;
  282. }
  283. void car::make_package()
  284. {
  285. sys::_CARD_POS_ cp;
  286. loc_point pt = getSmoothPoint();
  287. cp.area_info = m_area_tool->m_area_info;
  288. cp.map_id = m_area_tool->get_mapid();
  289. cp.biz_stat = get_stat();
  290. //cp.down_time = m_mine_tool->get_down_time();
  291. cp.work_time = m_mine_tool->get_work_time();
  292. cp.is_on_duty= m_mine_tool->is_on_duty();
  293. cp.down_time = m_last_point.x;
  294. cp.z = m_last_point.y;
  295. upt_card_pos(cp, pt);
  296. uint64_t _now = tool_time::now_to_ms();
  297. pt.m_time = _now;
  298. make_his_location(pt.m_time, pt);
  299. uint64_t t = _now>m_timeval?_now-m_timeval:m_timeval-_now;
  300. if(t>10*1000)
  301. {
  302. m_area_tool->on_point(shared_from_this(),pt);
  303. m_biz_stat=get_stat();
  304. }
  305. }
  306. void car::get_card(bool f)
  307. {
  308. if(f)
  309. mine_business::inst()->put(shared_from_this());
  310. }
  311. loc_point car::getSmoothPoint()
  312. {
  313. loc_point lp = m_smo_tool->smooth_strategy();
  314. m_speed = lp.m_speed;
  315. m_stat = lp.m_stat;
  316. lp.y = -lp.y;
  317. return lp;
  318. }