wsTimerThread.cpp 3.9 KB

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