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 a8845022 authored by Georg Vinogradov's avatar Georg Vinogradov
Browse files

Create Mqtt adapter

parent 83be86d6
Pipeline #147457 passed with stage
in 1 minute and 41 seconds
# Created by Georg Vinogradov on 28.05.19
# Setting cmake version
cmake_minimum_required(VERSION 3.1...3.14)
if(${CMAKE_VERSION} VERSION_LESS 3.12)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()
# Using C++ version 11 with threads for compiling
set (CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
# Setting search paths for cmake modules
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/modules")
# Setting project name and description
project(MqttAdapter VERSION 1.0
DESCRIPTION "MQTT adapter"
LANGUAGES CXX)
# Find mqtt packages using modules
find_package(MQTT REQUIRED)
# Adding library as a target
add_library(MqttAdapter_tests_a_compA MqttAdapter_tests_a_compA.cpp)
# Checking if everything was found
if (NOT MQTT_C_LIB OR NOT MQTT_A_LIB OR NOT MQTT_PP_LIB)
message(FATAL_ERROR "MQTT libraries not found!")
elseif (NOT MQTT_INCLUDE_DIR)
message(FATAL_ERROR "MQTT includes not found!")
else()
message("MQTT includes found in "${MQTT_INCLUDE_DIR})
message("MQTT libraries found in "${MQTT_LIBS})
endif()
# Adding include directory to a target
target_include_directories(MqttAdapter_tests_a_compA PUBLIC ${MQTT_INCLUDE_DIR})
# Linking libraries to target
target_link_libraries(MqttAdapter_tests_a_compA PUBLIC ${MQTT_LIBS})
# Export target to a cmake module file for outside usage
export(TARGETS MqttAdapter_tests_a_compA FILE MqttAdapter_tests_a_compA.cmake)
// 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() {}
void MqttAdapter_tests_a_compA::init(tests_a_compA *comp)
{
this->component = comp;
mqtt::connect_options connOpts;
connOpts.set_keep_alive_interval(20);
connOpts.set_clean_session(true);
_clockSubscriber = new mqtt::client(SERVER_ADDRESS, SUB_ID);
_echoPublisher = new mqtt::client(SERVER_ADDRESS, PUB_ID);
try {
_clockSubscriber->connect(connOpts);
callback cb(*_clockSubscriber);
_clockSubscriber->set_callback(cb);
_clockSubscriber->subscribe(TOPIC, 1);
} catch (const mqtt::exception& exc) {
cerr << exc.what() << endl;
}
}
void MqttAdapter_tests_a_compA::publish_echoPublisher()
{
mqtt::message tmpMsg;
tmpMsg.set_payload(std::to_string(component->rosOut));
try {
_echoPublisher->publish(tmpMsg);
} catch (const mqtt::exception& exc) {
cerr << exc.what() << endl;
}
}
void MqttAdapter_tests_a_compA::tick()
{
publish_echoPublisher();
}
// MqttAdapter
//
// Created by Georg Vinogradov on 05.06.19.
// Copyright © 2019 Georg Vinogradov. All rights reserved.
//
#pragma once
#include "tests_a_compA.h"
#include "mqtt/client.h"
#include <iostream>
using namespace std;
class MqttAdapter_tests_a_compA {
public:
MqttAdapter_tests_a_compA();
void init(tests_a_compA* comp);
void publish_echoPublisher();
void tick();
private:
const string SERVER_ADDRESS = "tcp://localhost:1883";
const string PUB_ID = "publisher_cpp";
const string SUB_ID = "subscriber_cpp";
const string TOPIC = "/clock";
tests_a_compA* component = 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) {}
};
# Created by Georg Vinogradov on 28.05.19
message("Searching for installed MQTT on your system...")
# Searching for include directory
if (NOT MQTT_INCLUDE_DIR)
find_path(MQTT_INCLUDE_DIR MQTTAsync.h HINTS "/usr/include/" "/usr/local/include/")
endif()
# Searching for libraries (without SSL support)
if (NOT MQTT_LIBS)
find_library(
MQTT_C_LIB
NAMES paho-mqtt3c)
find_library(
MQTT_A_LIB
NAMES paho-mqtt3a)
find_library(
MQTT_PP_LIB
NAMES paho-mqttpp3)
set(MQTT_LIBS ${MQTT_A_LIB} ${MQTT_C_LIB} ${MQTT_PP_LIB})
endif()
// MqttAdapter
//
// Created by Georg Vinogradov on 05.06.19.
// Copyright © 2019 Georg Vinogradov. All rights reserved.
//
#ifndef tests_a_compA_h
#define tests_a_compA_h
#include <iostream>
using namespace std;
class tests_a_compA {
public:
double rosIn = 0;
double rosOut = 0;
};
#endif /* tests_a_compA_h */
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