regularcycle_rate.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. #include "regularcycle_rate.h"
  2. #include "db_api/CDBSingletonDefine.h"
  3. #include "log.h"
  4. #include "three_rates_impl.h"
  5. #include "common_data.h"
  6. #include <iostream>
  7. #include <float.h>
  8. regularcycle_rate::regularcycle_rate()
  9. {
  10. }
  11. void regularcycle_rate::init()
  12. {
  13. m_coalmining_regularcycle_list.clear();
  14. m_coalmining_area_list.clear();
  15. init_data_coalmining_RegularCycle();
  16. init_data_coalminingface_RegularCycle();
  17. init_restart_halfcoalmining_regularcycle();
  18. init_data_pre_coalmining_RegularCycle();
  19. }
  20. /*初始化采煤机有关正规循环率计算的先前参数
  21. * param
  22. * return
  23. * 函数正常执行返回0,否则返回1
  24. */
  25. int regularcycle_rate::init_data_pre_coalmining_RegularCycle()
  26. {
  27. const char * sql = "SELECT work_face_id, start_time,start_x,start_y,move_direction \
  28. FROM his_regular_cycle_detail\
  29. where detail_type=1 and end_time is NULL and end_x is NULL;";
  30. std::shared_ptr<CoalMiningRegularCycle> pCoalRegularCycle = nullptr;
  31. std::string Error;
  32. YADB::CDBResultSet DBRes;
  33. if (0 == sDBConnPool.Query(sql,DBRes,Error))
  34. {
  35. logn_error(2,"TR-init_data_pre_coalmining_RegularCycle Query exist error, error: %s", Error.c_str());
  36. return 1;
  37. }
  38. int nCount = DBRes.GetRecordCount( Error );
  39. if (nCount > 0)
  40. {
  41. while ( DBRes.GetNextRecod(Error) )
  42. {
  43. bool bRet = true;
  44. int work_face_id = -1;
  45. bRet = DBRes.GetField( "work_face_id",work_face_id, Error );
  46. std::string str_start_time = "";
  47. bRet = DBRes.GetField( "start_time",str_start_time, Error );
  48. time_t start_time = tr_helper::string_switch_time_t(str_start_time);
  49. time_t tCurTime = time(NULL);
  50. if (tCurTime - start_time > 3600 *24*3)
  51. {
  52. continue;
  53. }
  54. CoalMiningRegularCycleMap::iterator it = m_coalmining_regularcycle_list.find(work_face_id);
  55. if(it != m_coalmining_regularcycle_list.end())
  56. {
  57. pCoalRegularCycle = it->second;
  58. }
  59. else
  60. {
  61. continue;
  62. }
  63. pCoalRegularCycle->coalface_id = work_face_id;
  64. pCoalRegularCycle->start_time = start_time;
  65. pCoalRegularCycle->last_time = time_t(0);
  66. pCoalRegularCycle->IsWorking = true;
  67. pCoalRegularCycle->bHalfKnife = true;
  68. bRet = DBRes.GetField("move_direction",pCoalRegularCycle->bDirection,Error);
  69. bRet = DBRes.GetField( "start_x",pCoalRegularCycle->start_x, Error );
  70. bRet = DBRes.GetField( "start_y",pCoalRegularCycle->start_y, Error );
  71. if(!bRet)
  72. {
  73. logn_error(2,"TR-init_data_pre_coalmining_RegularCycle GetField exist error, error: %s", Error.c_str());
  74. }
  75. pCoalRegularCycle->mindis_time = time(NULL);
  76. pCoalRegularCycle->mindistance = FLT_MAX;
  77. }
  78. }
  79. return 0;
  80. }
  81. /*初始化半刀未完成的正规循环率情况
  82. *
  83. * param
  84. *
  85. * return
  86. * 函数正常执行返回0,否则返回1
  87. */
  88. int regularcycle_rate::init_restart_halfcoalmining_regularcycle()
  89. {
  90. const char * sql = "SELECT work_face_id, start_time,start_x,start_y,move_direction \
  91. FROM his_regular_cycle_detail\
  92. where detail_type=1 and end_x = 1.0 and end_time is NULL;";
  93. std::shared_ptr<CoalMiningRegularCycle> pCoalRegularCycle = nullptr;
  94. std::string Error;
  95. YADB::CDBResultSet DBRes;
  96. sDBConnPool.Query(sql,DBRes,Error);
  97. int nCount = DBRes.GetRecordCount( Error );
  98. if (nCount >0)
  99. {
  100. while ( DBRes.GetNextRecod(Error) )
  101. {
  102. bool bRet = false;
  103. int workface_id = -1;
  104. DBRes.GetField( "work_face_id",workface_id, Error );
  105. std::string str_start_time = "";
  106. DBRes.GetField( "start_time",str_start_time, Error );
  107. time_t start_time = tr_helper::string_switch_time_t(str_start_time);
  108. time_t tCurTime = time(NULL);
  109. if (tCurTime - start_time > 3600 *24*3)
  110. {
  111. continue;
  112. }
  113. CoalMiningRegularCycleMap::iterator it = m_coalmining_regularcycle_list.find(workface_id);
  114. if(it != m_coalmining_regularcycle_list.end())
  115. {
  116. pCoalRegularCycle = it->second;
  117. }
  118. else
  119. {
  120. continue;
  121. }
  122. pCoalRegularCycle->coalface_id = workface_id;
  123. pCoalRegularCycle->start_time = start_time;
  124. pCoalRegularCycle->last_time = time_t(0);
  125. pCoalRegularCycle->IsWorking = true;
  126. pCoalRegularCycle->bHalfKnife = false;
  127. bRet = DBRes.GetField("move_direction",pCoalRegularCycle->bDirection,Error);
  128. bRet = DBRes.GetField( "start_x",pCoalRegularCycle->start_x, Error );
  129. bRet = DBRes.GetField( "start_y",pCoalRegularCycle->start_y, Error );
  130. if(!bRet)
  131. {
  132. logn_error(2,"TR-init_data_pre_coalmining_RegularCycle GetField exist error, error: %s", Error.c_str());
  133. }
  134. pCoalRegularCycle->mindis_time = time(NULL);
  135. pCoalRegularCycle->mindistance = FLT_MAX;
  136. }
  137. }
  138. return 0;
  139. }
  140. /*初始化采煤机有关正规循环率计算的中间参数
  141. * param
  142. * return
  143. * 函数正常执行返回0,否则返回1
  144. */
  145. int regularcycle_rate::init_data_coalmining_RegularCycle(const std::string& face_id)
  146. {
  147. std::string sql = "SELECT vehicle_id, coalface_id FROM dat_coalface_vehicle where state=1";
  148. if(face_id=="-1")
  149. {
  150. sql += ";";
  151. }
  152. else
  153. {
  154. sql += " and coalface_id = " + face_id+";";
  155. }
  156. std::shared_ptr<CoalMiningRegularCycle> pCoalRegularCycle = nullptr;
  157. std::string Error;
  158. YADB::CDBResultSet DBRes;
  159. if (0 == sDBConnPool.Query(sql.c_str(),DBRes,Error))
  160. {
  161. logn_error(2,"TR-init_data_coalmining_RegularCycle Query exist error, error: %s", Error.c_str());
  162. printf("sql=%s", sql.c_str());
  163. return 1;
  164. }
  165. int nCount = DBRes.GetRecordCount( Error );
  166. if (nCount > 0)
  167. {
  168. while ( DBRes.GetNextRecod(Error) )
  169. {
  170. bool bRet = true;
  171. int vehicle_id = -1;
  172. int workface_id = -1;
  173. bRet = DBRes.GetField( "vehicle_id",vehicle_id, Error );
  174. if (!bRet)
  175. {
  176. logn_error(2, "TR-init_data_coalmining_RegularCycle GetField exist error, error: %s", Error.c_str());
  177. continue;
  178. }
  179. bRet = DBRes.GetField( "coalface_id",workface_id, Error );
  180. if (!bRet)
  181. {
  182. logn_error(2, "TR-init_data_coalmining_RegularCycle GetField exist error, error: %s", Error.c_str());
  183. continue;
  184. }
  185. CoalMiningRegularCycleMap::iterator it = m_coalmining_regularcycle_list.find(workface_id);
  186. if(it != m_coalmining_regularcycle_list.end())
  187. {
  188. pCoalRegularCycle = it->second;
  189. logn_info(2, "TR-update_data_coalmining_RegularCycle workface_id: %s", face_id.c_str());
  190. }
  191. else
  192. {
  193. pCoalRegularCycle = std::make_shared<CoalMiningRegularCycle>();
  194. pCoalRegularCycle->last_time = time_t(0);
  195. pCoalRegularCycle->avg_distance = 0;
  196. pCoalRegularCycle->dis_num = 0;
  197. pCoalRegularCycle->IsWorking = false;
  198. pCoalRegularCycle->bDirection = 0;
  199. pCoalRegularCycle->bHalfKnife = false;
  200. pCoalRegularCycle->mindistance = FLT_MAX;
  201. m_coalmining_regularcycle_list.insert(std::make_pair(workface_id,pCoalRegularCycle));
  202. logn_info(2, "TR-add_data_coalmining_RegularCycle workface_id: %s", face_id.c_str());
  203. }
  204. pCoalRegularCycle->vehicle_id = vehicle_id;
  205. pCoalRegularCycle->coalface_id = workface_id;
  206. }
  207. }
  208. return 0;
  209. }
  210. void regularcycle_rate::remove_data_coalmining_RegularCycle(int face_id)
  211. {
  212. auto it_face = m_coalmining_regularcycle_list.find(face_id);
  213. if (it_face != m_coalmining_regularcycle_list.end())
  214. {
  215. m_coalmining_regularcycle_list.erase(it_face);
  216. }
  217. }
  218. void regularcycle_rate::remove_data_coalminingface_RegularCycle(int face_id)
  219. {
  220. auto it_face = m_coalmining_area_list.find(face_id);
  221. if (it_face != m_coalmining_area_list.end())
  222. {
  223. m_coalmining_area_list.erase(it_face);
  224. }
  225. }
  226. /*初始化综采面有关正规循环率计算的参数
  227. * param
  228. * return
  229. * 函数正常执行返回0,否则返回1
  230. */
  231. int regularcycle_rate::init_data_coalminingface_RegularCycle(const std::string& face_id)
  232. {
  233. std::string sql = "SELECT coalface_id, head_x , head_y, tail_x, tail_y, ht_radius FROM dat_coalface";//x_offset,y_offset
  234. if (face_id == "-1")
  235. {
  236. sql += ";";
  237. }
  238. else
  239. {
  240. sql += " where coalface_id = " + face_id + ";";
  241. }
  242. std::shared_ptr<CoalMiningArea> pCoalMiningArea = nullptr;
  243. std::string Error;
  244. YADB::CDBResultSet DBRes;
  245. if (0 == sDBConnPool.Query(sql.c_str(),DBRes,Error))
  246. {
  247. logn_error(2,"TR-init_data_coalminingface_RegularCycle Query exist error, error: %s", Error.c_str());
  248. return 1;
  249. }
  250. int nCount = DBRes.GetRecordCount( Error );
  251. if (nCount > 0)
  252. {
  253. while ( DBRes.GetNextRecod(Error) )
  254. {
  255. bool bRet = true;
  256. int coalface_id = -1;
  257. bRet = DBRes.GetField( "coalface_id",coalface_id, Error );
  258. if (!bRet)
  259. {
  260. logn_error(2, "TR-init_data_coalminingface_RegularCycle GetField exist error, error: %s", Error.c_str());
  261. continue;
  262. }
  263. CoalMiningAreaMap::iterator it = m_coalmining_area_list.find(coalface_id);
  264. if(it != m_coalmining_area_list.end())
  265. {
  266. pCoalMiningArea = it->second;
  267. logn_info(2,"TR-update_data_coalminingface_RegularCycle workface_id: %d", coalface_id);
  268. }
  269. else
  270. {
  271. pCoalMiningArea = std::make_shared<CoalMiningArea>();
  272. m_coalmining_area_list.insert(make_pair(coalface_id, pCoalMiningArea));
  273. logn_info( 2,"TR-add_data_coalminingface_RegularCycle workface_id: %d", coalface_id);
  274. }
  275. pCoalMiningArea->coalface_id = coalface_id;
  276. bRet = DBRes.GetField( "head_x",pCoalMiningArea->x1, Error );
  277. bRet = DBRes.GetField( "head_y",pCoalMiningArea->y1, Error );
  278. bRet = DBRes.GetField( "tail_x",pCoalMiningArea->x2, Error );
  279. bRet = DBRes.GetField( "tail_y",pCoalMiningArea->y2, Error );
  280. bRet = DBRes.GetField( "ht_radius",pCoalMiningArea->distance, Error );
  281. pCoalMiningArea->m_max_distance=sqrt((pCoalMiningArea->x1-pCoalMiningArea->x2)*(pCoalMiningArea->x1-pCoalMiningArea->x2)+(pCoalMiningArea->y1-pCoalMiningArea->y2)*(pCoalMiningArea->y1-pCoalMiningArea->y2))*2;
  282. if(!bRet)
  283. {
  284. logn_error(2,"TR-init_data_coalminingface_RegularCycle GetField exist error, error: %s", Error.c_str());
  285. }
  286. }
  287. }
  288. return 0;
  289. }
  290. /*获取采煤机的计划刀数
  291. * param
  292. * return
  293. * 函数正常执行返回0,否则返回1
  294. */
  295. int regularcycle_rate::get_data_coalmining_scheduletimes(int workface_id,double& scheduletimes)
  296. {
  297. time_t tCurTime = time(NULL);
  298. std::string curDate = tr_helper::time_t2string_date(tCurTime);
  299. char strsql[tr_helper::SQL_LENGTH] = {0};
  300. snprintf(strsql, sizeof(strsql),"SELECT a.schedule_mine_times FROM dat_workface_scheduling a,dat_work_face b where a.workface_id = %d and\
  301. schedule_date ='%s' and b.work_face_type = 1 and a.workface_id = b.work_face_id;",
  302. workface_id,curDate.c_str());
  303. std::shared_ptr<CoalMiningRegularCycle> pCoalRegularCycle = nullptr;
  304. std::string Error;
  305. YADB::CDBResultSet DBRes;
  306. if (0 == sDBConnPool.Query(strsql,DBRes,Error))
  307. {
  308. logn_error(2,"TR-get_data_coalmining_scheduletimes Query exist error, error: %s", Error.c_str());
  309. return 1;
  310. }
  311. int nCount = DBRes.GetRecordCount( Error );
  312. if (nCount > 0)
  313. {
  314. while ( DBRes.GetNextRecod(Error) )
  315. {
  316. bool bRet = true;
  317. bRet = DBRes.GetField( "schedule_mine_times",scheduletimes, Error );
  318. if(!bRet)
  319. {
  320. logn_error(2,"TR-get_data_coalmining_scheduletimes GetField exist error, error: %s", Error.c_str());
  321. }
  322. break;
  323. }
  324. }
  325. return 0;
  326. }
  327. /* 完成半刀,更新刀数值为0.5
  328. * param
  329. * reguilarCycle --- 采煤机0.5刀的参数
  330. * return
  331. * 函数正常执行返回0,否则返回1
  332. */
  333. int regularcycle_rate::update_halfknife_coalmining_Regular_cycle(std::shared_ptr<CoalminingRegularCycleDetail> reguilarCycle )
  334. {
  335. char sql_log[tr_helper::SQL_LENGTH] = {'\0'};
  336. std::string strstarttime = tr_helper::time_t_switch_string(reguilarCycle->start_time);
  337. time_t tCurTime = time(NULL);
  338. time_t tStartTime = tCurTime - 3000;
  339. std::string strCurTime = tr_helper::time_t_switch_string(tCurTime);
  340. std::string strStartTime = tr_helper::time_t_switch_string(tStartTime);
  341. snprintf(sql_log, sizeof(sql_log),
  342. "update his_regular_cycle_detail set detail_value=0.5, end_time = '%s',start_time = '%s' where work_face_id = %d and start_time = '%s';",
  343. strCurTime.c_str(),
  344. strStartTime.c_str(),
  345. reguilarCycle->coalface_id,
  346. strstarttime.c_str()
  347. );
  348. std::string error = "";
  349. if (sDBConnPool.ExecuteSql(sql_log,error ) < 0 )
  350. {
  351. logn_error(2,"TR-update_halfknife_coalmining_Regular_cycle Failed to ExecuteSql:%s,Err=%s\n", sql_log, error.c_str());
  352. }
  353. logn_info(2,"TR-update_halfknife_coalmining_Regular_cycle, -update- sql: %s",sql_log);
  354. return 0;
  355. }
  356. /*存储采煤机完成1刀的情况
  357. * param
  358. * reguilarCycle --- 采煤机1刀的参数
  359. * return
  360. * 函数正常执行返回0,否则返回1
  361. */
  362. int regularcycle_rate::Update_data_coalmining_Regular_cycle(std::shared_ptr<CoalminingRegularCycleDetail> reguilarCycle )
  363. {
  364. char sql_log[tr_helper::SQL_LENGTH] = {'\0'};
  365. std::string strstarttime = tr_helper::time_t_switch_string(reguilarCycle->start_time);
  366. std::string strendtime = tr_helper::time_t_switch_string(reguilarCycle->end_time);
  367. snprintf(sql_log, sizeof(sql_log),
  368. "update his_regular_cycle_detail set end_time ='%s', detail_value=0.5,end_x=%.4f,end_y=%.4f \
  369. where work_face_id = %d and start_time = '%s';",
  370. strendtime.c_str(),
  371. reguilarCycle->end_x,
  372. reguilarCycle->end_y,
  373. reguilarCycle->coalface_id,
  374. strstarttime.c_str()
  375. );
  376. std::string error = "";
  377. if (sDBConnPool.ExecuteSql(sql_log,error ) < 0 )
  378. {
  379. logn_error(2,"TR-Update_data_coalmining_Regular_cycle Failed to Execute -update- SQL sql=%s,error: %s", sql_log, error.c_str());
  380. }
  381. logn_info(2,"TR-Update_data_coalmining_Regular_cycle -update- sql=%s", sql_log);
  382. return 0;
  383. }
  384. /*存储采煤机半刀开始的情况
  385. *
  386. * param
  387. * reguilarCycle --- 采煤机1刀的参数
  388. *
  389. * return
  390. * 函数正常执行返回0,否则返回1
  391. */
  392. int regularcycle_rate::store_halfknife_coalmining_Regular_cycle(std::shared_ptr<CoalminingRegularCycleDetail> reguilarCycle )
  393. {
  394. char sql_log[tr_helper::SQL_LENGTH] = {'\0'};
  395. //获取综采面参数
  396. double schedule_times =5.0;// 数据库未配置,默认取5
  397. get_data_coalmining_scheduletimes(reguilarCycle->coalface_id,schedule_times);
  398. std::string strstarttime = tr_helper::time_t_switch_string(reguilarCycle->start_time);
  399. snprintf( sql_log, sizeof(sql_log),
  400. "insert IGNORE into his_regular_cycle_detail (work_face_id,detail_type,start_time,detail_value,schedule_value,dept_id,start_x,start_y,end_x,end_y,move_direction) \
  401. values(%d,%d,'%s',%.4f,%.4f,%d,%.4f,%.4f,%.4f,%.4f,%d);",
  402. reguilarCycle->coalface_id,
  403. 1,
  404. strstarttime.c_str(),
  405. 0.0,
  406. schedule_times,
  407. reguilarCycle->dept_id,
  408. reguilarCycle->start_x,
  409. reguilarCycle->start_y,
  410. 1.0,
  411. 1.0,
  412. reguilarCycle->move_direction
  413. );
  414. std::string error = "";
  415. if (sDBConnPool.ExecuteSql(sql_log,error ) < 0 )
  416. {
  417. logn_error(2,"TR-store_halfknife_coalmining_Regular_cycle Failed to Execute -insert- SQL sql=%s,error: %s", sql_log, error.c_str());
  418. return 1;
  419. }
  420. logn_info(2,"TR-store_halfknife_coalmining_Regular_cycle -insert- sql=%s", sql_log);
  421. return 0;
  422. }
  423. /*存储采煤机1刀开始的情况
  424. * param
  425. * reguilarCycle --- 采煤机1刀的参数
  426. * return
  427. * 函数正常执行返回0,否则返回1
  428. */
  429. int regularcycle_rate::store_data_coalmining_Regular_cycle(std::shared_ptr<CoalminingRegularCycleDetail> reguilarCycle )
  430. {
  431. char sql_log[tr_helper::SQL_LENGTH] = {'\0'};
  432. //获取综采面参数
  433. double schedule_times =5.0;// 数据库未配置,默认取5
  434. get_data_coalmining_scheduletimes(reguilarCycle->coalface_id,schedule_times);
  435. std::string strstarttime = tr_helper::time_t_switch_string(reguilarCycle->start_time);
  436. snprintf( sql_log, sizeof(sql_log),
  437. "insert IGNORE into his_regular_cycle_detail (work_face_id,detail_type,start_time,detail_value,schedule_value,dept_id,start_x,start_y,move_direction) \
  438. values(%d,%d,'%s',%.4f,%.4f,%d,%.4f,%.4f,%d);",
  439. reguilarCycle->coalface_id,
  440. 1,
  441. strstarttime.c_str(),
  442. 0.0,
  443. schedule_times,
  444. reguilarCycle->dept_id,
  445. reguilarCycle->start_x,
  446. reguilarCycle->start_y,
  447. reguilarCycle->move_direction
  448. );
  449. std::string error = "";
  450. if (sDBConnPool.ExecuteSql(sql_log,error ) < 0 )
  451. {
  452. logn_error(2,"TR-store_data_coalmining_Regular_cycle Failed to Execute -insert- SQL sql=%s,error: %s", sql_log, error.c_str());
  453. return 1;
  454. }
  455. logn_info(2,"TR-store_data_coalmining_Regular_cycle -insert- sql=%s", sql_log);
  456. return 0;
  457. }
  458. /*发送采煤机距离固定点距离(现在设置为机头)
  459. * param
  460. * vehicle_id --- 车ID
  461. * dis_record --- 过去一分钟记录的位置的平均距离
  462. * return
  463. * 函数正常执行返回0,否则返回1
  464. */
  465. int regularcycle_rate::store_data_coalmining_Pos(int workface_id, double dis_record)
  466. {
  467. char sql_log[tr_helper::SQL_LENGTH] = {'\0'};
  468. time_t tCurTime = time(NULL);
  469. std::string strcurTime = tr_helper::time_t_switch_string(tCurTime);
  470. CoalMiningAreaMap::iterator it_w= m_coalmining_area_list.find(workface_id);
  471. double maxdistance =300.0;
  472. if (it_w!= m_coalmining_area_list.end())
  473. {
  474. maxdistance=it_w->second->m_max_distance;
  475. if (maxdistance < dis_record)
  476. {
  477. maxdistance = dis_record;
  478. }
  479. }
  480. snprintf( sql_log, sizeof(sql_log),
  481. "insert IGNORE into his_draw_position (work_face_id,write_time,avg_distance,max_distance )values(%d,'%s','%f',%f);",
  482. workface_id,
  483. strcurTime.c_str(),
  484. dis_record,
  485. maxdistance
  486. );
  487. std::string error = "";
  488. if (sDBConnPool.ExecuteSql(sql_log,error ) < 0 )
  489. {
  490. logn_error(2,"TR-store_data_coalmining_Pos Failed to Execute -insert- SQL sql=%s,error: %s", sql_log, error.c_str());
  491. }
  492. return 0;
  493. }
  494. /*计算采煤机的正规循环率
  495. * param
  496. * card --- 车卡信息
  497. * return
  498. */
  499. void regularcycle_rate::Calc_CoalMining_RegularCycleRate(std::shared_ptr<coaldrivingface_card> card)
  500. {
  501. //当前地图缩放
  502. double _cur_map_scale = G_CommonData->m_map_scale;
  503. //求到目的点的距离,和开始点的距离;
  504. int work_face_id = G_CommonData->get_workface_id(card->m_scard_id);
  505. CoalMiningRegularCycleMap::iterator it_s = m_coalmining_regularcycle_list.find(work_face_id);
  506. if (it_s == m_coalmining_regularcycle_list.end())
  507. {
  508. return;
  509. }
  510. std::shared_ptr<CoalMiningArea> area;
  511. CoalMiningAreaMap::iterator it_area = m_coalmining_area_list.find(it_s->second->coalface_id);
  512. if (it_area == m_coalmining_area_list.end())
  513. {
  514. return;
  515. }
  516. area = it_area->second;
  517. //获取机头机尾位置
  518. double x1= area->x1;
  519. double x2 = area->x2;
  520. double y1 = area->y1;
  521. double y2 = area->y2;
  522. //记录采煤机位置
  523. time_t loctime = card->rec_time/1000;
  524. std::string strrecTime = tr_helper::time_t_switch_string(loctime);
  525. double distance = sqrt((card->x-x1)*(card->x-x1) + (card->y-y1)*(card->y - y1))* _cur_map_scale;
  526. //char strdistance[60]= {'\0'};
  527. // snprintf(strdistance, sizeof(strdistance), "%s,%.4f,%.4f",strrecTime.c_str(),distance,fabs(card->final_v));
  528. //it_s->second->pos_record = it_s->second->pos_record + strdistance;
  529. string time1 = tr_helper::time_t_switch_string(loctime);
  530. string time2 = tr_helper::time_t_switch_string(it_s->second->last_time);
  531. int time_int = loctime - it_s->second->last_time;
  532. //nt itSize = it_s->second->pos_record.size();
  533. logn_info(2,"regular_cycle_coalmining: card: %s, time1: %s, time2: %s, time_intervel: %d, size: %d", card->m_scard_id.c_str(),time1.c_str(),time2.c_str(),time_int);
  534. it_s->second->avg_distance+=distance;
  535. it_s->second->dis_num++;
  536. //每隔60秒发送一次采煤机位置信息
  537. if (loctime - it_s->second->last_time >= 60 )
  538. {
  539. it_s->second->avg_distance/=it_s->second->dis_num;
  540. store_data_coalmining_Pos(work_face_id,it_s->second->avg_distance);
  541. it_s->second->avg_distance = 0;
  542. it_s->second->dis_num=0;
  543. it_s->second->last_time = loctime;
  544. }
  545. //只处理位置有变化时
  546. //if (card->b_pos_change)
  547. {
  548. // 刀数统计逻辑
  549. bool stateChanged = false;
  550. //判断是否已经开始计算刀数
  551. if (it_s->second->IsWorking)
  552. {
  553. double x = 0.0,y = 0.0;
  554. if (1 == it_s->second->bDirection) // 机头《-》机尾
  555. {
  556. x = x2;
  557. y = y2;
  558. //tagNum = 1;
  559. }
  560. else if(2 == it_s->second->bDirection) // 机尾《-》机头
  561. {
  562. x = x1;
  563. y = y1;
  564. //tagNum = 2;
  565. }
  566. else
  567. {
  568. logn_info(2,"regular_cycle_coalmining error direction1: vehicle_id:%d ",it_s->second->vehicle_id);
  569. }
  570. //判断半刀是否完成,完成半刀则更新起始存储位置的刀数值0 - 0.5
  571. if(!it_s->second->bHalfKnife)
  572. {
  573. // 机身中心区域位置
  574. double center_x = (area->x1 + area->x2)/2.0;
  575. double center_y = (area->y1 + area->y2)/2.0;
  576. double half_dis = sqrt((area->x1-area->x2)*(area->x1-area->x2) + (area->y1-area->y2)*(area->y1 - area->y2))*_cur_map_scale/2.0;
  577. double dis = 0.0;
  578. // 分机头-》机尾和机尾-》机头两种情况
  579. if (1 == it_s->second->bDirection) // 机头-》机尾
  580. {
  581. dis = sqrt((card->x-area->x1)*(card->x-area->x1) + (card->y-area->y1)*(card->y - area->y1))*_cur_map_scale ;
  582. }
  583. else if(2 == it_s->second->bDirection) // 机尾-》机头
  584. {
  585. dis = sqrt((card->x-area->x2)*(card->x-area->x2) + (card->y-area->y2)*(card->y - area->y2))*_cur_map_scale ;
  586. }
  587. else
  588. {
  589. logn_error(2,"TR-regular_cycle_coalmining error direction2: vehicle_id:%d ",it_s->second->vehicle_id);
  590. }
  591. if(half_dis < dis)
  592. {
  593. //数据上传
  594. std::shared_ptr<CoalminingRegularCycleDetail> RegularCycle = std::make_shared<CoalminingRegularCycleDetail>();
  595. RegularCycle->vehicle_id = card->m_nvehicle_id;
  596. RegularCycle->dept_id = card->m_ndept_id;
  597. RegularCycle->end_x =card->x;
  598. RegularCycle->end_y = card->y;
  599. RegularCycle->end_time = loctime;
  600. RegularCycle->start_x = it_s->second->start_x;
  601. RegularCycle->start_y = it_s->second->start_y;
  602. RegularCycle->start_time = it_s->second->start_time;
  603. RegularCycle->coalface_id = it_s->second->coalface_id;
  604. update_halfknife_coalmining_Regular_cycle(RegularCycle);
  605. it_s->second->bHalfKnife = true;
  606. //更新状态改变计算参数
  607. it_s->second->start_time = RegularCycle->end_time;
  608. it_s->second->start_x = RegularCycle->end_x;
  609. it_s->second->start_y = RegularCycle->end_y;
  610. RegularCycle->start_time = RegularCycle->end_time;
  611. RegularCycle->start_x = RegularCycle->end_x;
  612. RegularCycle->start_y = RegularCycle->end_y;
  613. RegularCycle->move_direction = it_s->second->bDirection;
  614. store_data_coalmining_Regular_cycle(RegularCycle);
  615. logn_info(2,"TR-Calc_CoalMining_RegularCycleRate halfknife, vehicle id: %d, center_x: %.4f,center_y:%.4f,dis:%.4f time: %s",
  616. RegularCycle->vehicle_id, center_x, center_y, half_dis, strrecTime.c_str());
  617. }
  618. }
  619. //判断一刀是否完成
  620. double end_x=0.0,end_y=0.0;
  621. time_t endtime = loctime;
  622. double coalfacedis = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))*_cur_map_scale;
  623. double dis = sqrt((card->x-x)*(card->x-x) + (card->y-y)*(card->y - y))*_cur_map_scale ;
  624. if (dis< area->distance)
  625. {
  626. logn_info(2,"TR-Calc_CoalMining_RegularCycleRate one knife time:%s has arrived ,distance=%.4f\n",strrecTime.c_str(),dis);
  627. stateChanged = true;
  628. end_x = card->x;
  629. end_y = card->y;
  630. }
  631. else if (dis < it_s->second->mindistance )
  632. {
  633. it_s->second->mindistance = dis;
  634. it_s->second->mindis_time = loctime;
  635. it_s->second->end_x = card->x;
  636. it_s->second->end_y = card->y ;
  637. }
  638. else if (loctime - it_s->second->mindis_time > 1800 && it_s->second->mindistance < 0.2 * coalfacedis)
  639. {
  640. logn_info(2,"TR-Calc_CoalMining_RegularCycleRate: %s time is too long, is arrived ,distance=%.4f\n", strrecTime.c_str(),dis);
  641. //防止有障碍不能到机头或机尾的情况
  642. stateChanged = true;
  643. end_x = it_s->second->end_x;
  644. end_y = it_s->second->end_y;
  645. endtime = it_s->second->mindis_time;
  646. }
  647. if (stateChanged)
  648. {
  649. //数据上传
  650. std::shared_ptr<CoalminingRegularCycleDetail> RegularCycle = std::make_shared<CoalminingRegularCycleDetail>();
  651. RegularCycle->vehicle_id = card->m_nvehicle_id;
  652. //RegularCycle->coalface_id = m_coalWorkfaceVehicleMap[card->m_nvehicle_id];
  653. RegularCycle->dept_id = card->m_ndept_id;
  654. RegularCycle->end_x =end_x;
  655. RegularCycle->end_y = end_y;
  656. RegularCycle->end_time = endtime;
  657. RegularCycle->start_x = it_s->second->start_x;
  658. RegularCycle->start_y = it_s->second->start_y;
  659. RegularCycle->start_time = it_s->second->start_time;
  660. RegularCycle->coalface_id = it_s->second->coalface_id;
  661. //RegularCycle->schedule_times = it_s->second->schedule_times;
  662. Update_data_coalmining_Regular_cycle(RegularCycle);
  663. //更新状态改变计算参数
  664. it_s->second->start_time = endtime;
  665. it_s->second->start_x = end_x;
  666. it_s->second->start_y = end_y;
  667. //it_s->second->tgPointNum = tagNum;//到达端点后进行目标点切换
  668. it_s->second->mindistance = FLT_MAX;
  669. it_s->second->mindis_time = loctime;
  670. it_s->second->bHalfKnife = false;
  671. RegularCycle->start_time = endtime;
  672. RegularCycle->start_x = end_x;
  673. RegularCycle->start_y = end_y;
  674. if(1 == it_s->second->bDirection)
  675. {
  676. it_s->second->bDirection = 2;
  677. RegularCycle->move_direction = 2;
  678. }
  679. else if(2 == it_s->second->bDirection)
  680. {
  681. it_s->second->bDirection = 1;
  682. RegularCycle->move_direction = 1;
  683. }
  684. else
  685. {
  686. logn_info(2,"regular_cycle_coalmining error direction3: vehicle_id:%d ",RegularCycle->vehicle_id);
  687. }
  688. store_halfknife_coalmining_Regular_cycle(RegularCycle);
  689. }
  690. }
  691. else
  692. {
  693. //采煤机是否在机头或这机尾,并改变状态
  694. double _head_dist = sqrt((card->x-x1)*(card->x-x1) + (card->y-y1)*(card->y - y1));
  695. double _tail_dist = sqrt((card->x-x2)*(card->x-x2) + (card->y-y2)*(card->y - y2));
  696. if (_head_dist * _cur_map_scale < area->distance)
  697. {
  698. stateChanged = true;
  699. it_s->second->IsWorking = true;//改变工作状态
  700. //tagNum = 2;//开始计算正规循环率
  701. it_s->second->bDirection = 1; //在机头,下一刀运动方向是机头->机尾
  702. }
  703. else if (_tail_dist * _cur_map_scale < area->distance)
  704. {
  705. stateChanged = true;
  706. it_s->second->IsWorking = true;//改变工作状态
  707. //tagNum = 1;//开始计算正规循环率
  708. it_s->second->bDirection = 2; //在机尾,下一刀运动方向是机尾->机头
  709. }
  710. if (stateChanged)
  711. {
  712. //更新状态改变计算参数
  713. it_s->second->start_time = card->rec_time/1000;
  714. it_s->second->start_x = card->x;
  715. it_s->second->start_y = card->y;
  716. //it_s->second->tgPointNum = tagNum;//到达端点后进行目标点切换
  717. it_s->second->mindistance = FLT_MAX;
  718. it_s->second->mindis_time = loctime;
  719. it_s->second->bHalfKnife = false;
  720. std::shared_ptr<CoalminingRegularCycleDetail> RegularCycle = std::make_shared<CoalminingRegularCycleDetail>();
  721. RegularCycle->vehicle_id = card->id;
  722. RegularCycle->dept_id = card->m_ndept_id;
  723. RegularCycle->start_time = card->rec_time/1000;
  724. RegularCycle->start_x = card->x;
  725. RegularCycle->start_y = card->y;
  726. RegularCycle->move_direction = it_s->second->bDirection;
  727. RegularCycle->coalface_id = work_face_id;
  728. store_halfknife_coalmining_Regular_cycle(RegularCycle);
  729. }
  730. }
  731. }
  732. }