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
......@@ -5,3 +5,4 @@
.classpath
.DS_Store
.idea
.iml
stages:
- linux
- project
- adapter
- deploy
BranchJobLinux:
stage: linux
BranchJobDeploy:
stage: deploy
image: registry.git.rwth-aachen.de/monticore/embeddedmontiarc/generators/emam2mqtt
script:
- 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:
- master
......@@ -8,7 +8,7 @@ These instructions will get MQTT up and running on your **Ubuntu** system
### 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
......@@ -72,6 +72,17 @@ cd paho.mqtt.cpp
cmake -Bbuild -H.
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)
open terminal, switch to **mqtt_demo** directroy inside the project and run
`cmake .`
......
FROM ubuntu:latest
RUN apt-get update
RUN apt-get -y install software-properties-common
RUN apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
RUN add-apt-repository ppa:webupd8team/java
RUN apt-get update
RUN apt-get -y install mosquitto
RUN apt-get -y install mosquitto-clients
RUN apt-get -y install openjdk-8-jdk
RUN apt-get update && apt-get -y install \
mosquitto \
mosquitto-clients \
openjdk-8-jdk
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
cmake_minimum_required(VERSION 3.1...3.14)
......@@ -18,20 +17,38 @@ project(MqttAdapter VERSION 1.0
DESCRIPTION "MQTT adapter"
LANGUAGES CXX)
# Find mqtt packages using modules
find_package(MQTT REQUIRED)
set(SEARCH_MQTT FALSE)
# 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
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
if (NOT MQTT_C_LIB OR NOT MQTT_A_LIB OR NOT MQTT_PP_LIB)
if(SEARCH_MQTT)
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)
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})
else()
message("MQTT includes found in " ${MQTT_INCLUDE_DIR})
message("MQTT libraries found in " ${MQTT_LIBS})
endif()
endif()
# 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"
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) {
......@@ -33,18 +37,15 @@ void MqttAdapter_tests_a_compA::init(tests_a_compA *comp)
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 {
_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)
{
mqttOut = 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,8 +10,9 @@ using namespace std;
class tests_a_compA {
public:
double rosIn = 0;
double rosOut = 0;
tests_a_compA(double value);
double mqttIn = 0;
double mqttOut = 0;
};
#endif /* tests_a_compA_h */
......@@ -4,8 +4,8 @@
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>
<groupId>com.emam2mqtt</groupId>
<artifactId>emam2mqtt</artifactId>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-math-mqtt-generator</artifactId>
<version>1.0-SNAPSHOT</version>
<name>emam2mqtt</name>
......@@ -46,6 +46,14 @@
<dependencies>
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc</artifactId>
<!-- or newer version -->
<version>0.1.10-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>de.se_rwth.commons</groupId>
<artifactId>se-commons-logging</artifactId>
......
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;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.mqtt.MqttConnectionSymbol.MqttConnectionKind;
import java.util.*;
import java.util.stream.Collectors;
// Used to fill .ftl files
public class MqttAdapterModel {
private String compName;
private List<String> ports = new ArrayList<>();
private List<EMAPortInstanceSymbol> incoming = new ArrayList<>();
private List<EMAPortInstanceSymbol> outgoing = new ArrayList<>();
public MqttAdapterModel(String compName)
{
this.compName = compName;
}
public String getCompName()
{
return compName;
}
public String getEscapedCompName()
{
return compName
.replace('.', '_')
.replace('[', '_')
.replace(']', '_');
}
public List<EMAPortInstanceSymbol> getIncomingPorts()
{
return incoming;
}
public List<EMAPortInstanceSymbol> getOutgoingPorts()
{
return outgoing;
}
public void addPorts(Collection<EMAPortInstanceSymbol> ports)
{
incoming.addAll(ports);
incoming = incoming.stream().filter(fc -> fc.isMqttPort()).filter(fc -> fc.isIncoming()).collect(Collectors.toList());
outgoing.addAll(ports);
outgoing = outgoing.stream().filter(fc -> fc.isMqttPort()).filter(fc -> fc.isOutgoing()).collect(Collectors.toList());
}
public String getTopic(EMAPortInstanceSymbol port)
{
Optional<MiddlewareSymbol> symbol = port.getMiddlewareSymbol();
if(symbol.isPresent() && symbol.get().isKindOf(MqttConnectionKind.INSTANCE))
{
MqttConnectionSymbol sym = (MqttConnectionSymbol) symbol.get();
String topicName = sym.getTopicName().isPresent()?sym.getTopicName().get():"unknown";
return topicName;
}
return "";
}
// Parse through component to find information about its ports
public void addPortsDesc(Collection<EMAPortInstanceSymbol> ports)
{
for (EMAPortInstanceSymbol port : ports)
{