]> git.basschouten.com Git - openhab-addons.git/commitdiff
[pidcontroller] Fix for handling trigger input in action (#9842)
authorHilbrand Bouwkamp <hilbrand@h72.nl>
Tue, 19 Jan 2021 09:34:49 +0000 (10:34 +0100)
committerGitHub <noreply@github.com>
Tue, 19 Jan 2021 09:34:49 +0000 (10:34 +0100)
* [pidcontroller] Catch empty commandTopic

Signed-off-by: Hilbrand Bouwkamp <hilbrand@h72.nl>
* [pidcontroller] Fix handling action

keys in action context from trigger are passed with prefix of trigger  name.
This change removes the prefix to get the actual name and checks if it matches an item.
Else it tries the original name.

Signed-off-by: Hilbrand Bouwkamp <hilbrand@h72.nl>
* [pidcontroller] review comment

Signed-off-by: Hilbrand Bouwkamp <hilbrand@h72.nl>
bundles/org.openhab.automation.pidcontroller/src/main/java/org/openhab/automation/pidcontroller/internal/handler/PIDControllerActionHandler.java
bundles/org.openhab.automation.pidcontroller/src/main/java/org/openhab/automation/pidcontroller/internal/handler/PIDControllerTriggerHandler.java

index 11bcb378555448d322cbe699804d5a59cb984280..de9c7030ca831d3f5f5f451f415f8119af4d864b 100644 (file)
  */
 package org.openhab.automation.pidcontroller.internal.handler;
 
-import static org.openhab.automation.pidcontroller.internal.PIDControllerConstants.*;
+import static org.openhab.automation.pidcontroller.internal.PIDControllerConstants.AUTOMATION_NAME;
 
 import java.math.BigDecimal;
 import java.util.Map;
-import java.util.stream.Stream;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.core.automation.Action;
 import org.openhab.core.automation.handler.ActionHandler;
 import org.openhab.core.automation.handler.BaseModuleHandler;
+import org.openhab.core.config.core.Configuration;
 import org.openhab.core.events.EventPublisher;
 import org.openhab.core.items.ItemRegistry;
 import org.openhab.core.items.events.ItemCommandEvent;
@@ -53,16 +53,22 @@ public class PIDControllerActionHandler extends BaseModuleHandler<Action> implem
 
     @Override
     public @Nullable Map<String, Object> execute(Map<String, Object> context) {
-        Stream.of(OUTPUT, P_INSPECTOR, I_INSPECTOR, D_INSPECTOR, E_INSPECTOR).forEach(arg -> {
-            final String itemName = (String) module.getConfiguration().get(arg);
+        final Configuration configuration = module.getConfiguration();
+
+        context.forEach((k, v) -> {
+            // Remove triggername from key to get raw trigger param
+            String itemKey = k.substring(k.lastIndexOf('.') + 1);
+            String itemName = (String) configuration.get(itemKey);
 
             if (itemName == null || itemName.isBlank()) {
-                return;
+                // try original key name (<triggername>.<trigger_param>)
+                itemName = (String) configuration.get(k);
+                if (itemName == null || itemName.isBlank()) {
+                    return;
+                }
             }
-
-            final BigDecimal command = (BigDecimal) context.get("1." + arg);
-
-            if (command != null) {
+            if (v instanceof BigDecimal) {
+                final BigDecimal command = (BigDecimal) v;
                 final DecimalType outputValue = new DecimalType(command);
                 final ItemCommandEvent itemCommandEvent = ItemEventFactory.createCommandEvent(itemName, outputValue);
 
@@ -70,7 +76,7 @@ public class PIDControllerActionHandler extends BaseModuleHandler<Action> implem
             } else {
                 logger.warn(
                         "Command was not posted because either the configuration was not correct or a service was missing: ItemName: {}, Command: {}, eventPublisher: {}, ItemRegistry: {}",
-                        itemName, command, eventPublisher, itemRegistry);
+                        itemName, v, eventPublisher, itemRegistry);
             }
         });
         return null;
index c3280c1229c775555ca0b89a3921c4025c7eb41c..33a639c8c66a61aec69b13f5318bdb07334be2c3 100644 (file)
@@ -205,7 +205,7 @@ public class PIDControllerTriggerHandler extends BaseTriggerModuleHandler implem
     @Override
     public void receive(Event event) {
         if (event instanceof ItemStateChangedEvent) {
-            if (event.getTopic().equals(commandTopic.get())) {
+            if (commandTopic.isPresent() && event.getTopic().equals(commandTopic.get())) {
                 ItemStateChangedEvent changedEvent = (ItemStateChangedEvent) event;
                 if ("RESET".equals(changedEvent.getItemState().toString())) {
                     controller.setIntegralResult(0);