1
0

wsTimerThread.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "wsTimerThread.h"
  2. #include <iostream>
  3. #include "wsClientMgr.h"
  4. #include "constdef.h"
  5. #include "log.h"
  6. namespace YA
  7. {
  8. wsTimerThread::wsTimerThread()
  9. {
  10. __Reset();
  11. __LastSendTime = { 0 };
  12. __LastSendTime.tm_year = 1970 - 1900;
  13. __LastSendTime.tm_mon = 1;
  14. __LastSendTime.tm_mday = 1;
  15. }
  16. wsTimerThread::~wsTimerThread()
  17. {
  18. Stop();
  19. }
  20. void wsTimerThread::Start()
  21. {
  22. __Running = true;
  23. __Thread.reset( new boost::thread( boost::bind( &wsTimerThread::_ThreadFunc, this, this ) ) );
  24. //__Thread->detach();
  25. }
  26. void wsTimerThread::Stop()
  27. {
  28. if(__Thread==nullptr)
  29. {
  30. return;
  31. }
  32. __Thread->interrupt();
  33. __Running = false;
  34. std::cout << "::Stop() begin" << std::endl;
  35. __Enable = false;
  36. if ( __Running )
  37. {
  38. boost::unique_lock<boost::mutex> lock( __ExitMutex );
  39. __ExitCond.wait( lock );
  40. }
  41. __Reset();
  42. __Thread=nullptr;
  43. std::cout << "::Stop() end" << std::endl;
  44. }
  45. void wsTimerThread::__Reset()
  46. {
  47. __Enable = true;
  48. __Running = false;
  49. }
  50. void wsTimerThread::__ChkConfig()
  51. {
  52. if ( __Config.SendInterval < MIN_SEND_INTERVAL )
  53. {
  54. __Config.SendInterval = MIN_SEND_INTERVAL;
  55. }
  56. }
  57. void wsTimerThread::__SendCardPos()
  58. {
  59. std::map<uint64_t, _CARD_POS_> CardPosList;
  60. if(__CardPosList.empty())
  61. return;
  62. __CardPosList.copy( CardPosList );
  63. std::string jsCardPos = __jsBuilder.BuildCardPos( CardPosList );
  64. log_info("[pos_map]%s",jsCardPos.c_str());
  65. swsClientMgr.send( JSON_CMD_VALUE_PUSH, jsCardPos );
  66. }
  67. void wsTimerThread::_ThreadFunc( wsTimerThread * pOwner )
  68. {
  69. while ( pOwner->__Enable )
  70. {
  71. std::tm *Now;
  72. std::time_t t;
  73. t = time( 0 );
  74. Now = localtime( &t );
  75. int seconds = (int)std::difftime( mktime( Now ), mktime( &__LastSendTime ) );
  76. if ( seconds >= pOwner->__Config.SendInterval )
  77. {
  78. pOwner->__SendCardPos();
  79. __LastSendTime = *Now;
  80. }
  81. boost::this_thread::sleep( boost::posix_time::millisec( 1 ) );
  82. }
  83. pOwner->__ExitCond.notify_one();
  84. }
  85. void wsTimerThread::Init( const _THREAD_CONFIG_ & Config )
  86. {
  87. __Config = Config;
  88. __ChkConfig();
  89. }
  90. void wsTimerThread::upt_card_pos( const _CARD_POS_ & pos )
  91. {
  92. //如果已存在就更新,否则插入
  93. uint64_t type = pos.Type;
  94. uint64_t id=type<<32|pos.ID;
  95. if ( __CardPosList.seek( id ) )
  96. {
  97. __CardPosList.update( id, pos );
  98. }
  99. else
  100. {
  101. __CardPosList.insert( id, pos );
  102. }
  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. }