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 2020/12/10 14:30:58 UTC

[plc4x] branch develop updated: - Fixed a problem not finishing the request if a decoding error occurs - Made the date parsing robust against 0 values - Hopefully fixed the read-array bug

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

cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new b61937f  - Fixed a problem not finishing the request if a decoding error occurs - Made the date parsing robust against 0 values - Hopefully fixed the read-array bug
b61937f is described below

commit b61937f40af1ef72c84aee5606f25ca0eda7d3e8
Author: cdutz <ch...@c-ware.de>
AuthorDate: Thu Dec 10 15:30:47 2020 +0100

    - Fixed a problem not finishing the request if a decoding error occurs
    - Made the date parsing robust against 0 values
    - Hopefully fixed the read-array bug
---
 .../src/main/resources/templates/java/data-io-template.ftlh  |  6 +++---
 .../apache/plc4x/java/s7/readwrite/ManualS7DriverTest.java   |  8 ++++++++
 .../plc4x/java/spi/messages/DefaultPlcReadResponse.java      | 12 +++++++-----
 .../org/apache/plc4x/java/spi/optimizer/BaseOptimizer.java   |  6 +++++-
 .../apache/plc4x/java/spi/values/IEC61131ValueHandler.java   |  2 ++
 .../main/java/org/apache/plc4x/test/manual/ManualTest.java   |  4 +++-
 6 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/build-utils/language-java/src/main/resources/templates/java/data-io-template.ftlh b/build-utils/language-java/src/main/resources/templates/java/data-io-template.ftlh
index 1aec527..a1a1e12 100644
--- a/build-utils/language-java/src/main/resources/templates/java/data-io-template.ftlh
+++ b/build-utils/language-java/src/main/resources/templates/java/data-io-template.ftlh
@@ -253,7 +253,7 @@ public class ${type.name}IO {
                     <#break>
                     <#case "DATE">
                     <#if helper.hasFieldsWithNames(case.fields, "year", "month", "day")>
-            LocalDate value = LocalDate.of(year.intValue(), month.intValue(), day.intValue());
+            LocalDate value = LocalDate.of(year.intValue(), (month == 0) ? 1 : month.intValue(), (day == 0) ? 1 : day.intValue());
                     </#if>
             return new PlcDATE(value);
                     <#break>
@@ -267,9 +267,9 @@ public class ${type.name}IO {
                     <#break>
                     <#case "DATE_AND_TIME">
                     <#if helper.hasFieldsWithNames(case.fields, "year", "month", "day", "hour", "minutes", "seconds", "nanos")>
-            LocalDateTime value = LocalDateTime.of(year.intValue(), month.intValue(), day.intValue(), hour.intValue(), minutes.intValue(), seconds.intValue(), nanos.intValue());
+            LocalDateTime value = LocalDateTime.of(year.intValue(), (month == 0) ? 1 : month.intValue(), (day == 0) ? 1 : day.intValue(), hour.intValue(), minutes.intValue(), seconds.intValue(), nanos.intValue());
                     <#elseif helper.hasFieldsWithNames(case.fields, "year", "month", "day", "hour", "minutes", "seconds")>
-            LocalDateTime value = LocalDateTime.of(year.intValue(), month.intValue(), day.intValue(), hour.intValue(), minutes.intValue(), seconds.intValue());
+            LocalDateTime value = LocalDateTime.of(year.intValue(), (month == 0) ? 1 : month.intValue(), (day == 0) ? 1 : day.intValue(), hour.intValue(), minutes.intValue(), seconds.intValue());
                     </#if>
             return new PlcDATE_AND_TIME(value);
                     <#break>
diff --git a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/readwrite/ManualS7DriverTest.java b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/readwrite/ManualS7DriverTest.java
index 7a0cec9..3cdc4e0 100644
--- a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/readwrite/ManualS7DriverTest.java
+++ b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/readwrite/ManualS7DriverTest.java
@@ -79,18 +79,26 @@ public class ManualS7DriverTest extends ManualTest {
         test.addTestCase("%DB4:20:UINT", 42424);
         test.addTestCase("%DB4:22:DINT", -242442424);
         test.addTestCase("%DB4:26:UDINT", 4242442424L);
+        // Not supported in S7 1200
         //test.addTestCase("%DB4:30:LINT", -4242442424242424242L);
+        // Not supported in S7 1200
         //test.addTestCase("%DB4:38:ULINT", 4242442424242424242L);
         test.addTestCase("%DB4:46:REAL", 3.141593F);
+        // Not supported in S7 1200
         //test.addTestCase("%DB4:50:LREAL", 2.71828182846D);
         test.addTestCase("%DB4:58:TIME", "PT1.234S");
+        // Not supported in S7 1200
         //test.addTestCase("%DB4:62:LTIME", "PT24015H23M12.034002044S");
         test.addTestCase("%DB4:70:DATE", "1998-03-28");
         test.addTestCase("%DB4:72:TIME_OF_DAY", "15:36:30.123");
         test.addTestCase("%DB4:76:TOD", "16:17:18.123");
+        // Not supported in S7 1200
         //test.addTestCase("%DB4:96:DATE_AND_TIME", "1996-05-06T15:36:30");
+        // Not supported in S7 1200
         //test.addTestCase("%DB4:104:DT", "1992-03-29T00:00");
+        // Not supported in S7 1200
         //test.addTestCase("%DB4:112:LDATE_AND_TIME", "1978-03-28T15:36:30");
+        // Not supported in S7 1200
         //test.addTestCase("%DB4:124:LDT", "1978-03-28T15:36:30");
         test.addTestCase("%DB4:136:CHAR", "H");
         test.addTestCase("%DB4:138:WCHAR", "w");
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadResponse.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadResponse.java
index 6eb5250..1e6b24e 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadResponse.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadResponse.java
@@ -28,7 +28,7 @@ import org.apache.plc4x.java.api.types.PlcResponseCode;
 import org.apache.plc4x.java.spi.utils.XmlSerializable;
 import org.apache.plc4x.java.spi.values.PlcList;
 import org.apache.plc4x.java.api.value.PlcValue;
-import org.apache.plc4x.java.spi.values.PlcValues;
+import org.apache.plc4x.java.spi.values.PlcStruct;
 import org.apache.plc4x.java.spi.messages.utils.ResponseItem;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -39,8 +39,6 @@ import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
 import java.util.*;
-import java.util.function.Function;
-import java.util.stream.Collectors;
 
 @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "className")
 public class DefaultPlcReadResponse implements PlcReadResponse, XmlSerializable {
@@ -63,8 +61,12 @@ public class DefaultPlcReadResponse implements PlcReadResponse, XmlSerializable
     @Override
     @JsonIgnore
     public PlcValue getAsPlcValue() {
-        return PlcValues.of(request.getFieldNames().stream()
-            .collect(Collectors.toMap(Function.identity(), this::getPlcValue)));
+        Map<String, PlcValue> structMap = new HashMap<>();
+        for (String fieldName : request.getFieldNames()) {
+            PlcValue plcValue = getPlcValue(fieldName);
+            structMap.put(fieldName, plcValue);
+        }
+        return new PlcStruct(structMap);
     }
 
     @Override
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/optimizer/BaseOptimizer.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/optimizer/BaseOptimizer.java
index 2e48fd2..0544b24 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/optimizer/BaseOptimizer.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/optimizer/BaseOptimizer.java
@@ -163,13 +163,17 @@ public abstract class BaseOptimizer {
                     } catch (InterruptedException e) {
                         Thread.currentThread().interrupt();
                         results.put(subRequest, Either.right(new Exception("Something went wrong")));
-                    } catch (ExecutionException e) {
+                    } catch (Exception e) {
                         results.put(subRequest, Either.right(new Exception("Something went wrong")));
                     }
                 }
                 PlcResponse response = responseProcessor.apply(results);
                 parentFuture.complete(response);
                 return Void.TYPE;
+            }).exceptionally(throwable -> {
+                // TODO: If would be cool if we could still process all of the successful ones ...
+                parentFuture.completeExceptionally(throwable);
+                return null;
             });
             return parentFuture;
         } else {
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/IEC61131ValueHandler.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/IEC61131ValueHandler.java
index 3452695..f8e6ba8 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/IEC61131ValueHandler.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/IEC61131ValueHandler.java
@@ -85,6 +85,8 @@ public class IEC61131ValueHandler implements PlcValueHandler {
                 return new PlcDATE_AND_TIME((LocalDateTime) value);
             } else if (value instanceof String) {
                 return new PlcSTRING((String) value);
+            } else if (value instanceof PlcValue) {
+                return (PlcValue) value;
             } else {
                 throw new PlcUnsupportedDataTypeException("Data Type " + value.getClass()
                     + " Is not supported");
diff --git a/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/manual/ManualTest.java b/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/manual/ManualTest.java
index 43081e3..cb88745 100644
--- a/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/manual/ManualTest.java
+++ b/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/manual/ManualTest.java
@@ -105,7 +105,7 @@ public abstract class ManualTest {
                     String fieldName = testCase.address;
                     Assertions.assertEquals(PlcResponseCode.OK, readResponse.getResponseCode(fieldName));
                     Assertions.assertNotNull(readResponse.getPlcValue(fieldName));
-                    if(readResponse.getPlcValue(fieldName) instanceof PlcList) {
+                    if (readResponse.getPlcValue(fieldName) instanceof PlcList) {
                         PlcList plcList = (PlcList) readResponse.getPlcValue(fieldName);
                         List<Object> expectedValues = (List<Object>) testCase.expectedReadValue;
                         for (int j = 0; j < expectedValues.size(); j++) {
@@ -118,6 +118,8 @@ public abstract class ManualTest {
                 }
             }
             System.out.println("Success");
+        } catch (Exception e) {
+            Assertions.fail(e);
         }
     }