wsTimerThread.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // 发送卡的坐标等信息给WEB。人的数据和车的数据是合在一起的
  78. // 分为两块数据,一块是车辆地图的数据,一块是人员地图的数据,合在一起发给web端,用不同
  79. // 的字段进行区分。
  80. // Added by zengminguo 20220424
  81. std::string jsCardPos = __jsBuilder.BuildCardPos(CardPosList);
  82. if(jsCardPos == ""){
  83. return;
  84. }
  85. swsClientMgr.send( JSON_CMD_VALUE_PUSH, jsCardPos );
  86. }
  87. void wsTimerThread::send_card_pos()
  88. {
  89. if(__CardPosList.empty()){
  90. return;
  91. }
  92. std::map<uint64_t, _CARD_POS_> cards;
  93. __CardPosList.copy(cards);
  94. //service_position send
  95. for(auto it = cards.begin(); it != cards.end(); ++it)
  96. {
  97. std::string json_pos = __jsBuilder.build_ios_card_pos(it->second);
  98. //log_info("[service-position] json_pos: card_id=%d, freq=%.2f, json=%s", it->second.ID, it->second.m_freq, json_pos.c_str());
  99. ios_service::m_ios_service->notify(json_pos, std::to_string(it->second.ID), it->second.m_freq);
  100. }
  101. }
  102. void wsTimerThread::temp_send_card_pos()
  103. {
  104. if(__CardPosList.empty()){
  105. return;
  106. }
  107. std::map<uint64_t, _CARD_POS_> cards;
  108. __CardPosList.copy(cards);
  109. std::string json_pos = __jsBuilder.build_tmp_card_pos(cards);
  110. swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_pos);
  111. }
  112. void wsTimerThread::send_light_state()
  113. {
  114. if(light_state_list.empty()){
  115. return;
  116. }
  117. std::vector<light_state> lights = light_state_list;
  118. //log_info("[light_info] light_state's size=%d, copy=%d", light_state_list.size(), lights.size());
  119. std::string json_light = __jsBuilder.build_traffic_light(lights);
  120. swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_light);
  121. light_state_list.clear();
  122. light_state_list.resize(0);
  123. }
  124. void wsTimerThread::send_device_state()
  125. {
  126. if(device_state_list.empty()){
  127. return;
  128. }
  129. std::vector<device_state> ds = device_state_list;
  130. std::string json_device = __jsBuilder.build_device_state(ds);
  131. swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_device);
  132. //device_state_list.erase(device_state_list.begin(), device_state_list.end());
  133. device_state_list.clear();
  134. device_state_list.resize(0);
  135. }
  136. /*
  137. * 定时器线程发送定位数据
  138. *
  139. * */
  140. void wsTimerThread::_ThreadFunc( wsTimerThread * pOwner )
  141. {
  142. log_info("The timer thread is running");
  143. while ( pOwner->__Enable )
  144. {
  145. std::time_t t = time( 0 );
  146. int seconds = (int)std::difftime( t, __LastSendTime );
  147. if ( seconds >= pOwner->__Config.SendInterval )
  148. {
  149. pOwner->__SendCardPos();
  150. //service_position send
  151. //pOwner->send_card_pos();
  152. //pOwner->temp_send_card_pos();
  153. //pOwner->send_light_state();
  154. pOwner->send_device_state();
  155. __LastSendTime = t;
  156. }
  157. boost::this_thread::sleep( boost::posix_time::millisec( 1 ) );
  158. }
  159. pOwner->__ExitCond.notify_one();
  160. }
  161. void wsTimerThread::Init( const _THREAD_CONFIG_ & Config )
  162. {
  163. __Config = Config;
  164. __ChkConfig();
  165. }
  166. void wsTimerThread::upt_card_pos( const _CARD_POS_ & pos )
  167. {
  168. //如果已存在就更新,否则插入
  169. uint64_t type = pos.Type;
  170. uint64_t id=type<<32|pos.ID;
  171. if ( __CardPosList.seek( id ) )
  172. {
  173. __CardPosList.update( id, pos );
  174. }
  175. else
  176. {
  177. __CardPosList.insert( id, pos );
  178. }
  179. }
  180. void wsTimerThread::real_upt_card_pos(const _CARD_POS_& pos)
  181. {
  182. std::string pos_json = __jsBuilder.build_ios_card_pos(pos);
  183. ios_service::m_ios_service->notify(pos_json, std::to_string(pos.ID), 10);
  184. log_info("[service-position] json: %s", pos_json.c_str());
  185. }
  186. void wsTimerThread::del_card_pos( const _CARD_POS_ & pos )
  187. {
  188. uint64_t type = pos.Type;
  189. uint64_t id=type<<32|pos.ID;
  190. __CardPosList.erase( id );
  191. }
  192. void wsTimerThread::upt_light_state(const light_state& light)
  193. {
  194. light_state_list.push_back(light);
  195. }
  196. void wsTimerThread::upt_device_state(const device_state& ds)
  197. {
  198. device_state_list.push_back(ds);
  199. }
  200. }