Commit f957bd2b authored by Georg Vinogradov's avatar Georg Vinogradov

Add all needed .ftl/.java files

parent 93d97751
Pipeline #158164 passed with stages
in 1 minute and 35 seconds
......@@ -51,6 +51,43 @@ public class GeneratorMqtt
return files;
}
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.addPorts(ports);
//Generate files and write to project
String content = MqttTemplates.generateMqttAdapter(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;
}
File generateCMake(EMAComponentInstanceSymbol component)
{
......
#include "<@m.mwIdent/>Adapter_${model.getEscapedCompName()}.h"
using namespace std;
<@m.mwIdent/>Adapter_${model.getEscapedCompName()}::<@m.mwIdent/>Adapter_${model.getEscapedCompName()}()
{
}
void <@m.mwIdent/>Adapter_${model.getEscapedCompName()}::init(${model.getEscapedCompName()} *comp)
{
// Initialize component
this->component = comp;
// Initialize connect options
<@m.smallIdent/>::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);
_echoPublisher->connect(connOpts);
_clockSubscriber->subscribe(TOPIC, 1);
} catch (const mqtt::exception& exc) {
cerr << exc.what() << endl;
}
}
void MqttAdapter_tests_a_compA::publish_echoPublisher()
{
string value = to_string(component->rosOut);
auto pubmsg = mqtt::make_message(TOPIC, value);
try {
_echoPublisher->publish(pubmsg);
} catch (const mqtt::exception& exc) {
cerr << exc.to_string() << endl;
}
}
void MqttAdapter_tests_a_compA::tick()
{
publish_echoPublisher();
}
#pragma once
#include "${model.getEscapedCompName()}.h"
<@m.mwDefaultIncludes/>
<#list model.getIncludes() as incl>
<@m.mwMsgInclude incl=incl/>
</#list>
using namespace std;
class <@m.mwIdent/>Adapter_${model.getEscapedCompName()} {
public:
<@m.mwIdent/>Adapter_${model.getEscapedCompName()}();
void init(${model.getEscapedCompName()}* comp);
void publish_echoPublisher();
void tick();
private:
const string SERVER_ADDRESS = "${model.getServerAddress()}";
const string PUB_ID = "publisher_cpp";
const string SUB_ID = "subscriber_cpp";
const string TOPIC = "/clock";
${model.getEscapedCompName()}* component = nullptr;
Callback* _callback = nullptr;
<@m.fieldSubscriber name="${model.getSubscriberName()}"/> = nullptr;
<@m.fieldSubscriber name="${model.getPublisherName()}"/> = nullptr;
};
<#macro mwIdent>Mqtt</#macro>
<#macro smallIdent>mqtt</#macro>
<#macro mwDefaultIncludes>
#include "mqtt/client.h"
</#macro>
<#macro mwMsgInclude incl>
#include <${incl}>
</#macro>
<#macro fieldSubscriber name>
mqtt::client* ${name};
</#macro>
<#macro fieldPublisher name>
mqtt::client* ${name};
</#macro>
<#macro mwinit compname>
char* tmp = strdup("");
int i = 0;
ros::init(i, &tmp, "RosAdapter_${compname}_node");
ros::NodeHandle node_handle = ros::NodeHandle();
</#macro>
<#macro mwstart>
ros::spin();
</#macro>
<#macro initSubscriber sub compName>
${sub.getNameInTargetLanguage()} = node_handle.subscribe("${sub.getTopicName()}", 5, &RosAdapter_${compName}::${sub.getMethodName()}, this, ros::TransportHints().tcpNoDelay());
</#macro>
<#macro initPublisher pub>
${pub.getNameInTargetLanguage()} = node_handle.advertise<${pub.getTypeNameInTargetLanguage()}>("${pub.getTopicName()}",5);
</#macro>
<#macro callback sub>
void ${sub.getMethodName()}(const ${sub.getTypeNameInTargetLanguage()}::ConstPtr& msg){
<#if !sub.isStructInterface() && sub.getRosConnectionSymbol().getMsgField().isPresent()>
component->${sub.getPortNameInTargetLanguage()} = msg->${sub.getRosConnectionSymbol().getMsgField().get()};
<#else>
${sub.getRosSetStructInstruction()}
</#if>
}
</#macro>
<#macro publish pub>
void publish${pub.getNameInTargetLanguage()}(){
${pub.getTypeNameInTargetLanguage()} tmpMsg;
<#if !pub.isStructInterface() && pub.getRosConnectionSymbol().getMsgField().isPresent()>
tmpMsg.${pub.getRosConnectionSymbol().getMsgField().get()} = component->${pub.getPortNameInTargetLanguage()};
<#else>
${pub.getRosSetStructInstruction()}
</#if>
${pub.getNameInTargetLanguage()}.publish(tmpMsg);
}
</#macro>
package de.monticore.lang.monticar.generator.mqtt;
import static org.junit.Assert.assertNotNull;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.mqtt.MqttConnectionSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.ros.RosToEmamTagSchema;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
public class AdapterGenerationTest extends AbstractSymtabTest {
@Test
public void testAdapter() throws IOException {
TaggingResolver taggingResolver = createSymTabAndTaggingResolver("src/test/resources/");
// Ros schema is used for now
RosToEmamTagSchema.registerTagTypes(taggingResolver);
// Create component instance and run the generator
EMAComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<EMAComponentInstanceSymbol>resolve("tests.a.compA", EMAComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
GeneratorMqtt generatorMqtt = new GeneratorMqtt();
// Connect component's ports to topics
componentInstanceSymbol.getPortInstance("portA").orElse(null).setMiddlewareSymbol(new MqttConnectionSymbol("/clock"));
}
}
......@@ -33,7 +33,7 @@ public class PrettyPrintGenerationTest extends AbstractSymtabTest {
// Connect component's ports to topics
componentInstanceSymbol.getPortInstance("portA").orElse(null).setMiddlewareSymbol(new MqttConnectionSymbol("/clock"));
List<File> files = generatorMqtt.generateMqttAdapter(componentInstanceSymbol);
List<File> files = generatorMqtt.generatePrettyPrint(componentInstanceSymbol);
testFilesAreEqual(files, "echo/");
}
......
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