1
0

wsTimerThread.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "wsTimerThread.h"
  2. #include <iostream>
  3. #include "wsClientMgr.h"
  4. #include "constdef.h"
  5. #include "log.h"
  6. #include "sys_setting.h"
  7. namespace YA
  8. {
  9. wsTimerThread::wsTimerThread()
  10. {
  11. __Reset();
  12. }
  13. wsTimerThread::~wsTimerThread()
  14. {
  15. Stop();
  16. }
  17. void wsTimerThread::Start()
  18. {
  19. __Running = true;
  20. __Thread.reset( new boost::thread( boost::bind( &wsTimerThread::_ThreadFunc, this, this ) ) );
  21. //__Thread->detach();
  22. }
  23. void wsTimerThread::Stop()
  24. {
  25. if(__Thread==nullptr)
  26. {
  27. return;
  28. }
  29. __Thread->interrupt();
  30. __Running = false;
  31. std::cout << "wsTimerThread::Stop() begin" << std::endl;
  32. __Enable = false;
  33. if ( __Running )
  34. {
  35. boost::unique_lock<boost::mutex> lock( __ExitMutex );
  36. __ExitCond.wait( lock );
  37. }
  38. __Reset();
  39. __Thread=nullptr;
  40. std::cout << "wsTimerThread::Stop() end" << std::endl;
  41. }
  42. void wsTimerThread::__Reset()
  43. {
  44. __Enable = true;
  45. __Running = false;
  46. }
  47. void wsTimerThread::__ChkConfig()
  48. {
  49. if ( __Config.SendInterval < MIN_SEND_INTERVAL )
  50. {
  51. __Config.SendInterval = MIN_SEND_INTERVAL;
  52. }
  53. }
  54. void wsTimerThread::__SendCardPos()
  55. {
  56. std::map<uint64_t, _CARD_POS_> CardPosList;
  57. if(__CardPosList.empty())
  58. return;
  59. __CardPosList.copy( CardPosList );
  60. std::string jsCardPos = __jsBuilder.BuildCardPos( CardPosList );
  61. swsClientMgr.send( JSON_CMD_VALUE_PUSH, jsCardPos );
  62. }
  63. void wsTimerThread::send_card_pos()
  64. {
  65. if(__CardPosList.empty()){
  66. return;
  67. }
  68. std::map<uint64_t, _CARD_POS_> cards;
  69. __CardPosList.copy(cards);
  70. //service_position send
  71. for(auto it = cards.begin(); it != cards.end(); ++it)
  72. {
  73. std::string json_pos = __jsBuilder.build_ios_card_pos(it->second);
  74. //log_info("[service-position] json_pos: card_id=%d, freq=%.2f, json=%s", it->second.ID, it->second.m_freq, json_pos.c_str());
  75. ios_service::m_ios_service->notify(json_pos, std::to_string(it->second.ID), it->second.m_freq);
  76. }
  77. }
  78. void wsTimerThread::temp_send_card_pos()
  79. {
  80. if(__CardPosList.empty()){
  81. return;
  82. }
  83. std::map<uint64_t, _CARD_POS_> cards;
  84. __CardPosList.copy(cards);
  85. std::string json_pos = __jsBuilder.build_tmp_card_pos(cards);
  86. swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_pos);
  87. }
  88. void wsTimerThread::send_light_state()
  89. {
  90. if(light_state_list.empty()){
  91. return;
  92. }
  93. std::vector<light_state> lights = light_state_list;
  94. log_info("[light_test] light_state's size=%d, copy=%d", light_state_list.size(), lights.size());
  95. std::string json_light = __jsBuilder.build_traffic_light(lights);
  96. swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_light);
  97. light_state_list.erase(light_state_list.begin(), light_state_list.end());
  98. }
  99. /*
  100. * 定时器线程发送定位数据
  101. *
  102. * */
  103. void wsTimerThread::_ThreadFunc( wsTimerThread * pOwner )
  104. {
  105. log_info("The timer thread is running");
  106. while ( pOwner->__Enable )
  107. {
  108. std::time_t t = time( 0 );
  109. int seconds = (int)std::difftime( t, __LastSendTime );
  110. if ( seconds >= pOwner->__Config.SendInterval )
  111. {
  112. pOwner->__SendCardPos();
  113. //service_position send
  114. //pOwner->send_card_pos();
  115. //pOwner->temp_send_card_pos();
  116. pOwner->send_light_state();
  117. __LastSendTime = t;
  118. }
  119. boost::this_thread::sleep( boost::posix_time::millisec( 1 ) );
  120. }
  121. pOwner->__ExitCond.notify_one();
  122. }
  123. void wsTimerThread::Init( const _THREAD_CONFIG_ & Config )
  124. {
  125. __Config = Config;
  126. __ChkConfig();
  127. }
  128. void wsTimerThread::upt_card_pos( const _CARD_POS_ & pos )
  129. {
  130. //如果已存在就更新,否则插入
  131. uint64_t type = pos.Type;
  132. uint64_t id=type<<32|pos.ID;
  133. if ( __CardPosList.seek( id ) )
  134. {
  135. __CardPosList.update( id, pos );
  136. }
  137. else
  138. {
  139. __CardPosList.insert( id, pos );
  140. }
  141. }
  142. void wsTimerThread::real_upt_card_pos(const _CARD_POS_& pos)
  143. {
  144. std::string pos_json = __jsBuilder.build_ios_card_pos(pos);
  145. ios_service::m_ios_service->notify(pos_json, std::to_string(pos.ID), 10);
  146. log_info("[service-position] json: %s", pos_json.c_str());
  147. }
  148. void wsTimerThread::del_card_pos( const _CARD_POS_ & pos )
  149. {
  150. uint64_t type = pos.Type;
  151. uint64_t id=type<<32|pos.ID;
  152. __CardPosList.erase( id );
  153. }
  154. void wsTimerThread::upt_light_state(const light_state& light)
  155. {
  156. light_state_list.push_back(light);
  157. }
  158. }