123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- #include "wsTimerThread.h"
- #include <iostream>
- #include "wsClientMgr.h"
- #include "constdef.h"
- #include "log.h"
- #include "sys_setting.h"
- #include "module_service/module_screen.h"
- #include "../tool_time.h"
- #include "../db/db_api/CDBSingletonDefine.h"
- namespace sys
- {
- wsTimerThread::wsTimerThread()
- {
- __Reset();
- }
- wsTimerThread::~wsTimerThread()
- {
- Stop();
- }
- void wsTimerThread::Start()
- {
- __Running = true;
- __Thread.reset( new boost::thread( boost::bind( &wsTimerThread::_ThreadFunc, this, this ) ) );
- //__Thread->detach();
- }
- void wsTimerThread::Stop()
- {
- if(__Thread==nullptr)
- {
- return;
- }
- __Thread->interrupt();
- __Running = false;
- std::cout << "wsTimerThread::Stop() begin" << std::endl;
- __Enable = false;
- if ( __Running )
- {
- boost::unique_lock<boost::mutex> lock( __ExitMutex );
- __ExitCond.wait( lock );
- }
- __Reset();
- __Thread=nullptr;
- std::cout << "wsTimerThread::Stop() end" << std::endl;
- }
- void wsTimerThread::__Reset()
- {
- __Enable = true;
- __Running = false;
- }
- void wsTimerThread::__ChkConfig()
- {
- if ( __Config.SendInterval < MIN_SEND_INTERVAL )
- {
- __Config.SendInterval = MIN_SEND_INTERVAL;
- }
- }
- void wsTimerThread::__SendCardPos()
- {
- // 大屏只显示人卡相关数据
- std::map<uint64_t, _CARD_POS_> cpl;
- __CardPosList.copy(cpl);
- for(auto it = cpl.begin();it != cpl.end();){
- if(1 == it->second.Type){
- cpl.insert(std::make_pair(it->first, it->second));
- it++;
- }else{
- cpl.erase(it++);
- }
- }
- // 大屏相关数据业务处理
- if(cpl.size() > 0){
- module_screen::instance()->do_write(cpl);
- }
-
- // 人车数据发送给web端
- std::map<uint64_t, _CARD_POS_> CardPosList;
- if(__CardPosList.empty())
- return;
- __CardPosList.copy( CardPosList );
-
- std::string jsCardPos = __jsBuilder.BuildCardPos(CardPosList);
- if(jsCardPos == ""){
- return;
- }
- swsClientMgr.send( JSON_CMD_VALUE_PUSH, jsCardPos );
- }
- void wsTimerThread::send_card_pos()
- {
- if(__CardPosList.empty()){
- return;
- }
- std::map<uint64_t, _CARD_POS_> cards;
- __CardPosList.copy(cards);
- //service_position send
- for(auto it = cards.begin(); it != cards.end(); ++it)
- {
- std::string json_pos = __jsBuilder.build_ios_card_pos(it->second);
- //log_info("[service-position] json_pos: card_id=%d, freq=%.2f, json=%s", it->second.ID, it->second.m_freq, json_pos.c_str());
- ios_service::m_ios_service->notify(json_pos, std::to_string(it->second.ID), it->second.m_freq);
- }
- }
- void wsTimerThread::temp_send_card_pos()
- {
- if(__CardPosList.empty()){
- return;
- }
- std::map<uint64_t, _CARD_POS_> cards;
- __CardPosList.copy(cards);
- std::string json_pos = __jsBuilder.build_tmp_card_pos(cards);
- swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_pos);
- }
- void wsTimerThread::send_light_state()
- {
- if(light_state_list.empty()){
- return;
- }
- std::vector<light_state> lights = light_state_list;
- //log_info("[light_info] light_state's size=%d, copy=%d", light_state_list.size(), lights.size());
- std::string json_light = __jsBuilder.build_traffic_light(lights);
- swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_light);
- light_state_list.clear();
- light_state_list.resize(0);
- }
- void wsTimerThread::send_device_state()
- {
- if(device_state_list.empty()){
- return;
- }
- std::vector<device_state> ds = device_state_list;
- std::string json_device = __jsBuilder.build_device_state(ds);
- swsClientMgr.send(JSON_CMD_VALUE_PUSH, json_device);
- //device_state_list.erase(device_state_list.begin(), device_state_list.end());
- device_state_list.clear();
- device_state_list.resize(0);
- }
- void wsTimerThread::send_tof_data()
- {
- if(tof_data_list.empty()){
- return;
- }
- try{
- std::lock_guard<std::mutex> lg(m_mtx);
- std::vector<tof_data> vtd;
- //vtd.insert(vtd.end(), tof_data_list.begin(), tof_data_list.end());
- vtd.swap(tof_data_list);
- std::string val = __jsBuilder.build_tof_data(vtd);
- swsClientMgr.send(JSON_CMD_VALUE_PUSH, val);
- log_info("sended msg:%s", val.c_str());
- //tof_data_list.clear();
- //std::vector<tof_data>().swap(tof_data_list);
- tof_data_list.clear();
- //tof_data_list.resize(0);
- }catch(std::exception& e){
- //log_error("[error] err=%s", e.what());
- log_error("[error] err=%s", e.what());
- }
- }
- void wsTimerThread::send_tof_data_oneAntenna()
- {
- if (tof_data_list_oneAntenna.empty()) {
- log_info("send_tof_data_oneAntenna_return");
- return;
- }
- log_info("send_tof_data_oneAntenna_enter");
- try {
- std::lock_guard<std::mutex> lg(m_mtx);
- std::vector<tof_data_oneAntenna> vtd;
- //vtd.insert(vtd.end(), tof_data_list.begin(), tof_data_list.end());
- vtd.swap(tof_data_list_oneAntenna);
- for (size_t i = 0; i < vtd.size(); i++)
- {
- std::vector<tof_data_oneAntenna> vtdSingle;
- vtdSingle.push_back(vtd[i]);
- std::string val = __jsBuilder.build_tof_data_oneAntenna(vtdSingle);
- swsClientMgr.send(JSON_CMD_VALUE_PUSH, val);
- std::string valThirdPart = __jsBuilder.build_tof_data_oneAntenna_third_part(vtdSingle);
- ios_service::m_ios_service->notify(valThirdPart, "12", 13);
- log_info("sended third part:%s", valThirdPart.c_str());
- log_info("sended msg:%s", val.c_str());
- std::string strDistances = "";
- for (auto iterSiteDist = vtd[i].map_site_id_dist.begin(); iterSiteDist != vtd[i].map_site_id_dist.end(); iterSiteDist++)
- {
- if (strDistances != "")
- {
- strDistances += ",";
- }
- char szDistance[1024] = { 0 };
- snprintf(szDistance, 1024, "%.2f,%d",
- iterSiteDist->second,
- iterSiteDist->first);
- strDistances += szDistance;
- }
- std::string time = tool_time::to_str(tool_time::now_to_seconds()).c_str();
- //按新表插入
- char sql[1024] = { 0 };
- std::string strCardID = vtd[i].cid;
- strCardID = strCardID.substr(strCardID.length() - 4, 4);
- snprintf(sql, 1024, "insert into his_location_cell_card_tof_aodi(card_id, loc_time, distances) values('%s', '%s', '%s')",
- strCardID.c_str(),
- time.c_str(),
- strDistances.c_str());
- log_info(sql);
- sDBConnPool.PushAsync(sql);
- }
-
- //tof_data_list.clear();
- //std::vector<tof_data>().swap(tof_data_list);
- tof_data_list_oneAntenna.clear();
- //tof_data_list.resize(0);
- }
- catch (std::exception& e) {
- //log_error("[error] err=%s", e.what());
- log_error("[error] err=%s", e.what());
- }
- }
- void wsTimerThread::send_solid_ball_data()
- {
- if(sb_data_list.empty()){
- return;
- }
-
- try{
- std::lock_guard<std::mutex> lg(m_mtx);
- std::vector<sb_data> vts;
- vts.swap(sb_data_list);
- std::string val = __jsBuilder.build_sb_data(vts);
- swsClientMgr.send(JSON_CMD_VALUE_PUSH, val);
- sb_data_list.clear();
- }catch(std::exception& e){
- log_error("[error] err=%s", e.what());
- }
- }
- // generator 2d tdoa's json data to send to web client
- void wsTimerThread::send_tdoa_2d_data()
- {
- if (tdoa_2d_data_list.empty()) {
- return;
- }
- try {
- std::lock_guard<std::mutex> lg(m_mtx);
- std::vector<tdoa_2d_data> vts;
- vts.swap(tdoa_2d_data_list);
- std::string val = __jsBuilder.build_tdoa_2d_data(vts);
- log_info("sended msg:%s", val.c_str());
- swsClientMgr.send(JSON_CMD_VALUE_PUSH, val);
- tdoa_2d_data_list.clear();
- }
- catch (std::exception& e) {
- log_error("[error] err = %s", e.what());
- }
- }
- // generator 3d tof's json data to send to web client
- void wsTimerThread::send_tof_3d_data()
- {
- if(tof_3d_data_list.empty()){
- return;
- }
- try{
- std::lock_guard<std::mutex> lg(m_mtx);
- std::vector<tof_3d_data> vts;
- vts.swap(tof_3d_data_list);
- std::string val = __jsBuilder.build_tof_3d_data(vts);
- static int n_sended_time = 0;
- if (n_sended_time >= 0)
- {
- log_info("sended msg:%s", val.c_str());
- swsClientMgr.send(JSON_CMD_VALUE_PUSH, val);
- n_sended_time = 0;
- }
- n_sended_time++;
- tof_3d_data_list.clear();
- }
- catch(std::exception& e){
- log_error("[error] err = %s", e.what());
- }
- }
-
- /*
- * 定时器线程发送定位数据
- *
- * */
- void wsTimerThread::_ThreadFunc( wsTimerThread * pOwner )
- {
- //log_info("The timer thread is running");
- while ( pOwner->__Enable )
- {
- std::time_t t = time( 0 );
- //int seconds = (int)std::difftime( t, __LastSendTime );
- double seconds = std::difftime(t, __LastSendTime);
- {
- //pOwner->__SendCardPos();
- //service_position send
- //pOwner->send_card_pos();
- //pOwner->temp_send_card_pos();
- //pOwner->send_light_state();
- //pOwner->send_device_state();
- //pOwner->send_tof_data();
- pOwner->send_tof_data_oneAntenna();
- __LastSendTime = t;
- }
-
- //pOwner->send_solid_ball_data();
- //pOwner->send_tof_3d_data();
- pOwner->send_tdoa_2d_data();
- // 100毫秒发一次数据
- boost::this_thread::sleep( boost::posix_time::millisec( 100 ) );
- }
- pOwner->__ExitCond.notify_one();
- }
- void wsTimerThread::Init( const _THREAD_CONFIG_ & Config )
- {
- __Config = Config;
- __ChkConfig();
- }
- void wsTimerThread::upt_card_pos( const _CARD_POS_ & pos )
- {
- //如果已存在就更新,否则插入
- try{
- uint64_t type = pos.Type;
- uint64_t id=type<<32|pos.ID;
- if ( __CardPosList.seek( id ) )
- {
- __CardPosList.update( id, pos );
- }
- else
- {
- __CardPosList.insert( id, pos );
- }
- }catch(std::exception& e){
- log_error("[error] err=%s", e.what());
- }
- }
- void wsTimerThread::real_upt_card_pos(const _CARD_POS_& pos)
- {
- std::string pos_json = __jsBuilder.build_ios_card_pos(pos);
- ios_service::m_ios_service->notify(pos_json, std::to_string(pos.ID), 10);
- log_info("[service-position] json: %s", pos_json.c_str());
- }
- void wsTimerThread::del_card_pos( const _CARD_POS_ & pos )
- {
- uint64_t type = pos.Type;
- uint64_t id=type<<32|pos.ID;
- __CardPosList.erase( id );
- }
- void wsTimerThread::upt_light_state(const light_state& light)
- {
- light_state_list.push_back(light);
- }
- void wsTimerThread::upt_device_state(const device_state& ds)
- {
- device_state_list.push_back(ds);
- }
- void wsTimerThread::upt_tof_data(const tof_data& td)
- {
- try{
- std::lock_guard<std::mutex> lg(m_mtx);
- tof_data_list.push_back(td);
- }catch(std::exception& e){
- std::string err = e.what();
- log_error("[error] err=%s", err.c_str());
- }
- }
- void wsTimerThread::upt_tof_data_oneAntenna(const std::vector<tof_data_oneAntenna>& vec_td)
- {
- try {
- std::lock_guard<std::mutex> lg(m_mtx);
- for (auto iter = vec_td.begin(); iter != vec_td.end(); iter++)
- {
- tof_data_list_oneAntenna.push_back(*iter);
- }
- }
- catch (std::exception& e) {
- std::string err = e.what();
- log_error("[error] err=%s", err.c_str());
- }
- }
- void wsTimerThread::upt_sb_data(const sb_data& val)
- {
- try{
- std::lock_guard<std::mutex> lg(m_mtx);
- sb_data_list.push_back(val);
- }catch(std::exception& e){
- log_error("[error] err=%s", e.what());
- }
- }
- void wsTimerThread::upt_tdoa_2d_data(const tdoa_2d_data& val)
- {
- try {
- std::lock_guard<std::mutex> lg(m_mtx);
- tdoa_2d_data_list.push_back(val);
- }
- catch (std::exception& e) {
- log_error("[error] err = %s", e.what());
- }
- }
- void wsTimerThread::upt_tof_3d_data(const tof_3d_data& val)
- {
- try{
- std::lock_guard<std::mutex> lg(m_mtx);
- tof_3d_data_list.push_back(val);
- }catch(std::exception& e){
- log_error("[error] err = %s", e.what());
- }
- }
- }
|