Aufgrund einer Störung des s3 Storage, könnten in nächster Zeit folgende GitLab Funktionen nicht zur Verfügung stehen: LFS, Container Registry, Job Artifacs, Uploads (Wiki, Bilder, Projekt-Exporte). Wir bitten um Verständnis. Es wird mit Hochdruck an der Behebung des Problems gearbeitet. Weitere Informationen zur Störung des Object Storage finden Sie hier: https://maintenance.itc.rwth-aachen.de/ticket/status/messages/59-object-storage-pilot

Commit a1c7be0f authored by Georg Vinogradov's avatar Georg Vinogradov
Browse files

Change callback to set values of component on message received call and...

Change callback to set values of component on message received call and encapsulate callback in a separate class
parent c7a8a7f7
Pipeline #150950 passed with stage
in 53 seconds
#include "Callback.hpp"
Callback::Callback(mqtt::client& cli, tests_a_compA* comp) : mqtt::callback(), cli_(cli)
{
comp_ = comp;
}
// Callback for when connected to broker
void Callback::connected(const std::string& cause)
{
cout << "Connected" <<endl;
}
// Callback for when the connection is lost.
void Callback::connection_lost(const std::string& cause)
{
cout << "\nConnection lost";
if (!cause.empty())
cout << ": " << cause << endl;
}
// Callback for when message is received
void Callback::message_arrived(mqtt::const_message_ptr msg)
{
cout << "Message received "<< msg->get_topic() << ": " << msg->get_payload_str() << endl;
std::string::size_type sz;
double value = std::stod (msg->get_payload_str(),&sz);
comp_->rosIn = value;
}
#ifndef Callback_hpp
#define Callback_hpp
#include "tests_a_compA.h"
#include "mqtt/client.h"
using namespace std;
class Callback : public virtual mqtt::callback
{
mqtt::client& cli_;
public:
Callback(mqtt::client& cli, tests_a_compA* comp);
void connected(const std::string& cause);
void connection_lost(const std::string& cause);
void message_arrived(mqtt::const_message_ptr msg);
private:
tests_a_compA* comp_;
};
#endif /* Callback_hpp */
// MqttAdapter
//
// Created by Georg Vinogradov on 05.06.19.
// Copyright © 2019 Georg Vinogradov. All rights reserved.
//
#include "MqttAdapter_tests_a_compA.h"
MqttAdapter_tests_a_compA::MqttAdapter_tests_a_compA() {}
using namespace std;
MqttAdapter_tests_a_compA::MqttAdapter_tests_a_compA()
{
}
void MqttAdapter_tests_a_compA::init(tests_a_compA *comp)
{
// Initialize component
this->component = comp;
// Initialize connect options
mqtt::connect_options connOpts;
connOpts.set_keep_alive_interval(20);
connOpts.set_clean_session(true);
// Intitialize callback, subscriber and publisher
_clockSubscriber = new mqtt::client(SERVER_ADDRESS, SUB_ID);
_callback = new Callback(*_clockSubscriber, comp);
_echoPublisher = new mqtt::client(SERVER_ADDRESS, PUB_ID);
// Connect subscriber, publisher and subscribe to the topic
try {
_clockSubscriber->set_callback(*_callback);
_clockSubscriber->connect(connOpts);
callback cb(*_clockSubscriber);
_clockSubscriber->set_callback(cb);
_echoPublisher->connect(connOpts);
_clockSubscriber->subscribe(TOPIC, 1);
} catch (const mqtt::exception& exc) {
cerr << exc.what() << endl;
}
while(true);
}
void MqttAdapter_tests_a_compA::publish_echoPublisher()
{
mqtt::message tmpMsg;
double value = component->rosOut;
uint8_t *buf = (uint8_t*)&value;
tmpMsg.set_payload(buf, 8); // pointer to byte buffer of length 8
string value = to_string(component->rosOut);
auto pubmsg = mqtt::make_message(TOPIC, value);
try {
_echoPublisher->publish(tmpMsg);
_echoPublisher->publish(pubmsg);
} catch (const mqtt::exception& exc) {
cerr << exc.what() << endl;
cerr << exc.to_string() << endl;
}
}
......
// MqttAdapter
//
// Created by Georg Vinogradov on 05.06.19.
// Copyright © 2019 Georg Vinogradov. All rights reserved.
//
#pragma once
#include "tests_a_compA.h"
#include "Callback.hpp"
#include "mqtt/client.h"
#include <iostream>
......@@ -30,29 +25,7 @@ private:
const string TOPIC = "/clock";
tests_a_compA* component = nullptr;
Callback* _callback = nullptr;
mqtt::client* _clockSubscriber = nullptr;
mqtt::client* _echoPublisher = nullptr;
};
class callback : public virtual mqtt::callback
{
mqtt::client& cli_;
// Callback for when connected
void connected(const std::string& cause) {}
// Callback for when the connection is lost.
void connection_lost(const std::string& cause) {
cout << "\nConnection lost";
if (!cause.empty())
cout << ": " << cause << endl;
}
// Callback for when message is received
void message_arrived(mqtt::const_message_ptr msg) {
cout << "Message received "<< msg->get_topic() << ": " << msg->get_payload_str() << endl;
}
public:
callback(mqtt::client& cli) : cli_(cli) {}
};
#include <stdio.h>
#include "tests_a_compA.h"
tests_a_compA::tests_a_compA(double value)
{
rosOut = value;
}
// MqttAdapter
//
// Created by Georg Vinogradov on 05.06.19.
// Copyright © 2019 Georg Vinogradov. All rights reserved.
//
#pragma once
#ifndef tests_a_compA_h
#define tests_a_compA_h
......@@ -14,6 +10,7 @@ using namespace std;
class tests_a_compA {
public:
tests_a_compA(double value);
double rosIn = 0;
double rosOut = 0;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment