wsTimerThread.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. void wsTimerThread::send_tof_data()
  133. {
  134. if(tof_data_list.empty()){
  135. return;
  136. }
  137. try{
  138. std::lock_guard<std::mutex> lg(m_mtx);
  139. std::vector<tof_data> vtd;
  140. //vtd.insert(vtd.end(), tof_data_list.begin(), tof_data_list.end());
  141. vtd.swap(tof_data_list);
  142. std::string val = __jsBuilder.build_tof_data(vtd);
  143. swsClientMgr.send(JSON_CMD_VALUE_PUSH, val);
  144. //tof_data_list.clear();
  145. //std::vector<tof_data>().swap(tof_data_list);
  146. tof_data_list.clear();
  147. //tof_data_list.resize(0);
  148. }catch(std::exception& e){
  149. //log_error("[error] err=%s", e.what());
  150. log_error("[error] err=%s", e.what());
  151. }
  152. }
  153. void wsTimerThread::send_solid_ball_data()
  154. {
  155. if(sb_data_list.empty()){
  156. return;
  157. }
  158. try{
  159. std::lock_guard<std::mutex> lg(m_mtx);
  160. std::vector<sb_data> vts;
  161. vts.swap(sb_data_list);
  162. std::string val = __jsBuilder.build_sb_data(vts);
  163. swsClientMgr.send(JSON_CMD_VALUE_PUSH, val);
  164. sb_data_list.clear();
  165. }catch(std::exception& e){
  166. log_error("[error] err=%s", e.what());
  167. }
  168. }
  169. /*
  170. * 定时器线程发送定位数据
  171. *
  172. * */
  173. void wsTimerThread::_ThreadFunc( wsTimerThread * pOwner )
  174. {
  175. //log_info("The timer thread is running");
  176. while ( pOwner->__Enable )
  177. {
  178. std::time_t t = time( 0 );
  179. int seconds = (int)std::difftime( t, __LastSendTime );
  180. if ( seconds >= pOwner->__Config.SendInterval )
  181. {
  182. pOwner->__SendCardPos();
  183. //service_position send
  184. //pOwner->send_card_pos();
  185. //pOwner->temp_send_card_pos();
  186. //pOwner->send_light_state();
  187. pOwner->send_device_state();
  188. pOwner->send_tof_data();
  189. __LastSendTime = t;
  190. }
  191. pOwner->send_solid_ball_data();
  192. boost::this_thread::sleep( boost::posix_time::millisec( 1 ) );
  193. }
  194. pOwner->__ExitCond.notify_one();
  195. }
  196. void wsTimerThread::Init( const _THREAD_CONFIG_ & Config )
  197. {
  198. __Config = Config;
  199. __ChkConfig();
  200. }
  201. void wsTimerThread::upt_card_pos( const _CARD_POS_ & pos )
  202. {
  203. //如果已存在就更新,否则插入
  204. try{
  205. uint64_t type = pos.Type;
  206. uint64_t id=type<<32|pos.ID;
  207. if ( __CardPosList.seek( id ) )
  208. {
  209. __CardPosList.update( id, pos );
  210. }
  211. else
  212. {
  213. __CardPosList.insert( id, pos );
  214. }
  215. }catch(std::exception& e){
  216. log_error("[error] err=%s", e.what());
  217. }
  218. }
  219. void wsTimerThread::real_upt_card_pos(const _CARD_POS_& pos)
  220. {
  221. std::string pos_json = __jsBuilder.build_ios_card_pos(pos);
  222. ios_service::m_ios_service->notify(pos_json, std::to_string(pos.ID), 10);
  223. log_info("[service-position] json: %s", pos_json.c_str());
  224. }
  225. void wsTimerThread::del_card_pos( const _CARD_POS_ & pos )
  226. {
  227. uint64_t type = pos.Type;
  228. uint64_t id=type<<32|pos.ID;
  229. __CardPosList.erase( id );
  230. }
  231. void wsTimerThread::upt_light_state(const light_state& light)
  232. {
  233. light_state_list.push_back(light);
  234. }
  235. void wsTimerThread::upt_device_state(const device_state& ds)
  236. {
  237. device_state_list.push_back(ds);
  238. }
  239. void wsTimerThread::upt_tof_data(const tof_data& td)
  240. {
  241. try{
  242. std::lock_guard<std::mutex> lg(m_mtx);
  243. tof_data_list.push_back(td);
  244. }catch(std::exception& e){
  245. std::string err = e.what();
  246. log_error("[error] err=%s", err.c_str());
  247. }
  248. }
  249. void wsTimerThread::upt_sb_data(const sb_data& val)
  250. {
  251. try{
  252. std::lock_guard<std::mutex> lg(m_mtx);
  253. sb_data_list.push_back(val);
  254. }catch(std::exception& e){
  255. log_error("[error] err=%s", e.what());
  256. }
  257. }
  258. }