wsTimerThread.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. if(jsCardPos == ""){
  62. return;
  63. }
  64. swsClientMgr.send( JSON_CMD_VALUE_PUSH, jsCardPos );
  65. }
  66. void wsTimerThread::send_card_pos()
  67. {
  68. if(__CardPosList.empty()){
  69. return;
  70. }
  71. std::map<uint64_t, _CARD_POS_> cards;
  72. __CardPosList.copy(cards);
  73. //service_position send
  74. for(auto it = cards.begin(); it != cards.end(); ++it)
  75. {
  76. std::string json_pos = __jsBuilder.build_ios_card_pos(it->second);
  77. //log_info("[service-position] json_pos: card_id=%d, freq=%.2f, json=%s", it->second.ID, it->second.m_freq, json_pos.c_str());
  78. ios_service::m_ios_service->notify(json_pos, std::to_string(it->second.ID), it->second.m_freq);
  79. }
  80. }
  81. void wsTimerThread::temp_send_card_pos()
  82. {
  83. if(__CardPosList.empty()){
  84. return;
  85. }
  86. std::map<uint64_t, _CARD_POS_> cards;
  87. __CardPosList.copy(cards);
  88. std::string json_pos = __jsBuilder.build_tmp_card_pos(cards);
  89. swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_pos);
  90. }
  91. void wsTimerThread::send_light_state()
  92. {
  93. if(light_state_list.empty()){
  94. return;
  95. }
  96. std::vector<light_state> lights = light_state_list;
  97. //log_info("[light_info] light_state's size=%d, copy=%d", light_state_list.size(), lights.size());
  98. std::string json_light = __jsBuilder.build_traffic_light(lights);
  99. swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_light);
  100. light_state_list.erase(light_state_list.begin(), light_state_list.end());
  101. }
  102. /*
  103. * 定时器线程发送定位数据
  104. *
  105. * */
  106. void wsTimerThread::_ThreadFunc( wsTimerThread * pOwner )
  107. {
  108. log_info("The timer thread is running");
  109. while ( pOwner->__Enable )
  110. {
  111. std::time_t t = time( 0 );
  112. int seconds = (int)std::difftime( t, __LastSendTime );
  113. if ( seconds >= pOwner->__Config.SendInterval )
  114. {
  115. pOwner->__SendCardPos();
  116. //service_position send
  117. //pOwner->send_card_pos();
  118. //pOwner->temp_send_card_pos();
  119. pOwner->send_light_state();
  120. __LastSendTime = t;
  121. }
  122. boost::this_thread::sleep( boost::posix_time::millisec( 1 ) );
  123. }
  124. pOwner->__ExitCond.notify_one();
  125. }
  126. void wsTimerThread::Init( const _THREAD_CONFIG_ & Config )
  127. {
  128. __Config = Config;
  129. __ChkConfig();
  130. }
  131. void wsTimerThread::upt_card_pos( const _CARD_POS_ & pos )
  132. {
  133. //如果已存在就更新,否则插入
  134. uint64_t type = pos.Type;
  135. uint64_t id=type<<32|pos.ID;
  136. if ( __CardPosList.seek( id ) )
  137. {
  138. __CardPosList.update( id, pos );
  139. }
  140. else
  141. {
  142. __CardPosList.insert( id, pos );
  143. }
  144. }
  145. void wsTimerThread::real_upt_card_pos(const _CARD_POS_& pos)
  146. {
  147. std::string pos_json = __jsBuilder.build_ios_card_pos(pos);
  148. ios_service::m_ios_service->notify(pos_json, std::to_string(pos.ID), 10);
  149. log_info("[service-position] json: %s", pos_json.c_str());
  150. }
  151. void wsTimerThread::del_card_pos( const _CARD_POS_ & pos )
  152. {
  153. uint64_t type = pos.Type;
  154. uint64_t id=type<<32|pos.ID;
  155. __CardPosList.erase( id );
  156. }
  157. void wsTimerThread::upt_light_state(const light_state& light)
  158. {
  159. light_state_list.push_back(light);
  160. }
  161. }