// 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 #include #include #include #include #include #include #include #include #include #include "SystemPubSubTypes.hpp" #include "json.hpp" #include "DEBUG.hpp" using json = nlohmann::json; using namespace eprosima::fastdds::dds; PublisherApp::PublisherApp( const int& domain_id) : factory_(nullptr) , participant_(nullptr) , publisher_(nullptr) , topic_(nullptr) , writer_(nullptr) , type_(new WeighingSystem::LicenseSnapUpdatePubSubType()) , matched_(0) , samples_sent_(0) , stop_(false) { // // Create the participant DomainParticipantQos pqos = PARTICIPANT_QOS_DEFAULT; pqos.name("HttpServer_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("HttpServerPub Participant initialization failed"); } // Register the type 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("HttpServer Publisher initialization failed"); } // Create the topic TopicQos topic_qos = TOPIC_QOS_DEFAULT; participant_->get_default_topic_qos(topic_qos); topic_ = participant_->create_topic("LicenseSnapUpdate", type_.get_type_name(), topic_qos); if (topic_ == nullptr) { throw std::runtime_error("WeighingSystem::LicenseSnapUpdate 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::VOLATILE_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(); writer_ = publisher_->create_datawriter(topic_, writer_qos, this, StatusMask::all()); if (writer_ == nullptr) { throw std::runtime_error("WeighingSystem::LicenseSnapUpdate 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 lock(mutex_); matched_ = info.current_count; } DEBUG(writer->get_topic()->get_name() << " Publisher matched."); cv_.notify_one(); } else if (info.current_count_change == -1) { { std::lock_guard lock(mutex_); matched_ = info.current_count; } DEBUG(writer->get_topic()->get_name() << " Publisher unmatched."); } else { DEBUG(info.current_count_change << " is not a valid value for PublicationMatchedStatus current count change"); } } void PublisherApp::run(std::shared_ptr handler) { std::string old_License = ""; while (!is_stopped()) { handler->run(); if (handler->PostMsg.empty() != true) { std::cout << "[HTTPServer] 原始数据: " << handler->PostMsg << std::endl; json j = json::parse(handler->PostMsg); DEBUG("车牌: " << j["AlarmInfoPlate"]["result"]["PlateResult"]["license"]); WeighingSystem::LicenseSnapUpdate info; info.License() = j["AlarmInfoPlate"]["result"]["PlateResult"]["license"]; info.NewTag() = 1; if (info.License() == old_License) { info.NewTag() = 2; } old_License = info.License(); writer_->write(&info); handler->PostMsg.clear(); } // Wait for period or stop event std::unique_lock 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 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::LicenseSnapUpdate sample_; ret = (RETCODE_OK == writer_->write(&sample_)); } return ret; } bool PublisherApp::is_stopped() { return stop_.load(); } void PublisherApp::stop() { stop_.store(true); cv_.notify_one(); }