From 9baf6c8d385307fc8b6af2d394c49cbac5f3773f Mon Sep 17 00:00:00 2001 From: Hilbrand Bouwkamp Date: Tue, 19 Jan 2021 10:34:49 +0100 Subject: [PATCH] [pidcontroller] Fix for handling trigger input in action (#9842) * [pidcontroller] Catch empty commandTopic Signed-off-by: Hilbrand Bouwkamp * [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 * [pidcontroller] review comment Signed-off-by: Hilbrand Bouwkamp --- .../handler/PIDControllerActionHandler.java | 26 ++++++++++++------- .../handler/PIDControllerTriggerHandler.java | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/bundles/org.openhab.automation.pidcontroller/src/main/java/org/openhab/automation/pidcontroller/internal/handler/PIDControllerActionHandler.java b/bundles/org.openhab.automation.pidcontroller/src/main/java/org/openhab/automation/pidcontroller/internal/handler/PIDControllerActionHandler.java index 11bcb37855544..de9c7030ca831 100644 --- a/bundles/org.openhab.automation.pidcontroller/src/main/java/org/openhab/automation/pidcontroller/internal/handler/PIDControllerActionHandler.java +++ b/bundles/org.openhab.automation.pidcontroller/src/main/java/org/openhab/automation/pidcontroller/internal/handler/PIDControllerActionHandler.java @@ -12,17 +12,17 @@ */ 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 PIDControllerActionHandler(Action module, ItemRegistry itemRegistry, Even @Override public @Nullable Map execute(Map 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 (.) + 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 PIDControllerActionHandler(Action module, ItemRegistry itemRegistry, Even } 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; diff --git a/bundles/org.openhab.automation.pidcontroller/src/main/java/org/openhab/automation/pidcontroller/internal/handler/PIDControllerTriggerHandler.java b/bundles/org.openhab.automation.pidcontroller/src/main/java/org/openhab/automation/pidcontroller/internal/handler/PIDControllerTriggerHandler.java index c3280c1229c77..33a639c8c66a6 100644 --- a/bundles/org.openhab.automation.pidcontroller/src/main/java/org/openhab/automation/pidcontroller/internal/handler/PIDControllerTriggerHandler.java +++ b/bundles/org.openhab.automation.pidcontroller/src/main/java/org/openhab/automation/pidcontroller/internal/handler/PIDControllerTriggerHandler.java @@ -205,7 +205,7 @@ private double getItemValueAsNumber(Item item) throws PIDException { @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);