asfgit closed pull request #11: Implement basic example of connecting an S7 
device to Azure IoT Hub
URL: https://github.com/apache/incubator-plc4x/pull/11
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/examples/azure/pom.xml b/examples/azure/pom.xml
new file mode 100644
index 000000000..0e010a9c1
--- /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 000000000..0e183c3c0
--- /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 574edcdd6..5e6379431 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>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to