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 2022/05/25 21:59:49 UTC

[plc4x] branch develop updated: chore(plc4j/example): Updated the azure iot hub example to the latest sdk version.

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new a5e7636bda chore(plc4j/example): Updated the azure iot hub example to the latest sdk version.
a5e7636bda is described below

commit a5e7636bda1a05dafa9b766f57d09a9b08e60ac7
Author: cdutz <ch...@c-ware.de>
AuthorDate: Wed May 25 23:59:43 2022 +0200

    chore(plc4j/example): Updated the azure iot hub example to the latest sdk version.
---
 plc4j/examples/hello-cloud-azure/pom.xml            |  3 +--
 .../cloud/azure/S7PlcToAzureIoTHubSample.java       | 21 ++++++++++++++++-----
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/plc4j/examples/hello-cloud-azure/pom.xml b/plc4j/examples/hello-cloud-azure/pom.xml
index 08649bda02..c8d3f0b416 100644
--- a/plc4j/examples/hello-cloud-azure/pom.xml
+++ b/plc4j/examples/hello-cloud-azure/pom.xml
@@ -43,8 +43,7 @@
     <dependency>
       <groupId>com.microsoft.azure.sdk.iot</groupId>
       <artifactId>iot-device-client</artifactId>
-      <!-- Remark: 2.0.3 is the latest version, but due to API changes, the code needs to be refactored -->
-      <version>1.34.3</version>
+      <version>2.0.3</version>
     </dependency>
 
     <dependency>
diff --git a/plc4j/examples/hello-cloud-azure/src/main/java/org/apache/plc4x/java/examples/cloud/azure/S7PlcToAzureIoTHubSample.java b/plc4j/examples/hello-cloud-azure/src/main/java/org/apache/plc4x/java/examples/cloud/azure/S7PlcToAzureIoTHubSample.java
index 31cb9f0053..ffedf72c1d 100644
--- a/plc4j/examples/hello-cloud-azure/src/main/java/org/apache/plc4x/java/examples/cloud/azure/S7PlcToAzureIoTHubSample.java
+++ b/plc4j/examples/hello-cloud-azure/src/main/java/org/apache/plc4x/java/examples/cloud/azure/S7PlcToAzureIoTHubSample.java
@@ -52,12 +52,13 @@ public class S7PlcToAzureIoTHubSample {
         LOGGER.info("Connecting {}, {}, {}", options.getPlc4xConnectionString(), options.getPlc4xFieldAddress(),
             options.getIotHubConnectionString());
 
-        // Open both a connection to the remote PLC as well as a connection to the cloud service.
-        try (PlcConnection plcConnection = new PlcDriverManager().getConnection(options.getPlc4xConnectionString());
-             DeviceClient client = new DeviceClient(options.getIotHubConnectionString(), IotHubClientProtocol.MQTT)) {
+        // Open both a connection to the remote PLC and the cloud service.
+        DeviceClient client = new DeviceClient(options.getIotHubConnectionString(), IotHubClientProtocol.MQTT);
+        try (PlcConnection plcConnection = new PlcDriverManager().getConnection(options.getPlc4xConnectionString())) {
+
             LOGGER.info("Connected");
 
-            client.open();
+            client.open(true);
 
             // Prepare a read request.
             PlcReadRequest request = plcConnection.readRequestBuilder()
@@ -73,13 +74,23 @@ public class S7PlcToAzureIoTHubSample {
                             Message msg = new Message("{ \"bits\" : \"" + result + "\"}");
 
                             // Send the message.
-                            client.sendEventAsync(msg, (responseStatus, callbackContext) -> LOGGER.info("Received status: ", responseStatus), new Object());
+                            client.sendEventAsync(msg,
+                                (sentMessage, clientException, callbackContext) -> {
+                                    if(clientException != null) {
+                                        LOGGER.info("Received exception: ", clientException);
+                                    } else {
+                                        LOGGER.info("Sent successfully");
+                                    }
+                                },
+                                msg);
                         }
                     );
 
                 // Wait a second.
                 TimeUnit.SECONDS.sleep(1);
             }
+        } finally {
+            client.close();
         }
     }