]> git.basschouten.com Git - openhab-addons.git/blob
aacc622185e80bbec556d75fb3ff2e4cfa671acb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.binding.velux.internal.handler;
14
15 import java.util.Map;
16 import java.util.Map.Entry;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.velux.internal.config.VeluxThingConfiguration;
20 import org.openhab.binding.velux.internal.handler.utils.ExtendedBaseThingHandler;
21 import org.openhab.binding.velux.internal.utils.Localization;
22 import org.openhab.core.config.core.Configuration;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingStatusDetail;
28 import org.openhab.core.thing.binding.BridgeHandler;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.RefreshType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /***
35  * The{@link VeluxHandler} is responsible for handling commands, which are
36  * sent via {@link VeluxBridgeHandler} to one of the channels.
37  *
38  * @author Guenther Schreiner - Initial contribution
39  */
40 @NonNullByDefault
41 public class VeluxHandler extends ExtendedBaseThingHandler {
42     private final Logger logger = LoggerFactory.getLogger(VeluxHandler.class);
43
44     VeluxThingConfiguration configuration = new VeluxThingConfiguration();
45
46     public VeluxHandler(Thing thing, Localization localization) {
47         super(thing);
48         logger.trace("VeluxHandler(thing={},localization={}) constructor called.", thing, localization);
49     }
50
51     @Override
52     public void initialize() {
53         logger.trace("initialize() called.");
54         Bridge thisBridge = getBridge();
55         logger.debug("initialize(): Initializing thing {} in combination with bridge {}.", getThing().getUID(),
56                 thisBridge);
57         if (thisBridge == null) {
58             logger.trace("initialize() updating ThingStatus to OFFLINE/CONFIGURATION_PENDING.");
59             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING);
60
61         } else if (thisBridge.getStatus() == ThingStatus.ONLINE) {
62             logger.trace("initialize() updating ThingStatus to ONLINE.");
63             updateStatus(ThingStatus.ONLINE);
64             initializeProperties();
65         } else {
66             logger.trace("initialize() updating ThingStatus to OFFLINE/BRIDGE_OFFLINE.");
67             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
68         }
69         logger.trace("initialize() done.");
70     }
71
72     private synchronized void initializeProperties() {
73         configuration = getConfigAs(VeluxThingConfiguration.class);
74         logger.trace("initializeProperties() done.");
75     }
76
77     @Override
78     public void dispose() {
79         logger.trace("dispose() called.");
80         super.dispose();
81     }
82
83     @Override
84     public void channelLinked(ChannelUID channelUID) {
85         logger.trace("channelLinked({}) called.", channelUID.getAsString());
86
87         if (thing.getStatus() == ThingStatus.ONLINE) {
88             handleCommand(channelUID, RefreshType.REFRESH);
89         }
90     }
91
92     @Override
93     public void handleCommand(ChannelUID channelUID, Command command) {
94         logger.trace("handleCommand({},{}) initiated by {}.", channelUID.getAsString(), command,
95                 Thread.currentThread());
96         Bridge bridge = getBridge();
97         if (bridge == null) {
98             logger.trace("handleCommand() nothing yet to do as there is no bridge available.");
99         } else {
100             BridgeHandler handler = bridge.getHandler();
101             if (handler == null) {
102                 logger.trace("handleCommand() nothing yet to do as thing is not initialized.");
103             } else {
104                 handler.handleCommand(channelUID, command);
105             }
106         }
107         logger.trace("handleCommand() done.");
108     }
109
110     @Override
111     public void handleConfigurationUpdate(Map<String, Object> configurationParameters) {
112         if (isInitialized()) { // prevents change of address
113             validateConfigurationParameters(configurationParameters);
114             Configuration configuration = editConfiguration();
115             for (Entry<String, Object> configurationParameter : configurationParameters.entrySet()) {
116                 logger.trace("handleConfigurationUpdate(): found modified config entry {}.",
117                         configurationParameter.getKey());
118                 configuration.put(configurationParameter.getKey(), configurationParameter.getValue());
119             }
120             // persist new configuration and reinitialize handler
121             dispose();
122             updateConfiguration(configuration);
123             initialize();
124         } else {
125             super.handleConfigurationUpdate(configurationParameters);
126         }
127     }
128 }