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/08/13 15:08:56 UTC

[incubator-plc4x] 01/04: Implement basic example of connecting an S7 device to Azure IoT Hub

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

cdutz pushed a commit to branch DerDackel-s7_azure_iothub_example
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit 5202c9df366972a206777f33dca0e4737874a8d5
Author: Sebastian Jackel <se...@codecentric.de>
AuthorDate: Fri Aug 10 14:48:01 2018 +0200

    Implement basic example of connecting an S7 device to Azure IoT Hub
---
 examples/azure/pom.xml                             | 38 +++++++++++
 .../azure/iothub/S7PlcToAzureIoTHubSample.java     | 76 ++++++++++++++++++++++
 examples/pom.xml                                   |  1 +
 3 files changed, 115 insertions(+)

diff --git a/examples/azure/pom.xml b/examples/azure/pom.xml
new file mode 100644
index 0000000..0e010a9
--- /dev/null
+++ b/examples/azure/pom.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <parent>
+    <artifactId>examples</artifactId>
+    <groupId>org.apache.plc4x.examples</groupId>
+    <version>0.0.1-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>azure</artifactId>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-api</artifactId>
+      <version>0.0.1-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-core</artifactId>
+      <version>0.0.1-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-protocol-s7</artifactId>
+      <version>0.0.1-SNAPSHOT</version>
+    </dependency>
+
+    <dependency>
+      <groupId>com.microsoft.azure.sdk.iot</groupId>
+      <artifactId>iot-device-client</artifactId>
+      <version>1.3.30</version>
+    </dependency>
+  </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/examples/azure/src/main/java/org/apache/plc4x/java/examples/azure/iothub/S7PlcToAzureIoTHubSample.java b/examples/azure/src/main/java/org/apache/plc4x/java/examples/azure/iothub/S7PlcToAzureIoTHubSample.java
new file mode 100644
index 0000000..0e183c3
--- /dev/null
+++ b/examples/azure/src/main/java/org/apache/plc4x/java/examples/azure/iothub/S7PlcToAzureIoTHubSample.java
@@ -0,0 +1,76 @@
+package org.apache.plc4x.java.examples.azure.iothub;
+
+import com.microsoft.azure.sdk.iot.device.*;
+import org.apache.plc4x.java.PlcDriverManager;
+import org.apache.plc4x.java.api.connection.PlcConnection;
+import org.apache.plc4x.java.api.connection.PlcReader;
+import org.apache.plc4x.java.api.messages.specific.TypeSafePlcReadRequest;
+import org.apache.plc4x.java.api.messages.specific.TypeSafePlcReadResponse;
+import org.apache.plc4x.java.api.model.Address;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Optional;
+
+public class S7PlcToAzureIoTHubSample {
+
+    private static final Logger logger = LoggerFactory.getLogger(S7PlcToAzureIoTHubSample.class);
+
+    private static IotHubClientProtocol protocol = IotHubClientProtocol.MQTT;
+    private static DeviceClient client;
+
+    public static class Callback implements IotHubEventCallback {
+
+        @Override
+        public void execute(IotHubStatusCode iotHubStatusCode, Object ctx) {
+            System.out.println("Received status: " + iotHubStatusCode.name());
+        }
+    }
+
+    /**
+     * Example code do demonstrate sending events from an S7 device to Microsoft Azure IoT Hub
+     *
+     * @param args Expected: [device address, IoT-Hub connection string].
+     */
+    public static void main(String[] args) {
+        logger.info("Connecting");
+        try (PlcConnection plcConnection = new PlcDriverManager().getConnection(args[0])) {
+            logger.info("Connected");
+
+            client = new DeviceClient(args[1], protocol);
+            client.open();
+
+            Optional<PlcReader> reader = plcConnection.getReader();
+
+            if (reader.isPresent()) {
+                PlcReader plcReader = reader.get();
+                Address outputs = plcConnection.parseAddress("OUTPUTS/0");
+
+                while (true) {
+                    // Simulate telemetry.
+                    TypeSafePlcReadResponse<Byte> plcReadResponse = plcReader.read(new TypeSafePlcReadRequest<>(Byte.class, outputs)).get();
+
+                    System.out.println("Outputs: " + Long.toBinaryString(plcReadResponse.getResponseItem()
+                        .orElseThrow(() -> new IllegalStateException("No response available"))
+                        .getValues().get(0).longValue()));
+
+                    plcReadResponse.getResponseItem().map(byt -> {
+                            String result = Long.toBinaryString(byt.getValues().get(0).longValue());
+                            Message msg = new Message("{ \"bits\" : \"" + result + "\"}");
+                            // Send the message.
+                            Callback callback = new Callback();
+
+                            client.sendEventAsync(msg, callback, new Object());
+                            return byt;
+                        }
+                    );
+
+                    Thread.sleep(1000);
+                }
+            }
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}
diff --git a/examples/pom.xml b/examples/pom.xml
index 574edcd..5e63794 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -41,6 +41,7 @@
     <module>iotree</module>
     <module>kafka-bridge</module>
     <module>plclogger</module>
+    <module>azure</module>
   </modules>
 
   <dependencyManagement>