mine_business.cpp 27 KB

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