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

tmitsch pushed a commit to branch Scraper_improvement
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit 8ab469648ad2837682eb55edcf7d0028f008027f
Author: Tim Mitsch <[email protected]>
AuthorDate: Tue Apr 9 18:13:12 2019 +0200

    PLC4X-109 implemented comparison to previous value for numeric values
---
 .../triggerhandler/TriggerConfiguration.java       | 52 ++++++++++++++++++----
 .../triggerhandler/TriggerConfigurationTest.java   |  4 +-
 .../test/resources/example_triggered_scraper.yml   |  9 +++-
 3 files changed, 54 insertions(+), 11 deletions(-)

diff --git 
a/plc4j/utils/scraper/src/main/java/org/apache/plc4x/java/scraper/triggeredscraper/triggerhandler/TriggerConfiguration.java
 
b/plc4j/utils/scraper/src/main/java/org/apache/plc4x/java/scraper/triggeredscraper/triggerhandler/TriggerConfiguration.java
index 8d7ba26..f4a6e6a 100644
--- 
a/plc4j/utils/scraper/src/main/java/org/apache/plc4x/java/scraper/triggeredscraper/triggerhandler/TriggerConfiguration.java
+++ 
b/plc4j/utils/scraper/src/main/java/org/apache/plc4x/java/scraper/triggeredscraper/triggerhandler/TriggerConfiguration.java
@@ -43,7 +43,8 @@ public class TriggerConfiguration{
     private static final double TOLERANCE_FLOATING_EQUALITY = 1e-6;
 
     private static final Pattern TRIGGER_STRATEGY_PATTERN =
-        
Pattern.compile("\\((?<strategy>[A-Z_0-9]+),(?<scheduledInterval>\\d+)(,(\\((?<triggerVar>\\S+)\\))((?<comp>[!=<>]{1,2}))(\\((?<compVar>[a-z0-9.\\-]+)\\)))?\\)");
+        
Pattern.compile("\\((?<strategy>[A-Z_0-9]+),(?<scheduledInterval>\\d+)(,(\\((?<triggerVar>\\S+)\\))((?<comp>[!=<>]{1,2}))(\\((?<compVar>[PREVa-z0-9.\\-]+)\\)))?\\)");
+    public static final String PREVIOUS_DEF = "PREV";
 
     private final TriggerType triggerType;
     private final Long scrapeInterval;
@@ -52,7 +53,10 @@ public class TriggerConfiguration{
     private Comparators comparatorType;
     private TriggeredScrapeJobImpl triggeredScrapeJobImpl;
 
-    private final Object compareValue;
+    //if trigger should be compared to previous value
+    private Boolean previousMode;
+
+    private Object compareValue;
     private final PlcField plcField;
 
     /**
@@ -71,6 +75,7 @@ public class TriggerConfiguration{
         this.scrapeInterval = parseScrapeInterval(scrapeInterval);
         this.triggerVariable = triggerVariable;
         this.comparator = comparator;
+        this.previousMode = false;
 
         if(this.triggerType.equals(TriggerType.S7_TRIGGER_VAR)) {
             //test for valid field-connection string, on exception quit job 
and return message to user
@@ -160,7 +165,7 @@ public class TriggerConfiguration{
             double currentValue;
             double refValue;
             try{
-                refValue = (double) compareValue;
+
                 if(value instanceof Short){
                     currentValue = ((Short) value).doubleValue();
                 }
@@ -182,6 +187,18 @@ public class TriggerConfiguration{
                     }
 
                 }
+                if(this.previousMode){
+                    if(this.compareValue==null){
+                        this.compareValue = currentValue;
+                        return true;
+                    }
+                    else{
+                        refValue = (double) compareValue;
+                    }
+                }
+                else {
+                    refValue = (double) compareValue;
+                }
 
                 //
 
@@ -191,20 +208,33 @@ public class TriggerConfiguration{
                 return false;
             }
 
+            boolean triggerResult;
             switch (this.comparatorType) {
                 case EQUAL:
-                    return isApproximately(currentValue,refValue, 
TOLERANCE_FLOATING_EQUALITY);
+                    triggerResult = isApproximately(currentValue,refValue, 
TOLERANCE_FLOATING_EQUALITY);
+                    break;
                 case UNEQUAL:
-                    return !isApproximately(currentValue,refValue, 
TOLERANCE_FLOATING_EQUALITY);
+                    triggerResult = !isApproximately(currentValue,refValue, 
TOLERANCE_FLOATING_EQUALITY);
+                    break;
                 case SMALLER:
-                    return currentValue < refValue;
+                    triggerResult = currentValue < refValue;
+                    break;
                 case SMALLER_EQUAL:
-                    return currentValue <= refValue;
+                    triggerResult = currentValue <= refValue;
+                    break;
                 case GREATER:
-                    return currentValue > refValue;
+                    triggerResult = currentValue > refValue;
+                    break;
                 case GREATER_EQUAL:
-                    return currentValue >= refValue;
+                    triggerResult = currentValue >= refValue;
+                    break;
+                default:
+                    triggerResult=false;
             }
+            if(triggerResult && previousMode){
+                this.compareValue = currentValue;
+            }
+            return triggerResult;
 
         }
         //should not happen, as fallback return false which always implies 
that no data is collected
@@ -306,6 +336,10 @@ public class TriggerConfiguration{
             try {
                 //everything fits to Double for conversion ... so for first 
step use only double
                 //ToDo if different handling dependent on specific datatype is 
needed then differ
+                if(PREVIOUS_DEF.equals(compareValue)){
+                    this.previousMode=true;
+                    return null;
+                }
                 return Double.parseDouble(compareValue);
             }
             catch (Exception e){
diff --git 
a/plc4j/utils/scraper/src/test/java/org/apache/plc4x/java/scraper/triggeredscraper/triggerhandler/TriggerConfigurationTest.java
 
b/plc4j/utils/scraper/src/test/java/org/apache/plc4x/java/scraper/triggeredscraper/triggerhandler/TriggerConfigurationTest.java
index 3bd4d98..8de11b2 100644
--- 
a/plc4j/utils/scraper/src/test/java/org/apache/plc4x/java/scraper/triggeredscraper/triggerhandler/TriggerConfigurationTest.java
+++ 
b/plc4j/utils/scraper/src/test/java/org/apache/plc4x/java/scraper/triggeredscraper/triggerhandler/TriggerConfigurationTest.java
@@ -52,6 +52,7 @@ class TriggerConfigurationTest {
             
Arguments.of("(S7_TRIGGER_VAR,50,(%DB111:DBD10:REAL)>(33.3))",TriggerConfiguration.TriggerType.S7_TRIGGER_VAR,
 50, TriggerConfiguration.Comparators.GREATER, 33.3),
             
Arguments.of("(S7_TRIGGER_VAR,50,(%DB111:DBD10:REAL)>(33.3))",TriggerConfiguration.TriggerType.S7_TRIGGER_VAR,
 50, TriggerConfiguration.Comparators.GREATER, 33.3),
             
Arguments.of("(S7_TRIGGER_VAR,50,(%DB111:DBD10:REAL)>(-1))",TriggerConfiguration.TriggerType.S7_TRIGGER_VAR,
 50, TriggerConfiguration.Comparators.GREATER, -1.0),
+            
Arguments.of("(S7_TRIGGER_VAR,50,(%DB111:DBD10:REAL)>(PREV))",TriggerConfiguration.TriggerType.S7_TRIGGER_VAR,
 50, TriggerConfiguration.Comparators.GREATER, null),
             
Arguments.of("(SCHEDULED,1000)",TriggerConfiguration.TriggerType.SCHEDULED, 
1000, null, null)
         );
     }
@@ -69,7 +70,8 @@ class TriggerConfigurationTest {
             Arguments.of("(S7_TRIGGER_VAR,50,(%DB111:DBX10.1:BOOL)<=(true))"),
             Arguments.of("(S7_TRIGGER_VAR,50,(%DB111:DBW10:INT)<=(true))"),
             Arguments.of("(MODBUS_TRIGGER_VAR,50)"),
-            Arguments.of("(MODBUS_TRIGGER_VAR,50,(%DB111:DBW10:INT)<=(11))")
+            Arguments.of("(MODBUS_TRIGGER_VAR,50,(%DB111:DBW10:INT)<=(11))"),
+            Arguments.of("(S7_TRIGGER_VAR,50,(%DB111:DBD10:REAL)>(prev))")
         );
     }
 
diff --git 
a/plc4j/utils/scraper/src/test/resources/example_triggered_scraper.yml 
b/plc4j/utils/scraper/src/test/resources/example_triggered_scraper.yml
index b3c0c75..9fa17f4 100644
--- a/plc4j/utils/scraper/src/test/resources/example_triggered_scraper.yml
+++ b/plc4j/utils/scraper/src/test/resources/example_triggered_scraper.yml
@@ -53,4 +53,11 @@ jobs:
       test9: '%DB811:DBX280:STRING'
       test10: '%DB811:DBB1000:BYTE[8]'
       test11: '%DB811:DBX268.3:BOOL'
-      test12: '%DB811:DBB270:BYTE[8]'
\ No newline at end of file
+      test12: '%DB811:DBB270:BYTE[8]'
+
+  - name: triggered-demo-job3_prev_value
+      triggerConfig: (S7_TRIGGER_VAR,100,(%M0:USINT)>=(PREV))
+      sources:
+        - S7_PI
+      fields:
+        test1: '%DB810:DBW0:INT'
\ No newline at end of file

Reply via email to