[pidcontroller] Fix for handling trigger input in action (#9842)

* [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>
This commit is contained in:
Hilbrand Bouwkamp 2021-01-19 10:34:49 +01:00 committed by GitHub
parent 1a64f10ae8
commit b5b787932d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 11 deletions

View File

@ -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 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;

View 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);