wsTimerThread.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include "module_service/module_screen.h"
  8. namespace sys
  9. {
  10. wsTimerThread::wsTimerThread()
  11. {
  12. __Reset();
  13. }
  14. wsTimerThread::~wsTimerThread()
  15. {
  16. Stop();
  17. }
  18. void wsTimerThread::Start()
  19. {
  20. __Running = true;
  21. __Thread.reset( new boost::thread( boost::bind( &wsTimerThread::_ThreadFunc, this, this ) ) );
  22. //__Thread->detach();
  23. }
  24. void wsTimerThread::Stop()
  25. {
  26. if(__Thread==nullptr)
  27. {
  28. return;
  29. }
  30. __Thread->interrupt();
  31. __Running = false;
  32. std::cout << "wsTimerThread::Stop() begin" << std::endl;
  33. __Enable = false;
  34. if ( __Running )
  35. {
  36. boost::unique_lock<boost::mutex> lock( __ExitMutex );
  37. __ExitCond.wait( lock );
  38. }
  39. __Reset();
  40. __Thread=nullptr;
  41. std::cout << "wsTimerThread::Stop() end" << std::endl;
  42. }
  43. void wsTimerThread::__Reset()
  44. {
  45. __Enable = true;
  46. __Running = false;
  47. }
  48. void wsTimerThread::__ChkConfig()
  49. {
  50. if ( __Config.SendInterval < MIN_SEND_INTERVAL )
  51. {
  52. __Config.SendInterval = MIN_SEND_INTERVAL;
  53. }
  54. }
  55. void wsTimerThread::__SendCardPos()
  56. {
  57. // 大屏只显示人卡相关数据
  58. std::map<uint64_t, _CARD_POS_> cpl;
  59. __CardPosList.copy(cpl);
  60. for(auto it = cpl.begin();it != cpl.end();){
  61. if(1 == it->second.Type){
  62. cpl.insert(std::make_pair(it->first, it->second));
  63. it++;
  64. }else{
  65. cpl.erase(it++);
  66. }
  67. }
  68. // 大屏相关数据业务处理
  69. if(cpl.size() > 0){
  70. module_screen::instance()->do_write(cpl);
  71. }
  72. // 人车数据发送给web端
  73. std::map<uint64_t, _CARD_POS_> CardPosList;
  74. if(__CardPosList.empty())
  75. return;
  76. __CardPosList.copy( CardPosList );
  77. std::string jsCardPos = __jsBuilder.BuildCardPos(CardPosList);
  78. if(jsCardPos == ""){
  79. return;
  80. }
  81. swsClientMgr.send( JSON_CMD_VALUE_PUSH, jsCardPos );
  82. }
  83. void wsTimerThread::send_card_pos()
  84. {
  85. if(__CardPosList.empty()){
  86. return;
  87. }
  88. std::map<uint64_t, _CARD_POS_> cards;
  89. __CardPosList.copy(cards);
  90. //service_position send
  91. for(auto it = cards.begin(); it != cards.end(); ++it)
  92. {
  93. std::string json_pos = __jsBuilder.build_ios_card_pos(it->second);
  94. //log_info("[service-position] json_pos: card_id=%d, freq=%.2f, json=%s", it->second.ID, it->second.m_freq, json_pos.c_str());
  95. ios_service::m_ios_service->notify(json_pos, std::to_string(it->second.ID), it->second.m_freq);
  96. }
  97. }
  98. void wsTimerThread::temp_send_card_pos()
  99. {
  100. if(__CardPosList.empty()){
  101. return;
  102. }
  103. std::map<uint64_t, _CARD_POS_> cards;
  104. __CardPosList.copy(cards);
  105. std::string json_pos = __jsBuilder.build_tmp_card_pos(cards);
  106. swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_pos);
  107. }
  108. void wsTimerThread::send_light_state()
  109. {
  110. if(light_state_list.empty()){
  111. return;
  112. }
  113. std::vector<light_state> lights = light_state_list;
  114. //log_info("[light_info] light_state's size=%d, copy=%d", light_state_list.size(), lights.size());
  115. std::string json_light = __jsBuilder.build_traffic_light(lights);
  116. swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_light);
  117. light_state_list.erase(light_state_list.begin(), light_state_list.end());
  118. }
  119. /*
  120. * 定时器线程发送定位数据
  121. *
  122. * */
  123. void wsTimerThread::_ThreadFunc( wsTimerThread * pOwner )
  124. {
  125. log_info("The timer thread is running");
  126. while ( pOwner->__Enable )
  127. {
  128. std::time_t t = time( 0 );
  129. int seconds = (int)std::difftime( t, __LastSendTime );
  130. if ( seconds >= pOwner->__Config.SendInterval )
  131. {
  132. pOwner->__SendCardPos();
  133. //service_position send
  134. //pOwner->send_card_pos();
  135. //pOwner->temp_send_card_pos();
  136. pOwner->send_light_state();
  137. __LastSendTime = t;
  138. }
  139. boost::this_thread::sleep( boost::posix_time::millisec( 1 ) );
  140. }
  141. pOwner->__ExitCond.notify_one();
  142. }
  143. void wsTimerThread::Init( const _THREAD_CONFIG_ & Config )
  144. {
  145. __Config = Config;
  146. __ChkConfig();
  147. }
  148. void wsTimerThread::upt_card_pos( const _CARD_POS_ & pos )
  149. {
  150. //如果已存在就更新,否则插入
  151. uint64_t type = pos.Type;
  152. uint64_t id=type<<32|pos.ID;
  153. if ( __CardPosList.seek( id ) )
  154. {
  155. __CardPosList.update( id, pos );
  156. }
  157. else
  158. {
  159. __CardPosList.insert( id, pos );
  160. }
  161. }
  162. void wsTimerThread::real_upt_card_pos(const _CARD_POS_& pos)
  163. {
  164. std::string pos_json = __jsBuilder.build_ios_card_pos(pos);
  165. ios_service::m_ios_service->notify(pos_json, std::to_string(pos.ID), 10);
  166. log_info("[service-position] json: %s", pos_json.c_str());
  167. }
  168. void wsTimerThread::del_card_pos( const _CARD_POS_ & pos )
  169. {
  170. uint64_t type = pos.Type;
  171. uint64_t id=type<<32|pos.ID;
  172. __CardPosList.erase( id );
  173. }
  174. void wsTimerThread::upt_light_state(const light_state& light)
  175. {
  176. light_state_list.push_back(light);
  177. }
  178. }