]> git.basschouten.com Git - openhab-addons.git/blob
3e814f76797e4bfa6d6a320481902d9fde98e9cb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.automation.pidcontroller.internal.handler;
14
15 import static org.openhab.automation.pidcontroller.internal.PIDControllerConstants.*;
16
17 import java.math.BigDecimal;
18 import java.util.Map;
19 import java.util.stream.Stream;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.automation.Action;
24 import org.openhab.core.automation.handler.ActionHandler;
25 import org.openhab.core.automation.handler.BaseModuleHandler;
26 import org.openhab.core.events.EventPublisher;
27 import org.openhab.core.items.ItemRegistry;
28 import org.openhab.core.items.events.ItemCommandEvent;
29 import org.openhab.core.items.events.ItemEventFactory;
30 import org.openhab.core.library.types.DecimalType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  *
36  * @author Hilbrand Bouwkamp - Initial Contribution
37  * @author Fabian Wolter - Add PID debugging items
38  */
39 @NonNullByDefault
40 public class PIDControllerActionHandler extends BaseModuleHandler<Action> implements ActionHandler {
41     public static final String MODULE_TYPE_ID = AUTOMATION_NAME + ".action";
42
43     private final Logger logger = LoggerFactory.getLogger(PIDControllerActionHandler.class);
44
45     private ItemRegistry itemRegistry;
46     private EventPublisher eventPublisher;
47
48     public PIDControllerActionHandler(Action module, ItemRegistry itemRegistry, EventPublisher eventPublisher) {
49         super(module);
50         this.itemRegistry = itemRegistry;
51         this.eventPublisher = eventPublisher;
52     }
53
54     @Override
55     public @Nullable Map<String, Object> execute(Map<String, Object> context) {
56         Stream.of(OUTPUT, P_INSPECTOR, I_INSPECTOR, D_INSPECTOR, E_INSPECTOR).forEach(arg -> {
57             final String itemName = (String) module.getConfiguration().get(arg);
58
59             if (itemName == null || itemName.isBlank()) {
60                 return;
61             }
62
63             final BigDecimal command = (BigDecimal) context.get("1." + arg);
64
65             if (command != null) {
66                 final DecimalType outputValue = new DecimalType(command);
67                 final ItemCommandEvent itemCommandEvent = ItemEventFactory.createCommandEvent(itemName, outputValue);
68
69                 eventPublisher.post(itemCommandEvent);
70             } else {
71                 logger.warn(
72                         "Command was not posted because either the configuration was not correct or a service was missing: ItemName: {}, Command: {}, eventPublisher: {}, ItemRegistry: {}",
73                         itemName, command, eventPublisher, itemRegistry);
74             }
75         });
76         return null;
77     }
78 }