main.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include <iostream>
  2. #include "three_rates_impl.h"
  3. #include "struct_def.h"
  4. #include "operating_rate.h"
  5. #include "worktime_rate.h"
  6. #include "log.h"
  7. //采煤机开机率测试
  8. void test_coalface_openater(three_rates_impl * r ,int times )
  9. {
  10. card_pos pos;
  11. pos.work_type_id = 25;
  12. pos.rec_time = time(nullptr);
  13. pos.type = 4;
  14. pos.id = 1192; // 采煤机
  15. pos.x = 3119 ;
  16. pos.y = -489.2 ;
  17. pos.vibration = times ;
  18. r->put(pos);
  19. }
  20. //采煤机工作测试
  21. void test_coalface_work(three_rates_impl * r,int times )
  22. {
  23. static double head_x = 3119;
  24. static double head_y = -489.2;
  25. static double tail_x = 3268;
  26. //static double tail_y = -489.2;
  27. static bool bPositive = true;
  28. static double cur_x = head_x;
  29. card_pos pos;
  30. pos.work_type_id = 25;
  31. pos.vibration = 100;
  32. pos.rec_time = time(nullptr);
  33. pos.type = 4;
  34. pos.id = 1192; // 采煤机
  35. if (bPositive)
  36. {
  37. pos.x = (times + 3) / 3 == 0 ? cur_x - 1 : cur_x + times % 5 ;
  38. cur_x = pos.x;
  39. if (cur_x >= tail_x)
  40. {
  41. cur_x = tail_x;
  42. bPositive = false;
  43. }
  44. }
  45. else
  46. {
  47. pos.x = (times + 3) / 3 == 0 ? cur_x + 1 : cur_x - times % 5 ;
  48. cur_x = pos.x;
  49. if (cur_x <= head_x)
  50. {
  51. cur_x = head_x;
  52. bPositive = true;
  53. }
  54. }
  55. pos.y = head_y ;
  56. r->put(pos);
  57. }
  58. void test_drivingface_openater(three_rates_impl * r ,int times )
  59. {
  60. card_pos pos;
  61. pos.map_id = 5;
  62. pos.work_type_id = 26;
  63. pos.rec_time = time(nullptr);
  64. pos.dpt_id = 56;
  65. pos.type = 5;
  66. pos.id = 1307; // 工作面6 : 卡1307
  67. pos.x = 2325.9 ;
  68. pos.y = 75 ;
  69. pos.vibration = times ;
  70. r->put(pos);
  71. }
  72. //掘进机工作测试
  73. void test_drivingface_work(three_rates_impl * r ,int times)
  74. {
  75. static double g_y = 585;//75;
  76. card_pos pos;
  77. pos.map_id = 5;
  78. pos.running_stat = 1;
  79. pos.biz_stat = 1;
  80. pos.vibration = 100 ;
  81. pos.rec_time = time(nullptr);
  82. pos.dpt_id = 56;
  83. pos.type = 5;
  84. pos.id = 1307; // 掘进机
  85. pos.x = 2325.9 ;
  86. g_y = g_y + (double )(rand() % 10 - (times % 3 == 0 ? 5 : 3) )/20 ;
  87. pos.y = g_y ;
  88. pos.work_type_id = 26;
  89. pos.reader_id = 2;
  90. pos.reader_x = 2000;
  91. pos.reader_y = 1000;
  92. r->put(pos);
  93. }
  94. void test_checkdrivingfaceInfo(three_rates_impl * r ,int t)
  95. {
  96. static int g_last_time = 0;
  97. if (g_last_time == 0 || t - g_last_time >= 2)
  98. {
  99. g_last_time = t;
  100. ChangedDBDate d;
  101. d.edit_type_id = 2;
  102. d.webname = "drivingface";
  103. //参数
  104. d.param = "6";
  105. r->update_db_data(d.webname,d.param,d.edit_type_id);
  106. }
  107. }
  108. #define MAX_TEST_THREAD 10
  109. struct STestQueue
  110. {
  111. std::unique_ptr<std::thread> m_thread;
  112. three_rates_impl * m_three_rates;
  113. int id;
  114. int m_num;
  115. bool m_stop;
  116. bool m_run;
  117. STestQueue()
  118. {
  119. id = m_num = 0;
  120. m_stop = false;
  121. m_three_rates = nullptr;
  122. m_thread = nullptr;
  123. m_run = false;
  124. }
  125. };
  126. STestQueue g_TestQueue[MAX_TEST_THREAD];
  127. void test_queue_work(void * data)
  128. {
  129. STestQueue* p = (STestQueue*) data;
  130. while (true)
  131. {
  132. if(p->m_stop)
  133. {
  134. break;
  135. }
  136. card_pos pos ;
  137. pos.id = p->id;
  138. p->m_three_rates->put(pos);
  139. p->m_num++;
  140. usleep(1000 * 10);
  141. }
  142. }
  143. void test_queue(three_rates_impl* p_three)
  144. {
  145. for (int i = 0 ; i< MAX_TEST_THREAD ; ++i)
  146. {
  147. g_TestQueue[i].id = i+1;
  148. g_TestQueue[i].m_three_rates = p_three;
  149. g_TestQueue[i].m_stop = false;
  150. g_TestQueue[i].m_run = true;
  151. g_TestQueue[i].m_thread.reset(new std::thread(std::bind(&test_queue_work,&g_TestQueue[i])));
  152. g_TestQueue[i].m_thread->detach();
  153. }
  154. }
  155. void test_stop_queue()
  156. {
  157. for (int i = 0 ; i< MAX_TEST_THREAD ; ++i)
  158. {
  159. if (!g_TestQueue[i].m_run) continue;
  160. g_TestQueue[i].m_stop = true;
  161. }
  162. sleep(2);
  163. }
  164. void test_print_queue(three_rates_impl* p_three)
  165. {
  166. //打印
  167. for (int i = 0 ; i< MAX_TEST_THREAD ; ++i)
  168. {
  169. if (!g_TestQueue[i].m_run) continue;
  170. int recvNum = g_TestQueue[i].m_three_rates->m_card_sync_num[g_TestQueue[i].id];
  171. printf("Id = %d : sendNum=%d - recvNum=%d = %d \n"
  172. ,g_TestQueue[i].id, g_TestQueue[i].m_num, recvNum
  173. ,recvNum - g_TestQueue[i].m_num);
  174. }
  175. }
  176. void test_three_rates(three_rates_impl* p_three_rates)
  177. {
  178. //test_queue(p_three_rates);
  179. int openNum = 2;
  180. int time1 = 0;
  181. srand((int)time(nullptr));
  182. while(time1++<openNum)
  183. {
  184. //测试开机函数
  185. //test_coalface_openater(p_three_rates,time1 * 10);
  186. test_drivingface_openater(p_three_rates,time1 * 10);
  187. sleep(1);
  188. }
  189. time1 = 100000000;
  190. while(time1-->0)
  191. {
  192. //测试函数
  193. //test_coalface_work(p_three_rates,time1);
  194. test_drivingface_work(p_three_rates,time1);
  195. test_checkdrivingfaceInfo(p_three_rates,time(0));
  196. sleep(1);
  197. //std::this_thread::sleep_for (std::chrono::milliseconds(1));
  198. }
  199. sleep(3);
  200. time1 = 0;
  201. while(time1++<openNum)
  202. {
  203. //测试函数 关机
  204. //test_coalface_openater(p_three_rates,0);
  205. test_drivingface_openater(p_three_rates,0);
  206. sleep(1);
  207. }
  208. }
  209. //三率模块中告警回调
  210. void Handle_ThreeRates_Event_Callback(const int evType, const int evId, uint64_t id
  211. , double limitVal, double curVal, bool bFalg)
  212. {
  213. }
  214. int main()
  215. {
  216. log_init("../etc/log.ini");
  217. three_rates_impl* p_three_rates = three_rates_impl::_instance();
  218. card_pos pos;
  219. init_para para;
  220. para.driving_face_alarm = Handle_ThreeRates_Event_Callback;
  221. SendCallBack psaf = Handle_ThreeRates_Event_Callback;
  222. psaf(1,1,1,1,1,1);
  223. db_para dpara;
  224. dpara.Host = "127.0.0.1";
  225. dpara.User = "root";
  226. dpara.PWD="123456";
  227. dpara.DBName="yaxt4";
  228. dpara.CharSet="utf8";
  229. dpara.TimeOut=5;
  230. dpara.PoolSize=10;
  231. p_three_rates->setRunSoFile(false);
  232. p_three_rates->init(para,dpara);
  233. p_three_rates->start();
  234. bool ret = p_three_rates->GetVehicleState(1198);
  235. cout << "Three_rates Vehicle:1198 state=: " << ret << endl;
  236. //功能测试
  237. test_three_rates(p_three_rates);
  238. cout << "Three_rates_module Stop ......" << endl;
  239. //停止队列测试
  240. test_stop_queue();
  241. p_three_rates->stop();
  242. //打印队列
  243. test_print_queue(p_three_rates);
  244. return 0;
  245. }