Skip to content
Snippets Groups Projects
Commit d0cd0239 authored by René Schöne's avatar René Schöne
Browse files

Renamed MqttUpdater to MqttHandler.

parent 56be33c4
No related branches found
No related tags found
No related merge requests found
...@@ -27,11 +27,11 @@ aspect GenerationUtils { ...@@ -27,11 +27,11 @@ aspect GenerationUtils {
aspect AttributesForMustache { aspect AttributesForMustache {
// --- MRos2Rag --- // --- MRos2Rag ---
eq MRos2Rag.getChild().mqttUpdaterAttribute() = mqttUpdaterAttribute(); eq MRos2Rag.getChild().mqttHandlerAttribute() = mqttHandlerAttribute();
eq MRos2Rag.getChild().mqttUpdaterField() = mqttUpdaterField(); eq MRos2Rag.getChild().mqttHandlerField() = mqttHandlerField();
syn String MRos2Rag.mqttUpdaterAttribute() = getRos2Rag().mqttUpdaterAttribute(); syn String MRos2Rag.mqttHandlerAttribute() = getRos2Rag().mqttHandlerAttribute();
syn String MRos2Rag.mqttUpdaterField() = getRos2Rag().mqttUpdaterField(); syn String MRos2Rag.mqttHandlerField() = getRos2Rag().mqttHandlerField();
syn String MRos2Rag.mqttSetHostMethod() = getRos2Rag().mqttSetHostMethod(); syn String MRos2Rag.mqttSetHostMethod() = getRos2Rag().mqttSetHostMethod();
syn String MRos2Rag.mqttWaitUntilReadyMethod() = getRos2Rag().mqttWaitUntilReadyMethod(); syn String MRos2Rag.mqttWaitUntilReadyMethod() = getRos2Rag().mqttWaitUntilReadyMethod();
syn String MRos2Rag.mqttCloseMethod() = getRos2Rag().mqttCloseMethod(); syn String MRos2Rag.mqttCloseMethod() = getRos2Rag().mqttCloseMethod();
...@@ -46,7 +46,7 @@ aspect AttributesForMustache { ...@@ -46,7 +46,7 @@ aspect AttributesForMustache {
eq MUpdateDefinition.getInnerMappingDefinition().resultVarPrefix() = resultVarPrefix(); eq MUpdateDefinition.getInnerMappingDefinition().resultVarPrefix() = resultVarPrefix();
eq MUpdateDefinition.getInnerMappingDefinition(int i).inputVarName() = i == 0 ? firstInputVarName() : resultVarPrefix() + getInnerMappingDefinition(i - 1).getMappingDefinition().methodName(); eq MUpdateDefinition.getInnerMappingDefinition(int i).inputVarName() = i == 0 ? firstInputVarName() : resultVarPrefix() + getInnerMappingDefinition(i - 1).getMappingDefinition().methodName();
inh String MUpdateDefinition.mqttUpdaterAttribute(); inh String MUpdateDefinition.mqttHandlerAttribute();
syn String MUpdateDefinition.connectMethod() = updateDef().connectMethod(); syn String MUpdateDefinition.connectMethod() = updateDef().connectMethod();
syn TokenComponent MUpdateDefinition.token() = updateDef().getToken(); syn TokenComponent MUpdateDefinition.token() = updateDef().getToken();
...@@ -114,8 +114,8 @@ aspect AttributesForMustache { ...@@ -114,8 +114,8 @@ aspect AttributesForMustache {
// --- MTypeComponent --- // --- MTypeComponent ---
syn String MTypeComponent.name() = getTypeComponent().getName(); syn String MTypeComponent.name() = getTypeComponent().getName();
inh String MTypeComponent.mqttUpdaterAttribute(); inh String MTypeComponent.mqttHandlerAttribute();
inh String MTypeComponent.mqttUpdaterField(); inh String MTypeComponent.mqttHandlerField();
// --- MTokenComponent --- // --- MTokenComponent ---
syn String MTokenComponent.parentTypeName() = getTokenComponent().containingTypeDecl().getName(); syn String MTokenComponent.parentTypeName() = getTokenComponent().containingTypeDecl().getName();
...@@ -216,19 +216,19 @@ aspect AspectGeneration { ...@@ -216,19 +216,19 @@ aspect AspectGeneration {
syn String DependencyDefinition.internalRelationPrefix() = "_internal_" + getID(); syn String DependencyDefinition.internalRelationPrefix() = "_internal_" + getID();
syn String DependencyDefinition.internalTokenName() = getSource().internalName(); syn String DependencyDefinition.internalTokenName() = getSource().internalName();
syn String Ros2Rag.mqttUpdaterAttribute() = "_mqttUpdater"; syn String Ros2Rag.mqttHandlerAttribute() = "_mqttHandler";
syn String Ros2Rag.mqttUpdaterField() = "_mqttUpdater"; syn String Ros2Rag.mqttHandlerField() = "_mqttHandler";
syn String Ros2Rag.mqttSetHostMethod() = "MqttSetHost"; syn String Ros2Rag.mqttSetHostMethod() = "MqttSetHost";
syn String Ros2Rag.mqttWaitUntilReadyMethod() = "MqttWaitUntilReady"; syn String Ros2Rag.mqttWaitUntilReadyMethod() = "MqttWaitUntilReady";
syn String Ros2Rag.mqttCloseMethod() = "MqttCloseConnections"; syn String Ros2Rag.mqttCloseMethod() = "MqttCloseConnections";
// naming copy attributes // naming copy attributes
// --- mqttUpdaterAttribute --- // --- mqttHandlerAttribute ---
inh String UpdateDefinition.mqttUpdaterAttribute(); inh String UpdateDefinition.mqttHandlerAttribute();
inh String MappingDefinition.mqttUpdaterAttribute(); inh String MappingDefinition.mqttHandlerAttribute();
inh String DependencyDefinition.mqttUpdaterAttribute(); inh String DependencyDefinition.mqttHandlerAttribute();
eq Ros2Rag.getChild().mqttUpdaterAttribute() = mqttUpdaterAttribute(); eq Ros2Rag.getChild().mqttHandlerAttribute() = mqttHandlerAttribute();
// --- rootNodeName --- // --- rootNodeName ---
syn String ASTNode.rootNodeName() = rootNode.getName(); syn String ASTNode.rootNodeName() = rootNode.getName();
......
...@@ -96,18 +96,18 @@ public class Compiler { ...@@ -96,18 +96,18 @@ public class Compiler {
} }
printMessage("Writing output files"); printMessage("Writing output files");
// copy MqttUpdater into outputDir // copy MqttHandler into outputDir
final String mqttUpdaterFileName = "MqttUpdater.jadd"; final String mqttHandlerFileName = "MqttHandler.jadd";
try { try {
InputStream inputStream = Compiler.class.getClassLoader().getResourceAsStream(mqttUpdaterFileName); InputStream inputStream = Compiler.class.getClassLoader().getResourceAsStream(mqttHandlerFileName);
if (inputStream == null) { if (inputStream == null) {
throw new CompilerException("Could not open " + mqttUpdaterFileName); throw new CompilerException("Could not open " + mqttHandlerFileName);
} }
Files.copy(inputStream, Files.copy(inputStream,
Paths.get(outputDir, mqttUpdaterFileName), Paths.get(outputDir, mqttHandlerFileName),
StandardCopyOption.REPLACE_EXISTING); StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) { } catch (IOException e) {
throw new CompilerException("Could not copy " + mqttUpdaterFileName, e); throw new CompilerException("Could not copy " + mqttHandlerFileName, e);
} }
writeToFile(outputDir + "/Grammar.relast", ros2Rag.getProgram().generateAbstractGrammar()); writeToFile(outputDir + "/Grammar.relast", ros2Rag.getProgram().generateAbstractGrammar());
writeToFile(outputDir + "/ROS2RAG.jadd", ros2Rag.generateAspect(optionRootNode.getValue())); writeToFile(outputDir + "/ROS2RAG.jadd", ros2Rag.generateAspect(optionRootNode.getValue()));
......
aspect MqttUpdater { aspect MqttHandler {
/** /**
* Helper class to receive updates via MQTT and use callbacks to handle those messages. * Helper class to receive updates via MQTT and use callbacks to handle those messages.
* *
* @author rschoene - Initial contribution * @author rschoene - Initial contribution
*/ */
public class MqttUpdater { public class MqttHandler {
private static final int DEFAULT_PORT = 1883; private static final int DEFAULT_PORT = 1883;
private final org.apache.logging.log4j.Logger logger; private final org.apache.logging.log4j.Logger logger;
...@@ -23,13 +23,13 @@ public class MqttUpdater { ...@@ -23,13 +23,13 @@ public class MqttUpdater {
/** Dispatch knowledge */ /** Dispatch knowledge */
private final java.util.Map<String, java.util.function.Consumer<byte[]>> callbacks; private final java.util.Map<String, java.util.function.Consumer<byte[]>> callbacks;
public MqttUpdater() { public MqttHandler() {
this("Ros2Rag"); this("Ros2Rag");
} }
public MqttUpdater(String name) { public MqttHandler(String name) {
this.name = java.util.Objects.requireNonNull(name, "Name must be set"); this.name = java.util.Objects.requireNonNull(name, "Name must be set");
this.logger = org.apache.logging.log4j.LogManager.getLogger(MqttUpdater.class); this.logger = org.apache.logging.log4j.LogManager.getLogger(MqttHandler.class);
this.callbacks = new java.util.HashMap<>(); this.callbacks = new java.util.HashMap<>();
this.readyLock = new java.util.concurrent.locks.ReentrantLock(); this.readyLock = new java.util.concurrent.locks.ReentrantLock();
this.readyCondition = readyLock.newCondition(); this.readyCondition = readyLock.newCondition();
...@@ -37,7 +37,7 @@ public class MqttUpdater { ...@@ -37,7 +37,7 @@ public class MqttUpdater {
this.qos = org.fusesource.mqtt.client.QoS.AT_LEAST_ONCE; this.qos = org.fusesource.mqtt.client.QoS.AT_LEAST_ONCE;
} }
public MqttUpdater dontSendWelcomeMessage() { public MqttHandler dontSendWelcomeMessage() {
this.sendWelcomeMessage = false; this.sendWelcomeMessage = false;
return this; return this;
} }
...@@ -47,7 +47,7 @@ public class MqttUpdater { ...@@ -47,7 +47,7 @@ public class MqttUpdater {
* @throws IOException if could not connect, or could not subscribe to a topic * @throws IOException if could not connect, or could not subscribe to a topic
* @return self * @return self
*/ */
public MqttUpdater setHost(String host) throws java.io.IOException { public MqttHandler setHost(String host) throws java.io.IOException {
return setHost(host, DEFAULT_PORT); return setHost(host, DEFAULT_PORT);
} }
...@@ -56,7 +56,7 @@ public class MqttUpdater { ...@@ -56,7 +56,7 @@ public class MqttUpdater {
* @throws IOException if could not connect, or could not subscribe to a topic * @throws IOException if could not connect, or could not subscribe to a topic
* @return self * @return self
*/ */
public MqttUpdater setHost(String host, int port) throws java.io.IOException { public MqttHandler setHost(String host, int port) throws java.io.IOException {
java.util.Objects.requireNonNull(host, "Host need to be set!"); java.util.Objects.requireNonNull(host, "Host need to be set!");
this.host = java.net.URI.create("tcp://" + host + ":" + port); this.host = java.net.URI.create("tcp://" + host + ":" + port);
...@@ -109,7 +109,7 @@ public class MqttUpdater { ...@@ -109,7 +109,7 @@ public class MqttUpdater {
connection.connect(new org.fusesource.mqtt.client.Callback<Void>() { connection.connect(new org.fusesource.mqtt.client.Callback<Void>() {
@Override @Override
public void onSuccess(Void value) { public void onSuccess(Void value) {
if (MqttUpdater.this.sendWelcomeMessage) { if (MqttHandler.this.sendWelcomeMessage) {
connection.publish("components", (name + " is connected").getBytes(), org.fusesource.mqtt.client.QoS.AT_LEAST_ONCE, false, new org.fusesource.mqtt.client.Callback<Void>() { connection.publish("components", (name + " is connected").getBytes(), org.fusesource.mqtt.client.QoS.AT_LEAST_ONCE, false, new org.fusesource.mqtt.client.Callback<Void>() {
@Override @Override
public void onSuccess(Void value) { public void onSuccess(Void value) {
......
aspect MQTT { aspect MQTT {
private MqttUpdater {{rootNodeName}}.{{mqttUpdaterField}} = new MqttUpdater(); private MqttHandler {{rootNodeName}}.{{mqttHandlerField}} = new MqttHandler();
public void {{rootNodeName}}.{{mqttSetHostMethod}}(String host) throws java.io.IOException { public void {{rootNodeName}}.{{mqttSetHostMethod}}(String host) throws java.io.IOException {
{{mqttUpdaterField}}.setHost(host); {{mqttHandlerField}}.setHost(host);
} }
public void {{rootNodeName}}.{{mqttSetHostMethod}}(String host, int port) throws java.io.IOException { public void {{rootNodeName}}.{{mqttSetHostMethod}}(String host, int port) throws java.io.IOException {
{{mqttUpdaterField}}.setHost(host, port); {{mqttHandlerField}}.setHost(host, port);
} }
public boolean {{rootNodeName}}.{{mqttWaitUntilReadyMethod}}(long time, java.util.concurrent.TimeUnit unit) { public boolean {{rootNodeName}}.{{mqttWaitUntilReadyMethod}}(long time, java.util.concurrent.TimeUnit unit) {
return {{mqttUpdaterField}}.waitUntilReady(time, unit); return {{mqttHandlerField}}.waitUntilReady(time, unit);
} }
public void {{rootNodeName}}.{{mqttCloseMethod}}() { public void {{rootNodeName}}.{{mqttCloseMethod}}() {
{{mqttUpdaterField}}.close(); {{mqttHandlerField}}.close();
} }
inh MqttUpdater ASTNode.{{mqttUpdaterAttribute}}(); inh MqttHandler ASTNode.{{mqttHandlerAttribute}}();
{{#getRootTypeComponents}} {{#getRootTypeComponents}}
eq {{rootNodeName}}.get{{name}}().{{mqttUpdaterAttribute}}() = {{mqttUpdaterField}}; eq {{rootNodeName}}.get{{name}}().{{mqttHandlerAttribute}}() = {{mqttHandlerField}};
{{/getRootTypeComponents}} {{/getRootTypeComponents}}
} }
public void {{parentTypeName}}.{{connectMethod}}(String topic) { public void {{parentTypeName}}.{{connectMethod}}(String topic) {
{{mqttUpdaterAttribute}}().newConnection(topic, message -> { {{mqttHandlerAttribute}}().newConnection(topic, message -> {
{{> mappingApplication}} {{> mappingApplication}}
{{#loggingEnabledForReads}} {{#loggingEnabledForReads}}
System.out.println("[Read] " + topic + " -> {{tokenName}} = " + {{lastResult}});{{!lastResult has to be a new attribute}} System.out.println("[Read] " + topic + " -> {{tokenName}} = " + {{lastResult}});{{!lastResult has to be a new attribute}}
......
...@@ -20,5 +20,5 @@ ...@@ -20,5 +20,5 @@
{{#loggingEnabledForWrites}} {{#loggingEnabledForWrites}}
System.out.println("[Write] {{tokenName}} = " + get{{tokenName}}() + " -> " + {{writeTopic}}); System.out.println("[Write] {{tokenName}} = " + get{{tokenName}}() + " -> " + {{writeTopic}});
{{/loggingEnabledForWrites}} {{/loggingEnabledForWrites}}
{{mqttUpdaterAttribute}}().publish({{writeTopic}}, {{lastValue}}); {{mqttHandlerAttribute}}().publish({{writeTopic}}, {{lastValue}});
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment