2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.velux.internal.handler;
15 import java.util.ArrayList;
16 import java.util.List;
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;
31 * <B>Channel-specific retrieval and modification.</B>
33 * This class implements the Channel <B>check</B> of the Thing <B>klf200</B> :
35 * <LI><I>Velux</I> <B>bridge</B> → <B>OpenHAB</B>:
37 * Information retrieval by method {@link #handleRefresh}.</LI>
40 * @author Guenther Schreiner - Initial contribution.
43 final class ChannelBridgeCheck extends ChannelHandlerTemplate {
44 private static final Logger LOGGER = LoggerFactory.getLogger(ChannelBridgeCheck.class);
47 * ************************
48 * ***** Constructors *****
51 // Suppress default constructor for non-instantiability
53 private ChannelBridgeCheck() {
54 throw new AssertionError();
58 * Communication method to retrieve information to update the channel value.
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.
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.");
84 if (!ThingConfiguration.exists(thisBridgeHandler, thisChannelUID,
85 VeluxBindingProperties.PROPERTY_SCENE_NAME)) {
86 LOGGER.trace("handleCommand(): aborting processing as scene name is not set.");
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());
99 unusedScenes.add(scene.getName().toString());
100 LOGGER.trace("handleCommand(): scene {} is currently unused.", scene.getName());
104 if (!unusedScenes.isEmpty()) {
105 result = thisBridgeHandler.localization.getText("channelValue.check-integrity-failed")
106 .concat(unusedScenes.toString());
108 result = thisBridgeHandler.localization.getText("channelValue.check-integrity-ok");
110 LOGGER.debug("{}", result);
111 newState = StateUtils.createState(result);
112 } while (false); // common exit