]> git.basschouten.com Git - openhab-addons.git/blob
446d0914ac484621ccf51dc98c8149a919c9d60a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.nibeuplink.internal.handler;
14
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;
25
26 /**
27  * this class provides all methods which deal with channels
28  *
29  * @author Alexander Friese - initial contribution
30  */
31 @NonNullByDefault
32 public final class ChannelUtil {
33     private static final Logger logger = LoggerFactory.getLogger(ChannelUtil.class);
34
35     /**
36      * only static methods no instance needed
37      */
38     private ChannelUtil() {
39     }
40
41     /**
42      * checks whether the channel ha a valid numeric Id
43      *
44      * @param channel to check
45      * @return true or false
46      */
47     public static boolean isValidNibeChannel(Channel channel) {
48         String id = channel.getUID().getIdWithoutGroup();
49         return id.matches(NibeUplinkBindingConstants.VALID_CHANNEL_ID_REGEX);
50     }
51
52     /**
53      * map data response from API to OnOff-Type. uses mapping from channel configuration
54      *
55      * @param channel switch channel which has mapping configured
56      * @param value value to map
57      * @return mapped value
58      */
59     public static State mapValue(Channel channel, String value) {
60         String off = getOffMapping(channel);
61         String on = getOnMapping(channel);
62         if (value.equals(off)) {
63             return OnOffType.OFF;
64         } else if (value.equals(on)) {
65             return OnOffType.ON;
66         } else {
67             logger.warn("Channel {} value '{}' could not be mapped, valid values: ON={}, OFF={}",
68                     channel.getUID().getId(), value, on, off);
69             return UnDefType.UNDEF;
70         }
71     }
72
73     /**
74      * map data response from API to OnOff-Type. uses mapping from channel configuration
75      *
76      * @param channel switch channel which has mapping configured
77      * @param value value to map
78      * @return mapped value
79      */
80     public static State mapValue(Channel channel, long value) {
81         return mapValue(channel, String.valueOf(value));
82     }
83
84     /**
85      * map OnOff-Type to API compatible value. uses mapping from channel configuration
86      *
87      * @param channel switch channel which has mapping configured
88      * @param value value to map
89      * @return mapped value
90      */
91     public static String mapValue(Channel channel, OnOffType value) {
92         if (value.equals(OnOffType.OFF)) {
93             return String.valueOf(getOffMapping(channel));
94         } else {
95             return String.valueOf(getOnMapping(channel));
96         }
97     }
98
99     /**
100      * retrieves the validation expression which is assigned to this channel, fallback to a default, if no validation is
101      * defined.
102      *
103      * @param channel
104      * @return the validation expression
105      */
106     public static String getValidationExpression(Channel channel) {
107         String expr = getPropertyOrParameter(channel, NibeUplinkBindingConstants.PARAMETER_NAME_VALIDATION_REGEXP);
108         if (expr == null) {
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");
112         }
113         return expr;
114     }
115
116     /**
117      * retrieves the write API url suffix which is assigned to this channel.
118      *
119      * @param channel
120      * @return the url suffix
121      */
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");
128         }
129         return suffix;
130     }
131
132     private static @Nullable String getOffMapping(Channel channel) {
133         return getPropertyOrParameter(channel, NibeUplinkBindingConstants.PARAMETER_NAME_OFF_MAPPING);
134     }
135
136     private static @Nullable String getOnMapping(Channel channel) {
137         return getPropertyOrParameter(channel, NibeUplinkBindingConstants.PARAMETER_NAME_ON_MAPPING);
138     }
139
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();
146         }
147         return value;
148     }
149 }