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.

298 lines
11 KiB
C++

// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*!
* @file Publisher.cxx
* This file contains the implementation of the publisher functions.
*
* This file was generated by the tool fastddsgen.
*/
#include "Publisher.hpp"
#include <condition_variable>
#include <csignal>
#include <stdexcept>
#include <thread>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/log/Log.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
#include "SystemPubSubTypes.hpp"
#include "msg.hpp"
#include "MsgHandler.hpp"
using namespace eprosima::fastdds::dds;
PublisherApp::PublisherApp(
const int& domain_id)
: factory_(nullptr)
, participant_(nullptr)
, publisher_(nullptr)
, bar_topic_(nullptr)
, bar_writer_(nullptr)
, bar_type_(new WeighingSystem::BarUpdatePubSubType())
, light_topic_(nullptr)
, light_writer_(nullptr)
, light_type_(new WeighingSystem::LightsUpdatePubSubType())
, infrared_topic_(nullptr)
, infrared_writer_(nullptr)
, infrared_type_(new WeighingSystem::LightsUpdatePubSubType())
, matched_(0)
, samples_sent_(0)
, stop_(false)
{
//
// Create the participant
DomainParticipantQos pqos = PARTICIPANT_QOS_DEFAULT;
pqos.name("WeighingSystem_pub_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("WeighingSystem Participant initialization failed");
}
// Register the type
bar_type_.register_type(participant_);
light_type_.register_type(participant_);
infrared_type_.register_type(participant_);
// Create the publisher
PublisherQos pub_qos = PUBLISHER_QOS_DEFAULT;
participant_->get_default_publisher_qos(pub_qos);
publisher_ = participant_->create_publisher(pub_qos, nullptr, StatusMask::none());
if (publisher_ == nullptr)
{
throw std::runtime_error("WeighingSystem Publisher initialization failed");
}
// Create the topic
TopicQos topic_qos = TOPIC_QOS_DEFAULT;
participant_->get_default_topic_qos(topic_qos);
bar_topic_ = participant_->create_topic("BarUpdate", bar_type_.get_type_name(), topic_qos);
if (bar_topic_ == nullptr)
{
throw std::runtime_error("BarUpdate Topic initialization failed");
}
// Create the data writer
DataWriterQos writer_qos = DATAWRITER_QOS_DEFAULT;
publisher_->get_default_datawriter_qos(writer_qos);
writer_qos.reliability().kind = ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS;
writer_qos.reliability().max_blocking_time = Duration_t(1, 0);
writer_qos.durability().kind = DurabilityQosPolicyKind::TRANSIENT_LOCAL_DURABILITY_QOS;
writer_qos.history().kind = HistoryQosPolicyKind::KEEP_LAST_HISTORY_QOS;
writer_qos.history().depth = 1;
writer_qos.resource_limits().max_samples = 200;
writer_qos.resource_limits().max_instances = 1;
writer_qos.resource_limits().max_samples_per_instance = 100;
writer_qos.data_sharing().off();
bar_writer_ = publisher_->create_datawriter(bar_topic_, writer_qos, this, StatusMask::all());
if (bar_writer_ == nullptr)
{
throw std::runtime_error("WeighingSystem::BarUpdate DataWriter initialization failed");
}
// Create the topic
topic_qos = TOPIC_QOS_DEFAULT;
participant_->get_default_topic_qos(topic_qos);
light_topic_ = participant_->create_topic("LightsUpdate", light_type_.get_type_name(), topic_qos);
if (light_topic_ == nullptr)
{
throw std::runtime_error("LightsUpdate Topic initialization failed");
}
// Create the data writer
writer_qos = DATAWRITER_QOS_DEFAULT;
publisher_->get_default_datawriter_qos(writer_qos);
writer_qos.reliability().kind = ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS;
writer_qos.reliability().max_blocking_time = Duration_t(1, 0);
writer_qos.durability().kind = DurabilityQosPolicyKind::TRANSIENT_LOCAL_DURABILITY_QOS;
writer_qos.history().kind = HistoryQosPolicyKind::KEEP_LAST_HISTORY_QOS;
writer_qos.history().depth = 1;
writer_qos.resource_limits().max_samples = 200;
writer_qos.resource_limits().max_instances = 1;
writer_qos.resource_limits().max_samples_per_instance = 100;
writer_qos.data_sharing().off();
light_writer_ = publisher_->create_datawriter(light_topic_, writer_qos, this, StatusMask::all());
if (light_writer_ == nullptr)
{
throw std::runtime_error("WeighingSystem::LightsUpdate DataWriter initialization failed");
}
// Create the topic
topic_qos = TOPIC_QOS_DEFAULT;
participant_->get_default_topic_qos(topic_qos);
infrared_topic_ = participant_->create_topic("InfraredUpdate", infrared_type_.get_type_name(), topic_qos);
if (infrared_topic_ == nullptr)
{
throw std::runtime_error("InfraredUpdate Topic initialization failed");
}
// Create the data writer
writer_qos = DATAWRITER_QOS_DEFAULT;
publisher_->get_default_datawriter_qos(writer_qos);
writer_qos.reliability().kind = ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS;
writer_qos.reliability().max_blocking_time = Duration_t(1, 0);
writer_qos.durability().kind = DurabilityQosPolicyKind::TRANSIENT_LOCAL_DURABILITY_QOS;
writer_qos.history().kind = HistoryQosPolicyKind::KEEP_LAST_HISTORY_QOS;
writer_qos.history().depth = 1;
writer_qos.resource_limits().max_samples = 200;
writer_qos.resource_limits().max_instances = 1;
writer_qos.resource_limits().max_samples_per_instance = 100;
writer_qos.data_sharing().off();
infrared_writer_ = publisher_->create_datawriter(infrared_topic_, writer_qos, this, StatusMask::all());
if (infrared_writer_ == nullptr)
{
throw std::runtime_error("WeighingSystem::InfraredUpdate DataWriter initialization failed");
}
}
PublisherApp::~PublisherApp()
{
if (nullptr != participant_)
{
// Delete DDS entities contained within the DomainParticipant
participant_->delete_contained_entities();
// Delete DomainParticipant
factory_->delete_participant(participant_);
}
}
void PublisherApp::on_publication_matched(
DataWriter* writer,
const PublicationMatchedStatus& info)
{
if (info.current_count_change == 1)
{
{
std::lock_guard<std::mutex> lock(mutex_);
matched_ = info.current_count;
}
std::cout << writer->get_topic()->get_name() << " Publisher matched." << std::endl;
cv_.notify_one();
}
else if (info.current_count_change == -1)
{
{
std::lock_guard<std::mutex> lock(mutex_);
matched_ = info.current_count;
}
std::cout << writer->get_topic()->get_name() << " Publisher unmatched." << std::endl;
}
else
{
std::cout << info.current_count_change
<< " is not a valid value for PublicationMatchedStatus current count change" << std::endl;
}
}
void PublisherApp::run(std::shared_ptr<MsgHandler> handler)
{
while (!is_stopped())
{
if(handler->isOpen() == true)
{
if(handler->HandleDeviceMsg())
{
// if ((handler->old_bar_state.FrontResistanceSignal != handler->new_bar_state.FrontResistanceSignal) ||
// (handler->old_bar_state.BackResistanceSignal != handler->new_bar_state.BackResistanceSignal))
// {
WeighingSystem::InfraredUpdate info;
info.FrontResistanceSignal() = handler->new_bar_state.FrontResistanceSignal;
info.BackResistanceSignal() = handler->new_bar_state.BackResistanceSignal;
handler->old_bar_state.FrontResistanceSignal = handler->new_bar_state.FrontResistanceSignal;
handler->old_bar_state.BackResistanceSignal = handler->new_bar_state.BackResistanceSignal;
infrared_writer_->write(&info);
// }
}
}
if ((handler->old_bar_state.FrontBarState != handler->new_bar_state.FrontBarState) || \
(handler->old_bar_state.BackBarState != handler->new_bar_state.BackBarState)
)
{
WeighingSystem::BarUpdate info;
info.FrontBarState() = handler->new_bar_state.FrontBarState;
info.BackBarState() = handler->new_bar_state.BackBarState;
handler->old_bar_state.FrontBarState = handler->new_bar_state.FrontBarState;
handler->old_bar_state.BackBarState = handler->new_bar_state.BackBarState;
bar_writer_->write(&info);
}
if ((handler->old_bar_state.FrontLEDState != handler->new_bar_state.FrontLEDState) || \
(handler->old_bar_state.BackLEDState != handler->new_bar_state.BackLEDState)
)
{
WeighingSystem::LightsUpdate info;
info.FrontLEDState() = handler->new_bar_state.FrontLEDState;
info.BackLEDState() = handler->new_bar_state.BackLEDState;
handler->old_bar_state.FrontLEDState = handler->new_bar_state.FrontLEDState;
handler->old_bar_state.BackLEDState = handler->new_bar_state.BackLEDState;
light_writer_->write(&info);
}
// Wait for period or stop event
std::unique_lock<std::mutex> period_lock(mutex_);
cv_.wait_for(period_lock, std::chrono::milliseconds(period_ms_), [this]()
{
return is_stopped();
});
}
}
bool PublisherApp::publish()
{
bool ret = false;
// Wait for the data endpoints discovery
std::unique_lock<std::mutex> matched_lock(mutex_);
cv_.wait(matched_lock, [&]()
{
// at least one has been discovered
return ((matched_ > 0) || is_stopped());
});
if (!is_stopped())
{
/* Initialize your structure here */
WeighingSystem::BarUpdate sample_;
ret = (RETCODE_OK == bar_writer_->write(&sample_));
}
return ret;
}
bool PublisherApp::is_stopped()
{
return stop_.load();
}
void PublisherApp::stop()
{
stop_.store(true);
cv_.notify_one();
}