1
0

mine_business.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. #include <cmath>
  2. #include <sstream>
  3. #include <vector>
  4. #include <list>
  5. #include <array>
  6. #include <map>
  7. #include "mine_business.h"
  8. #include "log.h"
  9. #include "point.h"
  10. #include "db/db_api/CDBSingletonDefine.h"
  11. #include "db/db_tool.h"
  12. #include "common_tool.h"
  13. #include "ant.h"
  14. #include "sys_setting.h"
  15. #include "card_path.h"
  16. #include "card.h"
  17. #include "area.h"
  18. #include "event.h"
  19. #include <rapidjson/writer.h>
  20. #include <rapidjson/stringbuffer.h>
  21. #include <rapidjson/prettywriter.h>
  22. #include <rapidjson/document.h>
  23. #include <rapidjson/pointer.h>
  24. #include "wsClientMgr.h"
  25. struct card_sensor
  26. {
  27. card_sensor(int32_t wid,const point &p,const std::string &cid)
  28. :card_id(cid)
  29. ,sid(0)
  30. ,id(wid)
  31. ,bigger(-1),count(0),sum(0),ct(-1),stime(0),flag(false),is_alarm(false)
  32. {
  33. base_point=p;
  34. }
  35. std::string card_id;
  36. int32_t sid;
  37. int32_t id;
  38. std::vector<int> sensorId_list;
  39. point base_point;
  40. std::array<point,2> ant;
  41. int8_t bigger;
  42. uint32_t count,sum;
  43. uint16_t ct;
  44. uint64_t stime;
  45. bool flag,is_alarm;
  46. std::list<double> his_data;
  47. std::array<uint64_t,2> dist;
  48. void clear()
  49. {
  50. count=sum=0;stime=0;flag=false;
  51. }
  52. bool timeout()
  53. {
  54. time_t t=time(NULL);
  55. return flag && t-stime>10*60;
  56. }
  57. bool check_location_reversal(std::string & str)
  58. {
  59. time_t t=time(NULL);
  60. time_t tval= t-stime;
  61. bool f=false;
  62. if (tval !=0 && sum*1.0/tval > 0.8 && count*1.0/sum <0.2)
  63. f=true;
  64. char buf[1024]={0};
  65. sprintf(buf,"[reverse_alarm_checking]%s,count:%d,sum:%d,val:%ld,s:%.2f,v:%.2f,alarm:%d",
  66. card_id.c_str(),count,sum,tval,(sum*1.0/tval),(count*1.0/sum),f);
  67. str=buf;
  68. return f;
  69. }
  70. void make_reverse_condition(int16_t antid,uint16_t ct,double d)
  71. {
  72. dist[antid]=d;
  73. if(this->ct == ct){
  74. sum++;
  75. int b=-1;
  76. if (dist[0]>dist[1]) b=0 ;else b =1;
  77. if(b==bigger) count++; //合格的点
  78. }
  79. this->ct = ct;
  80. }
  81. void set(const point &p0,const point &p1,int id)
  82. {
  83. int8_t b=0;
  84. ant[0].set(p0);ant[1].set(p1);
  85. double d0=base_point.dist(p0);double d1=base_point.dist(p1);
  86. if(d0>d1)b=1;else b=0;
  87. if (bigger!=-1 && b!=bigger){
  88. count=sum=0;stime=time(0);
  89. }
  90. bigger=b;sid=id;
  91. }
  92. bool push(double d){
  93. bool f=true;
  94. his_data.push_back(d);
  95. if (his_data.size()==3){
  96. double da=0;
  97. for (const auto &d:his_data){
  98. if(da>d || d-da<0.004){f=false;break;}
  99. da=d;
  100. }
  101. his_data.pop_front();
  102. }
  103. else f=false;
  104. return f;
  105. }
  106. point &operator[](int i){return ant[i];}
  107. const point &operator[](int i)const {return ant[i];}
  108. void set_true(){flag=true;stime=time(NULL);}
  109. bool warning(){return flag;}
  110. bool empty(){return his_data.empty();}
  111. double back(){return his_data.back();}
  112. void clear_sensor(){clear();}
  113. int reader_id(){return sid;}
  114. void push_back(int32_t sensor_id){sensorId_list.push_back(sensor_id);}
  115. };
  116. struct card_sensor_mgr
  117. {
  118. void push(int32_t wid,int32_t sid,const point &p,const std::string & cardid)
  119. {
  120. std::shared_ptr<card_sensor> cs=nullptr;
  121. auto it=m_mgr.find(wid);
  122. if(it!=m_mgr.end())
  123. cs=it->second;
  124. else
  125. {
  126. cs=std::make_shared<card_sensor>(wid,p,cardid);
  127. m_mgr.insert({wid,cs});
  128. }
  129. cs->push_back(sid);
  130. }
  131. void for_each();
  132. void make_condition(int32_t antid,uint32_t ct,uint64_t tof,uint32_t sid);
  133. std::map<int32_t,std::shared_ptr<card_sensor>> m_mgr;
  134. };
  135. struct staffer_num_business
  136. {
  137. void record_staffer_num();
  138. void fetch_add(bool bDisplay)
  139. {
  140. if(bDisplay)
  141. m_staff_num++;
  142. m_staff_num_real++;
  143. }
  144. staffer_num_business()
  145. {
  146. reset();
  147. m_record_staffer_timeval = 0;
  148. }
  149. uint32_t get_staff_num() const { return m_staff_num.load();}
  150. private:
  151. void reset()
  152. {
  153. m_staff_num=0;
  154. m_staff_num_real=0;
  155. }
  156. // 井下考勤并且要显示的人数
  157. std::atomic<uint32_t> m_staff_num{0};
  158. // 井下总人数
  159. std::atomic<uint32_t> m_staff_num_real{0};
  160. // 通知web最后时间
  161. std::time_t m_record_staffer_timeval;
  162. };
  163. struct reverse_alarm_business
  164. {
  165. void load_sensor();
  166. void load_working_sensor();
  167. void run_sensor();
  168. void get_status();
  169. void make_condition(uint64_t type,uint32_t id,int32_t antid,uint32_t ct,uint64_t tof,uint32_t sid);
  170. private:
  171. std::map<uint64_t,std::shared_ptr<card_sensor_mgr>> m_sensor_mgr;
  172. std::map<std::string,int> m_working_sensor;
  173. };
  174. struct rear_end_collision_prevented_business
  175. {
  176. rear_end_collision_prevented_business()
  177. {
  178. _time=0;m_v.clear();
  179. }
  180. void clear(){m_v.clear();}
  181. void put(const std::shared_ptr<card_location_base>&c)
  182. {
  183. m_v.push_back(c);
  184. }
  185. bool rear_end_arg_algo(uint64_t u1,uint64_t u2);
  186. void handle_message();
  187. double get_absolute_distance(uint64_t c1,uint64_t c2);
  188. struct car_data
  189. {
  190. car_data()
  191. :_t(0),_arg(0x12345678)
  192. {}
  193. void push(uint64_t cid,const point &p,uint64_t t)
  194. {
  195. last_p=_p;_p=p;
  196. if (last_p.empty() || _p.empty())
  197. return;
  198. if (_p.dist(last_p)<0.5)
  199. return;
  200. if ( t-_t<5*1000)
  201. {
  202. _arg = std::arg(std::complex<double>(_p.x,_p.y)-std::complex<double>(last_p.x,last_p.y));
  203. line_v l(last_p,_p);_line=l;
  204. std::string card_id = tool_other::get_string_cardid(cid);
  205. log_info("[vehicle_alarm_arg ] %s,arg:%f (%.2f,%.2f)--(%.2f,%.2f)",card_id.c_str(),_arg,last_p.x,last_p.y,p.x,p.y);
  206. }
  207. _t=t;
  208. }
  209. point last_p,_p;
  210. line_v _line;
  211. uint64_t _t;
  212. double _arg;
  213. };
  214. struct vInfo
  215. {
  216. vInfo()
  217. {
  218. is_alarm=false;
  219. real_end_start_time=0;
  220. backfired_start_time=0;
  221. }
  222. void push(const std::string & c1, const std::string &c2)
  223. {
  224. m_cid[0]=c1;
  225. m_cid[1]=c2;
  226. std::sort(m_cid.begin(),m_cid.end());
  227. }
  228. std::string getKey() const
  229. {
  230. return m_cid[0]+"&"+m_cid[1];
  231. }
  232. bool is_alarm;
  233. std::array<std::string,2> m_cid;
  234. time_t real_end_start_time ;
  235. time_t backfired_start_time;
  236. };
  237. void make_arg(uint64_t cid, const point &p,uint64_t t)
  238. {
  239. m_map[cid].push(cid,p,t);
  240. }
  241. private:
  242. bool invalid_card(const std::shared_ptr<card_location_base> &c);
  243. time_t _time;
  244. std::vector<std::shared_ptr<card_location_base>> m_v;
  245. std::map<uint64_t,car_data> m_map;
  246. std::map<std::string,vInfo> m_CloserVehicle;
  247. };
  248. mine_business::mine_business()
  249. {
  250. m_staffer_num_ptr.reset(new staffer_num_business);
  251. m_reverse_alarm_ptr.reset(new reverse_alarm_business);
  252. m_rear_ended_ptr.reset(new rear_end_collision_prevented_business);
  253. }
  254. mine_business* mine_business::inst()
  255. {
  256. static mine_business mb;
  257. return &mb;
  258. }
  259. void mine_business::run_business()
  260. {
  261. record_staffer_num();
  262. handle_reverse_alarm();
  263. handle_rear_end();
  264. }
  265. void mine_business::fetch_add(bool bDisplay)
  266. {
  267. m_staffer_num_ptr->fetch_add(bDisplay);
  268. }
  269. void mine_business::record_staffer_num()
  270. {
  271. m_staffer_num_ptr->record_staffer_num();
  272. }
  273. /*
  274. * @brief 加载传感器数据
  275. * @param
  276. * @return
  277. * @note
  278. * @warning
  279. * @bug
  280. * */
  281. void mine_business::load()
  282. {
  283. //m_reverse_alarm_ptr->load_sensor();
  284. //m_reverse_alarm_ptr->load_working_sensor();
  285. }
  286. void mine_business::handle_reverse_alarm()
  287. {
  288. m_reverse_alarm_ptr->run_sensor();
  289. }
  290. void mine_business::make_reverse_condition(uint64_t type,uint32_t id,int32_t antid,uint32_t ct,uint64_t tof,uint32_t sid)
  291. {
  292. m_reverse_alarm_ptr->make_condition(type,id,antid,ct,tof,sid);
  293. }
  294. ///rear ended collision...
  295. void mine_business::put(const std::shared_ptr<card_location_base> &c)
  296. {
  297. m_rear_ended_ptr->put(c);
  298. }
  299. void mine_business::make_arg(uint64_t cid,const point &p,uint64_t t)
  300. {
  301. m_rear_ended_ptr->make_arg(cid,p,t);
  302. }
  303. void mine_business::handle_rear_end()
  304. {
  305. m_rear_ended_ptr->handle_message();
  306. }
  307. void mine_business::clear_vehicle()
  308. {
  309. m_rear_ended_ptr->clear();
  310. }
  311. uint32_t mine_business::get_mine_display_staff_num()
  312. {
  313. return m_staffer_num_ptr->get_staff_num();
  314. }
  315. ///////staffer_num_business
  316. /**********************************
  317. //每两分钟把人员数量入库。
  318. //不往前端推送的人员,不算在数量范围内
  319. *********************************/
  320. void staffer_num_business::record_staffer_num()
  321. {
  322. static uint32_t min_num=0,max_num=0,min_num_real=0,max_num_real=0;
  323. std::time_t t = time(NULL);
  324. char ti[64] = { 0 };
  325. strftime(ti,sizeof(ti),"%Y/%m/%d %H:%M:%S",localtime(&t));
  326. std::string sti(ti);
  327. int minute = atoi(sti.substr(sti.find_first_of(':')+1,2).c_str());
  328. uint32_t a=m_staff_num.load();
  329. uint32_t ar=m_staff_num_real.load();
  330. if(m_record_staffer_timeval==0)
  331. {
  332. min_num=max_num=a;
  333. min_num_real=max_num_real=ar;
  334. m_record_staffer_timeval = t;
  335. }
  336. if(t-m_record_staffer_timeval>=120 && minute%2 == 0)
  337. {
  338. std::stringstream ss;
  339. ss<< "INSERT INTO his_staff_number(max_num,min_num,ave_num,max_num_real,min_num_real,ave_num_real) VALUES("
  340. <<max_num<<','<<min_num<<','<<std::lround((max_num+min_num)/2)<<','
  341. <<max_num_real<<','<<min_num_real<<','<<std::lround((max_num_real+min_num_real)/2)<<");";
  342. db_tool::PushAsync(ss.str().c_str());
  343. min_num=max_num=a;
  344. min_num_real=max_num_real=ar;
  345. m_record_staffer_timeval = t;
  346. }
  347. else
  348. {
  349. if (min_num>a)min_num = a;
  350. if (max_num<a)max_num = a;
  351. if (min_num_real>ar)min_num_real = ar;
  352. if (max_num_real<ar)max_num_real = ar;
  353. }
  354. reset();
  355. return;
  356. }
  357. void reverse_alarm_business::load_working_sensor()
  358. {
  359. std::string sql = "SELECT sensor_id,work_face_id,v.vehicle_id,ve.card_id FROM dat_sensor ,dat_coalface_vehicle v,dat_coalface d,dat_vehicle_extend ve WHERE sensor_type_id = 8 AND work_face_id = v.coalface_id AND work_face_id = d.coalface_id AND ve.vehicle_id = v.vehicle_id union SELECT sensor_id,work_face_id,v.vehicle_id,ve.card_id FROM dat_sensor ,dat_drivingface_vehicle v,dat_drivingface d,dat_vehicle_extend ve WHERE sensor_type_id = 8 AND work_face_id = v.drivingface_id AND work_face_id = d.drivingface_id AND ve.vehicle_id = v.vehicle_id;";
  360. std::string Error;
  361. YADB::CDBResultSet DBRes;
  362. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  363. int nCount = DBRes.GetRecordCount( Error );
  364. if (nCount < 1)
  365. log_error("init_working_sensor..failed[%s]", sql.c_str());
  366. while ( DBRes.GetNextRecod(Error) )
  367. {
  368. int32_t sensor_id = 0;
  369. DBRes.GetField( "sensor_id",sensor_id, Error );
  370. int32_t workfaceid = 0;
  371. DBRes.GetField( "work_face_id",workfaceid, Error );
  372. std::string card_id;
  373. DBRes.GetField( "card_id",card_id, Error );
  374. std::string pre("00");
  375. card_id=pre+card_id;
  376. log_info("init_working_sensor :workfaceid:%d,sensor_id:%d,card:%s",workfaceid,sensor_id,card_id.c_str());
  377. m_working_sensor[card_id]=sensor_id;
  378. }
  379. }
  380. //前端修改数据库表,dat_sensor dat_drivingface,dat_drivingface_vehicle的时候,需要加载.
  381. void reverse_alarm_business::load_sensor()
  382. {
  383. std::string sql = "SELECT sensor_id,data_source,work_face_id,v.vehicle_id,ve.card_id,d.base_point_x,d.base_point_y FROM dat_sensor ,dat_drivingface_vehicle v,dat_drivingface d,dat_vehicle_extend ve WHERE sensor_type_id = 1 AND work_face_id = v.drivingface_id AND work_face_id = d.drivingface_id AND ve.vehicle_id = v.vehicle_id;";
  384. std::string Error;
  385. YADB::CDBResultSet DBRes;
  386. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  387. int nCount = DBRes.GetRecordCount( Error );
  388. if (nCount < 1)
  389. log_error("init_sensor..failed[%s]", sql.c_str());
  390. while ( DBRes.GetNextRecod(Error) )
  391. {
  392. int32_t sensor_id = 0;
  393. DBRes.GetField( "sensor_id",sensor_id, Error );
  394. int32_t data = 0;
  395. DBRes.GetField( "data_source",data, Error );
  396. int32_t workfaceid = 0;
  397. DBRes.GetField( "work_face_id",workfaceid, Error );
  398. std::string card_id;
  399. DBRes.GetField( "card_id",card_id, Error );
  400. double bx,by;
  401. DBRes.GetField( "base_point_x",bx, Error );
  402. DBRes.GetField( "base_point_y",by, Error );
  403. log_info("init sensor :workfaceid:%d,sensor_id:%d,data:%d,card:%s,(%.2f,%.2f)",workfaceid,sensor_id,data,card_id.c_str(),bx,by);
  404. if(data <= 0) continue;
  405. uint64_t cid = tool_other::card_id_to_u64(card_id);
  406. std::shared_ptr<card_sensor_mgr> tm=nullptr;
  407. auto iter = m_sensor_mgr.find(cid);
  408. if(iter != m_sensor_mgr.end())
  409. tm=iter->second;
  410. else
  411. {
  412. tm = std::make_shared<card_sensor_mgr>();
  413. m_sensor_mgr.insert({cid,tm});
  414. }
  415. point p(bx,by);
  416. tm->push(workfaceid,sensor_id,p,card_id);
  417. }
  418. }
  419. /************************************
  420. //获取传感器数据,从数据库中
  421. //每三分钟获取一次,判断其是否上升趋势。
  422. //如果上升趋势,则标识正在掘进中。开始判断反向
  423. ************************************/
  424. void reverse_alarm_business::run_sensor()
  425. {
  426. static time_t time_interval=0,time_working_interval=0;
  427. time_t t = time(NULL);
  428. if(t-time_working_interval>1*70)
  429. {
  430. time_working_interval=t;
  431. get_status();
  432. }
  433. if (t-time_interval>3*60)
  434. {
  435. time_interval =t;
  436. for(const auto &x:m_sensor_mgr)
  437. x.second->for_each();
  438. }
  439. }
  440. void reverse_alarm_business::get_status()
  441. {
  442. rapidjson::StringBuffer sb;
  443. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  444. rapidjson::Document doc;
  445. rapidjson::Document::AllocatorType& Allocator = doc.GetAllocator();
  446. rapidjson::Value LArray( rapidjson::kArrayType );
  447. rapidjson::Value root( rapidjson::kObjectType );
  448. for(const auto &x:m_working_sensor)
  449. {
  450. char strsql[512]={0};
  451. snprintf(strsql,512, "SELECT data_value FROM his_sensor_data WHERE write_time > DATE_SUB(NOW(),INTERVAL 3 MINUTE) and write_time < now() and sensor_id = %d ORDER BY write_time DESC LIMIT 1;",x.second);
  452. std::string Error;
  453. YADB::CDBResultSet DBRes;
  454. sDBConnPool.Query(strsql,DBRes,Error);
  455. int nCount = DBRes.GetRecordCount( Error );
  456. if (nCount < 1)
  457. {
  458. //log_warn("load_sensor_data..failed[%s]", strsql);
  459. logn_warn(2, "load_sensor_data...failed sql=%s", strsql);
  460. continue;
  461. }
  462. if( DBRes.GetNextRecod(Error) )
  463. {
  464. rapidjson::Value Array( rapidjson::kArrayType );
  465. rapidjson::Value _DetailItem;
  466. rapidjson::Value tmp_object( rapidjson::kObjectType );
  467. int num = 0;
  468. DBRes.GetField( "data_value",num, Error );
  469. log_info("working_sensor:%s,%d--%d",x.first.c_str(),x.second,num);
  470. num?num=0:num=1;
  471. tmp_object.SetString(x.first.c_str() , Allocator );
  472. Array.PushBack( tmp_object, Allocator );
  473. tmp_object.SetInt(num );
  474. Array.PushBack( tmp_object, Allocator );
  475. _DetailItem=Array;
  476. LArray.PushBack(_DetailItem, Allocator);
  477. }
  478. }
  479. rapidjson::Value value( rapidjson::kStringType );
  480. value.SetString("vehicle_state", Allocator );
  481. root.AddMember( "cmd", value, Allocator );
  482. if(LArray.Capacity() == 0){
  483. return;
  484. }
  485. root.AddMember("data",LArray,Allocator);
  486. value.SetString("1.0.0.4", Allocator );
  487. root.AddMember( "version", value, Allocator );
  488. root.Accept(writer);
  489. std::string vs=sb.GetString();
  490. log_info("vehicle_state:%s",vs.c_str());
  491. swsClientMgr.send( JSON_CMD_VALUE_PUSH,vs);
  492. }
  493. void card_sensor_mgr::for_each()
  494. {
  495. for(const auto & cst:m_mgr)
  496. {
  497. auto cs = cst.second;
  498. if(cs->sensorId_list.empty())
  499. continue;
  500. char strsql[512]={0};
  501. snprintf(strsql,512, "SELECT data_value FROM his_sensor_data WHERE WHERE write_time > DATE_SUB(NOW(),INTERVAL 10 MINUTE) and sensor_id = %d ORDER BY write_time DESC LIMIT 3;",cs->sensorId_list[0]);
  502. std::string Error;
  503. YADB::CDBResultSet DBRes;
  504. sDBConnPool.Query(strsql,DBRes,Error);
  505. int nCount = DBRes.GetRecordCount( Error );
  506. if (nCount < 1)
  507. {
  508. //log_warn("load_sensor_data..failed[%s]", strsql);
  509. logn_warn(2, "load_sensor_data...failed[%s]", strsql);
  510. continue;
  511. }
  512. double prenum=-1;
  513. double nrow=0;double sum=0;bool f=false;
  514. while ( DBRes.GetNextRecod(Error) )
  515. {
  516. double num = 0;
  517. DBRes.GetField( "data_value",num, Error );
  518. if (point::eq(num,0,1e-10))continue; //0异常数据抛掉
  519. if(point::eq(prenum,-1,1e-10)){ //如果没有参考数据,则判断,如果有历史数据,用历史,没有则用第一个数据。
  520. if(cs->empty()) prenum=num; else prenum=cs->back();
  521. }
  522. if (prenum-num>0.1){ //异常数据 如果之前存在正常数据则继续,如果没有则本次不作参考。0.1暂定
  523. if(cs->empty()){
  524. f=true;break;
  525. }
  526. else continue;
  527. }
  528. nrow++;sum+=num;
  529. }
  530. if (!f && nrow!=0){
  531. double result=sum/nrow;
  532. log_info("reverse_alarm_push %s,%.3f",cs->card_id.c_str(),result);
  533. if(cs->push(result)){//满足三个并且呈现上升趋势
  534. log_info("reverse_alarm_up %s",cs->card_id.c_str());
  535. if (!cs->warning())
  536. cs->set_true();//置true,主进程开始抓取数据。
  537. }
  538. }
  539. if (cs->timeout()){//时间满足,判断是否告警
  540. auto reader_it = sit_list::instance()->get(cs->reader_id());
  541. if (!reader_it){
  542. cs->clear_sensor();
  543. continue;
  544. }
  545. std::string msg;
  546. if(cs->check_location_reversal(msg) )//满足告警条件,查看是否已经存在告警,没有产生告警
  547. {
  548. log_info("reverse_alarm_create:%s",cs->card_id.c_str());
  549. //产生告警。
  550. //deal_readers_event(ET_READER_LOCATION_REVERSAL,true,reader_it->second);
  551. if(!cs->is_alarm)
  552. event_tool::instance()->handle_event(OT_DEVICE_READER,ET_READER_LOCATION_REVERSAL,cs->reader_id(),cs->count,cs->sum,cs->is_alarm=true);
  553. }
  554. else //不满足告警条件,查看是否已经存在告警,存在则取消告警。
  555. {
  556. log_info("reverse_alarm_delete:%s",cs->card_id.c_str());
  557. if(cs->is_alarm)
  558. event_tool::instance()->handle_event(OT_DEVICE_READER,ET_READER_LOCATION_REVERSAL,cs->reader_id(),cs->count,cs->sum,cs->is_alarm=false);
  559. //补
  560. //deal_readers_event(ET_READER_LOCATION_REVERSAL,false,reader_it->second);
  561. }
  562. log_info("%s",msg.c_str());
  563. cs->clear_sensor();//清除数据,重新判断
  564. }
  565. }
  566. }
  567. /*****************************************
  568. //当判断为反向后,则开始手机数据。
  569. //在规定的时间内,记录反向的数量以及双天线数量。
  570. //正常的数量n 总数量s 时长m(秒)
  571. //n/s<0.2 && s/m>0.8则标识天线反向
  572. ****************************************/
  573. void reverse_alarm_business::make_condition(uint64_t type,uint32_t id,int32_t antid,uint32_t ct,uint64_t tof,uint32_t sid)
  574. {
  575. uint64_t cid = tool_other::type_id_to_u64(type,id);
  576. auto it = m_sensor_mgr.find(cid);
  577. if(it != m_sensor_mgr.end())
  578. it->second->make_condition(antid,ct,tof,sid);
  579. }
  580. void card_sensor_mgr::make_condition(int32_t antid,uint32_t ct,uint64_t tof,uint32_t sid)
  581. {
  582. for(const auto x:m_mgr)
  583. {
  584. auto pdc =x.second;
  585. if (pdc->warning()){
  586. auto sit = sit_list::instance()->get(sid);
  587. point p0=(*sit)[0];point p1=(*sit)[1];
  588. if (!((*pdc)[0]==p0 && (*pdc)[1]==p1)){
  589. pdc->set(p0,p1,sid);
  590. log_info("[reverse_alarm_start]%s,bigger:%d",pdc->card_id.c_str(),pdc->bigger);
  591. }
  592. log_info("[reverse_alarm_working]%s,ct:%d,antid:%d,dist:%.2f",pdc->card_id.c_str(),ct,antid,tof);
  593. pdc->make_reverse_condition(antid,ct,tof);
  594. }
  595. }
  596. }
  597. bool rear_end_collision_prevented_business::invalid_card(const std::shared_ptr<card_location_base> &c)
  598. {
  599. bool f=false;
  600. uint64_t _now=time(0)*1000;
  601. uint64_t ti=c->time_();
  602. uint64_t t=_now>ti?_now-ti:ti-_now;
  603. if(t<45*1000 && !c->empty())
  604. f=true;
  605. return f;
  606. }
  607. void rear_end_collision_prevented_business::handle_message()
  608. {
  609. time_t now = time(0);
  610. if(now-_time>3)
  611. _time=now;
  612. else
  613. return;
  614. auto it=m_v.cbegin();
  615. for (;it!=m_v.cend();it++)
  616. {
  617. auto c=*it;
  618. if(!invalid_card(c))continue;
  619. double map_scale = c->get_area_tool()->m_scale;
  620. for (auto prv_it=std::next(it);prv_it!=m_v.cend();++prv_it)
  621. {
  622. auto c2=*prv_it;
  623. if(!invalid_card(c2))continue;
  624. double dis = (*c).dist(*c2)*map_scale;
  625. log_info("vehicle_alarm stpe3 : cid1:%d,cid2:%d (%.2f,%.2f),(%.2f,%.2f) %.2f,%f",c->m_id,c2->m_id,c->x,c->y,c2->x,c2->y,dis,map_scale);
  626. if(dis>CYaSetting::m_sys_setting.rear_end_d)
  627. continue;
  628. //判断反向行驶,平行行驶,背离行驶的车辆
  629. uint64_t u1=tool_other::type_id_to_u64(c->m_type,c->m_id);uint64_t u2=tool_other::type_id_to_u64(c2->m_type,c2->m_id);
  630. if (!rear_end_arg_algo(u1,u2))
  631. continue;
  632. //
  633. std::string card1=tool_other::type_id_to_str(c->m_type,c->m_id);std::string card2=tool_other::type_id_to_str(c2->m_type,c2->m_id);
  634. vInfo v;
  635. v.push(card1,card2);
  636. std::string cardid=v.getKey();
  637. if(m_CloserVehicle.find(cardid) == m_CloserVehicle.end())
  638. {
  639. m_CloserVehicle.insert(std::make_pair(cardid,v));
  640. log_info("vehicle_alarm_insert %s",cardid.c_str());
  641. }
  642. }
  643. }
  644. auto it_cv=m_CloserVehicle.begin();
  645. for (;it_cv != m_CloserVehicle.end();)
  646. {
  647. vInfo & vinfo = it_cv->second;
  648. std::string vvid = it_cv->first;
  649. uint64_t u1=tool_other::card_id_to_u64(vinfo.m_cid[0]);
  650. uint64_t u2=tool_other::card_id_to_u64(vinfo.m_cid[1]);
  651. uint64_t t=now*1000;
  652. uint64_t ct=m_map[u1]._t ;uint64_t rt = t>ct?t-ct:ct-t;
  653. uint64_t ct_=m_map[u2]._t; uint64_t rt_ = t>ct_?t-ct_:ct_-t;
  654. std::hash<std::string> hsh;
  655. uint32_t id = hsh(vvid);
  656. if (rt>45*1000 || rt_>45*1000 )
  657. {
  658. log_info("vehicle_alarm_time_ %s,%u",vvid.c_str(),id);
  659. if (vinfo.is_alarm){
  660. event_tool::instance()->handle_event(OT_CARD,ET_VEHICLE_REAR_END,id,0,0,false,DT_COMMON,vvid);
  661. }
  662. it_cv = m_CloserVehicle.erase(it_cv);
  663. continue;
  664. }
  665. auto c=card_list::instance()->get(u1);
  666. if(!c) continue;
  667. double map_scale = c->get_area_tool()->m_scale;
  668. double dis = get_absolute_distance(u1,u2)*map_scale;
  669. log_info("vehicle_alarm_distance %s %f",vvid.c_str(),dis);
  670. time_t tt = time(0);
  671. if (dis>CYaSetting::m_sys_setting.rear_end_d ||!rear_end_arg_algo(u1,u2))
  672. {
  673. if (vinfo.backfired_start_time==0)
  674. vinfo.backfired_start_time=tt;
  675. vinfo.real_end_start_time=0;
  676. if (tt-vinfo.backfired_start_time>CYaSetting::m_sys_setting.rear_end_t)
  677. {
  678. log_info("vehicle_alarm_backfired_timeout %s,%u",vvid.c_str(),id);
  679. if(vinfo.is_alarm){
  680. event_tool::instance()->handle_event(OT_CARD,ET_VEHICLE_REAR_END,id,CYaSetting::m_sys_setting.rear_end_d,dis,false,DT_COMMON,vvid);
  681. }
  682. it_cv = m_CloserVehicle.erase(it_cv);
  683. continue;
  684. }
  685. }
  686. else
  687. {
  688. vinfo.backfired_start_time =0;
  689. if (vinfo.real_end_start_time==0)
  690. vinfo.real_end_start_time=tt;
  691. if (tt-vinfo.real_end_start_time>CYaSetting::m_sys_setting.rear_end_t)
  692. {
  693. // debug_print_syslog(0,"vehicle_alarm_real_end_timeout %s",vvid.c_str());
  694. if (!vinfo.is_alarm)
  695. {
  696. log_info("vehicle_alarm_real_end_timeout_exec %s,%u",vvid.c_str(),id);
  697. event_tool::instance()->handle_event(OT_CARD,ET_VEHICLE_REAR_END,id,CYaSetting::m_sys_setting.rear_end_d,dis,true,DT_COMMON,vvid);
  698. vinfo.is_alarm = true;
  699. }
  700. }
  701. }
  702. ++it_cv;
  703. }
  704. }
  705. double rear_end_collision_prevented_business::get_absolute_distance(uint64_t c1,uint64_t c2)
  706. {
  707. point pp1=m_map[c1]._p;point pp2=m_map[c2]._p;
  708. double arg1=m_map[c1]._arg;double arg2=m_map[c2]._arg;
  709. point p1(pp1.x,-pp1.y);
  710. point p2(pp2.x,-pp2.y);
  711. if (fabs(arg1-arg2)<1e-10)
  712. return p1.dist(p2);
  713. std::stringstream ss;
  714. std::vector<point> v = card_path::inst().find_path(p1,p2);
  715. point pt = p1;double dis = 0;
  716. for (const auto & p:v)
  717. {
  718. dis += pt.dist(p);
  719. pt=p;
  720. }
  721. dis += pt.dist(p2);
  722. return dis;
  723. }
  724. bool rear_end_collision_prevented_business::rear_end_arg_algo(uint64_t u1,uint64_t u2)
  725. {
  726. point p_1=m_map[u1].last_p;point p_2=m_map[u2].last_p;
  727. double arg_1=m_map[u1]._arg;double arg_2=m_map[u2]._arg;
  728. line_v l_1=m_map[u1]._line;line_v l_2=m_map[u2]._line;
  729. std::string card1=tool_other::get_string_cardid(u1);std::string card2=tool_other::get_string_cardid(u2);
  730. if(p_1.empty() || p_2.empty())
  731. return false;
  732. log_info("[vehicle_alarm_begin_algo]%s&%s %f,%f (%s)(%s)--(%.2f,%.2f)(%.2f,%.2f)",card1.c_str(),card2.c_str(),arg_1,arg_2,l_1.to_string().c_str(),l_2.to_string().c_str(),p_1.x,p_1.y,p_2.x,p_2.y);
  733. if (point::eq(arg_1,0x12345678,1e-10) || point::eq(arg_2,0x12345678,1e-10))
  734. {
  735. log_info("[vehicle_alarm_invalid]%s&%s %f,%f",card1.c_str(),card2.c_str(),arg_1,arg_2);
  736. return false;
  737. }
  738. if (!point::eq(arg_1,0,1e-10))
  739. if (fabs(fabs(arg_1-arg_2)-3.141593)< 0.000005)
  740. {
  741. log_info("[vehicle_alarm_opposite]%s&%s",card1.c_str(),card2.c_str());
  742. return false;
  743. }
  744. if (point::eq(arg_1,arg_2,1e-10))
  745. {
  746. double arg = std::arg(std::complex<double>(p_1.x,p_1.y)-std::complex<double>(p_2.x,p_2.y));
  747. if (!(fabs(fabs(arg)-fabs(arg_1))<1e-10 || fabs(fabs(arg)+fabs(arg_2)-3.141593)< 0.000005))
  748. {
  749. log_info("[vehicle_alarm_parallel]%s&%s %f,%f()",card1.c_str(),card2.c_str(),arg,arg_1);
  750. return false;
  751. }
  752. }
  753. if(!point::eq(arg_1,arg_2,1e-10))
  754. {
  755. point p = l_1.line::crossing(l_2);
  756. if (p==l_1[1] || p==l_2[1])
  757. {
  758. log_info("[vehicle_alarm_point]%s&%s",card1.c_str(),card2.c_str());
  759. return true;
  760. }
  761. double arg1 = std::arg(std::complex<double>(p.x,p.y)-std::complex<double>(l_1[1].x,l_1[1].y));
  762. double arg2 = std::arg(std::complex<double>(p.x,p.y)-std::complex<double>(l_2[1].x,l_2[1].y));
  763. if (fabs(fabs(arg1-arg_1)-3.141593)<0.000005 && fabs(fabs(arg2-arg_2)-3.141593)<0.000005)
  764. {
  765. log_info(0,"[vehicle_alarm_backfired]%s&%s",card1.c_str(),card2.c_str());
  766. return false;
  767. }
  768. }
  769. log_info("[vehicle_alarm_end]%s&%s %f,%f",card1.c_str(),card2.c_str(),arg_1,arg_2);
  770. return true;
  771. }