You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ze...@apache.org on 2020/05/29 20:40:22 UTC

[incubator-streampipes-extensions] branch dev updated: PLC4X S7 Extension: Importing config from Excel files

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

zehnder pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampipes-extensions.git


The following commit(s) were added to refs/heads/dev by this push:
     new 8b9ae52  PLC4X S7 Extension: Importing config from Excel files
     new e08dde8  Merge pull request #17 from Madabaru/dev
8b9ae52 is described below

commit 8b9ae52c549c2b19b4d15daabf1d0dfd79a01b9a
Author: Madabaru <jo...@axantu.com>
AuthorDate: Fri May 29 09:17:25 2020 +0200

    PLC4X S7 Extension: Importing config from Excel files
---
 pom.xml                                            |  7 +++
 streampipes-connect-adapters/pom.xml               |  4 ++
 .../connect/adapters/plc4x/s7/Plc4xS7Adapter.java  | 55 +++++++++++++++-------
 .../connect/adapters/plc4x/s7/S7ConfigFile.java    |  4 ++
 .../strings.en                                     | 14 ++++--
 5 files changed, 64 insertions(+), 20 deletions(-)

diff --git a/pom.xml b/pom.xml
index 3c45d59..a6b70e8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -128,6 +128,8 @@
         <protobuf.version>3.6.1</protobuf.version>
         <pulsar.version>2.5.1</pulsar.version>
         <slack.version>1.2.0</slack.version>
+        <poiji.version>2.7.0</poiji.version>
+
     </properties>
 
     <dependencyManagement>
@@ -293,6 +295,11 @@
                 <artifactId>error_prone_annotations</artifactId>
                 <version>${errorprone-annotations.version}</version>
             </dependency>
+            <dependency>
+                <groupId>com.github.ozlerhakan</groupId>
+                <artifactId>poiji</artifactId>
+                <version>${poiji.version}</version>
+            </dependency>
 
             <dependency>
                 <groupId>com.fasterxml.jackson.module</groupId>
diff --git a/streampipes-connect-adapters/pom.xml b/streampipes-connect-adapters/pom.xml
index 17a1a7e..838f799 100644
--- a/streampipes-connect-adapters/pom.xml
+++ b/streampipes-connect-adapters/pom.xml
@@ -194,6 +194,10 @@
                 <groupId>org.java-websocket</groupId>
                 <artifactId>Java-WebSocket</artifactId>
             </dependency>
+            <dependency>
+                <groupId>com.github.ozlerhakan</groupId>
+                <artifactId>poiji</artifactId>
+            </dependency>
 
             <!-- Test dependencies -->
             <dependency>
diff --git a/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/plc4x/s7/Plc4xS7Adapter.java b/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/plc4x/s7/Plc4xS7Adapter.java
index 4df1c1d..6f8d29a 100644
--- a/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/plc4x/s7/Plc4xS7Adapter.java
+++ b/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/plc4x/s7/Plc4xS7Adapter.java
@@ -18,6 +18,7 @@
 
 package org.apache.streampipes.connect.adapters.plc4x.s7;
 
+import com.poiji.bind.Poiji;
 import com.opencsv.CSVReader;
 import com.opencsv.bean.CsvToBean;
 import com.opencsv.bean.CsvToBeanBuilder;
@@ -30,7 +31,6 @@ import org.apache.plc4x.java.api.messages.PlcReadResponse;
 import org.apache.plc4x.java.api.types.PlcResponseCode;
 import org.apache.streampipes.connect.adapter.Adapter;
 import org.apache.streampipes.connect.adapter.exception.AdapterException;
-import org.apache.streampipes.connect.adapter.sdk.ParameterExtractor;
 import org.apache.streampipes.connect.adapter.util.PollingSettings;
 import org.apache.streampipes.connect.adapters.PullAdapter;
 import org.apache.streampipes.model.AdapterType;
@@ -53,17 +53,15 @@ import org.apache.streampipes.sdk.helpers.Options;
 import org.apache.streampipes.sdk.utils.Assets;
 import org.apache.streampipes.sdk.utils.Datatypes;
 
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
-import java.io.InputStreamReader;
-import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
+import java.io.File;
 
 public class Plc4xS7Adapter extends PullAdapter {
 
@@ -80,10 +78,12 @@ public class Plc4xS7Adapter extends PullAdapter {
     private static final String PLC_NODE_NAME = "plc_node_name";
     private static final String PLC_NODE_RUNTIME_NAME = "plc_node_runtime_name";
     private static final String PLC_NODE_TYPE = "plc_node_type";
-    private static final String PLC_NODES_FILE = "plc_nodes_file";
+    private static final String PLC_NODES_CSV_FILE = "plc_nodes_csv_file";
+    private static final String PLC_NODES_EXCEL_FILE = "plc_nodes_excel_file";
     private static final String CONFIGURE = "configure";
     private static final String MANUALLY = "manually";
-    private static final String CSV_UPLOAD = "csv-upload";
+    private static final String CSV_IMPORT = "csv_import";
+    private static final String EXCEL_IMPORT = "excel_import";
 
     /**
      * Values of user configuration parameters
@@ -127,8 +127,10 @@ public class Plc4xS7Adapter extends PullAdapter {
                                         StaticProperties.stringFreeTextProperty(Labels.withId(PLC_NODE_NAME)),
                                         StaticProperties.singleValueSelection(Labels.withId(PLC_NODE_TYPE),
                                                 Options.from("Bool",  "Byte", "Int", "Word", "Real")))),
-                        Alternatives.from(Labels.withId(CSV_UPLOAD),
-                                StaticProperties.fileProperty(Labels.withId(PLC_NODES_FILE))))
+                        Alternatives.from(Labels.withId(CSV_IMPORT),
+                                StaticProperties.fileProperty(Labels.withId(PLC_NODES_CSV_FILE))),
+                        Alternatives.from(Labels.withId(EXCEL_IMPORT),
+                                StaticProperties.fileProperty(Labels.withId(PLC_NODES_EXCEL_FILE))))
                 .build();
         description.setAppId(ID);
 
@@ -273,9 +275,9 @@ public class Plc4xS7Adapter extends PullAdapter {
         this.ip = extractor.singleValueParameter(PLC_IP, String.class);
 
         String selectedAlternative = extractor.selectedAlternativeInternalId(CONFIGURE);
-        if (selectedAlternative.equals(CSV_UPLOAD)) {
-            // csv file
-            FileStaticProperty sp = (FileStaticProperty) extractor.getStaticPropertyByName(PLC_NODES_FILE);
+        if (selectedAlternative.equals(CSV_IMPORT)) {
+            // CSV file
+            FileStaticProperty sp = (FileStaticProperty) extractor.getStaticPropertyByName(PLC_NODES_CSV_FILE);
             this.nodes = new ArrayList<>();
             try {
                 List<S7ConfigFile> configFiles = this.getCsvConfig(sp.getLocationPath());
@@ -287,12 +289,28 @@ public class Plc4xS7Adapter extends PullAdapter {
                     this.nodes.add(map);
                 }
             } catch (FileNotFoundException e) {
-                throw new AdapterException("Could not read uploaded file");
+                throw new AdapterException("Could not read imported file");
             }
 
+        } else if (selectedAlternative.equals(EXCEL_IMPORT)) {
+            // Excel file
+            FileStaticProperty sp = (FileStaticProperty) extractor.getStaticPropertyByName(PLC_NODES_EXCEL_FILE);
+            this.nodes = new ArrayList<>();
+            try {
+                List<S7ConfigFile> configFiles = this.getExcelConfig(sp.getLocationPath());
+                for (S7ConfigFile entry : configFiles) {
+                    Map map = new HashMap();
+                    map.put(PLC_NODE_RUNTIME_NAME, entry.getName());
+                    map.put(PLC_NODE_NAME, entry.getLogicalAddress());
+                    map.put(PLC_NODE_TYPE, entry.getDataType());
+                    this.nodes.add(map);
+                }
+            } catch (FileNotFoundException e) {
+                throw new AdapterException("Could not read imported file");
+            }
 
         } else {
-            // manually
+            // Manually
             this.nodes = new ArrayList<>();
             CollectionStaticProperty sp = (CollectionStaticProperty) extractor.getStaticPropertyByName(PLC_NODES);
 
@@ -308,6 +326,12 @@ public class Plc4xS7Adapter extends PullAdapter {
         }
     }
 
+
+    private List<S7ConfigFile> getExcelConfig(String path) throws FileNotFoundException {
+        List<S7ConfigFile> configFiles = Poiji.fromExcel(new File(path), S7ConfigFile.class);
+        return configFiles;
+    }
+
     private List<S7ConfigFile> getCsvConfig(String path) throws FileNotFoundException {
 
         FileReader fr = new FileReader(path);
@@ -324,14 +348,13 @@ public class Plc4xS7Adapter extends PullAdapter {
         strategy.setType(S7ConfigFile.class);
         strategy.setColumnMapping(mapping);
 
-
         CsvToBean<S7ConfigFile> csvToBean = new CsvToBeanBuilder(reader)
                 .withType(S7ConfigFile.class)
                 .withMappingStrategy(strategy)
                 .build();
 
-        List<S7ConfigFile> result = csvToBean.parse();
-        return result;
+        List<S7ConfigFile> configFiles = csvToBean.parse();
+        return configFiles;
     }
 
     /**
diff --git a/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/plc4x/s7/S7ConfigFile.java b/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/plc4x/s7/S7ConfigFile.java
index 814f371..645c7e9 100644
--- a/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/plc4x/s7/S7ConfigFile.java
+++ b/streampipes-connect-adapters/src/main/java/org/apache/streampipes/connect/adapters/plc4x/s7/S7ConfigFile.java
@@ -17,13 +17,17 @@
  */
 
 package org.apache.streampipes.connect.adapters.plc4x.s7;
+import com.poiji.annotation.ExcelCellName;
 
 public class S7ConfigFile {
 
+    @ExcelCellName("Name")
     private String name;
 
+    @ExcelCellName("Data Type")
     private String dataType;
 
+    @ExcelCellName("Logical Address")
     private String logicalAddress;
 
     public S7ConfigFile() {
diff --git a/streampipes-connect-adapters/src/main/resources/org.apache.streampipes.connect.adapters.plc4x.s7/strings.en b/streampipes-connect-adapters/src/main/resources/org.apache.streampipes.connect.adapters.plc4x.s7/strings.en
index 5615e66..c3fc57e 100644
--- a/streampipes-connect-adapters/src/main/resources/org.apache.streampipes.connect.adapters.plc4x.s7/strings.en
+++ b/streampipes-connect-adapters/src/main/resources/org.apache.streampipes.connect.adapters.plc4x.s7/strings.en
@@ -22,8 +22,14 @@ plc_node_name.description=example: %Q0.4
 plc_node_type.title=Data Type
 plc_node_type.description=example: bool
 
-csv-upload.title=Upload CSV
-csv-upload.description=Export a CSV file with the configurations from the TIA portal
+csv_import.title=Import from CSV
+csv_import.description=Import a CSV file with the configurations from the TIA portal
 
-plc_nodes_file.title=CSV File
-plc_nodes_file.description=Contains three columns 'Name', 'Data Type', 'Logical Address'
+excel_import.title= Import from Excel file
+excel_import.description=Import a Excel file with the configurations from the TIA portal
+
+plc_nodes_csv_file.title=CSV File
+plc_nodes_csv_file.description=Contains three columns 'Name', 'Data Type', 'Logical Address'
+
+plc_nodes_excel_file.title=Excel File
+plc_nodes_excel_file.description=Contains three columns 'Name', 'Data Type', 'Logical Address'
\ No newline at end of file