mine_business.cpp 27 KB

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