three_rates_impl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include "three_rates_impl.h"
  2. #include "operating_rate.h"
  3. #include "regularcycle_rate.h"
  4. #include "common_data.h"
  5. #include <thread>
  6. #include <unistd.h>
  7. #include <string>
  8. #include <sys/time.h>
  9. #include <log.h>
  10. #include <iostream>
  11. #include <float.h>
  12. #include<numeric>
  13. //使用自己写的无锁队列
  14. //#define TR_QUEUE
  15. long long ustime(void) {
  16. struct timeval tv;
  17. long long ust;
  18. gettimeofday(&tv, NULL);
  19. ust = ((long long)tv.tv_sec)*1000000;
  20. ust += tv.tv_usec;
  21. return ust;
  22. }
  23. /* Return the UNIX time in milliseconds */
  24. long long mstime(void) {
  25. return ustime()/1000;
  26. }
  27. three_rates_impl::proxy_creator three_rates_impl::m_proxy;
  28. // 输入日志ID==log.ini中对应的日志属性(日志文件等)
  29. int three_rates_impl::g_three_rates_log_id = 2;
  30. // 更新数据的日志ID
  31. int three_rates_impl::g_three_rates_sql_log_id = 2;
  32. //
  33. three_rates_impl* three_rates_impl::_instance()
  34. {
  35. static three_rates_impl instance;
  36. return &instance;
  37. }
  38. /*
  39. * Functon: Init the three_rates module
  40. * Parameters:
  41. * init_param& para - in&out, three_rates parameters
  42. * Returns: void
  43. * Throws: void
  44. */
  45. void three_rates_impl::init(const init_para& para, const db_para& dpara )
  46. {
  47. logn_info(2,"TR-three_rates init call");
  48. m_bstop = true;
  49. m_shutdown_timeout_threshold = para.m_shutdown_timeout;
  50. if (m_shutdown_timeout_threshold <= 0)
  51. {
  52. m_shutdown_timeout_threshold = 480;
  53. }
  54. if (!m_runSo)
  55. {
  56. int ret = init_db(dpara);
  57. logn_info(2,"TR-init_db returns: %d",ret);
  58. }
  59. #ifdef TR_QUEUE
  60. p_tr_queue = new tr_queue(1024*1024);
  61. if(!p_tr_queue->initialize())
  62. {
  63. logn_error(2,"TR-Failed to init tr_queue!");
  64. }
  65. #endif
  66. //掘进面初始化数据
  67. m_drivingface_module.init_all(para.m_restart);
  68. m_poperating_rate = make_shared<operating_rate>();
  69. //采煤机正规循环率
  70. m_pregularcycle_rate = make_shared<regularcycle_rate>();
  71. m_pregularcycle_rate->init();
  72. //工作面司机与车辆距离告警
  73. m_pvehicle_driver_alarm = make_shared<vehicle_driver_alarm>();
  74. m_pvehicle_driver_alarm->init();
  75. m_pvehicle_driver_alarm->set_send_callback(para.driving_face_alarm);
  76. m_drivingface_alarm = make_shared<DrivingFaceAlarm>();
  77. m_drivingface_alarm->init();
  78. m_drivingface_alarm->set_send_callback(para.driving_face_alarm);
  79. //新增加的工作面初始化数据及公共数据
  80. m_common_data = make_shared<common_data>();
  81. m_common_data->init_data();
  82. m_time = 0;
  83. m_worktime.init_shift();
  84. return;
  85. }
  86. /*
  87. * Functon: Start the three_rates module
  88. * Parameters: void
  89. * Returns: void
  90. * Throws: void
  91. */
  92. void three_rates_impl::start()
  93. {
  94. logn_info(2,"TR-three_rates start call");
  95. m_bstop = false;
  96. m_pthree_rates_thread.reset(new std::thread(std::bind(&three_rates_impl::run,this)));
  97. m_pthree_rates_thread->detach();
  98. return;
  99. }
  100. /*
  101. * Functon: thread function
  102. * Parameters: void
  103. * Returns: void
  104. * Throws: void
  105. */
  106. void three_rates_impl::run()
  107. {
  108. logn_info(2,"TR-three_rates thread running");
  109. while(!m_bstop)
  110. {
  111. unsigned int ret = 0;
  112. if (m_DB_Changed_list.size()>0)
  113. {
  114. //处理工作面参数更改
  115. std::lock_guard<std::mutex> lock(m_mutex);
  116. for (size_t i = 0; i < m_DB_Changed_list.size(); i++)
  117. {
  118. deal_db_data_changed(m_DB_Changed_list[i]);
  119. }
  120. m_DB_Changed_list.clear();
  121. }
  122. card_pos pos;
  123. #ifdef TR_QUEUE
  124. ret = p_tr_queue->get((unsigned char*)&pos, sizeof(card_pos));
  125. #if 0 //for test
  126. if(pos.type == 1 || pos.type == 2 || pos.type == 0)
  127. {
  128. std::this_thread::sleep_for (std::chrono::milliseconds(1));
  129. continue;
  130. }
  131. #endif
  132. #else
  133. m_boost_queue.pop(pos);
  134. ret = sizeof(pos);
  135. if (pos.id <= 0)
  136. {
  137. std::this_thread::sleep_for (std::chrono::milliseconds(1));
  138. continue ;
  139. }
  140. #endif
  141. if(sizeof(card_pos) == ret)
  142. {
  143. //cout << "_______get ret: "<< ret <<",type: " << pos.type << ",id: " << pos.id << endl;
  144. logn_debug(2,"TR-FIFO three_rates_impl::run, type= %d , id: %d",pos.type, pos.id);
  145. }
  146. else
  147. {
  148. std::this_thread::sleep_for (std::chrono::milliseconds(1));
  149. continue;
  150. }
  151. add_cardsyncNum(pos.id);
  152. int vehicle_type = 0;
  153. if(pos.type !=THREE_RATES_CARD_TYPE::CT_PERSON)
  154. {
  155. //更新卡数据
  156. std::string scard_id = tr_helper::merge_typy_id_to_card(pos.type, pos.id);
  157. auto it_map = m_poperating_rate->m_coaldrivingface_map.find(pos.identifier_id);
  158. if (it_map != m_poperating_rate->m_coaldrivingface_map.end())
  159. {
  160. vehicle_type = it_map->second->m_vehicle_type;
  161. // store vibrate state
  162. it_map->second->m_cb_state.push_back(pos.vibration);
  163. //it_map->second->m_nvehicle_id = it_map->second->id = m_poperating_rate->m_coaldrivingface_map[it_map->second->m_scard_id]->m_nvehicle_id;
  164. it_map->second->m_ncard_type = pos.type;
  165. it_map->second->rec_time = pos.rec_time;
  166. it_map->second->x = pos.x;
  167. it_map->second->y = pos.y;
  168. it_map->second->z = pos.z;
  169. it_map->second->final_v = pos.final_v;
  170. it_map->second->m_ndept_id = pos.dpt_id;
  171. //设置当前使用的地图
  172. m_common_data->set_map_id(pos.map_id);
  173. // process operating rate 开机率
  174. m_poperating_rate->store_machine_state(it_map->second);
  175. if (tr_helper::is_coalminingface(it_map->second))
  176. {
  177. // process regularcycle rate 采煤机业务
  178. m_pregularcycle_rate->Calc_CoalMining_RegularCycleRate(it_map->second);
  179. }
  180. else if (tr_helper::is_drivingface(it_map->second))
  181. {
  182. //deal drivingface card 掘进机业务
  183. std::shared_ptr<card_pos> p_card_pos = std::make_shared<card_pos>();
  184. p_card_pos->copy(pos);
  185. auto it_drivingface_card = m_drivingface_module.mp_drivingface_card_list.find(scard_id);
  186. if (it_drivingface_card != m_drivingface_module.mp_drivingface_card_list.end()) {
  187. m_drivingface_module.deal_drivingface_service(it_drivingface_card->second, p_card_pos);
  188. }
  189. //处理分站距离超限报警
  190. if (m_drivingface_alarm != nullptr) {
  191. DrivingCard tmpCard;
  192. tmpCard.x = pos.x;
  193. tmpCard.y = pos.y;
  194. tmpCard.vehicle_card_id = pos.id;
  195. tmpCard.vehicle_id = it_map->second->m_nvehicle_id; //车卡ID
  196. tmpCard.card_type = pos.type;
  197. tmpCard.reader_id = pos.reader_id;
  198. tmpCard.reader_x = pos.reader_x;
  199. tmpCard.reader_y = pos.reader_y;
  200. tmpCard.map_scale = m_common_data->m_map_scale;
  201. if (tmpCard.map_scale > 0) {
  202. m_drivingface_alarm->driving_alarm(tmpCard);
  203. }
  204. }
  205. }
  206. } // end of if
  207. }
  208. //工作面司机与车辆的距离告警
  209. SDeal_Card_Pos s;
  210. s.Copy(pos);
  211. s.dentifier_type = vehicle_type;
  212. m_pvehicle_driver_alarm->deal_vehicle_driver_alarm(s);
  213. std::this_thread::sleep_for (std::chrono::milliseconds(1));
  214. }// end of while
  215. }
  216. /*
  217. * Functon: Stop the three_rates module
  218. * Parameters: void
  219. * Returns: void
  220. * Throws: void
  221. */
  222. void three_rates_impl::stop()
  223. {
  224. logn_info(2,"TR-three_rates stop call");
  225. m_bstop = true;
  226. #ifdef TR_QUEUE
  227. delete p_tr_queue;
  228. p_tr_queue = nullptr;
  229. #endif
  230. return;
  231. }
  232. /*
  233. * Functon: Input the located datas
  234. * Parameters:
  235. * card_pos& pos - in, position informations
  236. * Returns: void
  237. * Throws: void
  238. */
  239. void three_rates_impl::put(const card_pos& pos)
  240. {
  241. // if( pos.type == 2)
  242. //{
  243. // return;
  244. //}
  245. #ifdef TR_QUEUE
  246. unsigned int ret = p_tr_queue->put((unsigned char*)&pos, sizeof(card_pos));
  247. //cout << "put ret: "<< ret <<",type: " << pos.type << ",id: " << pos.id << endl;
  248. logn_info(2,"TR-FIFO three_rates_impl::put size=%d , type= %d , id: %d",ret,pos.type, pos.id);
  249. return ;
  250. #endif
  251. m_boost_queue.push(pos);
  252. return;
  253. }
  254. /*
  255. * Functon: Enter one area
  256. * Parameters:
  257. * card_pos& pos - in, the located datas
  258. * area_data& area - in, the area informations
  259. * Returns: void
  260. * Throws: void
  261. */
  262. void three_rates_impl::enter_area(std::shared_ptr<card_pos> &card_ptr, std::shared_ptr<area_data> &area_ptr)
  263. {
  264. logn_info(2,"TR-three_rates enter_area call");
  265. m_worktime.enter_area(card_ptr, area_ptr);
  266. }
  267. /*
  268. * Functon: Leave one area
  269. * Parameters:
  270. * card_pos& pos - in, the located datas
  271. * area_data& area - in, the area informations
  272. * Returns: void
  273. * Throws: void
  274. */
  275. void three_rates_impl::leave_area(std::shared_ptr<card_pos> &card_ptr, std::shared_ptr<area_data> &area_ptr)
  276. {
  277. logn_info(2,"TR-three_rates leave_area call");
  278. m_worktime.leave_area(card_ptr, area_ptr);
  279. return;
  280. }
  281. /*
  282. * Functon: Init database settings
  283. * Parameters: dpara, dp_rara database configure parameters
  284. * Returns: int, returns 0 normally, otherwise it returns 1
  285. * Throws: void
  286. */
  287. int three_rates_impl::init_db(const db_para& dpara)
  288. {
  289. m_db_setting.Host = dpara.Host;
  290. m_db_setting.User = dpara.User;
  291. m_db_setting.PWD = dpara.PWD;
  292. m_db_setting.DBName = dpara.DBName;
  293. m_db_setting.CharSet = dpara.CharSet;
  294. m_db_setting.TimeOut = dpara.TimeOut;
  295. m_db_setting.PoolSize = dpara.PoolSize;
  296. m_db_setting.Port = dpara.Port;
  297. string Error;
  298. if ( !sDBConnPool.Create( m_db_setting, Error ) )
  299. {
  300. logn_error(2,"TR-database pool create failed: %s", Error.c_str());
  301. return 1;
  302. }
  303. logn_info(2,"TR-database pool creat successed!");
  304. return 0;
  305. }
  306. /*
  307. * 获取掘进机或者采煤机的开机状态
  308. */
  309. bool three_rates_impl::GetVehicleState(int vehicleId)
  310. {
  311. std::string card_id = m_common_data->get_card_id(vehicleId);
  312. if (card_id == "")
  313. {
  314. return false;
  315. }
  316. auto it_map = m_poperating_rate->m_coaldrivingface_map.find( vehicleId );
  317. if (it_map == m_poperating_rate->m_coaldrivingface_map.end())
  318. {
  319. return false;
  320. }
  321. std::shared_ptr<coaldrivingface_card> & card = it_map->second;
  322. if(tr_helper::is_drivingface(card) || tr_helper::is_coalminingface(card))
  323. {
  324. int sum = std::accumulate(card->m_cb_state.begin(), card->m_cb_state.end(), 0);
  325. // 判断采煤机/掘进机惯导开关标
  326. int openThreshold = operating_rate::STARTUP_STATE_COUNT * card->m_nshake_threshold;
  327. // 判断为开机,
  328. if (sum >= openThreshold || card->m_bopen)
  329. {
  330. return true;
  331. }
  332. // 判断为关机
  333. int closeThreshold = operating_rate::SHUTDOWN_STATE_COUNT * card->m_nshake_threshold;
  334. if (sum <= closeThreshold || !card->m_bopen)
  335. {
  336. return false;
  337. }
  338. }
  339. return false;
  340. }
  341. void three_rates_impl::update_db_data(const std::string& webname, const std::string& param,int edit_type_id)
  342. {
  343. ChangedDBDate Db;
  344. Db.webname = webname;
  345. Db.param = param;
  346. Db.edit_type_id = edit_type_id;
  347. std::lock_guard<std::mutex> lock(m_mutex);
  348. m_DB_Changed_list.push_back(Db);
  349. logn_info(2,"update_db_data : edit_typeid=%d,webname=%s,Param=%s Size=%d"
  350. ,edit_type_id,webname.c_str(),param.c_str(),m_DB_Changed_list.size());
  351. }
  352. void three_rates_impl::deal_db_data_changed(const ChangedDBDate& Db)
  353. {
  354. logn_info(2,"deal_db_data_changed : edit_typeid=%d,webname=%s,Param=%s"
  355. ,Db.edit_type_id,Db.webname.c_str(),Db.param.c_str());
  356. if (Db.webname == "coalface_vehicle")
  357. {
  358. //增加
  359. if (Db.edit_type_id==0 || Db.edit_type_id==1)
  360. {
  361. m_pregularcycle_rate->init_data_coalmining_RegularCycle(Db.param);
  362. }
  363. else if (Db.edit_type_id == 2 )
  364. {
  365. m_pregularcycle_rate->remove_data_coalmining_RegularCycle(atoi(Db.param.c_str()));
  366. }
  367. m_pvehicle_driver_alarm->init_coalface_driver_warningparam();
  368. m_poperating_rate->init_coaldrivingface_vehicle();
  369. }
  370. else if (Db.webname == "coalface")
  371. {
  372. //增加和更新
  373. if (Db.edit_type_id == 0 || Db.edit_type_id == 1)
  374. {
  375. m_pregularcycle_rate->init_data_coalminingface_RegularCycle(Db.param);
  376. }
  377. else if (Db.edit_type_id == 2)
  378. {
  379. m_pregularcycle_rate->remove_data_coalminingface_RegularCycle(atoi(Db.param.c_str()));
  380. }
  381. m_pvehicle_driver_alarm->init_coalface_driver_warningparam();
  382. m_poperating_rate->init_coaldrivingface_vehicle();
  383. }
  384. else if (Db.webname == "drivingface")
  385. {
  386. //增加和更新
  387. if (Db.edit_type_id == 0 || Db.edit_type_id == 1)
  388. {
  389. m_drivingface_module.init_drivingface_card(Db.param);
  390. m_pvehicle_driver_alarm->init_driving_driver_warningparam(Db.param);
  391. }
  392. else if (Db.edit_type_id == 2)
  393. {
  394. m_drivingface_module.remove_drivingface(atoi(Db.param.c_str()));
  395. }
  396. m_poperating_rate->init_coaldrivingface_vehicle();
  397. }
  398. else if (Db.webname == "drivingface_vehicle")
  399. {
  400. if(Db.edit_type_id == 2)
  401. {
  402. m_drivingface_alarm->del_drivingface(atoi(Db.param.c_str()));
  403. }
  404. else
  405. {
  406. m_pvehicle_driver_alarm->init_driving_driver_warningparam();
  407. m_drivingface_alarm->init_drivingface_db(atoi(Db.param.c_str()));
  408. }
  409. m_poperating_rate->init_coaldrivingface_vehicle();
  410. }
  411. else if (Db.webname == "drivingface_ref_point")
  412. {
  413. m_drivingface_module.init_drivingface_ref_points(Db.param);
  414. }
  415. }
  416. //添加次数
  417. void three_rates_impl::add_cardsyncNum(int id)
  418. {
  419. std::map<int,int>::iterator it = m_card_sync_num.find(id);
  420. if (it == m_card_sync_num.end())
  421. {
  422. m_card_sync_num[id] = 1;
  423. }
  424. else
  425. {
  426. ++ it->second;
  427. }
  428. }