You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2018/11/01 20:17:54 UTC
[incubator-plc4x] 24/35: [OPM] Added MWE to HelloPlx4X Module.
This is an automated email from the ASF dual-hosted git repository.
jfeinauer pushed a commit to branch add-simple-mock-driver
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
commit dea5247f0ca7c0f0145735632395b7feb9de2289
Author: julian <j....@pragmaticminds.de>
AuthorDate: Thu Nov 1 12:05:20 2018 +0100
[OPM] Added MWE to HelloPlx4X Module.
---
examples/hello-world-plc4x/pom.xml | 12 +++
.../plc4x/java/examples/helloplc4x/HelloOpm.java | 109 +++++++++++++++++++++
2 files changed, 121 insertions(+)
diff --git a/examples/hello-world-plc4x/pom.xml b/examples/hello-world-plc4x/pom.xml
index a965de9..8572707 100644
--- a/examples/hello-world-plc4x/pom.xml
+++ b/examples/hello-world-plc4x/pom.xml
@@ -74,6 +74,18 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.plc4x</groupId>
+ <artifactId>plc4j-opm</artifactId>
+ <version>0.2.0-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.plc4x</groupId>
+ <artifactId>plc4j-connection-pool</artifactId>
+ <version>0.2.0-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
</dependencies>
<build>
diff --git a/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloOpm.java b/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloOpm.java
new file mode 100644
index 0000000..6ad1421
--- /dev/null
+++ b/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloOpm.java
@@ -0,0 +1,109 @@
+/*
+ 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.
+ */
+
+package org.apache.plc4x.java.examples.helloplc4x;
+
+import org.apache.plc4x.java.opm.OPMException;
+import org.apache.plc4x.java.opm.PlcEntity;
+import org.apache.plc4x.java.opm.PlcEntityManager;
+import org.apache.plc4x.java.opm.PlcField;
+import org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager;
+
+/**
+ * This Example shows how to use OPM from plc4j via the @{@link PlcEntityManager}.
+ * A @{@link PooledPlcDriverManager} is used to optimize the acces and to allow for automatic reconnection.
+ *
+ * The {@link PlcEntityManager} is similar to JPAs EntityManager.
+ * The "connected" Entity (shootCounter) can be kept and passed around and stays connected in the sense that all calls
+ * to a getter are forwared to the PLC.
+ * Finally, one can disconnect the Entity.
+ *
+ * This MT works against Tims S7 in NĂ¼rtingen.
+ * Thus, parameters have to be tuned possibly to get "good" values.
+ *
+ * @author julian
+ * Created by julian on 31.10.18
+ */
+public class HelloOpm {
+
+ private static final String ADDRESS = "s7://192.168.167.210/0/0";
+ private static final String PLC_FIELD_ADDRESS = "%DB225.DBW0:INT";
+ private final PlcEntityManager entityManager;
+
+ public static void main(String[] args) throws OPMException, InterruptedException {
+ HelloOpm helloOpm = new HelloOpm();
+ // Do a fetch via connected entity
+ helloOpm.readValueFromPlcUsingConnectedEntity();
+ // Do a fetch via read
+ helloOpm.readValueFromPlcUsingRead();
+ }
+
+ public HelloOpm() {
+ entityManager = new PlcEntityManager(new PooledPlcDriverManager());
+ }
+
+ /**
+ * The {@link PlcEntityManager#connect(Class, String)} method returns a "connected" Entity, i.e., a proxy Object.
+ * Whenever a getter is called on the Proxy object (whose Field Variable is annotated with @{@link PlcEntity}
+ * a call to the PLC is made to fetch the value.
+ * If another method is called on the Entity all Fields are feched from the Plc first, and then the method is
+ * invoked.
+ *
+ * @throws InterruptedException
+ * @throws OPMException
+ */
+ public void readValueFromPlcUsingConnectedEntity() throws InterruptedException, OPMException {
+ // Fetch connected Entity
+ DistanceSensor distanceSensor = entityManager.connect(DistanceSensor.class, ADDRESS);
+ // Read shoot values a hundred times
+ long distance = distanceSensor.getDistance();
+ System.out.println("Current distance: " + distance);
+ // Disconnect the Entity (not necessary)
+ entityManager.disconnect(distanceSensor);
+ }
+
+ /**
+ * The {@link PlcEntityManager#read(Class, String)} method fetches all fields annotated with @{@link PlcField}
+ * <b>once</b> and injects them in the new instance. After the constructing this is a regular POJO with no fancy
+ * functionality.
+ *
+ * @throws InterruptedException
+ * @throws OPMException
+ */
+ public void readValueFromPlcUsingRead() throws InterruptedException, OPMException {
+ // Read Entity from PLC
+ DistanceSensor distanceSensor = entityManager.read(DistanceSensor.class, ADDRESS);
+ System.out.println("Current distance: " + distanceSensor.getDistance());
+ }
+
+ /**
+ * Example entity which maps one field on a PLC where a Distance Sensor is connected.
+ */
+ @PlcEntity
+ public static class DistanceSensor {
+
+ @PlcField(PLC_FIELD_ADDRESS)
+ private long distance;
+
+ public long getDistance() {
+ return distance;
+ }
+
+ }
+}
\ No newline at end of file