You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2018/10/27 14:07:21 UTC
[incubator-plc4x] branch master updated: - Made the MqttConnector
actually work with a dummy public server using the test-driver.
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
The following commit(s) were added to refs/heads/master by this push:
new b978413 - Made the MqttConnector actually work with a dummy public server using the test-driver.
b978413 is described below
commit b978413e237ec379ac6bb970637d62c91e74d24a
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Sat Oct 27 16:07:17 2018 +0200
- Made the MqttConnector actually work with a dummy public server using the test-driver.
---
.../hello-connectivity-kafka/kafka-connector.yml | 21 ++++++++++-----------
examples/hello-connectivity-kafka/pom.xml | 7 +++++++
.../examples/connectivity/kafka/KafkaConnector.java | 2 +-
examples/hello-connectivity-mqtt/mqtt-connector.yml | 21 +++++++++++----------
examples/hello-connectivity-mqtt/pom.xml | 7 +++++++
.../examples/connectivity/mqtt/MqttConnector.java | 17 +++++++----------
6 files changed, 43 insertions(+), 32 deletions(-)
diff --git a/examples/hello-connectivity-kafka/kafka-connector.yml b/examples/hello-connectivity-kafka/kafka-connector.yml
index 55a387f..c5aa992 100644
--- a/examples/hello-connectivity-kafka/kafka-connector.yml
+++ b/examples/hello-connectivity-kafka/kafka-connector.yml
@@ -19,17 +19,16 @@
---
kafka:
topic-name: test-topic
- bootstrap-servers: localhost
- server-port: 1234
+ bootstrap-servers: localhost:9092
plc:
- connection: s7://192.168.0.1/1/1
+ connection: test:plc4x-example-mqtt
addresses:
- - name: param1
- address: INPUTS/0
- size: 1
- type: java.lang.Byte
- - name: param2
- address: OUTPUTS/0
- size: 1
- type: java.lang.Byte
+ - name: intParam1
+ address: RANDOM/foo:INTEGER
+ size: 1
+ type: java.lang.Integer
+ - name: intParam2
+ address: RANDOM/bar:INTEGER
+ size: 1
+ type: java.lang.Integer
polling-interval: 1000
diff --git a/examples/hello-connectivity-kafka/pom.xml b/examples/hello-connectivity-kafka/pom.xml
index 8ec838f..f8663cc 100644
--- a/examples/hello-connectivity-kafka/pom.xml
+++ b/examples/hello-connectivity-kafka/pom.xml
@@ -105,6 +105,12 @@
<version>0.2.0-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.plc4x</groupId>
+ <artifactId>plc4j-protocol-test</artifactId>
+ <version>0.2.0-SNAPSHOT</version>
+ <scope>runtime</scope>
+ </dependency>
</dependencies>
<!-- TODO: Can be removed as soon as Edgent is released in a version 1.3.0 -->
@@ -146,6 +152,7 @@
<configuration>
<usedDependencies>
<usedDependency>org.apache.plc4x:plc4j-protocol-s7</usedDependency>
+ <usedDependency>org.apache.plc4x:plc4j-protocol-test</usedDependency>
</usedDependencies>
</configuration>
</plugin>
diff --git a/examples/hello-connectivity-kafka/src/main/java/org/apache/plc4x/java/examples/connectivity/kafka/KafkaConnector.java b/examples/hello-connectivity-kafka/src/main/java/org/apache/plc4x/java/examples/connectivity/kafka/KafkaConnector.java
index 7080cd5..31eb2b9 100644
--- a/examples/hello-connectivity-kafka/src/main/java/org/apache/plc4x/java/examples/connectivity/kafka/KafkaConnector.java
+++ b/examples/hello-connectivity-kafka/src/main/java/org/apache/plc4x/java/examples/connectivity/kafka/KafkaConnector.java
@@ -96,7 +96,7 @@ public class KafkaConnector {
JsonObject jsonObject = new JsonObject();
value.getFieldNames().forEach(fieldName -> {
if(value.getNumberOfValues(fieldName) == 1) {
- jsonObject.addProperty(fieldName, Byte.toString(value.getByte(fieldName)));
+ jsonObject.addProperty(fieldName, value.getObject(fieldName).toString());
} else if (value.getNumberOfValues(fieldName) > 1) {
JsonArray values = new JsonArray();
value.getAllBytes(fieldName).forEach(values::add);
diff --git a/examples/hello-connectivity-mqtt/mqtt-connector.yml b/examples/hello-connectivity-mqtt/mqtt-connector.yml
index 07f5c69..a0b1c15 100644
--- a/examples/hello-connectivity-mqtt/mqtt-connector.yml
+++ b/examples/hello-connectivity-mqtt/mqtt-connector.yml
@@ -18,17 +18,18 @@
# ----------------------------------------------------------------------------
---
mqtt:
- topic-name: test-topic
- server-host: localhost:9092
+ topic-name: plc4x/test-topic
+ server-host: test.mosquitto.org
+ server-port: 1883
plc:
- connection: s7://192.168.0.1/1/1
+ connection: test:plc4x-example-mqtt
addresses:
- - name: param1
- address: INPUTS/0
+ - name: intParam
+ address: RANDOM/foo:INTEGER
size: 1
- type: java.lang.Byte
- - name: param2
- address: OUTPUTS/0
+ type: java.lang.Integer
+ - name: intParam2
+ address: RANDOM/bar:INTEGER
size: 1
- type: java.lang.Byte
-polling-interval: 1000
+ type: java.lang.Integer
+polling-interval: 2000
diff --git a/examples/hello-connectivity-mqtt/pom.xml b/examples/hello-connectivity-mqtt/pom.xml
index 8563acc..907b38e 100644
--- a/examples/hello-connectivity-mqtt/pom.xml
+++ b/examples/hello-connectivity-mqtt/pom.xml
@@ -96,6 +96,12 @@
<version>0.2.0-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.plc4x</groupId>
+ <artifactId>plc4j-protocol-test</artifactId>
+ <version>0.2.0-SNAPSHOT</version>
+ <scope>runtime</scope>
+ </dependency>
</dependencies>
<build>
@@ -106,6 +112,7 @@
<configuration>
<usedDependencies>
<usedDependency>org.apache.plc4x:plc4j-protocol-s7</usedDependency>
+ <usedDependency>org.apache.plc4x:plc4j-protocol-test</usedDependency>
</usedDependencies>
</configuration>
</plugin>
diff --git a/examples/hello-connectivity-mqtt/src/main/java/org/apache/plc4x/java/examples/connectivity/mqtt/MqttConnector.java b/examples/hello-connectivity-mqtt/src/main/java/org/apache/plc4x/java/examples/connectivity/mqtt/MqttConnector.java
index 0354836..948f11d 100644
--- a/examples/hello-connectivity-mqtt/src/main/java/org/apache/plc4x/java/examples/connectivity/mqtt/MqttConnector.java
+++ b/examples/hello-connectivity-mqtt/src/main/java/org/apache/plc4x/java/examples/connectivity/mqtt/MqttConnector.java
@@ -33,7 +33,6 @@ import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcReadResponse;
import org.apache.plc4x.java.examples.connectivity.mqtt.model.Configuration;
import org.apache.plc4x.java.examples.connectivity.mqtt.model.PlcFieldConfig;
-import org.apache.plc4x.java.examples.connectivity.mqtt.model.PlcMemoryBlock;
import org.mqttbee.api.mqtt.MqttClient;
import org.mqttbee.api.mqtt.datatypes.MqttQos;
import org.mqttbee.api.mqtt.mqtt3.Mqtt3Client;
@@ -95,21 +94,19 @@ public class MqttConnector {
// Create a new read request.
PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
- for(PlcMemoryBlock plcMemoryBlock : config.getPlcConfig().getPlcMemoryBlocks()) {
- for (PlcFieldConfig address : config.getPlcConfig().getPlcFields()) {
- builder = builder.addItem(plcMemoryBlock.getName() + "/" + address.getName(),
- "DATA_BLOCKS/" + plcMemoryBlock.getAddress() + "/" + address.getAddress());
- }
+ for (PlcFieldConfig fieldConfig : config.getPlcConfig().getPlcFields()) {
+ builder = builder.addItem(fieldConfig.getName(), fieldConfig.getAddress());
}
PlcReadRequest readRequest = builder.build();
// Send a message containing the PLC read response.
Flowable<Mqtt3Publish> messagesToPublish = Flowable.generate(emitter -> {
PlcReadResponse response = readRequest.execute().get();
+ String jsonPayload = getPayload(response);
final Mqtt3Publish publishMessage = Mqtt3Publish.builder()
.topic(config.getMqttConfig().getTopicName())
.qos(MqttQos.AT_LEAST_ONCE)
- .payload(getPayload(response))
+ .payload(jsonPayload.getBytes())
.build();
emitter.onNext(publishMessage);
});
@@ -132,18 +129,18 @@ public class MqttConnector {
}
}
- private static byte[] getPayload(PlcReadResponse response) {
+ private String getPayload(PlcReadResponse response) {
JsonObject jsonObject = new JsonObject();
response.getFieldNames().forEach(fieldName -> {
if(response.getNumberOfValues(fieldName) == 1) {
- jsonObject.addProperty(fieldName, Byte.toString(response.getByte(fieldName)));
+ jsonObject.addProperty(fieldName, response.getObject(fieldName).toString());
} else if (response.getNumberOfValues(fieldName) > 1) {
JsonArray values = new JsonArray();
response.getAllBytes(fieldName).forEach(values::add);
jsonObject.add(fieldName, values);
}
});
- return jsonObject.toString().getBytes();
+ return jsonObject.toString();
}
public static void main(String[] args) throws Exception {