123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- #include "wsTimerThread.h"
- #include <iostream>
- #include "wsClientMgr.h"
- #include "constdef.h"
- #include "log.h"
- #include "sys_setting.h"
- namespace YA
- {
- 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_> CardPosList;
- if(__CardPosList.empty())
- return;
- __CardPosList.copy( CardPosList );
- std::string jsCardPos = __jsBuilder.BuildCardPos( CardPosList );
- 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_test] 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.erase(light_state_list.begin(), light_state_list.end());
- }
- /*
- * 定时器线程发送定位数据
- *
- * */
- 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 );
- if ( seconds >= pOwner->__Config.SendInterval )
- {
- pOwner->__SendCardPos();
- //service_position send
- //pOwner->send_card_pos();
- //pOwner->temp_send_card_pos();
- pOwner->send_light_state();
- __LastSendTime = t;
- }
- boost::this_thread::sleep( boost::posix_time::millisec( 1 ) );
- }
- pOwner->__ExitCond.notify_one();
- }
- void wsTimerThread::Init( const _THREAD_CONFIG_ & Config )
- {
- __Config = Config;
- __ChkConfig();
- }
- void wsTimerThread::upt_card_pos( const _CARD_POS_ & pos )
- {
- //如果已存在就更新,否则插入
- 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 );
- }
- }
- 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);
- }
- }
|