Skip to content

Commit

Permalink
[pidcontroller] Fix for handling trigger input in action (openhab#9842)
Browse files Browse the repository at this point in the history
* [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>
  • Loading branch information
Hilbrand authored and thinkingstone committed Nov 7, 2021
1 parent db16f3b commit 9baf6c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,24 +53,30 @@ public PIDControllerActionHandler(Action module, ItemRegistry itemRegistry, Even

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

eventPublisher.post(itemCommandEvent);
} 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9baf6c8

Please sign in to comment.