]> git.basschouten.com Git - openhab-addons.git/blob
be67ba05e98f7f8cb9d366c683025e0b4e1db741
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.velux.internal.VeluxBindingProperties;
21 import org.openhab.binding.velux.internal.VeluxItemType;
22 import org.openhab.binding.velux.internal.handler.utils.StateUtils;
23 import org.openhab.binding.velux.internal.handler.utils.ThingConfiguration;
24 import org.openhab.binding.velux.internal.things.VeluxScene;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.types.State;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * <B>Channel-specific retrieval and modification.</B>
32  * <P>
33  * This class implements the Channel <B>check</B> of the Thing <B>klf200</B> :
34  * <UL>
35  * <LI><I>Velux</I> <B>bridge</B> &rarr; <B>OpenHAB</B>:
36  * <P>
37  * Information retrieval by method {@link #handleRefresh}.</LI>
38  * </UL>
39  *
40  * @author Guenther Schreiner - Initial contribution.
41  */
42 @NonNullByDefault
43 final class ChannelBridgeCheck extends ChannelHandlerTemplate {
44     private static final Logger LOGGER = LoggerFactory.getLogger(ChannelBridgeCheck.class);
45
46     /*
47      * ************************
48      * ***** Constructors *****
49      */
50
51     // Suppress default constructor for non-instantiability
52
53     private ChannelBridgeCheck() {
54         throw new AssertionError();
55     }
56
57     /**
58      * Communication method to retrieve information to update the channel value.
59      *
60      * @param channelUID The item passed as type {@link ChannelUID} for which a refresh is intended.
61      * @param channelId The same item passed as type {@link String} for which a refresh is intended.
62      * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
63      *            information for this channel.
64      * @return newState The value retrieved for the passed channel, or <I>null</I> in case if there is no (new) value.
65      */
66     static @Nullable State handleRefresh(ChannelUID channelUID, String channelId,
67             VeluxBridgeHandler thisBridgeHandler) {
68         LOGGER.debug("handleRefresh({},{},{}) called.", channelUID, channelId, thisBridgeHandler);
69         State newState = null;
70         do { // just for common exit
71             LOGGER.trace("handleCommand(): loop through all existing scenes.");
72             List<String> unusedScenes = new ArrayList<>();
73             for (VeluxScene scene : thisBridgeHandler.bridgeParameters.scenes.getChannel().existingScenes.values()) {
74                 boolean found = false;
75                 LOGGER.trace("handleCommand(): .loop through all handled channels.");
76                 for (ChannelUID thisChannelUID : BridgeChannels.getAllChannelUIDs(thisBridgeHandler)) {
77                     LOGGER.trace("handleCommand(): evaluating ChannelUID {}.", thisChannelUID);
78                     VeluxItemType thisItemType = VeluxItemType.getByThingAndChannel(
79                             thisBridgeHandler.thingTypeUIDOf(thisChannelUID), thisChannelUID.getId());
80                     if (!thisItemType.equals(VeluxItemType.SCENE_ACTION)) {
81                         LOGGER.trace("handleCommand(): ignoring non SCENE_ACTION.");
82                         continue;
83                     }
84                     if (!ThingConfiguration.exists(thisBridgeHandler, thisChannelUID,
85                             VeluxBindingProperties.PROPERTY_SCENE_NAME)) {
86                         LOGGER.trace("handleCommand(): aborting processing as scene name is not set.");
87                         break;
88                     }
89                     String sceneName = (String) ThingConfiguration.getValue(thisBridgeHandler, thisChannelUID,
90                             VeluxBindingProperties.PROPERTY_SCENE_NAME);
91                     LOGGER.trace("handleCommand(): comparing {} with {}.", scene.getName().toString(), sceneName);
92                     if (scene.getName().toString().equals(sceneName)) {
93                         LOGGER.trace("handleCommand(): scene {} used within item {}.", scene.getName(),
94                                 thisChannelUID.getAsString());
95                         found = true;
96                     }
97                 }
98                 if (!found) {
99                     unusedScenes.add(scene.getName().toString());
100                     LOGGER.trace("handleCommand(): scene {} is currently unused.", scene.getName());
101                 }
102             }
103             String result;
104             if (!unusedScenes.isEmpty()) {
105                 result = thisBridgeHandler.localization.getText("channelValue.check-integrity-failed")
106                         .concat(unusedScenes.toString());
107             } else {
108                 result = thisBridgeHandler.localization.getText("channelValue.check-integrity-ok");
109             }
110             LOGGER.debug("{}", result);
111             newState = StateUtils.createState(result);
112         } while (false); // common exit
113         return newState;
114     }
115 }