2 * Copyright (c) 2010-2024 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.nibeuplink.internal.handler;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.nibeuplink.internal.NibeUplinkBindingConstants;
18 import org.openhab.binding.nibeuplink.internal.model.ConfigurationException;
19 import org.openhab.core.library.types.OnOffType;
20 import org.openhab.core.thing.Channel;
21 import org.openhab.core.types.State;
22 import org.openhab.core.types.UnDefType;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * this class provides all methods which deal with channels
29 * @author Alexander Friese - initial contribution
32 public final class ChannelUtil {
33 private static final Logger logger = LoggerFactory.getLogger(ChannelUtil.class);
36 * only static methods no instance needed
38 private ChannelUtil() {
42 * checks whether the channel ha a valid numeric Id
44 * @param channel to check
45 * @return true or false
47 public static boolean isValidNibeChannel(Channel channel) {
48 String id = channel.getUID().getIdWithoutGroup();
49 return id.matches(NibeUplinkBindingConstants.VALID_CHANNEL_ID_REGEX);
53 * map data response from API to OnOff-Type. uses mapping from channel configuration
55 * @param channel switch channel which has mapping configured
56 * @param value value to map
57 * @return mapped value
59 public static State mapValue(Channel channel, String value) {
60 String off = getOffMapping(channel);
61 String on = getOnMapping(channel);
62 if (value.equals(off)) {
64 } else if (value.equals(on)) {
67 logger.warn("Channel {} value '{}' could not be mapped, valid values: ON={}, OFF={}",
68 channel.getUID().getId(), value, on, off);
69 return UnDefType.UNDEF;
74 * map data response from API to OnOff-Type. uses mapping from channel configuration
76 * @param channel switch channel which has mapping configured
77 * @param value value to map
78 * @return mapped value
80 public static State mapValue(Channel channel, long value) {
81 return mapValue(channel, String.valueOf(value));
85 * map OnOff-Type to API compatible value. uses mapping from channel configuration
87 * @param channel switch channel which has mapping configured
88 * @param value value to map
89 * @return mapped value
91 public static String mapValue(Channel channel, OnOffType value) {
92 if (value.equals(OnOffType.OFF)) {
93 return String.valueOf(getOffMapping(channel));
95 return String.valueOf(getOnMapping(channel));
100 * retrieves the validation expression which is assigned to this channel, fallback to a default, if no validation is
104 * @return the validation expression
106 public static String getValidationExpression(Channel channel) {
107 String expr = getPropertyOrParameter(channel, NibeUplinkBindingConstants.PARAMETER_NAME_VALIDATION_REGEXP);
109 logger.info("Channel {} does not have a validation expression configured", channel.getUID().getId());
110 throw new ConfigurationException(
111 "channel (" + channel.getUID().getId() + ") does not have a validation expression configured");
117 * retrieves the write API url suffix which is assigned to this channel.
120 * @return the url suffix
122 public static String getWriteApiUrlSuffix(Channel channel) {
123 String suffix = getPropertyOrParameter(channel, NibeUplinkBindingConstants.PARAMETER_NAME_WRITE_API_URL);
124 if (suffix == null) {
125 logger.info("channel {} does not have a write api url suffix configured", channel.getUID().getId());
126 throw new ConfigurationException(
127 "channel (" + channel.getUID().getId() + ") does not have a write api url suffix configured");
132 private static @Nullable String getOffMapping(Channel channel) {
133 return getPropertyOrParameter(channel, NibeUplinkBindingConstants.PARAMETER_NAME_OFF_MAPPING);
136 private static @Nullable String getOnMapping(Channel channel) {
137 return getPropertyOrParameter(channel, NibeUplinkBindingConstants.PARAMETER_NAME_ON_MAPPING);
140 private static @Nullable String getPropertyOrParameter(Channel channel, String name) {
141 String value = channel.getProperties().get(name);
142 // also eclipse says this cannot be null, it definitely can!
143 if (value == null || value.isEmpty()) {
144 Object obj = channel.getConfiguration().get(name);
145 value = obj == null ? null : obj.toString();