vehicle_driver_alarm.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. #include "vehicle_driver_alarm.h"
  2. #include "db_api/CDBSingletonDefine.h"
  3. #include "log.h"
  4. #include "tr_tool.h"
  5. #include <iostream>
  6. #include <float.h>
  7. #include "three_rates.h"
  8. vehicle_driver_alarm::vehicle_driver_alarm()
  9. {
  10. }
  11. void vehicle_driver_alarm::init()
  12. {
  13. init_coalface_driver_warningparam();
  14. init_driving_driver_warningparam();
  15. }
  16. void vehicle_driver_alarm::init_coalface_driver_warningparam()
  17. {
  18. std::string sql ="SELECT ve.vehicle_id,ve.dept_id,b.coalface_id , b.driver_dist,b.driver_dist_time"
  19. " from dat_vehicle_extend ve left join dat_coalface_vehicle b on ve.vehicle_id = b.vehicle_id "
  20. " where b.vehicle_id = ve.vehicle_id";
  21. std::string Error;
  22. YADB::CDBResultSet DBRes;
  23. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  24. int nCount = DBRes.GetRecordCount( Error );
  25. if (nCount > 0)
  26. {
  27. while ( DBRes.GetNextRecod(Error) )
  28. {
  29. int vehicle_id = -1;
  30. if (!DBRes.GetField( "vehicle_id",vehicle_id, Error ))
  31. {
  32. continue;
  33. }
  34. uint32_t dept_id = 0;
  35. if (!DBRes.GetField( "dept_id",dept_id, Error ))
  36. {
  37. continue;
  38. }
  39. int coalface_id = -1;
  40. if (!DBRes.GetField( "coalface_id",coalface_id, Error ))
  41. {
  42. continue;
  43. }
  44. double driver_dist = 0;
  45. if (!DBRes.GetField( "driver_dist",driver_dist, Error ))
  46. {
  47. continue;
  48. }
  49. int driver_dist_time = 0;
  50. if (!DBRes.GetField( "driver_dist_time",driver_dist_time, Error ))
  51. {
  52. continue;
  53. }
  54. std::shared_ptr<WorkfaceParam> pWorkfaceParam = nullptr;
  55. WorkfaceParamMap::iterator it_f= m_WorkfaceParamMap.find(coalface_id);
  56. if (it_f == m_WorkfaceParamMap.end())
  57. {
  58. pWorkfaceParam = std::make_shared<WorkfaceParam>();
  59. m_WorkfaceParamMap.insert(make_pair(coalface_id, pWorkfaceParam));
  60. }
  61. else
  62. {
  63. pWorkfaceParam = it_f->second;
  64. }
  65. pWorkfaceParam->dist = driver_dist;
  66. pWorkfaceParam->dist_time = driver_dist_time;
  67. std::shared_ptr<VehiclePos> pos= nullptr;
  68. VehiclePosMap::iterator it_p =m_CoalVehiclePosMap.find(vehicle_id);
  69. if (it_p == m_CoalVehiclePosMap.end())
  70. {
  71. pos= std::make_shared<VehiclePos>();
  72. m_CoalVehiclePosMap.insert(std::make_pair(vehicle_id,pos));
  73. }
  74. else
  75. {
  76. pos = it_p->second;
  77. }
  78. pos->dept_id =dept_id;
  79. pos->workface_id = coalface_id;
  80. pos->starttime =time(NULL);
  81. pos->on_vehicle_state_changed(false);
  82. }
  83. }
  84. else
  85. {
  86. logn_error(2,"采煤面车卡与司机距离告警查询失败,sql=%s,", sql.c_str());
  87. }
  88. }
  89. void vehicle_driver_alarm::init_driving_driver_warningparam(const std::string & sz_drivingface_id/* = "-1"*/)
  90. {
  91. std::string sql ="SELECT ve.vehicle_id,ve.dept_id,b.drivingface_id , b.driver_dist,b.driver_dist_time"
  92. " from dat_vehicle_extend ve left join dat_drivingface_vehicle b on ve.vehicle_id = b.vehicle_id "
  93. " where b.vehicle_id = ve.vehicle_id";
  94. if (std::atoi(sz_drivingface_id.c_str()) > 0)
  95. {
  96. sql += " and b.drivingface_id = " + sz_drivingface_id ;
  97. }
  98. sql.append(" ;");
  99. std::string Error;
  100. YADB::CDBResultSet DBRes;
  101. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  102. int nCount = DBRes.GetRecordCount( Error );
  103. if (nCount > 0)
  104. {
  105. while ( DBRes.GetNextRecod(Error) )
  106. {
  107. int vehicle_id = -1;
  108. if (!DBRes.GetField( "vehicle_id",vehicle_id, Error ))
  109. {
  110. continue;
  111. }
  112. uint32_t dept_id = 0;
  113. if (!DBRes.GetField( "dept_id",dept_id, Error ))
  114. {
  115. continue;
  116. }
  117. int db_drivingface_id = -1;
  118. if (!DBRes.GetField( "drivingface_id",db_drivingface_id, Error ))
  119. {
  120. continue;
  121. }
  122. double driver_dist = 0;
  123. if (!DBRes.GetField( "driver_dist",driver_dist, Error ))
  124. {
  125. continue;
  126. }
  127. int driver_dist_time = 0;
  128. if (!DBRes.GetField( "driver_dist_time",driver_dist_time, Error ))
  129. {
  130. continue;
  131. }
  132. std::shared_ptr<WorkfaceParam> pWorkfaceParam = nullptr;
  133. WorkfaceParamMap::iterator it_f= m_WorkfaceParamMap.find(db_drivingface_id);
  134. if (it_f == m_WorkfaceParamMap.end())
  135. {
  136. pWorkfaceParam = std::make_shared<WorkfaceParam>();
  137. m_WorkfaceParamMap.insert(make_pair(db_drivingface_id, pWorkfaceParam));
  138. }
  139. else
  140. {
  141. pWorkfaceParam = it_f->second;
  142. }
  143. pWorkfaceParam->dist = driver_dist;
  144. pWorkfaceParam->dist_time = driver_dist_time;
  145. std::shared_ptr<VehiclePos> pos= nullptr;
  146. VehiclePosMap::iterator it_p =m_DrivingVehiclePosMap.find(vehicle_id);
  147. if (it_p == m_DrivingVehiclePosMap.end())
  148. {
  149. pos= std::make_shared<VehiclePos>();
  150. m_DrivingVehiclePosMap.insert(std::make_pair(vehicle_id,pos));
  151. }
  152. else
  153. {
  154. pos = it_p->second;
  155. }
  156. pos->dept_id =dept_id;
  157. pos->workface_id = db_drivingface_id;
  158. pos->on_vehicle_state_changed(false);
  159. }
  160. }
  161. else
  162. {
  163. logn_error(2,"挖掘面车卡与司机距离告警查询失败,sql=%s,", sql.c_str());
  164. }
  165. logn_info(2,"init_driving_driver_warningparam: drivingface=%s,sqlcount=%d sql=%s"
  166. ,sz_drivingface_id.c_str(),nCount,sql.c_str());
  167. }
  168. std::shared_ptr<VehiclePos> vehicle_driver_alarm::get_vehicle_pos(int _id,int _type)
  169. {
  170. #define GET_VEHICLE_POS_D(M,_ID) \
  171. VehiclePosMap::iterator it_p = M.find(_ID);\
  172. if (it_p != M.end()){\
  173. return it_p->second;\
  174. }\
  175. return nullptr;\
  176. if (tr_helper::is_coalminingface(_type) )
  177. {
  178. GET_VEHICLE_POS_D(m_CoalVehiclePosMap,_id);
  179. }
  180. else
  181. {
  182. GET_VEHICLE_POS_D(m_DrivingVehiclePosMap,_id);
  183. }
  184. #undef GET_VEHICLE_POS_D
  185. return nullptr;
  186. }
  187. void vehicle_driver_alarm::deal_vehicle_driver_alarm(SDeal_Card_Pos & cp)
  188. {
  189. if (cp.type == THREE_RATES_CARD_TYPE::CT_PERSON
  190. && (cp.work_type_id == E_THREE_VEHICLE_TYPE::E_COALMINING_DRIVER
  191. || cp.work_type_id == E_THREE_VEHICLE_TYPE::E_DRIVING_DRIVER ))
  192. {
  193. DriverPosMap::iterator it_p =m_DriverPosMap.find(cp.identifier_id);
  194. std::shared_ptr<DriverPos> driverPos =nullptr;
  195. if (it_p != m_DriverPosMap.end())
  196. {
  197. driverPos = it_p->second;
  198. }
  199. else
  200. {
  201. driverPos = std::make_shared<DriverPos>();
  202. m_DriverPosMap.insert(std::make_pair(cp.identifier_id,driverPos));
  203. driverPos->dept_id = 0;
  204. driverPos->on_vehicle_state_changed(false);
  205. }
  206. if (driverPos->dept_id != cp.dpt_id)
  207. {
  208. add_driver_to_vehicle(cp,driverPos);
  209. }
  210. if (driverPos->isOn )
  211. {
  212. driverPos->calc_vehicle_driver_pos(cp.x,cp.y);
  213. }
  214. }
  215. else if (tr_helper::is_coalminingface(cp.dentifier_type) || tr_helper::is_drivingface(cp.dentifier_type))
  216. {
  217. std::shared_ptr<VehiclePos> vehiclePos_ptr = get_vehicle_pos(cp.identifier_id,cp.dentifier_type);
  218. if(nullptr == vehiclePos_ptr)
  219. {
  220. return;
  221. }
  222. bool statechange =false;
  223. bool bcalc=false;
  224. std::shared_ptr<WorkfaceParam> faceParam = nullptr;
  225. WorkfaceParamMap::iterator it_v = m_WorkfaceParamMap.find(vehiclePos_ptr->workface_id);
  226. if (it_v != m_WorkfaceParamMap.end())
  227. {
  228. faceParam = it_v->second;
  229. }
  230. else
  231. {
  232. return;
  233. }
  234. bool m_bOpen = three_rates::get_instance()->GetVehicleState(cp.identifier_id);
  235. if (m_bOpen^vehiclePos_ptr->isOn)
  236. {
  237. vehiclePos_ptr->isOn = m_bOpen;
  238. vehiclePos_ptr->reset_vehicle_driver_pos();
  239. statechange= true;
  240. }
  241. if (m_bOpen )
  242. {
  243. vehiclePos_ptr->calc_vehicle_driver_pos(cp.x,cp.y);
  244. int dist_time=faceParam ->dist_time;
  245. if (time(NULL)- vehiclePos_ptr->starttime > dist_time)
  246. {
  247. bcalc =true;
  248. }
  249. }
  250. if (statechange || bcalc)
  251. {
  252. uint64_t id = cp.type<<32|cp.id;
  253. double mean_vx= 0;
  254. double mean_vy= 0;
  255. if (bcalc)
  256. {
  257. if (vehiclePos_ptr->PointNum >0)
  258. {
  259. mean_vx =vehiclePos_ptr->Sum_x/vehiclePos_ptr->PointNum;
  260. mean_vy = vehiclePos_ptr->Sum_y/vehiclePos_ptr->PointNum;
  261. vehiclePos_ptr->reset_vehicle_driver_pos();
  262. }
  263. }
  264. double minDistance = FLT_MAX;
  265. std::vector<uint32_t>& drivelist = vehiclePos_ptr->m_drivelist;
  266. if (drivelist.size()<=0)
  267. {
  268. //开始报警
  269. if (m_send_callback && bcalc)
  270. {
  271. m_send_callback(9, ET_COALING_DRIVING_DRIVER, id,
  272. faceParam->dist, minDistance, true);
  273. //log_error("工作车开机,但没有司机,id=%l,", id);
  274. }
  275. return;
  276. }
  277. double distance= 0;
  278. for (size_t i =0;i < drivelist.size();i++)
  279. {
  280. if (bcalc)
  281. {
  282. DriverPosMap::iterator it_p =m_DriverPosMap.find(drivelist[i]);
  283. if (it_p != m_DriverPosMap.end())
  284. {
  285. if (it_p->second->calc_vehicle_driver_distance(mean_vx,mean_vy,distance))
  286. {
  287. if (distance < minDistance)
  288. {
  289. minDistance = distance;
  290. }
  291. }
  292. }
  293. }
  294. if (statechange)
  295. {
  296. vehiclePos_ptr->on_vehicle_state_changed(m_bOpen);
  297. }
  298. }
  299. if (bcalc)
  300. {
  301. if (minDistance > faceParam->dist)
  302. {
  303. //开始报警
  304. if (m_send_callback)
  305. {
  306. m_send_callback(9, ET_COALING_DRIVING_DRIVER, id,
  307. faceParam->dist, minDistance, true);
  308. }
  309. //log_info("工作车和司机距离超过门限,开始告警,id=%l,", id);
  310. }
  311. else
  312. {
  313. if (m_send_callback)
  314. {
  315. //结束报警
  316. m_send_callback(9, ET_COALING_DRIVING_DRIVER, id,
  317. faceParam->dist, minDistance, false);
  318. //log_info("工作车和司机距离未超过门限,结束告警,id=%l,", id);
  319. }
  320. }
  321. }
  322. if (statechange && !m_bOpen)
  323. {
  324. if (m_send_callback)
  325. {
  326. //结束报警
  327. m_send_callback(9, ET_COALING_DRIVING_DRIVER, id,
  328. faceParam->dist, 0, false);
  329. //log_info("工作车关机,结束车卡与司机距离告警,id=%l,", id);
  330. }
  331. }
  332. }
  333. }
  334. }
  335. void vehicle_driver_alarm::add_driver_to_vehicle(SDeal_Card_Pos &cp, std::shared_ptr<DriverPos> driverPos)
  336. {
  337. if (cp.work_type_id == E_THREE_VEHICLE_TYPE::E_COALMINING_DRIVER)
  338. {
  339. add_driver_to_vehicle(m_CoalVehiclePosMap,cp,driverPos);
  340. }
  341. else if (cp.work_type_id == E_THREE_VEHICLE_TYPE::E_DRIVING_DRIVER)
  342. {
  343. add_driver_to_vehicle(m_DrivingVehiclePosMap,cp,driverPos);
  344. }
  345. }
  346. void vehicle_driver_alarm::add_driver_to_vehicle(VehiclePosMap& vehiclePosMap, SDeal_Card_Pos &cp,std::shared_ptr<DriverPos> driverPos)
  347. {
  348. VehiclePosMap::iterator it_s = vehiclePosMap.begin();
  349. bool badd = false;
  350. bool breduce = false;
  351. uint32_t & dept_id = driverPos->dept_id ;
  352. if (dept_id == 0)
  353. {
  354. breduce =true;
  355. }
  356. while (it_s != vehiclePosMap.end() )
  357. {
  358. if (it_s->second->dept_id == cp.dpt_id)
  359. {
  360. if (!badd)
  361. {
  362. std::vector<uint32_t>& driverlist = it_s->second->m_drivelist;
  363. std::vector<uint32_t>::iterator it_d = std::find(driverlist.begin(),driverlist.end(),cp.identifier_id);
  364. if (it_d == driverlist.end())
  365. {
  366. driverlist.push_back(cp.identifier_id);
  367. driverPos->isOn = it_s->second->isOn ;
  368. driverPos->dept_id = it_s->second->dept_id ;
  369. badd =true;
  370. }
  371. }
  372. }
  373. else if (it_s->second->dept_id == dept_id)
  374. {
  375. if (!breduce)
  376. {
  377. std::vector<uint32_t>& driverlist = it_s->second->m_drivelist;
  378. std::vector<uint32_t>::iterator it_d = std::find(driverlist.begin(),driverlist.end(),cp.identifier_id);
  379. if (it_d != driverlist.end())
  380. {
  381. driverlist.erase(it_d);
  382. breduce = true;
  383. }
  384. }
  385. }
  386. if (badd && breduce)
  387. {
  388. return;
  389. }
  390. it_s ++;
  391. }
  392. }
  393. void DriverPos::calc_vehicle_driver_pos(double x,double y)
  394. {
  395. if (isOn)
  396. {
  397. if (PointNum == 0)
  398. {
  399. starttime = time(NULL);
  400. }
  401. Sum_x += x;
  402. Sum_y += y;
  403. PointNum++;
  404. }
  405. }
  406. /*重置记录的位置信息
  407. *
  408. * param
  409. *
  410. * return
  411. * 是否计算成功,true为成功,false失败,没有记录位置点
  412. */
  413. void DriverPos::reset_vehicle_driver_pos()
  414. {
  415. PointNum = 0;
  416. Sum_x = 0;
  417. Sum_y =0;
  418. }
  419. bool DriverPos::calc_vehicle_driver_distance(double mean_vx,double mean_vy,double& distance)
  420. {
  421. if (PointNum >0 )
  422. {
  423. double meanx = Sum_x/PointNum - mean_vx;
  424. double meany = Sum_y/PointNum - mean_vy;
  425. //double meanz = driverPos->Sum_z/driverPos->PointNum - mean_vz;
  426. distance = sqrt(meanx * meanx + meany * meany );
  427. reset_vehicle_driver_pos();
  428. return true;
  429. }
  430. return false;
  431. }
  432. void DriverPos::on_vehicle_state_changed(bool m_bOpen)
  433. {
  434. isOn = m_bOpen;
  435. reset_vehicle_driver_pos();
  436. }