card_car.cpp 11 KB

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