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/10/24 20:30:03 UTC

[incubator-plc4x] 07/07: - Added List-Producers to the Edgent integration to support multi-value responses. - Adjusted the IotElasticsearchFactory to work after the latest changes to API and Edgent Integration

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 1a80a93b92a17259a686704dd15be9cfdb384d47
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Wed Oct 24 21:29:50 2018 +0100

    - Added List-Producers to the Edgent integration to support multi-value responses.
    - Adjusted the IotElasticsearchFactory to work after the latest changes to API and Edgent Integration
---
 .../iotfactory/IotElasticsearchFactory.java        | 27 ++++----
 .../apache/plc4x/edgent/PlcConnectionAdapter.java  | 79 ++++++++++++++++++++++
 .../java/org/apache/plc4x/edgent/PlcFunctions.java | 45 ++++++++++++
 3 files changed, 138 insertions(+), 13 deletions(-)

diff --git a/examples/iot-factory/src/main/java/org/apache/plc4x/java/examples/iotfactory/IotElasticsearchFactory.java b/examples/iot-factory/src/main/java/org/apache/plc4x/java/examples/iotfactory/IotElasticsearchFactory.java
index 35e78f4..7331be2 100644
--- a/examples/iot-factory/src/main/java/org/apache/plc4x/java/examples/iotfactory/IotElasticsearchFactory.java
+++ b/examples/iot-factory/src/main/java/org/apache/plc4x/java/examples/iotfactory/IotElasticsearchFactory.java
@@ -44,6 +44,7 @@ import java.io.IOException;
 import java.util.Calendar;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 public class IotElasticsearchFactory {
@@ -148,9 +149,9 @@ public class IotElasticsearchFactory {
 
             // Define the event stream.
             // 1) PLC4X source generating a stream of bytes.
-            Supplier<Byte> plcSupplier = PlcFunctions.byteSupplier(plcAdapter, "%Q0:BYTE");
+            Supplier<List<Boolean>> plcSupplier = PlcFunctions.booleanListSupplier(plcAdapter, "%Q0:BYTE");
             // 2) Use polling to get an item from the byte-stream in regular intervals.
-            TStream<Byte> plcOutputStates = top.poll(plcSupplier, 100, TimeUnit.MILLISECONDS);
+            TStream<List<Boolean>> plcOutputStates = top.poll(plcSupplier, 100, TimeUnit.MILLISECONDS);
 
             // 3a) Create a stream that pumps all data into a 'factory-data' index.
             TStream<XContentBuilder> factoryData = plcOutputStates.map(this::translatePlcInput);
@@ -172,14 +173,14 @@ public class IotElasticsearchFactory {
         }
     }
 
-    private XContentBuilder translatePlcInput(Byte input) {
-        boolean conveyorEntry = (input & 1) != 0;
-        boolean load = (input & 2) != 0;
-        boolean unload = (input & 4) != 0;
-        boolean transferLeft = (input & 8) != 0;
-        boolean transferRight = (input & 16) != 0;
-        boolean conveyorLeft = (input & 32) != 0;
-        boolean conveyorRight = (input & 64) != 0;
+    private XContentBuilder translatePlcInput(List<Boolean> input) {
+        boolean conveyorEntry = input.get(0);
+        boolean load = input.get(1);
+        boolean unload = input.get(2);
+        boolean transferLeft = input.get(3);
+        boolean transferRight = input.get(4);
+        boolean conveyorLeft = input.get(5);
+        boolean conveyorRight = input.get(6);
 
         try(XContentBuilder builder = XContentFactory.jsonBuilder()
             .startObject()
@@ -198,9 +199,9 @@ public class IotElasticsearchFactory {
         }
     }
 
-    private XContentBuilder handlePlcInput(Byte input) {
-        boolean transferLeft = (input & 8) != 0;
-        boolean transferRight = (input & 16) != 0;
+    private XContentBuilder handlePlcInput(List<Boolean> input) {
+        boolean transferLeft = input.get(3);
+        boolean transferRight = input.get(4);
 
         if (conveyorState == ConveyorState.STOPPED) {
             if (transferLeft) {
diff --git a/integrations/apache-edgent/src/main/java/org/apache/plc4x/edgent/PlcConnectionAdapter.java b/integrations/apache-edgent/src/main/java/org/apache/plc4x/edgent/PlcConnectionAdapter.java
index a52cd34..0f148d7 100644
--- a/integrations/apache-edgent/src/main/java/org/apache/plc4x/edgent/PlcConnectionAdapter.java
+++ b/integrations/apache-edgent/src/main/java/org/apache/plc4x/edgent/PlcConnectionAdapter.java
@@ -36,6 +36,8 @@ import org.slf4j.LoggerFactory;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
+import java.util.Collections;
+import java.util.List;
 
 /**
  * PlcConnectionAdapter encapsulates a plc4x {@link PlcConnection}.
@@ -129,6 +131,11 @@ public class PlcConnectionAdapter implements AutoCloseable {
         return new MySupplier<>(genericDatatype, clientDatatype, fieldQuery);
     }
 
+    <T> Supplier<List<T>> newListSupplier(Class<T> genericDatatype, PlcClientDatatype clientDatatype, String fieldQuery) {
+        // satisfy sonar's "Reduce number of anonymous class lines" code smell
+        return new MyListSupplier<>(genericDatatype, clientDatatype, fieldQuery);
+    }
+
     private class MySupplier<T> implements Supplier<T> {
 
         private static final long serialVersionUID = 1L;
@@ -154,6 +161,9 @@ public class PlcConnectionAdapter implements AutoCloseable {
                 PlcReadResponse readResponse = readRequest.execute().get();
                 Object value = null;
                 switch (clientDatatype) {
+                    case BOOLEAN:
+                        value = readResponse.getBoolean(FIELD_NAME);
+                        break;
                     case BYTE:
                         value = readResponse.getByte(FIELD_NAME);
                         break;
@@ -199,6 +209,75 @@ public class PlcConnectionAdapter implements AutoCloseable {
         }
     }
 
+    private class MyListSupplier<T> implements Supplier<List<T>> {
+
+        private static final long serialVersionUID = 1L;
+
+        private Class<T> genericDatatype;
+        private PlcClientDatatype clientDatatype;
+        private String fieldQuery;
+
+        MyListSupplier(Class<T> genericDatatype, PlcClientDatatype clientDatatype, String fieldQuery) {
+            this.genericDatatype = genericDatatype;
+            this.clientDatatype = clientDatatype;
+            this.fieldQuery = fieldQuery;
+        }
+
+        @Override
+        @SuppressWarnings("unchecked")
+        public List<T> get() {
+            PlcConnection connection = null;
+            PlcField field = null;
+            try {
+                connection = getConnection();
+                PlcReadRequest readRequest = connection.readRequestBuilder().orElseThrow(() -> new PlcException("This connection doesn't support reading")).addItem(FIELD_NAME, fieldQuery).build();
+                PlcReadResponse readResponse = readRequest.execute().get();
+                Object value = null;
+                switch (clientDatatype) {
+                    case BOOLEAN:
+                        value = readResponse.getAllBooleans(FIELD_NAME);
+                        break;
+                    case BYTE:
+                        value = readResponse.getAllBytes(FIELD_NAME);
+                        break;
+                    case SHORT:
+                        value = readResponse.getAllShorts(FIELD_NAME);
+                        break;
+                    case INTEGER:
+                        value = readResponse.getAllIntegers(FIELD_NAME);
+                        break;
+                    case LONG:
+                        value = readResponse.getAllLongs(FIELD_NAME);
+                        break;
+                    case FLOAT:
+                        value = readResponse.getAllFloats(FIELD_NAME);
+                        break;
+                    case DOUBLE:
+                        value = readResponse.getAllDoubles(FIELD_NAME);
+                        break;
+                    case STRING:
+                        value = readResponse.getAllStrings(FIELD_NAME);
+                        break;
+                    case TIME:
+                        value = readResponse.getAllTimes(FIELD_NAME);
+                        break;
+                    case DATE:
+                        value = readResponse.getAllDates(FIELD_NAME);
+                        break;
+                    case DATE_TIME:
+                        value = readResponse.getAllDateTimes(FIELD_NAME);
+                        break;
+                }
+                if (value != null) {
+                    return Collections.checkedList((List<T>) value, genericDatatype);
+                }
+            } catch (Exception e) {
+                logger.error("reading from plc device {} {} failed", connection, field, e);
+            }
+            return null;
+        }
+    }
+
     <T> Consumer<T> newJsonConsumer(Class<T> genericDatatype, PlcClientDatatype clientDatatype, String fieldQuery) {
         return new ObjectConsumer<>(genericDatatype, clientDatatype, fieldQuery);
     }
diff --git a/integrations/apache-edgent/src/main/java/org/apache/plc4x/edgent/PlcFunctions.java b/integrations/apache-edgent/src/main/java/org/apache/plc4x/edgent/PlcFunctions.java
index ab00c82..58e630d 100644
--- a/integrations/apache-edgent/src/main/java/org/apache/plc4x/edgent/PlcFunctions.java
+++ b/integrations/apache-edgent/src/main/java/org/apache/plc4x/edgent/PlcFunctions.java
@@ -29,6 +29,7 @@ import org.apache.plc4x.java.api.types.PlcClientDatatype;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
+import java.util.List;
 
 /**
  * WIP - A plc4x Apache Edgent {@link Supplier} and {@link Consumer} connector factory.
@@ -104,46 +105,90 @@ public class PlcFunctions {
         return adapter.newSupplier(Boolean.class, PlcClientDatatype.BOOLEAN, addressStr);
     }
 
+    public static Supplier<List<Boolean>> booleanListSupplier(PlcConnectionAdapter adapter, String addressStr) {
+        return adapter.newListSupplier(Boolean.class, PlcClientDatatype.BOOLEAN, addressStr);
+    }
+
     public static Supplier<Byte> byteSupplier(PlcConnectionAdapter adapter, String addressStr) {
         return adapter.newSupplier(Byte.class, PlcClientDatatype.BYTE, addressStr);
     }
 
+    public static Supplier<List<Byte>> byteListSupplier(PlcConnectionAdapter adapter, String addressStr) {
+        return adapter.newListSupplier(Byte.class, PlcClientDatatype.BYTE, addressStr);
+    }
+
     public static Supplier<Short> shortSupplier(PlcConnectionAdapter adapter, String addressStr) {
         return adapter.newSupplier(Short.class, PlcClientDatatype.SHORT, addressStr);
     }
 
+    public static Supplier<List<Short>> shortListSupplier(PlcConnectionAdapter adapter, String addressStr) {
+        return adapter.newListSupplier(Short.class, PlcClientDatatype.BYTE, addressStr);
+    }
+
     public static Supplier<Integer> integerSupplier(PlcConnectionAdapter adapter, String addressStr) {
         return adapter.newSupplier(Integer.class, PlcClientDatatype.INTEGER, addressStr);
     }
 
+    public static Supplier<List<Integer>> integerListSupplier(PlcConnectionAdapter adapter, String addressStr) {
+        return adapter.newListSupplier(Integer.class, PlcClientDatatype.BYTE, addressStr);
+    }
+
     public static Supplier<Long> longSupplier(PlcConnectionAdapter adapter, String addressStr) {
         return adapter.newSupplier(Long.class, PlcClientDatatype.LONG, addressStr);
     }
 
+    public static Supplier<List<Long>> longListSupplier(PlcConnectionAdapter adapter, String addressStr) {
+        return adapter.newListSupplier(Long.class, PlcClientDatatype.BYTE, addressStr);
+    }
+
     public static Supplier<Float> floatSupplier(PlcConnectionAdapter adapter, String addressStr) {
         return adapter.newSupplier(Float.class, PlcClientDatatype.FLOAT, addressStr);
     }
 
+    public static Supplier<List<Float>> floatListSupplier(PlcConnectionAdapter adapter, String addressStr) {
+        return adapter.newListSupplier(Float.class, PlcClientDatatype.BYTE, addressStr);
+    }
+
     public static Supplier<Double> doubleSupplier(PlcConnectionAdapter adapter, String addressStr) {
         return adapter.newSupplier(Double.class, PlcClientDatatype.DOUBLE, addressStr);
     }
 
+    public static Supplier<List<Double>> doubleListSupplier(PlcConnectionAdapter adapter, String addressStr) {
+        return adapter.newListSupplier(Double.class, PlcClientDatatype.BYTE, addressStr);
+    }
+
     public static Supplier<String> stringSupplier(PlcConnectionAdapter adapter, String addressStr) {
         return adapter.newSupplier(String.class, PlcClientDatatype.STRING, addressStr);
     }
 
+    public static Supplier<List<String>> stringListSupplier(PlcConnectionAdapter adapter, String addressStr) {
+        return adapter.newListSupplier(String.class, PlcClientDatatype.BYTE, addressStr);
+    }
+
     public static Supplier<LocalTime> timeSupplier(PlcConnectionAdapter adapter, String addressStr) {
         return adapter.newSupplier(LocalTime.class, PlcClientDatatype.TIME, addressStr);
     }
 
+    public static Supplier<List<LocalTime>> timeListSupplier(PlcConnectionAdapter adapter, String addressStr) {
+        return adapter.newListSupplier(LocalTime.class, PlcClientDatatype.BYTE, addressStr);
+    }
+
     public static Supplier<LocalDate> dateSupplier(PlcConnectionAdapter adapter, String addressStr) {
         return adapter.newSupplier(LocalDate.class, PlcClientDatatype.DATE, addressStr);
     }
 
+    public static Supplier<List<LocalDate>> dateListSupplier(PlcConnectionAdapter adapter, String addressStr) {
+        return adapter.newListSupplier(LocalDate.class, PlcClientDatatype.BYTE, addressStr);
+    }
+
     public static Supplier<LocalDateTime> dateTimeSupplier(PlcConnectionAdapter adapter, String addressStr) {
         return adapter.newSupplier(LocalDateTime.class, PlcClientDatatype.DATE_TIME, addressStr);
     }
 
+    public static Supplier<List<LocalDateTime>> dateTimeListSupplier(PlcConnectionAdapter adapter, String addressStr) {
+        return adapter.newListSupplier(LocalDateTime.class, PlcClientDatatype.BYTE, addressStr);
+    }
+
     public static Supplier<PlcReadResponse> batchSupplier(PlcConnectionAdapter adapter, PlcReadRequest readRequest) {
         return adapter.newSupplier(readRequest);
     }