]> git.basschouten.com Git - openhab-addons.git/blob
2b6ab190d706dba1fee2a7b09614b8b0c2e2747c
[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.ihc.internal.config;
14
15 import static org.openhab.binding.ihc.internal.IhcBindingConstants.*;
16
17 import java.math.BigDecimal;
18 import java.util.Map;
19 import java.util.function.Function;
20
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.openhab.binding.ihc.internal.ws.exeptions.ConversionException;
23 import org.openhab.core.thing.Channel;
24 import org.openhab.core.thing.type.ChannelTypeUID;
25
26 /**
27  * Class for holding channel parameterization.
28  *
29  * @author Pauli Anttila - Initial contribution
30  */
31 public class ChannelParams {
32
33     private String channelTypeId;
34     private Integer resourceId;
35     private String direction;
36     private String commandToReact;
37     private Integer pulseWidth;
38     private Boolean inverted;
39     private Long serialNumber;
40     private Integer longPressTime;
41     private Integer onLevel;
42
43     public ChannelParams(@NonNull Channel channel) throws ConversionException {
44         ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
45         if (channelTypeUID != null) {
46             channelTypeId = channelTypeUID.getId();
47         }
48
49         Map<String, Object> map = channel.getConfiguration().getProperties();
50         for (Map.Entry<String, Object> entry : map.entrySet()) {
51             switch (entry.getKey()) {
52                 case PARAM_RESOURCE_ID:
53                     resourceId = getChannelParameterAsInteger(entry.getValue());
54                     break;
55                 case PARAM_DIRECTION:
56                     direction = getChannelParameterAsString(entry.getValue());
57                     break;
58                 case PARAM_CMD_TO_REACT:
59                     commandToReact = getChannelParameterAsString(entry.getValue());
60                     break;
61                 case PARAM_SERIAL_NUMBER:
62                     serialNumber = getChannelParameterAsLong(entry.getValue());
63                     break;
64                 case PARAM_PULSE_WIDTH:
65                     pulseWidth = getChannelParameterAsInteger(entry.getValue());
66                     break;
67                 case PARAM_LONG_PRESS_TIME:
68                     longPressTime = getChannelParameterAsInteger(entry.getValue());
69                     break;
70                 case PARAM_INVERTED:
71                     inverted = getChannelParameterAsBoolean(entry.getValue());
72                     break;
73                 case PARAM_ON_LEVEL:
74                     onLevel = getChannelParameterAsInteger(entry.getValue());
75                     break;
76             }
77         }
78     }
79
80     public String getChannelTypeId() {
81         return channelTypeId;
82     }
83
84     public Integer getResourceId() {
85         return resourceId;
86     }
87
88     public String getDirection() {
89         return direction;
90     }
91
92     public boolean isDirectionBoth() {
93         return direction != null && DIRECTION_READ_WRITE.equals(direction);
94     }
95
96     public boolean isDirectionReadOnly() {
97         return direction != null && DIRECTION_READ_ONLY.equals(direction);
98     }
99
100     public boolean isDirectionWriteOnly() {
101         return direction != null && DIRECTION_WRITE_ONLY.equals(direction);
102     }
103
104     public String getCommandToReact() {
105         return commandToReact;
106     }
107
108     public Integer getPulseWidth() {
109         return pulseWidth;
110     }
111
112     public Boolean getInverted() {
113         return inverted;
114     }
115
116     public boolean isInverted() {
117         if (inverted != null) {
118             return inverted;
119         }
120         return false;
121     }
122
123     public Long getSerialNumber() {
124         return serialNumber;
125     }
126
127     public Integer getLongPressTime() {
128         return longPressTime;
129     }
130
131     public Integer getOnLevel() {
132         return onLevel;
133     }
134
135     private static Boolean getChannelParameterAsBoolean(Object value) throws ConversionException {
136         return convert(value, (v) -> (Boolean) v);
137     }
138
139     private Integer getChannelParameterAsInteger(Object value) throws ConversionException {
140         return convert(value, (v) -> ((BigDecimal) v).intValue());
141     }
142
143     private Long getChannelParameterAsLong(Object value) throws ConversionException {
144         return convert(value, (v) -> ((BigDecimal) v).longValue());
145     }
146
147     private String getChannelParameterAsString(Object value) throws ConversionException {
148         return convert(value, (v) -> (String) v);
149     }
150
151     private static <T> T convert(Object value, Function<Object, T> f) throws ConversionException {
152         if (value == null) {
153             return null;
154         }
155
156         try {
157             return f.apply(value);
158         } catch (ClassCastException e) {
159             throw new ConversionException(String.format("Failed to convert, reason %s.", e.getMessage()), e);
160         }
161     }
162
163     @Override
164     public String toString() {
165         return String.format(
166                 "[ channelTypeId=%s, resourceId=%d, direction=%s, commandToReact=%s, pulseWidth=%d, inverted=%b, serialNumber=%d, longPressTime=%d, onLevel=%d ]",
167                 channelTypeId, resourceId, direction, commandToReact, pulseWidth, inverted, serialNumber, longPressTime,
168                 onLevel);
169     }
170 }