[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:
parent
1a64f10ae8
commit
b5b787932d
@ -12,17 +12,17 @@
|
|||||||
*/
|
*/
|
||||||
package org.openhab.automation.pidcontroller.internal.handler;
|
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.math.BigDecimal;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.openhab.core.automation.Action;
|
import org.openhab.core.automation.Action;
|
||||||
import org.openhab.core.automation.handler.ActionHandler;
|
import org.openhab.core.automation.handler.ActionHandler;
|
||||||
import org.openhab.core.automation.handler.BaseModuleHandler;
|
import org.openhab.core.automation.handler.BaseModuleHandler;
|
||||||
|
import org.openhab.core.config.core.Configuration;
|
||||||
import org.openhab.core.events.EventPublisher;
|
import org.openhab.core.events.EventPublisher;
|
||||||
import org.openhab.core.items.ItemRegistry;
|
import org.openhab.core.items.ItemRegistry;
|
||||||
import org.openhab.core.items.events.ItemCommandEvent;
|
import org.openhab.core.items.events.ItemCommandEvent;
|
||||||
@ -53,16 +53,22 @@ public class PIDControllerActionHandler extends BaseModuleHandler<Action> implem
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nullable Map<String, Object> execute(Map<String, Object> context) {
|
public @Nullable Map<String, Object> execute(Map<String, Object> context) {
|
||||||
Stream.of(OUTPUT, P_INSPECTOR, I_INSPECTOR, D_INSPECTOR, E_INSPECTOR).forEach(arg -> {
|
final Configuration configuration = module.getConfiguration();
|
||||||
final String itemName = (String) module.getConfiguration().get(arg);
|
|
||||||
|
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()) {
|
if (itemName == null || itemName.isBlank()) {
|
||||||
return;
|
// try original key name (<triggername>.<trigger_param>)
|
||||||
|
itemName = (String) configuration.get(k);
|
||||||
|
if (itemName == null || itemName.isBlank()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (v instanceof BigDecimal) {
|
||||||
final BigDecimal command = (BigDecimal) context.get("1." + arg);
|
final BigDecimal command = (BigDecimal) v;
|
||||||
|
|
||||||
if (command != null) {
|
|
||||||
final DecimalType outputValue = new DecimalType(command);
|
final DecimalType outputValue = new DecimalType(command);
|
||||||
final ItemCommandEvent itemCommandEvent = ItemEventFactory.createCommandEvent(itemName, outputValue);
|
final ItemCommandEvent itemCommandEvent = ItemEventFactory.createCommandEvent(itemName, outputValue);
|
||||||
|
|
||||||
@ -70,7 +76,7 @@ public class PIDControllerActionHandler extends BaseModuleHandler<Action> implem
|
|||||||
} else {
|
} else {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
"Command was not posted because either the configuration was not correct or a service was missing: ItemName: {}, Command: {}, eventPublisher: {}, ItemRegistry: {}",
|
"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;
|
return null;
|
||||||
|
|||||||
@ -205,7 +205,7 @@ public class PIDControllerTriggerHandler extends BaseTriggerModuleHandler implem
|
|||||||
@Override
|
@Override
|
||||||
public void receive(Event event) {
|
public void receive(Event event) {
|
||||||
if (event instanceof ItemStateChangedEvent) {
|
if (event instanceof ItemStateChangedEvent) {
|
||||||
if (event.getTopic().equals(commandTopic.get())) {
|
if (commandTopic.isPresent() && event.getTopic().equals(commandTopic.get())) {
|
||||||
ItemStateChangedEvent changedEvent = (ItemStateChangedEvent) event;
|
ItemStateChangedEvent changedEvent = (ItemStateChangedEvent) event;
|
||||||
if ("RESET".equals(changedEvent.getItemState().toString())) {
|
if ("RESET".equals(changedEvent.getItemState().toString())) {
|
||||||
controller.setIntegralResult(0);
|
controller.setIntegralResult(0);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user