wsTimerThread.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.clear();
  118. light_state_list.resize(0);
  119. }
  120. void wsTimerThread::send_device_state()
  121. {
  122. if(device_state_list.empty()){
  123. return;
  124. }
  125. std::vector<device_state> ds = device_state_list;
  126. std::string json_device = __jsBuilder.build_device_state(ds);
  127. swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_device);
  128. //device_state_list.erase(device_state_list.begin(), device_state_list.end());
  129. device_state_list.clear();
  130. device_state_list.resize(0);
  131. }
  132. /*
  133. * 定时器线程发送定位数据
  134. *
  135. * */
  136. void wsTimerThread::_ThreadFunc( wsTimerThread * pOwner )
  137. {
  138. log_info("The timer thread is running");
  139. while ( pOwner->__Enable )
  140. {
  141. std::time_t t = time( 0 );
  142. int seconds = (int)std::difftime( t, __LastSendTime );
  143. if ( seconds >= pOwner->__Config.SendInterval )
  144. {
  145. pOwner->__SendCardPos();
  146. //service_position send
  147. //pOwner->send_card_pos();
  148. //pOwner->temp_send_card_pos();
  149. //pOwner->send_light_state();
  150. pOwner->send_device_state();
  151. __LastSendTime = t;
  152. }
  153. boost::this_thread::sleep( boost::posix_time::millisec( 1 ) );
  154. }
  155. pOwner->__ExitCond.notify_one();
  156. }
  157. void wsTimerThread::Init( const _THREAD_CONFIG_ & Config )
  158. {
  159. __Config = Config;
  160. __ChkConfig();
  161. }
  162. void wsTimerThread::upt_card_pos( const _CARD_POS_ & pos )
  163. {
  164. //如果已存在就更新,否则插入
  165. uint64_t type = pos.Type;
  166. uint64_t id=type<<32|pos.ID;
  167. if ( __CardPosList.seek( id ) )
  168. {
  169. __CardPosList.update( id, pos );
  170. }
  171. else
  172. {
  173. __CardPosList.insert( id, pos );
  174. }
  175. }
  176. void wsTimerThread::real_upt_card_pos(const _CARD_POS_& pos)
  177. {
  178. std::string pos_json = __jsBuilder.build_ios_card_pos(pos);
  179. ios_service::m_ios_service->notify(pos_json, std::to_string(pos.ID), 10);
  180. log_info("[service-position] json: %s", pos_json.c_str());
  181. }
  182. void wsTimerThread::del_card_pos( const _CARD_POS_ & pos )
  183. {
  184. uint64_t type = pos.Type;
  185. uint64_t id=type<<32|pos.ID;
  186. __CardPosList.erase( id );
  187. }
  188. void wsTimerThread::upt_light_state(const light_state& light)
  189. {
  190. light_state_list.push_back(light);
  191. }
  192. void wsTimerThread::upt_device_state(const device_state& ds)
  193. {
  194. device_state_list.push_back(ds);
  195. }
  196. }