wsTimerThread.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. std::cout << "::Stop() begin" << std::endl;
  29. __Enable = false;
  30. if ( __Running )
  31. {
  32. boost::unique_lock<boost::mutex> lock( __ExitMutex );
  33. __ExitCond.wait( lock );
  34. }
  35. __Reset();
  36. std::cout << "::Stop() end" << std::endl;
  37. }
  38. void wsTimerThread::__Reset()
  39. {
  40. __Enable = true;
  41. __Running = false;
  42. }
  43. void wsTimerThread::__ChkConfig()
  44. {
  45. if ( __Config.SendInterval < MIN_SEND_INTERVAL )
  46. {
  47. __Config.SendInterval = MIN_SEND_INTERVAL;
  48. }
  49. }
  50. void wsTimerThread::__SendCardPos()
  51. {
  52. std::map<uint64_t, _CARD_POS_> CardPosList;
  53. if(__CardPosList.empty())
  54. return;
  55. __CardPosList.copy( CardPosList );
  56. std::string jsCardPos = __jsBuilder.BuildCardPos( CardPosList );
  57. log_info("[pos_map]%s",jsCardPos.c_str());
  58. swsClientMgr.send( JSON_CMD_VALUE_PUSH, jsCardPos );
  59. }
  60. void wsTimerThread::_ThreadFunc( wsTimerThread * pOwner )
  61. {
  62. while ( pOwner->__Enable )
  63. {
  64. std::tm *Now;
  65. std::time_t t;
  66. t = time( 0 );
  67. Now = localtime( &t );
  68. int seconds = (int)std::difftime( mktime( Now ), mktime( &__LastSendTime ) );
  69. if ( seconds >= pOwner->__Config.SendInterval )
  70. {
  71. pOwner->__SendCardPos();
  72. __LastSendTime = *Now;
  73. }
  74. boost::this_thread::sleep( boost::posix_time::millisec( 1 ) );
  75. }
  76. pOwner->__ExitCond.notify_one();
  77. }
  78. void wsTimerThread::Init( const _THREAD_CONFIG_ & Config )
  79. {
  80. __Config = Config;
  81. __ChkConfig();
  82. }
  83. void wsTimerThread::upt_card_pos( const _CARD_POS_ & pos )
  84. {
  85. //如果已存在就更新,否则插入
  86. uint64_t type = pos.Type;
  87. uint64_t id=type<<32|pos.ID;
  88. if ( __CardPosList.seek( id ) )
  89. {
  90. __CardPosList.update( id, pos );
  91. }
  92. else
  93. {
  94. __CardPosList.insert( id, pos );
  95. }
  96. }
  97. void wsTimerThread::del_card_pos( const _CARD_POS_ & pos )
  98. {
  99. uint64_t type = pos.Type;
  100. uint64_t id=type<<32|pos.ID;
  101. __CardPosList.erase( id );
  102. }
  103. }