operating_rate.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. #include "operating_rate.h"
  2. #include "db_api/CDBSingletonDefine.h"
  3. #include "log.h"
  4. #include "three_rates_impl.h"
  5. #include <numeric>
  6. #include "common_data.h"
  7. /*
  8. * Functon: Constructor
  9. */
  10. operating_rate::operating_rate():m_bzero(false),m_brestart(true)
  11. {
  12. init_coaldrivingface_vehicle();
  13. }
  14. /*
  15. * Functon: Destructor
  16. */
  17. operating_rate::~operating_rate()
  18. {
  19. }
  20. /*
  21. * Functon: Init drivingface and coalface vehicle
  22. * Parameters: void
  23. * Returns: int, returns 0 normally, otherwise it returns 1
  24. * Throws: void
  25. */
  26. int operating_rate::init_coaldrivingface_vehicle()
  27. {
  28. int ret = 0;
  29. m_coaldrivingface_map.clear();
  30. // init coaldrivingface card
  31. const char * sql_drivingface = "select a.drivingface_id,c.vehicle_type_id, b.vehicle_id, b.card_id,b.dept_id from dat_drivingface_vehicle a, dat_vehicle_extend b ,dat_vehicle as c where a.vehicle_id = b.vehicle_id and a.vehicle_id = c.vehicle_id;";
  32. const char * sql_coalface = "select a.coalface_id, c.vehicle_type_id, b.vehicle_id, b.card_id,b.dept_id from dat_coalface_vehicle a, dat_vehicle_extend b ,dat_vehicle as c where a.vehicle_id = b.vehicle_id and a.vehicle_id = c.vehicle_id;";
  33. std::string Error;
  34. YADB::CDBResultSet DBRes;
  35. if (0 == sDBConnPool.Query(sql_drivingface,DBRes,Error))
  36. {
  37. logn_error(2,"TR-init_coaldrivingface_vehicle drivingface Query exist error, error: %s", Error.c_str());
  38. return 1;
  39. }
  40. int nCount = DBRes.GetRecordCount( Error );
  41. if (nCount > 0)
  42. {
  43. while ( DBRes.GetNextRecod(Error) )
  44. {
  45. bool bRet = true;
  46. int vehicle_id = 0;
  47. int ndept_id = 0;
  48. std::string card_id;
  49. int vehicle_type_id = 0;
  50. int db_work_face_id = 0;
  51. bRet = DBRes.GetField( "vehicle_id",vehicle_id, Error );
  52. bRet = DBRes.GetField( "card_id",card_id, Error );
  53. bRet = DBRes.GetField( "dept_id",ndept_id, Error );
  54. DBRes.GetField( "vehicle_type_id",vehicle_type_id, Error );
  55. DBRes.GetField( "drivingface_id",db_work_face_id, Error );
  56. if(!bRet)
  57. {
  58. logn_error(2,"TR-init_coaldrivingface_vehicle drivingface GetField exist error, error: %s", Error.c_str());
  59. }
  60. printf("coaldrivingface_card cardid=%s,vehicleid=%d \n",card_id.c_str(),vehicle_id);
  61. std::shared_ptr<coaldrivingface_card> dr_card = std::make_shared<coaldrivingface_card>();
  62. dr_card->m_scard_id = card_id;
  63. dr_card->m_nvehicle_id = vehicle_id;
  64. dr_card->m_ndept_id = ndept_id;
  65. dr_card->m_vehicle_type = vehicle_type_id;
  66. dr_card->m_work_face_id = db_work_face_id;
  67. dr_card->m_ncard_type = THREE_RATES_CARD_TYPE::CT_TUNNELBORING_MACHINE;
  68. get_vibratethrethold(dr_card,dr_card->m_nshake_threshold);
  69. m_coaldrivingface_map.insert(make_pair(vehicle_id,dr_card));
  70. }
  71. }
  72. else
  73. {
  74. ret = 1;
  75. // there is no drivingface vehicle in db
  76. logn_warn(2,"TR-there is no drivingface vehicle in db");
  77. }
  78. if (0 == sDBConnPool.Query(sql_coalface,DBRes,Error))
  79. {
  80. logn_error(2,"TR-init_coaldrivingface_vehicle coalface Query exist error, error: %s", Error.c_str());
  81. return 1;
  82. }
  83. nCount = DBRes.GetRecordCount( Error );
  84. if (nCount > 0)
  85. {
  86. while ( DBRes.GetNextRecod(Error) )
  87. {
  88. bool bRet = true;
  89. int vehicle_id = 0;
  90. std::string card_id = "";
  91. int ndept_id = 0;
  92. int vehicle_type_id = 0;
  93. int db_work_face_id = 0;
  94. bRet = DBRes.GetField( "vehicle_id",vehicle_id, Error );
  95. bRet = DBRes.GetField( "card_id",card_id, Error );
  96. bRet = DBRes.GetField( "dept_id",ndept_id, Error );
  97. DBRes.GetField( "vehicle_type_id", vehicle_type_id, Error );
  98. DBRes.GetField( "coalface_id",db_work_face_id, Error );
  99. if(!bRet)
  100. {
  101. logn_error(2,"TR-init_coaldrivingface_vehicle coalface GetField exist error, error: %s", Error.c_str());
  102. }
  103. std_info("coaldrivingface_card cardid=%s,vehicleid=%d \n",card_id.c_str(),vehicle_id);
  104. std::shared_ptr<coaldrivingface_card> co_card = std::make_shared<coaldrivingface_card>();
  105. co_card->m_scard_id = card_id;
  106. co_card->m_nvehicle_id = vehicle_id;
  107. co_card->m_ndept_id = ndept_id;
  108. co_card->m_vehicle_type = vehicle_type_id;
  109. co_card->m_work_face_id = db_work_face_id;
  110. co_card->m_ncard_type = THREE_RATES_CARD_TYPE::CT_COALMINING_MACHINE;
  111. get_vibratethrethold(co_card,co_card->m_nshake_threshold);
  112. m_coaldrivingface_map.insert(make_pair(vehicle_id,co_card));
  113. }
  114. }
  115. else
  116. {
  117. ret = 1;
  118. // there is no coalface vehicle in db
  119. logn_warn(2,"TR-there is no coalface vehicle in db");
  120. }
  121. return ret;
  122. }
  123. /*
  124. * Functon: Update the unprocessed record across days 零点时关机再开机动作
  125. * Parameters: void
  126. * Returns: int, returns 0 normally, otherwise it returns 1
  127. * Throws: void
  128. */
  129. int operating_rate::zero_update_unprocess_record(time_t tCurTime)
  130. {
  131. char strsql[tr_helper::SQL_LENGTH] = {0};
  132. /*const char * sql = "select a.vehicle_id,a.start_up_time,a.schedule_work_time,a.dept_id,b.card_id \
  133. from his_startup_detail a, dat_vehicle_extend b \
  134. where a.proc_tag = 0 and a.vehicle_id = b.vehicle_id;";
  135. */
  136. sprintf(strsql,"(SELECT a.work_face_id,a.start_up_time,a.schedule_work_time,a.dept_id,f.card_id, a.proc_tag "
  137. "FROM his_startup_detail a, (select e.card_id, b.coalface_id "
  138. "from dat_vehicle_extend e, dat_coalface_vehicle b "
  139. "where b.vehicle_id = e.vehicle_id) f "
  140. "WHERE a.proc_tag = 0 and f.coalface_id = a.work_face_id)"
  141. "union"
  142. "(SELECT a.work_face_id,a.start_up_time,a.schedule_work_time,a.dept_id,f.card_id, a.proc_tag "
  143. "FROM his_startup_detail a, (select e.card_id, b.drivingface_id "
  144. "from dat_vehicle_extend e, dat_drivingface_vehicle b "
  145. "where b.vehicle_id = e.vehicle_id) f "
  146. "WHERE a.proc_tag = 0 and f.drivingface_id = a.work_face_id);");
  147. // 零点分割,开机时间更新到“xxxx-xx-xx 00:00:00”
  148. time_t tTime = tCurTime + 1;
  149. std::string Error;
  150. YADB::CDBResultSet DBRes;
  151. if (0 == sDBConnPool.Query(strsql,DBRes,Error))
  152. {
  153. logn_error(2,"TR-zero_update_unprocess_record Query exist error, error: %s", Error.c_str());
  154. return 1;
  155. }
  156. int nCount = DBRes.GetRecordCount( Error );
  157. if (nCount <= 0)
  158. {
  159. logn_errno(2,"zero_update_unprocess_record select no record!");
  160. return 1;
  161. }
  162. while ( DBRes.GetNextRecod(Error) )
  163. {
  164. bool bRet = true;
  165. // 局部变量初始化
  166. int nWorkfaceId = 0;
  167. std::string strStartTime = "";
  168. double fScheduleWorkTime = 0.0;
  169. int nDeptId = -1;
  170. std::string sCardId ="";
  171. // 取数据表记录
  172. bRet = DBRes.GetField( "work_face_id",nWorkfaceId, Error );
  173. bRet = DBRes.GetField( "start_up_time",strStartTime, Error );
  174. bRet = DBRes.GetField( "schedule_work_time",fScheduleWorkTime, Error );
  175. bRet = DBRes.GetField( "dept_id",nDeptId, Error );
  176. bRet = DBRes.GetField( "card_id",sCardId, Error );
  177. if(!bRet)
  178. {
  179. logn_error(2,"TR-zero_update_unprocess_record GetField exist error, error: %s", Error.c_str());
  180. }
  181. // 时间处理
  182. std::string strCurTime = tr_helper::time_t_switch_string(tCurTime);
  183. time_t tStartTime = tr_helper::string_switch_time_t(strStartTime);
  184. double timeInterval = 0;
  185. if (tCurTime > tStartTime)
  186. {
  187. timeInterval = (double)(tCurTime - tStartTime)/(double)SEC_TO_HOUR;
  188. }
  189. // 处理未关机的记录
  190. snprintf(strsql, sizeof(strsql), "Update his_startup_detail set shut_down_time ='%s',real_startup_time=%.3f, proc_tag = 1 where start_up_time='%s' and work_face_id=%d;",
  191. strCurTime.c_str(),timeInterval, strStartTime.c_str(),nWorkfaceId);
  192. std::string Error;
  193. if ( sDBConnPool.ExecuteSql( strsql, Error ) < 0 )
  194. {
  195. logn_error(2,"TR-Update_unprocess_record Failed to Execute -update- SQL sql=%s, error: %s", strsql, Error.c_str());
  196. }
  197. logn_info(2,"TR-Update_unprocess_record zero time:%s -update- SQL sql=%s", strStartTime.c_str(),strsql);
  198. // 关机后重开
  199. strStartTime = tr_helper::time_t_switch_string(tTime);
  200. // 零点插入时重新获取计划开机时间
  201. double zeroScheduleStartupTime = 0.0;
  202. std::string strDate = strStartTime.substr(0,10); // 截取日期字符串前10位
  203. zeroScheduleStartupTime = get_schedulestartuptime(nWorkfaceId, strDate);
  204. // 写库
  205. snprintf(strsql, sizeof(strsql),
  206. "INSERT IGNORE INTO his_startup_detail(work_face_id, start_up_time,schedule_work_time,dept_id) VALUES(%d, '%s',%.3f,%d);",
  207. nWorkfaceId,strStartTime.c_str(),zeroScheduleStartupTime,nDeptId);
  208. if ( sDBConnPool.ExecuteSql( strsql, Error ) < 0 )
  209. {
  210. logn_error(2,"TR-Update_unprocess_record Failed to Execute -insert- SQL sql=%s, error: %s", strsql, Error.c_str());
  211. }
  212. logn_info(2,"TR-zero reinsert time:%s -insert- SQL sql=%s", strStartTime.c_str(),strsql);
  213. }
  214. return 0;
  215. }
  216. /*
  217. * Functon: Update the unprocessed record when restarting
  218. * Parameters: void
  219. * Returns: int, returns 0 normally, otherwise it returns 1
  220. * Throws: void
  221. */
  222. int operating_rate::restart_update_unprocess_record()
  223. {
  224. char strsql[tr_helper::SQL_LENGTH] = {0};
  225. const char * sql = "select work_face_id, start_up_time from his_startup_detail where proc_tag=0;";
  226. std::string Error;
  227. YADB::CDBResultSet DBRes;
  228. if (0 == sDBConnPool.Query(sql,DBRes,Error))
  229. {
  230. logn_error(2,"TR-restart_update_unprocess_record Query exist error, error: %s", Error.c_str());
  231. return 1;
  232. }
  233. int nCount = DBRes.GetRecordCount( Error );
  234. if (nCount > 0)
  235. {
  236. while ( DBRes.GetNextRecod(Error) )
  237. {
  238. bool bRet = true;
  239. int workface_id = 0;
  240. std::string start_time;
  241. bRet = DBRes.GetField( "work_face_id",workface_id, Error );
  242. bRet = DBRes.GetField( "start_up_time",start_time, Error );
  243. if(!bRet)
  244. {
  245. logn_error(2,"TR-restart_update_unprocess_record GetField exist error, error: %s", Error.c_str());
  246. }
  247. time_t tcur_time = time(NULL);
  248. std::string cur_time = tr_helper::time_t_switch_string(tcur_time);
  249. time_t tstart_time = tr_helper::string_switch_time_t(start_time);
  250. double time_interval = 0;
  251. if (tcur_time > tstart_time)
  252. {
  253. time_interval = (double )(tcur_time - tstart_time)/(double )operating_rate::SEC_TO_HOUR;
  254. }
  255. snprintf(strsql, sizeof(strsql),
  256. "Update his_startup_detail set shut_down_time ='%s',real_startup_time=%.3f, proc_tag = 1 where start_up_time='%s' and work_face_id=%d;",
  257. cur_time.c_str(),time_interval, start_time.c_str(),workface_id);
  258. if ( sDBConnPool.ExecuteSql( strsql, Error ) < 0 )
  259. {
  260. logn_error(2,"TR-restart_update_unprocess_record Failed to Execute -update- SQL sql=%s, error: %s", strsql, Error.c_str());
  261. }
  262. logn_info(2,"TR-restart_update_unprocess_record time:%s -update- sql=%s", start_time.c_str(),strsql);
  263. }
  264. }
  265. return 0;
  266. }
  267. /*
  268. * Functon: Get the schedule startup time of vihicle
  269. * Parameters: void
  270. * Returns: void
  271. * Throws: void
  272. */
  273. void operating_rate::get_schedulestartuptime(int cardType, std::string strCard, double &scheduleStartupTime,std::string strDate)
  274. {
  275. char strsql[tr_helper::SQL_LENGTH] = {0};
  276. // 查计划开机时间
  277. if(THREE_RATES_CARD_TYPE::CT_COALMINING_MACHINE == cardType)
  278. {
  279. sprintf(strsql,"SELECT schedule_startup_time FROM dat_workface_scheduling "
  280. "WHERE schedule_date='%s' "
  281. "and workface_id = (select coalface_id from dat_coalface_vehicle "
  282. "where vehicle_id = (select vehicle_id from dat_vehicle_extend "
  283. "where card_id = %s));",strDate.c_str(),strCard.c_str());
  284. scheduleStartupTime = 0.0; // 采煤机计划开机时间数据库未配置,默认取0
  285. }
  286. else if(THREE_RATES_CARD_TYPE::CT_TUNNELBORING_MACHINE == cardType)
  287. {
  288. sprintf(strsql, "SELECT schedule_startup_time FROM dat_workface_scheduling "
  289. "WHERE schedule_date='%s' "
  290. "and workface_id = (select drivingface_id from dat_drivingface_vehicle "
  291. "where vehicle_id = (select vehicle_id from dat_vehicle_extend "
  292. "where card_id = %s));",strDate.c_str(),strCard.c_str());
  293. scheduleStartupTime = 0.0; // 掘进机计划开机时间数据库未配置,默认取0
  294. }
  295. std::string Error;
  296. YADB::CDBResultSet DBRes;
  297. if (0 == sDBConnPool.Query(strsql,DBRes,Error))
  298. {
  299. logn_error(2,"TR-get_schedulestartuptime Query exist error, error: %s", Error.c_str());
  300. return ;
  301. }
  302. int nCount = DBRes.GetRecordCount( Error );
  303. if (nCount > 0)
  304. {
  305. while ( DBRes.GetNextRecod(Error) )
  306. {
  307. bool bRet = true;
  308. bRet = DBRes.GetField( "schedule_startup_time",scheduleStartupTime, Error );
  309. if(!bRet)
  310. {
  311. logn_error(2,"TR-get_schedulestartuptime GetField exist error, error: %s", Error.c_str());
  312. }
  313. break;
  314. }
  315. }
  316. return ;
  317. }
  318. double operating_rate::get_schedulestartuptime(int work_face_id,const std::string &strDate)
  319. {
  320. char strsql[tr_helper::SQL_LENGTH] = {0};
  321. // 查计划开机时间
  322. sprintf(strsql, "SELECT schedule_startup_time FROM dat_workface_scheduling "
  323. "WHERE schedule_date='%s' and workface_id = %d;",strDate.c_str(),work_face_id);
  324. std::string Error;
  325. YADB::CDBResultSet DBRes;
  326. if (0 == sDBConnPool.Query(strsql,DBRes,Error))
  327. {
  328. logn_error(2,"TR-get_schedulestartuptime Query exist error, error: %s", Error.c_str());
  329. return 0.0;
  330. }
  331. double scheduleStartupTime = 0.0;
  332. int nCount = DBRes.GetRecordCount( Error );
  333. if (nCount > 0)
  334. {
  335. while ( DBRes.GetNextRecod(Error) )
  336. {
  337. bool bRet = true;
  338. bRet = DBRes.GetField( "schedule_startup_time",scheduleStartupTime, Error );
  339. if(!bRet)
  340. {
  341. logn_error(2,"TR-get_schedulestartuptime GetField exist error, error: %s", Error.c_str());
  342. }
  343. break;
  344. }
  345. }
  346. return scheduleStartupTime;
  347. }
  348. /*
  349. * Functon: Get vibrate threthold of vihicle
  350. * Parameters: void
  351. * Returns: void
  352. * Throws: void
  353. */
  354. void operating_rate::get_vibratethrethold(std::shared_ptr<coaldrivingface_card> card, int& threshold)
  355. {
  356. // 查卡号对应的震动阈值
  357. char strsql[tr_helper::SQL_LENGTH] = {0};
  358. if(tr_helper::is_coalminingface(card))
  359. {
  360. snprintf(strsql,sizeof(strsql),"SELECT a.shake_threshold FROM dat_coalface_vehicle a, dat_vehicle_extend b WHERE a.vehicle_id = b.vehicle_id AND b.card_id = %s;",card->m_scard_id.c_str());
  361. }
  362. else if(tr_helper::is_drivingface(card))
  363. {
  364. snprintf(strsql,sizeof(strsql), "SELECT a.shake_threshold FROM dat_drivingface_vehicle a, dat_vehicle_extend b WHERE a.vehicle_id = b.vehicle_id AND b.card_id = %s;",card->m_scard_id.c_str());
  365. }
  366. std::string Error;
  367. YADB::CDBResultSet DBRes;
  368. if (0 == sDBConnPool.Query(strsql,DBRes,Error))
  369. {
  370. logn_error(2,"TR-get_vibratethrethold Query exist error, error: %s", Error.c_str());
  371. return ;
  372. }
  373. int nCount = DBRes.GetRecordCount( Error );
  374. if (nCount > 0)
  375. {
  376. while ( DBRes.GetNextRecod(Error) )
  377. {
  378. bool bRet = true;
  379. bRet = DBRes.GetField( "shake_threshold",threshold, Error );
  380. if(!bRet)
  381. {
  382. logn_error(2,"TR-get_vibratethrethold GetField exist error, error: %s", Error.c_str());
  383. }
  384. break;
  385. }
  386. }
  387. return ;
  388. }
  389. /*
  390. * Functon: Open machine
  391. * Parameters: card, coalface or drivingface card
  392. * Returns: int, returns 0 normally, otherwise it returns 1
  393. * Throws: void
  394. */
  395. int operating_rate::open_machine(std::shared_ptr<coaldrivingface_card> card)
  396. {
  397. card->m_bopen = true;
  398. time_t tCurTime = time(NULL);
  399. card->m_tstartup_time = tCurTime;
  400. std::string strStartTime = tr_helper::time_t_switch_string(card->m_tstartup_time);
  401. // 从工作面排班表中获取计划开机时间
  402. double scheduleStartupTime = 0.0;
  403. int deptId = card->m_ndept_id;
  404. int nWorkfaceID = G_CommonData->get_workface_id(card->m_scard_id);
  405. if (nWorkfaceID == -1)
  406. {
  407. logn_errno(2,"TR-open_machine: WorkFaceId Not Find By Card=%s ", card->m_scard_id.c_str());
  408. }
  409. std::string strDate = strStartTime.substr(0,10); // 截取日期字符串前10位
  410. scheduleStartupTime = get_schedulestartuptime(nWorkfaceID, strDate);
  411. logn_info(2,"TR-open_machine get_schedulestartuptime card=%s,scheduleStartupTime-%.4f", card->m_scard_id.c_str(),scheduleStartupTime);
  412. char sql[tr_helper::SQL_LENGTH] = { 0 };
  413. snprintf(sql, sizeof(sql),
  414. "INSERT IGNORE INTO his_startup_detail(work_face_id, start_up_time,schedule_work_time,dept_id) VALUES(%d, '%s',%.3f,%d);",
  415. nWorkfaceID,strStartTime.c_str(),scheduleStartupTime,deptId);
  416. std::string Error;
  417. if ( sDBConnPool.ExecuteSql( sql, Error ) < 0 )
  418. {
  419. logn_error(2,"TR-open_machine Failed to Execute -insert- SQL sql=%s,error: %s", sql, Error.c_str());
  420. return -1;
  421. }
  422. logn_info(2,"TR-open_machine -insert- sql=%s", sql);
  423. logn_info(2,"open_machine time: %s,card: %s,open state: %d,scheduleTime: %.3f,deptId: %d,Update: %s",
  424. strStartTime.c_str(),card->m_scard_id.c_str(), card->m_bopen,scheduleStartupTime,deptId,sql);
  425. return 0;
  426. }
  427. /*
  428. * Functon: Close machine
  429. * Parameters: card, coalface or drivingface card
  430. * Parameters: bTimeOut 是否超时
  431. * Returns: int, returns 0 normally, otherwise it returns 1
  432. * Throws: void
  433. */
  434. int operating_rate::close_machine(std::shared_ptr<coaldrivingface_card> card, bool bTimeOut /*= true*/)
  435. {
  436. card->m_bopen = false;
  437. char sql[tr_helper::SQL_LENGTH] = { 0 };
  438. time_t tCurTime = time(NULL);
  439. if(bTimeOut) // 超时关机,因为延时值不是有效开机时间,需减去延时值
  440. {
  441. tCurTime -= three_rates_impl::_instance()->m_shutdown_timeout_threshold;
  442. }
  443. std::string strCurTime = tr_helper::time_t_switch_string(tCurTime);
  444. std::string strStartTime = tr_helper::time_t_switch_string(card->m_tstartup_time);
  445. int nWorkfaceID = G_CommonData->get_workface_id(card->m_scard_id);
  446. if (nWorkfaceID == -1)
  447. {
  448. logn_errno(2,"TR-open_machine: WorkFaceId Not Find By Card=%s ", card->m_scard_id.c_str());
  449. }
  450. double timeInterval = 0;
  451. if (tCurTime > card->m_tstartup_time)
  452. {
  453. timeInterval = (double )(tCurTime - card->m_tstartup_time)/(double )operating_rate::SEC_TO_HOUR;
  454. }
  455. snprintf(sql, sizeof(sql), "Update his_startup_detail set shut_down_time ='%s',real_startup_time=%.3f, proc_tag = 1 where start_up_time='%s' and work_face_id=%d;",
  456. strCurTime.c_str(),timeInterval, strStartTime.c_str(),nWorkfaceID);
  457. std::string Error;
  458. if ( sDBConnPool.ExecuteSql( sql, Error ) < 0 )
  459. {
  460. logn_error(2,"TR-close_machine Failed to Execute -update- SQL sql=%s,error: %s", sql, Error.c_str());
  461. }
  462. logn_info(2,"close_machine card:%s time: %s, open state: %d, Update: %s",card->m_scard_id.c_str(), strCurTime.c_str(), card->m_bopen,sql);
  463. // 循环队列置位
  464. for (unsigned int index = 0; index < card->m_cb_state.size(); ++index)
  465. {
  466. card->m_cb_state[index] = 0;
  467. }
  468. return 0;
  469. }
  470. /*
  471. * Functon: Store machine state /存储采煤机/掘进机开关机时间段到数据库
  472. * Parameters: card, coalface or drivingface card
  473. * Returns: int, returns 0 normally, otherwise it returns 1
  474. * Throws: void
  475. */
  476. int operating_rate::store_machine_state(std::shared_ptr<coaldrivingface_card> card)
  477. {
  478. // 分割跨日的开机记录
  479. this->zeroes_update();
  480. if(m_brestart) // 采集重启,更新his_startup_detail表中未处理的记录
  481. {
  482. m_brestart = false;
  483. restart_update_unprocess_record();
  484. }
  485. if(tr_helper::is_drivingface(card) || tr_helper::is_coalminingface(card))
  486. {
  487. int sum = std::accumulate(card->m_cb_state.begin(), card->m_cb_state.end(), 0);
  488. //日志
  489. logn_info(2,"TR-three_rates_impl::run, coaldrivingface card:%s, "
  490. "card->m_cb_state[0]:%d, card->m_cb_state[1]:%d,card->m_cb_state[2]:%d,card->m_cb_state[3] :%d, "
  491. "card->m_cb_state[4]:%d,card->m_cb_state[5]:%d,card->m_cb_state[6]:%d, card->m_cb_state[7]:%d,"
  492. "card->m_cb_state[8]:%d,card->m_cb_state[9]",
  493. card->m_scard_id.c_str(),card->m_cb_state[0],card->m_cb_state[1],card->m_cb_state[2],card->m_cb_state[3]
  494. ,card->m_cb_state[4],card->m_cb_state[5],card->m_cb_state[6],card->m_cb_state[7],
  495. card->m_cb_state[8],card->m_cb_state[9]);
  496. // 判断采煤机/掘进机惯导开关标
  497. int threshold = card->m_nshake_threshold;
  498. int openThreshold = STARTUP_STATE_COUNT * threshold;
  499. if(sum >= openThreshold && !card->m_bopen) // 设置为开机,开机的时候insert card_id,start_up_time,schedule_startup_time,dept_id字段
  500. {
  501. open_machine(card);
  502. }
  503. int closeThreshold = SHUTDOWN_STATE_COUNT * threshold;
  504. bool bCloseCondition = sum <= closeThreshold && card->m_tstartup_time != 0 && card->m_bopen;
  505. if(bCloseCondition) // 设置为关机,关机的时候update关机时间shut_down_time,real_startup_time字段
  506. {
  507. close_machine(card,false);
  508. }
  509. logn_info(2,"TR-three_rates_impl::run, coaldrivingface card:%s, threshold:%d, openThreshold:%d,closThreshold:%d,bCloseCondition :%d, bopen:%d,sum:%d",
  510. card->m_scard_id.c_str(),threshold,openThreshold,closeThreshold,bCloseCondition,card->m_bopen,sum);
  511. return 0;
  512. }
  513. else
  514. {
  515. return 1;
  516. }
  517. }
  518. //判断是否零点 再更新
  519. void operating_rate::zeroes_update()
  520. {
  521. // 分割跨日的开机记录
  522. time_t tCurTime = time(NULL);
  523. std::string strCurTime = tr_helper::time_t_switch_string(tCurTime);
  524. std::string substrCurTime = strCurTime.substr(11,8); // 截取日期字符串后八位
  525. if("23:59:59" == substrCurTime && !m_bzero)
  526. {
  527. m_bzero = true;
  528. zero_update_unprocess_record(tCurTime);
  529. logn_info(2,"zeroes_update zero flow. zero time: %s", strCurTime.c_str());
  530. }
  531. if("00:00:02" == substrCurTime)
  532. {
  533. m_bzero = false; // 后续零点初始化
  534. }
  535. }