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/05/22 15:59:41 UTC

[incubator-plc4x] 03/05: Added a new example using the iot-labs virtual factory.

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

commit 1e70443c001168eb8f65eadc9691b834fb88509b
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Fri May 18 16:44:43 2018 +0200

    Added a new example using the iot-labs virtual factory.
    
    (cherry picked from commit df9536a)
---
 examples/iot-factory/pom.xml                       | 100 +++++++++++++++++++++
 .../plc4x/java/examples/iotfactory/IotFactory.java |  86 ++++++++++++++++++
 .../iot-factory/src/main/resources/logback.xml     |  40 +++++++++
 examples/pom.xml                                   |  18 ++++
 4 files changed, 244 insertions(+)

diff --git a/examples/iot-factory/pom.xml b/examples/iot-factory/pom.xml
new file mode 100644
index 0000000..900c83d
--- /dev/null
+++ b/examples/iot-factory/pom.xml
@@ -0,0 +1,100 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+-->
+<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">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.plc4x.examples</groupId>
+    <artifactId>examples</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>iot-factory</artifactId>
+  <name>Examples: IoT Factory</name>
+  <description>Client application demonstrating PLC4X in our virtual IoT Factory.</description>
+
+  <properties>
+    <app.main.class>org.apache.plc4x.java.examples.iotfactory.IotFactory</app.main.class>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>apache-edgent</artifactId>
+      <version>0.0.1-SNAPSHOT</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.edgent</groupId>
+      <artifactId>edgent-api-function</artifactId>
+      <version>1.2.0</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.edgent</groupId>
+      <artifactId>edgent-api-topology</artifactId>
+      <version>1.2.0</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.edgent</groupId>
+      <artifactId>edgent-providers-direct</artifactId>
+      <version>1.2.0</version>
+    </dependency>
+
+    <!-- Required driver implementation -->
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-protocol-s7</artifactId>
+      <version>0.0.1-SNAPSHOT</version>
+      <scope>runtime</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>log4j-over-slf4j</artifactId>
+      <version>1.7.25</version>
+    </dependency>
+    <dependency>
+      <groupId>ch.qos.logback</groupId>
+      <artifactId>logback-classic</artifactId>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-dependency-plugin</artifactId>
+        <configuration>
+          <usedDependencies>
+            <usedDependency>org.apache.plc4x:plc4j-protocol-s7</usedDependency>
+            <usedDependency>org.slf4j:log4j-over-slf4j</usedDependency>
+          </usedDependencies>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>
\ No newline at end of file
diff --git a/examples/iot-factory/src/main/java/org/apache/plc4x/java/examples/iotfactory/IotFactory.java b/examples/iot-factory/src/main/java/org/apache/plc4x/java/examples/iotfactory/IotFactory.java
new file mode 100644
index 0000000..91f6d26
--- /dev/null
+++ b/examples/iot-factory/src/main/java/org/apache/plc4x/java/examples/iotfactory/IotFactory.java
@@ -0,0 +1,86 @@
+package org.apache.plc4x.java.examples.iotfactory;/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+import org.apache.edgent.function.Supplier;
+import org.apache.edgent.providers.direct.DirectProvider;
+import org.apache.edgent.topology.TStream;
+import org.apache.edgent.topology.Topology;
+import org.apache.plc4x.edgent.PlcConnectionAdapter;
+import org.apache.plc4x.edgent.PlcFunctions;
+
+import java.util.concurrent.TimeUnit;
+
+public class IotFactory {
+
+    public enum ConveyorState {
+        STOPPED,
+        RUNNING_LEFT,
+        RUNNING_RIGHT
+    };
+
+    private static int smallBoxes = 0;
+    private static int largeBoxes = 0;
+    private static ConveyorState conveyorState = ConveyorState.STOPPED;
+
+
+    public static void main(String[] args) throws Exception {
+        // Get a plc connection.
+        try (PlcConnectionAdapter plcAdapter = new PlcConnectionAdapter("s7://10.10.64.20/1/1")) {
+            // Initialize the Edgent core.
+            DirectProvider dp = new DirectProvider();
+            Topology top = dp.newTopology();
+
+            // Define the event stream.
+            // 1) PLC4X source generating a stream of bytes.
+            Supplier<Byte> plcSupplier = PlcFunctions.byteSupplier(plcAdapter, "OUTPUTS/0");
+            // 2) Use polling to get an item from the byte-stream in regular intervals.
+            TStream<Byte> source = top.poll(plcSupplier, 100, TimeUnit.MILLISECONDS);
+            // 3) Output the events in the stream on the console.
+
+            source.sink(value -> {
+                boolean runningLeft = (value & 8) != 0;
+                boolean runningRight = (value & 16) != 0;
+
+                if(conveyorState == ConveyorState.STOPPED) {
+                    if(runningLeft | runningRight) {
+                        if (runningLeft) {
+                            smallBoxes++;
+                            conveyorState = ConveyorState.RUNNING_LEFT;
+                            updateOutput();
+                        } else {
+                            largeBoxes++;
+                            conveyorState = ConveyorState.RUNNING_RIGHT;
+                            updateOutput();
+                        }
+                    }
+                } else if (!(runningLeft | runningRight)){
+                    conveyorState = ConveyorState.STOPPED;
+                }
+            });
+
+            // Submit the topology and hereby start the event streams.
+            dp.submit(top);
+        }
+    }
+
+    private static void updateOutput() {
+        System.out.println(String.format("Small Boxes: %3d, Large Boxes %3d", smallBoxes, largeBoxes));
+    }
+
+}
diff --git a/examples/iot-factory/src/main/resources/logback.xml b/examples/iot-factory/src/main/resources/logback.xml
new file mode 100644
index 0000000..71652b3
--- /dev/null
+++ b/examples/iot-factory/src/main/resources/logback.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+-->
+<configuration xmlns="http://ch.qos.logback/xml/ns/logback"
+               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+               xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback https://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd">
+
+  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+    <!-- encoders are assigned the type
+         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
+    <encoder>
+      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
+    </encoder>
+  </appender>
+
+  <logger level="OFF" name="io.netty.util" additivity="false">
+    <appender-ref ref="STDOUT" />
+  </logger>
+
+  <root level="warn">
+    <appender-ref ref="STDOUT" />
+  </root>
+
+</configuration>
\ No newline at end of file
diff --git a/examples/pom.xml b/examples/pom.xml
index 88e92bd..574edcd 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -37,11 +37,29 @@
 
   <modules>
     <module>dummy-driver</module>
+    <module>iot-factory</module>
     <module>iotree</module>
     <module>kafka-bridge</module>
     <module>plclogger</module>
   </modules>
 
+  <dependencyManagement>
+    <dependencies>
+      <dependency>
+        <groupId>ch.qos.logback</groupId>
+        <artifactId>logback-core</artifactId>
+        <version>${logback.version}</version>
+        <scope>compile</scope>
+      </dependency>
+      <dependency>
+        <groupId>ch.qos.logback</groupId>
+        <artifactId>logback-classic</artifactId>
+        <version>${logback.version}</version>
+        <scope>compile</scope>
+      </dependency>
+    </dependencies>
+  </dependencyManagement>
+
   <profiles>
     <profile>
       <id>build-executable-jars</id>

-- 
To stop receiving notification emails like this one, please contact
cdutz@apache.org.