wsTimerThread.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. #include "../tool_time.h"
  9. #include "../db/db_api/CDBSingletonDefine.h"
  10. namespace sys
  11. {
  12. wsTimerThread::wsTimerThread()
  13. {
  14. __Reset();
  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. if(__Thread==nullptr)
  29. {
  30. return;
  31. }
  32. __Thread->interrupt();
  33. __Running = false;
  34. std::cout << "wsTimerThread::Stop() begin" << std::endl;
  35. __Enable = false;
  36. if ( __Running )
  37. {
  38. boost::unique_lock<boost::mutex> lock( __ExitMutex );
  39. __ExitCond.wait( lock );
  40. }
  41. __Reset();
  42. __Thread=nullptr;
  43. std::cout << "wsTimerThread::Stop() end" << std::endl;
  44. }
  45. void wsTimerThread::__Reset()
  46. {
  47. __Enable = true;
  48. __Running = false;
  49. }
  50. void wsTimerThread::__ChkConfig()
  51. {
  52. if ( __Config.SendInterval < MIN_SEND_INTERVAL )
  53. {
  54. __Config.SendInterval = MIN_SEND_INTERVAL;
  55. }
  56. }
  57. void wsTimerThread::__SendCardPos()
  58. {
  59. // 大屏只显示人卡相关数据
  60. std::map<uint64_t, _CARD_POS_> cpl;
  61. __CardPosList.copy(cpl);
  62. for(auto it = cpl.begin();it != cpl.end();){
  63. if(1 == it->second.Type){
  64. cpl.insert(std::make_pair(it->first, it->second));
  65. it++;
  66. }else{
  67. cpl.erase(it++);
  68. }
  69. }
  70. // 大屏相关数据业务处理
  71. if(cpl.size() > 0){
  72. module_screen::instance()->do_write(cpl);
  73. }
  74. // 人车数据发送给web端
  75. std::map<uint64_t, _CARD_POS_> CardPosList;
  76. if(__CardPosList.empty())
  77. return;
  78. __CardPosList.copy( CardPosList );
  79. std::string jsCardPos = __jsBuilder.BuildCardPos(CardPosList);
  80. if(jsCardPos == ""){
  81. return;
  82. }
  83. swsClientMgr.send( JSON_CMD_VALUE_PUSH, jsCardPos );
  84. }
  85. void wsTimerThread::send_card_pos()
  86. {
  87. if(__CardPosList.empty()){
  88. return;
  89. }
  90. std::map<uint64_t, _CARD_POS_> cards;
  91. __CardPosList.copy(cards);
  92. //service_position send
  93. for(auto it = cards.begin(); it != cards.end(); ++it)
  94. {
  95. std::string json_pos = __jsBuilder.build_ios_card_pos(it->second);
  96. //log_info("[service-position] json_pos: card_id=%d, freq=%.2f, json=%s", it->second.ID, it->second.m_freq, json_pos.c_str());
  97. ios_service::m_ios_service->notify(json_pos, std::to_string(it->second.ID), it->second.m_freq);
  98. }
  99. }
  100. void wsTimerThread::temp_send_card_pos()
  101. {
  102. if(__CardPosList.empty()){
  103. return;
  104. }
  105. std::map<uint64_t, _CARD_POS_> cards;
  106. __CardPosList.copy(cards);
  107. std::string json_pos = __jsBuilder.build_tmp_card_pos(cards);
  108. swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_pos);
  109. }
  110. void wsTimerThread::send_light_state()
  111. {
  112. if(light_state_list.empty()){
  113. return;
  114. }
  115. std::vector<light_state> lights = light_state_list;
  116. //log_info("[light_info] light_state's size=%d, copy=%d", light_state_list.size(), lights.size());
  117. std::string json_light = __jsBuilder.build_traffic_light(lights);
  118. swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_light);
  119. light_state_list.clear();
  120. light_state_list.resize(0);
  121. }
  122. void wsTimerThread::send_device_state()
  123. {
  124. if(device_state_list.empty()){
  125. return;
  126. }
  127. std::vector<device_state> ds = device_state_list;
  128. std::string json_device = __jsBuilder.build_device_state(ds);
  129. swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_device);
  130. //device_state_list.erase(device_state_list.begin(), device_state_list.end());
  131. device_state_list.clear();
  132. device_state_list.resize(0);
  133. }
  134. void wsTimerThread::send_tof_data()
  135. {
  136. if(tof_data_list.empty()){
  137. return;
  138. }
  139. try{
  140. std::lock_guard<std::mutex> lg(m_mtx);
  141. std::vector<tof_data> vtd;
  142. //vtd.insert(vtd.end(), tof_data_list.begin(), tof_data_list.end());
  143. vtd.swap(tof_data_list);
  144. std::string val = __jsBuilder.build_tof_data(vtd);
  145. swsClientMgr.send(JSON_CMD_VALUE_PUSH, val);
  146. log_info("sended msg:%s", val.c_str());
  147. //tof_data_list.clear();
  148. //std::vector<tof_data>().swap(tof_data_list);
  149. tof_data_list.clear();
  150. //tof_data_list.resize(0);
  151. }catch(std::exception& e){
  152. //log_error("[error] err=%s", e.what());
  153. log_error("[error] err=%s", e.what());
  154. }
  155. }
  156. void wsTimerThread::send_tof_data_oneAntenna()
  157. {
  158. if (tof_data_list_oneAntenna.empty()) {
  159. log_info("send_tof_data_oneAntenna_return");
  160. return;
  161. }
  162. log_info("send_tof_data_oneAntenna_enter");
  163. try {
  164. std::lock_guard<std::mutex> lg(m_mtx);
  165. std::vector<tof_data_oneAntenna> vtd;
  166. //vtd.insert(vtd.end(), tof_data_list.begin(), tof_data_list.end());
  167. vtd.swap(tof_data_list_oneAntenna);
  168. for (size_t i = 0; i < vtd.size(); i++)
  169. {
  170. std::vector<tof_data_oneAntenna> vtdSingle;
  171. vtdSingle.push_back(vtd[i]);
  172. std::string val = __jsBuilder.build_tof_data_oneAntenna(vtdSingle);
  173. swsClientMgr.send(JSON_CMD_VALUE_PUSH, val);
  174. std::string valThirdPart = __jsBuilder.build_tof_data_oneAntenna_third_part(vtdSingle);
  175. ios_service::m_ios_service->notify(valThirdPart, "12", 13);
  176. log_info("sended third part:%s", valThirdPart.c_str());
  177. log_info("sended msg:%s", val.c_str());
  178. std::string strDistances = "";
  179. for (auto iterSiteDist = vtd[i].map_site_id_dist.begin(); iterSiteDist != vtd[i].map_site_id_dist.end(); iterSiteDist++)
  180. {
  181. if (strDistances != "")
  182. {
  183. strDistances += ",";
  184. }
  185. char szDistance[1024] = { 0 };
  186. snprintf(szDistance, 1024, "%.2f,%d",
  187. iterSiteDist->second,
  188. iterSiteDist->first);
  189. strDistances += szDistance;
  190. }
  191. std::string time = tool_time::to_str(tool_time::now_to_seconds()).c_str();
  192. //按新表插入
  193. char sql[1024] = { 0 };
  194. std::string strCardID = vtd[i].cid;
  195. strCardID = strCardID.substr(strCardID.length() - 4, 4);
  196. snprintf(sql, 1024, "insert into his_location_cell_card_tof_aodi(card_id, loc_time, distances) values('%s', '%s', '%s')",
  197. strCardID.c_str(),
  198. time.c_str(),
  199. strDistances.c_str());
  200. log_info(sql);
  201. sDBConnPool.PushAsync(sql);
  202. }
  203. //tof_data_list.clear();
  204. //std::vector<tof_data>().swap(tof_data_list);
  205. tof_data_list_oneAntenna.clear();
  206. //tof_data_list.resize(0);
  207. }
  208. catch (std::exception& e) {
  209. //log_error("[error] err=%s", e.what());
  210. log_error("[error] err=%s", e.what());
  211. }
  212. }
  213. void wsTimerThread::send_solid_ball_data()
  214. {
  215. if(sb_data_list.empty()){
  216. return;
  217. }
  218. try{
  219. std::lock_guard<std::mutex> lg(m_mtx);
  220. std::vector<sb_data> vts;
  221. vts.swap(sb_data_list);
  222. std::string val = __jsBuilder.build_sb_data(vts);
  223. swsClientMgr.send(JSON_CMD_VALUE_PUSH, val);
  224. sb_data_list.clear();
  225. }catch(std::exception& e){
  226. log_error("[error] err=%s", e.what());
  227. }
  228. }
  229. // generator 2d tdoa's json data to send to web client
  230. void wsTimerThread::send_tdoa_2d_data()
  231. {
  232. if (tdoa_2d_data_list.empty()) {
  233. return;
  234. }
  235. try {
  236. std::lock_guard<std::mutex> lg(m_mtx);
  237. std::vector<tdoa_2d_data> vts;
  238. vts.swap(tdoa_2d_data_list);
  239. std::string val = __jsBuilder.build_tdoa_2d_data(vts);
  240. log_info("sended msg:%s", val.c_str());
  241. swsClientMgr.send(JSON_CMD_VALUE_PUSH, val);
  242. tdoa_2d_data_list.clear();
  243. }
  244. catch (std::exception& e) {
  245. log_error("[error] err = %s", e.what());
  246. }
  247. }
  248. // generator 3d tof's json data to send to web client
  249. void wsTimerThread::send_tof_3d_data()
  250. {
  251. if(tof_3d_data_list.empty()){
  252. return;
  253. }
  254. try{
  255. std::lock_guard<std::mutex> lg(m_mtx);
  256. std::vector<tof_3d_data> vts;
  257. vts.swap(tof_3d_data_list);
  258. std::string val = __jsBuilder.build_tof_3d_data(vts);
  259. static int n_sended_time = 0;
  260. if (n_sended_time >= 0)
  261. {
  262. log_info("sended msg:%s", val.c_str());
  263. swsClientMgr.send(JSON_CMD_VALUE_PUSH, val);
  264. n_sended_time = 0;
  265. }
  266. n_sended_time++;
  267. tof_3d_data_list.clear();
  268. }
  269. catch(std::exception& e){
  270. log_error("[error] err = %s", e.what());
  271. }
  272. }
  273. /*
  274. * 定时器线程发送定位数据
  275. *
  276. * */
  277. void wsTimerThread::_ThreadFunc( wsTimerThread * pOwner )
  278. {
  279. //log_info("The timer thread is running");
  280. while ( pOwner->__Enable )
  281. {
  282. std::time_t t = time( 0 );
  283. //int seconds = (int)std::difftime( t, __LastSendTime );
  284. double seconds = std::difftime(t, __LastSendTime);
  285. {
  286. //pOwner->__SendCardPos();
  287. //service_position send
  288. //pOwner->send_card_pos();
  289. //pOwner->temp_send_card_pos();
  290. //pOwner->send_light_state();
  291. //pOwner->send_device_state();
  292. //pOwner->send_tof_data();
  293. pOwner->send_tof_data_oneAntenna();
  294. __LastSendTime = t;
  295. }
  296. //pOwner->send_solid_ball_data();
  297. //pOwner->send_tof_3d_data();
  298. pOwner->send_tdoa_2d_data();
  299. // 100毫秒发一次数据
  300. boost::this_thread::sleep( boost::posix_time::millisec( 100 ) );
  301. }
  302. pOwner->__ExitCond.notify_one();
  303. }
  304. void wsTimerThread::Init( const _THREAD_CONFIG_ & Config )
  305. {
  306. __Config = Config;
  307. __ChkConfig();
  308. }
  309. void wsTimerThread::upt_card_pos( const _CARD_POS_ & pos )
  310. {
  311. //如果已存在就更新,否则插入
  312. try{
  313. uint64_t type = pos.Type;
  314. uint64_t id=type<<32|pos.ID;
  315. if ( __CardPosList.seek( id ) )
  316. {
  317. __CardPosList.update( id, pos );
  318. }
  319. else
  320. {
  321. __CardPosList.insert( id, pos );
  322. }
  323. }catch(std::exception& e){
  324. log_error("[error] err=%s", e.what());
  325. }
  326. }
  327. void wsTimerThread::real_upt_card_pos(const _CARD_POS_& pos)
  328. {
  329. std::string pos_json = __jsBuilder.build_ios_card_pos(pos);
  330. ios_service::m_ios_service->notify(pos_json, std::to_string(pos.ID), 10);
  331. log_info("[service-position] json: %s", pos_json.c_str());
  332. }
  333. void wsTimerThread::del_card_pos( const _CARD_POS_ & pos )
  334. {
  335. uint64_t type = pos.Type;
  336. uint64_t id=type<<32|pos.ID;
  337. __CardPosList.erase( id );
  338. }
  339. void wsTimerThread::upt_light_state(const light_state& light)
  340. {
  341. light_state_list.push_back(light);
  342. }
  343. void wsTimerThread::upt_device_state(const device_state& ds)
  344. {
  345. device_state_list.push_back(ds);
  346. }
  347. void wsTimerThread::upt_tof_data(const tof_data& td)
  348. {
  349. try{
  350. std::lock_guard<std::mutex> lg(m_mtx);
  351. tof_data_list.push_back(td);
  352. }catch(std::exception& e){
  353. std::string err = e.what();
  354. log_error("[error] err=%s", err.c_str());
  355. }
  356. }
  357. void wsTimerThread::upt_tof_data_oneAntenna(const std::vector<tof_data_oneAntenna>& vec_td)
  358. {
  359. try {
  360. std::lock_guard<std::mutex> lg(m_mtx);
  361. for (auto iter = vec_td.begin(); iter != vec_td.end(); iter++)
  362. {
  363. tof_data_list_oneAntenna.push_back(*iter);
  364. }
  365. }
  366. catch (std::exception& e) {
  367. std::string err = e.what();
  368. log_error("[error] err=%s", err.c_str());
  369. }
  370. }
  371. void wsTimerThread::upt_sb_data(const sb_data& val)
  372. {
  373. try{
  374. std::lock_guard<std::mutex> lg(m_mtx);
  375. sb_data_list.push_back(val);
  376. }catch(std::exception& e){
  377. log_error("[error] err=%s", e.what());
  378. }
  379. }
  380. void wsTimerThread::upt_tdoa_2d_data(const tdoa_2d_data& val)
  381. {
  382. try {
  383. std::lock_guard<std::mutex> lg(m_mtx);
  384. tdoa_2d_data_list.push_back(val);
  385. }
  386. catch (std::exception& e) {
  387. log_error("[error] err = %s", e.what());
  388. }
  389. }
  390. void wsTimerThread::upt_tof_3d_data(const tof_3d_data& val)
  391. {
  392. try{
  393. std::lock_guard<std::mutex> lg(m_mtx);
  394. tof_3d_data_list.push_back(val);
  395. }catch(std::exception& e){
  396. log_error("[error] err = %s", e.what());
  397. }
  398. }
  399. }