Commit 89b91f30 authored by Alexander David Hellwig's avatar Alexander David Hellwig
Browse files

Merge branch '18-add-deploy-job-to-gitlab-ci' into 'master'

Issue 18

See merge request !2
parents 56d8c77a 8f147d9a
Pipeline #165540 passed with stages
in 59 seconds
...@@ -4,4 +4,5 @@ ...@@ -4,4 +4,5 @@
.project .project
.classpath .classpath
.DS_Store .DS_Store
.idea .idea
\ No newline at end of file .iml
stages: stages:
- linux - project
- adapter
- deploy
BranchJobLinux: BranchJobDeploy:
stage: linux stage: deploy
image: registry.git.rwth-aachen.de/monticore/embeddedmontiarc/generators/emam2mqtt image: registry.git.rwth-aachen.de/monticore/embeddedmontiarc/generators/emam2mqtt
script: script:
- mvn clean install -s settings.xml - mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B clean deploy --settings settings.xml
- cat target/site/jacoco/index.html
only:
- master
BranchJobProject:
stage: project
image: registry.git.rwth-aachen.de/monticore/embeddedmontiarc/generators/emam2mqtt
script:
- mvn clean install -s settings.xml
except:
- master
BranchJobAdapter:
stage: adapter
image: registry.git.rwth-aachen.de/monticore/embeddedmontiarc/generators/emam2mqtt
script:
- cd mqtt_adapter
- cmake .
- make
except: except:
- master - master
...@@ -8,7 +8,7 @@ These instructions will get MQTT up and running on your **Ubuntu** system ...@@ -8,7 +8,7 @@ These instructions will get MQTT up and running on your **Ubuntu** system
### Installing project ### Installing project
[Download](https://git.rwth-aachen.de/monticore/EmbeddedMontiArc/generators/emam2mqtt/-/archive/8-handwrite-a-mqtt-adapter/emam2mqtt-3-install-mqtt-linux.zip) this project to your computer Download this project to your computer
[Download](https://maven.apache.org/guides/getting-started/) and install Maven [Download](https://maven.apache.org/guides/getting-started/) and install Maven
...@@ -72,6 +72,17 @@ cd paho.mqtt.cpp ...@@ -72,6 +72,17 @@ cd paho.mqtt.cpp
cmake -Bbuild -H. cmake -Bbuild -H.
sudo cmake --build build/ --target install sudo cmake --build build/ --target install
``` ```
#### Set environment variables for MQTT
additionaly, you can set the environment variables for MQTT libs and includes directory by modifying the environment file on your system
```bash
sudo nano /etc/environment
```
in the file, add a new line and write
```bash
MQTT_LIBS = "PATH/to/libs"
MQTT_INCLUDE_DIR = "PATH/to/includes"
```
## Installing MQTT Publisher/Subscriber demo (locally on Ubuntu) ## Installing MQTT Publisher/Subscriber demo (locally on Ubuntu)
open terminal, switch to **mqtt_demo** directroy inside the project and run open terminal, switch to **mqtt_demo** directroy inside the project and run
`cmake .` `cmake .`
......
FROM ubuntu:latest FROM ubuntu:latest
RUN apt-get update
RUN apt-get -y install software-properties-common RUN apt-get update && apt-get -y install \
RUN apt-add-repository ppa:mosquitto-dev/mosquitto-ppa mosquitto \
RUN add-apt-repository ppa:webupd8team/java mosquitto-clients \
RUN apt-get update openjdk-8-jdk
RUN apt-get -y install mosquitto
RUN apt-get -y install mosquitto-clients
RUN apt-get -y install openjdk-8-jdk
ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
RUN apt -y install maven RUN apt -y install \
maven \
build-essential \
cmake \
git
RUN git clone https://github.com/eclipse/paho.mqtt.c.git ; cd paho.mqtt.c ; git checkout v1.2.1 ; cmake -Bbuild -H. -DPAHO_WITH_SSL=OFF ; cmake --build build/ --target install ; ldconfig
RUN git clone https://github.com/eclipse/paho.mqtt.cpp ; cd paho.mqtt.cpp ; cmake -Bbuild -H. -DPAHO_WITH_SSL=OFF ; cmake --build build/ --target install
RUN rm -rf paho.mqtt.c paho.mqtt.cpp
# Created by Georg Vinogradov on 28.05.19
# Setting cmake version # Setting cmake version
cmake_minimum_required(VERSION 3.1...3.14) cmake_minimum_required(VERSION 3.1...3.14)
...@@ -18,20 +17,38 @@ project(MqttAdapter VERSION 1.0 ...@@ -18,20 +17,38 @@ project(MqttAdapter VERSION 1.0
DESCRIPTION "MQTT adapter" DESCRIPTION "MQTT adapter"
LANGUAGES CXX) LANGUAGES CXX)
# Find mqtt packages using modules set(SEARCH_MQTT FALSE)
find_package(MQTT REQUIRED)
# Check is environment variable was set
IF(DEFINED ENV{MQTT_INCLUDE_DIR} AND DEFINED ENV{MQTT_LIBS})
message("...MQTT environment variables are set" )
ELSE()
message("MQTT environment variables are not defined.")
# Find mqtt packages using modules
find_package(MQTT REQUIRED)
SET(SEARCH_MQTT TRUE)
ENDIF()
# Adding library as a target # Adding library as a target
add_library(MqttAdapter_tests_a_compA MqttAdapter_tests_a_compA.cpp) add_library(MqttAdapter_tests_a_compA
Callback.cpp
MqttAdapter_tests_a_compA.cpp
tests_a_compA.cpp
Callback.hpp
MqttAdapter_tests_a_compA.h
tests_a_compA.h
)
# Checking if everything was found # Checking if everything was found
if (NOT MQTT_C_LIB OR NOT MQTT_A_LIB OR NOT MQTT_PP_LIB) if(SEARCH_MQTT)
message(FATAL_ERROR "MQTT libraries not found!") if (NOT MQTT_C_LIB OR NOT MQTT_A_LIB OR NOT MQTT_PP_LIB)
elseif (NOT MQTT_INCLUDE_DIR) message(FATAL_ERROR "MQTT libraries not found!")
message(FATAL_ERROR "MQTT includes not found!") elseif (NOT MQTT_INCLUDE_DIR)
else() message(FATAL_ERROR "MQTT includes not found!")
message("MQTT includes found in "${MQTT_INCLUDE_DIR}) else()
message("MQTT libraries found in "${MQTT_LIBS}) message("MQTT includes found in " ${MQTT_INCLUDE_DIR})
message("MQTT libraries found in " ${MQTT_LIBS})
endif()
endif() endif()
# Adding include directory to a target # Adding include directory to a target
......
#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_->mqttIn = 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" #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) void MqttAdapter_tests_a_compA::init(tests_a_compA *comp)
{ {
// Initialize component
this->component = comp; this->component = comp;
// Initialize connect options
mqtt::connect_options connOpts; mqtt::connect_options connOpts;
connOpts.set_keep_alive_interval(20); connOpts.set_keep_alive_interval(20);
connOpts.set_clean_session(true); connOpts.set_clean_session(true);
// Intitialize callback, subscriber and publisher
_clockSubscriber = new mqtt::client(SERVER_ADDRESS, SUB_ID); _clockSubscriber = new mqtt::client(SERVER_ADDRESS, SUB_ID);
_callback = new Callback(*_clockSubscriber, comp);
_echoPublisher = new mqtt::client(SERVER_ADDRESS, PUB_ID); _echoPublisher = new mqtt::client(SERVER_ADDRESS, PUB_ID);
// Connect subscriber, publisher and subscribe to the topic
try { try {
_clockSubscriber->set_callback(*_callback);
_clockSubscriber->connect(connOpts); _clockSubscriber->connect(connOpts);
callback cb(*_clockSubscriber); _echoPublisher->connect(connOpts);
_clockSubscriber->set_callback(cb);
_clockSubscriber->subscribe(TOPIC, 1); _clockSubscriber->subscribe(TOPIC, 1);
} catch (const mqtt::exception& exc) { } catch (const mqtt::exception& exc) {
...@@ -33,18 +37,15 @@ void MqttAdapter_tests_a_compA::init(tests_a_compA *comp) ...@@ -33,18 +37,15 @@ void MqttAdapter_tests_a_compA::init(tests_a_compA *comp)
void MqttAdapter_tests_a_compA::publish_echoPublisher() 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->mqttOut);
auto pubmsg = mqtt::make_message(TOPIC, value);
try { try {
_echoPublisher->publish(tmpMsg); _echoPublisher->publish(pubmsg);
} catch (const mqtt::exception& exc) { } 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 #pragma once
#include "tests_a_compA.h" #include "tests_a_compA.h"
#include "Callback.hpp"
#include "mqtt/client.h" #include "mqtt/client.h"
#include <iostream> #include <iostream>
...@@ -30,29 +25,7 @@ private: ...@@ -30,29 +25,7 @@ private:
const string TOPIC = "/clock"; const string TOPIC = "/clock";
tests_a_compA* component = nullptr; tests_a_compA* component = nullptr;
Callback* _callback = nullptr;
mqtt::client* _clockSubscriber = nullptr; mqtt::client* _clockSubscriber = nullptr;
mqtt::client* _echoPublisher = 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)
{
mqttOut = value;
}
// MqttAdapter #pragma once
//
// Created by Georg Vinogradov on 05.06.19.
// Copyright © 2019 Georg Vinogradov. All rights reserved.
//
#ifndef tests_a_compA_h #ifndef tests_a_compA_h
#define tests_a_compA_h #define tests_a_compA_h
...@@ -14,8 +10,9 @@ using namespace std; ...@@ -14,8 +10,9 @@ using namespace std;
class tests_a_compA { class tests_a_compA {
public: public:
double rosIn = 0; tests_a_compA(double value);
double rosOut = 0; double mqttIn = 0;
double mqttOut = 0;
}; };
#endif /* tests_a_compA_h */ #endif /* tests_a_compA_h */
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.emam2mqtt</groupId> <groupId>de.monticore.lang.monticar</groupId>
<artifactId>emam2mqtt</artifactId> <artifactId>embedded-montiarc-math-mqtt-generator</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<name>emam2mqtt</name> <name>emam2mqtt</name>
...@@ -45,6 +45,14 @@ ...@@ -45,6 +45,14 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc</artifactId>
<!-- or newer version -->
<version>0.1.10-SNAPSHOT</version>
</dependency>
<dependency> <dependency>
<groupId>de.se_rwth.commons</groupId> <groupId>de.se_rwth.commons</groupId>
......
package com.emam2mqtt;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
package de.monticore.lang.monticar.generator.mqtt;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAPortInstanceSymbol;
import de.monticore.lang.monticar.generator.mqtt.template.MqttAdapterModel;
import de.monticore.lang.monticar.generator.mqtt.template.MqttTemplates;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class GeneratorMqtt
{
List<File> generateMqttAdapter(EMAComponentInstanceSymbol component)
{
List<File> files = new ArrayList<>();
List<String> contents = new ArrayList<String>();
List<FileWriter> frs = new ArrayList<FileWriter>();
// Create and fill model
MqttAdapterModel model = new MqttAdapterModel(component.getFullName());
model.addPorts(component.getPortInstanceList());
//Generate files and write to project
contents.add(MqttTemplates.generateMqttAdapterH(model));
files.add(new File("./target/generated-sources/MqttAdapter_"+model.getEscapedCompName()+".h"));
contents.add(MqttTemplates.generateMqttAdapterCPP(model));
files.add(new File("./target/generated-sources/MqttAdapter_"+model.getEscapedCompName()+".cpp"));
contents.add(MqttTemplates.generateMqttCallbackH(model));
files.add(new File("./target/generated-sources/Callback.hpp"));
contents.add(MqttTemplates.generateMqttCallbackCPP(model));
files.add(new File("./target/generated-sources/Callback.cpp"));
try {
int counter = 0;
for (File file : files)
{
frs.add(new FileWriter(file));
frs.get(counter).write(contents.get(counter));
counter++;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
//Close resources
try {
for (FileWriter fr : frs)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
files.add(generateCMake(component));
return files;
}
File generateCMake(EMAComponentInstanceSymbol component)
{
// Create and fill model
MqttAdapterModel model = new MqttAdapterModel(component.getFullName());
//Generate files and write to project
String content = MqttTemplates.generateMqttCMakeLists(model);
File file = new File("./target/generated-sources/CMakeLists.txt");
FileWriter fr = null;
try {
fr = new FileWriter(file);
fr.write(content);
} catch (IOException e) {
e.printStackTrace();
}finally{
//Close resources
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
List<File> generatePrettyPrint(EMAComponentInstanceSymbol component)
{
List<File> files = new ArrayList<>();
// Get info about the ports from the component
Collection<EMAPortInstanceSymbol> ports = component.getPortInstanceList();
// Create and fill model
MqttAdapterModel model = new MqttAdapterModel(component.getFullName());
model.addPortsDesc(ports);
//Generate files and write to project
String content = MqttTemplates.generatePrettyPrint(model);
File file = new File("./target/generated-sources/ports.txt");
files.add(file);
FileWriter fr = null;
try {
fr = new FileWriter(file);
fr.write(content);
} catch (IOException e) {
e.printStackTrace();
}finally{
//Close resources
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return files;
}
}
package de.monticore.lang.monticar.generator.mqtt.template;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAPortInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.MiddlewareSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.mqtt.MqttConnectionSymbol;