wsTimerThread.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // log_info("[pos_map]%s",jsCardPos.c_str());
  62. swsClientMgr.send( JSON_CMD_VALUE_PUSH, jsCardPos );
  63. }
  64. void wsTimerThread::_ThreadFunc( wsTimerThread * pOwner )
  65. {
  66. while ( pOwner->__Enable )
  67. {
  68. std::time_t t = time( 0 );
  69. int seconds = (int)std::difftime( t, __LastSendTime );
  70. if ( seconds >= pOwner->__Config.SendInterval )
  71. {
  72. pOwner->__SendCardPos();
  73. __LastSendTime = t;
  74. }
  75. boost::this_thread::sleep( boost::posix_time::millisec( 1 ) );
  76. }
  77. pOwner->__ExitCond.notify_one();
  78. }
  79. void wsTimerThread::Init( const _THREAD_CONFIG_ & Config )
  80. {
  81. __Config = Config;
  82. __ChkConfig();
  83. }
  84. void wsTimerThread::upt_card_pos( const _CARD_POS_ & pos )
  85. {
  86. //如果已存在就更新,否则插入
  87. uint64_t type = pos.Type;
  88. uint64_t id=type<<32|pos.ID;
  89. if ( __CardPosList.seek( id ) )
  90. {
  91. __CardPosList.update( id, pos );
  92. }
  93. else
  94. {
  95. __CardPosList.insert( id, pos );
  96. }
  97. }
  98. void wsTimerThread::real_upt_card_pos(const _CARD_POS_& pos)
  99. {
  100. std::string pos_json = __jsBuilder.build_ios_card_pos(pos);
  101. ios_service::m_ios_service->notify(pos_json, std::to_string(pos.ID), 10);
  102. log_info("[service-position] json: %s", pos_json.c_str());
  103. }
  104. void wsTimerThread::del_card_pos( const _CARD_POS_ & pos )
  105. {
  106. uint64_t type = pos.Type;
  107. uint64_t id=type<<32|pos.ID;
  108. __CardPosList.erase( id );
  109. }
  110. }