You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
9.2 KiB
C++

#include "Subscriber.hpp"
#include <stdexcept>
#include <mutex>
#include <utility>
#include <fastdds/dds/core/status/SubscriptionMatchedStatus.hpp>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/subscriber/DataReader.hpp>
#include <fastdds/dds/subscriber/qos/DataReaderQos.hpp>
#include <fastdds/dds/subscriber/qos/SubscriberQos.hpp>
#include <fastdds/dds/subscriber/SampleInfo.hpp>
#include <fastdds/dds/subscriber/Subscriber.hpp>
#include <QDebug>
#include "msg.hpp"
using namespace eprosima::fastdds::dds;
DdsSubscriberApp::DdsSubscriberApp(const int& domain_id)
: factory_(nullptr)
, participant_(nullptr)
, subscriber_(nullptr)
, weightinfook_topic_(nullptr)
, weightinfook_reader_(nullptr)
, weightinfook_type_(new WeighingSystem::WeightInfoOkPubSubType())
, weightinfoerror_topic_(nullptr)
, weightinfoerror_reader_(nullptr)
, weightinfoerror_type_(new WeighingSystem::WeightInfoErrorPubSubType())
, summary_topic_(nullptr)
, summary_reader_(nullptr)
, summary_type_(new WeighingSystem::SummaryUpdatePubSubType())
, scaleinfo_topic_(nullptr)
, scaleinfo_reader_(nullptr)
, scaleinfo_type_(new WeighingSystem::ScaleInfoPubSubType())
, stop_(false)
{
DomainParticipantQos pqos = PARTICIPANT_QOS_DEFAULT;
pqos.name("WeighUI_sub_participant");
pqos.wire_protocol().builtin.discovery_config.leaseDuration = Duration_t(60, 0);
pqos.wire_protocol().builtin.discovery_config.leaseDuration_announcementperiod = Duration_t(30, 0);
factory_ = DomainParticipantFactory::get_shared_instance();
participant_ = factory_->create_participant(domain_id, pqos, nullptr, StatusMask::none());
if (participant_ == nullptr)
{
throw std::runtime_error("WeighUI Participant initialization failed");
}
weightinfook_type_.register_type(participant_);
qDebug() << "[WeighUI] registered WeightInfoOk type:"
<< weightinfook_type_.get_type_name().c_str();
SubscriberQos sub_qos = SUBSCRIBER_QOS_DEFAULT;
participant_->get_default_subscriber_qos(sub_qos);
subscriber_ = participant_->create_subscriber(sub_qos, nullptr, StatusMask::none());
if (subscriber_ == nullptr)
{
throw std::runtime_error("WeighUI Subscriber initialization failed");
}
TopicQos topic_qos = TOPIC_QOS_DEFAULT;
participant_->get_default_topic_qos(topic_qos);
weightinfook_topic_ = participant_->create_topic("WeightInfoOk", weightinfook_type_.get_type_name(), topic_qos);
if (weightinfook_topic_ == nullptr)
{
throw std::runtime_error("WeighUI::WeightInfoOk Topic initialization failed");
}
DataReaderQos reader_qos = DATAREADER_QOS_DEFAULT;
subscriber_->get_default_datareader_qos(reader_qos);
reader_qos.reliability().kind = ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS;
reader_qos.reliability().max_blocking_time = Duration_t(1, 0);
reader_qos.durability().kind = DurabilityQosPolicyKind::VOLATILE_DURABILITY_QOS;
reader_qos.history().kind = HistoryQosPolicyKind::KEEP_LAST_HISTORY_QOS;
reader_qos.history().depth = 1;
reader_qos.resource_limits().max_samples = 200;
reader_qos.resource_limits().max_instances = 1;
reader_qos.resource_limits().max_samples_per_instance = 100;
reader_qos.data_sharing().off();
weightinfook_reader_ = subscriber_->create_datareader(weightinfook_topic_, reader_qos, this, StatusMask::all());
if (weightinfook_reader_ == nullptr)
{
throw std::runtime_error("WeighUI::WeightInfoOk DataReader initialization failed");
}
// Register WeightInfoError type and create reader (Warning 主题, 弹窗样式与 WeightInfoOk 完全一致)
weightinfoerror_type_.register_type(participant_);
qDebug() << "[WeighUI] registered WeightInfoError type:"
<< weightinfoerror_type_.get_type_name().c_str();
weightinfoerror_topic_ = participant_->create_topic("WeightInfoError", weightinfoerror_type_.get_type_name(), topic_qos);
if (weightinfoerror_topic_ == nullptr)
{
throw std::runtime_error("WeighUI::WeightInfoError Topic initialization failed");
}
weightinfoerror_reader_ = subscriber_->create_datareader(weightinfoerror_topic_, reader_qos, this, StatusMask::all());
if (weightinfoerror_reader_ == nullptr)
{
throw std::runtime_error("WeighUI::WeightInfoError DataReader initialization failed");
}
// Register SummaryUpdate type and create reader
summary_type_.register_type(participant_);
qDebug() << "[WeighUI] registered SummaryUpdate type:"
<< summary_type_.get_type_name().c_str();
summary_topic_ = participant_->create_topic("SummaryUpdate", summary_type_.get_type_name(), topic_qos);
if (summary_topic_ == nullptr)
{
throw std::runtime_error("WeighUI::SummaryUpdate Topic initialization failed");
}
summary_reader_ = subscriber_->create_datareader(summary_topic_, reader_qos, this, StatusMask::all());
if (summary_reader_ == nullptr)
{
throw std::runtime_error("WeighUI::SummaryUpdate DataReader initialization failed");
}
// Register ScaleInfo type and create reader (实时重量)
scaleinfo_type_.register_type(participant_);
qDebug() << "[WeighUI] registered ScaleInfo type:"
<< scaleinfo_type_.get_type_name().c_str();
scaleinfo_topic_ = participant_->create_topic("ScaleInfo", scaleinfo_type_.get_type_name(), topic_qos);
if (scaleinfo_topic_ == nullptr)
{
throw std::runtime_error("WeighUI::ScaleInfo Topic initialization failed");
}
scaleinfo_reader_ = subscriber_->create_datareader(scaleinfo_topic_, reader_qos, this, StatusMask::all());
if (scaleinfo_reader_ == nullptr)
{
throw std::runtime_error("WeighUI::ScaleInfo DataReader initialization failed");
}
}
DdsSubscriberApp::~DdsSubscriberApp()
{
if (nullptr != participant_)
{
participant_->delete_contained_entities();
factory_->delete_participant(participant_);
}
}
void DdsSubscriberApp::on_subscription_matched(
DataReader* reader,
const SubscriptionMatchedStatus& info)
{
std::string topic = reader->get_topicdescription()->get_name();
qDebug() << "[WeighUI] SubscriptionMatched topic=" << topic.c_str()
<< " current_count=" << info.current_count
<< " change=" << info.current_count_change
<< " total_count=" << info.total_count;
}
void DdsSubscriberApp::on_data_available(DataReader* reader)
{
SampleInfo info;
std::string topic_name = reader->get_topicdescription()->get_name();
qDebug() << "[WeighUI] on_data_available topic=" << topic_name.c_str();
if (topic_name == "WeightInfoOk")
{
WeighingSystem::WeightInfoOk sample_;
while ((!is_stopped()) && (RETCODE_OK == reader->take_next_sample(&sample_, &info)))
{
if ((info.instance_state == ALIVE_INSTANCE_STATE) && info.valid_data)
{
std::unique_lock<std::mutex> lock(DdsMsgData::queue_cv_mtx_);
DdsMsgData::WeightInfoOk_queue_.push(std::move(sample_));
lock.unlock();
}
}
}
else if (topic_name == "WeightInfoError")
{
WeighingSystem::WeightInfoError sample_;
while ((!is_stopped()) && (RETCODE_OK == reader->take_next_sample(&sample_, &info)))
{
if ((info.instance_state == ALIVE_INSTANCE_STATE) && info.valid_data)
{
std::unique_lock<std::mutex> lock(DdsMsgData::queue_cv_mtx_);
DdsMsgData::WeightInfoError_queue_.push(std::move(sample_));
lock.unlock();
}
}
}
else if (topic_name == "SummaryUpdate")
{
WeighingSystem::SummaryUpdate sample_;
while ((!is_stopped()) && (RETCODE_OK == reader->take_next_sample(&sample_, &info)))
{
if ((info.instance_state == ALIVE_INSTANCE_STATE) && info.valid_data)
{
std::unique_lock<std::mutex> lock(DdsMsgData::queue_cv_mtx_);
DdsMsgData::SummaryUpdate_queue_.push(std::move(sample_));
lock.unlock();
}
}
}
else if (topic_name == "ScaleInfo")
{
WeighingSystem::ScaleInfo sample_;
while ((!is_stopped()) && (RETCODE_OK == reader->take_next_sample(&sample_, &info)))
{
if ((info.instance_state == ALIVE_INSTANCE_STATE) && info.valid_data)
{
std::unique_lock<std::mutex> lock(DdsMsgData::queue_cv_mtx_);
DdsMsgData::ScaleInfo_queue_.push(std::move(sample_));
lock.unlock();
}
}
}
}
void DdsSubscriberApp::run()
{
while (!is_stopped())
{
std::unique_lock<std::mutex> period_lock(terminate_cv_mtx_);
terminate_cv_.wait_for(period_lock, std::chrono::milliseconds(period_ms_),
[this]() { return is_stopped(); });
}
}
bool DdsSubscriberApp::is_stopped()
{
return stop_.load();
}
void DdsSubscriberApp::stop()
{
stop_.store(true);
terminate_cv_.notify_all();
}