#ifndef __UT_ROBOT_SDK_CHANNEL_SUBSCRIBER_HPP__ #define __UT_ROBOT_SDK_CHANNEL_SUBSCRIBER_HPP__ #include namespace unitree { namespace robot { template class ChannelSubscriber { public: explicit ChannelSubscriber(const std::string& channelName) : mChannelName(channelName), mQueueLen(0) {} explicit ChannelSubscriber(const std::string& channelName, const std::function& handler, int64_t queuelen = 0) : mChannelName(channelName), mQueueLen(queuelen), mHandler(handler) {} void InitChannel(const std::function& handler, int64_t queuelen = 0) { mHandler = handler; mQueueLen = queuelen; InitChannel(); } void InitChannel() { if (mHandler) { mChannelPtr = ChannelFactory::Instance()->CreateRecvChannel(mChannelName, mHandler, mQueueLen); } else { UT_THROW(common::CommonException, "subscribe handler is invalid"); } } void CloseChannel() { mChannelPtr.reset(); } int64_t GetLastDataAvailableTime() const { if (mChannelPtr) { return mChannelPtr->GetLastDataAvailableTime(); } return -1; } const std::string& GetChannelName() const { return mChannelName; } private: std::string mChannelName; int64_t mQueueLen; std::function mHandler; ChannelPtr mChannelPtr; }; template using ChannelSubscriberPtr = std::shared_ptr>; } } #endif//__UT_ROBOT_SDK_CHANNEL_SUBSCRIBER_HPP__